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