GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_i18n.cc Lines: 360 402 89.6 %
Date: 2021-04-26 04:12:24 Branches: 146 226 64.6 %

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
/*
23
 * notes: by srl295
24
 *  - When in NODE_HAVE_SMALL_ICU mode, ICU is linked against "stub" (null) data
25
 *     ( stubdata/libicudata.a ) containing nothing, no data, and it's also
26
 *    linked against a "small" data file which the SMALL_ICUDATA_ENTRY_POINT
27
 *    macro names. That's the "english+root" data.
28
 *
29
 *    If icu_data_path is non-null, the user has provided a path and we assume
30
 *    it goes somewhere useful. We set that path in ICU, and exit.
31
 *    If icu_data_path is null, they haven't set a path and we want the
32
 *    "english+root" data.  We call
33
 *       udata_setCommonData(SMALL_ICUDATA_ENTRY_POINT,...)
34
 *    to load up the english+root data.
35
 *
36
 *  - when NOT in NODE_HAVE_SMALL_ICU mode, ICU is linked directly with its full
37
 *    data. All of the variables and command line options for changing data at
38
 *    runtime are disabled, as they wouldn't fully override the internal data.
39
 *    See:  http://bugs.icu-project.org/trac/ticket/10924
40
 */
41
42
43
#include "node_i18n.h"
44
#include "node_external_reference.h"
45
46
#if defined(NODE_HAVE_I18N_SUPPORT)
47
48
#include "base_object-inl.h"
49
#include "node.h"
50
#include "node_buffer.h"
51
#include "node_errors.h"
52
#include "node_internals.h"
53
#include "util-inl.h"
54
#include "v8.h"
55
56
#include <unicode/utypes.h>
57
#include <unicode/putil.h>
58
#include <unicode/uchar.h>
59
#include <unicode/uclean.h>
60
#include <unicode/udata.h>
61
#include <unicode/uidna.h>
62
#include <unicode/ucnv.h>
63
#include <unicode/utf8.h>
64
#include <unicode/utf16.h>
65
#include <unicode/timezone.h>
66
#include <unicode/ulocdata.h>
67
#include <unicode/uvernum.h>
68
#include <unicode/uversion.h>
69
#include <unicode/ustring.h>
70
71
#ifdef NODE_HAVE_SMALL_ICU
72
/* if this is defined, we have a 'secondary' entry point.
73
   compare following to utypes.h defs for U_ICUDATA_ENTRY_POINT */
74
#define SMALL_ICUDATA_ENTRY_POINT \
75
  SMALL_DEF2(U_ICU_VERSION_MAJOR_NUM, U_LIB_SUFFIX_C_NAME)
