Coverage Report

Created: 2025-08-03 10:06

/src/node/src/string_bytes.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "string_bytes.h"
23
24
#include "base64-inl.h"
25
#include "env-inl.h"
26
#include "node_buffer.h"
27
#include "node_errors.h"
28
#include "simdutf.h"
29
#include "util.h"
30
31
#include <climits>
32
#include <cstring>  // memcpy
33
34
#include <algorithm>
35
36
// When creating strings >= this length v8's gc spins up and consumes
37
// most of the execution time. For these cases it's more performant to
38
// use external string resources.
39
961
#define EXTERN_APEX 0xFBEE9
40
41
namespace node {
42
43
using v8::HandleScope;
44
using v8::Isolate;
45
using v8::Just;
46
using v8::Local;
47
using v8::Maybe;
48
using v8::MaybeLocal;
49
using v8::Nothing;
50
using v8::String;
51
using v8::Value;
52
53
namespace {
54
55
template <typename ResourceType, typename TypeName>
56
class ExternString: public ResourceType {
57
 public:
58
0
  ~ExternString() override {
59
0
    free(const_cast<TypeName*>(data_));
60
0
    isolate()->AdjustAmountOfExternalAllocatedMemory(-byte_length());
61
0
  }
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalOneByteStringResource, char>::~ExternString()
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalStringResource, unsigned short>::~ExternString()
62
63
0
  const TypeName* data() const override {
64
0
    return data_;
65
0
  }
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalOneByteStringResource, char>::data() const
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalStringResource, unsigned short>::data() const
66
67
0
  size_t length() const override {
68
0
    return length_;
69
0
  }
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalOneByteStringResource, char>::length() const
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalStringResource, unsigned short>::length() const
70
71
0
  int64_t byte_length() const {
72
0
    return length() * sizeof(*data());
73
0
  }
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalOneByteStringResource, char>::byte_length() const
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalStringResource, unsigned short>::byte_length() const
74
75
  static MaybeLocal<Value> NewFromCopy(Isolate* isolate,
76
                                       const TypeName* data,
77
                                       size_t length,
78
5
                                       Local<Value>* error) {
79
5
    if (length == 0)
80
0
      return String::Empty(isolate);
81
82
5
    if (length < EXTERN_APEX)
83
5
      return NewSimpleFromCopy(isolate, data, length, error);
84
85
0
    TypeName* new_data = node::UncheckedMalloc<TypeName>(length);
86
0
    if (new_data == nullptr) {
87
0
      *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
88
0
      return MaybeLocal<Value>();
89
0
    }
90
0
    memcpy(new_data, data, length * sizeof(*new_data));
91
92
0
    return ExternString<ResourceType, TypeName>::New(isolate,
93
0
                                                     new_data,
94
0
                                                     length,
95
0
                                                     error);
96
0
  }
string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalOneByteStringResource, char>::NewFromCopy(v8::Isolate*, char const*, unsigned long, v8::Local<v8::Value>*)
Line
Count
Source
78
5
                                       Local<Value>* error) {
79
5
    if (length == 0)
80
0
      return String::Empty(isolate);
81
82
5
    if (length < EXTERN_APEX)
83
5
      return NewSimpleFromCopy(isolate, data, length, error);
84
85
0
    TypeName* new_data = node::UncheckedMalloc<TypeName>(length);
86
0
    if (new_data == nullptr) {
87
0
      *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
88
0
      return MaybeLocal<Value>();
89
0
    }
90
0
    memcpy(new_data, data, length * sizeof(*new_data));
91
92
0
    return ExternString<ResourceType, TypeName>::New(isolate,
93
0
                                                     new_data,
94
0
                                                     length,
95
0
                                                     error);
96
0
  }
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalStringResource, unsigned short>::NewFromCopy(v8::Isolate*, unsigned short const*, unsigned long, v8::Local<v8::Value>*)
97
98
  // uses "data" for external resource, and will be free'd on gc
99
  static MaybeLocal<Value> New(Isolate* isolate,
100
                               TypeName* data,
101
                               size_t length,
102
956
                               Local<Value>* error) {
103
956
    if (length == 0)
104
0
      return String::Empty(isolate);
105
106
956
    if (length < EXTERN_APEX) {
107
956
      MaybeLocal<Value> str = NewSimpleFromCopy(isolate, data, length, error);
108
956
      free(data);
109
956
      return str;
110
956
    }
111
112
0
    ExternString* h_str = new ExternString<ResourceType, TypeName>(isolate,
113
0
                                                                   data,
114
0
                                                                   length);
115
0
    MaybeLocal<Value> str = NewExternal(isolate, h_str);
116
0
    isolate->AdjustAmountOfExternalAllocatedMemory(h_str->byte_length());
117
118
0
    if (str.IsEmpty()) {
119
0
      delete h_str;
120
0
      *error = node::ERR_STRING_TOO_LONG(isolate);
121
0
      return MaybeLocal<Value>();
122
0
    }
123
124
0
    return str.ToLocalChecked();
125
0
  }
string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalOneByteStringResource, char>::New(v8::Isolate*, char*, unsigned long, v8::Local<v8::Value>*)
Line
Count
Source
102
956
                               Local<Value>* error) {
103
956
    if (length == 0)
104
0
      return String::Empty(isolate);
105
106
956
    if (length < EXTERN_APEX) {
107
956
      MaybeLocal<Value> str = NewSimpleFromCopy(isolate, data, length, error);
108
956
      free(data);
109
956
      return str;
110
956
    }
111
112
0
    ExternString* h_str = new ExternString<ResourceType, TypeName>(isolate,
113
0
                                                                   data,
114
0
                                                                   length);
115
0
    MaybeLocal<Value> str = NewExternal(isolate, h_str);
116
0
    isolate->AdjustAmountOfExternalAllocatedMemory(h_str->byte_length());
117
118
0
    if (str.IsEmpty()) {
119
0
      delete h_str;
120
0
      *error = node::ERR_STRING_TOO_LONG(isolate);
121
0
      return MaybeLocal<Value>();
122
0
    }
123
124
0
    return str.ToLocalChecked();
125
0
  }
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalStringResource, unsigned short>::New(v8::Isolate*, unsigned short*, unsigned long, v8::Local<v8::Value>*)
126
127
0
  inline Isolate* isolate() const { return isolate_; }
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalOneByteStringResource, char>::isolate() const
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalStringResource, unsigned short>::isolate() const
128
129
 private:
130
  ExternString(Isolate* isolate, const TypeName* data, size_t length)
131
0
    : isolate_(isolate), data_(data), length_(length) { }
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalOneByteStringResource, char>::ExternString(v8::Isolate*, char const*, unsigned long)
Unexecuted instantiation: string_bytes.cc:node::(anonymous namespace)::ExternString<v8::String::ExternalStringResource, unsigned short>::ExternString(v8::Isolate*, unsigned short const*, unsigned long)
132
  static MaybeLocal<Value> NewExternal(Isolate* isolate,
133
                                       ExternString* h_str);
134
135
  // This method does not actually create ExternString instances.
136
  static MaybeLocal<Value> NewSimpleFromCopy(Isolate* isolate,
137
                                             const TypeName* data,
138
                                             size_t length,
139
                                             Local<Value>* error);
140
141
  Isolate* isolate_;
142
  const TypeName* data_;
143
  size_t length_;
144
};
145
146
147
typedef ExternString<String::ExternalOneByteStringResource,
148
                     char> ExternOneByteString;
