Arg. I hate the "if (xxx) return foo" all on one line style of code.
[samba.git] / lib / util / charset / 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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "../lib/util/dlinklist.h"
23 #include "system/iconv.h"
24 #include "system/filesys.h"
25
26 #ifdef strcasecmp
27 #undef strcasecmp
28 #endif
29
30 #ifdef static_decl_charset
31 static_decl_charset;
32 #endif
33
34 /**
35  * @file
36  *
37  * @brief Samba wrapper/stub for iconv character set conversion.
38  *
39  * iconv is the XPG2 interface for converting between character
40  * encodings.  This file provides a Samba wrapper around it, and also
41  * a simple reimplementation that is used if the system does not
42  * implement iconv.
43  *
44  * Samba only works with encodings that are supersets of ASCII: ascii
45  * characters like whitespace can be tested for directly, multibyte
46  * sequences start with a byte with the high bit set, and strings are
47  * terminated by a nul byte.
48  *
49  * Note that the only function provided by iconv is conversion between
50  * characters.  It doesn't directly support operations like
51  * uppercasing or comparison.  We have to convert to UTF-16LE and
52  * compare there.
53  *
54  * @sa Samba Developers Guide
55  **/
56
57 static size_t ascii_pull  (void *,const char **, size_t *, char **, size_t *);
58 static size_t ascii_push  (void *,const char **, size_t *, char **, size_t *);
59 static size_t latin1_push(void *,const char **, size_t *, char **, size_t *);
60 static size_t utf8_pull   (void *,const char **, size_t *, char **, size_t *);
61 static size_t utf8_push   (void *,const char **, size_t *, char **, size_t *);
62 static size_t utf16_munged_pull(void *,const char **, size_t *, char **, size_t *);
63 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
64 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
65 static size_t iconv_copy  (void *,const char **, size_t *, char **, size_t *);
66 static size_t iconv_swab  (void *,const char **, size_t *, char **, size_t *);
67
68 static const struct charset_functions builtin_functions[] = {
69         /* windows is closest to UTF-16 */
70         {"UCS-2LE",  iconv_copy, iconv_copy},
71         {"UTF-16LE",  iconv_copy, iconv_copy},
72         {"UCS-2BE",  iconv_swab, iconv_swab},
73         {"UTF-16BE",  iconv_swab, iconv_swab},
74
75         /* we include the UTF-8 alias to cope with differing locale settings */
76         {"UTF8",   utf8_pull,  utf8_push},
77         {"UTF-8",   utf8_pull,  utf8_push},
78
79         /* this handles the munging needed for String2Key */
80         {"UTF16_MUNGED",   utf16_munged_pull,  iconv_copy},
81
82         {"ASCII", ascii_pull, ascii_push},
83         {"646", ascii_pull, ascii_push},
84         {"ISO-8859-1", ascii_pull, latin1_push},
85         {"UCS2-HEX", ucs2hex_pull, ucs2hex_push}
86 };
87
88 static struct charset_functions *charsets = NULL;
89
90 static struct charset_functions *find_charset_functions(const char *name)
91 {
92         struct charset_functions *c;
93
94         /* Check whether we already have this charset... */
95         for (c = charsets; c != NULL; c = c->next) {
96                 if(strcasecmp(c->name, name) == 0) { 
97                         return c;
98                 }
99                 c = c->next;
100         }
101
102         return NULL;
103 }
104
105 bool smb_register_charset(const struct charset_functions *funcs_in)
106 {
107         struct charset_functions *funcs;
108
109         DEBUG(5, ("Attempting to register new charset %s\n", funcs_in->name));
110         /* Check whether we already have this charset... */
111         if (find_charset_functions(funcs_in->name)) {
112                 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs_in->name));
113                 return false;
114         }
115
116         funcs = talloc(NULL, struct charset_functions);
117         if (!funcs) {
118                 DEBUG(0, ("Out of memory duplicating charset %s\n", funcs_in->name));
119                 return false;
120         }
121         *funcs = *funcs_in;
122
123         funcs->next = funcs->prev = NULL;
124         DEBUG(5, ("Registered charset %s\n", funcs->name));
125         DLIST_ADD(charsets, funcs);
126         return true;
127 }
128
129 static void lazy_initialize_iconv(void)
130 {
131 #ifdef static_init_charset
132         static bool initialized = false;
133
134         if (!initialized) {
135                 static_init_charset;
136                 initialized = true;
137         }
138 #endif
139 }
140
141 #ifdef HAVE_NATIVE_ICONV
142 /* if there was an error then reset the internal state,
143    this ensures that we don't have a shift state remaining for
144    character sets like SJIS */
145 static size_t sys_iconv(void *cd, 
146                         const char **inbuf, size_t *inbytesleft,
147                         char **outbuf, size_t *outbytesleft)
148 {
149         size_t ret = iconv((iconv_t)cd, 
150                            discard_const_p(char *, inbuf), inbytesleft, 
151                            outbuf, outbytesleft);
152         if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
153         return ret;
154 }
155 #endif
156
157 /**
158  * This is a simple portable iconv() implementaion.
159  *
160  * It only knows about a very small number of character sets - just
161  * enough that Samba works on systems that don't have iconv.
162  **/
163 _PUBLIC_ size_t smb_iconv(smb_iconv_t cd, 
164                  const char **inbuf, size_t *inbytesleft,
165                  char **outbuf, size_t *outbytesleft)
166 {
167         /* in many cases we can go direct */
168         if (cd->direct) {
169                 return cd->direct(cd->cd_direct, 
170                                   inbuf, inbytesleft, outbuf, outbytesleft);
171         }
172
173         /* otherwise we have to do it chunks at a time */
174         {
175 #ifndef SMB_ICONV_BUFSIZE
176 #define SMB_ICONV_BUFSIZE 2048
177 #endif
178                 size_t bufsize;
179                 char *cvtbuf = talloc_array(cd, char, SMB_ICONV_BUFSIZE);
180
181                 if (!cvtbuf) {
182                         return (size_t)-1;
183                 }
184
185                 while (*inbytesleft > 0) {
186                         char *bufp1 = cvtbuf;
187                         const char *bufp2 = cvtbuf;
188
189                         bufsize = SMB_ICONV_BUFSIZE;
190
191                         if (cd->pull(cd->cd_pull,
192                                      inbuf, inbytesleft, &bufp1, &bufsize) == -1
193                             && errno != E2BIG) {
194                                 talloc_free(cvtbuf);
195                                 return -1;
196                         }
197
198                         bufsize = SMB_ICONV_BUFSIZE - bufsize;
199
200                         if (cd->push(cd->cd_push,
201                                      &bufp2, &bufsize,
202                                      outbuf, outbytesleft) == -1) {
203                                 talloc_free(cvtbuf);
204                                 return -1;
205                         }
206                 }
207                 talloc_free(cvtbuf);
208         }
209
210         return 0;
211 }
212
213 static bool is_utf16(const char *name)
214 {
215         return strcasecmp(name, "UCS-2LE") == 0 ||
216                 strcasecmp(name, "UTF-16LE") == 0;
217 }
218
219 static int smb_iconv_t_destructor(smb_iconv_t hwd)
220 {
221 #ifdef HAVE_NATIVE_ICONV
222         if (hwd->cd_pull != NULL && hwd->cd_pull != (iconv_t)-1)
223                 iconv_close(hwd->cd_pull);
224         if (hwd->cd_push != NULL && hwd->cd_push != (iconv_t)-1)
225                 iconv_close(hwd->cd_push);
226         if (hwd->cd_direct != NULL && hwd->cd_direct != (iconv_t)-1)
227                 iconv_close(hwd->cd_direct);
228 #endif
229
230         return 0;
231 }
232
233 _PUBLIC_ smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode, 
234                               const char *fromcode, bool native_iconv)
235 {
236         smb_iconv_t ret;
237         const struct charset_functions *from=NULL, *to=NULL;
238         int i;
239
240         lazy_initialize_iconv();
241
242         ret = (smb_iconv_t)talloc_named(mem_ctx,
243                                         sizeof(*ret), 
244                                         "iconv(%s,%s)", tocode, fromcode);
245         if (!ret) {
246                 errno = ENOMEM;
247                 return (smb_iconv_t)-1;
248         }
249         memset(ret, 0, sizeof(*ret));
250         talloc_set_destructor(ret, smb_iconv_t_destructor);
251
252         /* check for the simplest null conversion */
253         if (strcmp(fromcode, tocode) == 0) {
254                 ret->direct = iconv_copy;
255                 return ret;
256         }
257
258         for (i=0;i<ARRAY_SIZE(builtin_functions);i++) {
259                 if (strcasecmp(fromcode, builtin_functions[i].name) == 0) {
260                         from = &builtin_functions[i];
261                 }
262                 if (strcasecmp(tocode, builtin_functions[i].name) == 0) {
263                         to = &builtin_functions[i];
264                 }
265         }
266
267         if (from == NULL) {
268                 for (from=charsets; from; from=from->next) {
269                         if (strcasecmp(from->name, fromcode) == 0) break;
270                 }
271         }
272
273         if (to == NULL) {
274                 for (to=charsets; to; to=to->next) {
275                         if (strcasecmp(to->name, tocode) == 0) break;
276                 }
277         }
278
279 #ifdef HAVE_NATIVE_ICONV
280         if ((!from || !to) && !native_iconv) {
281                 goto failed;
282         }
283         if (!from) {
284                 ret->pull = sys_iconv;
285                 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
286                 if (ret->cd_pull == (iconv_t)-1)
287                         ret->cd_pull = iconv_open("UCS-2LE", fromcode);
288                 if (ret->cd_pull == (iconv_t)-1) goto failed;
289         }
290
291         if (!to) {
292                 ret->push = sys_iconv;
293                 ret->cd_push = iconv_open(tocode, "UTF-16LE");
294                 if (ret->cd_push == (iconv_t)-1)
295                         ret->cd_push = iconv_open(tocode, "UCS-2LE");
296                 if (ret->cd_push == (iconv_t)-1) goto failed;
297         }
298 #else
299         if (!from || !to) {
300                 goto failed;
301         }
302 #endif
303
304         /* check for conversion to/from ucs2 */
305         if (is_utf16(fromcode) && to) {
306                 ret->direct = to->push;
307                 return ret;
308         }
309         if (is_utf16(tocode) && from) {
310                 ret->direct = from->pull;
311                 return ret;
312         }
313
314 #ifdef HAVE_NATIVE_ICONV
315         if (is_utf16(fromcode)) {
316                 ret->direct = sys_iconv;
317                 ret->cd_direct = ret->cd_push;
318                 ret->cd_push = NULL;
319                 return ret;
320         }
321         if (is_utf16(tocode)) {
322                 ret->direct = sys_iconv;
323                 ret->cd_direct = ret->cd_pull;
324                 ret->cd_pull = NULL;
325                 return ret;
326         }
327 #endif
328
329         /* the general case has to go via a buffer */
330         if (!ret->pull) ret->pull = from->pull;
331         if (!ret->push) ret->push = to->push;
332         return ret;
333
334 failed:
335         talloc_free(ret);
336         errno = EINVAL;
337         return (smb_iconv_t)-1;
338 }
339
340 /*
341   simple iconv_open() wrapper
342  */
343 _PUBLIC_ smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
344 {
345         return smb_iconv_open_ex(NULL, tocode, fromcode, true);
346 }
347
348 /*
349   simple iconv_close() wrapper
350 */
351 _PUBLIC_ int smb_iconv_close(smb_iconv_t cd)
352 {
353         talloc_free(cd);
354         return 0;
355 }
356
357
358 /**********************************************************************
359  the following functions implement the builtin character sets in Samba
360  and also the "test" character sets that are designed to test
361  multi-byte character set support for english users
362 ***********************************************************************/
363 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
364                          char **outbuf, size_t *outbytesleft)
365 {
366         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
367                 (*outbuf)[0] = (*inbuf)[0];
368                 (*outbuf)[1] = 0;
369                 (*inbytesleft)  -= 1;
370                 (*outbytesleft) -= 2;
371                 (*inbuf)  += 1;
372                 (*outbuf) += 2;
373         }
374
375         if (*inbytesleft > 0) {
376                 errno = E2BIG;
377                 return -1;
378         }
379         
380         return 0;
381 }
382
383 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
384                          char **outbuf, size_t *outbytesleft)
385 {
386         int ir_count=0;
387
388         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
389                 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
390                 if ((*inbuf)[1]) ir_count++;
391                 (*inbytesleft)  -= 2;
392                 (*outbytesleft) -= 1;
393                 (*inbuf)  += 2;
394                 (*outbuf) += 1;
395         }
396
397         if (*inbytesleft == 1) {
398                 errno = EINVAL;
399                 return -1;
400         }
401
402         if (*inbytesleft > 1) {
403                 errno = E2BIG;
404                 return -1;
405         }
406         
407         return ir_count;
408 }
409
410 static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
411                          char **outbuf, size_t *outbytesleft)
412 {
413         int ir_count=0;
414
415         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
416                 (*outbuf)[0] = (*inbuf)[0];
417                 if ((*inbuf)[1]) ir_count++;
418                 (*inbytesleft)  -= 2;
419                 (*outbytesleft) -= 1;
420                 (*inbuf)  += 2;
421                 (*outbuf) += 1;
422         }
423
424         if (*inbytesleft == 1) {
425                 errno = EINVAL;
426                 return -1;
427         }
428
429         if (*inbytesleft > 1) {
430                 errno = E2BIG;
431                 return -1;
432         }
433
434         return ir_count;
435 }
436
437 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
438                          char **outbuf, size_t *outbytesleft)
439 {
440         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
441                 unsigned int v;
442
443                 if ((*inbuf)[0] != '@') {
444                         /* seven bit ascii case */
445                         (*outbuf)[0] = (*inbuf)[0];
446                         (*outbuf)[1] = 0;
447                         (*inbytesleft)  -= 1;
448                         (*outbytesleft) -= 2;
449                         (*inbuf)  += 1;
450                         (*outbuf) += 2;
451                         continue;
452                 }
453                 /* it's a hex character */
454                 if (*inbytesleft < 5) {
455                         errno = EINVAL;
456                         return -1;
457                 }
458                 
459                 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
460                         errno = EILSEQ;
461                         return -1;
462                 }
463
464                 (*outbuf)[0] = v&0xff;
465                 (*outbuf)[1] = v>>8;
466                 (*inbytesleft)  -= 5;
467                 (*outbytesleft) -= 2;
468                 (*inbuf)  += 5;
469                 (*outbuf) += 2;
470         }
471
472         if (*inbytesleft > 0) {
473                 errno = E2BIG;
474                 return -1;
475         }
476         
477         return 0;
478 }
479
480 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
481                            char **outbuf, size_t *outbytesleft)
482 {
483         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
484                 char buf[6];
485
486                 if ((*inbuf)[1] == 0 && 
487                     ((*inbuf)[0] & 0x80) == 0 &&
488                     (*inbuf)[0] != '@') {
489                         (*outbuf)[0] = (*inbuf)[0];
490                         (*inbytesleft)  -= 2;
491                         (*outbytesleft) -= 1;
492                         (*inbuf)  += 2;
493                         (*outbuf) += 1;
494                         continue;
495                 }
496                 if (*outbytesleft < 5) {
497                         errno = E2BIG;
498                         return -1;
499                 }
500                 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
501                 memcpy(*outbuf, buf, 5);
502                 (*inbytesleft)  -= 2;
503                 (*outbytesleft) -= 5;
504                 (*inbuf)  += 2;
505                 (*outbuf) += 5;
506         }
507
508         if (*inbytesleft == 1) {
509                 errno = EINVAL;
510                 return -1;
511         }
512
513         if (*inbytesleft > 1) {
514                 errno = E2BIG;
515                 return -1;
516         }
517         
518         return 0;
519 }
520
521 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
522                          char **outbuf, size_t *outbytesleft)
523 {
524         int n;
525
526         n = MIN(*inbytesleft, *outbytesleft);
527
528         swab(*inbuf, *outbuf, (n&~1));
529         if (n&1) {
530                 (*outbuf)[n-1] = 0;
531         }
532
533         (*inbytesleft) -= n;
534         (*outbytesleft) -= n;
535         (*inbuf) += n;
536         (*outbuf) += n;
537
538         if (*inbytesleft > 0) {
539                 errno = E2BIG;
540                 return -1;
541         }
542
543         return 0;
544 }
545
546
547 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
548                          char **outbuf, size_t *outbytesleft)
549 {
550         int n;
551
552         n = MIN(*inbytesleft, *outbytesleft);
553
554         memmove(*outbuf, *inbuf, n);
555
556         (*inbytesleft) -= n;
557         (*outbytesleft) -= n;
558         (*inbuf) += n;
559         (*outbuf) += n;
560
561         if (*inbytesleft > 0) {
562                 errno = E2BIG;
563                 return -1;
564         }
565
566         return 0;
567 }
568
569 /*
570   this takes a UTF8 sequence and produces a UTF16 sequence
571  */
572 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
573                          char **outbuf, size_t *outbytesleft)
574 {
575         size_t in_left=*inbytesleft, out_left=*outbytesleft;
576         const uint8_t *c = (const uint8_t *)*inbuf;
577         uint8_t *uc = (uint8_t *)*outbuf;
578
579         while (in_left >= 1 && out_left >= 2) {
580                 if ((c[0] & 0x80) == 0) {
581                         uc[0] = c[0];
582                         uc[1] = 0;
583                         c  += 1;
584                         in_left  -= 1;
585                         out_left -= 2;
586                         uc += 2;
587                         continue;
588                 }
589
590                 if ((c[0] & 0xe0) == 0xc0) {
591                         if (in_left < 2 ||
592                             (c[1] & 0xc0) != 0x80) {
593                                 errno = EILSEQ;
594                                 goto error;
595                         }
596                         uc[1] = (c[0]>>2) & 0x7;
597                         uc[0] = (c[0]<<6) | (c[1]&0x3f);
598                         c  += 2;
599                         in_left  -= 2;
600                         out_left -= 2;
601                         uc += 2;
602                         continue;
603                 }
604
605                 if ((c[0] & 0xf0) == 0xe0) {
606                         if (in_left < 3 ||
607                             (c[1] & 0xc0) != 0x80 || 
608                             (c[2] & 0xc0) != 0x80) {
609                                 errno = EILSEQ;
610                                 goto error;
611                         }
612                         uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
613                         uc[0] = (c[1]<<6) | (c[2]&0x3f);
614                         c  += 3;
615                         in_left  -= 3;
616                         out_left -= 2;
617                         uc += 2;
618                         continue;
619                 }
620
621                 if ((c[0] & 0xf8) == 0xf0) {
622                         unsigned int codepoint;
623                         if (in_left < 4 ||
624                             (c[1] & 0xc0) != 0x80 || 
625                             (c[2] & 0xc0) != 0x80 ||
626                             (c[3] & 0xc0) != 0x80) {
627                                 errno = EILSEQ;
628                                 goto error;
629                         }
630                         codepoint = 
631                                 (c[3]&0x3f) | 
632                                 ((c[2]&0x3f)<<6) | 
633                                 ((c[1]&0x3f)<<12) |
634                                 ((c[0]&0x7)<<18);
635                         if (codepoint < 0x10000) {
636                                 /* accept UTF-8 characters that are not
637                                    minimally packed, but pack the result */
638                                 uc[0] = (codepoint & 0xFF);
639                                 uc[1] = (codepoint >> 8);
640                                 c += 4;
641                                 in_left -= 4;
642                                 out_left -= 2;
643                                 uc += 2;
644                                 continue;
645                         }
646
647                         codepoint -= 0x10000;
648
649                         if (out_left < 4) {
650                                 errno = E2BIG;
651                                 goto error;
652                         }
653
654                         uc[0] = (codepoint>>10) & 0xFF;
655                         uc[1] = (codepoint>>18) | 0xd8;
656                         uc[2] = codepoint & 0xFF;
657                         uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
658                         c  += 4;
659                         in_left  -= 4;
660                         out_left -= 4;
661                         uc += 4;
662                         continue;
663                 }
664
665                 /* we don't handle 5 byte sequences */
666                 errno = EINVAL;
667                 goto error;
668         }
669
670         if (in_left > 0) {
671                 errno = E2BIG;
672                 goto error;
673         }
674
675         *inbytesleft = in_left;
676         *outbytesleft = out_left;
677         *inbuf = (const char *)c;
678         *outbuf = (char *)uc;
679         return 0;
680
681 error:
682         *inbytesleft = in_left;
683         *outbytesleft = out_left;
684         *inbuf = (const char *)c;
685         *outbuf = (char *)uc;
686         return -1;
687 }
688
689
690 /*
691   this takes a UTF16 sequence and produces a UTF8 sequence
692  */
693 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
694                         char **outbuf, size_t *outbytesleft)
695 {
696         size_t in_left=*inbytesleft, out_left=*outbytesleft;
697         uint8_t *c = (uint8_t *)*outbuf;
698         const uint8_t *uc = (const uint8_t *)*inbuf;
699
700         while (in_left >= 2 && out_left >= 1) {
701                 unsigned int codepoint;
702
703                 if (uc[1] == 0 && !(uc[0] & 0x80)) {
704                         /* simplest case */
705                         c[0] = uc[0];
706                         in_left  -= 2;
707                         out_left -= 1;
708                         uc += 2;
709                         c  += 1;
710                         continue;
711                 }
712
713                 if ((uc[1]&0xf8) == 0) {
714                         /* next simplest case */
715                         if (out_left < 2) {
716                                 errno = E2BIG;
717                                 goto error;
718                         }
719                         c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
720                         c[1] = 0x80 | (uc[0] & 0x3f);
721                         in_left  -= 2;
722                         out_left -= 2;
723                         uc += 2;
724                         c  += 2;
725                         continue;
726                 }
727
728                 if ((uc[1] & 0xfc) == 0xdc) {
729                         /* its the second part of a 4 byte sequence. Illegal */
730                         if (in_left < 4) {
731                                 errno = EINVAL;
732                         } else {
733                                 errno = EILSEQ;
734                         }
735                         goto error;
736                 }
737
738                 if ((uc[1] & 0xfc) != 0xd8) {
739                         codepoint = uc[0] | (uc[1]<<8);
740                         if (out_left < 3) {
741                                 errno = E2BIG;
742                                 goto error;
743                         }
744                         c[0] = 0xe0 | (codepoint >> 12);
745                         c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
746                         c[2] = 0x80 | (codepoint & 0x3f);
747                         
748                         in_left  -= 2;
749                         out_left -= 3;
750                         uc  += 2;
751                         c   += 3;
752                         continue;
753                 }
754
755                 /* its the first part of a 4 byte sequence */
756                 if (in_left < 4) {
757                         errno = EINVAL;
758                         goto error;
759                 }
760                 if ((uc[3] & 0xfc) != 0xdc) {
761                         errno = EILSEQ;
762                         goto error;
763                 }
764                 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) | 
765                                        (uc[0]<<10) | ((uc[1] & 0x3)<<18));
766                 
767                 if (out_left < 4) {
768                         errno = E2BIG;
769                         goto error;
770                 }
771                 c[0] = 0xf0 | (codepoint >> 18);
772                 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
773                 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
774                 c[3] = 0x80 | (codepoint & 0x3f);
775                 
776                 in_left  -= 4;
777                 out_left -= 4;
778                 uc       += 4;
779                 c        += 4;
780         }
781
782         if (in_left == 1) {
783                 errno = EINVAL;
784                 goto error;
785         }
786
787         if (in_left > 1) {
788                 errno = E2BIG;
789                 goto error;
790         }
791
792         *inbytesleft = in_left;
793         *outbytesleft = out_left;
794         *inbuf  = (const char *)uc;
795         *outbuf = (char *)c;
796         
797         return 0;
798
799 error:
800         *inbytesleft = in_left;
801         *outbytesleft = out_left;
802         *inbuf  = (const char *)uc;
803         *outbuf = (char *)c;
804         return -1;
805 }
806
807
808 /*
809   this takes a UTF16 munged sequence, modifies it according to the
810   string2key rules, and produces a UTF16 sequence
811
812 The rules are:
813
814     1) any 0x0000 characters are mapped to 0x0001
815
816     2) convert any instance of 0xD800 - 0xDBFF (high surrogate)
817        without an immediately following 0xDC00 - 0x0xDFFF (low surrogate) to
818        U+FFFD (OBJECT REPLACEMENT CHARACTER).
819
820     3) the same for any low surrogate that was not preceded by a high surrogate.
821
822  */
823 static size_t utf16_munged_pull(void *cd, const char **inbuf, size_t *inbytesleft,
824                                char **outbuf, size_t *outbytesleft)
825 {
826         size_t in_left=*inbytesleft, out_left=*outbytesleft;
827         uint8_t *c = (uint8_t *)*outbuf;
828         const uint8_t *uc = (const uint8_t *)*inbuf;
829
830         while (in_left >= 2 && out_left >= 2) {
831                 unsigned int codepoint = uc[0] | (uc[1]<<8);
832
833                 if (codepoint == 0) {
834                         codepoint = 1;
835                 }
836
837                 if ((codepoint & 0xfc00) == 0xd800) {
838                         /* a high surrogate */
839                         unsigned int codepoint2;
840                         if (in_left < 4) {
841                                 codepoint = 0xfffd;
842                                 goto codepoint16;                               
843                         }
844                         codepoint2 = uc[2] | (uc[3]<<8);
845                         if ((codepoint2 & 0xfc00) != 0xdc00) {
846                                 /* high surrogate not followed by low
847                                    surrogate: convert to 0xfffd */
848                                 codepoint = 0xfffd;
849                                 goto codepoint16;
850                         }
851                         if (out_left < 4) {
852                                 errno = E2BIG;
853                                 goto error;
854                         }
855                         memcpy(c, uc, 4);
856                         in_left  -= 4;
857                         out_left -= 4;
858                         uc       += 4;
859                         c        += 4;
860                         continue;
861                 }
862
863                 if ((codepoint & 0xfc00) == 0xdc00) {
864                         /* low surrogate not preceded by high
865                            surrogate: convert to 0xfffd */
866                         codepoint = 0xfffd;
867                 }
868
869         codepoint16:
870                 c[0] = codepoint & 0xFF;
871                 c[1] = (codepoint>>8) & 0xFF;
872                 
873                 in_left  -= 2;
874                 out_left -= 2;
875                 uc  += 2;
876                 c   += 2;
877                 continue;               
878         }
879
880         if (in_left == 1) {
881                 errno = EINVAL;
882                 goto error;
883         }
884
885         if (in_left > 1) {
886                 errno = E2BIG;
887                 goto error;
888         }
889
890         *inbytesleft = in_left;
891         *outbytesleft = out_left;
892         *inbuf  = (const char *)uc;
893         *outbuf = (char *)c;
894         
895         return 0;
896
897 error:
898         *inbytesleft = in_left;
899         *outbytesleft = out_left;
900         *inbuf  = (const char *)uc;
901         *outbuf = (char *)c;
902         return -1;
903 }
904
905
906