76
#define SMALL_DEF2(major, suff) SMALL_DEF(major, suff)
77
#ifndef U_LIB_SUFFIX_C_NAME
78
#define SMALL_DEF(major, suff) icusmdt##major##_dat
79
#else
80
#define SMALL_DEF(major, suff) icusmdt##suff##major##_dat
81
#endif
82
83
extern "C" const char U_DATA_API SMALL_ICUDATA_ENTRY_POINT[];
84
#endif
85
86
namespace node {
87
88
using v8::Context;
89
using v8::FunctionCallbackInfo;
90
using v8::FunctionTemplate;
91
using v8::Int32;
92
using v8::Isolate;
93
using v8::Local;
94
using v8::MaybeLocal;
95
using v8::NewStringType;
96
using v8::Object;
97
using v8::ObjectTemplate;
98
using v8::String;
99
using v8::Uint8Array;
100
using v8::Value;
101
102
namespace i18n {
103
namespace {
104
105
template <typename T>
106
1006
MaybeLocal<Object> ToBufferEndian(Environment* env, MaybeStackBuffer<T>* buf) {
107
1006
  MaybeLocal<Object> ret = Buffer::New(env, buf);
108

1006
  if (ret.IsEmpty())
109
    return ret;
110
111
  static_assert(sizeof(T) == 1 || sizeof(T) == 2,
112
                "Currently only one- or two-byte buffers are supported");
113

1006
  if (sizeof(T) > 1 && IsBigEndian()) {
114
    SPREAD_BUFFER_ARG(ret.ToLocalChecked(), retbuf);
115
    SwapBytes16(retbuf_data, retbuf_length);
116
  }
117
118
1006
  return ret;
119
}
120
121
// One-Shot Converters
122
123
2
void CopySourceBuffer(MaybeStackBuffer<UChar>* dest,
124
                      const char* data,
125
                      const size_t length,
126
                      const size_t length_in_chars) {
127
2
  dest->AllocateSufficientStorage(length_in_chars);
128
2
  char* dst = reinterpret_cast<char*>(**dest);
129
2
  memcpy(dst, data, length);
130
2
  if (IsBigEndian()) {
131
    SwapBytes16(dst, length);
132
  }
133
2
}
134
135
typedef MaybeLocal<Object> (*TranscodeFunc)(Environment* env,
136
                                            const char* fromEncoding,
137
                                            const char* toEncoding,
138
                                            const char* source,
139
                                            const size_t source_length,
140
                                            UErrorCode* status);
141
142
3
MaybeLocal<Object> Transcode(Environment* env,
143
                             const char* fromEncoding,
144
                             const char* toEncoding,
145
                             const char* source,
146
                             const size_t source_length,
147
                             UErrorCode* status) {
148
3
  *status = U_ZERO_ERROR;
149
  MaybeLocal<Object> ret;
150
6
  MaybeStackBuffer<char> result;
151
6
  Converter to(toEncoding);
152
6
  Converter from(fromEncoding);
153
154
3
  size_t sublen = ucnv_getMinCharSize(to.conv());
155
6
  std::string sub(sublen, '?');
156
3
  to.set_subst_chars(sub.c_str());
157
158
3
  const uint32_t limit = source_length * to.max_char_size();
159
3
  result.AllocateSufficientStorage(limit);
160
3
  char* target = *result;
161
3
  ucnv_convertEx(to.conv(), from.conv(), &target, target + limit,
162
                 &source, source + source_length, nullptr, nullptr,
163
3
                 nullptr, nullptr, true, true, status);
164
3
  if (U_SUCCESS(*status)) {
165
3
    result.SetLength(target - &result[0]);
166
3
    ret = ToBufferEndian(env, &result);
167
  }
168
6
  return ret;
169
}
170
171
4
MaybeLocal<Object> TranscodeToUcs2(Environment* env,
172
                                   const char* fromEncoding,
173
                                   const char* toEncoding,
174
                                   const char* source,
175
                                   const size_t source_length,
176
                                   UErrorCode* status) {
177
4
  *status = U_ZERO_ERROR;
178
  MaybeLocal<Object> ret;
179
8
  MaybeStackBuffer<UChar> destbuf(source_length);
180
8
  Converter from(fromEncoding);
181
4
  const size_t length_in_chars = source_length * sizeof(UChar);
182
4
  ucnv_toUChars(from.conv(), *destbuf, length_in_chars,
183
4
                source, source_length, status);
184
4
  if (U_SUCCESS(*status))
185
4
    ret = ToBufferEndian(env, &destbuf);
186
8
  return ret;
187
}
188
189
MaybeLocal<Object> TranscodeFromUcs2(Environment* env,
190
                                     const char* fromEncoding,
191
                                     const char* toEncoding,
192
                                     const char* source,
193
                                     const size_t source_length,
194
                                     UErrorCode* status) {
195
  *status = U_ZERO_ERROR;
196
  MaybeStackBuffer<UChar> sourcebuf;
197
  MaybeLocal<Object> ret;
198
  Converter to(toEncoding);
199
200
  size_t sublen = ucnv_getMinCharSize(to.conv());
201
  std::string sub(sublen, '?');
202
  to.set_subst_chars(sub.c_str());
203
204
  const size_t length_in_chars = source_length / sizeof(UChar);
205
  CopySourceBuffer(&sourcebuf, source, source_length, length_in_chars);
206
  MaybeStackBuffer<char> destbuf(length_in_chars);
207
  const uint32_t len = ucnv_fromUChars(to.conv(), *destbuf, length_in_chars,
208
                                       *sourcebuf, length_in_chars, status);
209
  if (U_SUCCESS(*status)) {
210
    destbuf.SetLength(len);
211
    ret = ToBufferEndian(env, &destbuf);
212
  }
213
  return ret;
214
}
215
216
2
MaybeLocal<Object> TranscodeUcs2FromUtf8(Environment* env,
217
                                         const char* fromEncoding,
218
                                         const char* toEncoding,
219
                                         const char* source,
220
                                         const size_t source_length,
221
                                         UErrorCode* status) {
222
2
  *status = U_ZERO_ERROR;
223
4
  MaybeStackBuffer<UChar> destbuf;
224
  int32_t result_length;
225
2
  u_strFromUTF8(*destbuf, destbuf.capacity(), &result_length,
226
2
                source, source_length, status);
227
  MaybeLocal<Object> ret;
228
2
  if (U_SUCCESS(*status)) {
229
1
    destbuf.SetLength(result_length);
230
1
    ret = ToBufferEndian(env, &destbuf);
231
1
  } else if (*status == U_BUFFER_OVERFLOW_ERROR) {
232
1
    *status = U_ZERO_ERROR;
233
1
    destbuf.AllocateSufficientStorage(result_length);
234
2
    u_strFromUTF8(*destbuf, result_length, &result_length,
235
2
                  source, source_length, status);
236
1
    if (U_SUCCESS(*status)) {
237
1
      destbuf.SetLength(result_length);
238
1
      ret = ToBufferEndian(env, &destbuf);
239
    }
240
  }
241
4
  return ret;
242
}
243
244
2
MaybeLocal<Object> TranscodeUtf8FromUcs2(Environment* env,
245
                                         const char* fromEncoding,
246
                                         const char* toEncoding,
247
                                         const char* source,
248
                                         const size_t source_length,
249
                                         UErrorCode* status) {
250
2
  *status = U_ZERO_ERROR;
251
  MaybeLocal<Object> ret;
252
2
  const size_t length_in_chars = source_length / sizeof(UChar);
253
  int32_t result_length;
254
4
  MaybeStackBuffer<UChar> sourcebuf;
255
4
  MaybeStackBuffer<char> destbuf;
256
2
  CopySourceBuffer(&sourcebuf, source, source_length, length_in_chars);
257
4
  u_strToUTF8(*destbuf, destbuf.capacity(), &result_length,
258
4
              *sourcebuf, length_in_chars, status);
259
2
  if (U_SUCCESS(*status)) {
260
1
    destbuf.SetLength(result_length);
261
1
    ret = ToBufferEndian(env, &destbuf);
262
1
  } else if (*status == U_BUFFER_OVERFLOW_ERROR) {
263
1
    *status = U_ZERO_ERROR;
264
1
    destbuf.AllocateSufficientStorage(result_length);
265
2
    u_strToUTF8(*destbuf, result_length, &result_length, *sourcebuf,
266
2
                length_in_chars, status);
267
1
    if (U_SUCCESS(*status)) {
268
1
      destbuf.SetLength(result_length);
269
1
      ret = ToBufferEndian(env, &destbuf);
270
    }
271
  }
272
4
  return ret;
273
}
274
275
22
const char* EncodingName(const enum encoding encoding) {
276

22
  switch (encoding) {
277
2
    case ASCII: return "us-ascii";
278
4
    case LATIN1: return "iso8859-1";
279
10
    case UCS2: return "utf16le";
280
6
    case UTF8: return "utf-8";
281
    default: return nullptr;
282
  }
283
}
284
285
24
bool SupportedEncoding(const enum encoding encoding) {
286
24
  switch (encoding) {
287
    case ASCII:
288
    case LATIN1:
289
    case UCS2:
290
22
    case UTF8: return true;
291
2
    default: return false;
292
  }
293
}
294
295
13
void Transcode(const FunctionCallbackInfo<Value>&args) {
296
13
  Environment* env = Environment::GetCurrent(args);
297
13
  Isolate* isolate = env->isolate();
298
13
  UErrorCode status = U_ZERO_ERROR;
299
  MaybeLocal<Object> result;
300
301
13
  ArrayBufferViewContents<char> input(args[0]);
302
13
  const enum encoding fromEncoding = ParseEncoding(isolate, args[1], BUFFER);
303
13
  const enum encoding toEncoding = ParseEncoding(isolate, args[2], BUFFER);
304
305

13
  if (SupportedEncoding(fromEncoding) && SupportedEncoding(toEncoding)) {
306
11
    TranscodeFunc tfn = &Transcode;
307

11
    switch (fromEncoding) {
308
      case ASCII:
309
      case LATIN1:
310
4
        if (toEncoding == UCS2)
311
4
          tfn = &TranscodeToUcs2;
312
4
        break;
313
      case UTF8:
314
4
        if (toEncoding == UCS2)
315
2
          tfn = &TranscodeUcs2FromUtf8;
316
4
        break;
317
      case UCS2:
318
3
        switch (toEncoding) {
319
          case UCS2:
320
1
            tfn = &Transcode;
321
1
            break;
322
          case UTF8:
323
2
            tfn = &TranscodeUtf8FromUcs2;
324
2
            break;
325
          default:
326
            tfn = &TranscodeFromUcs2;
327
        }
328
3
        break;
329
      default:
330
        // This should not happen because of the SupportedEncoding checks
331
        ABORT();
332
    }
333
334
    result = tfn(env, EncodingName(fromEncoding), EncodingName(toEncoding),
335
11
                 input.data(), input.length(), &status);
336
  } else {
337
2
    status = U_ILLEGAL_ARGUMENT_ERROR;
338
  }
339
340
13
  if (result.IsEmpty())
341
6
    return args.GetReturnValue().Set(status);
342
343
22
  return args.GetReturnValue().Set(result.ToLocalChecked());
344
}
345
346
2
void ICUErrorName(const FunctionCallbackInfo<Value>& args) {
347
2
  Environment* env = Environment::GetCurrent(args);
348
4
  CHECK(args[0]->IsInt32());
349
6
  UErrorCode status = static_cast<UErrorCode>(args[0].As<Int32>()->Value());
350
4
  args.GetReturnValue().Set(
351
4
      String::NewFromUtf8(env->isolate(),
352
2
                          u_errorName(status)).ToLocalChecked());
353
2
}
354
355
}  // anonymous namespace
356
357
10
Converter::Converter(const char* name, const char* sub) {
358
10
  UErrorCode status = U_ZERO_ERROR;
359
10
  UConverter* conv = ucnv_open(name, &status);
360
10
  CHECK(U_SUCCESS(status));
361
10
  conv_.reset(conv);
362
10
  set_subst_chars(sub);
363
10
}
364
365
4099
Converter::Converter(UConverter* converter, const char* sub)
366
4099
    : conv_(converter) {
367
4099
  set_subst_chars(sub);
368
4099
}
369
370
4112
void Converter::set_subst_chars(const char* sub) {
371
4112
  CHECK(conv_);
372
4112
  UErrorCode status = U_ZERO_ERROR;
373
4112
  if (sub != nullptr) {
374
3
    ucnv_setSubstChars(conv_.get(), sub, strlen(sub), &status);
375
3
    CHECK(U_SUCCESS(status));
376
  }
377
4112
}
378
379
669
void Converter::reset() {
380
669
  ucnv_reset(conv_.get());
381
669
}
382
383
1090
size_t Converter::min_char_size() const {
384
1090
  CHECK(conv_);
385
1090
  return ucnv_getMinCharSize(conv_.get());
386
}
387
388
3
size_t Converter::max_char_size() const {
389
3
  CHECK(conv_);
390
3
  return ucnv_getMaxCharSize(conv_.get());
391
}
392
393
2
void ConverterObject::Has(const FunctionCallbackInfo<Value>& args) {
394
2
  Environment* env = Environment::GetCurrent(args);
395
396
2
  CHECK_GE(args.Length(), 1);
397
4
  Utf8Value label(env->isolate(), args[0]);
398
399
2
  UErrorCode status = U_ZERO_ERROR;
400
4
  ConverterPointer conv(ucnv_open(*label, &status));
401
6
  args.GetReturnValue().Set(!!U_SUCCESS(status));
402
2
}
403
404
4099
void ConverterObject::Create(const FunctionCallbackInfo<Value>& args) {
405
4099
  Environment* env = Environment::GetCurrent(args);
406
407
4098
  Local<ObjectTemplate> t = env->i18n_converter_template();
408
  Local<Object> obj;
409
12295
  if (!t->NewInstance(env->context()).ToLocal(&obj)) return;
410
411
4099
  CHECK_GE(args.Length(), 2);
412
8198
  Utf8Value label(env->isolate(), args[0]);
413
16387
  int flags = args[1]->Uint32Value(env->context()).ToChecked();
414
  bool fatal =
415
4098
      (flags & CONVERTER_FLAGS_FATAL) == CONVERTER_FLAGS_FATAL;
416
417
4098
  UErrorCode status = U_ZERO_ERROR;
418
4098
  UConverter* conv = ucnv_open(*label, &status);
419
4099
  if (U_FAILURE(status))
420
    return;
421
422
4099
  if (fatal) {
423
156
    status = U_ZERO_ERROR;
424
    ucnv_setToUCallBack(conv, UCNV_TO_U_CALLBACK_STOP,
425
156
                        nullptr, nullptr, nullptr, &status);
426
  }
427
428
4099
  new ConverterObject(env, obj, conv, flags);
429
8198
  args.GetReturnValue().Set(obj);
430
}
431
432
1090
void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
433
1090
  Environment* env = Environment::GetCurrent(args);
434
435
1090
  CHECK_GE(args.Length(), 3);  // Converter, Buffer, Flags
436
437
  ConverterObject* converter;
438
3175
  ASSIGN_OR_RETURN_UNWRAP(&converter, args[0].As<Object>());
439
1090
  ArrayBufferViewContents<char> input(args[1]);
440
4360
  int flags = args[2]->Uint32Value(env->context()).ToChecked();
441
442
1090
  UErrorCode status = U_ZERO_ERROR;
443
1185
  MaybeStackBuffer<UChar> result;
444
  MaybeLocal<Object> ret;
445
1090
  size_t limit = converter->min_char_size() * input.length();
446
1090
  if (limit > 0)
447
1051
    result.AllocateSufficientStorage(limit);
448
449
1090
  UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH;
450
1090
  auto cleanup = OnScopeLeave([&]() {
451
1090
    if (flush) {
452
      // Reset the converter state.
453
669
      converter->set_bom_seen(false);
454
669
      converter->reset();
455
    }
456
2275
  });
457
458
1090
  const char* source = input.data();
459
1090
  size_t source_length = input.length();
460
461
1090
  UChar* target = *result;
462
2180
  ucnv_toUnicode(converter->conv(),
463
                 &target,
464
1090
                 target + (limit * sizeof(UChar)),
465
                 &source,
466
                 source + source_length,
467
                 nullptr,
468
                 flush,
469
1090
                 &status);
470
471
1090
  if (U_SUCCESS(status)) {
472
995
    bool omit_initial_bom = false;
473
995
    if (limit > 0) {
474
959
      result.SetLength(target - &result[0]);
475

2766
      if (result.length() > 0 &&
476
1662
          converter->unicode() &&
477

2570
          !converter->ignore_bom() &&
478
797
          !converter->bom_seen()) {
479
        // If the very first result in the stream is a BOM, and we are not
480
        // explicitly told to ignore it, then we mark it for discarding.
481
517
        if (result[0] == 0xFEFF)
482
29
          omit_initial_bom = true;
483
517
        converter->set_bom_seen(true);
484
      }
485
    }
486
995
    ret = ToBufferEndian(env, &result);
487

1024
    if (omit_initial_bom && !ret.IsEmpty()) {
488
      // Peform `ret = ret.slice(2)`.
489
58
      CHECK(ret.ToLocalChecked()->IsUint8Array());
490
58
      Local<Uint8Array> orig_ret = ret.ToLocalChecked().As<Uint8Array>();
491
87
      ret = Buffer::New(env,
492
29
                        orig_ret->Buffer(),
493
29
                        orig_ret->ByteOffset() + 2,
494
87
                        orig_ret->ByteLength() - 2)
495
58
                            .FromMaybe(Local<Uint8Array>());
496
    }
497
995
    if (!ret.IsEmpty())
498
1990
      args.GetReturnValue().Set(ret.ToLocalChecked());
499
995
    return;
500
  }
501
502
285
  args.GetReturnValue().Set(status);
503
}
504
505
4099
ConverterObject::ConverterObject(
506
    Environment* env,
507
    Local<Object> wrap,
508
    UConverter* converter,
509
    int flags,
510
4099
    const char* sub)
511
    : BaseObject(env, wrap),
512
      Converter(converter, sub),
513
4099
      flags_(flags) {
514
4099
  MakeWeak();
515
516
4099
  switch (ucnv_getType(converter)) {
517
    case UCNV_UTF8:
518
    case UCNV_UTF16_BigEndian:
519
    case UCNV_UTF16_LittleEndian:
520
546
      flags_ |= CONVERTER_FLAGS_UNICODE;
521
546
      break;
522
    default: {
523
      // Fall through
524
    }
525
  }
526
4099
}
527
528
529
4787
bool InitializeICUDirectory(const std::string& path) {
530
4787
  UErrorCode status = U_ZERO_ERROR;
531
4787
  if (path.empty()) {
532
#ifdef NODE_HAVE_SMALL_ICU
533
    // install the 'small' data.
534
    udata_setCommonData(&SMALL_ICUDATA_ENTRY_POINT, &status);
535
#else  // !NODE_HAVE_SMALL_ICU
536
    // no small data, so nothing to do.
537
#endif  // !NODE_HAVE_SMALL_ICU
538
  } else {
539
    u_setDataDirectory(path.c_str());
540
    u_init(&status);
541
  }
542
4787
  return status == U_ZERO_ERROR;
543
}
544
545
384
int32_t ToUnicode(MaybeStackBuffer<char>* buf,
546
                  const char* input,
547
                  size_t length) {
548
384
  UErrorCode status = U_ZERO_ERROR;
549
384
  uint32_t options = UIDNA_NONTRANSITIONAL_TO_UNICODE;
550
384
  UIDNA* uidna = uidna_openUTS46(options, &status);
551
384
  if (U_FAILURE(status))
552
    return -1;
553
384
  UIDNAInfo info = UIDNA_INFO_INITIALIZER;
554
555
384
  int32_t len = uidna_nameToUnicodeUTF8(uidna,
556
                                        input, length,
557
384
                                        **buf, buf->capacity(),
558
                                        &info,
559
384
                                        &status);
560
561
  // Do not check info.errors like we do with ToASCII since ToUnicode always
562
  // returns a string, despite any possible errors that may have occurred.
563
564
384
  if (status == U_BUFFER_OVERFLOW_ERROR) {
565
    status = U_ZERO_ERROR;
566
    buf->AllocateSufficientStorage(len);
567
    len = uidna_nameToUnicodeUTF8(uidna,
568
                                  input, length,
569
                                  **buf, buf->capacity(),
570
                                  &info,
571
                                  &status);
572
  }
573
574
  // info.errors is ignored as UTS #46 ToUnicode always produces a Unicode
575
  // string, regardless of whether an error occurred.
576
577
384
  if (U_FAILURE(status)) {
578
    len = -1;
579
    buf->SetLength(0);
580
  } else {
581
384
    buf->SetLength(len);
582
  }
583
584
384
  uidna_close(uidna);
585
384
  return len;
586
}
587
588
14847
int32_t ToASCII(MaybeStackBuffer<char>* buf,
589
                const char* input,
590
                size_t length,
591
                enum idna_mode mode) {
592
14847
  UErrorCode status = U_ZERO_ERROR;
593
  uint32_t options =                  // CheckHyphens = false; handled later
594
    UIDNA_CHECK_BIDI |                // CheckBidi = true
595
    UIDNA_CHECK_CONTEXTJ |            // CheckJoiners = true
596
14847
    UIDNA_NONTRANSITIONAL_TO_ASCII;   // Nontransitional_Processing
597
14847
  if (mode == IDNA_STRICT) {
598
    options |= UIDNA_USE_STD3_RULES;  // UseSTD3ASCIIRules = beStrict
599
                                      // VerifyDnsLength = beStrict;
600
                                      //   handled later
601
  }
602
603
14847
  UIDNA* uidna = uidna_openUTS46(options, &status);
604
14847
  if (U_FAILURE(status))
605
    return -1;
606
14847
  UIDNAInfo info = UIDNA_INFO_INITIALIZER;
607
608
14847
  int32_t len = uidna_nameToASCII_UTF8(uidna,
609
                                       input, length,
610
14847
                                       **buf, buf->capacity(),
611
                                       &info,
612
14847
                                       &status);
613
614
14847
  if (status == U_BUFFER_OVERFLOW_ERROR) {
615
2
    status = U_ZERO_ERROR;
616
2
    buf->AllocateSufficientStorage(len);
617
2
    len = uidna_nameToASCII_UTF8(uidna,
618
                                 input, length,
619
2
                                 **buf, buf->capacity(),
620
                                 &info,
621
2
                                 &status);
622
  }
623
624
  // In UTS #46 which specifies ToASCII, certain error conditions are
625
  // configurable through options, and the WHATWG URL Standard promptly elects
626
  // to disable some of them to accommodate for real-world use cases.
627
  // Unfortunately, ICU4C's IDNA module does not support disabling some of
628
  // these options through `options` above, and thus continues throwing
629
  // unnecessary errors. To counter this situation, we just filter out the
630
  // errors that may have happened afterwards, before deciding whether to
631
  // return an error from this function.
632
633
  // CheckHyphens = false
634
  // (Specified in the current UTS #46 draft rev. 18.)
635
  // Refs:
636
  // - https://github.com/whatwg/url/issues/53
637
  // - https://github.com/whatwg/url/pull/309
638
  // - http://www.unicode.org/review/pri317/
639
  // - http://www.unicode.org/reports/tr46/tr46-18.html
640
  // - https://www.icann.org/news/announcement-2000-01-07-en
641
14847
  info.errors &= ~UIDNA_ERROR_HYPHEN_3_4;
642
14847
  info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN;
643
14847
  info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN;
644
645
14847
  if (mode != IDNA_STRICT) {
646
    // VerifyDnsLength = beStrict
647
14847
    info.errors &= ~UIDNA_ERROR_EMPTY_LABEL;
648
14847
    info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
649
14847
    info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
650
  }
651
652


14847
  if (U_FAILURE(status) || (mode != IDNA_LENIENT && info.errors != 0)) {
653
95
    len = -1;
654
95
    buf->SetLength(0);
655
  } else {
656
14752
    buf->SetLength(len);
657
  }
658
659
14847
  uidna_close(uidna);
660
14847
  return len;
661
}
662
663
189
static void ToUnicode(const FunctionCallbackInfo<Value>& args) {
664
189
  Environment* env = Environment::GetCurrent(args);
665
189
  CHECK_GE(args.Length(), 1);
666
567
  CHECK(args[0]->IsString());
667
378
  Utf8Value val(env->isolate(), args[0]);
668
669
378
  MaybeStackBuffer<char> buf;
670
189
  int32_t len = ToUnicode(&buf, *val, val.length());
671
672
189
  if (len < 0) {
673
    return THROW_ERR_INVALID_ARG_VALUE(env, "Cannot convert name to Unicode");
674
  }
675
676
378
  args.GetReturnValue().Set(
677
378
      String::NewFromUtf8(env->isolate(),
678
189
                          *buf,
679
                          NewStringType::kNormal,
680
189
                          len).ToLocalChecked());
681
}
682
683
10605
static void ToASCII(const FunctionCallbackInfo<Value>& args) {
684
10605
  Environment* env = Environment::GetCurrent(args);
685
10605
  CHECK_GE(args.Length(), 1);
686
31815
  CHECK(args[0]->IsString());
687
21201
  Utf8Value val(env->isolate(), args[0]);
688
  // optional arg
689
31815
  bool lenient = args[1]->BooleanValue(env->isolate());
690
10605
  enum idna_mode mode = lenient ? IDNA_LENIENT : IDNA_DEFAULT;
691
692
21201
  MaybeStackBuffer<char> buf;
693
10605
  int32_t len = ToASCII(&buf, *val, val.length(), mode);
694
695
10605
  if (len < 0) {
696
9
    return THROW_ERR_INVALID_ARG_VALUE(env, "Cannot convert name to ASCII");
697
  }
698
699
21192
  args.GetReturnValue().Set(
700
21192
      String::NewFromUtf8(env->isolate(),
701
10596
                          *buf,
702
                          NewStringType::kNormal,
703
10596
                          len).ToLocalChecked());
704
}
705
706
// This is similar to wcwidth except that it takes the current unicode
707
// character properties database into consideration, allowing it to
708
// correctly calculate the column widths of things like emoji's and
709
// newer wide characters. wcwidth, on the other hand, uses a fixed
710
// algorithm that does not take things like emoji into proper
711
// consideration.
712
//
713
// TODO(TimothyGu): Investigate Cc (C0/C1 control codes). Both VTE (used by
714
// GNOME Terminal) and Konsole don't consider them to be zero-width (see refs
715
// below), and when printed in VTE it is Narrow. However GNOME Terminal doesn't
716
// allow it to be input. Linux's PTY terminal prints control characters as
717
// Narrow rhombi.
718
//
719
// TODO(TimothyGu): Investigate Hangul jamo characters. Medial vowels and final
720
// consonants are 0-width when combined with initial consonants; otherwise they
721
// are technically Wide. But many terminals (including Konsole and
722
// VTE/GLib-based) implement all medials and finals as 0-width.
723
//
724
// Refs: https://eev.ee/blog/2015/09/12/dark-corners-of-unicode/#combining-characters-and-character-width
725
// Refs: https://github.com/GNOME/glib/blob/79e4d4c6be/glib/guniprop.c#L388-L420
726
// Refs: https://github.com/KDE/konsole/blob/8c6a5d13c0/src/konsole_wcwidth.cpp#L101-L223
727
641098
static int GetColumnWidth(UChar32 codepoint,
728
                          bool ambiguous_as_full_width = false) {
729
  // UCHAR_EAST_ASIAN_WIDTH is the Unicode property that identifies a
730
  // codepoint as being full width, wide, ambiguous, neutral, narrow,
731
  // or halfwidth.
732
641098
  const int eaw = u_getIntPropertyValue(codepoint, UCHAR_EAST_ASIAN_WIDTH);
733

641098
  switch (eaw) {
734
    case U_EA_FULLWIDTH:
735
    case U_EA_WIDE:
736
37211
      return 2;
737
    case U_EA_AMBIGUOUS:
738
      // See: http://www.unicode.org/reports/tr11/#Ambiguous for details
739
511139
      if (ambiguous_as_full_width) {
740
        return 2;
741
      }
742
      // If ambiguous_as_full_width is false:
743
      // Fall through
744
    case U_EA_NEUTRAL:
745
579454
      if (u_hasBinaryProperty(codepoint, UCHAR_EMOJI_PRESENTATION)) {
746
        return 2;
747
      }
748
      // Fall through
749
    case U_EA_HALFWIDTH:
750
    case U_EA_NARROW:
751
    default:
752
      const auto zero_width_mask = U_GC_CC_MASK |  // C0/C1 control code
753
                                  U_GC_CF_MASK |  // Format control character
754
                                  U_GC_ME_MASK |  // Enclosing mark
755
603887
                                  U_GC_MN_MASK;   // Nonspacing mark
756

1712482
      if (codepoint != 0x00AD &&  // SOFT HYPHEN is Cf but not zero-width
757
702925
          ((U_MASK(u_charType(codepoint)) & zero_width_mask) ||
758
702925
          u_hasBinaryProperty(codepoint, UCHAR_EMOJI_MODIFIER))) {
759
504755
        return 0;
760
      }
761
99132
      return 1;
762
  }
763
}
764
765
// Returns the column width for the given String.
766
602172
static void GetStringWidth(const FunctionCallbackInfo<Value>& args) {
767
602172
  Environment* env = Environment::GetCurrent(args);
768
1806516
  CHECK(args[0]->IsString());
769
770
1204344
  bool ambiguous_as_full_width = args[1]->IsTrue();
771

1204344
  bool expand_emoji_sequence = !args[2]->IsBoolean() || args[2]->IsTrue();
772
773
1204344
  TwoByteValue value(env->isolate(), args[0]);
774
  // reinterpret_cast is required by windows to compile
775
602172
  UChar* str = reinterpret_cast<UChar*>(*value);
776
  static_assert(sizeof(*str) == sizeof(**value),
777
                "sizeof(*str) == sizeof(**value)");
778
602172
  UChar32 c = 0;
779
  UChar32 p;
780
602172
  size_t n = 0;
781
602172
  uint32_t width = 0;
782
783
1884368
  while (n < value.length()) {
784
641098
    p = c;
785


641098
    U16_NEXT(str, n, value.length(), c);
786
    // Don't count individual emoji codepoints that occur within an
787
    // emoji sequence. This is not necessarily foolproof. Some
788
    // environments display emoji sequences in the appropriate
789
    // condensed form (as a single emoji glyph), other environments
790
    // may not understand an emoji sequence and will display each
791
    // individual emoji separately. When this happens, the width
792
    // calculated will be off, and there's no reliable way of knowing
793
    // in advance if a particular sequence is going to be supported.
794
    // The expand_emoji_sequence option allows the caller to skip this
795
    // check and count each code within an emoji sequence separately.
796
    // https://www.unicode.org/reports/tr51/tr51-16.html#Emoji_ZWJ_Sequences
797

1282196
    if (!expand_emoji_sequence &&
798

641098
        n > 0 && p == 0x200d &&  // 0x200d == ZWJ (zero width joiner)
799
        (u_hasBinaryProperty(c, UCHAR_EMOJI_PRESENTATION) ||
800
         u_hasBinaryProperty(c, UCHAR_EMOJI_MODIFIER))) {
801
      continue;
802
    }
803
641098
    width += GetColumnWidth(c, ambiguous_as_full_width);
804
  }
805
1204344
  args.GetReturnValue().Set(width);
806
602172
}
807
808
468
void Initialize(Local<Object> target,
809
                Local<Value> unused,
810
                Local<Context> context,
811
                void* priv) {
812
468
  Environment* env = Environment::GetCurrent(context);
813
468
  env->SetMethod(target, "toUnicode", ToUnicode);
814
468
  env->SetMethod(target, "toASCII", ToASCII);
815
468
  env->SetMethod(target, "getStringWidth", GetStringWidth);
816
817
  // One-shot converters
818
468
  env->SetMethod(target, "icuErrName", ICUErrorName);
819
468
  env->SetMethod(target, "transcode", Transcode);
820
821
  // ConverterObject
822
  {
823
468
    Local<FunctionTemplate> t = FunctionTemplate::New(env->isolate());
824
936
    t->Inherit(BaseObject::GetConstructorTemplate(env));
825
1404
    t->InstanceTemplate()->SetInternalFieldCount(
826
468
        ConverterObject::kInternalFieldCount);
827
    Local<String> converter_string =
828
468
        FIXED_ONE_BYTE_STRING(env->isolate(), "Converter");
829
468
    t->SetClassName(converter_string);
830
468
    env->set_i18n_converter_template(t->InstanceTemplate());
831
  }
832
833
468
  env->SetMethod(target, "getConverter", ConverterObject::Create);
834
468
  env->SetMethod(target, "decode", ConverterObject::Decode);
835
468
  env->SetMethod(target, "hasConverter", ConverterObject::Has);
836
468
}
837
838
4758
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
839
4758
  registry->Register(ToUnicode);
840
4758
  registry->Register(ToASCII);
841
4758
  registry->Register(GetStringWidth);
842
4758
  registry->Register(ICUErrorName);
843
4758
  registry->Register(Transcode);
844
4758
  registry->Register(ConverterObject::Create);
845
4758
  registry->Register(ConverterObject::Decode);
846
4758
  registry->Register(ConverterObject::Has);
847
4758
}
848
849
}  // namespace i18n
850
}  // namespace node
851
852
4817
NODE_MODULE_CONTEXT_AWARE_INTERNAL(icu, node::i18n::Initialize)
853

19233
NODE_MODULE_EXTERNAL_REFERENCE(icu, node::i18n::RegisterExternalReferences)
854
855
#endif  // NODE_HAVE_I18N_SUPPORT