149
typedef ExternString<String::ExternalStringResource,
150
                     uint16_t> ExternTwoByteString;
151
152
153
template <>
154
MaybeLocal<Value> ExternOneByteString::NewExternal(
155
0
    Isolate* isolate, ExternOneByteString* h_str) {
156
0
  return String::NewExternalOneByte(isolate, h_str).FromMaybe(Local<Value>());
157
0
}
158
159
160
template <>
161
MaybeLocal<Value> ExternTwoByteString::NewExternal(
162
0
    Isolate* isolate, ExternTwoByteString* h_str) {
163
0
  return String::NewExternalTwoByte(isolate, h_str).FromMaybe(Local<Value>());
164
0
}
165
166
template <>
167
MaybeLocal<Value> ExternOneByteString::NewSimpleFromCopy(Isolate* isolate,
168
                                                         const char* data,
169
                                                         size_t length,
170
961
                                                         Local<Value>* error) {
171
961
  MaybeLocal<String> str =
172
961
      String::NewFromOneByte(isolate,
173
961
                             reinterpret_cast<const uint8_t*>(data),
174
961
                             v8::NewStringType::kNormal,
175
961
                             length);
176
961
  if (str.IsEmpty()) {
177
0
    *error = node::ERR_STRING_TOO_LONG(isolate);
178
0
    return MaybeLocal<Value>();
179
0
  }
180
961
  return str.ToLocalChecked();
181
961
}
182
183
184
template <>
185
MaybeLocal<Value> ExternTwoByteString::NewSimpleFromCopy(Isolate* isolate,
186
                                                         const uint16_t* data,
187
                                                         size_t length,
188
0
                                                         Local<Value>* error) {
189
0
  MaybeLocal<String> str =
190
0
      String::NewFromTwoByte(isolate,
191
0
                             data,
192
0
                             v8::NewStringType::kNormal,
193
0
                             length);
194
0
  if (str.IsEmpty()) {
195
0
    *error = node::ERR_STRING_TOO_LONG(isolate);
196
0
    return MaybeLocal<Value>();
197
0
  }
198
0
  return str.ToLocalChecked();
199
0
}
200
201
}  // anonymous namespace
202
203
// supports regular and URL-safe base64
204
const int8_t unbase64_table[256] =
205
  { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -2, -1, -1,
206
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
207
    -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
208
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
209
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
210
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
211
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
212
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
213
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
214
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
215
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
216
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
217
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
218
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
219
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
220
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
221
  };
