1 /*-
2 * Copyright (c) 1999,2000
3 * Konstantin Chuguev. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Konstantin Chuguev
16 * and its contributors.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * iconv (Charset Conversion Library) v2.0
31 */
32
33 #include <errno.h> /* errno */
34 #include <stdlib.h> /* free */
35 #include <string.h> /* strcmp */
36
37 #define ICONV_INTERNAL
38 #include <iconv.h>
39
40 iconv_t
iconv_open(const char * to,const char * from)41 iconv_open(const char *to, const char *from)
42 {
43 iconv_converter *ic;
44
45 if(!to || !from || iconv_get_aliases((char **)&from, (char **)&to))
46 return (iconv_t)(-1);
47 ic = strcmp(from, to) ? iconv_unicode_conv_init(to, from)
48 : iconv_null_conv_init(to, from);
49 free((void *)to);
50 free((void *)from);
51 return ic ? (iconv_t)(ic) : (iconv_t)(-1);
52 }
53
54 size_t
iconv(iconv_t cd,const char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)55 iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
56 char **outbuf, size_t *outbytesleft)
57 {
58 if ((void *)cd == NULL) {
59 errno = EBADF;
60 return (size_t)(-1);
61 }
62 if (outbytesleft == NULL || *outbytesleft == 0 ||
63 outbuf == NULL || *outbuf == 0) {
64 errno = E2BIG;
65 return (size_t)(-1);
66 }
67 return ((iconv_converter *)cd)->convert((iconv_converter *)cd + 1,
68 (const unsigned char**)inbuf,
69 inbytesleft,
70 (unsigned char**)outbuf,
71 outbytesleft);
72 }
73
74 int
iconv_close(iconv_t cd)75 iconv_close(iconv_t cd)
76 {
77 int res;
78
79 if ((void *)cd == NULL) {
80 errno = EBADF;
81 return -1;
82 }
83 res = ((iconv_converter *)cd)->close((iconv_converter *)cd + 1);
84 free((void *)cd);
85 return res ? -1 : 0;
86 }
87