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, malloc */
35 #include <string.h>	/* bcopy */
36 
37 #define ICONV_INTERNAL
38 #include <iconv.h>
39 
40 static iconv_converter *
converter_init(iconv_conv_t conv_func,iconv_close_t close_func,size_t extra)41 converter_init(iconv_conv_t conv_func, iconv_close_t close_func, size_t extra)
42 {
43 	iconv_converter *res = malloc(sizeof(iconv_converter) + extra);
44 	if (res) {
45 		res->convert = conv_func;
46 		res->close = close_func;
47 	}
48 	return res;
49 }
50 
51 typedef struct {
52 	struct iconv_ces from;
53 	struct iconv_ces to;
54 	ucs_t  missing;
55 } unicode_converter;
56 
57 static int
unicode_close(void * data)58 unicode_close(void *data)
59 {
60 	int res;
61 	unicode_converter *uc = (unicode_converter *)data;
62 
63 	res = ICONV_CES_CLOSE(&(uc->from));
64 	res = ICONV_CES_CLOSE(&(uc->to)) || res;
65 	return res;
66 }
67 
68 static size_t
unicode_conv(void * data,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft)69 unicode_conv(void *data, const unsigned char **inbuf, size_t *inbytesleft,
70              unsigned char **outbuf, size_t *outbytesleft)
71 {
72 	size_t res = 0;
73 	unicode_converter *uc = (unicode_converter *)data;
74 
75 	if (inbuf == NULL || *inbuf == NULL) {
76 		if (ICONV_CES_CONVERT_FROM_UCS(&(uc->to), UCS_CHAR_NONE,
77 		                               outbuf, outbytesleft) <= 0) {
78 			errno = E2BIG;
79 			return (size_t)(-1);
80 		}
81 		ICONV_CES_RESET(&(uc->from));
82 		ICONV_CES_RESET(&(uc->to));
83 		return res;
84 	}
85 	if (inbytesleft == NULL || *inbytesleft == 0)
86 		return 0;
87 	while (*inbytesleft > 0 && *outbytesleft > 0) {
88 		ssize_t size;
89 		const unsigned char *ptr = *inbuf;
90 		ucs_t ch = ICONV_CES_CONVERT_TO_UCS(&(uc->from), inbuf,
91 		                                    inbytesleft);
92 		if (ch == UCS_CHAR_NONE) {
93 			/* Incomplete character in input buffer */
94 			errno = EINVAL;
95 			return (size_t)(-1);
96 		}
97 		if (ch == UCS_CHAR_INVALID) {
98 			/* Invalid character in source buffer */
99 			*inbytesleft += *inbuf - ptr;
100 			*inbuf = ptr;
101 			errno = EILSEQ;
102 			return (size_t)(-1);
103 		}
104 		size = ICONV_CES_CONVERT_FROM_UCS(&(uc->to), ch,
105 		                                  outbuf, outbytesleft);
106 		if (size < 0) {
107 			/* No equivalent in destination charset */
108 			size = ICONV_CES_CONVERT_FROM_UCS(&(uc->to),
109 			                                  uc->missing,
110 			                                  outbuf, outbytesleft);
111 			if (size)
112 				res ++;
113 		}
114 		if (!size) {
115 			/* Not enough space in output buffer */
116 			*inbytesleft += *inbuf - ptr;
117 			*inbuf = ptr;
118 			errno = E2BIG;
119 			return (size_t)(-1);
120 		}
121 	}
122 	return res;
123 }
124 
125 iconv_converter *
iconv_unicode_conv_init(const char * to,const char * from)126 iconv_unicode_conv_init(const char *to, const char *from)
127 {
128 	unicode_converter *uc;
129 	iconv_converter *ic = converter_init(unicode_conv, unicode_close,
130 	                                     sizeof(unicode_converter));
131 
132 	if (ic == NULL)
133 		return NULL;
134 	uc = (unicode_converter *)(ic + 1);
135 	if (!iconv_ces_init(&(uc->from), from)) {
136 		if(!iconv_ces_init(&(uc->to), to))
137 		{
138 			uc->missing = '_';
139 			return ic;
140 		}
141 		ICONV_CES_CLOSE(&(uc->from));
142 	}
143 	free(ic);
144 	return NULL;
145 }
146 
147 static int
null_close(void * data)148 null_close(void *data)
149 {
150 	return 0;
151 }
152 
153 static size_t
null_conv(void * data,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft)154 null_conv(void *data, const unsigned char **inbuf, size_t *inbytesleft,
155           unsigned char **outbuf, size_t *outbytesleft)
156 {
157 	if (inbuf && *inbuf && inbytesleft && *inbytesleft > 0 && outbuf
158 			&& *outbuf && outbytesleft && *outbytesleft > 0) {
159 		size_t result, len;
160 		if (*inbytesleft < *outbytesleft) {
161 			result = 0;
162 			len = *inbytesleft;
163 		} else {
164 			result = (size_t)(-1);
165 			errno = E2BIG;
166 			len = *outbytesleft;
167 		}
168 		bcopy(*inbuf, *outbuf, len);
169 		*inbuf += len;
170 		*inbytesleft -= len;
171 		*outbuf += len;
172 		*outbytesleft -= len;
173 
174 		return result;
175 	}
176 
177 	return 0;
178 }
179 
180 iconv_converter *
iconv_null_conv_init(const char * to,const char * from)181 iconv_null_conv_init(const char *to, const char *from)
182 {
183 	return converter_init(null_conv, null_close, 0);
184 }
185