222
223
224
static const int8_t unhex_table[256] =
225
  { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
226
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
227
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
228
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
229
    -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
230
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
231
    -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
232
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
233
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
234
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
235
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
236
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
237
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
238
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
239
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
240
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
241
  };
242
243
478
static inline unsigned unhex(uint8_t x) {
244
478
  return unhex_table[x];
245
478
}
246
247
template <typename TypeName>
248
static size_t hex_decode(char* buf,
249
                         size_t len,
250
                         const TypeName* src,
251
16
                         const size_t srcLen) {
252
16
  size_t i;
253
252
  for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) {
254
239
    unsigned a = unhex(static_cast<uint8_t>(src[i * 2 + 0]));
255
239
    unsigned b = unhex(static_cast<uint8_t>(src[i * 2 + 1]));
256
239
    if (!~a || !~b)
257
3
      return i;
258
236
    buf[i] = (a << 4) | b;
259
236
  }
260
261
13
  return i;
262
16
}
Unexecuted instantiation: string_bytes.cc:unsigned long node::hex_decode<char>(char*, unsigned long, char const*, unsigned long)
string_bytes.cc:unsigned long node::hex_decode<unsigned short>(char*, unsigned long, unsigned short const*, unsigned long)
Line
Count
Source
251
16
                         const size_t srcLen) {
252
16
  size_t i;
253
252
  for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) {
254
239
    unsigned a = unhex(static_cast<uint8_t>(src[i * 2 + 0]));
255
239
    unsigned b = unhex(static_cast<uint8_t>(src[i * 2 + 1]));
256
239
    if (!~a || !~b)
257
3
      return i;
258
236
    buf[i] = (a << 4) | b;
259
236
  }
260
261
13
  return i;
