]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 2011-2024 Free Software Foundation, Inc. |
db6af3eb | 2 | This file is part of the GNU C Library. |
db6af3eb UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
db6af3eb UD |
17 | |
18 | #include <assert.h> | |
19 | #include <dlfcn.h> | |
20 | #include <errno.h> | |
21 | #include <gconv.h> | |
22 | #include <uchar.h> | |
23 | #include <wcsmbsload.h> | |
24 | ||
88f4b692 | 25 | #include <pointer_guard.h> |
db6af3eb UD |
26 | |
27 | #ifndef EILSEQ | |
28 | # define EILSEQ EINVAL | |
29 | #endif | |
30 | ||
db6af3eb UD |
31 | |
32 | /* This is the private state used if PS is NULL. */ | |
33 | static mbstate_t state; | |
34 | ||
35 | size_t | |
36 | mbrtoc16 (char16_t *pc16, const char *s, size_t n, mbstate_t *ps) | |
37 | { | |
9954432e UD |
38 | if (ps == NULL) |
39 | ps = &state; | |
40 | ||
d3ed7225 UD |
41 | /* The standard text does not say that S being NULL means the state |
42 | is reset even if the second half of a surrogate still have to be | |
43 | returned. In fact, the error code description indicates | |
44 | otherwise. Therefore always first try to return a second | |
45 | half. */ | |
9954432e UD |
46 | if (ps->__count & 0x80000000) |
47 | { | |
48 | /* We have to return the second word for a surrogate. */ | |
49 | ps->__count &= 0x7fffffff; | |
50 | *pc16 = ps->__value.__wch; | |
51 | ps->__value.__wch = L'\0'; | |
52 | return (size_t) -3; | |
53 | } | |
54 | ||
d3ed7225 | 55 | wchar_t wc; |
db6af3eb UD |
56 | struct __gconv_step_data data; |
57 | int status; | |
58 | size_t result; | |
59 | size_t dummy; | |
60 | const unsigned char *inbuf, *endbuf; | |
d3ed7225 | 61 | unsigned char *outbuf = (unsigned char *) &wc; |
db6af3eb UD |
62 | const struct gconv_fcts *fcts; |
63 | ||
64 | /* Set information for this step. */ | |
65 | data.__invocation_counter = 0; | |
66 | data.__internal_use = 1; | |
67 | data.__flags = __GCONV_IS_LAST; | |
9954432e | 68 | data.__statep = ps; |
db6af3eb UD |
69 | |
70 | /* A first special case is if S is NULL. This means put PS in the | |
71 | initial state. */ | |
72 | if (s == NULL) | |
73 | { | |
d3ed7225 | 74 | pc16 = NULL; |
db6af3eb UD |
75 | s = ""; |
76 | n = 1; | |
77 | } | |
78 | ||
9781a370 OB |
79 | if (n == 0) |
80 | return (size_t) -2; | |
81 | ||
db6af3eb UD |
82 | /* Tell where we want the result. */ |
83 | data.__outbuf = outbuf; | |
d3ed7225 | 84 | data.__outbufend = outbuf + sizeof (wchar_t); |
db6af3eb UD |
85 | |
86 | /* Get the conversion functions. */ | |
87 | fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE)); | |
88 | ||
89 | /* Do a normal conversion. */ | |
90 | inbuf = (const unsigned char *) s; | |
91 | endbuf = inbuf + n; | |
a1ffb40e | 92 | if (__glibc_unlikely (endbuf < inbuf)) |
d3ed7225 UD |
93 | { |
94 | endbuf = (const unsigned char *) ~(uintptr_t) 0; | |
95 | if (endbuf == inbuf) | |
96 | goto ilseq; | |
97 | } | |
98 | __gconv_fct fct = fcts->towc->__fct; | |
d3ed7225 | 99 | if (fcts->towc->__shlib_handle != NULL) |
db6af3eb | 100 | PTR_DEMANGLE (fct); |
9954432e | 101 | |
d3ed7225 | 102 | status = DL_CALL_FCT (fct, (fcts->towc, &data, &inbuf, endbuf, |
db6af3eb UD |
103 | NULL, &dummy, 0, 1)); |
104 | ||
105 | /* There must not be any problems with the conversion but illegal input | |
106 | characters. The output buffer must be large enough, otherwise the | |
107 | definition of MB_CUR_MAX is not correct. All the other possible | |
108 | errors also must not happen. */ | |
109 | assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT | |
110 | || status == __GCONV_ILLEGAL_INPUT | |
111 | || status == __GCONV_INCOMPLETE_INPUT | |
112 | || status == __GCONV_FULL_OUTPUT); | |
113 | ||
114 | if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT | |
115 | || status == __GCONV_FULL_OUTPUT) | |
116 | { | |
d3ed7225 | 117 | result = inbuf - (const unsigned char *) s; |
9954432e | 118 | |
d3ed7225 | 119 | if (wc < 0x10000) |
db6af3eb | 120 | { |
d3ed7225 UD |
121 | if (pc16 != NULL) |
122 | *pc16 = wc; | |
123 | ||
124 | if (data.__outbuf != outbuf && wc == L'\0') | |
125 | { | |
126 | /* The converted character is the NUL character. */ | |
127 | assert (__mbsinit (data.__statep)); | |
128 | result = 0; | |
129 | } | |
db6af3eb UD |
130 | } |
131 | else | |
9954432e | 132 | { |
d3ed7225 UD |
133 | /* This is a surrogate. */ |
134 | if (pc16 != NULL) | |
135 | *pc16 = 0xd7c0 + (wc >> 10); | |
9954432e | 136 | |
d3ed7225 UD |
137 | ps->__count |= 0x80000000; |
138 | ps->__value.__wch = 0xdc00 + (wc & 0x3ff); | |
9954432e | 139 | } |
db6af3eb UD |
140 | } |
141 | else if (status == __GCONV_INCOMPLETE_INPUT) | |
142 | result = (size_t) -2; | |
143 | else | |
144 | { | |
d3ed7225 | 145 | ilseq: |
db6af3eb UD |
146 | result = (size_t) -1; |
147 | __set_errno (EILSEQ); | |
148 | } | |
149 | ||
150 | return result; | |
151 | } |