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