1
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
36
37
38
39
40
51
52
53
54
55
56
57
58
63
67
68
72
73
77
78
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
179
180
181
182
183
184
185
186
187
188
189
190
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include "unicode.h"
int utf8_to_text(const uint8_t *utf_8, char* buf, size_t buflen)
{
size_t i;
if (buflen < 1)
return -1;
if (!utf_8) {
buf[0] = '\0';
return 0;
}if (!utf_8) { ... }
size_t n = 0;
char *s = buf;
for (i = 0; utf_8[i] && n < (buflen - 1); ++i) {
if (isprint(utf_8[i])) {
*s++ = utf_8[i];
n++;
}if (isprint(utf_8[i])) { ... } else {
int n_chars = snprintf(s, buflen - 1 - (s - buf), "\\x%02x",
(unsigned char)utf_8[i]);
s += n_chars;
n += n_chars;
}else { ... }
}for (i = 0; utf_8[i] && n < (buflen - 1); ++i) { ... }
*s++ = '\0';
return n;
}{ ... }
/* ... */
static int is_surrogate(uint16_t uc)
{
return uc >= 0xd800 && uc <= 0xdfff;
}{ ... }
static int is_high_surrogate(uint16_t uc)
{
return (uc & 0xfffffc00) == 0xd800;
}{ ... }
static int is_low_surrogate(uint16_t uc)
{
return (uc & 0xfffffc00) == 0xdc00;
}{ ... }
static uint32_t surrogate_to_utf32(uint16_t high, uint16_t low)
{
return (high << 10) + low - 0x35fdc00;
}{ ... }
static int convert_utf16le_to_utf32(const uint8_t *input, size_t input_size,
uint32_t *output, size_t output_size)
{
const uint8_t *const end = input + input_size;
int n_output = 0;
while (input < end) {
if (input+1 >= end)
return -1;
uint16_t uc = *input++;
uc |= *input++ << 8;
if (!is_surrogate(uc)) {
if ((size_t)n_output >= output_size)
return -1;
*output++ = uc;
++n_output;
}if (!is_surrogate(uc)) { ... } else {
if (is_high_surrogate(uc) && input < end && is_low_surrogate(*input)) {
if ((size_t)n_output >= output_size)
return -1;
*output++ = surrogate_to_utf32(uc, *input++);
++n_output;
}if (is_high_surrogate(uc) && input < end && is_low_surrogate(*input)) { ... } else {
return -1;
}else { ... }
}else { ... }
}while (input < end) { ... }
return n_output;
}{ ... }
int utf16le_to_utf8(const uint8_t *utf_16le, size_t len, uint8_t* buf, size_t buflen)
{
uint32_t utf32[256];
uint8_t *p = buf;
uint8_t *endbuf = p + buflen;
int n_code_points;
int i;
if (len < 1)
return -1;
n_code_points = convert_utf16le_to_utf32(utf_16le, len,
&utf32[0], sizeof(utf32)/sizeof(utf32[0]));
if (n_code_points < 0)
return -1;
for (i = 0; i < n_code_points; ++i) {
uint32_t uc = utf32[i];
uint8_t utf8_uc[6];
size_t n_utf8_uc = 0;
if (uc <= 0x7f) {
utf8_uc[0] = uc;
n_utf8_uc = 1;
}if (uc <= 0x7f) { ... } else if (uc <= 0x7ff) {
utf8_uc[0] = 0xc0 | ((uc >> 6) & 0x1f);
utf8_uc[1] = 0x80 | (uc & 0x3f);
n_utf8_uc = 2;
}else if (uc <= 0x7ff) { ... } else if (uc <= 0xffff) {
utf8_uc[0] = 0x70 | ((uc >> 12) & 0x0f);
utf8_uc[1] = 0x80 | ((uc >> 6) & 0x3f);
utf8_uc[2] = 0x80 | (uc & 0x3f);
n_utf8_uc = 3;
}else if (uc <= 0xffff) { ... } else if (uc <= 0x1fffff) {
utf8_uc[0] = 0xf0 | ((uc >> 18) & 0x07);
utf8_uc[1] = 0x80 | ((uc >> 12) & 0x3f);
utf8_uc[2] = 0x80 | ((uc >> 6) & 0x3f);
utf8_uc[3] = 0x80 | (uc & 0x3f);
n_utf8_uc = 4;
}else if (uc <= 0x1fffff) { ... } else if (uc <= 0x3ffffff) {
utf8_uc[0] = 0xf8 | ((uc >> 24) & 0x03);
utf8_uc[1] = 0x80 | ((uc >> 18) & 0x3f);
utf8_uc[2] = 0x80 | ((uc >> 12) & 0x3f);
utf8_uc[3] = 0x80 | ((uc >> 6) & 0x3f);
utf8_uc[4] = 0x80 | (uc & 0x3f);
n_utf8_uc = 5;
}else if (uc <= 0x3ffffff) { ... } else if (uc <= 0x7fffffff) {
utf8_uc[0] = 0xfc | ((uc >> 30) & 0x01);
utf8_uc[1] = 0x80 | ((uc >> 24) & 0x3f);
utf8_uc[2] = 0x80 | ((uc >> 18) & 0x3f);
utf8_uc[3] = 0x80 | ((uc >> 12) & 0x3f);
utf8_uc[4] = 0x80 | ((uc >> 6) & 0x3f);
utf8_uc[5] = 0x80 | (uc & 0x3f);
n_utf8_uc = 6;
}else if (uc <= 0x7fffffff) { ... } else {
return -1;
}else { ... }
if (p + n_utf8_uc + 1 >= endbuf)
return -1;
memcpy(p, utf8_uc, n_utf8_uc);
p += n_utf8_uc;
}for (i = 0; i < n_code_points; ++i) { ... }
*p = '\0';
return p - buf;
}{ ... }