11cb0ef41Sopenharmony_ci"use strict"; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_civar Buffer = require("safer-buffer").Buffer; 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_civar bomHandling = require("./bom-handling"), 61cb0ef41Sopenharmony_ci iconv = module.exports; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci// All codecs and aliases are kept here, keyed by encoding name/alias. 91cb0ef41Sopenharmony_ci// They are lazy loaded in `iconv.getCodec` from `encodings/index.js`. 101cb0ef41Sopenharmony_ciiconv.encodings = null; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci// Characters emitted in case of error. 131cb0ef41Sopenharmony_ciiconv.defaultCharUnicode = '�'; 141cb0ef41Sopenharmony_ciiconv.defaultCharSingleByte = '?'; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci// Public API. 171cb0ef41Sopenharmony_ciiconv.encode = function encode(str, encoding, options) { 181cb0ef41Sopenharmony_ci str = "" + (str || ""); // Ensure string. 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci var encoder = iconv.getEncoder(encoding, options); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci var res = encoder.write(str); 231cb0ef41Sopenharmony_ci var trail = encoder.end(); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res; 261cb0ef41Sopenharmony_ci} 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciiconv.decode = function decode(buf, encoding, options) { 291cb0ef41Sopenharmony_ci if (typeof buf === 'string') { 301cb0ef41Sopenharmony_ci if (!iconv.skipDecodeWarning) { 311cb0ef41Sopenharmony_ci console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding'); 321cb0ef41Sopenharmony_ci iconv.skipDecodeWarning = true; 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci buf = Buffer.from("" + (buf || ""), "binary"); // Ensure buffer. 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci var decoder = iconv.getDecoder(encoding, options); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci var res = decoder.write(buf); 411cb0ef41Sopenharmony_ci var trail = decoder.end(); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci return trail ? (res + trail) : res; 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciiconv.encodingExists = function encodingExists(enc) { 471cb0ef41Sopenharmony_ci try { 481cb0ef41Sopenharmony_ci iconv.getCodec(enc); 491cb0ef41Sopenharmony_ci return true; 501cb0ef41Sopenharmony_ci } catch (e) { 511cb0ef41Sopenharmony_ci return false; 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci// Legacy aliases to convert functions 561cb0ef41Sopenharmony_ciiconv.toEncoding = iconv.encode; 571cb0ef41Sopenharmony_ciiconv.fromEncoding = iconv.decode; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci// Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache. 601cb0ef41Sopenharmony_ciiconv._codecDataCache = {}; 611cb0ef41Sopenharmony_ciiconv.getCodec = function getCodec(encoding) { 621cb0ef41Sopenharmony_ci if (!iconv.encodings) 631cb0ef41Sopenharmony_ci iconv.encodings = require("../encodings"); // Lazy load all encoding definitions. 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci // Canonicalize encoding name: strip all non-alphanumeric chars and appended year. 661cb0ef41Sopenharmony_ci var enc = iconv._canonicalizeEncoding(encoding); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci // Traverse iconv.encodings to find actual codec. 691cb0ef41Sopenharmony_ci var codecOptions = {}; 701cb0ef41Sopenharmony_ci while (true) { 711cb0ef41Sopenharmony_ci var codec = iconv._codecDataCache[enc]; 721cb0ef41Sopenharmony_ci if (codec) 731cb0ef41Sopenharmony_ci return codec; 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci var codecDef = iconv.encodings[enc]; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci switch (typeof codecDef) { 781cb0ef41Sopenharmony_ci case "string": // Direct alias to other encoding. 791cb0ef41Sopenharmony_ci enc = codecDef; 801cb0ef41Sopenharmony_ci break; 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci case "object": // Alias with options. Can be layered. 831cb0ef41Sopenharmony_ci for (var key in codecDef) 841cb0ef41Sopenharmony_ci codecOptions[key] = codecDef[key]; 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci if (!codecOptions.encodingName) 871cb0ef41Sopenharmony_ci codecOptions.encodingName = enc; 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci enc = codecDef.type; 901cb0ef41Sopenharmony_ci break; 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci case "function": // Codec itself. 931cb0ef41Sopenharmony_ci if (!codecOptions.encodingName) 941cb0ef41Sopenharmony_ci codecOptions.encodingName = enc; 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci // The codec function must load all tables and return object with .encoder and .decoder methods. 971cb0ef41Sopenharmony_ci // It'll be called only once (for each different options object). 981cb0ef41Sopenharmony_ci codec = new codecDef(codecOptions, iconv); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci iconv._codecDataCache[codecOptions.encodingName] = codec; // Save it to be reused later. 1011cb0ef41Sopenharmony_ci return codec; 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci default: 1041cb0ef41Sopenharmony_ci throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')"); 1051cb0ef41Sopenharmony_ci } 1061cb0ef41Sopenharmony_ci } 1071cb0ef41Sopenharmony_ci} 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ciiconv._canonicalizeEncoding = function(encoding) { 1101cb0ef41Sopenharmony_ci // Canonicalize encoding name: strip all non-alphanumeric chars and appended year. 1111cb0ef41Sopenharmony_ci return (''+encoding).toLowerCase().replace(/:\d{4}$|[^0-9a-z]/g, ""); 1121cb0ef41Sopenharmony_ci} 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ciiconv.getEncoder = function getEncoder(encoding, options) { 1151cb0ef41Sopenharmony_ci var codec = iconv.getCodec(encoding), 1161cb0ef41Sopenharmony_ci encoder = new codec.encoder(options, codec); 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci if (codec.bomAware && options && options.addBOM) 1191cb0ef41Sopenharmony_ci encoder = new bomHandling.PrependBOM(encoder, options); 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci return encoder; 1221cb0ef41Sopenharmony_ci} 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ciiconv.getDecoder = function getDecoder(encoding, options) { 1251cb0ef41Sopenharmony_ci var codec = iconv.getCodec(encoding), 1261cb0ef41Sopenharmony_ci decoder = new codec.decoder(options, codec); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci if (codec.bomAware && !(options && options.stripBOM === false)) 1291cb0ef41Sopenharmony_ci decoder = new bomHandling.StripBOM(decoder, options); 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci return decoder; 1321cb0ef41Sopenharmony_ci} 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci// Streaming API 1351cb0ef41Sopenharmony_ci// NOTE: Streaming API naturally depends on 'stream' module from Node.js. Unfortunately in browser environments this module can add 1361cb0ef41Sopenharmony_ci// up to 100Kb to the output bundle. To avoid unnecessary code bloat, we don't enable Streaming API in browser by default. 1371cb0ef41Sopenharmony_ci// If you would like to enable it explicitly, please add the following code to your app: 1381cb0ef41Sopenharmony_ci// > iconv.enableStreamingAPI(require('stream')); 1391cb0ef41Sopenharmony_ciiconv.enableStreamingAPI = function enableStreamingAPI(stream_module) { 1401cb0ef41Sopenharmony_ci if (iconv.supportsStreams) 1411cb0ef41Sopenharmony_ci return; 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci // Dependency-inject stream module to create IconvLite stream classes. 1441cb0ef41Sopenharmony_ci var streams = require("./streams")(stream_module); 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci // Not public API yet, but expose the stream classes. 1471cb0ef41Sopenharmony_ci iconv.IconvLiteEncoderStream = streams.IconvLiteEncoderStream; 1481cb0ef41Sopenharmony_ci iconv.IconvLiteDecoderStream = streams.IconvLiteDecoderStream; 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci // Streaming API. 1511cb0ef41Sopenharmony_ci iconv.encodeStream = function encodeStream(encoding, options) { 1521cb0ef41Sopenharmony_ci return new iconv.IconvLiteEncoderStream(iconv.getEncoder(encoding, options), options); 1531cb0ef41Sopenharmony_ci } 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci iconv.decodeStream = function decodeStream(encoding, options) { 1561cb0ef41Sopenharmony_ci return new iconv.IconvLiteDecoderStream(iconv.getDecoder(encoding, options), options); 1571cb0ef41Sopenharmony_ci } 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci iconv.supportsStreams = true; 1601cb0ef41Sopenharmony_ci} 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci// Enable Streaming API automatically if 'stream' module is available and non-empty (the majority of environments). 1631cb0ef41Sopenharmony_civar stream_module; 1641cb0ef41Sopenharmony_citry { 1651cb0ef41Sopenharmony_ci stream_module = require("stream"); 1661cb0ef41Sopenharmony_ci} catch (e) {} 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ciif (stream_module && stream_module.Transform) { 1691cb0ef41Sopenharmony_ci iconv.enableStreamingAPI(stream_module); 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci} else { 1721cb0ef41Sopenharmony_ci // In rare cases where 'stream' module is not available by default, throw a helpful exception. 1731cb0ef41Sopenharmony_ci iconv.encodeStream = iconv.decodeStream = function() { 1741cb0ef41Sopenharmony_ci throw new Error("iconv-lite Streaming API is not enabled. Use iconv.enableStreamingAPI(require('stream')); to enable it."); 1751cb0ef41Sopenharmony_ci }; 1761cb0ef41Sopenharmony_ci} 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ciif ("Ā" != "\u0100") { 1791cb0ef41Sopenharmony_ci console.error("iconv-lite warning: js files use non-utf8 encoding. See https://github.com/ashtuchkin/iconv-lite/wiki/Javascript-source-file-encodings for more info."); 1801cb0ef41Sopenharmony_ci} 181