2
* Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
4
* Jansson is free software; you can redistribute it and/or modify
5
* it under the terms of the MIT license. See LICENSE for details.
11
int utf8_encode(int32_t codepoint, char *buffer, int *size)
15
else if(codepoint < 0x80)
17
buffer[0] = (char)codepoint;
20
else if(codepoint < 0x800)
22
buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6);
23
buffer[1] = 0x80 + ((codepoint & 0x03F));
26
else if(codepoint < 0x10000)
28
buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12);
29
buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6);
30
buffer[2] = 0x80 + ((codepoint & 0x003F));
33
else if(codepoint <= 0x10FFFF)
35
buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18);
36
buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12);
37
buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6);
38
buffer[3] = 0x80 + ((codepoint & 0x00003F));
47
int utf8_check_first(char byte)
49
unsigned char u = (unsigned char)byte;
54
if(0x80 <= u && u <= 0xBF) {
55
/* second, third or fourth byte of a multi-byte
56
sequence, i.e. a "continuation byte" */
59
else if(u == 0xC0 || u == 0xC1) {
60
/* overlong encoding of an ASCII byte */
63
else if(0xC2 <= u && u <= 0xDF) {
68
else if(0xE0 <= u && u <= 0xEF) {
72
else if(0xF0 <= u && u <= 0xF4) {
76
else { /* u >= 0xF5 */
77
/* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
83
int utf8_check_full(const char *buffer, int size, int32_t *codepoint)
87
unsigned char u = (unsigned char)buffer[0];
104
for(i = 1; i < size; i++)
106
u = (unsigned char)buffer[i];
108
if(u < 0x80 || u > 0xBF) {
109
/* not a continuation byte */
113
value = (value << 6) + (u & 0x3F);
116
if(value > 0x10FFFF) {
117
/* not in Unicode range */
121
else if(0xD800 <= value && value <= 0xDFFF) {
122
/* invalid code point (UTF-16 surrogate halves) */
126
else if((size == 2 && value < 0x80) ||
127
(size == 3 && value < 0x800) ||
128
(size == 4 && value < 0x10000)) {
129
/* overlong encoding */
139
const char *utf8_iterate(const char *buffer, int32_t *codepoint)
147
count = utf8_check_first(buffer[0]);
152
value = (unsigned char)buffer[0];
155
if(!utf8_check_full(buffer, count, &value))
162
return buffer + count;
165
int utf8_check_string(const char *string, int length)
170
length = strlen(string);
172
for(i = 0; i < length; i++)
174
int count = utf8_check_first(string[i]);
179
if(i + count > length)
182
if(!utf8_check_full(&string[i], count, NULL))