GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_i18n.cc Lines: 369 419 88.1 %
Date: 2022-08-14 04:19:53 Branches: 140 216 64.8 %

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
4446
MaybeLocal<Object> ToBufferEndian(Environment* env, MaybeStackBuffer<T>* buf) {
107
4446
  MaybeLocal<Object> ret = Buffer::New(env, buf);
108
4446
  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
4436
  if (sizeof(T) > 1 && IsBigEndian()) {
114
    SPREAD_BUFFER_ARG(ret.ToLocalChecked(), retbuf);
115
    SwapBytes16(retbuf_data, retbuf_length);
116
  }
117
118
4446
  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
3
  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
                 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
3
  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
4
  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
                source, source_length, status);
184
4
  if (U_SUCCESS(*status))
185
4
    ret = ToBufferEndian(env, &destbuf);
186
4
  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
2
  MaybeStackBuffer<UChar> destbuf;
224
  int32_t result_length;
225
2
  u_strFromUTF8(*destbuf, destbuf.capacity(), &result_length,
226
                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
1
    u_strFromUTF8(*destbuf, result_length, &result_length,
235
                  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
2
  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
2
  MaybeStackBuffer<char> destbuf;
256
2
  CopySourceBuffer(&sourcebuf, source, source_length, length_in_chars);
257
2
  u_strToUTF8(*destbuf, destbuf.capacity(), &result_length,
258
2
              *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
1
    u_strToUTF8(*destbuf, result_length, &result_length, *sourcebuf,
266
                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
2
  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
22
    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
4
      case ASCII:
309
      case LATIN1:
310
4
        if (toEncoding == UCS2)
311
4
          tfn = &TranscodeToUcs2;
312
4
        break;
313
4
      case UTF8:
314
4
        if (toEncoding == UCS2)
315
2
          tfn = &TranscodeUcs2FromUtf8;
316
4
        break;
317
3
      case UCS2:
318
3
        switch (toEncoding) {
319
1
          case UCS2:
320
1
            tfn = &Transcode;
321
1
            break;
322
2
          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
4
    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
2
  CHECK(args[0]->IsInt32());
349
4
  UErrorCode status = static_cast<UErrorCode>(args[0].As<Int32>()->Value());
350
6
  args.GetReturnValue().Set(
351
2
      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
921
Converter::Converter(UConverter* converter, const char* sub)
366
921
    : conv_(converter) {
367
921
  set_subst_chars(sub);
368
921
}
369
370
1855
void Converter::set_subst_chars(const char* sub) {
371
1855
  CHECK(conv_);
372
1855
  UErrorCode status = U_ZERO_ERROR;
373
1855
  if (sub != nullptr) {
374
924
    ucnv_setSubstChars(conv_.get(), sub, strlen(sub), &status);
375
924
    CHECK(U_SUCCESS(status));
376
  }
377
1855
}
378
379
1411
void Converter::reset() {
380
1411
  ucnv_reset(conv_.get());
381
1411
}
382
383
2309
size_t Converter::min_char_size() const {
384
2309
  CHECK(conv_);
385
2309
  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
2
  ConverterPointer conv(ucnv_open(*label, &status));
401
4
  args.GetReturnValue().Set(!!U_SUCCESS(status));
402
2
}
403
404
921
void ConverterObject::Create(const FunctionCallbackInfo<Value>& args) {
405
921
  Environment* env = Environment::GetCurrent(args);
406
407
921
  Local<ObjectTemplate> t = env->i18n_converter_template();
408
  Local<Object> obj;
409
1842
  if (!t->NewInstance(env->context()).ToLocal(&obj)) return;
410
411
921
  CHECK_GE(args.Length(), 2);
412
921
  Utf8Value label(env->isolate(), args[0]);
413
921
  int flags = args[1]->Uint32Value(env->context()).ToChecked();
414
921
  bool fatal =
415
921
      (flags & CONVERTER_FLAGS_FATAL) == CONVERTER_FLAGS_FATAL;
416
417
921
  UErrorCode status = U_ZERO_ERROR;
418
921
  UConverter* conv = ucnv_open(*label, &status);
419
921
  if (U_FAILURE(status))
420
    return;
421
422
921
  if (fatal) {
423
337
    status = U_ZERO_ERROR;
424
337
    ucnv_setToUCallBack(conv, UCNV_TO_U_CALLBACK_STOP,
425
                        nullptr, nullptr, nullptr, &status);
426
  }
427
428
921
  auto converter = new ConverterObject(env, obj, conv, flags);
429
921
  size_t sublen = ucnv_getMinCharSize(conv);
430
921
  std::string sub(sublen, '?');
431
921
  converter->set_subst_chars(sub.c_str());
432
433
1842
  args.GetReturnValue().Set(obj);
434
}
435
436
2309
void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
437
2309
  Environment* env = Environment::GetCurrent(args);
438
439
2309
  CHECK_GE(args.Length(), 3);  // Converter, Buffer, Flags
440
441
  ConverterObject* converter;
442
6830
  ASSIGN_OR_RETURN_UNWRAP(&converter, args[0].As<Object>());
443
2309
  ArrayBufferViewContents<char> input(args[1]);
444
2309
  int flags = args[2]->Uint32Value(env->context()).ToChecked();
445
446
2309
  UErrorCode status = U_ZERO_ERROR;
447
2309
  MaybeStackBuffer<UChar> result;
448
  MaybeLocal<Object> ret;
449
450
2309
  UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH;
451
452
  // When flushing the final chunk, the limit is the maximum
453
  // of either the input buffer length or the number of pending
454
  // characters times the min char size, multiplied by 2 as unicode may
455
  // take up to 2 UChars to encode a character
456
2309
  size_t limit = 2 * converter->min_char_size() *
457
3720
      (!flush ?
458
898
          input.length() :
459
          std::max(
460
5131
              input.length(),
461
1411
              static_cast<size_t>(
462
2822
                  ucnv_toUCountPending(converter->conv(), &status))));
463
2309
  status = U_ZERO_ERROR;
464
465
2309
  if (limit > 0)
466
2093
    result.AllocateSufficientStorage(limit);
467
468
2309
  auto cleanup = OnScopeLeave([&]() {
469
2309
    if (flush) {
470
      // Reset the converter state.
471
1411
      converter->set_bom_seen(false);
472
1411
      converter->reset();
473
    }
474
2309
  });
475
476
2309
  const char* source = input.data();
477
2309
  size_t source_length = input.length();
478
479
2309
  UChar* target = *result;
480
2309
  ucnv_toUnicode(converter->conv(),
481
                 &target,
482
2309
                 target + limit,
483
                 &source,
484
                 source + source_length,
485
                 nullptr,
486
                 flush,
487
                 &status);
488
489
2309
  if (U_SUCCESS(status)) {
490
2212
    bool omit_initial_bom = false;
491
2212
    if (limit > 0) {
492
1996
      result.SetLength(target - &result[0]);
493
3788
      if (result.length() > 0 &&
494
1792
          converter->unicode() &&
495

5474
          !converter->ignore_bom() &&
496
1686
          !converter->bom_seen()) {
497
        // If the very first result in the stream is a BOM, and we are not
498
        // explicitly told to ignore it, then we mark it for discarding.
499
1199
        if (result[0] == 0xFEFF)
500
42
          omit_initial_bom = true;
501
1199
        converter->set_bom_seen(true);
502
      }
503
    }
504
2212
    ret = ToBufferEndian(env, &result);
505

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


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

1235940
  switch (eaw) {
762
68786
    case U_EA_FULLWIDTH:
763
    case U_EA_WIDE:
764
68786
      return 2;
765
1012414
    case U_EA_AMBIGUOUS:
766
      // See: http://www.unicode.org/reports/tr11/#Ambiguous for details
767
1012414
      if (ambiguous_as_full_width) {
768
        return 2;
769
      }
770
      // If ambiguous_as_full_width is false:
771
      // Fall through
772
    case U_EA_NEUTRAL:
773
1142237
      if (u_hasBinaryProperty(codepoint, UCHAR_EMOJI_PRESENTATION)) {
774
        return 2;
775
      }
776
      // Fall through
777
    case U_EA_HALFWIDTH:
778
    case U_EA_NARROW:
779
    default:
780
1167154
      const auto zero_width_mask = U_GC_CC_MASK |  // C0/C1 control code
781
                                  U_GC_CF_MASK |  // Format control character
782
                                  U_GC_ME_MASK |  // Enclosing mark
783
                                  U_GC_MN_MASK;   // Nonspacing mark
784

2334268
      if (codepoint != 0x00AD &&  // SOFT HYPHEN is Cf but not zero-width
785
1328956
          ((U_MASK(u_charType(codepoint)) & zero_width_mask) ||
786
1167114
          u_hasBinaryProperty(codepoint, UCHAR_EMOJI_MODIFIER))) {
787
1005272
        return 0;
788
      }
789
161882
      return 1;
790
  }
791
}
792
793
// Returns the column width for the given String.
794
1196092
static void GetStringWidth(const FunctionCallbackInfo<Value>& args) {
795
1196092
  Environment* env = Environment::GetCurrent(args);
796
2392184
  CHECK(args[0]->IsString());
797
798
1196092
  bool ambiguous_as_full_width = args[1]->IsTrue();
799

1196092
  bool expand_emoji_sequence = !args[2]->IsBoolean() || args[2]->IsTrue();
800
801
1196092
  TwoByteValue value(env->isolate(), args[0]);
802
  // reinterpret_cast is required by windows to compile
803
1196092
  UChar* str = reinterpret_cast<UChar*>(*value);
804
  static_assert(sizeof(*str) == sizeof(**value),
805
                "sizeof(*str) == sizeof(**value)");
806
1196092
  UChar32 c = 0;
807
  UChar32 p;
808
1196092
  size_t n = 0;
809
1196092
  uint32_t width = 0;
810
811
2432032
  while (n < value.length()) {
812
1235940
    p = c;
813


1235940
    U16_NEXT(str, n, value.length(), c);
814
    // Don't count individual emoji codepoints that occur within an
815
    // emoji sequence. This is not necessarily foolproof. Some
816
    // environments display emoji sequences in the appropriate
817
    // condensed form (as a single emoji glyph), other environments
818
    // may not understand an emoji sequence and will display each
819
    // individual emoji separately. When this happens, the width
820
    // calculated will be off, and there's no reliable way of knowing
821
    // in advance if a particular sequence is going to be supported.
822
    // The expand_emoji_sequence option allows the caller to skip this
823
    // check and count each code within an emoji sequence separately.
824
    // https://www.unicode.org/reports/tr51/tr51-16.html#Emoji_ZWJ_Sequences
825
2471880
    if (!expand_emoji_sequence &&
826


1235940
        n > 0 && p == 0x200d &&  // 0x200d == ZWJ (zero width joiner)
827
        (u_hasBinaryProperty(c, UCHAR_EMOJI_PRESENTATION) ||
828
         u_hasBinaryProperty(c, UCHAR_EMOJI_MODIFIER))) {
829
      continue;
830
    }
831
1235940
    width += GetColumnWidth(c, ambiguous_as_full_width);
832
  }
833
2392184
  args.GetReturnValue().Set(width);
834
1196092
}
835
836
762
void Initialize(Local<Object> target,
837
                Local<Value> unused,
838
                Local<Context> context,
839
                void* priv) {
840
762
  Environment* env = Environment::GetCurrent(context);
841
762
  SetMethod(context, target, "toUnicode", ToUnicode);
842
762
  SetMethod(context, target, "toASCII", ToASCII);
843
762
  SetMethod(context, target, "getStringWidth", GetStringWidth);
844
845
  // One-shot converters
846
762
  SetMethod(context, target, "icuErrName", ICUErrorName);
847
762
  SetMethod(context, target, "transcode", Transcode);
848
849
  // ConverterObject
850
  {
851
762
    Local<FunctionTemplate> t = NewFunctionTemplate(env->isolate(), nullptr);
852
762
    t->Inherit(BaseObject::GetConstructorTemplate(env));
853
1524
    t->InstanceTemplate()->SetInternalFieldCount(
854
        ConverterObject::kInternalFieldCount);
855
    Local<String> converter_string =
856
762
        FIXED_ONE_BYTE_STRING(env->isolate(), "Converter");
857
762
    t->SetClassName(converter_string);
858
762
    env->set_i18n_converter_template(t->InstanceTemplate());
859
  }
860
861
762
  SetMethod(context, target, "getConverter", ConverterObject::Create);
862
762
  SetMethod(context, target, "decode", ConverterObject::Decode);
863
762
  SetMethod(context, target, "hasConverter", ConverterObject::Has);
864
762
}
865
866
5259
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
867
5259
  registry->Register(ToUnicode);
868
5259
  registry->Register(ToASCII);
869
5259
  registry->Register(GetStringWidth);
870
5259
  registry->Register(ICUErrorName);
871
5259
  registry->Register(Transcode);
872
5259
  registry->Register(ConverterObject::Create);
873
5259
  registry->Register(ConverterObject::Decode);
874
5259
  registry->Register(ConverterObject::Has);
875
5259
}
876
877
}  // namespace i18n
878
}  // namespace node
879
880
5321
NODE_MODULE_CONTEXT_AWARE_INTERNAL(icu, node::i18n::Initialize)
881
5259
NODE_MODULE_EXTERNAL_REFERENCE(icu, node::i18n::RegisterExternalReferences)
882
883
#endif  // NODE_HAVE_I18N_SUPPORT