GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/string_bytes.cc Lines: 251 311 80.7 %
Date: 2020-05-27 22:15:15 Branches: 131 194 67.5 %

Line Branch Exec Source
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.h"
25
#include "env-inl.h"
26
#include "node_buffer.h"
27
#include "node_errors.h"
28
#include "util.h"
29
30
#include <climits>
31
#include <cstring>  // memcpy
32
33
#include <algorithm>
34
#include <vector>
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
#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
64
  ~ExternString() override {
59
32
    free(const_cast<TypeName*>(data_));
60
32
    isolate()->AdjustAmountOfExternalAllocatedMemory(-byte_length());
61
96
  }
62
63
158
  const TypeName* data() const override {
64
158
    return data_;
65
  }
66
67
273
  size_t length() const override {
68
273
    return length_;
69
  }
70
71
64
  int64_t byte_length() const {
72
64
    return length() * sizeof(*data());
73
  }
74
75
16375
  static MaybeLocal<Value> NewFromCopy(Isolate* isolate,
76
                                       const TypeName* data,
77
                                       size_t length,
78
                                       Local<Value>* error) {
79

16375
    if (length == 0)
80
2
      return String::Empty(isolate);
81
82

16373
    if (length < EXTERN_APEX)
83
16359
      return NewSimpleFromCopy(isolate, data, length, error);
84
85
14
    TypeName* new_data = node::UncheckedMalloc<TypeName>(length);
86

14
    if (new_data == nullptr) {
87
      *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
88
      return MaybeLocal<Value>();
89
    }
90
14
    memcpy(new_data, data, length * sizeof(*new_data));
91
92
    return ExternString<ResourceType, TypeName>::New(isolate,
93
                                                     new_data,
94
                                                     length,
95
14
                                                     error);
96
  }
97
98
  // uses "data" for external resource, and will be free'd on gc
99
672531
  static MaybeLocal<Value> New(Isolate* isolate,
100
                               TypeName* data,
101
                               size_t length,
102
                               Local<Value>* error) {
103

672531
    if (length == 0)
104
      return String::Empty(isolate);
105
106

672531
    if (length < EXTERN_APEX) {
107
672499
      MaybeLocal<Value> str = NewSimpleFromCopy(isolate, data, length, error);
108
672499
      free(data);
109
672499
      return str;
110
    }
111
112
32
    ExternString* h_str = new ExternString<ResourceType, TypeName>(isolate,
113
                                                                   data,
114
32
                                                                   length);
115
32
    MaybeLocal<Value> str = NewExternal(isolate, h_str);
116
32
    isolate->AdjustAmountOfExternalAllocatedMemory(h_str->byte_length());
117
118

32
    if (str.IsEmpty()) {
119

5
      delete h_str;
120
5
      *error = node::ERR_STRING_TOO_LONG(isolate);
121
5
      return MaybeLocal<Value>();
122
    }
123
124
27
    return str.ToLocalChecked();
125
  }
126
127
32
  inline Isolate* isolate() const { return isolate_; }
128
129
 private:
130
32
  ExternString(Isolate* isolate, const TypeName* data, size_t length)
