If we have blacklisted mmap() try to avoid using it accidentally by
[Samba/gebeck_regimport.git] / source / lib / iconv.c
blob0326ca70611db5bb27f426490a52e3f645d0c476
1 /*
2 Unix SMB/CIFS implementation.
3 minimal iconv implementation
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jelmer Vernooij 2002,2003
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
25 /**
26 * @file
28 * @brief Samba wrapper/stub for iconv character set conversion.
30 * iconv is the XPG2 interface for converting between character
31 * encodings. This file provides a Samba wrapper around it, and also
32 * a simple reimplementation that is used if the system does not
33 * implement iconv.
35 * Samba only works with encodings that are supersets of ASCII: ascii
36 * characters like whitespace can be tested for directly, multibyte
37 * sequences start with a byte with the high bit set, and strings are
38 * terminated by a nul byte.
40 * Note that the only function provided by iconv is conversion between
41 * characters. It doesn't directly support operations like
42 * uppercasing or comparison. We have to convert to UCS-2 and compare
43 * there.
45 * @sa Samba Developers Guide
46 **/
48 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
49 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
50 static size_t utf8_pull(void *,char **, size_t *, char **, size_t *);
51 static size_t utf8_push(void *,char **, size_t *, char **, size_t *);
52 static size_t ucs2hex_pull(void *,char **, size_t *, char **, size_t *);
53 static size_t ucs2hex_push(void *,char **, size_t *, char **, size_t *);
54 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
56 static struct charset_functions builtin_functions[] = {
57 {"UCS-2LE", iconv_copy, iconv_copy},
58 {"UTF8", utf8_pull, utf8_push},
59 {"ASCII", ascii_pull, ascii_push},
60 {"646", ascii_pull, ascii_push},
61 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
62 {NULL, NULL, NULL}
65 static struct charset_functions *charsets = NULL;
67 static struct charset_functions *find_charset_functions(const char *name)
69 struct charset_functions *c = charsets;
71 while(c) {
72 if (strcasecmp(name, c->name) == 0) {
73 return c;
75 c = c->next;
78 return NULL;
81 NTSTATUS smb_register_charset(struct charset_functions *funcs)
83 if (!funcs) {
84 return NT_STATUS_INVALID_PARAMETER;
87 DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
88 /* Check whether we already have this charset... */
89 if (find_charset_functions(funcs->name)) {
90 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs->name));
91 return NT_STATUS_OBJECT_NAME_COLLISION;
94 funcs->next = funcs->prev = NULL;
95 DEBUG(5, ("Registered charset %s\n", funcs->name));
96 DLIST_ADD(charsets, funcs);
97 return NT_STATUS_OK;
100 void lazy_initialize_iconv(void)
102 static BOOL initialized;
103 int i;
105 if (!initialized) {
106 initialized = True;
107 for(i = 0; builtin_functions[i].name; i++)
108 smb_register_charset(&builtin_functions[i]);
109 static_init_charset;
113 /* if there was an error then reset the internal state,
114 this ensures that we don't have a shift state remaining for
115 character sets like SJIS */
116 static size_t sys_iconv(void *cd,
117 char **inbuf, size_t *inbytesleft,
118 char **outbuf, size_t *outbytesleft)
120 #ifdef HAVE_NATIVE_ICONV
121 size_t ret = iconv((iconv_t)cd,
122 inbuf, inbytesleft,
123 outbuf, outbytesleft);
124 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
125 return ret;
126 #else
127 errno = EINVAL;
128 return -1;
129 #endif
133 * This is a simple portable iconv() implementaion.
135 * It only knows about a very small number of character sets - just
136 * enough that Samba works on systems that don't have iconv.
138 size_t smb_iconv(smb_iconv_t cd,
139 char **inbuf, size_t *inbytesleft,
140 char **outbuf, size_t *outbytesleft)
142 char cvtbuf[2048];
143 char *bufp = cvtbuf;
144 size_t bufsize;
146 /* in many cases we can go direct */
147 if (cd->direct) {
148 return cd->direct(cd->cd_direct,
149 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
153 /* otherwise we have to do it chunks at a time */
154 while (*inbytesleft > 0) {
155 bufp = cvtbuf;
156 bufsize = sizeof(cvtbuf);
158 if (cd->pull(cd->cd_pull,
159 (char **)inbuf, inbytesleft, &bufp, &bufsize) == -1
160 && errno != E2BIG) return -1;
162 bufp = cvtbuf;
163 bufsize = sizeof(cvtbuf) - bufsize;
165 if (cd->push(cd->cd_push,
166 &bufp, &bufsize,
167 outbuf, outbytesleft) == -1) return -1;
170 return 0;
174 simple iconv_open() wrapper
176 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
178 smb_iconv_t ret;
179 struct charset_functions *from, *to;
181 lazy_initialize_iconv();
182 from = charsets;
183 to = charsets;
185 ret = (smb_iconv_t)malloc(sizeof(*ret));
186 if (!ret) {
187 errno = ENOMEM;
188 return (smb_iconv_t)-1;
190 memset(ret, 0, sizeof(*ret));
192 ret->from_name = strdup(fromcode);
193 ret->to_name = strdup(tocode);
195 /* check for the simplest null conversion */
196 if (strcasecmp(fromcode, tocode) == 0) {
197 ret->direct = iconv_copy;
198 return ret;
201 /* check if we have a builtin function for this conversion */
202 from = find_charset_functions(fromcode);
203 if(from)ret->pull = from->pull;
205 to = find_charset_functions(tocode);
206 if(to)ret->push = to->push;
208 /* check if we can use iconv for this conversion */
209 #ifdef HAVE_NATIVE_ICONV
210 if (!ret->pull) {
211 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
212 if (ret->cd_pull != (iconv_t)-1)
213 ret->pull = sys_iconv;
216 if (!ret->push) {
217 ret->cd_push = iconv_open(tocode, "UCS-2LE");
218 if (ret->cd_push != (iconv_t)-1)
219 ret->push = sys_iconv;
221 #endif
223 /* check if there is a module available that can do this conversion */
224 if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
225 if(!(from = find_charset_functions(fromcode)))
226 DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
227 else
228 ret->pull = from->pull;
231 if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
232 if(!(to = find_charset_functions(tocode)))
233 DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
234 else
235 ret->push = to->push;
238 if (!ret->push || !ret->pull) {
239 SAFE_FREE(ret->from_name);
240 SAFE_FREE(ret->to_name);
241 SAFE_FREE(ret);
242 errno = EINVAL;
243 return (smb_iconv_t)-1;
246 /* check for conversion to/from ucs2 */
247 if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
248 ret->direct = to->push;
249 ret->push = ret->pull = NULL;
250 return ret;
253 if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
254 ret->direct = from->pull;
255 ret->push = ret->pull = NULL;
256 return ret;
259 /* Check if we can do the conversion direct */
260 #ifdef HAVE_NATIVE_ICONV
261 if (strcasecmp(fromcode, "UCS-2LE") == 0) {
262 ret->direct = sys_iconv;
263 ret->cd_direct = ret->cd_push;
264 ret->cd_push = NULL;
265 return ret;
267 if (strcasecmp(tocode, "UCS-2LE") == 0) {
268 ret->direct = sys_iconv;
269 ret->cd_direct = ret->cd_pull;
270 ret->cd_pull = NULL;
271 return ret;
273 #endif
275 return ret;
279 simple iconv_close() wrapper
281 int smb_iconv_close (smb_iconv_t cd)
283 #ifdef HAVE_NATIVE_ICONV
284 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
285 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
286 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
287 #endif
289 SAFE_FREE(cd->from_name);
290 SAFE_FREE(cd->to_name);
292 memset(cd, 0, sizeof(*cd));
293 SAFE_FREE(cd);
294 return 0;
298 /**********************************************************************
299 the following functions implement the builtin character sets in Samba
300 and also the "test" character sets that are designed to test
301 multi-byte character set support for english users
302 ***********************************************************************/
304 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
305 char **outbuf, size_t *outbytesleft)
307 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
308 (*outbuf)[0] = (*inbuf)[0];
309 (*outbuf)[1] = 0;
310 (*inbytesleft) -= 1;
311 (*outbytesleft) -= 2;
312 (*inbuf) += 1;
313 (*outbuf) += 2;
316 if (*inbytesleft > 0) {
317 errno = E2BIG;
318 return -1;
321 return 0;
324 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
325 char **outbuf, size_t *outbytesleft)
327 int ir_count=0;
329 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
330 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
331 if ((*inbuf)[1]) ir_count++;
332 (*inbytesleft) -= 2;
333 (*outbytesleft) -= 1;
334 (*inbuf) += 2;
335 (*outbuf) += 1;
338 if (*inbytesleft == 1) {
339 errno = EINVAL;
340 return -1;
343 if (*inbytesleft > 1) {
344 errno = E2BIG;
345 return -1;
348 return ir_count;
352 static size_t ucs2hex_pull(void *cd, char **inbuf, size_t *inbytesleft,
353 char **outbuf, size_t *outbytesleft)
355 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
356 unsigned v;
358 if ((*inbuf)[0] != '@') {
359 /* seven bit ascii case */
360 (*outbuf)[0] = (*inbuf)[0];
361 (*outbuf)[1] = 0;
362 (*inbytesleft) -= 1;
363 (*outbytesleft) -= 2;
364 (*inbuf) += 1;
365 (*outbuf) += 2;
366 continue;
368 /* it's a hex character */
369 if (*inbytesleft < 5) {
370 errno = EINVAL;
371 return -1;
374 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
375 errno = EILSEQ;
376 return -1;
379 (*outbuf)[0] = v&0xff;
380 (*outbuf)[1] = v>>8;
381 (*inbytesleft) -= 5;
382 (*outbytesleft) -= 2;
383 (*inbuf) += 5;
384 (*outbuf) += 2;
387 if (*inbytesleft > 0) {
388 errno = E2BIG;
389 return -1;
392 return 0;
395 static size_t ucs2hex_push(void *cd, char **inbuf, size_t *inbytesleft,
396 char **outbuf, size_t *outbytesleft)
398 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
399 char buf[6];
401 if ((*inbuf)[1] == 0 &&
402 ((*inbuf)[0] & 0x80) == 0 &&
403 (*inbuf)[0] != '@') {
404 (*outbuf)[0] = (*inbuf)[0];
405 (*inbytesleft) -= 2;
406 (*outbytesleft) -= 1;
407 (*inbuf) += 2;
408 (*outbuf) += 1;
409 continue;
411 if (*outbytesleft < 5) {
412 errno = E2BIG;
413 return -1;
415 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
416 memcpy(*outbuf, buf, 5);
417 (*inbytesleft) -= 2;
418 (*outbytesleft) -= 5;
419 (*inbuf) += 2;
420 (*outbuf) += 5;
423 if (*inbytesleft == 1) {
424 errno = EINVAL;
425 return -1;
428 if (*inbytesleft > 1) {
429 errno = E2BIG;
430 return -1;
433 return 0;
437 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
438 char **outbuf, size_t *outbytesleft)
440 int n;
442 n = MIN(*inbytesleft, *outbytesleft);
444 memmove(*outbuf, *inbuf, n);
446 (*inbytesleft) -= n;
447 (*outbytesleft) -= n;
448 (*inbuf) += n;
449 (*outbuf) += n;
451 if (*inbytesleft > 0) {
452 errno = E2BIG;
453 return -1;
456 return 0;
459 static size_t utf8_pull(void *cd, char **inbuf, size_t *inbytesleft,
460 char **outbuf, size_t *outbytesleft)
462 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
463 unsigned char *c = (unsigned char *)*inbuf;
464 unsigned char *uc = (unsigned char *)*outbuf;
465 int len = 1;
467 if ((c[0] & 0x80) == 0) {
468 uc[0] = c[0];
469 uc[1] = 0;
470 } else if ((c[0] & 0xf0) == 0xe0) {
471 if (*inbytesleft < 3) {
472 DEBUG(0,("short utf8 char\n"));
473 goto badseq;
475 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
476 uc[0] = (c[1]<<6) | (c[2]&0x3f);
477 len = 3;
478 } else if ((c[0] & 0xe0) == 0xc0) {
479 if (*inbytesleft < 2) {
480 DEBUG(0,("short utf8 char\n"));
481 goto badseq;
483 uc[1] = (c[0]>>2) & 0x7;
484 uc[0] = (c[0]<<6) | (c[1]&0x3f);
485 len = 2;
488 (*inbuf) += len;
489 (*inbytesleft) -= len;
490 (*outbytesleft) -= 2;
491 (*outbuf) += 2;
494 if (*inbytesleft > 0) {
495 errno = E2BIG;
496 return -1;
499 return 0;
501 badseq:
502 errno = EINVAL;
503 return -1;
506 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
507 char **outbuf, size_t *outbytesleft)
509 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
510 unsigned char *c = (unsigned char *)*outbuf;
511 unsigned char *uc = (unsigned char *)*inbuf;
512 int len=1;
514 if (uc[1] & 0xf8) {
515 if (*outbytesleft < 3) {
516 DEBUG(0,("short utf8 write\n"));
517 goto toobig;
519 c[0] = 0xe0 | (uc[1]>>4);
520 c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
521 c[2] = 0x80 | (uc[0]&0x3f);
522 len = 3;
523 } else if (uc[1] | (uc[0] & 0x80)) {
524 if (*outbytesleft < 2) {
525 DEBUG(0,("short utf8 write\n"));
526 goto toobig;
528 c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
529 c[1] = 0x80 | (uc[0]&0x3f);
530 len = 2;
531 } else {
532 c[0] = uc[0];
536 (*inbytesleft) -= 2;
537 (*outbytesleft) -= len;
538 (*inbuf) += 2;
539 (*outbuf) += len;
542 if (*inbytesleft == 1) {
543 errno = EINVAL;
544 return -1;
547 if (*inbytesleft > 1) {
548 errno = E2BIG;
549 return -1;
552 return 0;
554 toobig:
555 errno = E2BIG;
556 return -1;