-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathvalidation.c
109 lines (90 loc) · 2.53 KB
/
validation.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
#define NAPI_VERSION 1
#include <assert.h>
#include <string.h>
#include <node_api.h>
napi_value IsValidUTF8(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 1;
napi_value argv[1];
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
assert(status == napi_ok);
uint8_t *buf;
size_t len;
status = napi_get_buffer_info(env, argv[0], (void **)&buf, &len);
assert(status == napi_ok);
size_t i = 0;
//
// This code has been taken from utf8_check.c which was developed by
// Markus Kuhn <http://www.cl.cam.ac.uk/~mgk25/>.
//
// For original code / licensing please refer to
// https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c
//
while (i < len) {
size_t j = i + 8;
if (j <= len) {
//
// Read 8 bytes and check if they are ASCII.
//
uint64_t chunk;
memcpy(&chunk, buf + i, 8);
if ((chunk & 0x8080808080808080) == 0x00) {
i = j;
continue;
}
}
while ((buf[i] & 0x80) == 0x00) { // 0xxxxxxx
if (++i == len) {
goto exit;
}
}
if ((buf[i] & 0xe0) == 0xc0) { // 110xxxxx 10xxxxxx
if (
i + 1 == len ||
(buf[i + 1] & 0xc0) != 0x80 ||
(buf[i] & 0xfe) == 0xc0 // overlong
) {
break;
}
i += 2;
} else if ((buf[i] & 0xf0) == 0xe0) { // 1110xxxx 10xxxxxx 10xxxxxx
if (
i + 2 >= len ||
(buf[i + 1] & 0xc0) != 0x80 ||
(buf[i + 2] & 0xc0) != 0x80 ||
(buf[i] == 0xe0 && (buf[i + 1] & 0xe0) == 0x80) || // overlong
(buf[i] == 0xed && (buf[i + 1] & 0xe0) == 0xa0) // surrogate (U+D800 - U+DFFF)
) {
break;
}
i += 3;
} else if ((buf[i] & 0xf8) == 0xf0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
if (
i + 3 >= len ||
(buf[i + 1] & 0xc0) != 0x80 ||
(buf[i + 2] & 0xc0) != 0x80 ||
(buf[i + 3] & 0xc0) != 0x80 ||
(buf[i] == 0xf0 && (buf[i + 1] & 0xf0) == 0x80) || // overlong
(buf[i] == 0xf4 && buf[i + 1] > 0x8f) || buf[i] > 0xf4 // > U+10FFFF
) {
break;
}
i += 4;
} else {
break;
}
}
exit:;
napi_value result;
status = napi_get_boolean(env, i == len, &result);
assert(status == napi_ok);
return result;
}
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_value isValidUTF8;
status = napi_create_function(env, NULL, 0, IsValidUTF8, NULL, &isValidUTF8);
assert(status == napi_ok);
return isValidUTF8;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)