262
16
}
263
264
size_t StringBytes::WriteUCS2(
265
7
    Isolate* isolate, char* buf, size_t buflen, Local<String> str, int flags) {
266
7
  uint16_t* const dst = reinterpret_cast<uint16_t*>(buf);
267
268
7
  size_t max_chars = buflen / sizeof(*dst);
269
7
  if (max_chars == 0) {
270
0
    return 0;
271
0
  }
272
273
7
  uint16_t* const aligned_dst = AlignUp(dst, sizeof(*dst));
274
7
  size_t nchars;
275
7
  if (aligned_dst == dst) {
276
7
    nchars = str->Write(isolate, dst, 0, max_chars, flags);
277
7
    return nchars * sizeof(*dst);
278
7
  }
279
280
0
  CHECK_EQ(reinterpret_cast<uintptr_t>(aligned_dst) % sizeof(*dst), 0);
281
282
  // Write all but the last char
283
0
  max_chars = std::min(max_chars, static_cast<size_t>(str->Length()));
284
0
  if (max_chars == 0) {
285
0
    return 0;
286
0
  }
287
0
  nchars = str->Write(isolate, aligned_dst, 0, max_chars - 1, flags);
288
0
  CHECK_EQ(nchars, max_chars - 1);
289
290
  // Shift everything to unaligned-left
291
0
  memmove(dst, aligned_dst, nchars * sizeof(*dst));
292
293
  // One more char to be written
294
0
  uint16_t last;
295
0
  CHECK_EQ(str->Write(isolate, &last, nchars, 1, flags), 1);
296
0
  memcpy(buf + nchars * sizeof(*dst), &last, sizeof(last));
297
0
  nchars++;
298
299
0
  return nchars * sizeof(*dst);
300
0
}
301
302
size_t StringBytes::Write(Isolate* isolate,
303
                          char* buf,
304
                          size_t buflen,
305
                          Local<Value> val,
306
149k
                          enum encoding encoding) {
307
149k
  HandleScope scope(isolate);
308
149k
  size_t nbytes;
309
310
149k
  CHECK(val->IsString() == true);
311
149k
  Local<String> str = val.As<String>();
312
313
149k
  int flags = String::HINT_MANY_WRITES_EXPECTED |
314
149k
              String::NO_NULL_TERMINATION |
315
149k
              String::REPLACE_INVALID_UTF8;
316
317
149k
  switch (encoding) {
318
3.60k
    case ASCII:
319
3.60k
    case LATIN1:
320
3.60k
      if (str->IsExternalOneByte()) {
321
0
        auto ext = str->GetExternalOneByteStringResource();
322
0
        nbytes = std::min(buflen, ext->length());
323
0
        memcpy(buf, ext->data(), nbytes);
324
3.60k
      } else {
325
3.60k
        uint8_t* const dst = reinterpret_cast<uint8_t*>(buf);
326
3.60k
        nbytes = str->WriteOneByte(isolate, dst, 0, buflen, flags);
327
3.60k
      }
328
3.60k
      break;
329
330
0
    case BUFFER:
331
145k
    case UTF8:
332
145k
      nbytes = str->WriteUtf8(isolate, buf, buflen, nullptr, flags);
333
145k
      break;
334
335
7
    case UCS2: {
336
7
      nbytes = WriteUCS2(isolate, buf, buflen, str, flags);
337
338
      // Node's "ucs2" encoding wants LE character data stored in
339
      // the Buffer, so we need to reorder on BE platforms.  See
340
      // https://nodejs.org/api/buffer.html regarding Node's "ucs2"
341
      // encoding specification
342
7
      if (IsBigEndian())
343
0
        SwapBytes16(buf, nbytes);
344
345
7
      break;
346
0
    }
347
348
0
    case BASE64URL:
349
      // Fall through
350
2
    case BASE64:
351
2
      if (str->IsExternalOneByte()) {
352
0
        auto ext = str->GetExternalOneByteStringResource();
353
0
        nbytes = base64_decode(buf, buflen, ext->data(), ext->length());
354
2
      } else {
355
2
        String::Value value(isolate, str);
356
2
        nbytes = base64_decode(buf, buflen, *value, value.length());
357
2
      }
358
2
      break;
359
360
16
    case HEX:
361
16
      if (str->IsExternalOneByte()) {
362
0
        auto ext = str->GetExternalOneByteStringResource();
363
0
        nbytes = hex_decode(buf, buflen, ext->data(), ext->length());
364
16
      } else {
365
16
        String::Value value(isolate, str);
366
16
        nbytes = hex_decode(buf, buflen, *value, value.length());
367
16
      }
368
16
      break;
369
370
0
    default:
371
0
      UNREACHABLE("unknown encoding");
372
149k
  }
373
374
149k
  return nbytes;
375
149k
}
376
377
// Quick and dirty size calculation
378
// Will always be at least big enough, but may have some extra
379
// UTF8 can be as much as 3x the size, Base64 can have 1-2 extra bytes
380
Maybe<size_t> StringBytes::StorageSize(Isolate* isolate,
381
                                       Local<Value> val,
382
15.9M
                                       enum encoding encoding) {
383
15.9M
  HandleScope scope(isolate);
384
15.9M
  size_t data_size = 0;
385
15.9M
  bool is_buffer = Buffer::HasInstance(val);
386
387
15.9M
  if (is_buffer && (encoding == BUFFER || encoding == LATIN1)) {
388
0
    return Just(Buffer::Length(val));
389
0
  }
390
391
15.9M
  Local<String> str;
392
15.9M
  if (!val->ToString(isolate->GetCurrentContext()).ToLocal(&str))
393
0
    return Nothing<size_t>();
394
395
15.9M
  switch (encoding) {
396
0
    case ASCII:
397
0
    case LATIN1:
398
0
      data_size = str->Length();
399
0
      break;
400
401
0
    case BUFFER:
402
15.9M
    case UTF8:
403
      // A single UCS2 codepoint never takes up more than 3 utf8 bytes.
404
      // It is an exercise for the caller to decide when a string is
405
      // long enough to justify calling Size() instead of StorageSize()
406
15.9M
      data_size = 3 * str->Length();
407
15.9M
      break;
408
409
0
    case UCS2:
410
0
      data_size = str->Length() * sizeof(uint16_t);
411
0
      break;
412
413
0
    case BASE64URL:
414
      // Fall through
415
0
    case BASE64:
416
0
      data_size = base64_decoded_size_fast(str->Length());
417
0
      break;
418
419
3
    case HEX:
420
3
      CHECK(str->Length() % 2 == 0 && "invalid hex string length");
421
3
      data_size = str->Length() / 2;
422
3
      break;
423
424
0
    default:
425
0
      UNREACHABLE("unknown encoding");
426
15.9M
  }
427
428
15.9M
  return Just(data_size);
429
15.9M
}
430
431
Maybe<size_t> StringBytes::Size(Isolate* isolate,
432
                                Local<Value> val,
433
4.76k
                                enum encoding encoding) {
434
4.76k
  HandleScope scope(isolate);
435
436
4.76k
  if (Buffer::HasInstance(val) && (encoding == BUFFER || encoding == LATIN1))
437
0
    return Just(Buffer::Length(val));
438
439
4.76k
  Local<String> str;
440
4.76k
  if (!val->ToString(isolate->GetCurrentContext()).ToLocal(&str))
441
0
    return Nothing<size_t>();
442
443
4.76k
  switch (encoding) {
444
0
    case ASCII:
445
0
    case LATIN1:
446
0
      return Just<size_t>(str->Length());
447
448
0
    case BUFFER:
449
4.76k
    case UTF8:
450
4.76k
      return Just<size_t>(str->Utf8Length(isolate));
451
452
4
    case UCS2:
453
4
      return Just(str->Length() * sizeof(uint16_t));
454
455
0
    case BASE64URL:
456
      // Fall through
457
0
    case BASE64: {
458
0
      String::Value value(isolate, str);
459
0
      return Just(base64_decoded_size(*value, value.length()));
460
0
    }
461
462
0
    case HEX:
463
0
      return Just<size_t>(str->Length() / 2);
464
4.76k
  }
465
466
0
  UNREACHABLE();
467
0
}
468
469
0
static void force_ascii_slow(const char* src, char* dst, size_t len) {
470
0
  for (size_t i = 0; i < len; ++i) {
471
0
    dst[i] = src[i] & 0x7f;
472
0
  }
473
0
}
474
475
476
0
static void force_ascii(const char* src, char* dst, size_t len) {
477
0
  if (len < 16) {
478
0
    force_ascii_slow(src, dst, len);
479
0
    return;
480
0
  }
481
482
0
  const unsigned bytes_per_word = sizeof(uintptr_t);
483
0
  const unsigned align_mask = bytes_per_word - 1;
484
0
  const unsigned src_unalign = reinterpret_cast<uintptr_t>(src) & align_mask;
485
0
  const unsigned dst_unalign = reinterpret_cast<uintptr_t>(dst) & align_mask;
486
487
0
  if (src_unalign > 0) {
488
0
    if (src_unalign == dst_unalign) {
489
0
      const unsigned unalign = bytes_per_word - src_unalign;
490
0
      force_ascii_slow(src, dst, unalign);
491
0
      src += unalign;
492
0
      dst += unalign;
493
0
      len -= src_unalign;
494
0
    } else {
495
0
      force_ascii_slow(src, dst, len);
496
0
      return;
497
0
    }
498
0
  }
499
500
0
#if defined(_WIN64) || defined(_LP64)
501
0
  const uintptr_t mask = ~0x8080808080808080ll;
502
#else
503
  const uintptr_t mask = ~0x80808080l;
504
#endif
505
506
0
  const uintptr_t* srcw = reinterpret_cast<const uintptr_t*>(src);
507
0
  uintptr_t* dstw = reinterpret_cast<uintptr_t*>(dst);
508
509
0
  for (size_t i = 0, n = len / bytes_per_word; i < n; ++i) {
510
0
    dstw[i] = srcw[i] & mask;
511
0
  }
512
513
0
  const unsigned remainder = len & align_mask;
514
0
  if (remainder > 0) {
515
0
    const size_t offset = len - remainder;
516
0
    force_ascii_slow(src + offset, dst + offset, remainder);
517
0
  }
518
0
}
519
520
521
size_t StringBytes::hex_encode(
522
    const char* src,
523
    size_t slen,
524
    char* dst,
525
955
    size_t dlen) {
526
  // We know how much we'll write, just make sure that there's space.
527
955
  CHECK(dlen >= slen * 2 &&
528
955
      "not enough space provided for hex encode");
529
530
955
  dlen = slen * 2;
531
20.9k
  for (uint32_t i = 0, k = 0; k < dlen; i += 1, k += 2) {
532
20.0k
    static const char hex[] = "0123456789abcdef";
533
20.0k
    uint8_t val = static_cast<uint8_t>(src[i]);
534
20.0k
    dst[k + 0] = hex[val >> 4];
535
20.0k
    dst[k + 1] = hex[val & 15];
536
20.0k
  }
537
538
955
  return dlen;
539
955
}
540
541
0
std::string StringBytes::hex_encode(const char* src, size_t slen) {
542
0
  size_t dlen = slen * 2;
543
0
  std::string dst(dlen, '\0');
544
0
  hex_encode(src, slen, dst.data(), dlen);
545
0
  return dst;
546
0
}
547
548
#define CHECK_BUFLEN_IN_RANGE(len)                                    \
549
1.88k
  do {                                                                \
550
1.88k
    if ((len) > Buffer::kMaxLength) {                                 \
551
0
      *error = node::ERR_BUFFER_TOO_LARGE(isolate);                   \
552
0
      return MaybeLocal<Value>();                                     \
553
0
    }                                                                 \
554
1.88k
  } while (0)
