summaryrefslogtreecommitdiff
path: root/usr/src/lib/iconv_modules/utf-8/common/utf8_to_ucs.c
blob: 9f00d9a0a3ff56f32d6872616fa863e189903fd2 (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
/*
 * 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 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 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 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * This is for conversions from UTF-8 to various UCS forms, esp.,
 * UCS-2, UCS-2BE, UCS-2LE, UTF-16, UTF-16BE, UTF-16LE, UCS-4, UCS-4BE,
 * UCS-4LE, UTF-32, UTF-32BE, and UTF-32LE.
 */


#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/isa_defs.h>
#include "utf8_to_ucs.h"


void *
_icv_open()
{
	ucs_state_t *cd = (ucs_state_t *)calloc(1, sizeof(ucs_state_t));

	if (cd == (ucs_state_t *)NULL) {
		errno = ENOMEM;
		return((void *)-1);
	}

#if defined(UTF_16BE) || defined(UCS_2BE) || defined(UCS_4BE) || \
	defined(UTF_32BE)
	cd->little_endian = false;
	cd->bom_written = true;
#elif defined(UTF_16LE) || defined(UCS_2LE) || defined(UCS_4LE) || \
	defined(UTF_32LE)
	cd->little_endian = true;
	cd->bom_written = true;
#elif defined(_LITTLE_ENDIAN)
	cd->little_endian = true;
#endif

	return((void *)cd);
}


void
_icv_close(ucs_state_t *cd)
{
	if (! cd)
		errno = EBADF;
	else
		free((void *)cd);
}


size_t
_icv_iconv(ucs_state_t *cd, char **inbuf, size_t *inbufleft, char **outbuf,
                size_t *outbufleft)
{
	size_t ret_val = 0;
	uchar_t *ib;
	uchar_t *ob;
	uchar_t *ibtail;
	uchar_t *obtail;

	if (! cd) {
		errno = EBADF;
		return((size_t)-1);
	}

	if (!inbuf || !(*inbuf)) {
#if defined(UCS_2) || defined(UCS_4) || defined(UTF_16) || defined(UTF_32)
		cd->bom_written = false;
#endif
		return((size_t)0);
	}

	ib = (uchar_t *)*inbuf;
	ob = (uchar_t *)*outbuf;
	ibtail = ib + *inbufleft;
	obtail = ob + *outbufleft;

	while (ib < ibtail) {
		uchar_t *ib_org;
		uint_t u4;
#if defined(UTF_16) || defined(UTF_16BE) || defined(UTF_16LE)
		uint_t u4_2;
#endif
		uint_t first_byte;
		signed char sz;
		signed char obsz;

		sz = number_of_bytes_in_utf8_char[*ib];
		if (sz == ICV_TYPE_ILLEGAL_CHAR) {
			errno = EILSEQ;
			ret_val = (size_t)-1;
			break;
		}

		if ((ibtail - ib) < sz) {
			errno = EINVAL;
			ret_val = (size_t)-1;
			break;
		}

		ib_org = ib;
		first_byte = *ib;
		u4 = (uint_t)(*ib++ & masks_tbl[sz]);
		for (; sz > 1; sz--) {
			if (first_byte) {
				if (((uchar_t)*ib) <
					valid_min_2nd_byte[first_byte] ||
				    ((uchar_t)*ib) >
					valid_max_2nd_byte[first_byte]) {
					ib = ib_org;
					errno = EILSEQ;
					ret_val = (size_t)-1;
					goto ILLEGAL_CHAR_ERR;
				}
				first_byte = 0;
			} else if (((uint_t)*ib) < 0x80 ||
				   ((uint_t)*ib) > 0xbf) {
				ib = ib_org;
				errno = EILSEQ;
				ret_val = (size_t)-1;
				goto ILLEGAL_CHAR_ERR;
			}
			u4 = (u4 << ICV_UTF8_BIT_SHIFT) |
				(((uint_t)*ib) & ICV_UTF8_BIT_MASK);
			ib++;
		}

		/* Check against known non-characters. */
		if ((u4 & ICV_UTF32_NONCHAR_mask) == ICV_UTF32_NONCHAR_fffe ||
		    (u4 & ICV_UTF32_NONCHAR_mask) == ICV_UTF32_NONCHAR_ffff ||
		    u4 > ICV_UTF32_LAST_VALID_CHAR ||
		    (u4 >= ICV_UTF32_SURROGATE_START_d800 &&
		    u4 <= ICV_UTF32_SURROGATE_END_dfff) ||
		    (u4 >= ICV_UTF32_ARABIC_NONCHAR_START_fdd0 &&
		    u4 <= ICV_UTF32_ARABIC_NONCHAR_END_fdef)) {
			ib = ib_org;
			errno = EILSEQ;
			ret_val = (size_t)-1;
			goto ILLEGAL_CHAR_ERR;
		}

#if defined(UTF_16) || defined(UTF_16BE) || defined(UTF_16LE)
		u4_2 = 0;
#endif

		if (u4 == ICV_BOM_IN_BIG_ENDIAN) {
			cd->bom_written = true;
		}

#if defined(UCS_4) || defined(UCS_4BE) || defined(UCS_4LE)
		obsz = (cd->bom_written) ? 4 : 8;
#elif defined(UTF_32) || defined(UTF_32BE) || defined(UTF_32LE)
		obsz = (cd->bom_written) ? 4 : 8;
		if (u4 > 0x10ffff) {
			u4 = ICV_CHAR_UCS2_REPLACEMENT;
			ret_val++;
		}
#elif defined(UCS_2) || defined(UCS_2BE) || defined(UCS_2LE)
		obsz = (cd->bom_written) ? 2 : 4;
		if (u4 > 0x00ffff) {
			u4 = ICV_CHAR_UCS2_REPLACEMENT;
			ret_val++;
		}
#elif defined(UTF_16) || defined(UTF_16BE) || defined(UTF_16LE)
		obsz = (cd->bom_written) ? 2 : 4;
		if (u4 > 0x10ffff) {
			u4 = ICV_CHAR_UCS2_REPLACEMENT;
			ret_val++;
		} else if (u4 > 0x00ffff) {
			u4_2 = ((u4 - 0x010000) % 0x400) + 0x00dc00;
			u4   = ((u4 - 0x010000) / 0x400) + 0x00d800;
			obsz += 2;
		}
#else
#error	"Fatal: one of the UCS macros need to be defined."
#endif
		if ((obtail - ob) < obsz) {
			ib = ib_org;
			errno = E2BIG;
			ret_val = (size_t)-1;
			break;
		}

		if (cd->little_endian) {
			if (! cd->bom_written) {
				*ob++ = (uchar_t)0xff;
				*ob++ = (uchar_t)0xfe;
#if defined(UCS_4) || defined(UCS_4BE) || defined(UCS_4LE) || \
	defined(UTF_32) || defined(UTF_32BE) || defined(UTF_32LE)
				*(ushort_t *)ob = (ushort_t)0;
				ob += 2;
#endif
				cd->bom_written = true;
			}
			*ob++ = (uchar_t)(u4 & 0xff);
			*ob++ = (uchar_t)((u4 >> 8) & 0xff);
#if defined(UCS_4) || defined(UCS_4BE) || defined(UCS_4LE) || \
	defined(UTF_32) || defined(UTF_32BE) || defined(UTF_32LE)
			*ob++ = (uchar_t)((u4 >> 16) & 0xff);
			*ob++ = (uchar_t)((u4 >> 24) & 0xff);
#elif defined(UTF_16) || defined(UTF_16BE) || defined(UTF_16LE)
			if (u4_2) {
				*ob++ = (uchar_t)(u4_2 & 0xff);
				*ob++ = (uchar_t)((u4_2 >> 8) & 0xff);
			}
#endif
		} else {
			if (! cd->bom_written) {
#if defined(UCS_4) || defined(UCS_4BE) || defined(UCS_4LE) || \
	defined(UTF_32) || defined(UTF_32BE) || defined(UTF_32LE)
				*(ushort_t *)ob = (ushort_t)0;
				ob += 2;
#endif
				*ob++ = (uchar_t)0xfe;
				*ob++ = (uchar_t)0xff;
				cd->bom_written = true;
			}
#if defined(UCS_4) || defined(UCS_4BE) || defined(UCS_4LE) || \
	defined(UTF_32) || defined(UTF_32BE) || defined(UTF_32LE)
			*ob++ = (uchar_t)((u4 >> 24) & 0xff);
			*ob++ = (uchar_t)((u4 >> 16) & 0xff);
#endif
			*ob++ = (uchar_t)((u4 >> 8) & 0xff);
			*ob++ = (uchar_t)(u4 & 0xff);
#if defined(UTF_16) || defined(UTF_16BE) || defined(UTF_16LE)
			if (u4_2) {
				*ob++ = (uchar_t)((u4_2 >> 8) & 0xff);
				*ob++ = (uchar_t)(u4_2 & 0xff);
			}
#endif
		}
	}

ILLEGAL_CHAR_ERR:
	*inbuf = (char *)ib;
	*inbufleft = ibtail - ib;
	*outbuf = (char *)ob;
	*outbufleft = obtail - ob;

	return(ret_val);
}