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 | #ifdef HAVE_NATIVE_ICONV
|
---|
131 | /* if there was an error then reset the internal state,
|
---|
132 | this ensures that we don't have a shift state remaining for
|
---|
133 | character sets like SJIS */
|
---|
134 | static size_t sys_iconv(void *cd,
|
---|
135 | const char **inbuf, size_t *inbytesleft,
|
---|
136 | char **outbuf, size_t *outbytesleft)
|
---|
137 | {
|
---|
138 | #ifdef __OS2__
|
---|
139 | uint16 *outbuf_uc = ( uint16 * )*outbuf;
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | size_t ret = iconv((iconv_t)cd,
|
---|
143 | (void *)inbuf, inbytesleft,
|
---|
144 | outbuf, outbytesleft);
|
---|
145 | if (ret == (size_t)-1) {
|
---|
146 | int saved_errno = errno;
|
---|
147 | iconv(cd, NULL, NULL, NULL, NULL);
|
---|
148 | errno = saved_errno;
|
---|
149 | }
|
---|
150 | #ifdef __OS2__
|
---|
151 | /* Workaround for path separator on OS/2 */
|
---|
152 | else
|
---|
153 | {
|
---|
154 | while(( char * )outbuf_uc < *outbuf )
|
---|
155 | {
|
---|
156 | if( *outbuf_uc == 0x20a9 || /* Korean WON */
|
---|
157 | *outbuf_uc == 0x00a5 ) /* Japanese YEN */
|
---|
158 | *outbuf_uc = '\\';
|
---|
159 |
|
---|
160 | outbuf_uc++;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | #endif
|
---|
164 | return ret;
|
---|
165 | }
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * This is a simple portable iconv() implementaion.
|
---|
170 | *
|
---|
171 | * It only knows about a very small number of character sets - just
|
---|
172 | * enough that Samba works on systems that don't have iconv.
|
---|
173 | **/
|
---|
174 | size_t smb_iconv(smb_iconv_t cd,
|
---|
175 | const char **inbuf, size_t *inbytesleft,
|
---|
176 | char **outbuf, size_t *outbytesleft)
|
---|
177 | {
|
---|
178 | char cvtbuf[2048];
|
---|
179 | char *bufp = cvtbuf;
|
---|
180 | size_t bufsize;
|
---|
181 |
|
---|
182 | /* in many cases we can go direct */
|
---|
183 | if (cd->direct) {
|
---|
184 | return cd->direct(cd->cd_direct,
|
---|
185 | inbuf, inbytesleft, outbuf, outbytesleft);
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /* otherwise we have to do it chunks at a time */
|
---|
190 | while (*inbytesleft > 0) {
|
---|
191 | bufp = cvtbuf;
|
---|
192 | bufsize = sizeof(cvtbuf);
|
---|
193 |
|
---|
194 | if (cd->pull(cd->cd_pull,
|
---|
195 | inbuf, inbytesleft, &bufp, &bufsize) == -1
|
---|
196 | && errno != E2BIG) {
|
---|
197 | DEBUG(0,("smb_iconv 2\n"));
|
---|
198 | return -1;}
|
---|
199 |
|
---|
200 | bufp = cvtbuf;
|
---|
201 | bufsize = sizeof(cvtbuf) - bufsize;
|
---|
202 |
|
---|
203 | if (cd->push(cd->cd_push,
|
---|
204 | (const char **)&bufp, &bufsize,
|
---|
205 | outbuf, outbytesleft) == -1) {
|
---|
206 | DEBUG(0,("smb_iconv 3\n"));
|
---|
207 | return -1;}
|
---|
208 | }
|
---|
209 |
|
---|
210 | return 0;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | static bool is_utf16(const char *name)
|
---|
215 | {
|
---|
216 | return strcasecmp(name, "UCS-2LE") == 0 ||
|
---|
217 | strcasecmp(name, "UTF-16LE") == 0;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /*
|
---|
221 | simple iconv_open() wrapper
|
---|
222 | */
|
---|
223 | smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
|
---|
224 | {
|
---|
225 | smb_iconv_t ret;
|
---|
226 | struct charset_functions *from, *to;
|
---|
227 |
|
---|
228 | lazy_initialize_iconv();
|
---|
229 | from = charsets;
|
---|
230 | to = charsets;
|
---|
231 |
|
---|
232 | ret = SMB_MALLOC_P(struct _smb_iconv_t);
|
---|
233 | if (!ret) {
|
---|
234 | errno = ENOMEM;
|
---|
235 | return (smb_iconv_t)-1;
|
---|
236 | }
|
---|
237 | memset(ret, 0, sizeof(struct _smb_iconv_t));
|
---|
238 |
|
---|
239 | ret->from_name = SMB_STRDUP(fromcode);
|
---|
240 | ret->to_name = SMB_STRDUP(tocode);
|
---|
241 |
|
---|
242 | /* check for the simplest null conversion */
|
---|
243 | if (strcasecmp(fromcode, tocode) == 0) {
|
---|
244 | ret->direct = iconv_copy;
|
---|
245 | return ret;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /* check if we have a builtin function for this conversion */
|
---|
249 | from = find_charset_functions(fromcode);
|
---|
250 | if(from)ret->pull = from->pull;
|
---|
251 |
|
---|
252 | to = find_charset_functions(tocode);
|
---|
253 | if(to)ret->push = to->push;
|
---|
254 |
|
---|
255 | /* check if we can use iconv for this conversion */
|
---|
256 | #ifdef HAVE_NATIVE_ICONV
|
---|
257 | if (!ret->pull) {
|
---|
258 | ret->cd_pull = iconv_open("UTF-16LE", fromcode);
|
---|
259 | if (ret->cd_pull == (iconv_t)-1)
|
---|
260 | ret->cd_pull = iconv_open("UCS-2LE", fromcode);
|
---|
261 | if (ret->cd_pull != (iconv_t)-1)
|
---|
262 | ret->pull = sys_iconv;
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (!ret->push) {
|
---|
266 | ret->cd_push = iconv_open(tocode, "UTF-16LE");
|
---|
267 | if (ret->cd_push == (iconv_t)-1)
|
---|
268 | ret->cd_push = iconv_open(tocode, "UCS-2LE");
|
---|
269 | if (ret->cd_push != (iconv_t)-1)
|
---|
270 | ret->push = sys_iconv;
|
---|
271 | }
|
---|
272 | #endif
|
---|
273 |
|
---|
274 | /* check if there is a module available that can do this conversion */
|
---|
275 | if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
|
---|
276 | if(!(from = find_charset_functions(fromcode)))
|
---|
277 | DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
|
---|
278 | else
|
---|
279 | ret->pull = from->pull;
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
|
---|
283 | if(!(to = find_charset_functions(tocode)))
|
---|
284 | DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
|
---|
285 | else
|
---|
286 | ret->push = to->push;
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (!ret->push || !ret->pull) {
|
---|
290 | SAFE_FREE(ret->from_name);
|
---|
291 | SAFE_FREE(ret->to_name);
|
---|
292 | SAFE_FREE(ret);
|
---|
293 | errno = EINVAL;
|
---|
294 | return (smb_iconv_t)-1;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /* check for conversion to/from ucs2 */
|
---|
298 | if (is_utf16(fromcode) && to) {
|
---|
299 | ret->direct = to->push;
|
---|
300 | ret->push = ret->pull = NULL;
|
---|
301 | return ret;
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (is_utf16(tocode) && from) {
|
---|
305 | ret->direct = from->pull;
|
---|
306 | ret->push = ret->pull = NULL;
|
---|
307 | return ret;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /* Check if we can do the conversion direct */
|
---|
311 | #ifdef HAVE_NATIVE_ICONV
|
---|
312 | if (is_utf16(fromcode)) {
|
---|
313 | ret->direct = sys_iconv;
|
---|
314 | ret->cd_direct = ret->cd_push;
|
---|
315 | ret->cd_push = NULL;
|
---|
316 | return ret;
|
---|
317 | }
|
---|
318 | if (is_utf16(tocode)) {
|
---|
319 | ret->direct = sys_iconv;
|
---|
320 | ret->cd_direct = ret->cd_pull;
|
---|
321 | ret->cd_pull = NULL;
|
---|
322 | return ret;
|
---|
323 | }
|
---|
324 | #endif
|
---|
325 |
|
---|
326 | return ret;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /*
|
---|
330 | simple iconv_close() wrapper
|
---|
331 | */
|
---|
332 | int smb_iconv_close (smb_iconv_t cd)
|
---|
333 | {
|
---|
334 | #ifdef HAVE_NATIVE_ICONV
|
---|
335 | if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
|
---|
336 | if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
|
---|
337 | if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
|
---|
338 | #endif
|
---|
339 |
|
---|
340 | SAFE_FREE(cd->from_name);
|
---|
341 | SAFE_FREE(cd->to_name);
|
---|
342 |
|
---|
343 | memset(cd, 0, sizeof(*cd));
|
---|
344 | SAFE_FREE(cd);
|
---|
345 | return 0;
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | /**********************************************************************
|
---|
350 | the following functions implement the builtin character sets in Samba
|
---|
351 | and also the "test" character sets that are designed to test
|
---|
352 | multi-byte character set support for english users
|
---|
353 | ***********************************************************************/
|
---|
354 |
|
---|
355 | static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
356 | char **outbuf, size_t *outbytesleft)
|
---|
357 | {
|
---|
358 | while (*inbytesleft >= 1 && *outbytesleft >= 2) {
|
---|
359 | (*outbuf)[0] = (*inbuf)[0];
|
---|
360 | (*outbuf)[1] = 0;
|
---|
361 | (*inbytesleft) -= 1;
|
---|
362 | (*outbytesleft) -= 2;
|
---|
363 | (*inbuf) += 1;
|
---|
364 | (*outbuf) += 2;
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (*inbytesleft > 0) {
|
---|
368 | errno = E2BIG;
|
---|
369 | return -1;
|
---|
370 | }
|
---|
371 |
|
---|
372 | return 0;
|
---|
373 | }
|
---|
374 |
|
---|
375 | static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
376 | char **outbuf, size_t *outbytesleft)
|
---|
377 | {
|
---|
378 | int ir_count=0;
|
---|
379 |
|
---|
380 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
381 | (*outbuf)[0] = (*inbuf)[0] & 0x7F;
|
---|
382 | if ((*inbuf)[1]) ir_count++;
|
---|
383 | (*inbytesleft) -= 2;
|
---|
384 | (*outbytesleft) -= 1;
|
---|
385 | (*inbuf) += 2;
|
---|
386 | (*outbuf) += 1;
|
---|
387 | }
|
---|
388 |
|
---|
389 | if (*inbytesleft == 1) {
|
---|
390 | errno = EINVAL;
|
---|
391 | return -1;
|
---|
392 | }
|
---|
393 |
|
---|
394 | if (*inbytesleft > 1) {
|
---|
395 | errno = E2BIG;
|
---|
396 | return -1;
|
---|
397 | }
|
---|
398 |
|
---|
399 | return ir_count;
|
---|
400 | }
|
---|
401 |
|
---|
402 | static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
403 | char **outbuf, size_t *outbytesleft)
|
---|
404 | {
|
---|
405 | int ir_count=0;
|
---|
406 |
|
---|
407 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
408 | (*outbuf)[0] = (*inbuf)[0];
|
---|
409 | if ((*inbuf)[1]) ir_count++;
|
---|
410 | (*inbytesleft) -= 2;
|
---|
411 | (*outbytesleft) -= 1;
|
---|
412 | (*inbuf) += 2;
|
---|
413 | (*outbuf) += 1;
|
---|
414 | }
|
---|
415 |
|
---|
416 | if (*inbytesleft == 1) {
|
---|
417 | errno = EINVAL;
|
---|
418 | return -1;
|
---|
419 | }
|
---|
420 |
|
---|
421 | if (*inbytesleft > 1) {
|
---|
422 | errno = E2BIG;
|
---|
423 | return -1;
|
---|
424 | }
|
---|
425 |
|
---|
426 | return ir_count;
|
---|
427 | }
|
---|
428 |
|
---|
429 | static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
430 | char **outbuf, size_t *outbytesleft)
|
---|
431 | {
|
---|
432 | while (*inbytesleft >= 1 && *outbytesleft >= 2) {
|
---|
433 | unsigned v;
|
---|
434 |
|
---|
435 | if ((*inbuf)[0] != '@') {
|
---|
436 | /* seven bit ascii case */
|
---|
437 | (*outbuf)[0] = (*inbuf)[0];
|
---|
438 | (*outbuf)[1] = 0;
|
---|
439 | (*inbytesleft) -= 1;
|
---|
440 | (*outbytesleft) -= 2;
|
---|
441 | (*inbuf) += 1;
|
---|
442 | (*outbuf) += 2;
|
---|
443 | continue;
|
---|
444 | }
|
---|
445 | /* it's a hex character */
|
---|
446 | if (*inbytesleft < 5) {
|
---|
447 | errno = EINVAL;
|
---|
448 | return -1;
|
---|
449 | }
|
---|
450 |
|
---|
451 | if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
|
---|
452 | errno = EILSEQ;
|
---|
453 | return -1;
|
---|
454 | }
|
---|
455 |
|
---|
456 | (*outbuf)[0] = v&0xff;
|
---|
457 | (*outbuf)[1] = v>>8;
|
---|
458 | (*inbytesleft) -= 5;
|
---|
459 | (*outbytesleft) -= 2;
|
---|
460 | (*inbuf) += 5;
|
---|
461 | (*outbuf) += 2;
|
---|
462 | }
|
---|
463 |
|
---|
464 | if (*inbytesleft > 0) {
|
---|
465 | errno = E2BIG;
|
---|
466 | return -1;
|
---|
467 | }
|
---|
468 |
|
---|
469 | return 0;
|
---|
470 | }
|
---|
471 |
|
---|
472 | static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
473 | char **outbuf, size_t *outbytesleft)
|
---|
474 | {
|
---|
475 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
476 | char buf[6];
|
---|
477 |
|
---|
478 | if ((*inbuf)[1] == 0 &&
|
---|
479 | ((*inbuf)[0] & 0x80) == 0 &&
|
---|
480 | (*inbuf)[0] != '@') {
|
---|
481 | (*outbuf)[0] = (*inbuf)[0];
|
---|
482 | (*inbytesleft) -= 2;
|
---|
483 | (*outbytesleft) -= 1;
|
---|
484 | (*inbuf) += 2;
|
---|
485 | (*outbuf) += 1;
|
---|
486 | continue;
|
---|
487 | }
|
---|
488 | if (*outbytesleft < 5) {
|
---|
489 | errno = E2BIG;
|
---|
490 | return -1;
|
---|
491 | }
|
---|
492 | snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
|
---|
493 | memcpy(*outbuf, buf, 5);
|
---|
494 | (*inbytesleft) -= 2;
|
---|
495 | (*outbytesleft) -= 5;
|
---|
496 | (*inbuf) += 2;
|
---|
497 | (*outbuf) += 5;
|
---|
498 | }
|
---|
499 |
|
---|
500 | if (*inbytesleft == 1) {
|
---|
501 | errno = EINVAL;
|
---|
502 | return -1;
|
---|
503 | }
|
---|
504 |
|
---|
505 | if (*inbytesleft > 1) {
|
---|
506 | errno = E2BIG;
|
---|
507 | return -1;
|
---|
508 | }
|
---|
509 |
|
---|
510 | return 0;
|
---|
511 | }
|
---|
512 |
|
---|
513 | static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
514 | char **outbuf, size_t *outbytesleft)
|
---|
515 | {
|
---|
516 | int n;
|
---|
517 |
|
---|
518 | n = MIN(*inbytesleft, *outbytesleft);
|
---|
519 |
|
---|
520 | swab(*inbuf, *outbuf, (n&~1));
|
---|
521 | if (n&1) {
|
---|
522 | (*outbuf)[n-1] = 0;
|
---|
523 | }
|
---|
524 |
|
---|
525 | (*inbytesleft) -= n;
|
---|
526 | (*outbytesleft) -= n;
|
---|
527 | (*inbuf) += n;
|
---|
528 | (*outbuf) += n;
|
---|
529 |
|
---|
530 | if (*inbytesleft > 0) {
|
---|
531 | errno = E2BIG;
|
---|
532 | return -1;
|
---|
533 | }
|
---|
534 |
|
---|
535 | return 0;
|
---|
536 | }
|
---|
537 |
|
---|
538 | static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
539 | char **outbuf, size_t *outbytesleft)
|
---|
540 | {
|
---|
541 | int n;
|
---|
542 |
|
---|
543 | n = MIN(*inbytesleft, *outbytesleft);
|
---|
544 |
|
---|
545 | memmove(*outbuf, *inbuf, n);
|
---|
546 |
|
---|
547 | (*inbytesleft) -= n;
|
---|
548 | (*outbytesleft) -= n;
|
---|
549 | (*inbuf) += n;
|
---|
550 | (*outbuf) += n;
|
---|
551 |
|
---|
552 | if (*inbytesleft > 0) {
|
---|
553 | errno = E2BIG;
|
---|
554 | return -1;
|
---|
555 | }
|
---|
556 |
|
---|
557 | return 0;
|
---|
558 | }
|
---|
559 |
|
---|
560 | static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
561 | char **outbuf, size_t *outbytesleft)
|
---|
562 | {
|
---|
563 | size_t in_left=*inbytesleft, out_left=*outbytesleft;
|
---|
564 | const uint8 *c = (const uint8 *)*inbuf;
|
---|
565 | uint8 *uc = (uint8 *)*outbuf;
|
---|
566 |
|
---|
567 | while (in_left >= 1 && out_left >= 2) {
|
---|
568 | unsigned int codepoint;
|
---|
569 |
|
---|
570 | if ((c[0] & 0x80) == 0) {
|
---|
571 | uc[0] = c[0];
|
---|
572 | uc[1] = 0;
|
---|
573 | c += 1;
|
---|
574 | in_left -= 1;
|
---|
575 | out_left -= 2;
|
---|
576 | uc += 2;
|
---|
577 | continue;
|
---|
578 | }
|
---|
579 |
|
---|
580 | if ((c[0] & 0xe0) == 0xc0) {
|
---|
581 | if (in_left < 2 ||
|
---|
582 | (c[1] & 0xc0) != 0x80) {
|
---|
583 | errno = EILSEQ;
|
---|
584 | goto error;
|
---|
585 | }
|
---|
586 | codepoint = (c[1]&0x3f) | ((c[0]&0x1f)<<6);
|
---|
587 | if (codepoint < 0x80) {
|
---|
588 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
589 | errno = EILSEQ;
|
---|
590 | goto error;
|
---|
591 | }
|
---|
592 | uc[1] = codepoint >> 8;
|
---|
593 | uc[0] = codepoint & 0xff;
|
---|
594 | c += 2;
|
---|
595 | in_left -= 2;
|
---|
596 | out_left -= 2;
|
---|
597 | uc += 2;
|
---|
598 | continue;
|
---|
599 | }
|
---|
600 |
|
---|
601 | if ((c[0] & 0xf0) == 0xe0) {
|
---|
602 | if (in_left < 3 ||
|
---|
603 | (c[1] & 0xc0) != 0x80 ||
|
---|
604 | (c[2] & 0xc0) != 0x80) {
|
---|
605 | errno = EILSEQ;
|
---|
606 | goto error;
|
---|
607 | }
|
---|
608 | codepoint = (c[2]&0x3f) | ((c[1]&0x3f)<<6) | ((c[0]&0xf)<<12);
|
---|
609 | if (codepoint < 0x800) {
|
---|
610 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
611 | errno = EILSEQ;
|
---|
612 | goto error;
|
---|
613 | }
|
---|
614 | uc[1] = codepoint >> 8;
|
---|
615 | uc[0] = codepoint & 0xff;
|
---|
616 | c += 3;
|
---|
617 | in_left -= 3;
|
---|
618 | out_left -= 2;
|
---|
619 | uc += 2;
|
---|
620 | continue;
|
---|
621 | }
|
---|
622 |
|
---|
623 | if ((c[0] & 0xf8) == 0xf0) {
|
---|
624 | if (in_left < 4 ||
|
---|
625 | (c[1] & 0xc0) != 0x80 ||
|
---|
626 | (c[2] & 0xc0) != 0x80 ||
|
---|
627 | (c[3] & 0xc0) != 0x80) {
|
---|
628 | errno = EILSEQ;
|
---|
629 | goto error;
|
---|
630 | }
|
---|
631 | codepoint =
|
---|
632 | (c[3]&0x3f) |
|
---|
633 | ((c[2]&0x3f)<<6) |
|
---|
634 | ((c[1]&0x3f)<<12) |
|
---|
635 | ((c[0]&0x7)<<18);
|
---|
636 | if (codepoint < 0x10000 || codepoint > 0x10ffff) {
|
---|
637 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
638 | errno = EILSEQ;
|
---|
639 | goto error;
|
---|
640 | }
|
---|
641 |
|
---|
642 | codepoint -= 0x10000;
|
---|
643 |
|
---|
644 | if (out_left < 4) {
|
---|
645 | errno = E2BIG;
|
---|
646 | goto error;
|
---|
647 | }
|
---|
648 |
|
---|
649 | uc[0] = (codepoint>>10) & 0xFF;
|
---|
650 | uc[1] = (codepoint>>18) | 0xd8;
|
---|
651 | uc[2] = codepoint & 0xFF;
|
---|
652 | uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
|
---|
653 | c += 4;
|
---|
654 | in_left -= 4;
|
---|
655 | out_left -= 4;
|
---|
656 | uc += 4;
|
---|
657 | continue;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /* we don't handle 5 byte sequences */
|
---|
661 | errno = EINVAL;
|
---|
662 | goto error;
|
---|
663 | }
|
---|
664 |
|
---|
665 | if (in_left > 0) {
|
---|
666 | errno = E2BIG;
|
---|
667 | goto error;
|
---|
668 | }
|
---|
669 |
|
---|
670 | *inbytesleft = in_left;
|
---|
671 | *outbytesleft = out_left;
|
---|
672 | *inbuf = (char *)c;
|
---|
673 | *outbuf = (char *)uc;
|
---|
674 | return 0;
|
---|
675 |
|
---|
676 | error:
|
---|
677 | *inbytesleft = in_left;
|
---|
678 | *outbytesleft = out_left;
|
---|
679 | *inbuf = (char *)c;
|
---|
680 | *outbuf = (char *)uc;
|
---|
681 | return -1;
|
---|
682 | }
|
---|
683 |
|
---|
684 | static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
685 | char **outbuf, size_t *outbytesleft)
|
---|
686 | {
|
---|
687 | size_t in_left=*inbytesleft, out_left=*outbytesleft;
|
---|
688 | uint8 *c = (uint8 *)*outbuf;
|
---|
689 | const uint8 *uc = (const uint8 *)*inbuf;
|
---|
690 |
|
---|
691 | while (in_left >= 2 && out_left >= 1) {
|
---|
692 | unsigned int codepoint;
|
---|
693 |
|
---|
694 | if (uc[1] == 0 && !(uc[0] & 0x80)) {
|
---|
695 | /* simplest case */
|
---|
696 | c[0] = uc[0];
|
---|
697 | in_left -= 2;
|
---|
698 | out_left -= 1;
|
---|
699 | uc += 2;
|
---|
700 | c += 1;
|
---|
701 | continue;
|
---|
702 | }
|
---|
703 |
|
---|
704 | if ((uc[1]&0xf8) == 0) {
|
---|
705 | /* next simplest case */
|
---|
706 | if (out_left < 2) {
|
---|
707 | errno = E2BIG;
|
---|
708 | goto error;
|
---|
709 | }
|
---|
710 | c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
|
---|
711 | c[1] = 0x80 | (uc[0] & 0x3f);
|
---|
712 | in_left -= 2;
|
---|
713 | out_left -= 2;
|
---|
714 | uc += 2;
|
---|
715 | c += 2;
|
---|
716 | continue;
|
---|
717 | }
|
---|
718 |
|
---|
719 | if ((uc[1] & 0xfc) == 0xdc) {
|
---|
720 | /* its the second part of a 4 byte sequence. Illegal */
|
---|
721 | if (in_left < 4) {
|
---|
722 | errno = EINVAL;
|
---|
723 | } else {
|
---|
724 | errno = EILSEQ;
|
---|
725 | }
|
---|
726 | goto error;
|
---|
727 | }
|
---|
728 |
|
---|
729 | if ((uc[1] & 0xfc) != 0xd8) {
|
---|
730 | codepoint = uc[0] | (uc[1]<<8);
|
---|
731 | if (out_left < 3) {
|
---|
732 | errno = E2BIG;
|
---|
733 | goto error;
|
---|
734 | }
|
---|
735 | c[0] = 0xe0 | (codepoint >> 12);
|
---|
736 | c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
|
---|
737 | c[2] = 0x80 | (codepoint & 0x3f);
|
---|
738 |
|
---|
739 | in_left -= 2;
|
---|
740 | out_left -= 3;
|
---|
741 | uc += 2;
|
---|
742 | c += 3;
|
---|
743 | continue;
|
---|
744 | }
|
---|
745 |
|
---|
746 | /* its the first part of a 4 byte sequence */
|
---|
747 | if (in_left < 4) {
|
---|
748 | errno = EINVAL;
|
---|
749 | goto error;
|
---|
750 | }
|
---|
751 | if ((uc[3] & 0xfc) != 0xdc) {
|
---|
752 | errno = EILSEQ;
|
---|
753 | goto error;
|
---|
754 | }
|
---|
755 | codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
|
---|
756 | (uc[0]<<10) | ((uc[1] & 0x3)<<18));
|
---|
757 |
|
---|
758 | if (out_left < 4) {
|
---|
759 | errno = E2BIG;
|
---|
760 | goto error;
|
---|
761 | }
|
---|
762 | c[0] = 0xf0 | (codepoint >> 18);
|
---|
763 | c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
|
---|
764 | c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
|
---|
765 | c[3] = 0x80 | (codepoint & 0x3f);
|
---|
766 |
|
---|
767 | in_left -= 4;
|
---|
768 | out_left -= 4;
|
---|
769 | uc += 4;
|
---|
770 | c += 4;
|
---|
771 | }
|
---|
772 |
|
---|
773 | if (in_left == 1) {
|
---|
774 | errno = EINVAL;
|
---|
775 | goto error;
|
---|
776 | }
|
---|
777 |
|
---|
778 | if (in_left > 1) {
|
---|
779 | errno = E2BIG;
|
---|
780 | goto error;
|
---|
781 | }
|
---|
782 |
|
---|
783 | *inbytesleft = in_left;
|
---|
784 | *outbytesleft = out_left;
|
---|
785 | *inbuf = (char *)uc;
|
---|
786 | *outbuf = (char *)c;
|
---|
787 |
|
---|
788 | return 0;
|
---|
789 |
|
---|
790 | error:
|
---|
791 | *inbytesleft = in_left;
|
---|
792 | *outbytesleft = out_left;
|
---|
793 | *inbuf = (char *)uc;
|
---|
794 | *outbuf = (char *)c;
|
---|
795 | return -1;
|
---|
796 | }
|
---|
797 |
|
---|