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