131
32
    : isolate_(isolate), data_(data), length_(length) { }
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
27
MaybeLocal<Value> ExternOneByteString::NewExternal(
155
    Isolate* isolate, ExternOneByteString* h_str) {
156
54
  return String::NewExternalOneByte(isolate, h_str).FromMaybe(Local<Value>());
157
}
158
159
160
template <>
161
5
MaybeLocal<Value> ExternTwoByteString::NewExternal(
162
    Isolate* isolate, ExternTwoByteString* h_str) {
163
10
  return String::NewExternalTwoByte(isolate, h_str).FromMaybe(Local<Value>());
164
}
165
166
template <>
167
677818
MaybeLocal<Value> ExternOneByteString::NewSimpleFromCopy(Isolate* isolate,
168
                                                         const char* data,
169
                                                         size_t length,
170
                                                         Local<Value>* error) {
171
  MaybeLocal<String> str =
172
      String::NewFromOneByte(isolate,
173
                             reinterpret_cast<const uint8_t*>(data),
174
                             v8::NewStringType::kNormal,
175
677818
                             length);
176
677818
  if (str.IsEmpty()) {
177
    *error = node::ERR_STRING_TOO_LONG(isolate);
178
    return MaybeLocal<Value>();
179
  }
180
677818
  return str.ToLocalChecked();
181
}
182
183
184
template <>
185
11040
MaybeLocal<Value> ExternTwoByteString::NewSimpleFromCopy(Isolate* isolate,
186
                                                         const uint16_t* data,
187
                                                         size_t length,
188
                                                         Local<Value>* error) {
189
  MaybeLocal<String> str =
190
      String::NewFromTwoByte(isolate,
191
                             data,
192
                             v8::NewStringType::kNormal,
193
11040
                             length);
194
11040
  if (str.IsEmpty()) {
195
    *error = node::ERR_STRING_TOO_LONG(isolate);
196
    return MaybeLocal<Value>();
197
  }
198
11040
  return str.ToLocalChecked();
199
}
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
183244772
static inline unsigned unhex(uint8_t x) {
244
183244772
  return unhex_table[x];
245
}
246
247
template <typename TypeName>
248
2419
static size_t hex_decode(char* buf,
249
                         size_t len,
250
                         const TypeName* src,
251
                         const size_t srcLen) {
252
  size_t i;
253


91624796
  for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) {
254
91622386
    unsigned a = unhex(src[i * 2 + 0]);
255
91622386
    unsigned b = unhex(src[i * 2 + 1]);
256


91622386
    if (!~a || !~b)
257
9
      return i;
258
91622377
    buf[i] = (a << 4) | b;
259
  }
260
261
2410
  return i;
