summaryrefslogtreecommitdiff
path: root/src/utils/utf8.c
blob: b80f04e7bf573b8886918264f01d5b19d99169ff (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
 */

/** \file
 * UTF-8 manipulation functions (implementation).
 */

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "utils/utf8.h"

/** Number of continuation bytes for a given start byte */
static const uint8_t numContinuations[256] = {
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
};

/**
 * Convert a UTF-8 multibyte sequence into a single UCS4 character
 *
 * Encoding of UCS values outside the UTF-16 plane has been removed from
 * RFC3629. This function conforms to RFC2279, however.
 *
 * \param s     The sequence to process
 * \param len   Length of sequence
 * \param ucs4  Pointer to location to receive UCS4 character (host endian)
 * \param clen  Pointer to location to receive byte length of UTF-8 sequence
 * \return CHARSET_OK on success, appropriate error otherwise
 */
inline charset_error _dom_utf8_to_ucs4(const uint8_t *s, size_t len,
		uint32_t *ucs4, size_t *clen)
{
	if (s == NULL || ucs4 == NULL || clen == NULL)
		return CHARSET_BADPARM;

	if (len == 0)
		return CHARSET_NEEDDATA;

	if (*s < 0x80) {
		*ucs4 = *s;
		*clen = 1;
	} else if ((*s & 0xE0) == 0xC0) {
		if (len < 2)
			return CHARSET_NEEDDATA;
		else if ((*(s+1) & 0xC0) != 0x80)
			return CHARSET_INVALID;
		else {
			*ucs4 = ((*s & 0x1F) << 6) | (*(s+1) & 0x3F);
			*clen = 2;
		}
	} else if ((*s & 0xF0) == 0xE0) {
		if (len < 3)
			return CHARSET_NEEDDATA;
		else if ((*(s+1) & 0xC0) != 0x80 ||
				(*(s+2) & 0xC0) != 0x80)
			return CHARSET_INVALID;
		else {
			*ucs4 = ((*s & 0x0F) << 12) |
				((*(s+1) & 0x3F) << 6) |
				(*(s+2) & 0x3F);
			*clen = 3;
		}
	} else if ((*s & 0xF8) == 0xF0) {
		if (len < 4)
			return CHARSET_NEEDDATA;
		else if ((*(s+1) & 0xC0) != 0x80 ||
				(*(s+2) & 0xC0) != 0x80 ||
				(*(s+3) & 0xC0) != 0x80)
			return CHARSET_INVALID;
		else {
			*ucs4 = ((*s & 0x0F) << 18) |
				((*(s+1) & 0x3F) << 12) |
				((*(s+2) & 0x3F) << 6) |
				(*(s+3) & 0x3F);
			*clen = 4;
		}
	} else if ((*s & 0xFC) == 0xF8) {
		if (len < 5)
			return CHARSET_NEEDDATA;
		else if ((*(s+1) & 0xC0) != 0x80 ||
				(*(s+2) & 0xC0) != 0x80 ||
				(*(s+3) & 0xC0) != 0x80 ||
				(*(s+4) & 0xC0) != 0x80)
			return CHARSET_INVALID;
		else {
			*ucs4 = ((*s & 0x0F) << 24) |
				((*(s+1) & 0x3F) << 18) |
				((*(s+2) & 0x3F) << 12) |
				((*(s+3) & 0x3F) << 6) |
				(*(s+4) & 0x3F);
			*clen = 5;
		}
	} else if ((*s & 0xFE) == 0xFC) {
		if (len < 6)
			return CHARSET_NEEDDATA;
		else if ((*(s+1) & 0xC0) != 0x80 ||
				(*(s+2) & 0xC0) != 0x80 ||
				(*(s+3) & 0xC0) != 0x80 ||
				(*(s+4) & 0xC0) != 0x80 ||
				(*(s+5) & 0xC0) != 0x80)
			return CHARSET_INVALID;
		else {
			*ucs4 = ((*s & 0x0F) << 28) |
				((*(s+1) & 0x3F) << 24) |
				((*(s+2) & 0x3F) << 18) |
				((*(s+3) & 0x3F) << 12) |
				((*(s+4) & 0x3F) << 6) |
				(*(s+5) & 0x3F);
			*clen = 6;
		}
	} else {
		return CHARSET_INVALID;
	}

	return CHARSET_OK;
}

/**
 * Convert a single UCS4 character into a UTF-8 multibyte sequence
 *
 * Encoding of UCS values outside the UTF-16 plane has been removed from
 * RFC3629. This function conforms to RFC2279, however.
 *
 * \param ucs4  The character to process (0 <= c <= 0x7FFFFFFF) (host endian)
 * \param s     Pointer to 6 byte long output buffer
 * \param len   Pointer to location to receive length of multibyte sequence
 * \return CHARSET_OK on success, appropriate error otherwise
 */
inline charset_error _dom_utf8_from_ucs4(uint32_t ucs4, uint8_t *s,
		size_t *len)
{
	uint32_t l = 0;

	if (s == NULL || len == NULL)
		return CHARSET_BADPARM;
	else if (ucs4 < 0x80) {
		*s = (uint8_t) ucs4;
		l = 1;
	} else if (ucs4 < 0x800) {
		*s = 0xC0 | ((ucs4 >> 6) & 0x1F);
		*(s+1) = 0x80 | (ucs4 & 0x3F);
		l = 2;
	} else if (ucs4 < 0x10000) {
		*s = 0xE0 | ((ucs4 >> 12) & 0xF);
		*(s+1) = 0x80 | ((ucs4 >> 6) & 0x3F);
		*(s+2) = 0x80 | (ucs4 & 0x3F);
		l = 3;
	} else if (ucs4 < 0x200000) {
		*s = 0xF0 | ((ucs4 >> 18) & 0x7);
		*(s+1) = 0x80 | ((ucs4 >> 12) & 0x3F);
		*(s+2) = 0x80 | ((ucs4 >> 6) & 0x3F);
		*(s+3) = 0x80 | (ucs4 & 0x3F);
		l = 4;
	} else if (ucs4 < 0x4000000) {
		*s = 0xF8 | ((ucs4 >> 24) & 0x3);
		*(s+1) = 0x80 | ((ucs4 >> 18) & 0x3F);
		*(s+2) = 0x80 | ((ucs4 >> 12) & 0x3F);
		*(s+3) = 0x80 | ((ucs4 >> 6) & 0x3F);
		*(s+4) = 0x80 | (ucs4 & 0x3F);
		l = 5;
	} else if (ucs4 <= 0x7FFFFFFF) {
		*s = 0xFC | ((ucs4 >> 30) & 0x1);
		*(s+1) = 0x80 | ((ucs4 >> 24) & 0x3F);
		*(s+2) = 0x80 | ((ucs4 >> 18) & 0x3F);
		*(s+3) = 0x80 | ((ucs4 >> 12) & 0x3F);
		*(s+4) = 0x80 | ((ucs4 >> 6) & 0x3F);
		*(s+5) = 0x80 | (ucs4 & 0x3F);
		l = 6;
	} else {
		return CHARSET_INVALID;
	}

	*len = l;

	return CHARSET_OK;
}

/**
 * Calculate the length (in characters) of a bounded UTF-8 string
 *
 * \param s    The string
 * \param max  Maximum length
 * \param len  Pointer to location to receive length of string
 * \return CHARSET_OK on success, appropriate error otherwise
 */
inline charset_error _dom_utf8_length(const uint8_t *s, size_t max,
		size_t *len)
{
	const uint8_t *end = s + max;
	int l = 0;

	if (s == NULL || len == NULL)
		return CHARSET_BADPARM;

	while (s < end) {
		if ((*s & 0x80) == 0x00)
			s += 1;
		else if ((*s & 0xE0) == 0xC0)
			s += 2;
		else if ((*s & 0xF0) == 0xE0)
			s += 3;
		else if ((*s & 0xF8) == 0xF0)
			s += 4;
		else if ((*s & 0xFC) == 0xF8)
			s += 5;
		else if ((*s & 0xFE) == 0xFC)
			s += 6;
		else
			return CHARSET_INVALID;
		l++;
	}

	*len = l;

	return CHARSET_OK;
}

/**
 * Calculate the length (in bytes) of a UTF-8 character
 *
 * \param s    Pointer to start of character
 * \param len  Pointer to location to receive length
 * \return CHARSET_OK on success, appropriate error otherwise
 */
inline charset_error _dom_utf8_char_byte_length(const uint8_t *s,
		size_t *len)
{
	if (s == NULL || len == NULL)
		return CHARSET_BADPARM;

	*len = numContinuations[s[0]] + 1 /* Start byte */;

	return CHARSET_OK;
}

/**
 * Find previous legal UTF-8 char in string
 *
 * \param s        The string
 * \param off      Offset in the string to start at
 * \param prevoff  Pointer to location to receive offset of first byte of
 *                 previous legal character
 * \return CHARSET_OK on success, appropriate error otherwise
 */
inline charset_error _dom_utf8_prev(const uint8_t *s, uint32_t off,
		uint32_t *prevoff)
{
	if (s == NULL || prevoff == NULL)
		return CHARSET_BADPARM;

	while (off != 0 && (s[--off] & 0xC0) == 0x80)
		/* do nothing */;

	*prevoff = off;

	return CHARSET_OK;
}

/**
 * Find next legal UTF-8 char in string
 *
 * \param s        The string (assumed valid)
 * \param len      Maximum offset in string
 * \param off      Offset in the string to start at
 * \param nextoff  Pointer to location to receive offset of first byte of
 *                 next legal character
 * \return CHARSET_OK on success, appropriate error otherwise
 */
inline charset_error _dom_utf8_next(const uint8_t *s, uint32_t len,
		uint32_t off, uint32_t *nextoff)
{
	if (s == NULL || off >= len || nextoff == NULL)
		return CHARSET_BADPARM;

	/* Skip current start byte (if present - may be mid-sequence) */
	if (s[off] < 0x80 || (s[off] & 0xC0) == 0xC0)
		off++;

	while (off < len && (s[off] & 0xC0) == 0x80)
		off++;

	*nextoff = off;

	return CHARSET_OK;
}

/**
 * Find next legal UTF-8 char in string
 *
 * \param s        The string (assumed to be of dubious validity)
 * \param len      Maximum offset in string
 * \param off      Offset in the string to start at
 * \param nextoff  Pointer to location to receive offset of first byte of
 *                 next legal character
 * \return CHARSET_OK on success, appropriate error otherwise
 */
inline charset_error _dom_utf8_next_paranoid(const uint8_t *s, uint32_t len,
		uint32_t off, uint32_t *nextoff)
{
	bool valid;

	if (s == NULL || off >= len || nextoff == NULL)
		return CHARSET_BADPARM;

	/* Skip current start byte (if present - may be mid-sequence) */
	if (s[off] < 0x80 || (s[off] & 0xC0) == 0xC0)
		off++;

	while (1) {
		/* Find next possible start byte */
		while (off < len && (s[off] & 0xC0) == 0x80)
			off++;

		/* Ran off end of data */
		if (off == len || off + numContinuations[s[off]] >= len)
			return CHARSET_NEEDDATA;

		/* Found if start byte is ascii,
		 * or next n bytes are valid continuations */
		valid = true;

		switch (numContinuations[s[off]]) {
		case 5:
			valid &= ((s[off + 5] & 0xC0) == 0x80);
		case 4:
			valid &= ((s[off + 4] & 0xC0) == 0x80);
		case 3:
			valid &= ((s[off + 3] & 0xC0) == 0x80);
		case 2:
			valid &= ((s[off + 2] & 0xC0) == 0x80);
		case 1:
			valid &= ((s[off + 1] & 0xC0) == 0x80);
		case 0:
			valid &= (s[off + 0] < 0x80);
		}

		if (valid)
			break;

		/* Otherwise, skip this (invalid) start byte and try again */
		off++;
	}

	*nextoff = off;

	return CHARSET_OK;
}