Fix bug in firstversions.awk version range handling.
[glibc.git] / wcsmbs / mbrtoc16.c
blobf5ed2b4ac97c87de53f480479fa38bb829f6c9e9
1 /* Copyright (C) 2011, 2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <gconv.h>
24 #include <uchar.h>
25 #include <wcsmbsload.h>
27 #include <sysdep.h>
29 #ifndef EILSEQ
30 # define EILSEQ EINVAL
31 #endif
34 /* This is the private state used if PS is NULL. */
35 static mbstate_t state;
37 size_t
38 mbrtoc16 (char16_t *pc16, const char *s, size_t n, mbstate_t *ps)
40 if (ps == NULL)
41 ps = &state;
43 /* The standard text does not say that S being NULL means the state
44 is reset even if the second half of a surrogate still have to be
45 returned. In fact, the error code description indicates
46 otherwise. Therefore always first try to return a second
47 half. */
48 if (ps->__count & 0x80000000)
50 /* We have to return the second word for a surrogate. */
51 ps->__count &= 0x7fffffff;
52 *pc16 = ps->__value.__wch;
53 ps->__value.__wch = L'\0';
54 return (size_t) -3;
57 wchar_t wc;
58 struct __gconv_step_data data;
59 int status;
60 size_t result;
61 size_t dummy;
62 const unsigned char *inbuf, *endbuf;
63 unsigned char *outbuf = (unsigned char *) &wc;
64 const struct gconv_fcts *fcts;
66 /* Set information for this step. */
67 data.__invocation_counter = 0;
68 data.__internal_use = 1;
69 data.__flags = __GCONV_IS_LAST;
70 data.__statep = ps;
71 data.__trans = NULL;
73 /* A first special case is if S is NULL. This means put PS in the
74 initial state. */
75 if (s == NULL)
77 pc16 = NULL;
78 s = "";
79 n = 1;
82 /* Tell where we want the result. */
83 data.__outbuf = outbuf;
84 data.__outbufend = outbuf + sizeof (wchar_t);
86 /* Get the conversion functions. */
87 fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
89 /* Do a normal conversion. */
90 inbuf = (const unsigned char *) s;
91 endbuf = inbuf + n;
92 if (__builtin_expect (endbuf < inbuf, 0))
94 endbuf = (const unsigned char *) ~(uintptr_t) 0;
95 if (endbuf == inbuf)
96 goto ilseq;
98 __gconv_fct fct = fcts->towc->__fct;
99 #ifdef PTR_DEMANGLE
100 if (fcts->towc->__shlib_handle != NULL)
101 PTR_DEMANGLE (fct);
102 #endif
104 status = DL_CALL_FCT (fct, (fcts->towc, &data, &inbuf, endbuf,
105 NULL, &dummy, 0, 1));
107 /* There must not be any problems with the conversion but illegal input
108 characters. The output buffer must be large enough, otherwise the
109 definition of MB_CUR_MAX is not correct. All the other possible
110 errors also must not happen. */
111 assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
112 || status == __GCONV_ILLEGAL_INPUT
113 || status == __GCONV_INCOMPLETE_INPUT
114 || status == __GCONV_FULL_OUTPUT);
116 if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
117 || status == __GCONV_FULL_OUTPUT)
119 result = inbuf - (const unsigned char *) s;
121 if (wc < 0x10000)
123 if (pc16 != NULL)
124 *pc16 = wc;
126 if (data.__outbuf != outbuf && wc == L'\0')
128 /* The converted character is the NUL character. */
129 assert (__mbsinit (data.__statep));
130 result = 0;
133 else
135 /* This is a surrogate. */
136 if (pc16 != NULL)
137 *pc16 = 0xd7c0 + (wc >> 10);
139 ps->__count |= 0x80000000;
140 ps->__value.__wch = 0xdc00 + (wc & 0x3ff);
143 else if (status == __GCONV_INCOMPLETE_INPUT)
144 result = (size_t) -2;
145 else
147 ilseq:
148 result = (size_t) -1;
149 __set_errno (EILSEQ);
152 return result;