555
556
557
MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
558
                                      const char* buf,
559
                                      size_t buflen,
560
                                      enum encoding encoding,
561
1.88k
                                      Local<Value>* error) {
562
1.88k
  CHECK_BUFLEN_IN_RANGE(buflen);
563
564
1.88k
  if (!buflen && encoding != BUFFER) {
565
0
    return String::Empty(isolate);
566
0
  }
567
568
1.88k
  MaybeLocal<String> val;
569
570
1.88k
  switch (encoding) {
571
3
    case BUFFER:
572
3
      {
573
3
        auto maybe_buf = Buffer::Copy(isolate, buf, buflen);
574
3
        Local<v8::Object> buf;
575
3
        if (!maybe_buf.ToLocal(&buf)) {
576
0
          *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
577
0
        }
578
3
        return buf;
579
0
      }
580
581
4
    case ASCII:
582
4
      if (simdutf::validate_ascii_with_errors(buf, buflen).error) {
583
        // The input contains non-ASCII bytes.
584
0
        char* out = node::UncheckedMalloc(buflen);
585
0
        if (out == nullptr) {
586
0
          *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
587
0
          return MaybeLocal<Value>();
588
0
        }
589
0
        force_ascii(buf, out, buflen);
590
0
        return ExternOneByteString::New(isolate, out, buflen, error);
591
4
      } else {
592
4
        return ExternOneByteString::NewFromCopy(isolate, buf, buflen, error);
593
4
      }
594
595
916
    case UTF8:
596
916
      {
597
916
        val = String::NewFromUtf8(isolate,
598
916
                                  buf,
599
916
                                  v8::NewStringType::kNormal,
600
916
                                  buflen);
601
916
        Local<String> str;
602
916
        if (!val.ToLocal(&str)) {
603
0
          *error = node::ERR_STRING_TOO_LONG(isolate);
604
0
        }
605
916
        return str;
606
4
      }
607
608
1
    case LATIN1:
609
1
      return ExternOneByteString::NewFromCopy(isolate, buf, buflen, error);
610
611
1
    case BASE64: {
612
1
      size_t dlen = base64_encoded_size(buflen);
613
1
      char* dst = node::UncheckedMalloc(dlen);
614
1
      if (dst == nullptr) {
615
0
        *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
616
0
        return MaybeLocal<Value>();
617
0
      }
618
619
1
      size_t written = base64_encode(buf, buflen, dst, dlen);
620
1
      CHECK_EQ(written, dlen);
621
622
1
      return ExternOneByteString::New(isolate, dst, dlen, error);
623
1
    }
624
625
0
    case BASE64URL: {
626
0
      size_t dlen = base64_encoded_size(buflen, Base64Mode::URL);
627
0
      char* dst = node::UncheckedMalloc(dlen);
628
0
      if (dst == nullptr) {
629
0
        *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
630
0
        return MaybeLocal<Value>();
631
0
      }
632
633
0
      size_t written = base64_encode(buf, buflen, dst, dlen, Base64Mode::URL);
634
0
      CHECK_EQ(written, dlen);
635
636
0
      return ExternOneByteString::New(isolate, dst, dlen, error);
637
0
    }
638
639
955
    case HEX: {
640
955
      size_t dlen = buflen * 2;
641
955
      char* dst = node::UncheckedMalloc(dlen);
642
955
      if (dst == nullptr) {
643
0
        *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
644
0
        return MaybeLocal<Value>();
645
0
      }
646
955
      size_t written = hex_encode(buf, buflen, dst, dlen);
647
955
      CHECK_EQ(written, dlen);
648
649
955
      return ExternOneByteString::New(isolate, dst, dlen, error);
650
955
    }
651
652
0
    case UCS2: {
653
0
      size_t str_len = buflen / 2;
654
0
      if (IsBigEndian()) {
655
0
        uint16_t* dst = node::UncheckedMalloc<uint16_t>(str_len);
656
0
        if (str_len != 0 && dst == nullptr) {
657
0
          *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
658
0
          return MaybeLocal<Value>();
659
0
        }
660
0
        for (size_t i = 0, k = 0; k < str_len; i += 2, k += 1) {
661
          // The input is in *little endian*, because that's what Node.js
662
          // expects, so the high byte comes after the low byte.
663
0
          const uint8_t hi = static_cast<uint8_t>(buf[i + 1]);
664
0
          const uint8_t lo = static_cast<uint8_t>(buf[i + 0]);
665
0
          dst[k] = static_cast<uint16_t>(hi) << 8 | lo;
666
0
        }
667
0
        return ExternTwoByteString::New(isolate, dst, str_len, error);
668
0
      }
669
0
      if (reinterpret_cast<uintptr_t>(buf) % 2 != 0) {
670
        // Unaligned data still means we can't directly pass it to V8.
671
0
        char* dst = node::UncheckedMalloc(buflen);
672
0
        if (dst == nullptr) {
673
0
          *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
674
0
          return MaybeLocal<Value>();
675
0
        }
676
0
        memcpy(dst, buf, buflen);
677
0
        return ExternTwoByteString::New(
678
0
            isolate, reinterpret_cast<uint16_t*>(dst), str_len, error);
679
0
      }
680
0
      return ExternTwoByteString::NewFromCopy(
681
0
          isolate, reinterpret_cast<const uint16_t*>(buf), str_len, error);
682
0
    }
683
684
0
    default:
685
0
      UNREACHABLE("unknown encoding");
686
1.88k
  }
687
1.88k
}
688
689
690
MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
691
                                      const uint16_t* buf,
