1 |
|
|
#include "node.h" |
2 |
|
|
#include "string_bytes.h" |
3 |
|
|
#include "util-inl.h" |
4 |
|
|
#include "v8.h" |
5 |
|
|
|
6 |
|
|
namespace node { |
7 |
|
|
|
8 |
|
|
using v8::HandleScope; |
9 |
|
|
using v8::Isolate; |
10 |
|
|
using v8::Local; |
11 |
|
|
using v8::Value; |
12 |
|
|
|
13 |
|
89607 |
enum encoding ParseEncoding(const char* encoding, |
14 |
|
|
enum encoding default_encoding) { |
15 |
✓✓✓✓ ✓✓ |
89607 |
switch (encoding[0]) { |
16 |
|
17114 |
case 'u': |
17 |
|
|
case 'U': |
18 |
|
|
// utf8, utf16le |
19 |
✓✓✓✗
|
17114 |
if (encoding[1] == 't' && encoding[2] == 'f') { |
20 |
|
|
// Skip `-` |
21 |
✓✓ |
17089 |
const size_t skip = encoding[3] == '-' ? 4 : 3; |
22 |
✓✓✓✗
|
17089 |
if (encoding[skip] == '8' && encoding[skip + 1] == '\0') |
23 |
|
17067 |
return UTF8; |
24 |
✓✓ |
22 |
if (strncmp(encoding + skip, "16le", 5) == 0) |
25 |
|
22 |
return UCS2; |
26 |
|
|
// ucs2 |
27 |
✓✓✓✗
|
25 |
} else if (encoding[1] == 'c' && encoding[2] == 's') { |
28 |
✗✓ |
22 |
const size_t skip = encoding[3] == '-' ? 4 : 3; |
29 |
✓✗✓✗
|
22 |
if (encoding[skip] == '2' && encoding[skip + 1] == '\0') |
30 |
|
22 |
return UCS2; |
31 |
|
|
} |
32 |
✗✓ |
6 |
if (StringEqualNoCase(encoding, "utf8")) |
33 |
|
|
return UTF8; |
34 |
✓✓ |
6 |
if (StringEqualNoCase(encoding, "utf-8")) |
35 |
|
2 |
return UTF8; |
36 |
✗✓ |
4 |
if (StringEqualNoCase(encoding, "ucs2")) |
37 |
|
|
return UCS2; |
38 |
✗✓ |
4 |
if (StringEqualNoCase(encoding, "ucs-2")) |
39 |
|
|
return UCS2; |
40 |
✗✓ |
4 |
if (StringEqualNoCase(encoding, "utf16le")) |
41 |
|
|
return UCS2; |
42 |
✓✓ |
4 |
if (StringEqualNoCase(encoding, "utf-16le")) |
43 |
|
1 |
return UCS2; |
44 |
|
3 |
break; |
45 |
|
|
|
46 |
|
66722 |
case 'l': |
47 |
|
|
case 'L': |
48 |
|
|
// latin1 |
49 |
✓✓ |
66722 |
if (encoding[1] == 'a') { |
50 |
✓✗ |
66720 |
if (strncmp(encoding + 2, "tin1", 5) == 0) |
51 |
|
66720 |
return LATIN1; |
52 |
|
|
} |
53 |
✗✓ |
2 |
if (StringEqualNoCase(encoding, "latin1")) |
54 |
|
|
return LATIN1; |
55 |
|
2 |
break; |
56 |
|
|
|
57 |
|
2362 |
case 'b': |
58 |
|
|
case 'B': |
59 |
|
|
// binary is a deprecated alias of latin1 |
60 |
✓✓ |
2362 |
if (encoding[1] == 'i') { |
61 |
✓✗ |
19 |
if (strncmp(encoding + 2, "nary", 5) == 0) |
62 |
|
19 |
return LATIN1; |
63 |
|
|
// buffer |
64 |
✓✓ |
2343 |
} else if (encoding[1] == 'u') { |
65 |
✓✗ |
2154 |
if (strncmp(encoding + 2, "ffer", 5) == 0) |
66 |
|
2154 |
return BUFFER; |
67 |
|
|
// base64 |
68 |
✓✓ |
189 |
} else if (encoding[1] == 'a') { |
69 |
✓✓ |
187 |
if (strncmp(encoding + 2, "se64", 5) == 0) |
70 |
|
170 |
return BASE64; |
71 |
✓✗ |
17 |
if (strncmp(encoding + 2, "se64url", 8) == 0) |
72 |
|
17 |
return BASE64URL; |
73 |
|
|
} |
74 |
✗✓ |
2 |
if (StringEqualNoCase(encoding, "binary")) |
75 |
|
|
return LATIN1; // BINARY is a deprecated alias of LATIN1. |
76 |
✗✓ |
2 |
if (StringEqualNoCase(encoding, "buffer")) |
77 |
|
|
return BUFFER; |
78 |
✗✓ |
2 |
if (StringEqualNoCase(encoding, "base64")) |
79 |
|
|
return BASE64; |
80 |
✗✓ |
2 |
if (StringEqualNoCase(encoding, "base64url")) |
81 |
|
|
return BASE64URL; |
82 |
|
2 |
break; |
83 |
|
|
|
84 |
|
93 |
case 'a': |
85 |
|
|
case 'A': |
86 |
|
|
// ascii |
87 |
✓✗ |
93 |
if (encoding[1] == 's') { |
88 |
✓✗ |
93 |
if (strncmp(encoding + 2, "cii", 4) == 0) |
89 |
|
93 |
return ASCII; |
90 |
|
|
} |
91 |
|
|
if (StringEqualNoCase(encoding, "ascii")) |
92 |
|
|
return ASCII; |
93 |
|
|
break; |
94 |
|
|
|
95 |
|
3315 |
case 'h': |
96 |
|
|
case 'H': |
97 |
|
|
// hex |
98 |
✓✗ |
3315 |
if (encoding[1] == 'e') |
99 |
✓✗✓✗
|
3315 |
if (encoding[2] == 'x' && encoding[3] == '\0') |
100 |
|
3315 |
return HEX; |
101 |
|
|
if (StringEqualNoCase(encoding, "hex")) |
102 |
|
|
return HEX; |
103 |
|
|
break; |
104 |
|
|
} |
105 |
|
8 |
return default_encoding; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
|
109 |
|
229564 |
enum encoding ParseEncoding(Isolate* isolate, |
110 |
|
|
Local<Value> encoding_v, |
111 |
|
|
enum encoding default_encoding) { |
112 |
✗✓ |
229564 |
CHECK(!encoding_v.IsEmpty()); |
113 |
|
|
|
114 |
✓✓ |
459128 |
if (!encoding_v->IsString()) |
115 |
|
139957 |
return default_encoding; |
116 |
|
|
|
117 |
|
179214 |
Utf8Value encoding(isolate, encoding_v); |
118 |
|
|
|
119 |
|
89607 |
return ParseEncoding(*encoding, default_encoding); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
2 |
Local<Value> Encode(Isolate* isolate, |
123 |
|
|
const char* buf, |
124 |
|
|
size_t len, |
125 |
|
|
enum encoding encoding) { |
126 |
✗✓ |
2 |
CHECK_NE(encoding, UCS2); |
127 |
|
|
Local<Value> error; |
128 |
|
4 |
return StringBytes::Encode(isolate, buf, len, encoding, &error) |
129 |
|
2 |
.ToLocalChecked(); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) { |
133 |
|
|
Local<Value> error; |
134 |
|
|
return StringBytes::Encode(isolate, buf, len, &error) |
135 |
|
|
.ToLocalChecked(); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
// Returns -1 if the handle was not valid for decoding |
139 |
|
|
ssize_t DecodeBytes(Isolate* isolate, |
140 |
|
|
Local<Value> val, |
141 |
|
|
enum encoding encoding) { |
142 |
|
|
HandleScope scope(isolate); |
143 |
|
|
|
144 |
|
|
return StringBytes::Size(isolate, val, encoding).FromMaybe(-1); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
// Returns number of bytes written. |
148 |
|
|
ssize_t DecodeWrite(Isolate* isolate, |
149 |
|
|
char* buf, |
150 |
|
|
size_t buflen, |
151 |
|
|
Local<Value> val, |
152 |
|
|
enum encoding encoding) { |
153 |
|
|
return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
} // namespace node |