262
}
263
264
174
size_t StringBytes::WriteUCS2(Isolate* isolate,
265
                              char* buf,
266
                              size_t buflen,
267
                              Local<String> str,
268
                              int flags,
269
                              size_t* chars_written) {
270
174
  uint16_t* const dst = reinterpret_cast<uint16_t*>(buf);
271
272
174
  size_t max_chars = buflen / sizeof(*dst);
273
174
  if (max_chars == 0) {
274
5
    return 0;
275
  }
276
277
  size_t nchars;
278
169
  size_t alignment = reinterpret_cast<uintptr_t>(dst) % sizeof(*dst);
279
169
  if (alignment == 0) {
280
328
    nchars = str->Write(isolate, dst, 0, max_chars, flags);
281
164
    *chars_written = nchars;
282
164
    return nchars * sizeof(*dst);
283
  }
284
285
  uint16_t* aligned_dst =
286
5
      reinterpret_cast<uint16_t*>(buf + sizeof(*dst) - alignment);
287
5
  CHECK_EQ(reinterpret_cast<uintptr_t>(aligned_dst) % sizeof(*dst), 0);
288
289
  // Write all but the last char
290
5
  max_chars = std::min(max_chars, static_cast<size_t>(str->Length()));
291
5
  if (max_chars == 0) return 0;
292
10
  nchars = str->Write(isolate, aligned_dst, 0, max_chars - 1, flags);
293
5
  CHECK_EQ(nchars, max_chars - 1);
294
295
  // Shift everything to unaligned-left
296
5
  memmove(dst, aligned_dst, nchars * sizeof(*dst));
297
298
  // One more char to be written
299
  uint16_t last;
300
10
  CHECK_EQ(str->Write(isolate, &last, nchars, 1, flags), 1);
301
5
  memcpy(buf + nchars * sizeof(*dst), &last, sizeof(last));
302
5
  nchars++;
303
304
5
  *chars_written = nchars;
305
5
  return nchars * sizeof(*dst);
306
}
307
308
309
1162698
size_t StringBytes::Write(Isolate* isolate,
310
                          char* buf,
311
                          size_t buflen,
312
                          Local<Value> val,
313
                          enum encoding encoding,
314
                          int* chars_written) {
315
2325396
  HandleScope scope(isolate);
316
  size_t nbytes;
317
  int nchars;
318
319
1162698
  if (chars_written == nullptr)
320
1162698
    chars_written = &nchars;
321
322
2325396
  CHECK(val->IsString() == true);
323
1162698
  Local<String> str = val.As<String>();
324
325
  int flags = String::HINT_MANY_WRITES_EXPECTED |
326
              String::NO_NULL_TERMINATION |
327
1162698
              String::REPLACE_INVALID_UTF8;
328
329

1162698
  switch (encoding) {
330
    case ASCII:
331
    case LATIN1:
332
35549
      if (str->IsExternalOneByte()) {
333
1
        auto ext = str->GetExternalOneByteStringResource();
334
1
        nbytes = std::min(buflen, ext->length());
335
1
        memcpy(buf, ext->data(), nbytes);
336
      } else {
337
35548
        uint8_t* const dst = reinterpret_cast<uint8_t*>(buf);
338
71096
        nbytes = str->WriteOneByte(isolate, dst, 0, buflen, flags);
339
      }
340
35549
      *chars_written = nbytes;
341
35549
      break;
342
343
    case BUFFER:
344
    case UTF8:
345
1584164
      nbytes = str->WriteUtf8(isolate, buf, buflen, chars_written, flags);
346
792082
      break;
347
348
    case UCS2: {
349
      size_t nchars;
350
351
174
      nbytes = WriteUCS2(isolate, buf, buflen, str, flags, &nchars);
352
174
      *chars_written = static_cast<int>(nchars);
353
354
      // Node's "ucs2" encoding wants LE character data stored in
355
      // the Buffer, so we need to reorder on BE platforms.  See
356
      // https://nodejs.org/api/buffer.html regarding Node's "ucs2"
357
      // encoding specification
358
174
      if (IsBigEndian())
359
        SwapBytes16(buf, nbytes);
360
361
174
      break;
362
    }
363
364
    case BASE64:
365
332474
      if (str->IsExternalOneByte()) {
366
4
        auto ext = str->GetExternalOneByteStringResource();
367
4
        nbytes = base64_decode(buf, buflen, ext->data(), ext->length());
368
      } else {
369
664940
        String::Value value(isolate, str);
370
332470
        nbytes = base64_decode(buf, buflen, *value, value.length());
371
      }
372
332474
      *chars_written = nbytes;
373
332474
      break;
374
375
    case HEX:
376
2419
      if (str->IsExternalOneByte()) {
377
91
        auto ext = str->GetExternalOneByteStringResource();
378
91
        nbytes = hex_decode(buf, buflen, ext->data(), ext->length());
379
      } else {
380
4656
        String::Value value(isolate, str);
381
2328
        nbytes = hex_decode(buf, buflen, *value, value.length());
382
      }
383
2419
      *chars_written = nbytes;
384
2419
      break;
385
386
    default:
387
      CHECK(0 && "unknown encoding");
388
      break;
389
  }
390
391
2325396
  return nbytes;
392
}
393
394
395
// Quick and dirty size calculation
396
// Will always be at least big enough, but may have some extra
397
// UTF8 can be as much as 3x the size, Base64 can have 1-2 extra bytes
398
4374235
Maybe<size_t> StringBytes::StorageSize(Isolate* isolate,
399
                                       Local<Value> val,
400
                                       enum encoding encoding) {
401
8748474
  HandleScope scope(isolate);
402
4374247
  size_t data_size = 0;
403
4374247
  bool is_buffer = Buffer::HasInstance(val);
404
405

4374239
  if (is_buffer && (encoding == BUFFER || encoding == LATIN1)) {
406
    return Just(Buffer::Length(val));
407
  }
408
409
  Local<String> str;
410
13122710
  if (!val->ToString(isolate->GetCurrentContext()).ToLocal(&str))
411
    return Nothing<size_t>();
412
413

4374238
  switch (encoding) {
414
    case ASCII:
415
    case LATIN1:
416
33737
      data_size = str->Length();
417
33737
      break;
418
419
    case BUFFER:
420
    case UTF8:
421
      // A single UCS2 codepoint never takes up more than 3 utf8 bytes.
422
      // It is an exercise for the caller to decide when a string is
423
      // long enough to justify calling Size() instead of StorageSize()
424
4339332
      data_size = 3 * str->Length();
425
4339330
      break;
426
427
    case UCS2:
428
6
      data_size = str->Length() * sizeof(uint16_t);
429
6
      break;
430
431
    case BASE64:
432
10
      data_size = base64_decoded_size_fast(str->Length());
433
10
      break;
434
435
    case HEX:
436

1153
      CHECK(str->Length() % 2 == 0 && "invalid hex string length");
437
1153
      data_size = str->Length() / 2;
438
1153
      break;
439
440
    default:
441
      CHECK(0 && "unknown encoding");
442
      break;
443
  }
444
445
4374236
  return Just(data_size);
446
}
447
448
1114
Maybe<size_t> StringBytes::Size(Isolate* isolate,
449
                                Local<Value> val,
450
                                enum encoding encoding) {
451
2228
  HandleScope scope(isolate);
452
453


1114
  if (Buffer::HasInstance(val) && (encoding == BUFFER || encoding == LATIN1))
454
    return Just(Buffer::Length(val));
455
456
  Local<String> str;
457
3342
  if (!val->ToString(isolate->GetCurrentContext()).ToLocal(&str))
458
    return Nothing<size_t>();
459
460

1114
  switch (encoding) {
461
    case ASCII:
462
    case LATIN1:
463
17
      return Just<size_t>(str->Length());
464
465
    case BUFFER:
466
    case UTF8:
467
902
      return Just<size_t>(str->Utf8Length(isolate));
468
469
    case UCS2:
470
97
      return Just(str->Length() * sizeof(uint16_t));
471
472
    case BASE64: {
473
10
      String::Value value(isolate, str);
474
5
      return Just(base64_decoded_size(*value, value.length()));
475
    }
476
477
    case HEX:
478
93
      return Just<size_t>(str->Length() / 2);
479
  }
480
481
  UNREACHABLE();
482
}
483
484
485
486
487
4464
static bool contains_non_ascii_slow(const char* buf, size_t len) {
488
21851
  for (size_t i = 0; i < len; ++i) {
489
17569
    if (buf[i] & 0x80)
490
182
      return true;
491
  }
492
4282
  return false;
493
}
494
495
496
4943
static bool contains_non_ascii(const char* src, size_t len) {
497
4943
  if (len < 16) {
498
4357
    return contains_non_ascii_slow(src, len);
499
  }
500
501
586
  const unsigned bytes_per_word = sizeof(uintptr_t);
502
586
  const unsigned align_mask = bytes_per_word - 1;
503
586
  const unsigned unaligned = reinterpret_cast<uintptr_t>(src) & align_mask;
504
505
586
  if (unaligned > 0) {
506
51
    const unsigned n = bytes_per_word - unaligned;
507
51
    if (contains_non_ascii_slow(src, n))
508
17
      return true;
509
34
    src += n;
510
34
    len -= n;
511
  }
512
513
514
#if defined(_WIN64) || defined(_LP64)
515
569
  const uintptr_t mask = 0x8080808080808080ll;
516
#else
517
  const uintptr_t mask = 0x80808080l;
518
#endif
519
520
569
  const uintptr_t* srcw = reinterpret_cast<const uintptr_t*>(src);
521
522
67112115
  for (size_t i = 0, n = len / bytes_per_word; i < n; ++i) {
523
67112058
    if (srcw[i] & mask)
524
512
      return true;
525
  }
526
527
57
  const unsigned remainder = len & align_mask;
528
57
  if (remainder > 0) {
529
56
    const size_t offset = len - remainder;
530
56
    if (contains_non_ascii_slow(src + offset, remainder))
531
      return true;
532
  }
533
534
57
  return false;
535
}
536
537
538
631
static void force_ascii_slow(const char* src, char* dst, size_t len) {
539
5959
  for (size_t i = 0; i < len; ++i) {
540
5328
    dst[i] = src[i] & 0x7f;
541
  }
542
631
}
543
544
545
694
static void force_ascii(const char* src, char* dst, size_t len) {
546
694
  if (len < 16) {
547
165
    force_ascii_slow(src, dst, len);
548
165
    return;
549
  }
550
551
529
  const unsigned bytes_per_word = sizeof(uintptr_t);
552
529
  const unsigned align_mask = bytes_per_word - 1;
553
529
  const unsigned src_unalign = reinterpret_cast<uintptr_t>(src) & align_mask;
554
529
  const unsigned dst_unalign = reinterpret_cast<uintptr_t>(dst) & align_mask;
555
556
529
  if (src_unalign > 0) {
557
50
    if (src_unalign == dst_unalign) {
558
      const unsigned unalign = bytes_per_word - src_unalign;
559
      force_ascii_slow(src, dst, unalign);
560
      src += unalign;
561
      dst += unalign;
562
      len -= src_unalign;
563
    } else {
564
50
      force_ascii_slow(src, dst, len);
565
50
      return;
566
    }
567
  }
568
569
#if defined(_WIN64) || defined(_LP64)
570
479
  const uintptr_t mask = ~0x8080808080808080ll;
571
#else
572
  const uintptr_t mask = ~0x80808080l;
573
#endif
574
575
479
  const uintptr_t* srcw = reinterpret_cast<const uintptr_t*>(src);
576
479
  uintptr_t* dstw = reinterpret_cast<uintptr_t*>(dst);
577
578
3482
  for (size_t i = 0, n = len / bytes_per_word; i < n; ++i) {
579
3003
    dstw[i] = srcw[i] & mask;
580
  }
581
582
479
  const unsigned remainder = len & align_mask;
583
479
  if (remainder > 0) {
584
416
    const size_t offset = len - remainder;
585
416
    force_ascii_slow(src + offset, dst + offset, remainder);
586
  }
587
}
588
589
590
670024
size_t StringBytes::hex_encode(
591
    const char* src,
592
    size_t slen,
593
    char* dst,
594
    size_t dlen) {
595
  // We know how much we'll write, just make sure that there's space.
596
670024
  CHECK(dlen >= slen * 2 &&
597
      "not enough space provided for hex encode");
598
599
670024
  dlen = slen * 2;
600
571826487
  for (uint32_t i = 0, k = 0; k < dlen; i += 1, k += 2) {
601
    static const char hex[] = "0123456789abcdef";
602
571156463
    uint8_t val = static_cast<uint8_t>(src[i]);
603
571156463
    dst[k + 0] = hex[val >> 4];
604
571156463
    dst[k + 1] = hex[val & 15];
605
  }
606
607
670024
  return dlen;
608
}
609
610
std::string StringBytes::hex_encode(const char* src, size_t slen) {
611
  size_t dlen = slen * 2;
612
  std::string dst(dlen, '\0');
613
  hex_encode(src, slen, &dst[0], dlen);
614
  return dst;
615
}
616
617
#define CHECK_BUFLEN_IN_RANGE(len)                                    \
618
  do {                                                                \
619
    if ((len) > Buffer::kMaxLength) {                                 \
620
      *error = node::ERR_BUFFER_TOO_LARGE(isolate);                   \
621
      return MaybeLocal<Value>();                                     \
622
    }                                                                 \
623
  } while (0)
