11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ciconst inspect = require('util').inspect;
261cb0ef41Sopenharmony_ciconst StringDecoder = require('string_decoder').StringDecoder;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci// Test default encoding
291cb0ef41Sopenharmony_cilet decoder = new StringDecoder();
301cb0ef41Sopenharmony_ciassert.strictEqual(decoder.encoding, 'utf8');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// Should work without 'new' keyword
331cb0ef41Sopenharmony_ciconst decoder2 = {};
341cb0ef41Sopenharmony_ciStringDecoder.call(decoder2);
351cb0ef41Sopenharmony_ciassert.strictEqual(decoder2.encoding, 'utf8');
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// UTF-8
381cb0ef41Sopenharmony_citest('utf-8', Buffer.from('$', 'utf-8'), '$');
391cb0ef41Sopenharmony_citest('utf-8', Buffer.from('¢', 'utf-8'), '¢');
401cb0ef41Sopenharmony_citest('utf-8', Buffer.from('€', 'utf-8'), '€');
411cb0ef41Sopenharmony_citest('utf-8', Buffer.from('�', 'utf-8'), '�');
421cb0ef41Sopenharmony_ci// A mixed ascii and non-ascii string
431cb0ef41Sopenharmony_ci// Test stolen from deps/v8/test/cctest/test-strings.cc
441cb0ef41Sopenharmony_ci// U+02E4 -> CB A4
451cb0ef41Sopenharmony_ci// U+0064 -> 64
461cb0ef41Sopenharmony_ci// U+12E4 -> E1 8B A4
471cb0ef41Sopenharmony_ci// U+0030 -> 30
481cb0ef41Sopenharmony_ci// U+3045 -> E3 81 85
491cb0ef41Sopenharmony_citest(
501cb0ef41Sopenharmony_ci  'utf-8',
511cb0ef41Sopenharmony_ci  Buffer.from([0xCB, 0xA4, 0x64, 0xE1, 0x8B, 0xA4, 0x30, 0xE3, 0x81, 0x85]),
521cb0ef41Sopenharmony_ci  '\u02e4\u0064\u12e4\u0030\u3045'
531cb0ef41Sopenharmony_ci);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// Some invalid input, known to have caused trouble with chunking
561cb0ef41Sopenharmony_ci// in https://github.com/nodejs/node/pull/7310#issuecomment-226445923
571cb0ef41Sopenharmony_ci// 00: |00000000 ASCII
581cb0ef41Sopenharmony_ci// 41: |01000001 ASCII
591cb0ef41Sopenharmony_ci// B8: 10|111000 continuation
601cb0ef41Sopenharmony_ci// CC: 110|01100 two-byte head
611cb0ef41Sopenharmony_ci// E2: 1110|0010 three-byte head
621cb0ef41Sopenharmony_ci// F0: 11110|000 four-byte head
631cb0ef41Sopenharmony_ci// F1: 11110|001'another four-byte head
641cb0ef41Sopenharmony_ci// FB: 111110|11 "five-byte head", not UTF-8
651cb0ef41Sopenharmony_citest('utf-8', Buffer.from('C9B5A941', 'hex'), '\u0275\ufffdA');
661cb0ef41Sopenharmony_citest('utf-8', Buffer.from('E2', 'hex'), '\ufffd');
671cb0ef41Sopenharmony_citest('utf-8', Buffer.from('E241', 'hex'), '\ufffdA');
681cb0ef41Sopenharmony_citest('utf-8', Buffer.from('CCCCB8', 'hex'), '\ufffd\u0338');
691cb0ef41Sopenharmony_citest('utf-8', Buffer.from('F0B841', 'hex'), '\ufffdA');
701cb0ef41Sopenharmony_citest('utf-8', Buffer.from('F1CCB8', 'hex'), '\ufffd\u0338');
711cb0ef41Sopenharmony_citest('utf-8', Buffer.from('F0FB00', 'hex'), '\ufffd\ufffd\0');
721cb0ef41Sopenharmony_citest('utf-8', Buffer.from('CCE2B8B8', 'hex'), '\ufffd\u2e38');
731cb0ef41Sopenharmony_citest('utf-8', Buffer.from('E2B8CCB8', 'hex'), '\ufffd\u0338');
741cb0ef41Sopenharmony_citest('utf-8', Buffer.from('E2FBCC01', 'hex'), '\ufffd\ufffd\ufffd\u0001');
751cb0ef41Sopenharmony_citest('utf-8', Buffer.from('CCB8CDB9', 'hex'), '\u0338\u0379');
761cb0ef41Sopenharmony_ci// CESU-8 of U+1D40D
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci// V8 has changed their invalid UTF-8 handling, see
791cb0ef41Sopenharmony_ci// https://chromium-review.googlesource.com/c/v8/v8/+/671020 for more info.
801cb0ef41Sopenharmony_citest('utf-8', Buffer.from('EDA0B5EDB08D', 'hex'),
811cb0ef41Sopenharmony_ci     '\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd');
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci// UCS-2
841cb0ef41Sopenharmony_citest('ucs2', Buffer.from('ababc', 'ucs2'), 'ababc');
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci// UTF-16LE
871cb0ef41Sopenharmony_citest('utf16le', Buffer.from('3DD84DDC', 'hex'), '\ud83d\udc4d'); // thumbs up
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci// Additional UTF-8 tests
901cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf8');
911cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('E1', 'hex')), '');
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci// A quick test for lastChar, lastNeed & lastTotal which are undocumented.
941cb0ef41Sopenharmony_ciassert(decoder.lastChar.equals(new Uint8Array([0xe1, 0, 0, 0])));
951cb0ef41Sopenharmony_ciassert.strictEqual(decoder.lastNeed, 2);
961cb0ef41Sopenharmony_ciassert.strictEqual(decoder.lastTotal, 3);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '\ufffd');
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci// ArrayBufferView tests
1011cb0ef41Sopenharmony_ciconst arrayBufferViewStr = 'String for ArrayBufferView tests\n';
1021cb0ef41Sopenharmony_ciconst inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
1031cb0ef41Sopenharmony_cifor (const expectView of common.getArrayBufferViews(inputBuffer)) {
1041cb0ef41Sopenharmony_ci  assert.strictEqual(
1051cb0ef41Sopenharmony_ci    decoder.write(expectView),
1061cb0ef41Sopenharmony_ci    inputBuffer.toString('utf8')
1071cb0ef41Sopenharmony_ci  );
1081cb0ef41Sopenharmony_ci  assert.strictEqual(decoder.end(), '');
1091cb0ef41Sopenharmony_ci}
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf8');
1121cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('E18B', 'hex')), '');
1131cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '\ufffd');
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf8');
1161cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('\ufffd')), '\ufffd');
1171cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '');
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf8');
1201cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('\ufffd\ufffd\ufffd')),
1211cb0ef41Sopenharmony_ci                   '\ufffd\ufffd\ufffd');
1221cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '');
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf8');
1251cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('EFBFBDE2', 'hex')), '\ufffd');
1261cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '\ufffd');
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf8');
1291cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('F1', 'hex')), '');
1301cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('41F2', 'hex')), '\ufffdA');
1311cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '\ufffd');
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci// Additional utf8Text test
1341cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf8');
1351cb0ef41Sopenharmony_ciassert.strictEqual(decoder.text(Buffer.from([0x41]), 2), '');
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci// Additional UTF-16LE surrogate pair tests
1381cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf16le');
1391cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
1401cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
1411cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('DC', 'hex')), '\ud83d\udc4d');
1421cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '');
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf16le');
1451cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
1461cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '\ud83d');
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf16le');
1491cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
1501cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
1511cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '\ud83d');
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf16le');
1541cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('3DD84D', 'hex')), '\ud83d');
1551cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '');
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/22358
1581cb0ef41Sopenharmony_ci// (unaligned UTF-16 access).
1591cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf16le');
1601cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.alloc(1)), '');
1611cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.alloc(20)), '\0'.repeat(10));
1621cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.alloc(48)), '\0'.repeat(24));
1631cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '');
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci// Regression tests for https://github.com/nodejs/node/issues/22626
1661cb0ef41Sopenharmony_ci// (not enough replacement chars when having seen more than one byte of an
1671cb0ef41Sopenharmony_ci// incomplete multibyte characters).
1681cb0ef41Sopenharmony_cidecoder = new StringDecoder('utf8');
1691cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('f69b', 'hex')), '');
1701cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('d1', 'hex')), '\ufffd\ufffd');
1711cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '\ufffd');
1721cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('f4', 'hex')), '');
1731cb0ef41Sopenharmony_ciassert.strictEqual(decoder.write(Buffer.from('bde5', 'hex')), '\ufffd\ufffd');
1741cb0ef41Sopenharmony_ciassert.strictEqual(decoder.end(), '\ufffd');
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ciassert.throws(
1771cb0ef41Sopenharmony_ci  () => new StringDecoder(1),
1781cb0ef41Sopenharmony_ci  {
1791cb0ef41Sopenharmony_ci    code: 'ERR_UNKNOWN_ENCODING',
1801cb0ef41Sopenharmony_ci    name: 'TypeError',
1811cb0ef41Sopenharmony_ci    message: 'Unknown encoding: 1'
1821cb0ef41Sopenharmony_ci  }
1831cb0ef41Sopenharmony_ci);
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ciassert.throws(
1861cb0ef41Sopenharmony_ci  () => new StringDecoder('test'),
1871cb0ef41Sopenharmony_ci  {
1881cb0ef41Sopenharmony_ci    code: 'ERR_UNKNOWN_ENCODING',
1891cb0ef41Sopenharmony_ci    name: 'TypeError',
1901cb0ef41Sopenharmony_ci    message: 'Unknown encoding: test'
1911cb0ef41Sopenharmony_ci  }
1921cb0ef41Sopenharmony_ci);
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ciassert.throws(
1951cb0ef41Sopenharmony_ci  () => new StringDecoder('utf8').write(null),
1961cb0ef41Sopenharmony_ci  {
1971cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1981cb0ef41Sopenharmony_ci    name: 'TypeError',
1991cb0ef41Sopenharmony_ci    message: 'The "buf" argument must be an instance of Buffer, TypedArray,' +
2001cb0ef41Sopenharmony_ci      ' or DataView. Received null'
2011cb0ef41Sopenharmony_ci  }
2021cb0ef41Sopenharmony_ci);
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ciif (common.enoughTestMem) {
2051cb0ef41Sopenharmony_ci  assert.throws(
2061cb0ef41Sopenharmony_ci    () => new StringDecoder().write(Buffer.alloc((process.arch === 'ia32' ? 0x18ffffe8 : 0x1fffffe8) + 1).fill('a')),
2071cb0ef41Sopenharmony_ci    {
2081cb0ef41Sopenharmony_ci      code: 'ERR_STRING_TOO_LONG',
2091cb0ef41Sopenharmony_ci    }
2101cb0ef41Sopenharmony_ci  );
2111cb0ef41Sopenharmony_ci}
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ciassert.throws(
2141cb0ef41Sopenharmony_ci  () => new StringDecoder('utf8').__proto__.write(Buffer.from('abc')), // eslint-disable-line no-proto
2151cb0ef41Sopenharmony_ci  {
2161cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
2171cb0ef41Sopenharmony_ci  }
2181cb0ef41Sopenharmony_ci);
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci// Test verifies that StringDecoder will correctly decode the given input
2211cb0ef41Sopenharmony_ci// buffer with the given encoding to the expected output. It will attempt all
2221cb0ef41Sopenharmony_ci// possible ways to write() the input buffer, see writeSequences(). The
2231cb0ef41Sopenharmony_ci// singleSequence allows for easy debugging of a specific sequence which is
2241cb0ef41Sopenharmony_ci// useful in case of test failures.
2251cb0ef41Sopenharmony_cifunction test(encoding, input, expected, singleSequence) {
2261cb0ef41Sopenharmony_ci  let sequences;
2271cb0ef41Sopenharmony_ci  if (!singleSequence) {
2281cb0ef41Sopenharmony_ci    sequences = writeSequences(input.length);
2291cb0ef41Sopenharmony_ci  } else {
2301cb0ef41Sopenharmony_ci    sequences = [singleSequence];
2311cb0ef41Sopenharmony_ci  }
2321cb0ef41Sopenharmony_ci  const hexNumberRE = /.{2}/g;
2331cb0ef41Sopenharmony_ci  sequences.forEach((sequence) => {
2341cb0ef41Sopenharmony_ci    const decoder = new StringDecoder(encoding);
2351cb0ef41Sopenharmony_ci    let output = '';
2361cb0ef41Sopenharmony_ci    sequence.forEach((write) => {
2371cb0ef41Sopenharmony_ci      output += decoder.write(input.slice(write[0], write[1]));
2381cb0ef41Sopenharmony_ci    });
2391cb0ef41Sopenharmony_ci    output += decoder.end();
2401cb0ef41Sopenharmony_ci    if (output !== expected) {
2411cb0ef41Sopenharmony_ci      const message =
2421cb0ef41Sopenharmony_ci        `Expected "${unicodeEscape(expected)}", ` +
2431cb0ef41Sopenharmony_ci        `but got "${unicodeEscape(output)}"\n` +
2441cb0ef41Sopenharmony_ci        `input: ${input.toString('hex').match(hexNumberRE)}\n` +
2451cb0ef41Sopenharmony_ci        `Write sequence: ${JSON.stringify(sequence)}\n` +
2461cb0ef41Sopenharmony_ci        `Full Decoder State: ${inspect(decoder)}`;
2471cb0ef41Sopenharmony_ci      assert.fail(message);
2481cb0ef41Sopenharmony_ci    }
2491cb0ef41Sopenharmony_ci  });
2501cb0ef41Sopenharmony_ci}
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci// unicodeEscape prints the str contents as unicode escape codes.
2531cb0ef41Sopenharmony_cifunction unicodeEscape(str) {
2541cb0ef41Sopenharmony_ci  let r = '';
2551cb0ef41Sopenharmony_ci  for (let i = 0; i < str.length; i++) {
2561cb0ef41Sopenharmony_ci    r += `\\u${str.charCodeAt(i).toString(16)}`;
2571cb0ef41Sopenharmony_ci  }
2581cb0ef41Sopenharmony_ci  return r;
2591cb0ef41Sopenharmony_ci}
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci// writeSequences returns an array of arrays that describes all possible ways a
2621cb0ef41Sopenharmony_ci// buffer of the given length could be split up and passed to sequential write
2631cb0ef41Sopenharmony_ci// calls.
2641cb0ef41Sopenharmony_ci//
2651cb0ef41Sopenharmony_ci// e.G. writeSequences(3) will return: [
2661cb0ef41Sopenharmony_ci//   [ [ 0, 3 ] ],
2671cb0ef41Sopenharmony_ci//   [ [ 0, 2 ], [ 2, 3 ] ],
2681cb0ef41Sopenharmony_ci//   [ [ 0, 1 ], [ 1, 3 ] ],
2691cb0ef41Sopenharmony_ci//   [ [ 0, 1 ], [ 1, 2 ], [ 2, 3 ] ]
2701cb0ef41Sopenharmony_ci// ]
2711cb0ef41Sopenharmony_cifunction writeSequences(length, start, sequence) {
2721cb0ef41Sopenharmony_ci  if (start === undefined) {
2731cb0ef41Sopenharmony_ci    start = 0;
2741cb0ef41Sopenharmony_ci    sequence = [];
2751cb0ef41Sopenharmony_ci  } else if (start === length) {
2761cb0ef41Sopenharmony_ci    return [sequence];
2771cb0ef41Sopenharmony_ci  }
2781cb0ef41Sopenharmony_ci  let sequences = [];
2791cb0ef41Sopenharmony_ci  for (let end = length; end > start; end--) {
2801cb0ef41Sopenharmony_ci    const subSequence = sequence.concat([[start, end]]);
2811cb0ef41Sopenharmony_ci    const subSequences = writeSequences(length, end, subSequence, sequences);
2821cb0ef41Sopenharmony_ci    sequences = sequences.concat(subSequences);
2831cb0ef41Sopenharmony_ci  }
2841cb0ef41Sopenharmony_ci  return sequences;
2851cb0ef41Sopenharmony_ci}
286