692
                                      size_t buflen,
693
0
                                      Local<Value>* error) {
694
0
  if (buflen == 0) return String::Empty(isolate);
695
0
  CHECK_BUFLEN_IN_RANGE(buflen);
696
697
  // Node's "ucs2" encoding expects LE character data inside a
698
  // Buffer, so we need to reorder on BE platforms.  See
699
  // https://nodejs.org/api/buffer.html regarding Node's "ucs2"
700
  // encoding specification
701
0
  if (IsBigEndian()) {
702
0
    uint16_t* dst = node::UncheckedMalloc<uint16_t>(buflen);
703
0
    if (dst == nullptr) {
704
0
      *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
705
0
      return MaybeLocal<Value>();
706
0
    }
707
0
    size_t nbytes = buflen * sizeof(uint16_t);
708
0
    memcpy(dst, buf, nbytes);
709
0
    SwapBytes16(reinterpret_cast<char*>(dst), nbytes);
710
0
    return ExternTwoByteString::New(isolate, dst, buflen, error);
711
0
  } else {
712
0
    return ExternTwoByteString::NewFromCopy(isolate, buf, buflen, error);
713
0
  }
714
0
}
715
716
MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
717
                                      const char* buf,
718
                                      enum encoding encoding,
719
0
                                      Local<Value>* error) {
720
0
  const size_t len = strlen(buf);
721
0
  return Encode(isolate, buf, len, encoding, error);
722
0
}
723
724
}  // namespace node