r21076: Two pieces of infrastructure from Samba4: An API-compatible messaging wrapper
[sfrench/samba-autobuild/.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_decl_charset;
55
56 static size_t ascii_pull(void *,const char **, size_t *, char **, size_t *);
57 static size_t ascii_push(void *,const char **, size_t *, char **, size_t *);
58 static size_t latin1_push(void *,const char **, size_t *, char **, size_t *);
59 static size_t  utf8_pull(void *,const char **, size_t *, char **, size_t *);
60 static size_t  utf8_push(void *,const char **, size_t *, char **, size_t *);
61 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
62 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
63 static size_t iconv_copy(void *,const char **, size_t *, char **, size_t *);
64 static size_t iconv_swab  (void *,const char **, size_t *, char **, size_t *);
65
66 static struct charset_functions builtin_functions[] = {
67         /* windows is really neither UCS-2 not UTF-16 */
68         {"UCS-2LE",  iconv_copy, iconv_copy},
69         {"UTF-16LE",  iconv_copy, iconv_copy},
70         {"UCS-2BE",  iconv_swab, iconv_swab},
71         {"UTF-16BE",  iconv_swab, iconv_swab},
72
73         /* we include the UTF-8 alias to cope with differing locale settings */
74         {"UTF8",   utf8_pull,  utf8_push},
75         {"UTF-8",   utf8_pull,  utf8_push},
76         {"ASCII", ascii_pull, ascii_push},
77         {"646", ascii_pull, ascii_push},
78         {"ISO-8859-1", ascii_pull, latin1_push},
79         {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
80         {NULL, NULL, NULL}
81 };
82
83 static struct charset_functions *charsets = NULL;
84
85 static struct charset_functions *find_charset_functions(const char *name) 
86 {
87         struct charset_functions *c = charsets;
88
89         while(c) {
90                 if (strcasecmp(name, c->name) == 0) {
91                         return c;
92                 }
93                 c = c->next;
94         }
95
96         return NULL;
97 }
98
99 NTSTATUS smb_register_charset(struct charset_functions *funcs) 
100 {
101         if (!funcs) {
102                 return NT_STATUS_INVALID_PARAMETER;
103         }
104
105         DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
106         /* Check whether we already have this charset... */
107         if (find_charset_functions(funcs->name)) {
108                 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs->name));
109                 return NT_STATUS_OBJECT_NAME_COLLISION;
110         }
111
112         funcs->next = funcs->prev = NULL;
113         DEBUG(5, ("Registered charset %s\n", funcs->name));
114         DLIST_ADD(charsets, funcs);
115         return NT_STATUS_OK;
116 }
117
118 static void lazy_initialize_iconv(void)
119 {
120         static BOOL initialized;
121         int i;
122
123         if (!initialized) {
124                 initialized = True;
125                 for(i = 0; builtin_functions[i].name; i++) 
126                         smb_register_charset(&builtin_functions[i]);
127                 static_init_charset;
128         }
129 }
130
131 #ifdef HAVE_NATIVE_ICONV
132 /* if there was an error then reset the internal state,
133    this ensures that we don't have a shift state remaining for
134    character sets like SJIS */
135 static size_t sys_iconv(void *cd, 
136                         const char **inbuf, size_t *inbytesleft,
137                         char **outbuf, size_t *outbytesleft)
138 {
139         size_t ret = iconv((iconv_t)cd, 
140                            (char **)inbuf, inbytesleft, 
141                            outbuf, outbytesleft);
142         if (ret == (size_t)-1) {
143                 int saved_errno = errno;
144                 iconv(cd, NULL, NULL, NULL, NULL);
145                 errno = saved_errno;
146         }
147         return ret;
148 }
149 #endif
150
151 /**
152  * This is a simple portable iconv() implementaion.
153  *
154  * It only knows about a very small number of character sets - just
155  * enough that Samba works on systems that don't have iconv.
156  **/
157 size_t smb_iconv(smb_iconv_t cd, 
158                  const char **inbuf, size_t *inbytesleft,
159                  char **outbuf, size_t *outbytesleft)
160 {
161         char cvtbuf[2048];
162         char *bufp = cvtbuf;
163         size_t bufsize;
164
165         /* in many cases we can go direct */
166         if (cd->direct) {
167                 return cd->direct(cd->cd_direct, 
168                                   inbuf, inbytesleft, outbuf, outbytesleft);
169         }
170
171
172         /* otherwise we have to do it chunks at a time */
173         while (*inbytesleft > 0) {
174                 bufp = cvtbuf;
175                 bufsize = sizeof(cvtbuf);
176                 
177                 if (cd->pull(cd->cd_pull, 
178                              inbuf, inbytesleft, &bufp, &bufsize) == -1
179                     && errno != E2BIG) return -1;
180
181                 bufp = cvtbuf;
182                 bufsize = sizeof(cvtbuf) - bufsize;
183
184                 if (cd->push(cd->cd_push, 
185                              (const char **)&bufp, &bufsize, 
186                              outbuf, outbytesleft) == -1) return -1;
187         }
188
189         return 0;
190 }
191
192
193 static BOOL is_utf16(const char *name)
194 {
195         return strcasecmp(name, "UCS-2LE") == 0 ||
196                 strcasecmp(name, "UTF-16LE") == 0;
197 }
198
199 /*
200   simple iconv_open() wrapper
201  */
202 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
203 {
204         smb_iconv_t ret;
205         struct charset_functions *from, *to;
206         
207         lazy_initialize_iconv();
208         from = charsets;
209         to = charsets;
210
211         ret = SMB_MALLOC_P(struct _smb_iconv_t);
212         if (!ret) {
213                 errno = ENOMEM;
214                 return (smb_iconv_t)-1;
215         }
216         memset(ret, 0, sizeof(struct _smb_iconv_t));
217
218         ret->from_name = SMB_STRDUP(fromcode);
219         ret->to_name = SMB_STRDUP(tocode);
220
221         /* check for the simplest null conversion */
222         if (strcasecmp(fromcode, tocode) == 0) {
223                 ret->direct = iconv_copy;
224                 return ret;
225         }
226
227         /* check if we have a builtin function for this conversion */
228         from = find_charset_functions(fromcode);
229         if(from)ret->pull = from->pull;
230         
231         to = find_charset_functions(tocode);
232         if(to)ret->push = to->push;
233
234         /* check if we can use iconv for this conversion */
235 #ifdef HAVE_NATIVE_ICONV
236         if (!ret->pull) {
237                 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
238                 if (ret->cd_pull == (iconv_t)-1)
239                         ret->cd_pull = iconv_open("UCS-2LE", fromcode);
240                 if (ret->cd_pull != (iconv_t)-1)
241                         ret->pull = sys_iconv;
242         }
243
244         if (!ret->push) {
245                 ret->cd_push = iconv_open(tocode, "UTF-16LE");
246                 if (ret->cd_push == (iconv_t)-1)
247                         ret->cd_push = iconv_open(tocode, "UCS-2LE");
248                 if (ret->cd_push != (iconv_t)-1)
249                         ret->push = sys_iconv;
250         }
251 #endif
252         
253         /* check if there is a module available that can do this conversion */
254         if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
255                 if(!(from = find_charset_functions(fromcode)))
256                         DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
257                 else 
258                         ret->pull = from->pull;
259         }
260
261         if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
262                 if(!(to = find_charset_functions(tocode)))
263                         DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
264                 else 
265                         ret->push = to->push;
266         }
267
268         if (!ret->push || !ret->pull) {
269                 SAFE_FREE(ret->from_name);
270                 SAFE_FREE(ret->to_name);
271                 SAFE_FREE(ret);
272                 errno = EINVAL;
273                 return (smb_iconv_t)-1;
274         }
275
276         /* check for conversion to/from ucs2 */
277         if (is_utf16(fromcode) && to) {
278                 ret->direct = to->push;
279                 ret->push = ret->pull = NULL;
280                 return ret;
281         }
282
283         if (is_utf16(tocode) && from) {
284                 ret->direct = from->pull;
285                 ret->push = ret->pull = NULL;
286                 return ret;
287         }
288
289         /* Check if we can do the conversion direct */
290 #ifdef HAVE_NATIVE_ICONV
291         if (is_utf16(fromcode)) {
292                 ret->direct = sys_iconv;
293                 ret->cd_direct = ret->cd_push;
294                 ret->cd_push = NULL;
295                 return ret;
296         }
297         if (is_utf16(tocode)) {
298                 ret->direct = sys_iconv;
299                 ret->cd_direct = ret->cd_pull;
300                 ret->cd_pull = NULL;
301                 return ret;
302         }
303 #endif
304
305         return ret;
306 }
307
308 /*
309   simple iconv_close() wrapper
310 */
311 int smb_iconv_close (smb_iconv_t cd)
312 {
313 #ifdef HAVE_NATIVE_ICONV
314         if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
315         if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
316         if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
317 #endif
318
319         SAFE_FREE(cd->from_name);
320         SAFE_FREE(cd->to_name);
321
322         memset(cd, 0, sizeof(*cd));
323         SAFE_FREE(cd);
324         return 0;
325 }
326
327
328 /**********************************************************************
329  the following functions implement the builtin character sets in Samba
330  and also the "test" character sets that are designed to test
331  multi-byte character set support for english users
332 ***********************************************************************/
333
334 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
335                          char **outbuf, size_t *outbytesleft)
336 {
337         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
338                 (*outbuf)[0] = (*inbuf)[0];
339                 (*outbuf)[1] = 0;
340                 (*inbytesleft)  -= 1;
341                 (*outbytesleft) -= 2;
342                 (*inbuf)  += 1;
343                 (*outbuf) += 2;
344         }
345
346         if (*inbytesleft > 0) {
347                 errno = E2BIG;
348                 return -1;
349         }
350         
351         return 0;
352 }
353
354 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
355                          char **outbuf, size_t *outbytesleft)
356 {
357         int ir_count=0;
358
359         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
360                 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
361                 if ((*inbuf)[1]) ir_count++;
362                 (*inbytesleft)  -= 2;
363                 (*outbytesleft) -= 1;
364                 (*inbuf)  += 2;
365                 (*outbuf) += 1;
366         }
367
368         if (*inbytesleft == 1) {
369                 errno = EINVAL;
370                 return -1;
371         }
372
373         if (*inbytesleft > 1) {
374                 errno = E2BIG;
375                 return -1;
376         }
377         
378         return ir_count;
379 }
380
381 static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
382                          char **outbuf, size_t *outbytesleft)
383 {
384         int ir_count=0;
385
386         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
387                 (*outbuf)[0] = (*inbuf)[0];
388                 if ((*inbuf)[1]) ir_count++;
389                 (*inbytesleft)  -= 2;
390                 (*outbytesleft) -= 1;
391                 (*inbuf)  += 2;
392                 (*outbuf) += 1;
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 ir_count;
406 }
407
408 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
409                          char **outbuf, size_t *outbytesleft)
410 {
411         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
412                 unsigned v;
413
414                 if ((*inbuf)[0] != '@') {
415                         /* seven bit ascii case */
416                         (*outbuf)[0] = (*inbuf)[0];
417                         (*outbuf)[1] = 0;
418                         (*inbytesleft)  -= 1;
419                         (*outbytesleft) -= 2;
420                         (*inbuf)  += 1;
421                         (*outbuf) += 2;
422                         continue;
423                 }
424                 /* it's a hex character */
425                 if (*inbytesleft < 5) {
426                         errno = EINVAL;
427                         return -1;
428                 }
429                 
430                 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
431                         errno = EILSEQ;
432                         return -1;
433                 }
434
435                 (*outbuf)[0] = v&0xff;
436                 (*outbuf)[1] = v>>8;
437                 (*inbytesleft)  -= 5;
438                 (*outbytesleft) -= 2;
439                 (*inbuf)  += 5;
440                 (*outbuf) += 2;
441         }
442
443         if (*inbytesleft > 0) {
444                 errno = E2BIG;
445                 return -1;
446         }
447         
448         return 0;
449 }
450
451 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
452                            char **outbuf, size_t *outbytesleft)
453 {
454         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
455                 char buf[6];
456
457                 if ((*inbuf)[1] == 0 && 
458                     ((*inbuf)[0] & 0x80) == 0 &&
459                     (*inbuf)[0] != '@') {
460                         (*outbuf)[0] = (*inbuf)[0];
461                         (*inbytesleft)  -= 2;
462                         (*outbytesleft) -= 1;
463                         (*inbuf)  += 2;
464                         (*outbuf) += 1;
465                         continue;
466                 }
467                 if (*outbytesleft < 5) {
468                         errno = E2BIG;
469                         return -1;
470                 }
471                 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
472                 memcpy(*outbuf, buf, 5);
473                 (*inbytesleft)  -= 2;
474                 (*outbytesleft) -= 5;
475                 (*inbuf)  += 2;
476                 (*outbuf) += 5;
477         }
478
479         if (*inbytesleft == 1) {
480                 errno = EINVAL;
481                 return -1;
482         }
483
484         if (*inbytesleft > 1) {
485                 errno = E2BIG;
486                 return -1;
487         }
488         
489         return 0;
490 }
491
492 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
493                          char **outbuf, size_t *outbytesleft)
494 {
495         int n;
496
497         n = MIN(*inbytesleft, *outbytesleft);
498
499         swab(*inbuf, *outbuf, (n&~1));
500         if (n&1) {
501                 (*outbuf)[n-1] = 0;
502         }
503
504         (*inbytesleft) -= n;
505         (*outbytesleft) -= n;
506         (*inbuf) += n;
507         (*outbuf) += n;
508
509         if (*inbytesleft > 0) {
510                 errno = E2BIG;
511                 return -1;
512         }
513
514         return 0;
515 }
516
517 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
518                          char **outbuf, size_t *outbytesleft)
519 {
520         int n;
521
522         n = MIN(*inbytesleft, *outbytesleft);
523
524         memmove(*outbuf, *inbuf, n);
525
526         (*inbytesleft) -= n;
527         (*outbytesleft) -= n;
528         (*inbuf) += n;
529         (*outbuf) += n;
530
531         if (*inbytesleft > 0) {
532                 errno = E2BIG;
533                 return -1;
534         }
535
536         return 0;
537 }
538
539 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
540                          char **outbuf, size_t *outbytesleft)
541 {
542         size_t in_left=*inbytesleft, out_left=*outbytesleft;
543         const uint8 *c = (const uint8 *)*inbuf;
544         uint8 *uc = (uint8 *)*outbuf;
545
546         while (in_left >= 1 && out_left >= 2) {
547                 if ((c[0] & 0x80) == 0) {
548                         uc[0] = c[0];
549                         uc[1] = 0;
550                         c  += 1;
551                         in_left  -= 1;
552                         out_left -= 2;
553                         uc += 2;
554                         continue;
555                 }
556
557                 if ((c[0] & 0xe0) == 0xc0) {
558                         if (in_left < 2 ||
559                             (c[1] & 0xc0) != 0x80) {
560                                 errno = EILSEQ;
561                                 goto error;
562                         }
563                         uc[1] = (c[0]>>2) & 0x7;
564                         uc[0] = (c[0]<<6) | (c[1]&0x3f);
565                         c  += 2;
566                         in_left  -= 2;
567                         out_left -= 2;
568                         uc += 2;
569                         continue;
570                 }
571
572                 if ((c[0] & 0xf0) == 0xe0) {
573                         if (in_left < 3 ||
574                             (c[1] & 0xc0) != 0x80 || 
575                             (c[2] & 0xc0) != 0x80) {
576                                 errno = EILSEQ;
577                                 goto error;
578                         }
579                         uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
580                         uc[0] = (c[1]<<6) | (c[2]&0x3f);
581                         c  += 3;
582                         in_left  -= 3;
583                         out_left -= 2;
584                         uc += 2;
585                         continue;
586                 }
587
588                 if ((c[0] & 0xf8) == 0xf0) {
589                         unsigned int codepoint;
590                         if (in_left < 4 ||
591                             (c[1] & 0xc0) != 0x80 || 
592                             (c[2] & 0xc0) != 0x80 ||
593                             (c[3] & 0xc0) != 0x80) {
594                                 errno = EILSEQ;
595                                 goto error;
596                         }
597                         codepoint = 
598                                 (c[3]&0x3f) | 
599                                 ((c[2]&0x3f)<<6) | 
600                                 ((c[1]&0x3f)<<12) |
601                                 ((c[0]&0x7)<<18);
602                         if (codepoint < 0x10000) {
603                                 /* accept UTF-8 characters that are not
604                                    minimally packed, but pack the result */
605                                 uc[0] = (codepoint & 0xFF);
606                                 uc[1] = (codepoint >> 8);
607                                 c += 4;
608                                 in_left -= 4;
609                                 out_left -= 2;
610                                 uc += 2;
611                                 continue;
612                         }
613
614                         codepoint -= 0x10000;
615
616                         if (out_left < 4) {
617                                 errno = E2BIG;
618                                 goto error;
619                         }
620
621                         uc[0] = (codepoint>>10) & 0xFF;
622                         uc[1] = (codepoint>>18) | 0xd8;
623                         uc[2] = codepoint & 0xFF;
624                         uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
625                         c  += 4;
626                         in_left  -= 4;
627                         out_left -= 4;
628                         uc += 4;
629                         continue;
630                 }
631
632                 /* we don't handle 5 byte sequences */
633                 errno = EINVAL;
634                 goto error;
635         }
636
637         if (in_left > 0) {
638                 errno = E2BIG;
639                 goto error;
640         }
641
642         *inbytesleft = in_left;
643         *outbytesleft = out_left;
644         *inbuf = (char *)c;
645         *outbuf = (char *)uc;   
646         return 0;
647
648 error:
649         *inbytesleft = in_left;
650         *outbytesleft = out_left;
651         *inbuf = (char *)c;
652         *outbuf = (char *)uc;
653         return -1;
654 }
655
656 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
657                         char **outbuf, size_t *outbytesleft)
658 {
659         size_t in_left=*inbytesleft, out_left=*outbytesleft;
660         uint8 *c = (uint8 *)*outbuf;
661         const uint8 *uc = (const uint8 *)*inbuf;
662
663         while (in_left >= 2 && out_left >= 1) {
664                 unsigned int codepoint;
665
666                 if (uc[1] == 0 && !(uc[0] & 0x80)) {
667                         /* simplest case */
668                         c[0] = uc[0];
669                         in_left  -= 2;
670                         out_left -= 1;
671                         uc += 2;
672                         c  += 1;
673                         continue;
674                 }
675
676                 if ((uc[1]&0xf8) == 0) {
677                         /* next simplest case */
678                         if (out_left < 2) {
679                                 errno = E2BIG;
680                                 goto error;
681                         }
682                         c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
683                         c[1] = 0x80 | (uc[0] & 0x3f);
684                         in_left  -= 2;
685                         out_left -= 2;
686                         uc += 2;
687                         c  += 2;
688                         continue;
689                 }
690
691                 if ((uc[1] & 0xfc) == 0xdc) {
692                         /* its the second part of a 4 byte sequence. Illegal */
693                         if (in_left < 4) {
694                                 errno = EINVAL;
695                         } else {
696                                 errno = EILSEQ;
697                         }
698                         goto error;
699                 }
700
701                 if ((uc[1] & 0xfc) != 0xd8) {
702                         codepoint = uc[0] | (uc[1]<<8);
703                         if (out_left < 3) {
704                                 errno = E2BIG;
705                                 goto error;
706                         }
707                         c[0] = 0xe0 | (codepoint >> 12);
708                         c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
709                         c[2] = 0x80 | (codepoint & 0x3f);
710                         
711                         in_left  -= 2;
712                         out_left -= 3;
713                         uc  += 2;
714                         c   += 3;
715                         continue;
716                 }
717
718                 /* its the first part of a 4 byte sequence */
719                 if (in_left < 4) {
720                         errno = EINVAL;
721                         goto error;
722                 }
723                 if ((uc[3] & 0xfc) != 0xdc) {
724                         errno = EILSEQ;
725                         goto error;
726                 }
727                 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) | 
728                                        (uc[0]<<10) | ((uc[1] & 0x3)<<18));
729                 
730                 if (out_left < 4) {
731                         errno = E2BIG;
732                         goto error;
733                 }
734                 c[0] = 0xf0 | (codepoint >> 18);
735                 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
736                 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
737                 c[3] = 0x80 | (codepoint & 0x3f);
738                 
739                 in_left  -= 4;
740                 out_left -= 4;
741                 uc       += 4;
742                 c        += 4;
743         }
744
745         if (in_left == 1) {
746                 errno = EINVAL;
747                 goto error;
748         }
749
750         if (in_left > 1) {
751                 errno = E2BIG;
752                 goto error;
753         }
754
755         *inbytesleft = in_left;
756         *outbytesleft = out_left;
757         *inbuf  = (char *)uc;
758         *outbuf = (char *)c;
759         
760         return 0;
761
762 error:
763         *inbytesleft = in_left;
764         *outbytesleft = out_left;
765         *inbuf  = (char *)uc;
766         *outbuf = (char *)c;
767         return -1;
768 }
769