8#ifndef parserutils_charset_encodings_utf8impl_h_
9#define parserutils_charset_encodings_utf8impl_h_
34#define UTF8_TO_UCS4(s, len, ucs4, clen, error) \
40 error = PARSERUTILS_OK; \
42 if (s == NULL || ucs4 == NULL || clen == NULL) { \
43 error = PARSERUTILS_BADPARM; \
48 error = PARSERUTILS_NEEDDATA; \
57 } else if ((c & 0xE0) == 0xC0) { \
61 } else if ((c & 0xF0) == 0xE0) { \
65 } else if ((c & 0xF8) == 0xF0) { \
69 } else if ((c & 0xFC) == 0xF8) { \
73 } else if ((c & 0xFE) == 0xFC) { \
78 error = PARSERUTILS_INVALID; \
83 error = PARSERUTILS_NEEDDATA; \
87 for (i = 1; i < n; i++) { \
90 if ((t & 0xC0) != 0x80) { \
91 error = PARSERUTILS_INVALID; \
99 if (error == PARSERUTILS_OK) { \
101 if (c < min || (c >= 0xD800 && c <= 0xDFFF) || \
102 c == 0xFFFE || c == 0xFFFF) { \
103 error = PARSERUTILS_INVALID; \
123#define UTF8_FROM_UCS4(ucs4, s, len, error) \
128 error = PARSERUTILS_OK; \
130 if (s == NULL || *s == NULL || len == NULL) { \
131 error = PARSERUTILS_BADPARM; \
137 } else if (ucs4 < 0x800) { \
139 } else if (ucs4 < 0x10000) { \
141 } else if (ucs4 < 0x200000) { \
143 } else if (ucs4 < 0x4000000) { \
145 } else if (ucs4 <= 0x7FFFFFFF) { \
148 error = PARSERUTILS_INVALID; \
153 error = PARSERUTILS_NOMEM; \
160 buf[0] = (uint8_t) ucs4; \
163 for (i = l; i > 1; i--) { \
164 buf[i - 1] = 0x80 | (ucs4 & 0x3F); \
167 buf[0] = ~((1 << (8 - l)) - 1) | ucs4; \
182#define UTF8_LENGTH(s, max, len, error) \
184 const uint8_t *end = s + max; \
187 error = PARSERUTILS_OK; \
189 if (s == NULL || len == NULL) { \
190 error = PARSERUTILS_BADPARM; \
197 if ((c & 0x80) == 0x00) \
199 else if ((c & 0xE0) == 0xC0) \
201 else if ((c & 0xF0) == 0xE0) \
203 else if ((c & 0xF8) == 0xF0) \
205 else if ((c & 0xFC) == 0xF8) \
207 else if ((c & 0xFE) == 0xFC) \
210 error = PARSERUTILS_INVALID; \
217 if (error == PARSERUTILS_OK) \
228#define UTF8_CHAR_BYTE_LENGTH(s, len, error) \
230 if (s == NULL || len == NULL) { \
231 error = PARSERUTILS_BADPARM; \
235 *len = numContinuations[s[0]] + 1 ; \
237 error = PARSERUTILS_OK; \
249#define UTF8_PREV(s, off, prevoff, error) \
251 if (s == NULL || prevoff == NULL) { \
252 error = PARSERUTILS_BADPARM; \
256 while (off != 0 && (s[--off] & 0xC0) == 0x80) \
261 error = PARSERUTILS_OK; \
274#define UTF8_NEXT(s, len, off, nextoff, error) \
276 if (s == NULL || off >= len || nextoff == NULL) { \
277 error = PARSERUTILS_BADPARM; \
282 if (s[off] < 0x80 || (s[off] & 0xC0) == 0xC0) \
285 while (off < len && (s[off] & 0xC0) == 0x80) \
290 error = PARSERUTILS_OK; \
303#define UTF8_NEXT_PARANOID(s, len, off, nextoff, error) \
307 error = PARSERUTILS_OK; \
309 if (s == NULL || off >= len || nextoff == NULL) { \
310 error = PARSERUTILS_BADPARM; \
317 if (!(c < 0x80 || (c & 0xC0) == 0xC0)) { \
320 uint32_t nCont = numContinuations[c]; \
323 if (off + nCont + 1 >= len) { \
324 error = PARSERUTILS_NEEDDATA; \
329 for (nToSkip = 1; nToSkip <= nCont; nToSkip++) { \
330 if ((s[off + nToSkip] & 0xC0) != 0x80) \
unsigned char uint8_t
Definition acefiex.h:161
const uint8_t numContinuations[256]
Definition utf8.c:20