624
625
626
1448263
MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
627
                                      const char* buf,
628
                                      size_t buflen,
629
                                      enum encoding encoding,
630
                                      Local<Value>* error) {
631
1448263
  CHECK_BUFLEN_IN_RANGE(buflen);
632
633

1448263
  if (!buflen && encoding != BUFFER) {
634
170
    return String::Empty(isolate);
635
  }
636
637
  MaybeLocal<String> val;
638
639


1448093
  switch (encoding) {
640
    case BUFFER:
641
      {
642
1550
        if (buflen > node::Buffer::kMaxLength) {
643
          *error = node::ERR_BUFFER_TOO_LARGE(isolate);
644
          return MaybeLocal<Value>();
645
        }
646
1550
        auto maybe_buf = Buffer::Copy(isolate, buf, buflen);
647
1550
        if (maybe_buf.IsEmpty()) {
648
          *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
649
          return MaybeLocal<Value>();
650
        }
651
1550
        return maybe_buf.ToLocalChecked();
652
      }
653
654
    case ASCII:
655
4943
      if (contains_non_ascii(buf, buflen)) {
656
694
        char* out = node::UncheckedMalloc(buflen);
657
694
        if (out == nullptr) {
658
          *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
659
          return MaybeLocal<Value>();
660
        }
661
694
        force_ascii(buf, out, buflen);
662
694
        return ExternOneByteString::New(isolate, out, buflen, error);
663
      } else {
664
4249
        return ExternOneByteString::NewFromCopy(isolate, buf, buflen, error);
665
      }
666
667
    case UTF8:
668
      val = String::NewFromUtf8(isolate,
669
                                buf,
670
                                v8::NewStringType::kNormal,
671
757651
                                buflen);
672
757651
      if (val.IsEmpty()) {
673
2
        *error = node::ERR_STRING_TOO_LONG(isolate);
674
2
        return MaybeLocal<Value>();
675
      }
676
757649
      return val.ToLocalChecked();
677
678
    case LATIN1:
679
1310
      return ExternOneByteString::NewFromCopy(isolate, buf, buflen, error);
680
681
    case BASE64: {
682
1568
      size_t dlen = base64_encoded_size(buflen);
683
1568
      char* dst = node::UncheckedMalloc(dlen);
684
1568
      if (dst == nullptr) {
685
        *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
686
        return MaybeLocal<Value>();
687
      }
688
689
1568
      size_t written = base64_encode(buf, buflen, dst, dlen);
690
1568
      CHECK_EQ(written, dlen);
691
692
1568
      return ExternOneByteString::New(isolate, dst, dlen, error);
693
    }
694
695
    case HEX: {
696
670024
      size_t dlen = buflen * 2;
697
670024
      char* dst = node::UncheckedMalloc(dlen);
698
670024
      if (dst == nullptr) {
699
        *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
700
        return MaybeLocal<Value>();
701
      }
702
670024
      size_t written = hex_encode(buf, buflen, dst, dlen);
703
670024
      CHECK_EQ(written, dlen);
704
705
670024
      return ExternOneByteString::New(isolate, dst, dlen, error);
706
    }
707
708
    case UCS2: {
709
11047
      if (IsBigEndian()) {
710
        uint16_t* dst = node::UncheckedMalloc<uint16_t>(buflen / 2);
711
        if (dst == nullptr) {
712
          *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
713
          return MaybeLocal<Value>();
714
        }
715
        for (size_t i = 0, k = 0; k < buflen / 2; i += 2, k += 1) {
716
          // The input is in *little endian*, because that's what Node.js
717
          // expects, so the high byte comes after the low byte.
718
          const uint8_t hi = static_cast<uint8_t>(buf[i + 1]);
719
          const uint8_t lo = static_cast<uint8_t>(buf[i + 0]);
720
          dst[k] = static_cast<uint16_t>(hi) << 8 | lo;
721
        }
722
        return ExternTwoByteString::New(isolate, dst, buflen / 2, error);
723
      }
724
11047
      if (reinterpret_cast<uintptr_t>(buf) % 2 != 0) {
725
        // Unaligned data still means we can't directly pass it to V8.
726
231
        char* dst = node::UncheckedMalloc(buflen);
727
231
        if (dst == nullptr) {
728
          *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
729
          return MaybeLocal<Value>();
730
        }
731
231
        memcpy(dst, buf, buflen);
732
        return ExternTwoByteString::New(
733
231
            isolate, reinterpret_cast<uint16_t*>(dst), buflen / 2, error);
734
      }
735
      return ExternTwoByteString::NewFromCopy(
736
10816
          isolate, reinterpret_cast<const uint16_t*>(buf), buflen / 2, error);
737
    }
738
739
    default:
740
      CHECK(0 && "unknown encoding");
741
      break;
742
  }
743
744
  UNREACHABLE();
745
}
746
747
748
MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
749
                                      const uint16_t* buf,
