summaryrefslogtreecommitdiff
path: root/src/charset/encodings/utf8impl.h
blob: b1721c74b8b92d45810079c8d3ee8506e731fe6d (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
/*
 * This file is part of LibParserUtils.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
 */

#ifndef parserutils_charset_encodings_utf8impl_h_
#define parserutils_charset_encodings_utf8impl_h_

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

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

/** Number of continuation bytes for a given start byte */
extern const uint8_t numContinuations[256];

/**
 * Convert a UTF-8 multibyte sequence into a single UCS-4 character
 *
 * Encoding of UCS values outside the UTF-16 plane has been removed from
 * RFC3629. This macro conforms to RFC2279, however.
 *
 * \param s      The sequence to process
 * \param len    Length of sequence
 * \param ucs4   Pointer to location to receive UCS-4 character (host endian)
 * \param clen   Pointer to location to receive byte length of UTF-8 sequence
 * \param error  Location to receive error code
 */
#define UTF8_TO_UCS4(s, len, ucs4, clen, error)				\
do {									\
	uint32_t c, min;						\
	uint8_t n;							\
									\
	error = PARSERUTILS_OK;						\
									\
	if (s == NULL || ucs4 == NULL || clen == NULL) {		\
		error = PARSERUTILS_BADPARM;				\
		break;							\
	}								\
									\
	if (len == 0) {							\
		error = PARSERUTILS_NEEDDATA;				\
		break;							\
	}								\
									\
	c = s[0];							\
									\
	if (c < 0x80) {							\
		n = 1;							\
		min = 0;						\
	} else if ((c & 0xE0) == 0xC0) {				\
		c &= 0x1F;						\
		n = 2;							\
		min = 0x80;						\
	} else if ((c & 0xF0) == 0xE0) {				\
		c &= 0x0F;						\
		n = 3;							\
		min = 0x800;						\
	} else if ((c & 0xF8) == 0xF0) {				\
		c &= 0x07;						\
		n = 4;							\
		min = 0x10000;						\
	} else if ((c & 0xFC) == 0xF8) {				\
		c &= 0x03;						\
		n = 5;							\
		min = 0x200000;						\
	} else if ((c & 0xFE) == 0xFC) {				\
		c &= 0x01;						\
		n = 6;							\
		min = 0x4000000;					\
	} else {							\
		error = PARSERUTILS_INVALID;				\
		break;							\
	}								\
									\
	if (len < n) {							\
		error = PARSERUTILS_NEEDDATA;				\
		break;							\
	}								\
									\
	for (uint8_t i = 1; i < n; i++) {				\
		uint32_t t = s[i];					\
									\
		if ((t & 0xC0) != 0x80) {				\
			error = PARSERUTILS_INVALID;			\
			break;						\
		}							\
									\
		c <<= 6;						\
		c |= t & 0x3F;						\
	}								\
									\
	if (error == PARSERUTILS_OK) {					\
		/* Detect overlong sequences, surrogates and fffe/ffff */ \
		if (c < min || (c >= 0xD800 && c <= 0xDFFF) ||		\
				c == 0xFFFE || c == 0xFFFF) {		\
			error = PARSERUTILS_INVALID;			\
			break;						\
		}							\
									\
		*ucs4 = c;						\
		*clen = n;						\
	}								\
} while(0)

/**
 * Convert a single UCS-4 character into a UTF-8 multibyte sequence
 *
 * Encoding of UCS values outside the UTF-16 plane has been removed from
 * RFC3629. This macro conforms to RFC2279, however.
 *
 * \param ucs4   The character to process (0 <= c <= 0x7FFFFFFF) (host endian)
 * \param s      Pointer to pointer to output buffer, updated on exit
 * \param len    Pointer to length, in bytes, of output buffer, updated on exit
 * \param error  Location to receive error code
 */
#define UTF8_FROM_UCS4(ucs4, s, len, error)				\
do {									\
	uint8_t *buf;							\
	uint8_t l = 0;							\
									\
	error = PARSERUTILS_OK;						\
									\
	if (s == NULL || *s == NULL || len == NULL) {			\
		error = PARSERUTILS_BADPARM;				\
		break;							\
	}								\
									\
	if (ucs4 < 0x80) {						\
		l = 1;							\
	} else if (ucs4 < 0x800) {					\
		l = 2;							\
	} else if (ucs4 < 0x10000) {					\
		l = 3;							\
	} else if (ucs4 < 0x200000) {					\
		l = 4;							\
	} else if (ucs4 < 0x4000000) {					\
		l = 5;							\
	} else if (ucs4 <= 0x7FFFFFFF) {				\
		l = 6;							\
	} else {							\
		error = PARSERUTILS_INVALID;				\
		break;							\
	}								\
									\
	if (l > *len) {							\
		error = PARSERUTILS_NOMEM;				\
		break;							\
	}								\
									\
	buf = *s;							\
									\
	if (l == 1) {							\
		buf[0] = (uint8_t) ucs4;				\
	} else {							\
		for (uint8_t i = l; i > 1; i--) {			\
			buf[i - 1] = 0x80 | (ucs4 & 0x3F);		\
			ucs4 >>= 6;					\
		}							\
		buf[0] = ~((1 << (8 - l)) - 1) | ucs4;			\
	}								\
									\
	*s += l;							\
	*len -= l;							\
} while(0)

/**
 * 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
 * \param error  Location to receive error code
 */
#define UTF8_LENGTH(s, max, len, error)					\
do {									\
	const uint8_t *end = s + max;					\
	int l = 0;							\
									\
	error = PARSERUTILS_OK;						\
									\
	if (s == NULL || len == NULL) {					\
		error = PARSERUTILS_BADPARM;				\
		break;							\
	}								\
									\
	while (s < end) {						\
		uint32_t c = s[0];					\
									\
		if ((c & 0x80) == 0x00)					\
			s += 1;						\
		else if ((c & 0xE0) == 0xC0)				\
			s += 2;						\
		else if ((c & 0xF0) == 0xE0)				\
			s += 3;						\
		else if ((c & 0xF8) == 0xF0)				\
			s += 4;						\
		else if ((c & 0xFC) == 0xF8)				\
			s += 5;						\
		else if ((c & 0xFE) == 0xFC)				\
			s += 6;						\
		else {							\
			error = PARSERUTILS_INVALID;			\
			break;						\
		}							\
									\
		l++;							\
	}								\
									\
	if (error == PARSERUTILS_OK)					\
		*len = l;						\
} while(0)

/**
 * 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
 * \param error  Location to receive error code
 */
#define UTF8_CHAR_BYTE_LENGTH(s, len, error)				\
do {									\
	if (s == NULL || len == NULL) {					\
		error = PARSERUTILS_BADPARM;				\
		break;							\
	}								\
									\
	*len = numContinuations[s[0]] + 1 /* Start byte */;		\
									\
	error = PARSERUTILS_OK;						\
} while(0)

/**
 * 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
 * \param error    Location to receive error code
 */
#define UTF8_PREV(s, off, prevoff, error)				\
do {									\
	if (s == NULL || prevoff == NULL) {				\
		error = PARSERUTILS_BADPARM;				\
		break;							\
	}								\
									\
	while (off != 0 && (s[--off] & 0xC0) == 0x80)			\
		/* do nothing */;					\
									\
	*prevoff = off;							\
									\
	error = PARSERUTILS_OK;						\
} while(0)

/**
 * 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
 * \param error    Location to receive error code
 */
#define UTF8_NEXT(s, len, off, nextoff, error)				\
do {									\
	if (s == NULL || off >= len || nextoff == NULL) {		\
		error = PARSERUTILS_BADPARM;				\
		break;							\
	}								\
									\
	/* 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;							\
									\
	error = PARSERUTILS_OK;						\
} while(0)

/**
 * Skip to start of next sequence in UTF-8 input
 *
 * \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
 * \param error    Location to receive error code
 */
#define UTF8_NEXT_PARANOID(s, len, off, nextoff, error)			\
do {									\
	uint8_t c;							\
									\
	error = PARSERUTILS_OK;						\
									\
	if (s == NULL || off >= len || nextoff == NULL) {		\
		error = PARSERUTILS_BADPARM;				\
		break;							\
	}								\
									\
	c = s[off];							\
									\
	/* If we're mid-sequence, simply advance to next byte */	\
	if (!(c < 0x80 || (c & 0xC0) == 0xC0)) {			\
		off++;							\
	} else {							\
		uint32_t nCont = numContinuations[c];			\
		uint32_t nToSkip;					\
									\
		if (off + nCont + 1 >= len) {				\
			error = PARSERUTILS_NEEDDATA;			\
			break;						\
		}							\
									\
		/* Verify continuation bytes */				\
		for (nToSkip = 1; nToSkip <= nCont; nToSkip++) {	\
			if ((s[off + nToSkip] & 0xC0) != 0x80)		\
				break;					\
		}							\
									\
		/* Skip over the valid bytes */				\
		off += nToSkip;						\
	}								\
									\
	*nextoff = off;							\
} while(0)

#endif