11cb0ef41Sopenharmony_ci"use strict"; 21cb0ef41Sopenharmony_civar Buffer = require("safer-buffer").Buffer; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci// Single-byte codec. Needs a 'chars' string parameter that contains 256 or 128 chars that 51cb0ef41Sopenharmony_ci// correspond to encoded bytes (if 128 - then lower half is ASCII). 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciexports._sbcs = SBCSCodec; 81cb0ef41Sopenharmony_cifunction SBCSCodec(codecOptions, iconv) { 91cb0ef41Sopenharmony_ci if (!codecOptions) 101cb0ef41Sopenharmony_ci throw new Error("SBCS codec is called without the data.") 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci // Prepare char buffer for decoding. 131cb0ef41Sopenharmony_ci if (!codecOptions.chars || (codecOptions.chars.length !== 128 && codecOptions.chars.length !== 256)) 141cb0ef41Sopenharmony_ci throw new Error("Encoding '"+codecOptions.type+"' has incorrect 'chars' (must be of len 128 or 256)"); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci if (codecOptions.chars.length === 128) { 171cb0ef41Sopenharmony_ci var asciiString = ""; 181cb0ef41Sopenharmony_ci for (var i = 0; i < 128; i++) 191cb0ef41Sopenharmony_ci asciiString += String.fromCharCode(i); 201cb0ef41Sopenharmony_ci codecOptions.chars = asciiString + codecOptions.chars; 211cb0ef41Sopenharmony_ci } 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci this.decodeBuf = Buffer.from(codecOptions.chars, 'ucs2'); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci // Encoding buffer. 261cb0ef41Sopenharmony_ci var encodeBuf = Buffer.alloc(65536, iconv.defaultCharSingleByte.charCodeAt(0)); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci for (var i = 0; i < codecOptions.chars.length; i++) 291cb0ef41Sopenharmony_ci encodeBuf[codecOptions.chars.charCodeAt(i)] = i; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci this.encodeBuf = encodeBuf; 321cb0ef41Sopenharmony_ci} 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciSBCSCodec.prototype.encoder = SBCSEncoder; 351cb0ef41Sopenharmony_ciSBCSCodec.prototype.decoder = SBCSDecoder; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_cifunction SBCSEncoder(options, codec) { 391cb0ef41Sopenharmony_ci this.encodeBuf = codec.encodeBuf; 401cb0ef41Sopenharmony_ci} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciSBCSEncoder.prototype.write = function(str) { 431cb0ef41Sopenharmony_ci var buf = Buffer.alloc(str.length); 441cb0ef41Sopenharmony_ci for (var i = 0; i < str.length; i++) 451cb0ef41Sopenharmony_ci buf[i] = this.encodeBuf[str.charCodeAt(i)]; 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci return buf; 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciSBCSEncoder.prototype.end = function() { 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_cifunction SBCSDecoder(options, codec) { 551cb0ef41Sopenharmony_ci this.decodeBuf = codec.decodeBuf; 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ciSBCSDecoder.prototype.write = function(buf) { 591cb0ef41Sopenharmony_ci // Strings are immutable in JS -> we use ucs2 buffer to speed up computations. 601cb0ef41Sopenharmony_ci var decodeBuf = this.decodeBuf; 611cb0ef41Sopenharmony_ci var newBuf = Buffer.alloc(buf.length*2); 621cb0ef41Sopenharmony_ci var idx1 = 0, idx2 = 0; 631cb0ef41Sopenharmony_ci for (var i = 0; i < buf.length; i++) { 641cb0ef41Sopenharmony_ci idx1 = buf[i]*2; idx2 = i*2; 651cb0ef41Sopenharmony_ci newBuf[idx2] = decodeBuf[idx1]; 661cb0ef41Sopenharmony_ci newBuf[idx2+1] = decodeBuf[idx1+1]; 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci return newBuf.toString('ucs2'); 691cb0ef41Sopenharmony_ci} 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciSBCSDecoder.prototype.end = function() { 721cb0ef41Sopenharmony_ci} 73