"use strict"; var Zmodem = module.exports; Object.assign( Zmodem, require("./zmlib") ); //encode() variables - declare them here so we don’t //create them in the function. var encode_cur, encode_todo; const ZDLE = Zmodem.ZMLIB.ZDLE; /** * Class that handles ZDLE encoding and decoding. * Encoding is subject to a given configuration--specifically, whether * we want to escape all control characters. Decoding is static; however * a given string is encoded we can always decode it. */ Zmodem.ZDLE = class ZmodemZDLE { /** * Create a ZDLE encoder. * * @param {object} [config] - The initial configuration. * @param {object} config.escape_ctrl_chars - Whether the ZDLE encoder * should escape control characters. */ constructor(config) { this._config = {}; if (config) { this.set_escape_ctrl_chars(!!config.escape_ctrl_chars); } } /** * Enable or disable control-character escaping. * You should probably enable this for sender sessions. * * @param {boolean} value - Whether to enable (true) or disable (false). */ set_escape_ctrl_chars(value) { if (typeof value !== "boolean") throw "need boolean!"; if (value !== this._config.escape_ctrl_chars) { this._config.escape_ctrl_chars = value; this._setup_zdle_table(); } } /** * Whether or not control-character escaping is enabled. * * @return {boolean} Whether the escaping is on (true) or off (false). */ escapes_ctrl_chars() { return !!this._config.escape_ctrl_chars; } //I don’t know of any Zmodem implementations that use ZESC8 //(“escape_8th_bit”)?? /* ZMODEM software escapes ZDLE, 020, 0220, 021, 0221, 023, and 0223. If preceded by 0100 or 0300 (@), 015 and 0215 are also escaped to protect the Telenet command escape CR-@-CR. */ /** * Encode an array of octet values and return it. * This will mutate the given array. * * @param {number[]} octets - The octet values to transform. * Each array member should be an 8-bit unsigned integer (0-255). * This object is mutated in the function. * * @returns {number[]} The passed-in array, transformed. This is the * same object that is passed in. */ encode(octets) { //NB: Performance matters here! if (!this._zdle_table) throw "No ZDLE encode table configured!"; var zdle_table = this._zdle_table; var last_code = this._lastcode; var arrbuf = new ArrayBuffer( 2 * octets.length ); var arrbuf_uint8 = new Uint8Array(arrbuf); var escctl_yn = this._config.escape_ctrl_chars; var arrbuf_i = 0; for (encode_cur=0; encode_cur=0; o--) { if (octets[o] === ZDLE) { octets.splice( o, 2, octets[o+1] - 64 ); } } return octets; } /** * Remove, ZDLE-decode, and return bytes from the passed-in array. * If the requested number of ZDLE-encoded bytes isn’t available, * then the passed-in array is unmodified (and the return is undefined). * * @param {number[]} octets - The octet values to transform. * Each array member should be an 8-bit unsigned integer (0-255). * This object is mutated in the function. * * @param {number} offset - The number of (undecoded) bytes to skip * at the beginning of the “octets” array. * * @param {number} count - The number of bytes (octet values) to return. * * @returns {number[]|undefined} An array with the requested number of * decoded octet values, or undefined if that number of decoded * octets isn’t available (given the passed-in offset). */ static splice(octets, offset, count) { var so_far = 0; if (!offset) offset = 0; for (var i = offset; i