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
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
#include "nxt_tests.h"
#define NXT_UTF8_START_TEST 0xc2
//#define NXT_UTF8_START_TEST 0
static u_char invalid[] = {
/* Invalid first byte less than 0xc2. */
1, 0x80, 0x00, 0x00, 0x00,
1, 0xc0, 0x00, 0x00, 0x00,
2, 0xc0, 0x00, 0x00, 0x00,
3, 0xc0, 0x00, 0x00, 0x00,
4, 0xc0, 0x00, 0x00, 0x00,
/* Invalid 0x0x110000 value. */
4, 0xf4, 0x90, 0x80, 0x80,
/* Incomplete length. */
2, 0xe0, 0xaf, 0xb5, 0x00,
/* Overlong values. */
2, 0xc0, 0x80, 0x00, 0x00,
2, 0xc1, 0xb3, 0x00, 0x00,
3, 0xe0, 0x80, 0x80, 0x00,
3, 0xe0, 0x81, 0xb3, 0x00,
3, 0xe0, 0x90, 0x9a, 0x00,
4, 0xf0, 0x80, 0x8a, 0x80,
4, 0xf0, 0x80, 0x81, 0xb3,
4, 0xf0, 0x80, 0xaf, 0xb5,
};
static nxt_int_t
nxt_utf8_overlong(nxt_thread_t *thr, u_char *overlong, size_t len)
{
u_char *p, utf8[4];
size_t size;
uint32_t u, d;
nxt_uint_t i;
const u_char *pp;
pp = overlong;
d = nxt_utf8_decode(&pp, overlong + len);
len = pp - overlong;
if (d != 0xffffffff) {
p = nxt_utf8_encode(utf8, d);
size = (p != NULL) ? p - utf8 : 0;
if (len != size || nxt_memcmp(overlong, utf8, size) != 0) {
u = 0;
for (i = 0; i < len; i++) {
u = (u << 8) + overlong[i];
}
nxt_log_alert(thr->log,
"nxt_utf8_decode(%05uxD, %uz) failed: %05uxD, %uz",
u, len, d, size);
return NXT_ERROR;
}
}
return NXT_OK;
}
nxt_int_t
nxt_utf8_test(nxt_thread_t *thr)
{
u_char *p, utf8[4];
size_t len;
int32_t n;
uint32_t u, d;
nxt_uint_t i, k, l, m;
const u_char *pp;
nxt_thread_time_update(thr);
nxt_log_error(NXT_LOG_NOTICE, thr->log, "utf8 test started");
/* Test valid UTF-8. */
for (u = 0; u < 0x110000; u++) {
p = nxt_utf8_encode(utf8, u);
if (p == NULL) {
nxt_log_alert(thr->log, "nxt_utf8_encode(%05uxD) failed", u);
return NXT_ERROR;
}
pp = utf8;
d = nxt_utf8_decode(&pp, p);
if (u != d) {
nxt_log_alert(thr->log, "nxt_utf8_decode(%05uxD) failed: %05uxD",
u, d);
return NXT_ERROR;
}
}
/* Test some invalid UTF-8. */
for (i = 0; i < sizeof(invalid); i += 5) {
len = invalid[i];
utf8[0] = invalid[i + 1];
utf8[1] = invalid[i + 2];
utf8[2] = invalid[i + 3];
utf8[3] = invalid[i + 4];
pp = utf8;
d = nxt_utf8_decode(&pp, utf8 + len);
if (d != 0xffffffff) {
u = 0;
for (i = 0; i < len; i++) {
u = (u << 8) + utf8[i];
}
nxt_log_alert(thr->log,
"nxt_utf8_decode(%05uxD, %uz) failed: %05uxD",
u, len, d);
return NXT_ERROR;
}
}
/* Test all overlong UTF-8. */
for (i = NXT_UTF8_START_TEST; i < 256; i++) {
utf8[0] = i;
if (nxt_utf8_overlong(thr, utf8, 1) != NXT_OK) {
return NXT_ERROR;
}
for (k = 0; k < 256; k++) {
utf8[1] = k;
if (nxt_utf8_overlong(thr, utf8, 2) != NXT_OK) {
return NXT_ERROR;
}
for (l = 0; l < 256; l++) {
utf8[2] = l;
if (nxt_utf8_overlong(thr, utf8, 3) != NXT_OK) {
return NXT_ERROR;
}
for (m = 0; m < 256; m++) {
utf8[3] = m;
if (nxt_utf8_overlong(thr, utf8, 4) != NXT_OK) {
return NXT_ERROR;
}
}
}
}
}
n = nxt_utf8_casecmp((u_char *) "ABC АБВ ΑΒΓ",
(u_char *) "abc абв αβγ",
sizeof("ABC АБВ ΑΒΓ") - 1,
sizeof("abc абв αβγ") - 1);
if (n != 0) {
nxt_log_alert(thr->log, "nxt_utf8_casecmp() failed");
return NXT_ERROR;
}
nxt_log_error(NXT_LOG_NOTICE, thr->log, "utf8 test passed");
return NXT_OK;
}
|