summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/kiconv/kiconv_sc/kiconv_cck_common.c
blob: e0a342e26c031affd6de3f43e0e786c0d3a40d36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/sunddi.h>
#include <sys/byteorder.h>
#include <sys/errno.h>
#include <sys/u8_textprep.h>
#include <sys/kiconv.h>
#include <sys/kiconv_cck_common.h>

/*
 * Common kiconv_open method for UTF-8 -> CCK conversion.
 */
void *
kiconv_open_to_cck()
{
	kiconv_state_t st;

	st = (kiconv_state_t)kmem_alloc(sizeof (kiconv_state_data_t), KM_SLEEP);

	st->bom_processed = 0;

	return ((void *)st);
}

/*
 * Common kiconv_close method for UTF-8 -> CCK conversion.
 */
int
kiconv_close_to_cck(void *kcd)
{
	if (! kcd || kcd == (void *)-1)
		return (EBADF);

	kmem_free(kcd, sizeof (kiconv_state_data_t));

	return (0);
}

/*
 * Common routine to convert UTF-8 sequence to CCK legal character sequence.
 */
size_t
kiconv_utf8_to_cck(void *kcd, char **inbuf, size_t *inbytesleft,
    char **outbuf, size_t *outbytesleft, int *errno,
    kiconv_utf8tocck_t ptr_utf8tocck)
{
	uchar_t		*ib;
	uchar_t		*ob;
	uchar_t		*ibtail;
	uchar_t		*obtail;
	uchar_t		*oldib;
	size_t		ret_val;
	size_t		i;		/* temp variable in for loop */
	uint32_t	u8;
	int8_t		sz;

	/* Check on the kiconv code conversion descriptor. */
	if (! kcd || kcd == (void *)-1) {
		*errno = EBADF;
		return ((size_t)-1);
	}

	/* If this is a state reset request, process and return. */
	if (! inbuf || !(*inbuf)) {
		((kiconv_state_t)kcd)->bom_processed = 0;
		return (0);
	}

	ret_val = 0;
	ib = (uchar_t *)*inbuf;
	ob = (uchar_t *)*outbuf;
	ibtail = ib + *inbytesleft;
	obtail = ob + *outbytesleft;

	KICONV_CHECK_UTF8_BOM(ib, ibtail);

	while (ib < ibtail) {
		sz = u8_number_of_bytes[*ib];

		/*
		 * If it is a 7-bit ASCII character, we don't need to
		 * process further and we just copy the character over.
		 *
		 * If not, we connect the chracter bytes up to four bytes,
		 * validate the bytes, and binary search for the corresponding
		 * table. If we find it from the mapping table, we put that
		 * into the output buffer; otherwise, we put a replacement
		 * character instead as a non-identical conversion.
		 */
		if (sz == 1) {
			if (ob >= obtail) {
				KICONV_SET_ERRNO_AND_BREAK(E2BIG);
			}

			*ob++ = *ib++;
			continue;
		}

		/*
		 * Issue EILSEQ error if the first byte is a
		 * invalid UTF-8 character leading byte.
		 */
		if (sz <= 0) {
			KICONV_SET_ERRNO_AND_BREAK(EILSEQ);
		}

		/*
		 * Issue EINVAL error if input buffer has an incomplete
		 * character at the end of the buffer.
		 */
		if (ibtail - ib < sz) {
			KICONV_SET_ERRNO_AND_BREAK(EINVAL);
		}

		/*
		 * We collect UTF-8 character bytes and also check if this
		 * is a valid UTF-8 character without any bogus bytes based
		 * on the latest UTF-8 binary representation.
		 */
		oldib = ib;
		u8 = *ib++;

		if (KICONV_IS_INVALID_UTF8_SECOND_BYTE(*ib, u8))
			goto ILLEGAL_CHAR_PROCESS;
		u8 = (u8 << 8) | *ib++;

		for (i = 2; i < sz; i++) {
			if (*ib < 0x80 || *ib > 0xbf) {
ILLEGAL_CHAR_PROCESS:
				*errno = EILSEQ;
				ret_val = (size_t)-1;
				ib = oldib;
				goto ILLEGAL_CHAR_ERR;
			}

			u8 = (u8 << 8) | *ib++;
		}

		/* Now we have a valid UTF-8 character. */
		sz = ptr_utf8tocck(u8, &ib, ibtail, ob, obtail, &ret_val);
		if (sz < 0) {
			ib = oldib;
			KICONV_SET_ERRNO_AND_BREAK(E2BIG);
		}

		ob += sz;
	}

ILLEGAL_CHAR_ERR:
	*inbuf = (char *)ib;
	*inbytesleft = ibtail - ib;
	*outbuf = (char *)ob;
	*outbytesleft = obtail - ob;

	return (ret_val);
}

