-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutf.c
159 lines (133 loc) · 5.25 KB
/
utf.c
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
/* Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
*/
#include <stdint.h>
#define UTF8_ACCEPT 0
#define UTF8_REJECT 1
static const uint8_t utf8d[] = {
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, // 00..1f
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, // 20..3f
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, // 40..5f
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, // 60..7f
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df
0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2
1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4
1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6
1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
};
static inline uint32_t
decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
uint32_t type = utf8d[byte];
*codep = (*state != UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte);
*state = utf8d[256 + *state*16 + type];
return *state;
}
/* returns count of valid code points, and upper bound at invalid cp count */
uint32_t
zhban_8len(uint8_t *s, uint32_t *errors_ptr) {
uint32_t codepoint;
uint32_t state = UTF8_ACCEPT;
uint32_t count, errors = 0;
for (count = 0; *s; ++s)
switch (decode(&state, &codepoint, *s)) {
case UTF8_ACCEPT:
count += 1;
break;
case UTF8_REJECT:
errors += 1;
state = UTF8_ACCEPT;
break;
default:
break;
}
if (errors_ptr)
*errors_ptr = count;
return count;
}
/* returns words written to the dst buffer. if == dstWords, do what? repeat with larger dst buffer.
skips invalid codepoints. */
uint32_t
zhban_8to16(uint8_t* src, uint32_t srcBytes, uint16_t* dst, uint32_t dstBytes) {
uint8_t* src_actual_end = src + srcBytes;
uint8_t* s = src;
uint16_t* d = dst;
uint32_t codepoint;
uint32_t state = 0;
while (s < src_actual_end) {
uint32_t dst_words_free = dstBytes/2 - (d - dst);
uint8_t* src_current_end = s + dst_words_free - 1;
if (src_actual_end < src_current_end)
src_current_end = src_actual_end;
if (src_current_end <= s)
return dstBytes;
while (s < src_current_end) {
if (decode(&state, &codepoint, *s++))
continue;
if (codepoint > 0xffff) {
/* *d++ = (uint16_t)(0xD7C0 + (codepoint >> 10));
*d++ = (uint16_t)(0xDC00 + (codepoint & 0x3FF)); */
} else {
*d++ = (uint16_t)codepoint;
}
}
}
if ((dstBytes/2 - (d - dst)) > 0)
*d++ = 0;
return (d - dst)/2;
}
/* I neither remember copying this function from somewhere, nor writing it myself. */
uint32_t
zhban_16to8(uint16_t* src, uint32_t srcBytes, uint8_t* dst, uint32_t dstBytes) {
uint8_t *ptr = dst;
uint32_t i = 0;
while ((srcBytes/2 > i) && ((ptr - dst) < (dstBytes - 4))) {
uint16_t c = src[i++];
if (c <= 0x7f) {
*ptr++ = c;
continue;
}
if (c <= 0x7ff) {
*ptr++ = (c >> 6) | 0xc0;
*ptr++ = (c & 0x3f) | 0x80;
continue;
}
*ptr++ = (c >> 12) | 0xe0;
*ptr++ = ((c >> 6) & 0x3f) | 0x80;
*ptr++ = (c & 0x3f) | 0x80;
}
*dst = 0;
return (ptr - dst);
}
uint16_t *
zhban_utf16chr(uint16_t *hay, const uint16_t *haylimit, const uint16_t needle) {
uint16_t *ptr = hay;
while(haylimit - ptr > 0)
if (*ptr == needle)
return ptr;
else
ptr++;
return ptr;
}