11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// An implementation of the WHATWG Encoding Standard
41cb0ef41Sopenharmony_ci// https://encoding.spec.whatwg.org
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst {
71cb0ef41Sopenharmony_ci  Boolean,
81cb0ef41Sopenharmony_ci  ObjectCreate,
91cb0ef41Sopenharmony_ci  ObjectDefineProperties,
101cb0ef41Sopenharmony_ci  ObjectGetOwnPropertyDescriptors,
111cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf,
121cb0ef41Sopenharmony_ci  ObjectValues,
131cb0ef41Sopenharmony_ci  SafeMap,
141cb0ef41Sopenharmony_ci  StringPrototypeSlice,
151cb0ef41Sopenharmony_ci  Symbol,
161cb0ef41Sopenharmony_ci  SymbolToStringTag,
171cb0ef41Sopenharmony_ci  Uint32Array,
181cb0ef41Sopenharmony_ci  Uint8Array,
191cb0ef41Sopenharmony_ci} = primordials;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst {
221cb0ef41Sopenharmony_ci  ERR_ENCODING_NOT_SUPPORTED,
231cb0ef41Sopenharmony_ci  ERR_INVALID_ARG_TYPE,
241cb0ef41Sopenharmony_ci  ERR_INVALID_THIS,
251cb0ef41Sopenharmony_ci  ERR_NO_ICU,
261cb0ef41Sopenharmony_ci} = require('internal/errors').codes;
271cb0ef41Sopenharmony_ciconst kHandle = Symbol('handle');
281cb0ef41Sopenharmony_ciconst kFlags = Symbol('flags');
291cb0ef41Sopenharmony_ciconst kEncoding = Symbol('encoding');
301cb0ef41Sopenharmony_ciconst kDecoder = Symbol('decoder');
311cb0ef41Sopenharmony_ciconst kEncoder = Symbol('encoder');
321cb0ef41Sopenharmony_ciconst kFatal = Symbol('kFatal');
331cb0ef41Sopenharmony_ciconst kUTF8FastPath = Symbol('kUTF8FastPath');
341cb0ef41Sopenharmony_ciconst kIgnoreBOM = Symbol('kIgnoreBOM');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst {
371cb0ef41Sopenharmony_ci  getConstructorOf,
381cb0ef41Sopenharmony_ci  customInspectSymbol: inspect,
391cb0ef41Sopenharmony_ci  kEmptyObject,
401cb0ef41Sopenharmony_ci  kEnumerableProperty,
411cb0ef41Sopenharmony_ci} = require('internal/util');
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciconst {
441cb0ef41Sopenharmony_ci  isAnyArrayBuffer,
451cb0ef41Sopenharmony_ci  isArrayBufferView,
461cb0ef41Sopenharmony_ci  isUint8Array,
471cb0ef41Sopenharmony_ci} = require('internal/util/types');
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciconst {
501cb0ef41Sopenharmony_ci  validateString,
511cb0ef41Sopenharmony_ci  validateObject,
521cb0ef41Sopenharmony_ci} = require('internal/validators');
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciconst {
551cb0ef41Sopenharmony_ci  encodeInto,
561cb0ef41Sopenharmony_ci  encodeUtf8String,
571cb0ef41Sopenharmony_ci  decodeUTF8,
581cb0ef41Sopenharmony_ci} = internalBinding('buffer');
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_cilet Buffer;
611cb0ef41Sopenharmony_cifunction lazyBuffer() {
621cb0ef41Sopenharmony_ci  if (Buffer === undefined)
631cb0ef41Sopenharmony_ci    Buffer = require('buffer').Buffer;
641cb0ef41Sopenharmony_ci  return Buffer;
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_cifunction validateEncoder(obj) {
681cb0ef41Sopenharmony_ci  if (obj == null || obj[kEncoder] !== true)
691cb0ef41Sopenharmony_ci    throw new ERR_INVALID_THIS('TextEncoder');
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cifunction validateDecoder(obj) {
731cb0ef41Sopenharmony_ci  if (obj == null || obj[kDecoder] !== true)
741cb0ef41Sopenharmony_ci    throw new ERR_INVALID_THIS('TextDecoder');
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciconst CONVERTER_FLAGS_FLUSH = 0x1;
781cb0ef41Sopenharmony_ciconst CONVERTER_FLAGS_FATAL = 0x2;
791cb0ef41Sopenharmony_ciconst CONVERTER_FLAGS_IGNORE_BOM = 0x4;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciconst empty = new Uint8Array(0);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ciconst encodings = new SafeMap([
841cb0ef41Sopenharmony_ci  ['unicode-1-1-utf-8', 'utf-8'],
851cb0ef41Sopenharmony_ci  ['utf8', 'utf-8'],
861cb0ef41Sopenharmony_ci  ['utf-8', 'utf-8'],
871cb0ef41Sopenharmony_ci  ['866', 'ibm866'],
881cb0ef41Sopenharmony_ci  ['cp866', 'ibm866'],
891cb0ef41Sopenharmony_ci  ['csibm866', 'ibm866'],
901cb0ef41Sopenharmony_ci  ['ibm866', 'ibm866'],
911cb0ef41Sopenharmony_ci  ['csisolatin2', 'iso-8859-2'],
921cb0ef41Sopenharmony_ci  ['iso-8859-2', 'iso-8859-2'],
931cb0ef41Sopenharmony_ci  ['iso-ir-101', 'iso-8859-2'],
941cb0ef41Sopenharmony_ci  ['iso8859-2', 'iso-8859-2'],
951cb0ef41Sopenharmony_ci  ['iso88592', 'iso-8859-2'],
961cb0ef41Sopenharmony_ci  ['iso_8859-2', 'iso-8859-2'],
971cb0ef41Sopenharmony_ci  ['iso_8859-2:1987', 'iso-8859-2'],
981cb0ef41Sopenharmony_ci  ['l2', 'iso-8859-2'],
991cb0ef41Sopenharmony_ci  ['latin2', 'iso-8859-2'],
1001cb0ef41Sopenharmony_ci  ['csisolatin3', 'iso-8859-3'],
1011cb0ef41Sopenharmony_ci  ['iso-8859-3', 'iso-8859-3'],
1021cb0ef41Sopenharmony_ci  ['iso-ir-109', 'iso-8859-3'],
1031cb0ef41Sopenharmony_ci  ['iso8859-3', 'iso-8859-3'],
1041cb0ef41Sopenharmony_ci  ['iso88593', 'iso-8859-3'],
1051cb0ef41Sopenharmony_ci  ['iso_8859-3', 'iso-8859-3'],
1061cb0ef41Sopenharmony_ci  ['iso_8859-3:1988', 'iso-8859-3'],
1071cb0ef41Sopenharmony_ci  ['l3', 'iso-8859-3'],
1081cb0ef41Sopenharmony_ci  ['latin3', 'iso-8859-3'],
1091cb0ef41Sopenharmony_ci  ['csisolatin4', 'iso-8859-4'],
1101cb0ef41Sopenharmony_ci  ['iso-8859-4', 'iso-8859-4'],
1111cb0ef41Sopenharmony_ci  ['iso-ir-110', 'iso-8859-4'],
1121cb0ef41Sopenharmony_ci  ['iso8859-4', 'iso-8859-4'],
1131cb0ef41Sopenharmony_ci  ['iso88594', 'iso-8859-4'],
1141cb0ef41Sopenharmony_ci  ['iso_8859-4', 'iso-8859-4'],
1151cb0ef41Sopenharmony_ci  ['iso_8859-4:1988', 'iso-8859-4'],
1161cb0ef41Sopenharmony_ci  ['l4', 'iso-8859-4'],
1171cb0ef41Sopenharmony_ci  ['latin4', 'iso-8859-4'],
1181cb0ef41Sopenharmony_ci  ['csisolatincyrillic', 'iso-8859-5'],
1191cb0ef41Sopenharmony_ci  ['cyrillic', 'iso-8859-5'],
1201cb0ef41Sopenharmony_ci  ['iso-8859-5', 'iso-8859-5'],
1211cb0ef41Sopenharmony_ci  ['iso-ir-144', 'iso-8859-5'],
1221cb0ef41Sopenharmony_ci  ['iso8859-5', 'iso-8859-5'],
1231cb0ef41Sopenharmony_ci  ['iso88595', 'iso-8859-5'],
1241cb0ef41Sopenharmony_ci  ['iso_8859-5', 'iso-8859-5'],
1251cb0ef41Sopenharmony_ci  ['iso_8859-5:1988', 'iso-8859-5'],
1261cb0ef41Sopenharmony_ci  ['arabic', 'iso-8859-6'],
1271cb0ef41Sopenharmony_ci  ['asmo-708', 'iso-8859-6'],
1281cb0ef41Sopenharmony_ci  ['csiso88596e', 'iso-8859-6'],
1291cb0ef41Sopenharmony_ci  ['csiso88596i', 'iso-8859-6'],
1301cb0ef41Sopenharmony_ci  ['csisolatinarabic', 'iso-8859-6'],
1311cb0ef41Sopenharmony_ci  ['ecma-114', 'iso-8859-6'],
1321cb0ef41Sopenharmony_ci  ['iso-8859-6', 'iso-8859-6'],
1331cb0ef41Sopenharmony_ci  ['iso-8859-6-e', 'iso-8859-6'],
1341cb0ef41Sopenharmony_ci  ['iso-8859-6-i', 'iso-8859-6'],
1351cb0ef41Sopenharmony_ci  ['iso-ir-127', 'iso-8859-6'],
1361cb0ef41Sopenharmony_ci  ['iso8859-6', 'iso-8859-6'],
1371cb0ef41Sopenharmony_ci  ['iso88596', 'iso-8859-6'],
1381cb0ef41Sopenharmony_ci  ['iso_8859-6', 'iso-8859-6'],
1391cb0ef41Sopenharmony_ci  ['iso_8859-6:1987', 'iso-8859-6'],
1401cb0ef41Sopenharmony_ci  ['csisolatingreek', 'iso-8859-7'],
1411cb0ef41Sopenharmony_ci  ['ecma-118', 'iso-8859-7'],
1421cb0ef41Sopenharmony_ci  ['elot_928', 'iso-8859-7'],
1431cb0ef41Sopenharmony_ci  ['greek', 'iso-8859-7'],
1441cb0ef41Sopenharmony_ci  ['greek8', 'iso-8859-7'],
1451cb0ef41Sopenharmony_ci  ['iso-8859-7', 'iso-8859-7'],
1461cb0ef41Sopenharmony_ci  ['iso-ir-126', 'iso-8859-7'],
1471cb0ef41Sopenharmony_ci  ['iso8859-7', 'iso-8859-7'],
1481cb0ef41Sopenharmony_ci  ['iso88597', 'iso-8859-7'],
1491cb0ef41Sopenharmony_ci  ['iso_8859-7', 'iso-8859-7'],
1501cb0ef41Sopenharmony_ci  ['iso_8859-7:1987', 'iso-8859-7'],
1511cb0ef41Sopenharmony_ci  ['sun_eu_greek', 'iso-8859-7'],
1521cb0ef41Sopenharmony_ci  ['csiso88598e', 'iso-8859-8'],
1531cb0ef41Sopenharmony_ci  ['csisolatinhebrew', 'iso-8859-8'],
1541cb0ef41Sopenharmony_ci  ['hebrew', 'iso-8859-8'],
1551cb0ef41Sopenharmony_ci  ['iso-8859-8', 'iso-8859-8'],
1561cb0ef41Sopenharmony_ci  ['iso-8859-8-e', 'iso-8859-8'],
1571cb0ef41Sopenharmony_ci  ['iso-ir-138', 'iso-8859-8'],
1581cb0ef41Sopenharmony_ci  ['iso8859-8', 'iso-8859-8'],
1591cb0ef41Sopenharmony_ci  ['iso88598', 'iso-8859-8'],
1601cb0ef41Sopenharmony_ci  ['iso_8859-8', 'iso-8859-8'],
1611cb0ef41Sopenharmony_ci  ['iso_8859-8:1988', 'iso-8859-8'],
1621cb0ef41Sopenharmony_ci  ['visual', 'iso-8859-8'],
1631cb0ef41Sopenharmony_ci  ['csiso88598i', 'iso-8859-8-i'],
1641cb0ef41Sopenharmony_ci  ['iso-8859-8-i', 'iso-8859-8-i'],
1651cb0ef41Sopenharmony_ci  ['logical', 'iso-8859-8-i'],
1661cb0ef41Sopenharmony_ci  ['csisolatin6', 'iso-8859-10'],
1671cb0ef41Sopenharmony_ci  ['iso-8859-10', 'iso-8859-10'],
1681cb0ef41Sopenharmony_ci  ['iso-ir-157', 'iso-8859-10'],
1691cb0ef41Sopenharmony_ci  ['iso8859-10', 'iso-8859-10'],
1701cb0ef41Sopenharmony_ci  ['iso885910', 'iso-8859-10'],
1711cb0ef41Sopenharmony_ci  ['l6', 'iso-8859-10'],
1721cb0ef41Sopenharmony_ci  ['latin6', 'iso-8859-10'],
1731cb0ef41Sopenharmony_ci  ['iso-8859-13', 'iso-8859-13'],
1741cb0ef41Sopenharmony_ci  ['iso8859-13', 'iso-8859-13'],
1751cb0ef41Sopenharmony_ci  ['iso885913', 'iso-8859-13'],
1761cb0ef41Sopenharmony_ci  ['iso-8859-14', 'iso-8859-14'],
1771cb0ef41Sopenharmony_ci  ['iso8859-14', 'iso-8859-14'],
1781cb0ef41Sopenharmony_ci  ['iso885914', 'iso-8859-14'],
1791cb0ef41Sopenharmony_ci  ['csisolatin9', 'iso-8859-15'],
1801cb0ef41Sopenharmony_ci  ['iso-8859-15', 'iso-8859-15'],
1811cb0ef41Sopenharmony_ci  ['iso8859-15', 'iso-8859-15'],
1821cb0ef41Sopenharmony_ci  ['iso885915', 'iso-8859-15'],
1831cb0ef41Sopenharmony_ci  ['iso_8859-15', 'iso-8859-15'],
1841cb0ef41Sopenharmony_ci  ['l9', 'iso-8859-15'],
1851cb0ef41Sopenharmony_ci  ['cskoi8r', 'koi8-r'],
1861cb0ef41Sopenharmony_ci  ['koi', 'koi8-r'],
1871cb0ef41Sopenharmony_ci  ['koi8', 'koi8-r'],
1881cb0ef41Sopenharmony_ci  ['koi8-r', 'koi8-r'],
1891cb0ef41Sopenharmony_ci  ['koi8_r', 'koi8-r'],
1901cb0ef41Sopenharmony_ci  ['koi8-ru', 'koi8-u'],
1911cb0ef41Sopenharmony_ci  ['koi8-u', 'koi8-u'],
1921cb0ef41Sopenharmony_ci  ['csmacintosh', 'macintosh'],
1931cb0ef41Sopenharmony_ci  ['mac', 'macintosh'],
1941cb0ef41Sopenharmony_ci  ['macintosh', 'macintosh'],
1951cb0ef41Sopenharmony_ci  ['x-mac-roman', 'macintosh'],
1961cb0ef41Sopenharmony_ci  ['dos-874', 'windows-874'],
1971cb0ef41Sopenharmony_ci  ['iso-8859-11', 'windows-874'],
1981cb0ef41Sopenharmony_ci  ['iso8859-11', 'windows-874'],
1991cb0ef41Sopenharmony_ci  ['iso885911', 'windows-874'],
2001cb0ef41Sopenharmony_ci  ['tis-620', 'windows-874'],
2011cb0ef41Sopenharmony_ci  ['windows-874', 'windows-874'],
2021cb0ef41Sopenharmony_ci  ['cp1250', 'windows-1250'],
2031cb0ef41Sopenharmony_ci  ['windows-1250', 'windows-1250'],
2041cb0ef41Sopenharmony_ci  ['x-cp1250', 'windows-1250'],
2051cb0ef41Sopenharmony_ci  ['cp1251', 'windows-1251'],
2061cb0ef41Sopenharmony_ci  ['windows-1251', 'windows-1251'],
2071cb0ef41Sopenharmony_ci  ['x-cp1251', 'windows-1251'],
2081cb0ef41Sopenharmony_ci  ['ansi_x3.4-1968', 'windows-1252'],
2091cb0ef41Sopenharmony_ci  ['ascii', 'windows-1252'],
2101cb0ef41Sopenharmony_ci  ['cp1252', 'windows-1252'],
2111cb0ef41Sopenharmony_ci  ['cp819', 'windows-1252'],
2121cb0ef41Sopenharmony_ci  ['csisolatin1', 'windows-1252'],
2131cb0ef41Sopenharmony_ci  ['ibm819', 'windows-1252'],
2141cb0ef41Sopenharmony_ci  ['iso-8859-1', 'windows-1252'],
2151cb0ef41Sopenharmony_ci  ['iso-ir-100', 'windows-1252'],
2161cb0ef41Sopenharmony_ci  ['iso8859-1', 'windows-1252'],
2171cb0ef41Sopenharmony_ci  ['iso88591', 'windows-1252'],
2181cb0ef41Sopenharmony_ci  ['iso_8859-1', 'windows-1252'],
2191cb0ef41Sopenharmony_ci  ['iso_8859-1:1987', 'windows-1252'],
2201cb0ef41Sopenharmony_ci  ['l1', 'windows-1252'],
2211cb0ef41Sopenharmony_ci  ['latin1', 'windows-1252'],
2221cb0ef41Sopenharmony_ci  ['us-ascii', 'windows-1252'],
2231cb0ef41Sopenharmony_ci  ['windows-1252', 'windows-1252'],
2241cb0ef41Sopenharmony_ci  ['x-cp1252', 'windows-1252'],
2251cb0ef41Sopenharmony_ci  ['cp1253', 'windows-1253'],
2261cb0ef41Sopenharmony_ci  ['windows-1253', 'windows-1253'],
2271cb0ef41Sopenharmony_ci  ['x-cp1253', 'windows-1253'],
2281cb0ef41Sopenharmony_ci  ['cp1254', 'windows-1254'],
2291cb0ef41Sopenharmony_ci  ['csisolatin5', 'windows-1254'],
2301cb0ef41Sopenharmony_ci  ['iso-8859-9', 'windows-1254'],
2311cb0ef41Sopenharmony_ci  ['iso-ir-148', 'windows-1254'],
2321cb0ef41Sopenharmony_ci  ['iso8859-9', 'windows-1254'],
2331cb0ef41Sopenharmony_ci  ['iso88599', 'windows-1254'],
2341cb0ef41Sopenharmony_ci  ['iso_8859-9', 'windows-1254'],
2351cb0ef41Sopenharmony_ci  ['iso_8859-9:1989', 'windows-1254'],
2361cb0ef41Sopenharmony_ci  ['l5', 'windows-1254'],
2371cb0ef41Sopenharmony_ci  ['latin5', 'windows-1254'],
2381cb0ef41Sopenharmony_ci  ['windows-1254', 'windows-1254'],
2391cb0ef41Sopenharmony_ci  ['x-cp1254', 'windows-1254'],
2401cb0ef41Sopenharmony_ci  ['cp1255', 'windows-1255'],
2411cb0ef41Sopenharmony_ci  ['windows-1255', 'windows-1255'],
2421cb0ef41Sopenharmony_ci  ['x-cp1255', 'windows-1255'],
2431cb0ef41Sopenharmony_ci  ['cp1256', 'windows-1256'],
2441cb0ef41Sopenharmony_ci  ['windows-1256', 'windows-1256'],
2451cb0ef41Sopenharmony_ci  ['x-cp1256', 'windows-1256'],
2461cb0ef41Sopenharmony_ci  ['cp1257', 'windows-1257'],
2471cb0ef41Sopenharmony_ci  ['windows-1257', 'windows-1257'],
2481cb0ef41Sopenharmony_ci  ['x-cp1257', 'windows-1257'],
2491cb0ef41Sopenharmony_ci  ['cp1258', 'windows-1258'],
2501cb0ef41Sopenharmony_ci  ['windows-1258', 'windows-1258'],
2511cb0ef41Sopenharmony_ci  ['x-cp1258', 'windows-1258'],
2521cb0ef41Sopenharmony_ci  ['x-mac-cyrillic', 'x-mac-cyrillic'],
2531cb0ef41Sopenharmony_ci  ['x-mac-ukrainian', 'x-mac-cyrillic'],
2541cb0ef41Sopenharmony_ci  ['chinese', 'gbk'],
2551cb0ef41Sopenharmony_ci  ['csgb2312', 'gbk'],
2561cb0ef41Sopenharmony_ci  ['csiso58gb231280', 'gbk'],
2571cb0ef41Sopenharmony_ci  ['gb2312', 'gbk'],
2581cb0ef41Sopenharmony_ci  ['gb_2312', 'gbk'],
2591cb0ef41Sopenharmony_ci  ['gb_2312-80', 'gbk'],
2601cb0ef41Sopenharmony_ci  ['gbk', 'gbk'],
2611cb0ef41Sopenharmony_ci  ['iso-ir-58', 'gbk'],
2621cb0ef41Sopenharmony_ci  ['x-gbk', 'gbk'],
2631cb0ef41Sopenharmony_ci  ['gb18030', 'gb18030'],
2641cb0ef41Sopenharmony_ci  ['big5', 'big5'],
2651cb0ef41Sopenharmony_ci  ['big5-hkscs', 'big5'],
2661cb0ef41Sopenharmony_ci  ['cn-big5', 'big5'],
2671cb0ef41Sopenharmony_ci  ['csbig5', 'big5'],
2681cb0ef41Sopenharmony_ci  ['x-x-big5', 'big5'],
2691cb0ef41Sopenharmony_ci  ['cseucpkdfmtjapanese', 'euc-jp'],
2701cb0ef41Sopenharmony_ci  ['euc-jp', 'euc-jp'],
2711cb0ef41Sopenharmony_ci  ['x-euc-jp', 'euc-jp'],
2721cb0ef41Sopenharmony_ci  ['csiso2022jp', 'iso-2022-jp'],
2731cb0ef41Sopenharmony_ci  ['iso-2022-jp', 'iso-2022-jp'],
2741cb0ef41Sopenharmony_ci  ['csshiftjis', 'shift_jis'],
2751cb0ef41Sopenharmony_ci  ['ms932', 'shift_jis'],
2761cb0ef41Sopenharmony_ci  ['ms_kanji', 'shift_jis'],
2771cb0ef41Sopenharmony_ci  ['shift-jis', 'shift_jis'],
2781cb0ef41Sopenharmony_ci  ['shift_jis', 'shift_jis'],
2791cb0ef41Sopenharmony_ci  ['sjis', 'shift_jis'],
2801cb0ef41Sopenharmony_ci  ['windows-31j', 'shift_jis'],
2811cb0ef41Sopenharmony_ci  ['x-sjis', 'shift_jis'],
2821cb0ef41Sopenharmony_ci  ['cseuckr', 'euc-kr'],
2831cb0ef41Sopenharmony_ci  ['csksc56011987', 'euc-kr'],
2841cb0ef41Sopenharmony_ci  ['euc-kr', 'euc-kr'],
2851cb0ef41Sopenharmony_ci  ['iso-ir-149', 'euc-kr'],
2861cb0ef41Sopenharmony_ci  ['korean', 'euc-kr'],
2871cb0ef41Sopenharmony_ci  ['ks_c_5601-1987', 'euc-kr'],
2881cb0ef41Sopenharmony_ci  ['ks_c_5601-1989', 'euc-kr'],
2891cb0ef41Sopenharmony_ci  ['ksc5601', 'euc-kr'],
2901cb0ef41Sopenharmony_ci  ['ksc_5601', 'euc-kr'],
2911cb0ef41Sopenharmony_ci  ['windows-949', 'euc-kr'],
2921cb0ef41Sopenharmony_ci  ['utf-16be', 'utf-16be'],
2931cb0ef41Sopenharmony_ci  ['utf-16le', 'utf-16le'],
2941cb0ef41Sopenharmony_ci  ['utf-16', 'utf-16le'],
2951cb0ef41Sopenharmony_ci]);
2961cb0ef41Sopenharmony_ci
2971cb0ef41Sopenharmony_ci// Unfortunately, String.prototype.trim also removes non-ascii whitespace,
2981cb0ef41Sopenharmony_ci// so we have to do this manually
2991cb0ef41Sopenharmony_cifunction trimAsciiWhitespace(label) {
3001cb0ef41Sopenharmony_ci  let s = 0;
3011cb0ef41Sopenharmony_ci  let e = label.length;
3021cb0ef41Sopenharmony_ci  while (s < e && (
3031cb0ef41Sopenharmony_ci    label[s] === '\u0009' ||
3041cb0ef41Sopenharmony_ci    label[s] === '\u000a' ||
3051cb0ef41Sopenharmony_ci    label[s] === '\u000c' ||
3061cb0ef41Sopenharmony_ci    label[s] === '\u000d' ||
3071cb0ef41Sopenharmony_ci    label[s] === '\u0020')) {
3081cb0ef41Sopenharmony_ci    s++;
3091cb0ef41Sopenharmony_ci  }
3101cb0ef41Sopenharmony_ci  while (e > s && (
3111cb0ef41Sopenharmony_ci    label[e - 1] === '\u0009' ||
3121cb0ef41Sopenharmony_ci    label[e - 1] === '\u000a' ||
3131cb0ef41Sopenharmony_ci    label[e - 1] === '\u000c' ||
3141cb0ef41Sopenharmony_ci    label[e - 1] === '\u000d' ||
3151cb0ef41Sopenharmony_ci    label[e - 1] === '\u0020')) {
3161cb0ef41Sopenharmony_ci    e--;
3171cb0ef41Sopenharmony_ci  }
3181cb0ef41Sopenharmony_ci  return StringPrototypeSlice(label, s, e);
3191cb0ef41Sopenharmony_ci}
3201cb0ef41Sopenharmony_ci
3211cb0ef41Sopenharmony_cifunction getEncodingFromLabel(label) {
3221cb0ef41Sopenharmony_ci  const enc = encodings.get(label);
3231cb0ef41Sopenharmony_ci  if (enc !== undefined) return enc;
3241cb0ef41Sopenharmony_ci  return encodings.get(trimAsciiWhitespace(label.toLowerCase()));
3251cb0ef41Sopenharmony_ci}
3261cb0ef41Sopenharmony_ci
3271cb0ef41Sopenharmony_ciconst encodeIntoResults = new Uint32Array(2);
3281cb0ef41Sopenharmony_ci
3291cb0ef41Sopenharmony_ciclass TextEncoder {
3301cb0ef41Sopenharmony_ci  constructor() {
3311cb0ef41Sopenharmony_ci    this[kEncoder] = true;
3321cb0ef41Sopenharmony_ci  }
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ci  get encoding() {
3351cb0ef41Sopenharmony_ci    validateEncoder(this);
3361cb0ef41Sopenharmony_ci    return 'utf-8';
3371cb0ef41Sopenharmony_ci  }
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ci  encode(input = '') {
3401cb0ef41Sopenharmony_ci    validateEncoder(this);
3411cb0ef41Sopenharmony_ci    return encodeUtf8String(`${input}`);
3421cb0ef41Sopenharmony_ci  }
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci  encodeInto(src, dest) {
3451cb0ef41Sopenharmony_ci    validateEncoder(this);
3461cb0ef41Sopenharmony_ci    validateString(src, 'src');
3471cb0ef41Sopenharmony_ci    if (!dest || !isUint8Array(dest))
3481cb0ef41Sopenharmony_ci      throw new ERR_INVALID_ARG_TYPE('dest', 'Uint8Array', dest);
3491cb0ef41Sopenharmony_ci    encodeInto(src, dest, encodeIntoResults);
3501cb0ef41Sopenharmony_ci    return { read: encodeIntoResults[0], written: encodeIntoResults[1] };
3511cb0ef41Sopenharmony_ci  }
3521cb0ef41Sopenharmony_ci
3531cb0ef41Sopenharmony_ci  [inspect](depth, opts) {
3541cb0ef41Sopenharmony_ci    validateEncoder(this);
3551cb0ef41Sopenharmony_ci    if (typeof depth === 'number' && depth < 0)
3561cb0ef41Sopenharmony_ci      return this;
3571cb0ef41Sopenharmony_ci    const ctor = getConstructorOf(this);
3581cb0ef41Sopenharmony_ci    const obj = ObjectCreate({
3591cb0ef41Sopenharmony_ci      constructor: ctor === null ? TextEncoder : ctor,
3601cb0ef41Sopenharmony_ci    });
3611cb0ef41Sopenharmony_ci    obj.encoding = this.encoding;
3621cb0ef41Sopenharmony_ci    // Lazy to avoid circular dependency
3631cb0ef41Sopenharmony_ci    return require('internal/util/inspect').inspect(obj, opts);
3641cb0ef41Sopenharmony_ci  }
3651cb0ef41Sopenharmony_ci}
3661cb0ef41Sopenharmony_ci
3671cb0ef41Sopenharmony_ciObjectDefineProperties(
3681cb0ef41Sopenharmony_ci  TextEncoder.prototype, {
3691cb0ef41Sopenharmony_ci    'encode': kEnumerableProperty,
3701cb0ef41Sopenharmony_ci    'encodeInto': kEnumerableProperty,
3711cb0ef41Sopenharmony_ci    'encoding': kEnumerableProperty,
3721cb0ef41Sopenharmony_ci    [SymbolToStringTag]: { __proto__: null, configurable: true, value: 'TextEncoder' },
3731cb0ef41Sopenharmony_ci  });
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ciconst TextDecoder =
3761cb0ef41Sopenharmony_ci  internalBinding('config').hasIntl ?
3771cb0ef41Sopenharmony_ci    makeTextDecoderICU() :
3781cb0ef41Sopenharmony_ci    makeTextDecoderJS();
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_cifunction makeTextDecoderICU() {
3811cb0ef41Sopenharmony_ci  const {
3821cb0ef41Sopenharmony_ci    decode: _decode,
3831cb0ef41Sopenharmony_ci    getConverter,
3841cb0ef41Sopenharmony_ci  } = internalBinding('icu');
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ci  class TextDecoder {
3871cb0ef41Sopenharmony_ci    constructor(encoding = 'utf-8', options = kEmptyObject) {
3881cb0ef41Sopenharmony_ci      encoding = `${encoding}`;
3891cb0ef41Sopenharmony_ci      validateObject(options, 'options', {
3901cb0ef41Sopenharmony_ci        nullable: true,
3911cb0ef41Sopenharmony_ci        allowArray: true,
3921cb0ef41Sopenharmony_ci        allowFunction: true,
3931cb0ef41Sopenharmony_ci      });
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci      const enc = getEncodingFromLabel(encoding);
3961cb0ef41Sopenharmony_ci      if (enc === undefined)
3971cb0ef41Sopenharmony_ci        throw new ERR_ENCODING_NOT_SUPPORTED(encoding);
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_ci      let flags = 0;
4001cb0ef41Sopenharmony_ci      if (options !== null) {
4011cb0ef41Sopenharmony_ci        flags |= options.fatal ? CONVERTER_FLAGS_FATAL : 0;
4021cb0ef41Sopenharmony_ci        flags |= options.ignoreBOM ? CONVERTER_FLAGS_IGNORE_BOM : 0;
4031cb0ef41Sopenharmony_ci      }
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_ci      this[kDecoder] = true;
4061cb0ef41Sopenharmony_ci      this[kFlags] = flags;
4071cb0ef41Sopenharmony_ci      this[kEncoding] = enc;
4081cb0ef41Sopenharmony_ci      this[kIgnoreBOM] = Boolean(options?.ignoreBOM);
4091cb0ef41Sopenharmony_ci      this[kFatal] = Boolean(options?.fatal);
4101cb0ef41Sopenharmony_ci      // Only support fast path for UTF-8.
4111cb0ef41Sopenharmony_ci      this[kUTF8FastPath] = enc === 'utf-8';
4121cb0ef41Sopenharmony_ci      this[kHandle] = undefined;
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ci      if (!this[kUTF8FastPath]) {
4151cb0ef41Sopenharmony_ci        this.#prepareConverter();
4161cb0ef41Sopenharmony_ci      }
4171cb0ef41Sopenharmony_ci    }
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ci    #prepareConverter() {
4201cb0ef41Sopenharmony_ci      if (this[kHandle] !== undefined) return;
4211cb0ef41Sopenharmony_ci      const handle = getConverter(this[kEncoding], this[kFlags]);
4221cb0ef41Sopenharmony_ci      if (handle === undefined)
4231cb0ef41Sopenharmony_ci        throw new ERR_ENCODING_NOT_SUPPORTED(this[kEncoding]);
4241cb0ef41Sopenharmony_ci      this[kHandle] = handle;
4251cb0ef41Sopenharmony_ci    }
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_ci    decode(input = empty, options = kEmptyObject) {
4281cb0ef41Sopenharmony_ci      validateDecoder(this);
4291cb0ef41Sopenharmony_ci
4301cb0ef41Sopenharmony_ci      this[kUTF8FastPath] &&= !(options?.stream);
4311cb0ef41Sopenharmony_ci
4321cb0ef41Sopenharmony_ci      if (this[kUTF8FastPath]) {
4331cb0ef41Sopenharmony_ci        return decodeUTF8(input, this[kIgnoreBOM], this[kFatal]);
4341cb0ef41Sopenharmony_ci      }
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci      this.#prepareConverter();
4371cb0ef41Sopenharmony_ci
4381cb0ef41Sopenharmony_ci      validateObject(options, 'options', {
4391cb0ef41Sopenharmony_ci        nullable: true,
4401cb0ef41Sopenharmony_ci        allowArray: true,
4411cb0ef41Sopenharmony_ci        allowFunction: true,
4421cb0ef41Sopenharmony_ci      });
4431cb0ef41Sopenharmony_ci
4441cb0ef41Sopenharmony_ci      let flags = 0;
4451cb0ef41Sopenharmony_ci      if (options !== null)
4461cb0ef41Sopenharmony_ci        flags |= options.stream ? 0 : CONVERTER_FLAGS_FLUSH;
4471cb0ef41Sopenharmony_ci
4481cb0ef41Sopenharmony_ci      return _decode(this[kHandle], input, flags, this.encoding);
4491cb0ef41Sopenharmony_ci    }
4501cb0ef41Sopenharmony_ci  }
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ci  return TextDecoder;
4531cb0ef41Sopenharmony_ci}
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_cifunction makeTextDecoderJS() {
4561cb0ef41Sopenharmony_ci  let StringDecoder;
4571cb0ef41Sopenharmony_ci  function lazyStringDecoder() {
4581cb0ef41Sopenharmony_ci    if (StringDecoder === undefined)
4591cb0ef41Sopenharmony_ci      ({ StringDecoder } = require('string_decoder'));
4601cb0ef41Sopenharmony_ci    return StringDecoder;
4611cb0ef41Sopenharmony_ci  }
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_ci  const kBOMSeen = Symbol('BOM seen');
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ci  function hasConverter(encoding) {
4661cb0ef41Sopenharmony_ci    return encoding === 'utf-8' || encoding === 'utf-16le';
4671cb0ef41Sopenharmony_ci  }
4681cb0ef41Sopenharmony_ci
4691cb0ef41Sopenharmony_ci  class TextDecoder {
4701cb0ef41Sopenharmony_ci    constructor(encoding = 'utf-8', options = kEmptyObject) {
4711cb0ef41Sopenharmony_ci      encoding = `${encoding}`;
4721cb0ef41Sopenharmony_ci      validateObject(options, 'options', {
4731cb0ef41Sopenharmony_ci        nullable: true,
4741cb0ef41Sopenharmony_ci        allowArray: true,
4751cb0ef41Sopenharmony_ci        allowFunction: true,
4761cb0ef41Sopenharmony_ci      });
4771cb0ef41Sopenharmony_ci
4781cb0ef41Sopenharmony_ci      const enc = getEncodingFromLabel(encoding);
4791cb0ef41Sopenharmony_ci      if (enc === undefined || !hasConverter(enc))
4801cb0ef41Sopenharmony_ci        throw new ERR_ENCODING_NOT_SUPPORTED(encoding);
4811cb0ef41Sopenharmony_ci
4821cb0ef41Sopenharmony_ci      let flags = 0;
4831cb0ef41Sopenharmony_ci      if (options !== null) {
4841cb0ef41Sopenharmony_ci        if (options.fatal) {
4851cb0ef41Sopenharmony_ci          throw new ERR_NO_ICU('"fatal" option');
4861cb0ef41Sopenharmony_ci        }
4871cb0ef41Sopenharmony_ci        flags |= options.ignoreBOM ? CONVERTER_FLAGS_IGNORE_BOM : 0;
4881cb0ef41Sopenharmony_ci      }
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci      this[kDecoder] = true;
4911cb0ef41Sopenharmony_ci      // StringDecoder will normalize WHATWG encoding to Node.js encoding.
4921cb0ef41Sopenharmony_ci      this[kHandle] = new (lazyStringDecoder())(enc);
4931cb0ef41Sopenharmony_ci      this[kFlags] = flags;
4941cb0ef41Sopenharmony_ci      this[kEncoding] = enc;
4951cb0ef41Sopenharmony_ci      this[kBOMSeen] = false;
4961cb0ef41Sopenharmony_ci    }
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci    decode(input = empty, options = kEmptyObject) {
4991cb0ef41Sopenharmony_ci      validateDecoder(this);
5001cb0ef41Sopenharmony_ci      if (isAnyArrayBuffer(input)) {
5011cb0ef41Sopenharmony_ci        try {
5021cb0ef41Sopenharmony_ci          input = lazyBuffer().from(input);
5031cb0ef41Sopenharmony_ci        } catch {
5041cb0ef41Sopenharmony_ci          input = empty;
5051cb0ef41Sopenharmony_ci        }
5061cb0ef41Sopenharmony_ci      } else if (isArrayBufferView(input)) {
5071cb0ef41Sopenharmony_ci        try {
5081cb0ef41Sopenharmony_ci          input = lazyBuffer().from(input.buffer, input.byteOffset,
5091cb0ef41Sopenharmony_ci                                    input.byteLength);
5101cb0ef41Sopenharmony_ci        } catch {
5111cb0ef41Sopenharmony_ci          input = empty;
5121cb0ef41Sopenharmony_ci        }
5131cb0ef41Sopenharmony_ci      } else {
5141cb0ef41Sopenharmony_ci        throw new ERR_INVALID_ARG_TYPE('input',
5151cb0ef41Sopenharmony_ci                                       ['ArrayBuffer', 'ArrayBufferView'],
5161cb0ef41Sopenharmony_ci                                       input);
5171cb0ef41Sopenharmony_ci      }
5181cb0ef41Sopenharmony_ci      validateObject(options, 'options', {
5191cb0ef41Sopenharmony_ci        nullable: true,
5201cb0ef41Sopenharmony_ci        allowArray: true,
5211cb0ef41Sopenharmony_ci        allowFunction: true,
5221cb0ef41Sopenharmony_ci      });
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci      if (this[kFlags] & CONVERTER_FLAGS_FLUSH) {
5251cb0ef41Sopenharmony_ci        this[kBOMSeen] = false;
5261cb0ef41Sopenharmony_ci      }
5271cb0ef41Sopenharmony_ci
5281cb0ef41Sopenharmony_ci      if (options !== null && options.stream) {
5291cb0ef41Sopenharmony_ci        this[kFlags] &= ~CONVERTER_FLAGS_FLUSH;
5301cb0ef41Sopenharmony_ci      } else {
5311cb0ef41Sopenharmony_ci        this[kFlags] |= CONVERTER_FLAGS_FLUSH;
5321cb0ef41Sopenharmony_ci      }
5331cb0ef41Sopenharmony_ci
5341cb0ef41Sopenharmony_ci      let result = this[kFlags] & CONVERTER_FLAGS_FLUSH ?
5351cb0ef41Sopenharmony_ci        this[kHandle].end(input) :
5361cb0ef41Sopenharmony_ci        this[kHandle].write(input);
5371cb0ef41Sopenharmony_ci
5381cb0ef41Sopenharmony_ci      if (result.length > 0 &&
5391cb0ef41Sopenharmony_ci          !this[kBOMSeen] &&
5401cb0ef41Sopenharmony_ci          !(this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM)) {
5411cb0ef41Sopenharmony_ci        // If the very first result in the stream is a BOM, and we are not
5421cb0ef41Sopenharmony_ci        // explicitly told to ignore it, then we discard it.
5431cb0ef41Sopenharmony_ci        if (result[0] === '\ufeff') {
5441cb0ef41Sopenharmony_ci          result = StringPrototypeSlice(result, 1);
5451cb0ef41Sopenharmony_ci        }
5461cb0ef41Sopenharmony_ci        this[kBOMSeen] = true;
5471cb0ef41Sopenharmony_ci      }
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ci      return result;
5501cb0ef41Sopenharmony_ci    }
5511cb0ef41Sopenharmony_ci  }
5521cb0ef41Sopenharmony_ci
5531cb0ef41Sopenharmony_ci  return TextDecoder;
5541cb0ef41Sopenharmony_ci}
5551cb0ef41Sopenharmony_ci
5561cb0ef41Sopenharmony_ci// Mix in some shared properties.
5571cb0ef41Sopenharmony_ciconst sharedProperties = ObjectGetOwnPropertyDescriptors({
5581cb0ef41Sopenharmony_ci  get encoding() {
5591cb0ef41Sopenharmony_ci    validateDecoder(this);
5601cb0ef41Sopenharmony_ci    return this[kEncoding];
5611cb0ef41Sopenharmony_ci  },
5621cb0ef41Sopenharmony_ci
5631cb0ef41Sopenharmony_ci  get fatal() {
5641cb0ef41Sopenharmony_ci    validateDecoder(this);
5651cb0ef41Sopenharmony_ci    return (this[kFlags] & CONVERTER_FLAGS_FATAL) === CONVERTER_FLAGS_FATAL;
5661cb0ef41Sopenharmony_ci  },
5671cb0ef41Sopenharmony_ci
5681cb0ef41Sopenharmony_ci  get ignoreBOM() {
5691cb0ef41Sopenharmony_ci    validateDecoder(this);
5701cb0ef41Sopenharmony_ci    return (this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM) ===
5711cb0ef41Sopenharmony_ci              CONVERTER_FLAGS_IGNORE_BOM;
5721cb0ef41Sopenharmony_ci  },
5731cb0ef41Sopenharmony_ci
5741cb0ef41Sopenharmony_ci  [inspect](depth, opts) {
5751cb0ef41Sopenharmony_ci    validateDecoder(this);
5761cb0ef41Sopenharmony_ci    if (typeof depth === 'number' && depth < 0)
5771cb0ef41Sopenharmony_ci      return this;
5781cb0ef41Sopenharmony_ci    const constructor = getConstructorOf(this) || TextDecoder;
5791cb0ef41Sopenharmony_ci    const obj = ObjectCreate({ constructor });
5801cb0ef41Sopenharmony_ci    obj.encoding = this.encoding;
5811cb0ef41Sopenharmony_ci    obj.fatal = this.fatal;
5821cb0ef41Sopenharmony_ci    obj.ignoreBOM = this.ignoreBOM;
5831cb0ef41Sopenharmony_ci    if (opts.showHidden) {
5841cb0ef41Sopenharmony_ci      obj[kFlags] = this[kFlags];
5851cb0ef41Sopenharmony_ci      obj[kHandle] = this[kHandle];
5861cb0ef41Sopenharmony_ci    }
5871cb0ef41Sopenharmony_ci    // Lazy to avoid circular dependency
5881cb0ef41Sopenharmony_ci    const { inspect } = require('internal/util/inspect');
5891cb0ef41Sopenharmony_ci    return `${constructor.name} ${inspect(obj)}`;
5901cb0ef41Sopenharmony_ci  },
5911cb0ef41Sopenharmony_ci});
5921cb0ef41Sopenharmony_ciconst propertiesValues = ObjectValues(sharedProperties);
5931cb0ef41Sopenharmony_cifor (let i = 0; i < propertiesValues.length; i++) {
5941cb0ef41Sopenharmony_ci  // We want to use null-prototype objects to not rely on globally mutable
5951cb0ef41Sopenharmony_ci  // %Object.prototype%.
5961cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf(propertiesValues[i], null);
5971cb0ef41Sopenharmony_ci}
5981cb0ef41Sopenharmony_cisharedProperties[inspect].enumerable = false;
5991cb0ef41Sopenharmony_ci
6001cb0ef41Sopenharmony_ciObjectDefineProperties(TextDecoder.prototype, {
6011cb0ef41Sopenharmony_ci  decode: kEnumerableProperty,
6021cb0ef41Sopenharmony_ci  ...sharedProperties,
6031cb0ef41Sopenharmony_ci  [SymbolToStringTag]: {
6041cb0ef41Sopenharmony_ci    __proto__: null,
6051cb0ef41Sopenharmony_ci    configurable: true,
6061cb0ef41Sopenharmony_ci    value: 'TextDecoder',
6071cb0ef41Sopenharmony_ci  },
6081cb0ef41Sopenharmony_ci});
6091cb0ef41Sopenharmony_ci
6101cb0ef41Sopenharmony_cimodule.exports = {
6111cb0ef41Sopenharmony_ci  getEncodingFromLabel,
6121cb0ef41Sopenharmony_ci  TextDecoder,
6131cb0ef41Sopenharmony_ci  TextEncoder,
6141cb0ef41Sopenharmony_ci};
615