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.
21 From samba 3.0 beta and GNU libiconv-1.8
22 It's bad but most of the time we can't use libc iconv service:
23 - it doesn't round trip for most encoding
24 - it doesn't know about Apple extension
29 #endif /* HAVE_CONFIG_H */
35 #include <sys/param.h>
37 #include <atalk/logger.h>
40 #include <netatalk/endian.h>
41 #include <atalk/unicode.h>
43 #ifdef HAVE_USABLE_ICONV
48 #include "mac_roman.h"
49 #include "mac_hebrew.h"
54 * @brief Samba wrapper/stub for iconv character set conversion.
56 * iconv is the XPG2 interface for converting between character
57 * encodings. This file provides a Samba wrapper around it, and also
58 * a simple reimplementation that is used if the system does not
61 * Samba only works with encodings that are supersets of ASCII: ascii
62 * characters like whitespace can be tested for directly, multibyte
63 * sequences start with a byte with the high bit set, and strings are
64 * terminated by a nul byte.
66 * Note that the only function provided by iconv is conversion between
67 * characters. It doesn't directly support operations like
68 * uppercasing or comparison. We have to convert to UCS-2 and compare
71 * @sa Samba Developers Guide
74 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
75 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
76 static size_t utf8_pull(void *,char **, size_t *, char **, size_t *);
77 static size_t utf8_push(void *,char **, size_t *, char **, size_t *);
78 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
80 static size_t mac_pull(void *,char **, size_t *, char **, size_t *);
81 static size_t mac_push(void *,char **, size_t *, char **, size_t *);
83 static size_t mac_hebrew_pull(void *,char **, size_t *, char **, size_t *);
84 static size_t mac_hebrew_push(void *,char **, size_t *, char **, size_t *);
86 static struct charset_functions builtin_functions[] = {
87 {"UCS-2LE", iconv_copy, iconv_copy},
88 {"UTF8", utf8_pull, utf8_push},
89 {"UTF-8", utf8_pull, utf8_push},
90 {"ASCII", ascii_pull, ascii_push},
91 {"MAC", mac_pull, mac_push},
92 {"MAC-HEBR", mac_hebrew_pull, mac_hebrew_push},
96 #define DLIST_ADD(list, p) \
100 (p)->next = (p)->prev = NULL; \
102 (list)->prev = (p); \
103 (p)->next = (list); \
111 static struct charset_functions *charsets = NULL;
113 static struct charset_functions *find_charset_functions(const char *name)
115 struct charset_functions *c = charsets;
118 if (strcasecmp(name, c->name) == 0) {
127 int atalk_register_charset(struct charset_functions *funcs)
133 LOG(log_debug, logtype_default, "Attempting to register new charset %s", funcs->name);
134 /* Check whether we already have this charset... */
135 if (find_charset_functions(funcs->name)) {
136 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
140 funcs->next = funcs->prev = NULL;
141 LOG(log_debug, logtype_default, "Registered charset %s", funcs->name);
142 DLIST_ADD(charsets, funcs);
146 void lazy_initialize_iconv(void)
148 static int initialized = 0;
153 for(i = 0; builtin_functions[i].name; i++)
154 atalk_register_charset(&builtin_functions[i]);
158 /* if there was an error then reset the internal state,
159 this ensures that we don't have a shift state remaining for
160 character sets like SJIS */
161 static size_t sys_iconv(void *cd,
162 char **inbuf, size_t *inbytesleft,
163 char **outbuf, size_t *outbytesleft)
165 #ifdef HAVE_USABLE_ICONV
166 size_t ret = iconv((iconv_t)cd,
168 outbuf, outbytesleft);
169 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
178 * This is a simple portable iconv() implementaion.
180 * It only knows about a very small number of character sets - just
181 * enough that netatalk works on systems that don't have iconv.
183 size_t atalk_iconv(atalk_iconv_t cd,
184 const char **inbuf, size_t *inbytesleft,
185 char **outbuf, size_t *outbytesleft)
191 /* in many cases we can go direct */
193 return cd->direct(cd->cd_direct,
194 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
198 /* otherwise we have to do it chunks at a time */
199 while (*inbytesleft > 0) {
201 bufsize = sizeof(cvtbuf);
203 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
209 bufsize = sizeof(cvtbuf) - bufsize;
211 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
220 size_t atalk_iconv_ignore(atalk_iconv_t cd,
221 const char **inbuf, size_t *inbytesleft,
222 char **outbuf, size_t *outbytesleft, int *ignore)
227 size_t outlen = *outbytesleft;
230 /* we have to do it chunks at a time */
231 while (*inbytesleft > 0) {
233 bufsize = sizeof(cvtbuf);
235 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
241 bufsize = sizeof(cvtbuf) - bufsize;
245 if (cd->push(cd->cd_push,
247 outbuf, outbytesleft) == (size_t)-1) {
248 if (errno == EILSEQ) {
249 o_save[outlen-*outbytesleft] = '_';
250 (*outbuf) = o_save + outlen-*outbytesleft+1;
254 //outlen=*outbytesleft;
266 simple iconv_open() wrapper
268 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
271 struct charset_functions *from, *to;
274 lazy_initialize_iconv();
278 ret = (atalk_iconv_t)malloc(sizeof(*ret));
281 return (atalk_iconv_t)-1;
283 memset(ret, 0, sizeof(*ret));
285 ret->from_name = strdup(fromcode);
286 ret->to_name = strdup(tocode);
288 /* check for the simplest null conversion */
289 if (strcasecmp(fromcode, tocode) == 0) {
290 ret->direct = iconv_copy;
294 /* check if we have a builtin function for this conversion */
295 from = find_charset_functions(fromcode);
296 if(from)ret->pull = from->pull;
298 to = find_charset_functions(tocode);
299 if(to)ret->push = to->push;
301 /* check if we can use iconv for this conversion */
302 #ifdef HAVE_USABLE_ICONV
304 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
305 if (ret->cd_pull != (iconv_t)-1)
306 ret->pull = sys_iconv;
310 ret->cd_push = iconv_open(tocode, "UCS-2LE");
311 if (ret->cd_push != (iconv_t)-1)
312 ret->push = sys_iconv;
316 if (!ret->push || !ret->pull) {
317 SAFE_FREE(ret->from_name);
318 SAFE_FREE(ret->to_name);
321 return (atalk_iconv_t)-1;
324 /* check for conversion to/from ucs2 */
325 if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
326 ret->direct = to->push;
327 ret->push = ret->pull = NULL;
331 if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
332 ret->direct = from->pull;
333 ret->push = ret->pull = NULL;
337 /* Check if we can do the conversion direct */
338 #ifdef HAVE_USABLE_ICONV
339 if (strcasecmp(fromcode, "UCS-2LE") == 0) {
340 ret->direct = sys_iconv;
341 ret->cd_direct = ret->cd_push;
345 if (strcasecmp(tocode, "UCS-2LE") == 0) {
346 ret->direct = sys_iconv;
347 ret->cd_direct = ret->cd_pull;
357 simple iconv_close() wrapper
359 int atalk_iconv_close (atalk_iconv_t cd)
361 #ifdef HAVE_USABLE_ICONV
362 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
363 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
364 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
367 SAFE_FREE(cd->from_name);
368 SAFE_FREE(cd->to_name);
370 memset(cd, 0, sizeof(*cd));
376 /************************************************************************
377 the following functions implement the builtin character sets in Netatalk
378 *************************************************************************/
380 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
381 char **outbuf, size_t *outbytesleft)
383 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
384 (*outbuf)[0] = (*inbuf)[0];
387 (*outbytesleft) -= 2;
392 if (*inbytesleft > 0) {
400 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
401 char **outbuf, size_t *outbytesleft)
405 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
406 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
407 if ((*inbuf)[1]) ir_count++;
409 (*outbytesleft) -= 1;
414 if (*inbytesleft == 1) {
419 if (*inbytesleft > 1) {
428 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
429 char **outbuf, size_t *outbytesleft)
433 n = MIN(*inbytesleft, *outbytesleft);
435 memmove(*outbuf, *inbuf, n);
438 (*outbytesleft) -= n;
442 if (*inbytesleft > 0) {
450 /* ------------------------ */
451 static size_t utf8_pull(void *cd, char **inbuf, size_t *inbytesleft,
452 char **outbuf, size_t *outbytesleft)
454 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
455 unsigned char *c = (unsigned char *)*inbuf;
456 unsigned char *uc = (unsigned char *)*outbuf;
459 if ((c[0] & 0x80) == 0) {
462 } else if ((c[0] & 0xf0) == 0xe0) {
463 if (*inbytesleft < 3) {
464 LOG(log_debug, logtype_default, "short utf8 char\n");
467 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
468 uc[0] = (c[1]<<6) | (c[2]&0x3f);
470 } else if ((c[0] & 0xe0) == 0xc0) {
471 if (*inbytesleft < 2) {
472 LOG(log_debug, logtype_default, "short utf8 char\n");
475 uc[1] = (c[0]>>2) & 0x7;
476 uc[0] = (c[0]<<6) | (c[1]&0x3f);
481 (*inbytesleft) -= len;
482 (*outbytesleft) -= 2;
486 if (*inbytesleft > 0) {
498 /* ------------------------ */
499 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
500 char **outbuf, size_t *outbytesleft)
502 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
503 unsigned char *c = (unsigned char *)*outbuf;
504 unsigned char *uc = (unsigned char *)*inbuf;
508 if (*outbytesleft < 3) {
509 LOG(log_debug, logtype_default, "short utf8 write\n");
512 c[0] = 0xe0 | (uc[1]>>4);
513 c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
514 c[2] = 0x80 | (uc[0]&0x3f);
516 } else if (uc[1] | (uc[0] & 0x80)) {
517 if (*outbytesleft < 2) {
518 LOG(log_debug, logtype_default, "short utf8 write\n");
521 c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
522 c[1] = 0x80 | (uc[0]&0x3f);
530 (*outbytesleft) -= len;
535 if (*inbytesleft == 1) {
540 if (*inbytesleft > 1) {
552 /* ------------------------ */
554 char_ucs2_to_mac_roman ( unsigned char *r, ucs2_t wc)
561 else if (wc >= 0x00a0 && wc < 0x0100)
562 c = mac_roman_page00[wc-0x00a0];
563 else if (wc >= 0x0130 && wc < 0x0198)
564 c = mac_roman_page01[wc-0x0130];
565 else if (wc >= 0x02c0 && wc < 0x02e0)
566 c = mac_roman_page02[wc-0x02c0];
567 else if (wc == 0x03c0)
569 else if (wc >= 0x2010 && wc < 0x2048)
570 c = mac_roman_page20[wc-0x2010];
571 else if (wc >= 0x2120 && wc < 0x2128)
572 c = mac_roman_page21[wc-0x2120];
573 else if (wc >= 0x2200 && wc < 0x2268)
574 c = mac_roman_page22[wc-0x2200];
575 else if (wc == 0x25ca)
577 else if (wc >= 0xfb00 && wc < 0xfb08)
578 c = mac_roman_pagefb[wc-0xfb00];
579 else if (wc == 0xf8ff)
589 static size_t mac_push( void *cd, char **inbuf, size_t *inbytesleft,
590 char **outbuf, size_t *outbytesleft)
593 unsigned char *tmpptr = (unsigned char *) *outbuf;
595 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
597 ucs2_t *inptr = (ucs2_t *) *inbuf;
598 if (char_ucs2_to_mac_roman ( tmpptr, *inptr)) {
603 (*outbytesleft) -= 1;
612 if (*inbytesleft > 0) {
620 /* ------------------------ */
622 char_mac_roman_to_ucs2 (ucs2_t *pwc, const unsigned char *s)
624 unsigned char c = *s;
630 unsigned short wc = mac_roman_2uni[c-0x80];
637 static size_t mac_pull ( void *cd, char **inbuf, size_t *inbytesleft,
638 char **outbuf, size_t *outbytesleft)
641 unsigned char *inptr;
644 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
646 inptr = (unsigned char *) *inbuf;
647 temp = (ucs2_t*) *outbuf;
648 if (char_mac_roman_to_ucs2 ( temp, inptr)) {
663 if (*inbytesleft > 0) {
672 /* ------------------------
673 * from unicode to mac hebrew code page
676 char_ucs2_to_mac_hebrew ( unsigned char *r, ucs2_t wc)
683 else if (wc >= 0x00a0 && wc < 0x0100)
684 c = mac_hebrew_page00[wc-0x00a0];
685 else if (wc >= 0x05b0 && wc < 0x05f0)
686 c = mac_hebrew_page05[wc-0x05b0];
687 else if (wc >= 0x2010 && wc < 0x2028)
688 c = mac_hebrew_page20[wc-0x2010];
689 else if (wc == 0x20aa)
691 else if (wc >= 0xfb18 && wc < 0xfb50)
692 c = mac_hebrew_pagefb[wc-0xfb18];
700 static size_t mac_hebrew_push( void *cd, char **inbuf, size_t *inbytesleft,
701 char **outbuf, size_t *outbytesleft)
705 unsigned char *tmpptr = (unsigned char *) *outbuf;
707 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
708 ucs2_t *inptr = (ucs2_t *) *inbuf;
709 if (*inptr == 0x05b8) {
712 if (*inbytesleft >= 2 && *((ucs2_t *)*inbuf) == 0xf87f ) {
722 else if (*inptr == 0x05f2 && *inbytesleft >= 4 && *(inptr +1) == 0x05b7) {
727 else if (*inptr == 0xf86a && *inbytesleft >= 6 && *(inptr +1) == 0x05dc && *(inptr +2) == 0x05b9) {
732 else if (char_ucs2_to_mac_hebrew ( tmpptr, *inptr)) {
740 (*outbytesleft) -= 1;
745 if (*inbytesleft > 0) {
753 /* ------------------------ */
755 char_mac_hebrew_to_ucs2 (ucs2_t *pwc, const unsigned char *s)
757 unsigned char c = *s;
763 unsigned short wc = mac_hebrew_2uni[c-0x80];
772 static size_t mac_hebrew_pull ( void *cd, char **inbuf, size_t *inbytesleft,
773 char **outbuf, size_t *outbytesleft)
776 unsigned char *inptr;
779 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
780 inptr = (unsigned char *) *inbuf;
781 temp = (ucs2_t*) *outbuf;
782 if (char_mac_hebrew_to_ucs2 ( temp, inptr)) {
783 if (*temp == 1) { /* 0x81 --> 0x05f2+0x05b7 */
784 if (*outbytesleft < 4) {
794 else if (*temp == 2) { /* 0xc0 -> 0xf86a 0x05dc 0x05b9*/
795 if (*outbytesleft < 6) {
806 else if (*temp == 3) { /* 0xde --> 0x05b8 0xf87f */
807 if (*outbytesleft < 4) {
832 if (*inbytesleft > 0) {