-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutf.c
More file actions
225 lines (215 loc) · 5.83 KB
/
utf.c
File metadata and controls
225 lines (215 loc) · 5.83 KB
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
#include"utf.h"
// Convert 32-bit unicode into UTF-8 form one by one.
// The UTF-8 output string pointer will be moved to the next position.
void u32toutf8
(
char **ppUTF8,
const uint32_t CharCode
)
{
char *pUTF8 = ppUTF8[0];
if (CharCode >= 0x4000000)
{
*pUTF8++ = 0xFC | ((CharCode >> 30) & 0x01);
*pUTF8++ = 0x80 | ((CharCode >> 24) & 0x3F);
*pUTF8++ = 0x80 | ((CharCode >> 18) & 0x3F);
*pUTF8++ = 0x80 | ((CharCode >> 12) & 0x3F);
*pUTF8++ = 0x80 | ((CharCode >> 6) & 0x3F);
*pUTF8++ = 0x80 | (CharCode & 0x3F);
}
else if (CharCode >= 0x200000)
{
*pUTF8++ = 0xF8 | ((CharCode >> 24) & 0x03);
*pUTF8++ = 0x80 | ((CharCode >> 18) & 0x3F);
*pUTF8++ = 0x80 | ((CharCode >> 12) & 0x3F);
*pUTF8++ = 0x80 | ((CharCode >> 6) & 0x3F);
*pUTF8++ = 0x80 | (CharCode & 0x3F);
}
else if (CharCode >= 0x10000)
{
*pUTF8++ = 0xF0 | ((CharCode >> 18) & 0x07);
*pUTF8++ = 0x80 | ((CharCode >> 12) & 0x3F);
*pUTF8++ = 0x80 | ((CharCode >> 6) & 0x3F);
*pUTF8++ = 0x80 | (CharCode & 0x3F);
}
else if (CharCode >= 0x0800)
{
*pUTF8++ = 0xE0 | ((CharCode >> 12) & 0x0F);
*pUTF8++ = 0x80 | ((CharCode >> 6) & 0x3F);
*pUTF8++ = 0x80 | (CharCode & 0x3F);
}
else if (CharCode >= 0x0080)
{
*pUTF8++ = 0xC0 | ((CharCode >> 6) & 0x1F);
*pUTF8++ = 0x80 | (CharCode & 0x3F);
}
else
{
*pUTF8++ = (char)CharCode;
}
// Move the pointer
ppUTF8[0] = pUTF8;
}
// Convert UTF-8 character into 32-bit unicode.
// The UTF-8 string pointer will be moved to the next position.
uint32_t utf8tou32char
(
char **ppUTF8
)
{
size_t cb = 0;
uint32_t ret = 0;
char *pUTF8 = ppUTF8[0];
// Detect the available room size of the UTF-8 buffer
while (cb < 6 && pUTF8[cb]) cb++;
if ((pUTF8[0] & 0xFE) == 0xFC) // 1111110x
{
if (6 <= cb)
{
ret =
(((uint32_t)pUTF8[0] & 0x01) << 30) |
(((uint32_t)pUTF8[1] & 0x3F) << 24) |
(((uint32_t)pUTF8[2] & 0x3F) << 18) |
(((uint32_t)pUTF8[3] & 0x3F) << 12) |
(((uint32_t)pUTF8[4] & 0x3F) << 6) |
(((uint32_t)pUTF8[5] & 0x3F) << 0);
ppUTF8[0] = &pUTF8[6];
return ret;
}
else
goto FailExit;
}
else if ((pUTF8[0] & 0xFC) == 0xF8) // 111110xx
{
if (5 <= cb)
{
ret =
(((uint32_t)pUTF8[0] & 0x03) << 24) |
(((uint32_t)pUTF8[1] & 0x3F) << 18) |
(((uint32_t)pUTF8[2] & 0x3F) << 12) |
(((uint32_t)pUTF8[3] & 0x3F) << 6) |
(((uint32_t)pUTF8[4] & 0x3F) << 0);
ppUTF8[0] = &pUTF8[5];
return ret;
}
else
goto FailExit;
}
else if ((pUTF8[0] & 0xF8) == 0xF0) // 11110xxx
{
if (4 <= cb)
{
ret =
(((uint32_t)pUTF8[0] & 0x07) << 18) |
(((uint32_t)pUTF8[1] & 0x3F) << 12) |
(((uint32_t)pUTF8[2] & 0x3F) << 6) |
(((uint32_t)pUTF8[3] & 0x3F) << 0);
ppUTF8[0] = &pUTF8[4];
return ret;
}
else
goto FailExit;
}
else if ((pUTF8[0] & 0xF0) == 0xE0) // 1110xxxx
{
if (3 <= cb)
{
ret =
(((uint32_t)pUTF8[0] & 0x0F) << 12) |
(((uint32_t)pUTF8[1] & 0x3F) << 6) |
(((uint32_t)pUTF8[2] & 0x3F) << 0);
ppUTF8[0] = &pUTF8[3];
return ret;
}
else
goto FailExit;
}
else if ((pUTF8[0] & 0xE0) == 0xC0) // 110xxxxx
{
if (2 <= cb)
{
ret =
(((uint32_t)pUTF8[0] & 0x1F) << 6) |
(((uint32_t)pUTF8[1] & 0x3F) << 0);
ppUTF8[0] = &pUTF8[2];
return ret;
}
else
goto FailExit;
}
else if ((pUTF8[0] & 0xC0) == 0x80) // 10xxxxxx
{
// Wrong encode
goto FailExit;
}
else if ((pUTF8[0] & 0x80) == 0x00) // 0xxxxxxx
{
ret = pUTF8[0] & 0x7F;
ppUTF8[0] = &pUTF8[1];
return ret;
}
return ret;
FailExit:
// If convert failed, null-char will be returned.
return ret;
}
// Convert 32-bit unicode into UTF-16 form one by one.
// The UTF-16 output string pointer will be moved to the next position.
void u32toutf16
(
uint16_t **ppUTF16,
const uint32_t CharCode
)
{
uint16_t *pUTF16 = ppUTF16[0];
if (CharCode <= 0xffff)
{
*pUTF16++ = (uint16_t)CharCode;
}
else if (CharCode <= 0x10ffff)
{
uint32_t U20 = CharCode - 0x10000;
uint16_t HighSurrogate = 0xD800 | (uint16_t)(U20 >> 10);
uint16_t LowSurrogate = 0xDC00 | (uint16_t)(U20 & 0x3FF);
*pUTF16++ = HighSurrogate;
*pUTF16++ = LowSurrogate;
}
else
{
*pUTF16++ = 0xFFFD;
}
ppUTF16[0] = pUTF16;
}
// Convert UTF-16 character into 32-bit unicode.
// The UTF-16 string pointer will be moved to the next position.
uint32_t u16tou32char
(
uint16_t **ppUTF16
)
{
uint16_t *pUTF16 = ppUTF16[0];
uint32_t CharCode = 0xFFFD;
switch (pUTF16[0] & 0xFC00)
{
case 0xD800:
if ((pUTF16[1] & 0xFC00) == 0xDC00)
{
CharCode = (pUTF16[1] & 0x3FF) | ((uint32_t)(pUTF16[0] & 0x3FF) << 10);
CharCode += 0x10000;
pUTF16 += 2;
}
break;
case 0xDC00:
if ((pUTF16[1] & 0xFC00) == 0xD800)
{
CharCode = (pUTF16[0] & 0x3FF) | ((uint32_t)(pUTF16[1] & 0x3FF) << 10);
CharCode += 0x10000;
pUTF16 += 2;
}
break;
default:
CharCode = *pUTF16++;
}
ppUTF16[0] = pUTF16;
return CharCode;
}