1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | minimal iconv implementation
|
---|
4 | Copyright (C) Andrew Tridgell 2001
|
---|
5 | Copyright (C) Jelmer Vernooij 2002,2003
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * We have to use strcasecmp here as the character conversions
|
---|
25 | * haven't been initialised yet. JRA.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #undef strcasecmp
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @file
|
---|
32 | *
|
---|
33 | * @brief Samba wrapper/stub for iconv character set conversion.
|
---|
34 | *
|
---|
35 | * iconv is the XPG2 interface for converting between character
|
---|
36 | * encodings. This file provides a Samba wrapper around it, and also
|
---|
37 | * a simple reimplementation that is used if the system does not
|
---|
38 | * implement iconv.
|
---|
39 | *
|
---|
40 | * Samba only works with encodings that are supersets of ASCII: ascii
|
---|
41 | * characters like whitespace can be tested for directly, multibyte
|
---|
42 | * sequences start with a byte with the high bit set, and strings are
|
---|
43 | * terminated by a nul byte.
|
---|
44 | *
|
---|
45 | * Note that the only function provided by iconv is conversion between
|
---|
46 | * characters. It doesn't directly support operations like
|
---|
47 | * uppercasing or comparison. We have to convert to UCS-2 and compare
|
---|
48 | * there.
|
---|
49 | *
|
---|
50 | * @sa Samba Developers Guide
|
---|
51 | **/
|
---|
52 |
|
---|
53 | static_decl_charset;
|
---|
54 |
|
---|
55 | static size_t ascii_pull(void *,const char **, size_t *, char **, size_t *);
|
---|
56 | static size_t ascii_push(void *,const char **, size_t *, char **, size_t *);
|
---|
57 | static size_t latin1_push(void *,const char **, size_t *, char **, size_t *);
|
---|
58 | static size_t utf8_pull(void *,const char **, size_t *, char **, size_t *);
|
---|
59 | static size_t utf8_push(void *,const char **, size_t *, char **, size_t *);
|
---|
60 | static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
|
---|
61 | static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
|
---|
62 | static size_t iconv_copy(void *,const char **, size_t *, char **, size_t *);
|
---|
63 | static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
|
---|
64 |
|
---|
65 | static struct charset_functions builtin_functions[] = {
|
---|
66 | /* windows is really neither UCS-2 not UTF-16 */
|
---|
67 | {"UCS-2LE", iconv_copy, iconv_copy},
|
---|
68 | {"UTF-16LE", iconv_copy, iconv_copy},
|
---|
69 | {"UCS-2BE", iconv_swab, iconv_swab},
|
---|
70 | {"UTF-16BE", iconv_swab, iconv_swab},
|
---|
71 |
|
---|
72 | /* we include the UTF-8 alias to cope with differing locale settings */
|
---|
73 | {"UTF8", utf8_pull, utf8_push},
|
---|
74 | {"UTF-8", utf8_pull, utf8_push},
|
---|
75 | {"ASCII", ascii_pull, ascii_push},
|
---|
76 | {"646", ascii_pull, ascii_push},
|
---|
77 | {"ISO-8859-1", ascii_pull, latin1_push},
|
---|
78 | {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
|
---|
79 | {NULL, NULL, NULL}
|
---|
80 | };
|
---|
81 |
|
---|
82 | static struct charset_functions *charsets = NULL;
|
---|
83 |
|
---|
84 | static struct charset_functions *find_charset_functions(const char *name)
|
---|
85 | {
|
---|
86 | struct charset_functions *c = charsets;
|
---|
87 |
|
---|
88 | while(c) {
|
---|
89 | if (strcasecmp(name, c->name) == 0) {
|
---|
90 | return c;
|
---|
91 | }
|
---|
92 | c = c->next;
|
---|
93 | }
|
---|
94 |
|
---|
95 | return NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | NTSTATUS smb_register_charset(struct charset_functions *funcs)
|
---|
99 | {
|
---|
100 | if (!funcs) {
|
---|
101 | return NT_STATUS_INVALID_PARAMETER;
|
---|
102 | }
|
---|
103 |
|
---|
104 | DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
|
---|
105 | /* Check whether we already have this charset... */
|
---|
106 | if (find_charset_functions(funcs->name)) {
|
---|
107 | DEBUG(0, ("Duplicate charset %s, not registering\n", funcs->name));
|
---|
108 | return NT_STATUS_OBJECT_NAME_COLLISION;
|
---|
109 | }
|
---|
110 |
|
---|
111 | funcs->next = funcs->prev = NULL;
|
---|
112 | DEBUG(5, ("Registered charset %s\n", funcs->name));
|
---|
113 | DLIST_ADD(charsets, funcs);
|
---|
114 | return NT_STATUS_OK;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static void lazy_initialize_iconv(void)
|
---|
118 | {
|
---|
119 | static bool initialized;
|
---|
120 | int i;
|
---|
121 |
|
---|
122 | if (!initialized) {
|
---|
123 | initialized = True;
|
---|
124 | for(i = 0; builtin_functions[i].name; i++)
|
---|
125 | smb_register_charset(&builtin_functions[i]);
|
---|
126 | static_init_charset;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | #if defined(__OS2__) && defined(__INNOTEK_LIBC__)
|
---|
131 | #include <uconv.h>
|
---|
132 |
|
---|
133 | typedef struct os2_iconv_t
|
---|
134 | {
|
---|
135 | UconvObject from;
|
---|
136 | } os2_iconv_t;
|
---|
137 |
|
---|
138 | iconv_t os2_iconv_open (const char *tocode, const char *fromcode)
|
---|
139 | {
|
---|
140 | os2_iconv_t *os2_cd = (os2_iconv_t *)iconv_open(tocode, fromcode);
|
---|
141 |
|
---|
142 | if (os2_cd != (iconv_t)(-1))
|
---|
143 | {
|
---|
144 | /* Assume strings contain pathnames */
|
---|
145 | uconv_attribute_t attr;
|
---|
146 |
|
---|
147 | UniQueryUconvObject(os2_cd->from, &attr,
|
---|
148 | sizeof(uconv_attribute_t),
|
---|
149 | NULL, NULL, NULL );
|
---|
150 | attr.converttype |= CVTTYPE_PATH;
|
---|
151 | UniSetUconvObject(os2_cd->from, &attr);
|
---|
152 | }
|
---|
153 |
|
---|
154 | return (iconv_t)os2_cd;
|
---|
155 | }
|
---|
156 |
|
---|
157 | #define iconv_open os2_iconv_open
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | #ifdef HAVE_NATIVE_ICONV
|
---|
161 | /* if there was an error then reset the internal state,
|
---|
162 | this ensures that we don't have a shift state remaining for
|
---|
163 | character sets like SJIS */
|
---|
164 | static size_t sys_iconv(void *cd,
|
---|
165 | const char **inbuf, size_t *inbytesleft,
|
---|
166 | char **outbuf, size_t *outbytesleft)
|
---|
167 | {
|
---|
168 | size_t ret = iconv((iconv_t)cd,
|
---|
169 | (void *)inbuf, inbytesleft,
|
---|
170 | outbuf, outbytesleft);
|
---|
171 | if (ret == (size_t)-1) {
|
---|
172 | int saved_errno = errno;
|
---|
173 | iconv(cd, NULL, NULL, NULL, NULL);
|
---|
174 | errno = saved_errno;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return ret;
|
---|
178 | }
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * This is a simple portable iconv() implementaion.
|
---|
183 | *
|
---|
184 | * It only knows about a very small number of character sets - just
|
---|
185 | * enough that Samba works on systems that don't have iconv.
|
---|
186 | **/
|
---|
187 | size_t smb_iconv(smb_iconv_t cd,
|
---|
188 | const char **inbuf, size_t *inbytesleft,
|
---|
189 | char **outbuf, size_t *outbytesleft)
|
---|
190 | {
|
---|
191 | char cvtbuf[2048];
|
---|
192 | char *bufp = cvtbuf;
|
---|
193 | size_t bufsize;
|
---|
194 |
|
---|
195 | /* in many cases we can go direct */
|
---|
196 | if (cd->direct) {
|
---|
197 | return cd->direct(cd->cd_direct,
|
---|
198 | inbuf, inbytesleft, outbuf, outbytesleft);
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | /* otherwise we have to do it chunks at a time */
|
---|
203 | while (*inbytesleft > 0) {
|
---|
204 | bufp = cvtbuf;
|
---|
205 | bufsize = sizeof(cvtbuf);
|
---|
206 |
|
---|
207 | if (cd->pull(cd->cd_pull,
|
---|
208 | inbuf, inbytesleft, &bufp, &bufsize) == -1
|
---|
209 | && errno != E2BIG) return -1;
|
---|
210 |
|
---|
211 | bufp = cvtbuf;
|
---|
212 | bufsize = sizeof(cvtbuf) - bufsize;
|
---|
213 |
|
---|
214 | if (cd->push(cd->cd_push,
|
---|
215 | (const char **)&bufp, &bufsize,
|
---|
216 | outbuf, outbytesleft) == -1) return -1;
|
---|
217 | }
|
---|
218 |
|
---|
219 | return 0;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | static bool is_utf16(const char *name)
|
---|
224 | {
|
---|
225 | return strcasecmp(name, "UCS-2LE") == 0 ||
|
---|
226 | strcasecmp(name, "UTF-16LE") == 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | simple iconv_open() wrapper
|
---|
231 | */
|
---|
232 | smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
|
---|
233 | {
|
---|
234 | smb_iconv_t ret;
|
---|
235 | struct charset_functions *from, *to;
|
---|
236 |
|
---|
237 | lazy_initialize_iconv();
|
---|
238 | from = charsets;
|
---|
239 | to = charsets;
|
---|
240 |
|
---|
241 | ret = SMB_MALLOC_P(struct smb_iconv_s);
|
---|
242 | if (!ret) {
|
---|
243 | errno = ENOMEM;
|
---|
244 | return (smb_iconv_t)-1;
|
---|
245 | }
|
---|
246 | memset(ret, 0, sizeof(struct smb_iconv_s));
|
---|
247 |
|
---|
248 | ret->from_name = SMB_STRDUP(fromcode);
|
---|
249 | ret->to_name = SMB_STRDUP(tocode);
|
---|
250 |
|
---|
251 | /* check for the simplest null conversion */
|
---|
252 | if (strcasecmp(fromcode, tocode) == 0) {
|
---|
253 | ret->direct = iconv_copy;
|
---|
254 | return ret;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* check if we have a builtin function for this conversion */
|
---|
258 | from = find_charset_functions(fromcode);
|
---|
259 | if(from)ret->pull = from->pull;
|
---|
260 |
|
---|
261 | to = find_charset_functions(tocode);
|
---|
262 | if(to)ret->push = to->push;
|
---|
263 |
|
---|
264 | /* check if we can use iconv for this conversion */
|
---|
265 | #ifdef HAVE_NATIVE_ICONV
|
---|
266 | if (!ret->pull) {
|
---|
267 | ret->cd_pull = iconv_open("UTF-16LE", fromcode);
|
---|
268 | if (ret->cd_pull == (iconv_t)-1)
|
---|
269 | ret->cd_pull = iconv_open("UCS-2LE", fromcode);
|
---|
270 | if (ret->cd_pull != (iconv_t)-1)
|
---|
271 | ret->pull = sys_iconv;
|
---|
272 | }
|
---|
273 |
|
---|
274 | if (!ret->push) {
|
---|
275 | ret->cd_push = iconv_open(tocode, "UTF-16LE");
|
---|
276 | if (ret->cd_push == (iconv_t)-1)
|
---|
277 | ret->cd_push = iconv_open(tocode, "UCS-2LE");
|
---|
278 | if (ret->cd_push != (iconv_t)-1)
|
---|
279 | ret->push = sys_iconv;
|
---|
280 | }
|
---|
281 | #endif
|
---|
282 |
|
---|
283 | /* check if there is a module available that can do this conversion */
|
---|
284 | if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
|
---|
285 | if(!(from = find_charset_functions(fromcode)))
|
---|
286 | DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
|
---|
287 | else
|
---|
288 | ret->pull = from->pull;
|
---|
289 | }
|
---|
290 |
|
---|
291 | if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
|
---|
292 | if(!(to = find_charset_functions(tocode)))
|
---|
293 | DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
|
---|
294 | else
|
---|
295 | ret->push = to->push;
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (!ret->push || !ret->pull) {
|
---|
299 | SAFE_FREE(ret->from_name);
|
---|
300 | SAFE_FREE(ret->to_name);
|
---|
301 | SAFE_FREE(ret);
|
---|
302 | errno = EINVAL;
|
---|
303 | return (smb_iconv_t)-1;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /* check for conversion to/from ucs2 */
|
---|
307 | if (is_utf16(fromcode) && to) {
|
---|
308 | ret->direct = to->push;
|
---|
309 | ret->push = ret->pull = NULL;
|
---|
310 | return ret;
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (is_utf16(tocode) && from) {
|
---|
314 | ret->direct = from->pull;
|
---|
315 | ret->push = ret->pull = NULL;
|
---|
316 | return ret;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /* Check if we can do the conversion direct */
|
---|
320 | #ifdef HAVE_NATIVE_ICONV
|
---|
321 | if (is_utf16(fromcode)) {
|
---|
322 | ret->direct = sys_iconv;
|
---|
323 | ret->cd_direct = ret->cd_push;
|
---|
324 | ret->cd_push = NULL;
|
---|
325 | return ret;
|
---|
326 | }
|
---|
327 | if (is_utf16(tocode)) {
|
---|
328 | ret->direct = sys_iconv;
|
---|
329 | ret->cd_direct = ret->cd_pull;
|
---|
330 | ret->cd_pull = NULL;
|
---|
331 | return ret;
|
---|
332 | }
|
---|
333 | #endif
|
---|
334 |
|
---|
335 | return ret;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /*
|
---|
339 | simple iconv_close() wrapper
|
---|
340 | */
|
---|
341 | int smb_iconv_close (smb_iconv_t cd)
|
---|
342 | {
|
---|
343 | #ifdef HAVE_NATIVE_ICONV
|
---|
344 | if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
|
---|
345 | if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
|
---|
346 | if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
|
---|
347 | #endif
|
---|
348 |
|
---|
349 | SAFE_FREE(cd->from_name);
|
---|
350 | SAFE_FREE(cd->to_name);
|
---|
351 |
|
---|
352 | memset(cd, 0, sizeof(*cd));
|
---|
353 | SAFE_FREE(cd);
|
---|
354 | return 0;
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | /**********************************************************************
|
---|
359 | the following functions implement the builtin character sets in Samba
|
---|
360 | and also the "test" character sets that are designed to test
|
---|
361 | multi-byte character set support for english users
|
---|
362 | ***********************************************************************/
|
---|
363 |
|
---|
364 | static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
365 | char **outbuf, size_t *outbytesleft)
|
---|
366 | {
|
---|
367 | while (*inbytesleft >= 1 && *outbytesleft >= 2) {
|
---|
368 | (*outbuf)[0] = (*inbuf)[0];
|
---|
369 | (*outbuf)[1] = 0;
|
---|
370 | (*inbytesleft) -= 1;
|
---|
371 | (*outbytesleft) -= 2;
|
---|
372 | (*inbuf) += 1;
|
---|
373 | (*outbuf) += 2;
|
---|
374 | }
|
---|
375 |
|
---|
376 | if (*inbytesleft > 0) {
|
---|
377 | errno = E2BIG;
|
---|
378 | return -1;
|
---|
379 | }
|
---|
380 |
|
---|
381 | return 0;
|
---|
382 | }
|
---|
383 |
|
---|
384 | static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
385 | char **outbuf, size_t *outbytesleft)
|
---|
386 | {
|
---|
387 | int ir_count=0;
|
---|
388 |
|
---|
389 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
390 | (*outbuf)[0] = (*inbuf)[0] & 0x7F;
|
---|
391 | if ((*inbuf)[1]) ir_count++;
|
---|
392 | (*inbytesleft) -= 2;
|
---|
393 | (*outbytesleft) -= 1;
|
---|
394 | (*inbuf) += 2;
|
---|
395 | (*outbuf) += 1;
|
---|
396 | }
|
---|
397 |
|
---|
398 | if (*inbytesleft == 1) {
|
---|
399 | errno = EINVAL;
|
---|
400 | return -1;
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (*inbytesleft > 1) {
|
---|
404 | errno = E2BIG;
|
---|
405 | return -1;
|
---|
406 | }
|
---|
407 |
|
---|
408 | return ir_count;
|
---|
409 | }
|
---|
410 |
|
---|
411 | static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
412 | char **outbuf, size_t *outbytesleft)
|
---|
413 | {
|
---|
414 | int ir_count=0;
|
---|
415 |
|
---|
416 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
417 | (*outbuf)[0] = (*inbuf)[0];
|
---|
418 | if ((*inbuf)[1]) ir_count++;
|
---|
419 | (*inbytesleft) -= 2;
|
---|
420 | (*outbytesleft) -= 1;
|
---|
421 | (*inbuf) += 2;
|
---|
422 | (*outbuf) += 1;
|
---|
423 | }
|
---|
424 |
|
---|
425 | if (*inbytesleft == 1) {
|
---|
426 | errno = EINVAL;
|
---|
427 | return -1;
|
---|
428 | }
|
---|
429 |
|
---|
430 | if (*inbytesleft > 1) {
|
---|
431 | errno = E2BIG;
|
---|
432 | return -1;
|
---|
433 | }
|
---|
434 |
|
---|
435 | return ir_count;
|
---|
436 | }
|
---|
437 |
|
---|
438 | static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
439 | char **outbuf, size_t *outbytesleft)
|
---|
440 | {
|
---|
441 | while (*inbytesleft >= 1 && *outbytesleft >= 2) {
|
---|
442 | unsigned v;
|
---|
443 |
|
---|
444 | if ((*inbuf)[0] != '@') {
|
---|
445 | /* seven bit ascii case */
|
---|
446 | (*outbuf)[0] = (*inbuf)[0];
|
---|
447 | (*outbuf)[1] = 0;
|
---|
448 | (*inbytesleft) -= 1;
|
---|
449 | (*outbytesleft) -= 2;
|
---|
450 | (*inbuf) += 1;
|
---|
451 | (*outbuf) += 2;
|
---|
452 | continue;
|
---|
453 | }
|
---|
454 | /* it's a hex character */
|
---|
455 | if (*inbytesleft < 5) {
|
---|
456 | errno = EINVAL;
|
---|
457 | return -1;
|
---|
458 | }
|
---|
459 |
|
---|
460 | if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
|
---|
461 | errno = EILSEQ;
|
---|
462 | return -1;
|
---|
463 | }
|
---|
464 |
|
---|
465 | (*outbuf)[0] = v&0xff;
|
---|
466 | (*outbuf)[1] = v>>8;
|
---|
467 | (*inbytesleft) -= 5;
|
---|
468 | (*outbytesleft) -= 2;
|
---|
469 | (*inbuf) += 5;
|
---|
470 | (*outbuf) += 2;
|
---|
471 | }
|
---|
472 |
|
---|
473 | if (*inbytesleft > 0) {
|
---|
474 | errno = E2BIG;
|
---|
475 | return -1;
|
---|
476 | }
|
---|
477 |
|
---|
478 | return 0;
|
---|
479 | }
|
---|
480 |
|
---|
481 | static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
482 | char **outbuf, size_t *outbytesleft)
|
---|
483 | {
|
---|
484 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
485 | char buf[6];
|
---|
486 |
|
---|
487 | if ((*inbuf)[1] == 0 &&
|
---|
488 | ((*inbuf)[0] & 0x80) == 0 &&
|
---|
489 | (*inbuf)[0] != '@') {
|
---|
490 | (*outbuf)[0] = (*inbuf)[0];
|
---|
491 | (*inbytesleft) -= 2;
|
---|
492 | (*outbytesleft) -= 1;
|
---|
493 | (*inbuf) += 2;
|
---|
494 | (*outbuf) += 1;
|
---|
495 | continue;
|
---|
496 | }
|
---|
497 | if (*outbytesleft < 5) {
|
---|
498 | errno = E2BIG;
|
---|
499 | return -1;
|
---|
500 | }
|
---|
501 | snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
|
---|
502 | memcpy(*outbuf, buf, 5);
|
---|
503 | (*inbytesleft) -= 2;
|
---|
504 | (*outbytesleft) -= 5;
|
---|
505 | (*inbuf) += 2;
|
---|
506 | (*outbuf) += 5;
|
---|
507 | }
|
---|
508 |
|
---|
509 | if (*inbytesleft == 1) {
|
---|
510 | errno = EINVAL;
|
---|
511 | return -1;
|
---|
512 | }
|
---|
513 |
|
---|
514 | if (*inbytesleft > 1) {
|
---|
515 | errno = E2BIG;
|
---|
516 | return -1;
|
---|
517 | }
|
---|
518 |
|
---|
519 | return 0;
|
---|
520 | }
|
---|
521 |
|
---|
522 | static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
523 | char **outbuf, size_t *outbytesleft)
|
---|
524 | {
|
---|
525 | int n;
|
---|
526 |
|
---|
527 | n = MIN(*inbytesleft, *outbytesleft);
|
---|
528 |
|
---|
529 | swab(*inbuf, *outbuf, (n&~1));
|
---|
530 | if (n&1) {
|
---|
531 | (*outbuf)[n-1] = 0;
|
---|
532 | }
|
---|
533 |
|
---|
534 | (*inbytesleft) -= n;
|
---|
535 | (*outbytesleft) -= n;
|
---|
536 | (*inbuf) += n;
|
---|
537 | (*outbuf) += n;
|
---|
538 |
|
---|
539 | if (*inbytesleft > 0) {
|
---|
540 | errno = E2BIG;
|
---|
541 | return -1;
|
---|
542 | }
|
---|
543 |
|
---|
544 | return 0;
|
---|
545 | }
|
---|
546 |
|
---|
547 | static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
548 | char **outbuf, size_t *outbytesleft)
|
---|
549 | {
|
---|
550 | int n;
|
---|
551 |
|
---|
552 | n = MIN(*inbytesleft, *outbytesleft);
|
---|
553 |
|
---|
554 | memmove(*outbuf, *inbuf, n);
|
---|
555 |
|
---|
556 | (*inbytesleft) -= n;
|
---|
557 | (*outbytesleft) -= n;
|
---|
558 | (*inbuf) += n;
|
---|
559 | (*outbuf) += n;
|
---|
560 |
|
---|
561 | if (*inbytesleft > 0) {
|
---|
562 | errno = E2BIG;
|
---|
563 | return -1;
|
---|
564 | }
|
---|
565 |
|
---|
566 | return 0;
|
---|
567 | }
|
---|
568 |
|
---|
569 | static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
570 | char **outbuf, size_t *outbytesleft)
|
---|
571 | {
|
---|
572 | size_t in_left=*inbytesleft, out_left=*outbytesleft;
|
---|
573 | const uint8 *c = (const uint8 *)*inbuf;
|
---|
574 | uint8 *uc = (uint8 *)*outbuf;
|
---|
575 |
|
---|
576 | while (in_left >= 1 && out_left >= 2) {
|
---|
577 | unsigned int codepoint;
|
---|
578 |
|
---|
579 | if ((c[0] & 0x80) == 0) {
|
---|
580 | uc[0] = c[0];
|
---|
581 | uc[1] = 0;
|
---|
582 | c += 1;
|
---|
583 | in_left -= 1;
|
---|
584 | out_left -= 2;
|
---|
585 | uc += 2;
|
---|
586 | continue;
|
---|
587 | }
|
---|
588 |
|
---|
589 | if ((c[0] & 0xe0) == 0xc0) {
|
---|
590 | if (in_left < 2 ||
|
---|
591 | (c[1] & 0xc0) != 0x80) {
|
---|
592 | errno = EILSEQ;
|
---|
593 | goto error;
|
---|
594 | }
|
---|
595 | codepoint = (c[1]&0x3f) | ((c[0]&0x1f)<<6);
|
---|
596 | if (codepoint < 0x80) {
|
---|
597 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
598 | errno = EILSEQ;
|
---|
599 | goto error;
|
---|
600 | }
|
---|
601 | uc[1] = codepoint >> 8;
|
---|
602 | uc[0] = codepoint & 0xff;
|
---|
603 | c += 2;
|
---|
604 | in_left -= 2;
|
---|
605 | out_left -= 2;
|
---|
606 | uc += 2;
|
---|
607 | continue;
|
---|
608 | }
|
---|
609 |
|
---|
610 | if ((c[0] & 0xf0) == 0xe0) {
|
---|
611 | if (in_left < 3 ||
|
---|
612 | (c[1] & 0xc0) != 0x80 ||
|
---|
613 | (c[2] & 0xc0) != 0x80) {
|
---|
614 | errno = EILSEQ;
|
---|
615 | goto error;
|
---|
616 | }
|
---|
617 | codepoint = (c[2]&0x3f) | ((c[1]&0x3f)<<6) | ((c[0]&0xf)<<12);
|
---|
618 | if (codepoint < 0x800) {
|
---|
619 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
620 | errno = EILSEQ;
|
---|
621 | goto error;
|
---|
622 | }
|
---|
623 | uc[1] = codepoint >> 8;
|
---|
624 | uc[0] = codepoint & 0xff;
|
---|
625 | c += 3;
|
---|
626 | in_left -= 3;
|
---|
627 | out_left -= 2;
|
---|
628 | uc += 2;
|
---|
629 | continue;
|
---|
630 | }
|
---|
631 |
|
---|
632 | if ((c[0] & 0xf8) == 0xf0) {
|
---|
633 | if (in_left < 4 ||
|
---|
634 | (c[1] & 0xc0) != 0x80 ||
|
---|
635 | (c[2] & 0xc0) != 0x80 ||
|
---|
636 | (c[3] & 0xc0) != 0x80) {
|
---|
637 | errno = EILSEQ;
|
---|
638 | goto error;
|
---|
639 | }
|
---|
640 | codepoint =
|
---|
641 | (c[3]&0x3f) |
|
---|
642 | ((c[2]&0x3f)<<6) |
|
---|
643 | ((c[1]&0x3f)<<12) |
|
---|
644 | ((c[0]&0x7)<<18);
|
---|
645 | if (codepoint < 0x10000 || codepoint > 0x10ffff) {
|
---|
646 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
647 | errno = EILSEQ;
|
---|
648 | goto error;
|
---|
649 | }
|
---|
650 |
|
---|
651 | codepoint -= 0x10000;
|
---|
652 |
|
---|
653 | if (out_left < 4) {
|
---|
654 | errno = E2BIG;
|
---|
655 | goto error;
|
---|
656 | }
|
---|
657 |
|
---|
658 | uc[0] = (codepoint>>10) & 0xFF;
|
---|
659 | uc[1] = (codepoint>>18) | 0xd8;
|
---|
660 | uc[2] = codepoint & 0xFF;
|
---|
661 | uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
|
---|
662 | c += 4;
|
---|
663 | in_left -= 4;
|
---|
664 | out_left -= 4;
|
---|
665 | uc += 4;
|
---|
666 | continue;
|
---|
667 | }
|
---|
668 |
|
---|
669 | /* we don't handle 5 byte sequences */
|
---|
670 | errno = EINVAL;
|
---|
671 | goto error;
|
---|
672 | }
|
---|
673 |
|
---|
674 | if (in_left > 0) {
|
---|
675 | errno = E2BIG;
|
---|
676 | goto error;
|
---|
677 | }
|
---|
678 |
|
---|
679 | *inbytesleft = in_left;
|
---|
680 | *outbytesleft = out_left;
|
---|
681 | *inbuf = (char *)c;
|
---|
682 | *outbuf = (char *)uc;
|
---|
683 | return 0;
|
---|
684 |
|
---|
685 | error:
|
---|
686 | *inbytesleft = in_left;
|
---|
687 | *outbytesleft = out_left;
|
---|
688 | *inbuf = (char *)c;
|
---|
689 | *outbuf = (char *)uc;
|
---|
690 | return -1;
|
---|
691 | }
|
---|
692 |
|
---|
693 | static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
694 | char **outbuf, size_t *outbytesleft)
|
---|
695 | {
|
---|
696 | size_t in_left=*inbytesleft, out_left=*outbytesleft;
|
---|
697 | uint8 *c = (uint8 *)*outbuf;
|
---|
698 | const uint8 *uc = (const uint8 *)*inbuf;
|
---|
699 |
|
---|
700 | while (in_left >= 2 && out_left >= 1) {
|
---|
701 | unsigned int codepoint;
|
---|
702 |
|
---|
703 | if (uc[1] == 0 && !(uc[0] & 0x80)) {
|
---|
704 | /* simplest case */
|
---|
705 | c[0] = uc[0];
|
---|
706 | in_left -= 2;
|
---|
707 | out_left -= 1;
|
---|
708 | uc += 2;
|
---|
709 | c += 1;
|
---|
710 | continue;
|
---|
711 | }
|
---|
712 |
|
---|
713 | if ((uc[1]&0xf8) == 0) {
|
---|
714 | /* next simplest case */
|
---|
715 | if (out_left < 2) {
|
---|
716 | errno = E2BIG;
|
---|
717 | goto error;
|
---|
718 | }
|
---|
719 | c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
|
---|
720 | c[1] = 0x80 | (uc[0] & 0x3f);
|
---|
721 | in_left -= 2;
|
---|
722 | out_left -= 2;
|
---|
723 | uc += 2;
|
---|
724 | c += 2;
|
---|
725 | continue;
|
---|
726 | }
|
---|
727 |
|
---|
728 | if ((uc[1] & 0xfc) == 0xdc) {
|
---|
729 | /* its the second part of a 4 byte sequence. Illegal */
|
---|
730 | if (in_left < 4) {
|
---|
731 | errno = EINVAL;
|
---|
732 | } else {
|
---|
733 | errno = EILSEQ;
|
---|
734 | }
|
---|
735 | goto error;
|
---|
736 | }
|
---|
737 |
|
---|
738 | if ((uc[1] & 0xfc) != 0xd8) {
|
---|
739 | codepoint = uc[0] | (uc[1]<<8);
|
---|
740 | if (out_left < 3) {
|
---|
741 | errno = E2BIG;
|
---|
742 | goto error;
|
---|
743 | }
|
---|
744 | c[0] = 0xe0 | (codepoint >> 12);
|
---|
745 | c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
|
---|
746 | c[2] = 0x80 | (codepoint & 0x3f);
|
---|
747 |
|
---|
748 | in_left -= 2;
|
---|
749 | out_left -= 3;
|
---|
750 | uc += 2;
|
---|
751 | c += 3;
|
---|
752 | continue;
|
---|
753 | }
|
---|
754 |
|
---|
755 | /* its the first part of a 4 byte sequence */
|
---|
756 | if (in_left < 4) {
|
---|
757 | errno = EINVAL;
|
---|
758 | goto error;
|
---|
759 | }
|
---|
760 | if ((uc[3] & 0xfc) != 0xdc) {
|
---|
761 | errno = EILSEQ;
|
---|
762 | goto error;
|
---|
763 | }
|
---|
764 | codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
|
---|
765 | (uc[0]<<10) | ((uc[1] & 0x3)<<18));
|
---|
766 |
|
---|
767 | if (out_left < 4) {
|
---|
768 | errno = E2BIG;
|
---|
769 | goto error;
|
---|
770 | }
|
---|
771 | c[0] = 0xf0 | (codepoint >> 18);
|
---|
772 | c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
|
---|
773 | c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
|
---|
774 | c[3] = 0x80 | (codepoint & 0x3f);
|
---|
775 |
|
---|
776 | in_left -= 4;
|
---|
777 | out_left -= 4;
|
---|
778 | uc += 4;
|
---|
779 | c += 4;
|
---|
780 | }
|
---|
781 |
|
---|
782 | if (in_left == 1) {
|
---|
783 | errno = EINVAL;
|
---|
784 | goto error;
|
---|
785 | }
|
---|
786 |
|
---|
787 | if (in_left > 1) {
|
---|
788 | errno = E2BIG;
|
---|
789 | goto error;
|
---|
790 | }
|
---|
791 |
|
---|
792 | *inbytesleft = in_left;
|
---|
793 | *outbytesleft = out_left;
|
---|
794 | *inbuf = (char *)uc;
|
---|
795 | *outbuf = (char *)c;
|
---|
796 |
|
---|
797 | return 0;
|
---|
798 |
|
---|
799 | error:
|
---|
800 | *inbytesleft = in_left;
|
---|
801 | *outbytesleft = out_left;
|
---|
802 | *inbuf = (char *)uc;
|
---|
803 | *outbuf = (char *)c;
|
---|
804 | return -1;
|
---|
805 | }
|
---|
806 |
|
---|