11cb0ef41Sopenharmony_ci"use strict"; 21cb0ef41Sopenharmony_civar Buffer = require("safer-buffer").Buffer; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci// Note: UTF16-LE (or UCS2) codec is Node.js native. See encodings/internal.js 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// == UTF16-BE codec. ========================================================== 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciexports.utf16be = Utf16BECodec; 91cb0ef41Sopenharmony_cifunction Utf16BECodec() { 101cb0ef41Sopenharmony_ci} 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciUtf16BECodec.prototype.encoder = Utf16BEEncoder; 131cb0ef41Sopenharmony_ciUtf16BECodec.prototype.decoder = Utf16BEDecoder; 141cb0ef41Sopenharmony_ciUtf16BECodec.prototype.bomAware = true; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci// -- Encoding 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cifunction Utf16BEEncoder() { 201cb0ef41Sopenharmony_ci} 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciUtf16BEEncoder.prototype.write = function(str) { 231cb0ef41Sopenharmony_ci var buf = Buffer.from(str, 'ucs2'); 241cb0ef41Sopenharmony_ci for (var i = 0; i < buf.length; i += 2) { 251cb0ef41Sopenharmony_ci var tmp = buf[i]; buf[i] = buf[i+1]; buf[i+1] = tmp; 261cb0ef41Sopenharmony_ci } 271cb0ef41Sopenharmony_ci return buf; 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciUtf16BEEncoder.prototype.end = function() { 311cb0ef41Sopenharmony_ci} 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci// -- Decoding 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cifunction Utf16BEDecoder() { 371cb0ef41Sopenharmony_ci this.overflowByte = -1; 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciUtf16BEDecoder.prototype.write = function(buf) { 411cb0ef41Sopenharmony_ci if (buf.length == 0) 421cb0ef41Sopenharmony_ci return ''; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci var buf2 = Buffer.alloc(buf.length + 1), 451cb0ef41Sopenharmony_ci i = 0, j = 0; 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci if (this.overflowByte !== -1) { 481cb0ef41Sopenharmony_ci buf2[0] = buf[0]; 491cb0ef41Sopenharmony_ci buf2[1] = this.overflowByte; 501cb0ef41Sopenharmony_ci i = 1; j = 2; 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci for (; i < buf.length-1; i += 2, j+= 2) { 541cb0ef41Sopenharmony_ci buf2[j] = buf[i+1]; 551cb0ef41Sopenharmony_ci buf2[j+1] = buf[i]; 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1; 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci return buf2.slice(0, j).toString('ucs2'); 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciUtf16BEDecoder.prototype.end = function() { 641cb0ef41Sopenharmony_ci this.overflowByte = -1; 651cb0ef41Sopenharmony_ci} 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci// == UTF-16 codec ============================================================= 691cb0ef41Sopenharmony_ci// Decoder chooses automatically from UTF-16LE and UTF-16BE using BOM and space-based heuristic. 701cb0ef41Sopenharmony_ci// Defaults to UTF-16LE, as it's prevalent and default in Node. 711cb0ef41Sopenharmony_ci// http://en.wikipedia.org/wiki/UTF-16 and http://encoding.spec.whatwg.org/#utf-16le 721cb0ef41Sopenharmony_ci// Decoder default can be changed: iconv.decode(buf, 'utf16', {defaultEncoding: 'utf-16be'}); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci// Encoder uses UTF-16LE and prepends BOM (which can be overridden with addBOM: false). 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ciexports.utf16 = Utf16Codec; 771cb0ef41Sopenharmony_cifunction Utf16Codec(codecOptions, iconv) { 781cb0ef41Sopenharmony_ci this.iconv = iconv; 791cb0ef41Sopenharmony_ci} 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ciUtf16Codec.prototype.encoder = Utf16Encoder; 821cb0ef41Sopenharmony_ciUtf16Codec.prototype.decoder = Utf16Decoder; 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci// -- Encoding (pass-through) 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_cifunction Utf16Encoder(options, codec) { 881cb0ef41Sopenharmony_ci options = options || {}; 891cb0ef41Sopenharmony_ci if (options.addBOM === undefined) 901cb0ef41Sopenharmony_ci options.addBOM = true; 911cb0ef41Sopenharmony_ci this.encoder = codec.iconv.getEncoder('utf-16le', options); 921cb0ef41Sopenharmony_ci} 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciUtf16Encoder.prototype.write = function(str) { 951cb0ef41Sopenharmony_ci return this.encoder.write(str); 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciUtf16Encoder.prototype.end = function() { 991cb0ef41Sopenharmony_ci return this.encoder.end(); 1001cb0ef41Sopenharmony_ci} 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci// -- Decoding 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_cifunction Utf16Decoder(options, codec) { 1061cb0ef41Sopenharmony_ci this.decoder = null; 1071cb0ef41Sopenharmony_ci this.initialBufs = []; 1081cb0ef41Sopenharmony_ci this.initialBufsLen = 0; 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci this.options = options || {}; 1111cb0ef41Sopenharmony_ci this.iconv = codec.iconv; 1121cb0ef41Sopenharmony_ci} 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ciUtf16Decoder.prototype.write = function(buf) { 1151cb0ef41Sopenharmony_ci if (!this.decoder) { 1161cb0ef41Sopenharmony_ci // Codec is not chosen yet. Accumulate initial bytes. 1171cb0ef41Sopenharmony_ci this.initialBufs.push(buf); 1181cb0ef41Sopenharmony_ci this.initialBufsLen += buf.length; 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci if (this.initialBufsLen < 16) // We need more bytes to use space heuristic (see below) 1211cb0ef41Sopenharmony_ci return ''; 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci // We have enough bytes -> detect endianness. 1241cb0ef41Sopenharmony_ci var encoding = detectEncoding(this.initialBufs, this.options.defaultEncoding); 1251cb0ef41Sopenharmony_ci this.decoder = this.iconv.getDecoder(encoding, this.options); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci var resStr = ''; 1281cb0ef41Sopenharmony_ci for (var i = 0; i < this.initialBufs.length; i++) 1291cb0ef41Sopenharmony_ci resStr += this.decoder.write(this.initialBufs[i]); 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci this.initialBufs.length = this.initialBufsLen = 0; 1321cb0ef41Sopenharmony_ci return resStr; 1331cb0ef41Sopenharmony_ci } 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci return this.decoder.write(buf); 1361cb0ef41Sopenharmony_ci} 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ciUtf16Decoder.prototype.end = function() { 1391cb0ef41Sopenharmony_ci if (!this.decoder) { 1401cb0ef41Sopenharmony_ci var encoding = detectEncoding(this.initialBufs, this.options.defaultEncoding); 1411cb0ef41Sopenharmony_ci this.decoder = this.iconv.getDecoder(encoding, this.options); 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci var resStr = ''; 1441cb0ef41Sopenharmony_ci for (var i = 0; i < this.initialBufs.length; i++) 1451cb0ef41Sopenharmony_ci resStr += this.decoder.write(this.initialBufs[i]); 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci var trail = this.decoder.end(); 1481cb0ef41Sopenharmony_ci if (trail) 1491cb0ef41Sopenharmony_ci resStr += trail; 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci this.initialBufs.length = this.initialBufsLen = 0; 1521cb0ef41Sopenharmony_ci return resStr; 1531cb0ef41Sopenharmony_ci } 1541cb0ef41Sopenharmony_ci return this.decoder.end(); 1551cb0ef41Sopenharmony_ci} 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_cifunction detectEncoding(bufs, defaultEncoding) { 1581cb0ef41Sopenharmony_ci var b = []; 1591cb0ef41Sopenharmony_ci var charsProcessed = 0; 1601cb0ef41Sopenharmony_ci var asciiCharsLE = 0, asciiCharsBE = 0; // Number of ASCII chars when decoded as LE or BE. 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci outer_loop: 1631cb0ef41Sopenharmony_ci for (var i = 0; i < bufs.length; i++) { 1641cb0ef41Sopenharmony_ci var buf = bufs[i]; 1651cb0ef41Sopenharmony_ci for (var j = 0; j < buf.length; j++) { 1661cb0ef41Sopenharmony_ci b.push(buf[j]); 1671cb0ef41Sopenharmony_ci if (b.length === 2) { 1681cb0ef41Sopenharmony_ci if (charsProcessed === 0) { 1691cb0ef41Sopenharmony_ci // Check BOM first. 1701cb0ef41Sopenharmony_ci if (b[0] === 0xFF && b[1] === 0xFE) return 'utf-16le'; 1711cb0ef41Sopenharmony_ci if (b[0] === 0xFE && b[1] === 0xFF) return 'utf-16be'; 1721cb0ef41Sopenharmony_ci } 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci if (b[0] === 0 && b[1] !== 0) asciiCharsBE++; 1751cb0ef41Sopenharmony_ci if (b[0] !== 0 && b[1] === 0) asciiCharsLE++; 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci b.length = 0; 1781cb0ef41Sopenharmony_ci charsProcessed++; 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci if (charsProcessed >= 100) { 1811cb0ef41Sopenharmony_ci break outer_loop; 1821cb0ef41Sopenharmony_ci } 1831cb0ef41Sopenharmony_ci } 1841cb0ef41Sopenharmony_ci } 1851cb0ef41Sopenharmony_ci } 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci // Make decisions. 1881cb0ef41Sopenharmony_ci // Most of the time, the content has ASCII chars (U+00**), but the opposite (U+**00) is uncommon. 1891cb0ef41Sopenharmony_ci // So, we count ASCII as if it was LE or BE, and decide from that. 1901cb0ef41Sopenharmony_ci if (asciiCharsBE > asciiCharsLE) return 'utf-16be'; 1911cb0ef41Sopenharmony_ci if (asciiCharsBE < asciiCharsLE) return 'utf-16le'; 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ci // Couldn't decide (likely all zeros or not enough data). 1941cb0ef41Sopenharmony_ci return defaultEncoding || 'utf-16le'; 1951cb0ef41Sopenharmony_ci} 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci 198