1 |
|
|
#include "node.h" |
2 |
|
|
#include "env-inl.h" |
3 |
|
|
#include "string_bytes.h" |
4 |
|
|
#include "util-inl.h" |
5 |
|
|
#include "v8.h" |
6 |
|
|
|
7 |
|
|
namespace node { |
8 |
|
|
|
9 |
|
|
using v8::HandleScope; |
10 |
|
|
using v8::Isolate; |
11 |
|
|
using v8::Local; |
12 |
|
|
using v8::Value; |
13 |
|
|
|
14 |
|
1764601 |
enum encoding ParseEncoding(const char* encoding, |
15 |
|
|
enum encoding default_encoding) { |
16 |
✓✓✓✓ ✓ |
1764601 |
switch (encoding[0]) { |
17 |
|
|
case 'u': |
18 |
|
|
// utf8, utf16le |
19 |
✓✓✓✗
|
562766 |
if (encoding[1] == 't' && encoding[2] == 'f') { |
20 |
|
|
// Skip `-` |
21 |
✓✓ |
562729 |
encoding += encoding[3] == '-' ? 4 : 3; |
22 |
✓✓✓✗
|
562729 |
if (encoding[0] == '8' && encoding[1] == '\0') |
23 |
|
562712 |
return UTF8; |
24 |
✓✗ |
17 |
if (strncmp(encoding, "16le", 4) == 0) |
25 |
|
17 |
return UCS2; |
26 |
|
|
|
27 |
|
|
// ucs2 |
28 |
✓✓✓✗
|
37 |
} else if (encoding[1] == 'c' && encoding[2] == 's') { |
29 |
✗✓ |
36 |
encoding += encoding[3] == '-' ? 4 : 3; |
30 |
✓✗✓✗
|
36 |
if (encoding[0] == '2' && encoding[1] == '\0') |
31 |
|
36 |
return UCS2; |
32 |
|
|
} |
33 |
|
1 |
break; |
34 |
|
|
case 'l': |
35 |
|
|
// latin1 |
36 |
✓✓ |
278037 |
if (encoding[1] == 'a') { |
37 |
✓✗ |
278035 |
if (strncmp(encoding + 2, "tin1", 4) == 0) |
38 |
|
278035 |
return LATIN1; |
39 |
|
|
} |
40 |
|
2 |
break; |
41 |
|
|
case 'b': |
42 |
|
|
// binary |
43 |
✓✓ |
404689 |
if (encoding[1] == 'i') { |
44 |
✓✗ |
19 |
if (strncmp(encoding + 2, "nary", 4) == 0) |
45 |
|
19 |
return LATIN1; |
46 |
|
|
|
47 |
|
|
// buffer |
48 |
✓✓ |
404670 |
} else if (encoding[1] == 'u') { |
49 |
✓✗ |
404049 |
if (strncmp(encoding + 2, "ffer", 4) == 0) |
50 |
|
404049 |
return BUFFER; |
51 |
|
|
} |
52 |
|
621 |
break; |
53 |
|
|
case '\0': |
54 |
|
1 |
return default_encoding; |
55 |
|
|
default: |
56 |
|
519108 |
break; |
57 |
|
|
} |
58 |
|
|
|
59 |
✗✓ |
519732 |
if (StringEqualNoCase(encoding, "utf8")) { |
60 |
|
|
return UTF8; |
61 |
✓✓ |
519732 |
} else if (StringEqualNoCase(encoding, "utf-8")) { |
62 |
|
2 |
return UTF8; |
63 |
✓✓ |
519730 |
} else if (StringEqualNoCase(encoding, "ascii")) { |
64 |
|
114768 |
return ASCII; |
65 |
✓✓ |
404962 |
} else if (StringEqualNoCase(encoding, "base64")) { |
66 |
|
619 |
return BASE64; |
67 |
✗✓ |
404343 |
} else if (StringEqualNoCase(encoding, "ucs2")) { |
68 |
|
|
return UCS2; |
69 |
✗✓ |
404343 |
} else if (StringEqualNoCase(encoding, "ucs-2")) { |
70 |
|
|
return UCS2; |
71 |
✗✓ |
404343 |
} else if (StringEqualNoCase(encoding, "utf16le")) { |
72 |
|
|
return UCS2; |
73 |
✗✓ |
404343 |
} else if (StringEqualNoCase(encoding, "utf-16le")) { |
74 |
|
|
return UCS2; |
75 |
✗✓ |
404343 |
} else if (StringEqualNoCase(encoding, "latin1")) { |
76 |
|
|
return LATIN1; |
77 |
✗✓ |
404343 |
} else if (StringEqualNoCase(encoding, "binary")) { |
78 |
|
|
return LATIN1; // BINARY is a deprecated alias of LATIN1. |
79 |
✗✓ |
404343 |
} else if (StringEqualNoCase(encoding, "buffer")) { |
80 |
|
|
return BUFFER; |
81 |
✓✓ |
404343 |
} else if (StringEqualNoCase(encoding, "hex")) { |
82 |
|
404338 |
return HEX; |
83 |
|
|
} else { |
84 |
|
5 |
return default_encoding; |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
|
89 |
|
1779277 |
enum encoding ParseEncoding(Isolate* isolate, |
90 |
|
|
Local<Value> encoding_v, |
91 |
|
|
enum encoding default_encoding) { |
92 |
✗✓ |
1779277 |
CHECK(!encoding_v.IsEmpty()); |
93 |
|
|
|
94 |
✓✓ |
3558554 |
if (!encoding_v->IsString()) |
95 |
|
14676 |
return default_encoding; |
96 |
|
|
|
97 |
|
1764601 |
Utf8Value encoding(isolate, encoding_v); |
98 |
|
|
|
99 |
|
1764601 |
return ParseEncoding(*encoding, default_encoding); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
2 |
Local<Value> Encode(Isolate* isolate, |
103 |
|
|
const char* buf, |
104 |
|
|
size_t len, |
105 |
|
|
enum encoding encoding) { |
106 |
✗✓ |
2 |
CHECK_NE(encoding, UCS2); |
107 |
|
|
Local<Value> error; |
108 |
|
2 |
return StringBytes::Encode(isolate, buf, len, encoding, &error) |
109 |
|
4 |
.ToLocalChecked(); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) { |
113 |
|
|
Local<Value> error; |
114 |
|
|
return StringBytes::Encode(isolate, buf, len, &error) |
115 |
|
|
.ToLocalChecked(); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
// Returns -1 if the handle was not valid for decoding |
119 |
|
|
ssize_t DecodeBytes(Isolate* isolate, |
120 |
|
|
Local<Value> val, |
121 |
|
|
enum encoding encoding) { |
122 |
|
|
HandleScope scope(isolate); |
123 |
|
|
|
124 |
|
|
return StringBytes::Size(isolate, val, encoding).FromMaybe(-1); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
// Returns number of bytes written. |
128 |
|
|
ssize_t DecodeWrite(Isolate* isolate, |
129 |
|
|
char* buf, |
130 |
|
|
size_t buflen, |
131 |
|
|
Local<Value> val, |
132 |
|
|
enum encoding encoding) { |
133 |
|
|
return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
} // namespace node |