This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[samba.git] / source / lib / iconv.c
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 /**
26  * @file
27  *
28  * @brief Samba wrapper/stub for iconv character set conversion.
29  *
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.
34  *
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.
39  *
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.
44  *
45  * @sa Samba Developers Guide
46  **/
47
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 *);
55
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         {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
61         {NULL, NULL, NULL}
62 };
63
64 static struct charset_functions *charsets = NULL;
65
66 static struct charset_functions *find_charset_functions(const char *name) 
67 {
68         struct charset_functions *c = charsets;
69
70         while(c) {
71                 if (strcasecmp(name, c->name) == 0) {
72                         return c;
73                 }
74                 c = c->next;
75         }
76
77         return NULL;
78 }
79
80 NTSTATUS smb_register_charset(struct charset_functions *funcs) 
81 {
82         if (!funcs) {
83                 return NT_STATUS_INVALID_PARAMETER;
84         }
85
86         DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
87         /* Check whether we already have this charset... */
88         if (find_charset_functions(funcs->name)) {
89                 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs->name));
90                 return NT_STATUS_OBJECT_NAME_COLLISION;
91         }
92
93         funcs->next = funcs->prev = NULL;
94         DEBUG(5, ("Registered charset %s\n", funcs->name));
95         DLIST_ADD(charsets, funcs);
96         return NT_STATUS_OK;
97 }
98
99 void lazy_initialize_iconv(void)
100 {
101         static BOOL initialized;
102         int i;
103
104         if (!initialized) {
105                 initialized = True;
106                 for(i = 0; builtin_functions[i].name; i++) 
107                         smb_register_charset(&builtin_functions[i]);
108                 static_init_charset;
109         }
110 }
111
112 /* if there was an error then reset the internal state,
113    this ensures that we don't have a shift state remaining for
114    character sets like SJIS */
115 static size_t sys_iconv(void *cd, 
116                         char **inbuf, size_t *inbytesleft,
117                         char **outbuf, size_t *outbytesleft)
118 {
119 #ifdef HAVE_NATIVE_ICONV
120         size_t ret = iconv((iconv_t)cd, 
121                            inbuf, inbytesleft, 
122                            outbuf, outbytesleft);
123         if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
124         return ret;
125 #else
126         errno = EINVAL;
127         return -1;
128 #endif
129 }
130
131 /**
132  * This is a simple portable iconv() implementaion.
133  *
134  * It only knows about a very small number of character sets - just
135  * enough that Samba works on systems that don't have iconv.
136  **/
137 size_t smb_iconv(smb_iconv_t cd, 
138                  const char **inbuf, size_t *inbytesleft,
139                  char **outbuf, size_t *outbytesleft)
140 {
141         char cvtbuf[2048];
142         char *bufp = cvtbuf;
143         size_t bufsize;
144
145         /* in many cases we can go direct */
146         if (cd->direct) {
147                 return cd->direct(cd->cd_direct, 
148                                   (char **)inbuf, inbytesleft, outbuf, outbytesleft);
149         }
150
151
152         /* otherwise we have to do it chunks at a time */
153         while (*inbytesleft > 0) {
154                 bufp = cvtbuf;
155                 bufsize = sizeof(cvtbuf);
156                 
157                 if (cd->pull(cd->cd_pull, 
158                              (char **)inbuf, inbytesleft, &bufp, &bufsize) == -1
159                     && errno != E2BIG) return -1;
160
161                 bufp = cvtbuf;
162                 bufsize = sizeof(cvtbuf) - bufsize;
163
164                 if (cd->push(cd->cd_push, 
165                              &bufp, &bufsize, 
166                              outbuf, outbytesleft) == -1) return -1;
167         }
168
169         return 0;
170 }
171
172 /*
173   simple iconv_open() wrapper
174  */
175 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
176 {
177         smb_iconv_t ret;
178         struct charset_functions *from, *to;
179         
180         lazy_initialize_iconv();
181         from = charsets;
182         to = charsets;
183
184         ret = (smb_iconv_t)malloc(sizeof(*ret));
185         if (!ret) {
186                 errno = ENOMEM;
187                 return (smb_iconv_t)-1;
188         }
189         memset(ret, 0, sizeof(*ret));
190
191         ret->from_name = strdup(fromcode);
192         ret->to_name = strdup(tocode);
193
194         /* check for the simplest null conversion */
195         if (strcasecmp(fromcode, tocode) == 0) {
196                 ret->direct = iconv_copy;
197                 return ret;
198         }
199
200         /* check if we have a builtin function for this conversion */
201         from = find_charset_functions(fromcode);
202         if(from)ret->pull = from->pull;
203         
204         to = find_charset_functions(tocode);
205         if(to)ret->push = to->push;
206
207         /* check if we can use iconv for this conversion */
208 #ifdef HAVE_NATIVE_ICONV
209         if (!ret->pull) {
210                 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
211                 if (ret->cd_pull != (iconv_t)-1)
212                         ret->pull = sys_iconv;
213         }
214
215         if (!ret->push) {
216                 ret->cd_push = iconv_open(tocode, "UCS-2LE");
217                 if (ret->cd_push != (iconv_t)-1)
218                         ret->push = sys_iconv;
219         }
220 #endif
221         
222         /* check if there is a module available that can do this conversion */
223         if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
224                 if(!(from = find_charset_functions(fromcode)))
225                         DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
226                 else 
227                         ret->pull = from->pull;
228         }
229
230         if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
231                 if(!(to = find_charset_functions(tocode)))
232                         DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
233                 else 
234                         ret->push = to->push;
235         }
236
237         if (!ret->push || !ret->pull) {
238                 SAFE_FREE(ret->from_name);
239                 SAFE_FREE(ret->to_name);
240                 SAFE_FREE(ret);
241                 errno = EINVAL;
242                 return (smb_iconv_t)-1;
243         }
244
245         /* check for conversion to/from ucs2 */
246         if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
247                 ret->direct = to->push;
248                 ret->push = ret->pull = NULL;
249                 return ret;
250         }
251
252         if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
253                 ret->direct = from->pull;
254                 ret->push = ret->pull = NULL;
255                 return ret;
256         }
257
258         /* Check if we can do the conversion direct */
259 #ifdef HAVE_NATIVE_ICONV
260         if (strcasecmp(fromcode, "UCS-2LE") == 0) {
261                 ret->direct = sys_iconv;
262                 ret->cd_direct = ret->cd_push;
263                 ret->cd_push = NULL;
264                 return ret;
265         }
266         if (strcasecmp(tocode, "UCS-2LE") == 0) {
267                 ret->direct = sys_iconv;
268                 ret->cd_direct = ret->cd_pull;
269                 ret->cd_pull = NULL;
270                 return ret;
271         }
272 #endif
273
274         return ret;
275 }
276
277 /*
278   simple iconv_close() wrapper
279 */
280 int smb_iconv_close (smb_iconv_t cd)
281 {
282 #ifdef HAVE_NATIVE_ICONV
283         if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
284         if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
285         if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
286 #endif
287
288         SAFE_FREE(cd->from_name);
289         SAFE_FREE(cd->to_name);
290
291         memset(cd, 0, sizeof(*cd));
292         SAFE_FREE(cd);
293         return 0;
294 }
295
296
297 /**********************************************************************
298  the following functions implement the builtin character sets in Samba
299  and also the "test" character sets that are designed to test
300  multi-byte character set support for english users
301 ***********************************************************************/
302
303 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
304                          char **outbuf, size_t *outbytesleft)
305 {
306         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
307                 (*outbuf)[0] = (*inbuf)[0];
308                 (*outbuf)[1] = 0;
309                 (*inbytesleft)  -= 1;
310                 (*outbytesleft) -= 2;
311                 (*inbuf)  += 1;
312                 (*outbuf) += 2;
313         }
314
315         if (*inbytesleft > 0) {
316                 errno = E2BIG;
317                 return -1;
318         }
319         
320         return 0;
321 }
322
323 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
324                          char **outbuf, size_t *outbytesleft)
325 {
326         int ir_count=0;
327
328         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
329                 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
330                 if ((*inbuf)[1]) ir_count++;
331                 (*inbytesleft)  -= 2;
332                 (*outbytesleft) -= 1;
333                 (*inbuf)  += 2;
334                 (*outbuf) += 1;
335         }
336
337         if (*inbytesleft == 1) {
338                 errno = EINVAL;
339                 return -1;
340         }
341
342         if (*inbytesleft > 1) {
343                 errno = E2BIG;
344                 return -1;
345         }
346         
347         return ir_count;
348 }
349
350
351 static size_t ucs2hex_pull(void *cd, char **inbuf, size_t *inbytesleft,
352                          char **outbuf, size_t *outbytesleft)
353 {
354         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
355                 unsigned v;
356
357                 if ((*inbuf)[0] != '@') {
358                         /* seven bit ascii case */
359                         (*outbuf)[0] = (*inbuf)[0];
360                         (*outbuf)[1] = 0;
361                         (*inbytesleft)  -= 1;
362                         (*outbytesleft) -= 2;
363                         (*inbuf)  += 1;
364                         (*outbuf) += 2;
365                         continue;
366                 }
367                 /* it's a hex character */
368                 if (*inbytesleft < 5) {
369                         errno = EINVAL;
370                         return -1;
371                 }
372                 
373                 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
374                         errno = EILSEQ;
375                         return -1;
376                 }
377
378                 (*outbuf)[0] = v&0xff;
379                 (*outbuf)[1] = v>>8;
380                 (*inbytesleft)  -= 5;
381                 (*outbytesleft) -= 2;
382                 (*inbuf)  += 5;
383                 (*outbuf) += 2;
384         }
385
386         if (*inbytesleft > 0) {
387                 errno = E2BIG;
388                 return -1;
389         }
390         
391         return 0;
392 }
393
394 static size_t ucs2hex_push(void *cd, char **inbuf, size_t *inbytesleft,
395                            char **outbuf, size_t *outbytesleft)
396 {
397         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
398                 char buf[6];
399
400                 if ((*inbuf)[1] == 0 && 
401                     ((*inbuf)[0] & 0x80) == 0 &&
402                     (*inbuf)[0] != '@') {
403                         (*outbuf)[0] = (*inbuf)[0];
404                         (*inbytesleft)  -= 2;
405                         (*outbytesleft) -= 1;
406                         (*inbuf)  += 2;
407                         (*outbuf) += 1;
408                         continue;
409                 }
410                 if (*outbytesleft < 5) {
411                         errno = E2BIG;
412                         return -1;
413                 }
414                 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
415                 memcpy(*outbuf, buf, 5);
416                 (*inbytesleft)  -= 2;
417                 (*outbytesleft) -= 5;
418                 (*inbuf)  += 2;
419                 (*outbuf) += 5;
420         }
421
422         if (*inbytesleft == 1) {
423                 errno = EINVAL;
424                 return -1;
425         }
426
427         if (*inbytesleft > 1) {
428                 errno = E2BIG;
429                 return -1;
430         }
431         
432         return 0;
433 }
434
435
436 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
437                          char **outbuf, size_t *outbytesleft)
438 {
439         int n;
440
441         n = MIN(*inbytesleft, *outbytesleft);
442
443         memmove(*outbuf, *inbuf, n);
444
445         (*inbytesleft) -= n;
446         (*outbytesleft) -= n;
447         (*inbuf) += n;
448         (*outbuf) += n;
449
450         if (*inbytesleft > 0) {
451                 errno = E2BIG;
452                 return -1;
453         }
454
455         return 0;
456 }
457
458 static size_t utf8_pull(void *cd, char **inbuf, size_t *inbytesleft,
459                          char **outbuf, size_t *outbytesleft)
460 {
461         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
462                 unsigned char *c = (unsigned char *)*inbuf;
463                 unsigned char *uc = (unsigned char *)*outbuf;
464                 int len = 1;
465
466                 if ((c[0] & 0x80) == 0) {
467                         uc[0] = c[0];
468                         uc[1] = 0;
469                 } else if ((c[0] & 0xf0) == 0xe0) {
470                         if (*inbytesleft < 3) {
471                                 DEBUG(0,("short utf8 char\n"));
472                                 goto badseq;
473                         }
474                         uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
475                         uc[0] = (c[1]<<6) | (c[2]&0x3f);
476                         len = 3;
477                 } else if ((c[0] & 0xe0) == 0xc0) {
478                         if (*inbytesleft < 2) {
479                                 DEBUG(0,("short utf8 char\n"));
480                                 goto badseq;
481                         }
482                         uc[1] = (c[0]>>2) & 0x7;
483                         uc[0] = (c[0]<<6) | (c[1]&0x3f);
484                         len = 2;
485                 }
486
487                 (*inbuf)  += len;
488                 (*inbytesleft)  -= len;
489                 (*outbytesleft) -= 2;
490                 (*outbuf) += 2;
491         }
492
493         if (*inbytesleft > 0) {
494                 errno = E2BIG;
495                 return -1;
496         }
497         
498         return 0;
499
500 badseq:
501         errno = EINVAL;
502         return -1;
503 }
504
505 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
506                          char **outbuf, size_t *outbytesleft)
507 {
508         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
509                 unsigned char *c = (unsigned char *)*outbuf;
510                 unsigned char *uc = (unsigned char *)*inbuf;
511                 int len=1;
512
513                 if (uc[1] & 0xf8) {
514                         if (*outbytesleft < 3) {
515                                 DEBUG(0,("short utf8 write\n"));
516                                 goto toobig;
517                         }
518                         c[0] = 0xe0 | (uc[1]>>4);
519                         c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
520                         c[2] = 0x80 | (uc[0]&0x3f);
521                         len = 3;
522                 } else if (uc[1] | (uc[0] & 0x80)) {
523                         if (*outbytesleft < 2) {
524                                 DEBUG(0,("short utf8 write\n"));
525                                 goto toobig;
526                         }
527                         c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
528                         c[1] = 0x80 | (uc[0]&0x3f);
529                         len = 2;
530                 } else {
531                         c[0] = uc[0];
532                 }
533
534
535                 (*inbytesleft)  -= 2;
536                 (*outbytesleft) -= len;
537                 (*inbuf)  += 2;
538                 (*outbuf) += len;
539         }
540
541         if (*inbytesleft == 1) {
542                 errno = EINVAL;
543                 return -1;
544         }
545
546         if (*inbytesleft > 1) {
547                 errno = E2BIG;
548                 return -1;
549         }
550         
551         return 0;
552
553 toobig:
554         errno = E2BIG;
555         return -1;
556 }
557