Debian Codegraph @

lib/iconv.c

raw
1 /* Character set conversion.
2 Copyright (C) 1999-2001, 2007, 2009-2025 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file 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
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 /* Specification. */
20 #include <iconv.h>
21
22 #include <stddef.h>
23
24 #if REPLACE_ICONV_UTF
25 # include <errno.h>
26 # include <stdint.h>
27 # include <stdlib.h>
28 # include "unistr.h"
29 #endif
30
31 #if REPLACE_ICONV_UTF
32
33 /* UTF-{16,32}{BE,LE} converters taken from GNU libiconv 1.11. */
34
35 /* Return code if invalid. (xxx_mbtowc) */
36 # define RET_ILSEQ -1
37 /* Return code if no bytes were read. (xxx_mbtowc) */
38 # define RET_TOOFEW -2
39
40 /* Return code if invalid. (xxx_wctomb) */
41 # define RET_ILUNI -1
42 /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
43 # define RET_TOOSMALL -2
44
45 /*
46 * UTF-16BE
47 */
48
49 /* Specification: RFC 2781 */
50
51 static int
52 utf16be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
53 {
54 if (n >= 2)
55 {
56 ucs4_t wc = (s[0] << 8) + s[1];
57 if (wc >= 0xd800 && wc < 0xdc00)
58 {
59 if (n >= 4)
60 {
61 ucs4_t wc2 = (s[2] << 8) + s[3];
62 if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
63 return RET_ILSEQ;
64 *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
65 return 4;
66 }
67 }
68 else if (wc >= 0xdc00 && wc < 0xe000)
69 {
70 return RET_ILSEQ;
71 }
72 else
73 {
74 *pwc = wc;
75 return 2;
76 }
77 }
78 return RET_TOOFEW;
79 }
80
81 static int
82 utf16be_wctomb (unsigned char *r, ucs4_t wc, size_t n)
83 {
84 if (!(wc >= 0xd800 && wc < 0xe000))
85 {
86 if (wc < 0x10000)
87 {
88 if (n >= 2)
89 {
90 r[0] = (unsigned char) (wc >> 8);
91 r[1] = (unsigned char) wc;
92 return 2;
93 }
94 else
95 return RET_TOOSMALL;
96 }
97 else if (wc < 0x110000)
98 {
99 if (n >= 4)
100 {
101 ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
102 ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
103 r[0] = (unsigned char) (wc1 >> 8);
104 r[1] = (unsigned char) wc1;
105 r[2] = (unsigned char) (wc2 >> 8);
106 r[3] = (unsigned char) wc2;
107 return 4;
108 }
109 else
110 return RET_TOOSMALL;
111 }
112 }
113 return RET_ILUNI;
114 }
115
116 /*
117 * UTF-16LE
118 */
119
120 /* Specification: RFC 2781 */
121
122 static int
123 utf16le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
124 {
125 if (n >= 2)
126 {
127 ucs4_t wc = s[0] + (s[1] << 8);
128 if (wc >= 0xd800 && wc < 0xdc00)
129 {
130 if (n >= 4)
131 {
132 ucs4_t wc2 = s[2] + (s[3] << 8);
133 if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
134 return RET_ILSEQ;
135 *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
136 return 4;
137 }
138 }
139 else if (wc >= 0xdc00 && wc < 0xe000)
140 {
141 return RET_ILSEQ;
142 }
143 else
144 {
145 *pwc = wc;
146 return 2;
147 }
148 }
149 return RET_TOOFEW;
150 }
151
152 static int
153 utf16le_wctomb (unsigned char *r, ucs4_t wc, size_t n)
154 {
155 if (!(wc >= 0xd800 && wc < 0xe000))
156 {
157 if (wc < 0x10000)
158 {
159 if (n >= 2)
160 {
161 r[0] = (unsigned char) wc;
162 r[1] = (unsigned char) (wc >> 8);
163 return 2;
164 }
165 else
166 return RET_TOOSMALL;
167 }
168 else if (wc < 0x110000)
169 {
170 if (n >= 4)
171 {
172 ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
173 ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
174 r[0] = (unsigned char) wc1;
175 r[1] = (unsigned char) (wc1 >> 8);
176 r[2] = (unsigned char) wc2;
177 r[3] = (unsigned char) (wc2 >> 8);
178 return 4;
179 }
180 else
181 return RET_TOOSMALL;
182 }
183 }
184 return RET_ILUNI;
185 }
186
187 /*
188 * UTF-32BE
189 */
190
191 /* Specification: Unicode 3.1 Standard Annex #19 */
192
193 static int
194 utf32be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
195 {
196 if (n >= 4)
197 {
198 ucs4_t wc = ((ucs4_t) s[0] << 24)
199 + ((ucs4_t) s[1] << 16)
200 + ((ucs4_t) s[2] << 8)
201 + (ucs4_t) s[3];
202 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
203 {
204 *pwc = wc;
205 return 4;
206 }
207 else
208 return RET_ILSEQ;
209 }
210 return RET_TOOFEW;
211 }
212
213 static int
214 utf32be_wctomb (unsigned char *r, ucs4_t wc, size_t n)
215 {
216 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
217 {
218 if (n >= 4)
219 {
220 r[0] = 0;
221 r[1] = (unsigned char) (wc >> 16);
222 r[2] = (unsigned char) (wc >> 8);
223 r[3] = (unsigned char) wc;
224 return 4;
225 }
226 else
227 return RET_TOOSMALL;
228 }
229 return RET_ILUNI;
230 }
231
232 /*
233 * UTF-32LE
234 */
235
236 /* Specification: Unicode 3.1 Standard Annex #19 */
237
238 static int
239 utf32le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
240 {
241 if (n >= 4)
242 {
243 ucs4_t wc = (ucs4_t) s[0]
244 + ((ucs4_t) s[1] << 8)
245 + ((ucs4_t) s[2] << 16)
246 + ((ucs4_t) s[3] << 24);
247 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
248 {
249 *pwc = wc;
250 return 4;
251 }
252 else
253 return RET_ILSEQ;
254 }
255 return RET_TOOFEW;
256 }
257
258 static int
259 utf32le_wctomb (unsigned char *r, ucs4_t wc, size_t n)
260 {
261 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
262 {
263 if (n >= 4)
264 {
265 r[0] = (unsigned char) wc;
266 r[1] = (unsigned char) (wc >> 8);
267 r[2] = (unsigned char) (wc >> 16);
268 r[3] = 0;
269 return 4;
270 }
271 else
272 return RET_TOOSMALL;
273 }
274 return RET_ILUNI;
275 }
276
277 #endif
278
279 size_t
280 rpl_iconv (iconv_t cd,
281 ICONV_CONST char **inbuf, size_t *inbytesleft,
282 char **outbuf, size_t *outbytesleft)
283 #undef iconv
284 {
285 #if REPLACE_ICONV_UTF
286 switch ((uintptr_t) cd)
287 {
288 {
289 int (*xxx_wctomb) (unsigned char *, ucs4_t, size_t);
290
291 case (uintptr_t) _ICONV_UTF8_UTF16BE:
292 xxx_wctomb = utf16be_wctomb;
293 goto loop_from_utf8;
294 case (uintptr_t) _ICONV_UTF8_UTF16LE:
295 xxx_wctomb = utf16le_wctomb;
296 goto loop_from_utf8;
297 case (uintptr_t) _ICONV_UTF8_UTF32BE:
298 xxx_wctomb = utf32be_wctomb;
299 goto loop_from_utf8;
300 case (uintptr_t) _ICONV_UTF8_UTF32LE:
301 xxx_wctomb = utf32le_wctomb;
302 goto loop_from_utf8;
303
304 loop_from_utf8:
305 if (inbuf == NULL || *inbuf == NULL)
306 return 0;
307 {
308 ICONV_CONST char *inptr = *inbuf;
309 size_t inleft = *inbytesleft;
310 char *outptr = *outbuf;
311 size_t outleft = *outbytesleft;
312 size_t res = 0;
313 while (inleft > 0)
314 {
315 ucs4_t uc;
316 int m = u8_mbtoucr (&uc, (const uint8_t *) inptr, inleft);
317 if (m <= 0)
318 {
319 if (m == -1)
320 {
321 errno = EILSEQ;
322 res = (size_t)(-1);
323 break;
324 }
325 if (m == -2)
326 {
327 errno = EINVAL;
328 res = (size_t)(-1);
329 break;
330 }
331 abort ();
332 }
333 else
334 {
335 int n = xxx_wctomb ((uint8_t *) outptr, uc, outleft);
336 if (n < 0)
337 {
338 if (n == RET_ILUNI)
339 {
340 errno = EILSEQ;
341 res = (size_t)(-1);
342 break;
343 }
344 if (n == RET_TOOSMALL)
345 {
346 errno = E2BIG;
347 res = (size_t)(-1);
348 break;
349 }
350 abort ();
351 }
352 else
353 {
354 inptr += m;
355 inleft -= m;
356 outptr += n;
357 outleft -= n;
358 }
359 }
360 }
361 *inbuf = inptr;
362 *inbytesleft = inleft;
363 *outbuf = outptr;
364 *outbytesleft = outleft;
365 return res;
366 }
367 }
368
369 {
370 int (*xxx_mbtowc) (ucs4_t *, const unsigned char *, size_t);
371
372 case (uintptr_t) _ICONV_UTF16BE_UTF8:
373 xxx_mbtowc = utf16be_mbtowc;
374 goto loop_to_utf8;
375 case (uintptr_t) _ICONV_UTF16LE_UTF8:
376 xxx_mbtowc = utf16le_mbtowc;
377 goto loop_to_utf8;
378 case (uintptr_t) _ICONV_UTF32BE_UTF8:
379 xxx_mbtowc = utf32be_mbtowc;
380 goto loop_to_utf8;
381 case (uintptr_t) _ICONV_UTF32LE_UTF8:
382 xxx_mbtowc = utf32le_mbtowc;
383 goto loop_to_utf8;
384
385 loop_to_utf8:
386 if (inbuf == NULL || *inbuf == NULL)
387 return 0;
388 {
389 ICONV_CONST char *inptr = *inbuf;
390 size_t inleft = *inbytesleft;
391 char *outptr = *outbuf;
392 size_t outleft = *outbytesleft;
393 size_t res = 0;
394 while (inleft > 0)
395 {
396 ucs4_t uc;
397 int m = xxx_mbtowc (&uc, (const uint8_t *) inptr, inleft);
398 if (m <= 0)
399 {
400 if (m == RET_ILSEQ)
401 {
402 errno = EILSEQ;
403 res = (size_t)(-1);
404 break;
405 }
406 if (m == RET_TOOFEW)
407 {
408 errno = EINVAL;
409 res = (size_t)(-1);
410 break;
411 }
412 abort ();
413 }
414 else
415 {
416 int n = u8_uctomb ((uint8_t *) outptr, uc, outleft);
417 if (n < 0)
418 {
419 if (n == -1)
420 {
421 errno = EILSEQ;
422 res = (size_t)(-1);
423 break;
424 }
425 if (n == -2)
426 {
427 errno = E2BIG;
428 res = (size_t)(-1);
429 break;
430 }
431 abort ();
432 }
433 else
434 {
435 inptr += m;
436 inleft -= m;
437 outptr += n;
438 outleft -= n;
439 }
440 }
441 }
442 *inbuf = inptr;
443 *inbytesleft = inleft;
444 *outbuf = outptr;
445 *outbytesleft = outleft;
446 return res;
447 }
448 }
449 }
450 #endif
451 return iconv (cd, inbuf, inbytesleft, outbuf, outbytesleft);
452 }