750
                                      size_t buflen,
751
                                      Local<Value>* error) {
752
  CHECK_BUFLEN_IN_RANGE(buflen);
753
754
  // Node's "ucs2" encoding expects LE character data inside a
755
  // Buffer, so we need to reorder on BE platforms.  See
756
  // https://nodejs.org/api/buffer.html regarding Node's "ucs2"
757
  // encoding specification
758
  if (IsBigEndian()) {
759
    uint16_t* dst = node::UncheckedMalloc<uint16_t>(buflen);
760
    if (dst == nullptr) {
761
      *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
762
      return MaybeLocal<Value>();
763
    }
764
    size_t nbytes = buflen * sizeof(uint16_t);
765
    memcpy(dst, buf, nbytes);
766
    SwapBytes16(reinterpret_cast<char*>(dst), nbytes);
767
    return ExternTwoByteString::New(isolate, dst, buflen, error);
768
  } else {
769
    return ExternTwoByteString::NewFromCopy(isolate, buf, buflen, error);
770
  }
771
}
772
773
693554
MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
774
                                      const char* buf,
775
                                      enum encoding encoding,
776
                                      Local<Value>* error) {
777
693554
  const size_t len = strlen(buf);
778
693554
  return Encode(isolate, buf, len, encoding, error);
779
}
780
781
}  // namespace node