size_t
kiconvstr_utf8_to_cck(uchar_t *ib, size_t *inlen, uchar_t *ob, size_t *outlen,
    int flag, int *errno, kiconv_utf8tocck_t ptr_utf8tocck)
{
	uchar_t		*ibtail;
	uchar_t		*obtail;
	uchar_t		*oldib;
	size_t		ret_val;
	size_t		i;		/* temp variable in for loop */
	uint32_t	u8;
	int8_t		sz;
	boolean_t	do_not_ignore_null;

	ret_val = 0;
	ibtail = ib + *inlen;
	obtail = ob + *outlen;
	do_not_ignore_null = ((flag & KICONV_IGNORE_NULL) == 0);

	KICONV_CHECK_UTF8_BOM_WITHOUT_STATE(ib, ibtail);

	while (ib < ibtail) {
		if (*ib == '\0' && do_not_ignore_null)
			break;

		sz = u8_number_of_bytes[*ib];

		if (sz == 1) {
			if (ob >= obtail) {
				KICONV_SET_ERRNO_AND_BREAK(E2BIG);
			}

			*ob++ = *ib++;
			continue;
		}

		oldib = ib;

		if (sz <= 0) {
			KICONV_SET_ERRNO_WITH_FLAG(1, EILSEQ);
		}

		if (ibtail - ib < sz) {
			if (flag & KICONV_REPLACE_INVALID) {
				ib = ibtail;
				goto REPLACE_INVALID;
			}

			KICONV_SET_ERRNO_AND_BREAK(EINVAL);
		}

		u8 = *ib++;

		if (KICONV_IS_INVALID_UTF8_SECOND_BYTE(*ib, u8))
			goto ILLEGAL_CHAR_PROCESS;
		u8 = (u8 << 8) | *ib++;

		for (i = 2; i < sz; i++) {
			if (*ib < 0x80 || *ib > 0xbf) {
ILLEGAL_CHAR_PROCESS:
				if (flag & KICONV_REPLACE_INVALID) {
					ib = oldib + sz;
					goto REPLACE_INVALID;
				}

				*errno = EILSEQ;
				ret_val = (size_t)-1;
				ib = oldib;
				goto ILLEGAL_CHAR_ERR;
			}

			u8 = (u8 << 8) | *ib++;
		}

		/* Now we get a valid character encoded in UTF-8. */
		sz = ptr_utf8tocck(u8, &ib, ibtail, ob, obtail, &ret_val);
		if (sz < 0) {
			ib = oldib;
			KICONV_SET_ERRNO_AND_BREAK(E2BIG);
		}

		ob += sz;
		continue;

REPLACE_INVALID:
		if (ob >= obtail) {
			ib = oldib;
			KICONV_SET_ERRNO_AND_BREAK(E2BIG);
		}

		*ob++ = KICONV_ASCII_REPLACEMENT_CHAR;
		ret_val++;
	}

ILLEGAL_CHAR_ERR:
	*inlen = ibtail - ib;
	*outlen = obtail - ob;

	return (ret_val);
}

/*
 * Search key in tbl[0] <= tbl[1] <= ... <= tbl[n-1].  Return 0 if not found.
 * tbl[0] is a special element for non-identical conversion.
 */
size_t
kiconv_binsearch(uint32_t key, void *tbl, size_t nitems)
{
	size_t low, high, mid;
	kiconv_table_t *table;

	low = 1;
	high = nitems - 1;
	table = (kiconv_table_t *)tbl;

	while (low <= high) {
		mid = (low + high) / 2;

		if (key < table[mid].key)
			high = mid - 1;
		else if (key > table[mid].key)
			low = mid + 1;
		else
			return (mid);
	}

	return (0);
}