diff --git a/archive/archive.js b/archive/archive.js index b066dc6..5137c25 100644 --- a/archive/archive.js +++ b/archive/archive.js @@ -16,16 +16,16 @@ bitjs.archive = bitjs.archive || {}; // =========================================================================== // Stolen from Closure because it's the best way to do Java-like inheritance. bitjs.base = function(me, opt_methodName, var_args) { - var caller = arguments.callee.caller; + const caller = arguments.callee.caller; if (caller.superClass_) { // This is a constructor. Call the superclass constructor. return caller.superClass_.constructor.apply( me, Array.prototype.slice.call(arguments, 1)); } - var args = Array.prototype.slice.call(arguments, 2); - var foundCaller = false; - for (var ctor = me.constructor; + const args = Array.prototype.slice.call(arguments, 2); + let foundCaller = false; + for (let ctor = me.constructor; ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) { if (ctor.prototype[opt_methodName] === caller) { foundCaller = true; @@ -210,7 +210,7 @@ bitjs.archive.Unarchiver = function(arrayBuffer, opt_pathToBitJS) { * @type {Map.} */ this.listeners_ = {}; - for (var type in bitjs.archive.UnarchiveEvent.Type) { + for (let type in bitjs.archive.UnarchiveEvent.Type) { this.listeners_[bitjs.archive.UnarchiveEvent.Type[type]] = []; } }; @@ -253,7 +253,7 @@ bitjs.archive.Unarchiver.prototype.addEventListener = function(type, listener) { */ bitjs.archive.Unarchiver.prototype.removeEventListener = function(type, listener) { if (type in this.listeners_) { - var index = this.listeners_[type].indexOf(listener); + const index = this.listeners_[type].indexOf(listener); if (index != -1) { this.listeners_[type].splice(index, 1); } @@ -282,8 +282,8 @@ bitjs.archive.Unarchiver.prototype.handleWorkerEvent_ = function(e) { * Starts the unarchive in a separate Web Worker thread and returns immediately. */ bitjs.archive.Unarchiver.prototype.start = function() { - var me = this; - var scriptFileName = this.pathToBitJS_ + this.getScriptFileName(); + const me = this; + const scriptFileName = this.pathToBitJS_ + this.getScriptFileName(); if (scriptFileName) { this.worker_ = new Worker(scriptFileName); @@ -326,7 +326,7 @@ bitjs.archive.Unzipper = function(arrayBuffer, opt_pathToBitJS) { bitjs.base(this, arrayBuffer, opt_pathToBitJS); }; bitjs.inherits(bitjs.archive.Unzipper, bitjs.archive.Unarchiver); -bitjs.archive.Unzipper.prototype.getScriptFileName = function() { return 'unzip.js' }; +bitjs.archive.Unzipper.prototype.getScriptFileName = function() { return 'archive/unzip.js' }; /** * Unrarrer @@ -337,7 +337,7 @@ bitjs.archive.Unrarrer = function(arrayBuffer, opt_pathToBitJS) { bitjs.base(this, arrayBuffer, opt_pathToBitJS); }; bitjs.inherits(bitjs.archive.Unrarrer, bitjs.archive.Unarchiver); -bitjs.archive.Unrarrer.prototype.getScriptFileName = function() { return 'unrar.js' }; +bitjs.archive.Unrarrer.prototype.getScriptFileName = function() { return 'archive/unrar.js' }; /** * Untarrer @@ -348,7 +348,7 @@ bitjs.archive.Untarrer = function(arrayBuffer, opt_pathToBitJS) { bitjs.base(this, arrayBuffer, opt_pathToBitJS); }; bitjs.inherits(bitjs.archive.Untarrer, bitjs.archive.Unarchiver); -bitjs.archive.Untarrer.prototype.getScriptFileName = function() { return 'untar.js' }; +bitjs.archive.Untarrer.prototype.getScriptFileName = function() { return 'archive/untar.js' }; /** * Factory method that creates an unarchiver based on the byte signature found @@ -358,9 +358,9 @@ bitjs.archive.Untarrer.prototype.getScriptFileName = function() { return 'untar. * @return {bitjs.archive.Unarchiver} */ bitjs.archive.GetUnarchiver = function(ab, opt_pathToBitJS) { - var unarchiver = null; - var pathToBitJS = opt_pathToBitJS || ''; - var h = new Uint8Array(ab, 0, 10); + let unarchiver = null; + const pathToBitJS = opt_pathToBitJS || ''; + const h = new Uint8Array(ab, 0, 10); if (h[0] == 0x52 && h[1] == 0x61 && h[2] == 0x72 && h[3] == 0x21) { // Rar! unarchiver = new bitjs.archive.Unrarrer(ab, pathToBitJS); diff --git a/archive/rarvm.js b/archive/rarvm.js index 0e6870e..c86c6a6 100644 --- a/archive/rarvm.js +++ b/archive/rarvm.js @@ -9,12 +9,12 @@ /** * CRC Implementation. */ -var CRCTab = new Array(256).fill(0); +const CRCTab = new Array(256).fill(0); function InitCRC() { - for (var i = 0; i < 256; ++i) { - var c = i; - for (var j = 0; j < 8; ++j) { + for (let i = 0; i < 256; ++i) { + let c = i; + for (let j = 0; j < 8; ++j) { // Read http://stackoverflow.com/questions/6798111/bitwise-operations-on-32-bit-unsigned-ints // for the bitwise operator issue (JS interprets operands as 32-bit signed // integers and we need to deal with unsigned ones here). @@ -60,8 +60,8 @@ function CRC(startCRC, arr) { #endif */ - for (var i = 0; i < arr.length; ++i) { - var byte = ((startCRC ^ arr[i]) >>> 0) & 0xff; + for (let i = 0; i < arr.length; ++i) { + const byte = ((startCRC ^ arr[i]) >>> 0) & 0xff; startCRC = (CRCTab[byte] ^ (startCRC >>> 8)) >>> 0; } @@ -74,17 +74,17 @@ function CRC(startCRC, arr) { /** * RarVM Implementation. */ -var VM_MEMSIZE = 0x40000; -var VM_MEMMASK = (VM_MEMSIZE - 1); -var VM_GLOBALMEMADDR = 0x3C000; -var VM_GLOBALMEMSIZE = 0x2000; -var VM_FIXEDGLOBALSIZE = 64; -var MAXWINSIZE = 0x400000; -var MAXWINMASK = (MAXWINSIZE - 1); +const VM_MEMSIZE = 0x40000; +const VM_MEMMASK = (VM_MEMSIZE - 1); +const VM_GLOBALMEMADDR = 0x3C000; +const VM_GLOBALMEMSIZE = 0x2000; +const VM_FIXEDGLOBALSIZE = 64; +const MAXWINSIZE = 0x400000; +const MAXWINMASK = (MAXWINSIZE - 1); /** */ -var VM_Commands = { +const VM_Commands = { VM_MOV: 0, VM_CMP: 1, VM_ADD: 2, @@ -141,7 +141,7 @@ var VM_Commands = { /** */ -var VM_StandardFilters = { +const VM_StandardFilters = { VMSF_NONE: 0, VMSF_E8: 1, VMSF_E8E9: 2, @@ -154,7 +154,7 @@ var VM_StandardFilters = { /** */ -var VM_Flags = { +const VM_Flags = { VM_FC: 1, VM_FZ: 2, VM_FS: 0x80000000, @@ -162,7 +162,7 @@ var VM_Flags = { /** */ -var VM_OpType = { +const VM_OpType = { VM_OPREG: 0, VM_OPINT: 1, VM_OPREGMEM: 2, @@ -177,7 +177,7 @@ var VM_OpType = { * @return {string} The key/enum value as a string. */ function findKeyForValue(obj, val) { - for (var key in obj) { + for (let key in obj) { if (obj[key] === val) { return key; } @@ -186,7 +186,7 @@ function findKeyForValue(obj, val) { } function getDebugString(obj, val) { - var s = 'Unknown.'; + let s = 'Unknown.'; if (obj === VM_Commands) { s = 'VM_Commands.'; } else if (obj === VM_StandardFilters) { @@ -204,7 +204,7 @@ function getDebugString(obj, val) { * @struct * @constructor */ -var VM_PreparedOperand = function() { +const VM_PreparedOperand = function() { /** @type {VM_OpType} */ this.Type; @@ -235,7 +235,7 @@ VM_PreparedOperand.prototype.toString = function() { * @struct * @constructor */ -var VM_PreparedCommand = function() { +const VM_PreparedCommand = function() { /** @type {VM_Commands} */ this.OpCode; @@ -267,7 +267,7 @@ VM_PreparedCommand.prototype.toString = function(indent) { * @struct * @constructor */ -var VM_PreparedProgram = function() { +const VM_PreparedProgram = function() { /** @type {Array} */ this.Cmd = []; @@ -292,8 +292,8 @@ var VM_PreparedProgram = function() { /** @return {string} */ VM_PreparedProgram.prototype.toString = function() { - var s = '{\n Cmd: [\n'; - for (var i = 0; i < this.Cmd.length; ++i) { + let s = '{\n Cmd: [\n'; + for (let i = 0; i < this.Cmd.length; ++i) { s += this.Cmd[i].toString(' ') + ',\n'; } s += '],\n'; @@ -306,7 +306,7 @@ VM_PreparedProgram.prototype.toString = function() { * @struct * @constructor */ -var UnpackFilter = function() { +const UnpackFilter = function() { /** @type {number} */ this.BlockStart = 0; @@ -328,17 +328,17 @@ var UnpackFilter = function() { this.Prg = new VM_PreparedProgram(); }; -var VMCF_OP0 = 0; -var VMCF_OP1 = 1; -var VMCF_OP2 = 2; -var VMCF_OPMASK = 3; -var VMCF_BYTEMODE = 4; -var VMCF_JUMP = 8; -var VMCF_PROC = 16; -var VMCF_USEFLAGS = 32; -var VMCF_CHFLAGS = 64; +const VMCF_OP0 = 0; +const VMCF_OP1 = 1; +const VMCF_OP2 = 2; +const VMCF_OPMASK = 3; +const VMCF_BYTEMODE = 4; +const VMCF_JUMP = 8; +const VMCF_PROC = 16; +const VMCF_USEFLAGS = 32; +const VMCF_CHFLAGS = 64; -var VM_CmdFlags = [ +const VM_CmdFlags = [ /* VM_MOV */ VMCF_OP2 | VMCF_BYTEMODE , /* VM_CMP */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , /* VM_ADD */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , @@ -389,7 +389,7 @@ var VM_CmdFlags = [ * @struct * @constructor */ -var StandardFilterSignature = function(length, crc, type) { +const StandardFilterSignature = function(length, crc, type) { /** @type {number} */ this.Length = length; @@ -403,7 +403,7 @@ var StandardFilterSignature = function(length, crc, type) { /** * @type {Array} */ -var StdList = [ +const StdList = [ new StandardFilterSignature(53, 0xad576887, VM_StandardFilters.VMSF_E8), new StandardFilterSignature(57, 0x3cd7e57e, VM_StandardFilters.VMSF_E8E9), new StandardFilterSignature(120, 0x3769893f, VM_StandardFilters.VMSF_ITANIUM), @@ -416,7 +416,7 @@ var StdList = [ /** * @constructor */ -var RarVM = function() { +const RarVM = function() { /** @private {Uint8Array} */ this.mem_ = null; @@ -441,8 +441,8 @@ RarVM.prototype.init = function() { * @return {VM_StandardFilters} */ RarVM.prototype.isStandardFilter = function(code) { - var codeCRC = (CRC(0xffffffff, code, code.length) ^ 0xffffffff) >>> 0; - for (var i = 0; i < StdList.length; ++i) { + const codeCRC = (CRC(0xffffffff, code, code.length) ^ 0xffffffff) >>> 0; + for (let i = 0; i < StdList.length; ++i) { if (StdList[i].CRC == codeCRC && StdList[i].Length == code.length) return StdList[i].Type; } @@ -456,7 +456,7 @@ RarVM.prototype.isStandardFilter = function(code) { * @param {bitjs.io.BitStream} bstream A rtl bit stream. */ RarVM.prototype.decodeArg = function(op, byteMode, bstream) { - var data = bstream.peekBits(16); + const data = bstream.peekBits(16); if (data & 0x8000) { op.Type = VM_OpType.VM_OPREG; // Operand is register (R[0]..R[7]) bstream.readBits(1); // 1 flag bit and... @@ -502,12 +502,12 @@ RarVM.prototype.decodeArg = function(op, byteMode, bstream) { RarVM.prototype.execute = function(prg) { this.R_.set(prg.InitR); - var globalSize = Math.min(prg.GlobalData.length, VM_GLOBALMEMSIZE); + const globalSize = Math.min(prg.GlobalData.length, VM_GLOBALMEMSIZE); if (globalSize) { this.mem_.set(prg.GlobalData.subarray(0, globalSize), VM_GLOBALMEMADDR); } - var staticSize = Math.min(prg.StaticData.length, VM_GLOBALMEMSIZE - globalSize); + const staticSize = Math.min(prg.StaticData.length, VM_GLOBALMEMSIZE - globalSize); if (staticSize) { this.mem_.set(prg.StaticData.subarray(0, staticSize), VM_GLOBALMEMADDR + globalSize); } @@ -515,15 +515,15 @@ RarVM.prototype.execute = function(prg) { this.R_[7] = VM_MEMSIZE; this.flags_ = 0; - var preparedCodes = prg.AltCmd ? prg.AltCmd : prg.Cmd; + const preparedCodes = prg.AltCmd ? prg.AltCmd : prg.Cmd; if (prg.Cmd.length > 0 && !this.executeCode(preparedCodes)) { // Invalid VM program. Let's replace it with 'return' command. preparedCode.OpCode = VM_Commands.VM_RET; } - var dataView = new DataView(this.mem_.buffer, VM_GLOBALMEMADDR); - var newBlockPos = dataView.getUint32(0x20, true /* little endian */) & VM_MEMMASK; - var newBlockSize = dataView.getUint32(0x1c, true /* little endian */) & VM_MEMMASK; + const dataView = new DataView(this.mem_.buffer, VM_GLOBALMEMADDR); + let newBlockPos = dataView.getUint32(0x20, true /* little endian */) & VM_MEMMASK; + const newBlockSize = dataView.getUint32(0x1c, true /* little endian */) & VM_MEMMASK; if (newBlockPos + newBlockSize >= VM_MEMSIZE) { newBlockPos = newBlockSize = 0; } @@ -531,10 +531,9 @@ RarVM.prototype.execute = function(prg) { prg.GlobalData = new Uint8Array(0); - var dataSize = Math.min(dataView.getUint32(0x30), - (VM_GLOBALMEMSIZE - VM_FIXEDGLOBALSIZE)); + const dataSize = Math.min(dataView.getUint32(0x30), (VM_GLOBALMEMSIZE - VM_FIXEDGLOBALSIZE)); if (dataSize != 0) { - var len = dataSize + VM_FIXEDGLOBALSIZE; + const len = dataSize + VM_FIXEDGLOBALSIZE; prg.GlobalData = new Uint8Array(len); prg.GlobalData.set(mem.subarray(VM_GLOBALMEMADDR, VM_GLOBALMEMADDR + len)); } @@ -545,8 +544,8 @@ RarVM.prototype.execute = function(prg) { * @return {boolean} */ RarVM.prototype.executeCode = function(preparedCodes) { - var codeIndex = 0; - var cmd = preparedCodes[codeIndex]; + let codeIndex = 0; + let cmd = preparedCodes[codeIndex]; // TODO: Why is this an infinite loop instead of just returning // when a VM_RET is hit? while (1) { @@ -578,13 +577,13 @@ RarVM.prototype.executeCode = function(preparedCodes) { RarVM.prototype.executeStandardFilter = function(filterType) { switch (filterType) { case VM_StandardFilters.VMSF_DELTA: - var dataSize = this.R_[4]; - var channels = this.R_[0]; - var srcPos = 0; - var border = dataSize * 2; + const dataSize = this.R_[4]; + const channels = this.R_[0]; + let srcPos = 0; + const border = dataSize * 2; //SET_VALUE(false,&Mem[VM_GLOBALMEMADDR+0x20],DataSize); - var dataView = new DataView(this.mem_.buffer, VM_GLOBALMEMADDR); + const dataView = new DataView(this.mem_.buffer, VM_GLOBALMEMADDR); dataView.setUint32(0x20, dataSize, true /* little endian */); if (dataSize >= VM_GLOBALMEMADDR / 2) { @@ -593,9 +592,9 @@ RarVM.prototype.executeStandardFilter = function(filterType) { // Bytes from same channels are grouped to continual data blocks, // so we need to place them back to their interleaving positions. - for (var curChannel = 0; curChannel < channels; ++curChannel) { - var prevByte = 0; - for (var destPos = dataSize + curChannel; destPos < border; destPos += channels) { + for (let curChannel = 0; curChannel < channels; ++curChannel) { + let prevByte = 0; + for (let destPos = dataSize + curChannel; destPos < border; destPos += channels) { prevByte = (prevByte - this.mem_[srcPos++]) & 0xff; this.mem_[destPos] = prevByte; } @@ -614,15 +613,15 @@ RarVM.prototype.executeStandardFilter = function(filterType) { * @param {VM_PreparedProgram} prg */ RarVM.prototype.prepare = function(code, prg) { - var codeSize = code.length; + let codeSize = code.length; //InitBitInput(); //memcpy(InBuf,Code,Min(CodeSize,BitInput::MAX_SIZE)); - var bstream = new bitjs.io.BitStream(code.buffer, true /* rtl */); + const bstream = new bitjs.io.BitStream(code.buffer, true /* rtl */); // Calculate the single byte XOR checksum to check validity of VM code. - var xorSum=0; - for (var i = 1; i < codeSize; ++i) { + let xorSum = 0; + for (let i = 1; i < codeSize; ++i) { xorSum ^= code[i]; } @@ -632,10 +631,10 @@ RarVM.prototype.prepare = function(code, prg) { // VM code is valid if equal. if (xorSum == code[0]) { - var filterType = this.isStandardFilter(code); + const filterType = this.isStandardFilter(code); if (filterType != VM_StandardFilters.VMSF_NONE) { // VM code is found among standard filters. - var curCmd = new VM_PreparedCommand(); + const curCmd = new VM_PreparedCommand(); prg.Cmd.push(curCmd); curCmd.OpCode = VM_Commands.VM_STANDARD; @@ -648,17 +647,17 @@ RarVM.prototype.prepare = function(code, prg) { codeSize = 0; } - var dataFlag = bstream.readBits(1); + const dataFlag = bstream.readBits(1); // Read static data contained in DB operators. This data cannot be // changed, it is a part of VM code, not a filter parameter. if (dataFlag & 0x8000) { - var dataSize = RarVM.readData(bstream) + 1; + const dataSize = RarVM.readData(bstream) + 1; // TODO: This accesses the byte pointer of the bstream directly. Is that ok? - for (var i = 0; i < bstream.bytePtr < codeSize && i < dataSize; ++i) { + for (let i = 0; i < bstream.bytePtr < codeSize && i < dataSize; ++i) { // Append a byte to the program's static data. - var newStaticData = new Uint8Array(prg.StaticData.length + 1); + const newStaticData = new Uint8Array(prg.StaticData.length + 1); newStaticData.set(prg.StaticData); newStaticData[newStaticData.length - 1] = bstream.readBits(8); prg.StaticData = newStaticData; @@ -666,9 +665,9 @@ RarVM.prototype.prepare = function(code, prg) { } while (bstream.bytePtr < codeSize) { - var curCmd = new VM_PreparedCommand(); + const curCmd = new VM_PreparedCommand(); prg.Cmd.push(curCmd); // Prg->Cmd.Add(1) - var flag = bstream.peekBits(1); + const flag = bstream.peekBits(1); if (!flag) { // (Data&0x8000)==0 curCmd.OpCode = bstream.readBits(4); } else { @@ -682,7 +681,7 @@ RarVM.prototype.prepare = function(code, prg) { } curCmd.Op1.Type = VM_OpType.VM_OPNONE; curCmd.Op2.Type = VM_OpType.VM_OPNONE; - var opNum = (VM_CmdFlags[curCmd.OpCode] & VMCF_OPMASK); + const opNum = (VM_CmdFlags[curCmd.OpCode] & VMCF_OPMASK); curCmd.Op1.Addr = null; curCmd.Op2.Addr = null; if (opNum > 0) { @@ -692,7 +691,7 @@ RarVM.prototype.prepare = function(code, prg) { } else { if (curCmd.Op1.Type == VM_OpType.VM_OPINT && (VM_CmdFlags[curCmd.OpCode] & (VMCF_JUMP|VMCF_PROC))) { // Calculating jump distance. - var distance = curCmd.Op1.Data; + let distance = curCmd.Op1.Data; if (distance >= 256) { distance -= 256; } else { @@ -716,7 +715,7 @@ RarVM.prototype.prepare = function(code, prg) { } // while ((uint)InAddrOp1.Data @@ -730,8 +729,8 @@ RarVM.prototype.prepare = function(code, prg) { // VM_OPINT type operands (usual integers) or maybe if something was // not set properly for other operands. 'Addr' field is required // for quicker addressing of operand data. - for (var i = 0; i < prg.Cmd.length; ++i) { - var cmd = prg.Cmd[i]; + for (let i = 0; i < prg.Cmd.length; ++i) { + const cmd = prg.Cmd[i]; if (cmd.Op1.Addr == null) { cmd.Op1.Addr = [cmd.Op1.Data]; } @@ -754,7 +753,7 @@ RarVM.prototype.prepare = function(code, prg) { * @param {number} offset Offset into arr to start setting the value, defaults to 0. */ RarVM.prototype.setLowEndianValue = function(arr, value, offset) { - var i = offset || 0; + const i = offset || 0; arr[i] = value & 0xff; arr[i + 1] = (value >>> 8) & 0xff; arr[i + 2] = (value >>> 16) & 0xff; @@ -770,8 +769,8 @@ RarVM.prototype.setLowEndianValue = function(arr, value, offset) { */ RarVM.prototype.setMemory = function(pos, buffer, dataSize) { if (pos < VM_MEMSIZE) { - var numBytes = Math.min(dataSize, VM_MEMSIZE - pos); - for (var i = 0; i < numBytes; ++i) { + const numBytes = Math.min(dataSize, VM_MEMSIZE - pos); + for (let i = 0; i < numBytes; ++i) { this.mem_[pos + i] = buffer[i]; } } @@ -785,7 +784,7 @@ RarVM.prototype.setMemory = function(pos, buffer, dataSize) { */ RarVM.readData = function(bstream) { // Read in the first 2 bits. - var flags = bstream.readBits(2); + const flags = bstream.readBits(2); switch (flags) { // Data&0xc000 // Return the next 4 bits. case 0: @@ -805,7 +804,7 @@ RarVM.readData = function(bstream) { // Read in the next 16. case 2: // 0x8000 - var val = bstream.getBits(); + const val = bstream.getBits(); bstream.readBits(16); return val; //bstream.readBits(16); diff --git a/archive/unrar.js b/archive/unrar.js index fe2c74b..f7e93a2 100644 --- a/archive/unrar.js +++ b/archive/unrar.js @@ -14,21 +14,21 @@ importScripts('archive.js'); importScripts('rarvm.js'); // Progress variables. -var currentFilename = ""; -var currentFileNumber = 0; -var currentBytesUnarchivedInFile = 0; -var currentBytesUnarchived = 0; -var totalUncompressedBytesInArchive = 0; -var totalFilesInArchive = 0; +let currentFilename = ""; +let currentFileNumber = 0; +let currentBytesUnarchivedInFile = 0; +let currentBytesUnarchived = 0; +let totalUncompressedBytesInArchive = 0; +let totalFilesInArchive = 0; // Helper functions. -var info = function(str) { +const info = function(str) { postMessage(new bitjs.archive.UnarchiveInfoEvent(str)); }; -var err = function(str) { +const err = function(str) { postMessage(new bitjs.archive.UnarchiveErrorEvent(str)); }; -var postProgress = function() { +const postProgress = function() { postMessage(new bitjs.archive.UnarchiveProgressEvent( currentFilename, currentFileNumber, @@ -39,26 +39,26 @@ var postProgress = function() { }; // shows a byte value as its hex representation -var nibble = "0123456789ABCDEF"; -var byteValueToHexString = function(num) { +const nibble = "0123456789ABCDEF"; +const byteValueToHexString = function(num) { return nibble[num>>4] + nibble[num&0xF]; }; -var twoByteValueToHexString = function(num) { +const twoByteValueToHexString = function(num) { return nibble[(num>>12)&0xF] + nibble[(num>>8)&0xF] + nibble[(num>>4)&0xF] + nibble[num&0xF]; }; // Volume Types -var MARK_HEAD = 0x72, - MAIN_HEAD = 0x73, - FILE_HEAD = 0x74, - COMM_HEAD = 0x75, - AV_HEAD = 0x76, - SUB_HEAD = 0x77, - PROTECT_HEAD = 0x78, - SIGN_HEAD = 0x79, - NEWSUB_HEAD = 0x7a, - ENDARC_HEAD = 0x7b; +const MARK_HEAD = 0x72; +const MAIN_HEAD = 0x73; +const FILE_HEAD = 0x74; +const COMM_HEAD = 0x75; +const AV_HEAD = 0x76; +const SUB_HEAD = 0x77; +const PROTECT_HEAD = 0x78; +const SIGN_HEAD = 0x79; +const NEWSUB_HEAD = 0x7a; +const ENDARC_HEAD = 0x7b; // ============================================================================================== // @@ -66,8 +66,8 @@ var MARK_HEAD = 0x72, * @param {bitjs.io.BitStream} bstream * @constructor */ -var RarVolumeHeader = function(bstream) { - var headPos = bstream.bytePtr; +const RarVolumeHeader = function(bstream) { + const headPos = bstream.bytePtr; // byte 1,2 info("Rar Volume Header @"+bstream.bytePtr); @@ -163,7 +163,8 @@ var RarVolumeHeader = function(bstream) { // read in filename this.filename = bstream.readBytes(this.nameSize); - for (var _i = 0, _s = ''; _i < this.filename.length; _i++) { + let _s = ''; + for (let _i = 0; _i < this.filename.length; _i++) { _s += String.fromCharCode(this.filename[_i]); } @@ -176,18 +177,18 @@ var RarVolumeHeader = function(bstream) { if (this.flags.LHD_EXTTIME) { // 16-bit flags - var extTimeFlags = bstream.readBits(16); + const extTimeFlags = bstream.readBits(16); // this is adapted straight out of arcread.cpp, Archive::ReadHeader() - for (var I = 0; I < 4; ++I) { - var rmode = extTimeFlags >> ((3-I)*4); - if ((rmode & 8)==0) { + for (let I = 0; I < 4; ++I) { + const rmode = extTimeFlags >> ((3 - I) * 4); + if ((rmode & 8) == 0) { continue; } - if (I!=0) + if (I != 0) bstream.readBits(16); - var count = (rmode&3); - for (var J = 0; J < count; ++J) { + const count = (rmode & 3); + for (let J = 0; J < count; ++J) { bstream.readBits(8); } } @@ -208,63 +209,62 @@ var RarVolumeHeader = function(bstream) { default: info("Found a header of type 0x" + byteValueToHexString(this.headType)); // skip the rest of the header bytes (for now) - bstream.readBytes( this.headSize - 7 ); + bstream.readBytes(this.headSize - 7); break; } }; -var BLOCK_LZ = 0, - BLOCK_PPM = 1; +const BLOCK_LZ = 0; +const BLOCK_PPM = 1; -var rLDecode = [0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224], - rLBits = [0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5], - rDBitLengthCounts = [4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,14,0,12], - rSDDecode = [0,4,8,16,32,64,128,192], - rSDBits = [2,2,3, 4, 5, 6, 6, 6]; +const rLDecode = [0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224]; +const rLBits = [0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5]; +const rDBitLengthCounts = [4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,14,0,12]; +const rSDDecode = [0,4,8,16,32,64,128,192]; +const rSDBits = [2,2,3, 4, 5, 6, 6, 6]; -var rDDecode = [0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, +const rDDecode = [0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, 65536, 98304, 131072, 196608, 262144, 327680, 393216, 458752, 524288, 589824, 655360, 720896, 786432, 851968, 917504, 983040]; -var rDBits = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, +const rDBits = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16]; -var rLOW_DIST_REP_COUNT = 16; +const rLOW_DIST_REP_COUNT = 16; -var rNC = 299, - rDC = 60, - rLDC = 17, - rRC = 28, - rBC = 20, - rHUFF_TABLE_SIZE = (rNC+rDC+rRC+rLDC); +const rNC = 299; +const rDC = 60; +const rLDC = 17; +const rRC = 28; +const rBC = 20; +const rHUFF_TABLE_SIZE = (rNC+rDC+rRC+rLDC); -var UnpBlockType = BLOCK_LZ; -var UnpOldTable = new Array(rHUFF_TABLE_SIZE); +const UnpOldTable = new Array(rHUFF_TABLE_SIZE); -var BD = { //bitdecode +const BD = { //bitdecode DecodeLen: new Array(16), DecodePos: new Array(16), DecodeNum: new Array(rBC) }; -var LD = { //litdecode +const LD = { //litdecode DecodeLen: new Array(16), DecodePos: new Array(16), DecodeNum: new Array(rNC) }; -var DD = { //distdecode +const DD = { //distdecode DecodeLen: new Array(16), DecodePos: new Array(16), DecodeNum: new Array(rDC) }; -var LDD = { //low dist decode +const LDD = { //low dist decode DecodeLen: new Array(16), DecodePos: new Array(16), DecodeNum: new Array(rLDC) }; -var RD = { //rep decode +const RD = { //rep decode DecodeLen: new Array(16), DecodePos: new Array(16), DecodeNum: new Array(rRC) @@ -273,19 +273,19 @@ var RD = { //rep decode /** * @type {Array} */ -var rOldBuffers = []; +const rOldBuffers = []; /** * The current buffer we are unpacking to. * @type {bitjs.io.ByteBuffer} */ -var rBuffer; +let rBuffer; /** * The buffer of the final bytes after filtering (only used in Unpack29). * @type {bitjs.io.ByteBuffer} */ -var wBuffer; +let wBuffer; /** @@ -304,8 +304,8 @@ var wBuffer; * @param {bitjs.io.BitStream} bstream */ function RarReadTables(bstream) { - var BitLength = new Array(rBC); - var Table = new Array(rHUFF_TABLE_SIZE); + const BitLength = new Array(rBC); + const Table = new Array(rHUFF_TABLE_SIZE); // before we start anything we need to get byte-aligned bstream.readBits( (8 - bstream.bitPtr) & 0x7 ); @@ -316,16 +316,16 @@ function RarReadTables(bstream) { } if (!bstream.readBits(1)) { //discard old table - for (var i = UnpOldTable.length; i--;) { + for (let i = UnpOldTable.length; i--;) { UnpOldTable[i] = 0; } } // read in bit lengths - for (var I = 0; I < rBC; ++I) { - var Length = bstream.readBits(4); + for (let I = 0; I < rBC; ++I) { + const Length = bstream.readBits(4); if (Length == 15) { - var ZeroCount = bstream.readBits(4); + let ZeroCount = bstream.readBits(4); if (ZeroCount == 0) { BitLength[I] = 15; } @@ -345,22 +345,21 @@ function RarReadTables(bstream) { RarMakeDecodeTables(BitLength, 0, BD, rBC); - var TableSize = rHUFF_TABLE_SIZE; - //console.log(DecodeLen, DecodePos, DecodeNum); - for (var i = 0; i < TableSize;) { - var num = RarDecodeNumber(bstream, BD); + const TableSize = rHUFF_TABLE_SIZE; + for (let i = 0; i < TableSize;) { + const num = RarDecodeNumber(bstream, BD); if (num < 16) { Table[i] = (num + UnpOldTable[i]) & 0xf; i++; } else if (num < 18) { - var N = (num == 16) ? (bstream.readBits(3) + 3) : (bstream.readBits(7) + 11); + let N = (num == 16) ? (bstream.readBits(3) + 3) : (bstream.readBits(7) + 11); while (N-- > 0 && i < TableSize) { Table[i] = Table[i - 1]; i++; } } else { - var N = (num == 18) ? (bstream.readBits(3) + 3) : (bstream.readBits(7) + 11); + let N = (num == 18) ? (bstream.readBits(3) + 3) : (bstream.readBits(7) + 11); while (N-- > 0 && i < TableSize) { Table[i++] = 0; @@ -373,7 +372,7 @@ function RarReadTables(bstream) { RarMakeDecodeTables(Table, rNC + rDC, LDD, rLDC); RarMakeDecodeTables(Table, rNC + rDC + rLDC, RD, rRC); - for (var i = UnpOldTable.length; i--;) { + for (let i = UnpOldTable.length; i--;) { UnpOldTable[i] = Table[i]; } return true; @@ -381,10 +380,12 @@ function RarReadTables(bstream) { function RarDecodeNumber(bstream, dec) { - var DecodeLen = dec.DecodeLen, DecodePos = dec.DecodePos, DecodeNum = dec.DecodeNum; - var bitField = bstream.getBits() & 0xfffe; + const DecodeLen = dec.DecodeLen; + const DecodePos = dec.DecodePos; + const DecodeNum = dec.DecodeNum; + const bitField = bstream.getBits() & 0xfffe; //some sort of rolled out binary search - var bits = ((bitField < DecodeLen[8])? + const bits = ((bitField < DecodeLen[8])? ((bitField < DecodeLen[4])? ((bitField < DecodeLen[2])? ((bitField < DecodeLen[1])?1:2) @@ -400,25 +401,25 @@ function RarDecodeNumber(bstream, dec) { ((bitField < DecodeLen[13])?13:14) :15)); bstream.readBits(bits); - var N = DecodePos[bits] + ((bitField - DecodeLen[bits -1]) >>> (16 - bits)); + const N = DecodePos[bits] + ((bitField - DecodeLen[bits -1]) >>> (16 - bits)); return DecodeNum[N]; } function RarMakeDecodeTables(BitLength, offset, dec, size) { - var DecodeLen = dec.DecodeLen; - var DecodePos = dec.DecodePos; - var DecodeNum = dec.DecodeNum; - var LenCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; - var TmpPos = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; - var N = 0; - var M = 0; + const DecodeLen = dec.DecodeLen; + const DecodePos = dec.DecodePos; + const DecodeNum = dec.DecodeNum; + const LenCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; + const TmpPos = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; + let N = 0; + let M = 0; - for (var i = DecodeNum.length; i--;) { + for (let i = DecodeNum.length; i--;) { DecodeNum[i] = 0; } - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { LenCount[BitLength[i + offset] & 0xF]++; } LenCount[0] = 0; @@ -426,7 +427,7 @@ function RarMakeDecodeTables(BitLength, offset, dec, size) { DecodePos[0] = 0; DecodeLen[0] = 0; - for (var I = 1; I < 16; ++I) { + for (let I = 1; I < 16; ++I) { N = 2 * (N+LenCount[I]); M = (N << (15-I)); if (M > 0xFFFF) { @@ -436,7 +437,7 @@ function RarMakeDecodeTables(BitLength, offset, dec, size) { DecodePos[I] = DecodePos[I-1] + LenCount[I-1]; TmpPos[I] = DecodePos[I]; } - for (I = 0; I < size; ++I) { + for (let I = 0; I < size; ++I) { if (BitLength[I + offset] != 0) { DecodeNum[ TmpPos[ BitLength[offset + I] & 0xF ]++] = I; } @@ -459,23 +460,23 @@ function Unpack15(bstream, Solid) { * @param {boolean} Solid */ function Unpack20(bstream, Solid) { - var destUnpSize = rBuffer.data.length; - var oldDistPtr = 0; + const destUnpSize = rBuffer.data.length; + let oldDistPtr = 0; RarReadTables20(bstream); while (destUnpSize > rBuffer.ptr) { - var num = RarDecodeNumber(bstream, LD); + const num = RarDecodeNumber(bstream, LD); if (num < 256) { rBuffer.insertByte(num); continue; } if (num > 269) { - var Length = rLDecode[num -= 270] + 3; + let Length = rLDecode[num -= 270] + 3; if ((Bits = rLBits[num]) > 0) { Length += bstream.readBits(Bits); } - var DistNumber = RarDecodeNumber(bstream, DD); - var Distance = rDDecode[DistNumber] + 1; + let DistNumber = RarDecodeNumber(bstream, DD); + let Distance = rDDecode[DistNumber] + 1; if ((Bits = rDBits[DistNumber]) > 0) { Distance += bstream.readBits(Bits); } @@ -501,9 +502,9 @@ function Unpack20(bstream, Solid) { continue; } if (num < 261) { - var Distance = rOldDist[(oldDistPtr - (num - 256)) & 3]; - var LengthNumber = RarDecodeNumber(bstream, RD); - var Length = rLDecode[LengthNumber] +2; + const Distance = rOldDist[(oldDistPtr - (num - 256)) & 3]; + const LengthNumber = RarDecodeNumber(bstream, RD); + let Length = rLDecode[LengthNumber] +2; if ((Bits = rLBits[LengthNumber]) > 0) { Length += bstream.readBits(Bits); } @@ -522,7 +523,7 @@ function Unpack20(bstream, Solid) { continue; } if (num < 270) { - var Distance = rSDDecode[num -= 261] + 1; + let Distance = rSDDecode[num -= 261] + 1; if ((Bits = rSDBits[num]) > 0) { Distance += bstream.readBits(Bits); } @@ -537,38 +538,40 @@ function Unpack20(bstream, Solid) { } function RarUpdateProgress() { - var change = rBuffer.ptr - currentBytesUnarchivedInFile; + const change = rBuffer.ptr - currentBytesUnarchivedInFile; currentBytesUnarchivedInFile = rBuffer.ptr; currentBytesUnarchived += change; postProgress(); } -var rNC20 = 298, - rDC20 = 48, - rRC20 = 28, - rBC20 = 19, - rMC20 = 257; +const rNC20 = 298; +const rDC20 = 48; +const rRC20 = 28; +const rBC20 = 19; +const rMC20 = 257; -var UnpOldTable20 = new Array(rMC20 * 4); +const UnpOldTable20 = new Array(rMC20 * 4); function RarReadTables20(bstream) { - var BitLength = new Array(rBC20); - var Table = new Array(rMC20 * 4); - var TableSize, N, I; - var AudioBlock = bstream.readBits(1); + const BitLength = new Array(rBC20); + const Table = new Array(rMC20 * 4); + let TableSize; + let N; + let I; + const AudioBlock = bstream.readBits(1); if (!bstream.readBits(1)) { - for (var i = UnpOldTable20.length; i--;) { + for (let i = UnpOldTable20.length; i--;) { UnpOldTable20[i] = 0; } } TableSize = rNC20 + rDC20 + rRC20; - for (var I = 0; I < rBC20; I++) { + for (I = 0; I < rBC20; I++) { BitLength[I] = bstream.readBits(4); } RarMakeDecodeTables(BitLength, 0, BD, rBC20); I = 0; while (I < TableSize) { - var num = RarDecodeNumber(bstream, BD); + const num = RarDecodeNumber(bstream, BD); if (num < 16) { Table[I] = num + UnpOldTable20[I] & 0xf; I++; @@ -592,43 +595,43 @@ function RarReadTables20(bstream) { RarMakeDecodeTables(Table, 0, LD, rNC20); RarMakeDecodeTables(Table, rNC20, DD, rDC20); RarMakeDecodeTables(Table, rNC20 + rDC20, RD, rRC20); - for (var i = UnpOldTable20.length; i--;) { + for (let i = UnpOldTable20.length; i--;) { UnpOldTable20[i] = Table[i]; } } -var lowDistRepCount = 0; -var prevLowDist = 0; +let lowDistRepCount = 0; +let prevLowDist = 0; -var rOldDist = [0,0,0,0]; -var lastDist; -var lastLength; +let rOldDist = [0,0,0,0]; +let lastDist; +let lastLength; // ============================================================================================== // // Unpack code specific to RarVM -var VM = new RarVM(); +const VM = new RarVM(); /** * Filters code, one entry per filter. * @type {Array} */ -var Filters = []; +let Filters = []; /** * Filters stack, several entrances of same filter are possible. * @type {Array} */ -var PrgStack = []; +let PrgStack = []; /** * Lengths of preceding blocks, one length per filter. Used to reduce * size required to write block length if lengths are repeating. * @type {Array} */ -var OldFilterLengths = []; +let OldFilterLengths = []; -var LastFilter = 0; +let LastFilter = 0; function InitFilters() { OldFilterLengths = []; @@ -644,9 +647,9 @@ function InitFilters() { */ function RarAddVMCode(firstByte, vmCode) { VM.init(); - var bstream = new bitjs.io.BitStream(vmCode.buffer, true /* rtl */); + const bstream = new bitjs.io.BitStream(vmCode.buffer, true /* rtl */); - var filtPos; + let filtPos; if (firstByte & 0x80) { filtPos = RarVM.readData(bstream); if (filtPos == 0) { @@ -663,11 +666,11 @@ function RarAddVMCode(firstByte, vmCode) { } LastFilter = filtPos; - var newFilter = (filtPos == Filters.length); + const newFilter = (filtPos == Filters.length); // new filter for PrgStack - var stackFilter = new UnpackFilter(); - var filter = null; + const stackFilter = new UnpackFilter(); + let filter = null; // new filter code, never used before since VM reset if (newFilter) { // too many different filters, corrupt archive @@ -686,8 +689,8 @@ function RarAddVMCode(firstByte, vmCode) { filter.ExecCount++; } - var emptyCount = 0; - for (var i = 0; i < PrgStack.length; ++i) { + let emptyCount = 0; + for (let i = 0; i < PrgStack.length; ++i) { PrgStack[i - emptyCount] = PrgStack[i]; if (PrgStack[i] == null) { @@ -703,11 +706,11 @@ function RarAddVMCode(firstByte, vmCode) { emptyCount = 1; } - var stackPos = PrgStack.length - emptyCount; + const stackPos = PrgStack.length - emptyCount; PrgStack[stackPos] = stackFilter; stackFilter.ExecCount = filter.ExecCount; - var blockStart = RarVM.readData(bstream); + let blockStart = RarVM.readData(bstream); if (firstByte & 0x40) { blockStart += 258; } @@ -725,7 +728,7 @@ function RarAddVMCode(firstByte, vmCode) { OldFilterLengths[filtPos] = stackFilter.BlockLength; - for (var i = 0; i < 7; ++i) { + for (let i = 0; i < 7; ++i) { stackFilter.Prg.InitR[i] = 0; } stackFilter.Prg.InitR[3] = VM_GLOBALMEMADDR; @@ -734,8 +737,8 @@ function RarAddVMCode(firstByte, vmCode) { // set registers to optional parameters if any if (firstByte & 0x10) { - var initMask = bstream.readBits(7); - for (var i = 0; i < 7; ++i) { + const initMask = bstream.readBits(7); + for (let i = 0; i < 7; ++i) { if (initMask & (1 << i)) { stackFilter.Prg.InitR[i] = RarVM.readData(bstream); } @@ -743,12 +746,12 @@ function RarAddVMCode(firstByte, vmCode) { } if (newFilter) { - var vmCodeSize = RarVM.readData(bstream); + const vmCodeSize = RarVM.readData(bstream); if (vmCodeSize >= 0x10000 || vmCodeSize == 0) { return false; } - var vmCode = new Uint8Array(vmCodeSize); - for (var i = 0; i < vmCodeSize; ++i) { + const vmCode = new Uint8Array(vmCodeSize); + for (let i = 0; i < vmCodeSize; ++i) { //if (Inp.Overflow(3)) // return(false); vmCode[i] = bstream.readBits(8); @@ -758,10 +761,10 @@ function RarAddVMCode(firstByte, vmCode) { stackFilter.Prg.Cmd = filter.Prg.Cmd; stackFilter.Prg.AltCmd = filter.Prg.Cmd; - var staticDataSize = filter.Prg.StaticData.length; + const staticDataSize = filter.Prg.StaticData.length; if (staticDataSize > 0 && staticDataSize < VM_GLOBALMEMSIZE) { // read statically defined data contained in DB commands - for (var i = 0; i < staticDataSize; ++i) { + for (let i = 0; i < staticDataSize; ++i) { stackFilter.Prg.StaticData[i] = filter.Prg.StaticData[i]; } } @@ -770,15 +773,15 @@ function RarAddVMCode(firstByte, vmCode) { stackFilter.Prg.GlobalData = new Uint8Array(VM_FIXEDGLOBALSIZE); } - var globalData = stackFilter.Prg.GlobalData; - for (var i = 0; i < 7; ++i) { + const globalData = stackFilter.Prg.GlobalData; + for (let i = 0; i < 7; ++i) { VM.setLowEndianValue(globalData, stackFilter.Prg.InitR[i], i * 4); } VM.setLowEndianValue(globalData, stackFilter.BlockLength, 0x1c); VM.setLowEndianValue(globalData, 0, 0x20); VM.setLowEndianValue(globalData, stackFilter.ExecCount, 0x2c); - for (var i = 0; i < 16; ++i) { + for (let i = 0; i < 16; ++i) { globalData[0x30 + i] = 0; } @@ -786,23 +789,23 @@ function RarAddVMCode(firstByte, vmCode) { if (firstByte & 8) { //if (Inp.Overflow(3)) // return(false); - var dataSize = RarVM.readData(bstream); + const dataSize = RarVM.readData(bstream); if (dataSize > (VM_GLOBALMEMSIZE - VM_FIXEDGLOBALSIZE)) { - return(false); + return false; } - var curSize = stackFilter.Prg.GlobalData.length; + const curSize = stackFilter.Prg.GlobalData.length; if (curSize < dataSize + VM_FIXEDGLOBALSIZE) { // Resize global data and update the stackFilter and local variable. - var numBytesToAdd = dataSize + VM_FIXEDGLOBALSIZE - curSize; - var newGlobalData = new Uint8Array(globalData.length + numBytesToAdd); + const numBytesToAdd = dataSize + VM_FIXEDGLOBALSIZE - curSize; + const newGlobalData = new Uint8Array(globalData.length + numBytesToAdd); newGlobalData.set(globalData); stackFilter.Prg.GlobalData = newGlobalData; globalData = newGlobalData; } //byte *GlobalData=&StackFilter->Prg.GlobalData[VM_FIXEDGLOBALSIZE]; - for (var i = 0; i < dataSize; ++i) { + for (let i = 0; i < dataSize; ++i) { //if (Inp.Overflow(3)) // return(false); globalData[VM_FIXEDGLOBALSIZE + i] = bstream.readBits(8); @@ -817,8 +820,8 @@ function RarAddVMCode(firstByte, vmCode) { * @param {!bitjs.io.BitStream} bstream */ function RarReadVMCode(bstream) { - var firstByte = bstream.readBits(8); - var length = (firstByte & 7) + 1; + const firstByte = bstream.readBits(8); + let length = (firstByte & 7) + 1; if (length == 7) { length = bstream.readBits(8) + 7; } else if (length == 8) { @@ -826,8 +829,8 @@ function RarReadVMCode(bstream) { } // Read all bytes of VM code into an array. - var vmCode = new Uint8Array(length); - for (var i = 0; i < length; i++) { + const vmCode = new Uint8Array(length); + for (let i = 0; i < length; i++) { // Do something here with checking readbuf. vmCode[i] = bstream.readBits(8); } @@ -842,21 +845,21 @@ function RarReadVMCode(bstream) { function Unpack29(bstream, Solid) { // lazy initialize rDDecode and rDBits - var DDecode = new Array(rDC); - var DBits = new Array(rDC); + const DDecode = new Array(rDC); + const DBits = new Array(rDC); - var Dist = 0; - var BitLength = 0; - var Slot = 0; + let Dist = 0; + let BitLength = 0; + let Slot = 0; - for (var I = 0; I < rDBitLengthCounts.length; I++,BitLength++) { - for (var J = 0; J < rDBitLengthCounts[I]; J++,Slot++,Dist+=(1<= 271) { - var Length = rLDecode[num -= 271] + 3; + let Length = rLDecode[num -= 271] + 3; if ((Bits = rLBits[num]) > 0) { Length += bstream.readBits(Bits); } - var DistNumber = RarDecodeNumber(bstream, DD); - var Distance = DDecode[DistNumber] + 1; + const DistNumber = RarDecodeNumber(bstream, DD); + let Distance = DDecode[DistNumber] + 1; if ((Bits = DBits[DistNumber]) > 0) { if (DistNumber > 9) { if (Bits > 4) { @@ -896,7 +899,7 @@ function Unpack29(bstream, Solid) { lowDistRepCount--; Distance += prevLowDist; } else { - var LowDist = RarDecodeNumber(bstream, LDD); + const LowDist = RarDecodeNumber(bstream, LDD); if (LowDist == 16) { lowDistRepCount = rLOW_DIST_REP_COUNT - 1; Distance += prevLowDist; @@ -939,16 +942,16 @@ function Unpack29(bstream, Solid) { continue; } if (num < 263) { - var DistNum = num - 259; - var Distance = rOldDist[DistNum]; + const DistNum = num - 259; + const Distance = rOldDist[DistNum]; - for (var I = DistNum; I > 0; I--) { + for (let I = DistNum; I > 0; I--) { rOldDist[I] = rOldDist[I-1]; } rOldDist[0] = Distance; - var LengthNumber = RarDecodeNumber(bstream, RD); - var Length = rLDecode[LengthNumber] + 2; + const LengthNumber = RarDecodeNumber(bstream, RD); + let Length = rLDecode[LengthNumber] + 2; if ((Bits = rLBits[LengthNumber]) > 0) { Length += bstream.readBits(Bits); } @@ -957,7 +960,7 @@ function Unpack29(bstream, Solid) { continue; } if (num < 272) { - var Distance = rSDDecode[num -= 263] + 1; + let Distance = rSDDecode[num -= 263] + 1; if ((Bits = rSDBits[num]) > 0) { Distance += bstream.readBits(Bits); } @@ -976,10 +979,10 @@ function Unpack29(bstream, Solid) { * the filters loaded into the RarVM and writes out to wBuffer. */ function RarWriteBuf() { - var writeSize = (rBuffer.ptr & MAXWINMASK); + let writeSize = (rBuffer.ptr & MAXWINMASK); - for (var i = 0; i < PrgStack.length; ++i) { - var flt = PrgStack[i]; + for (let i = 0; i < PrgStack.length; ++i) { + const flt = PrgStack[i]; if (flt == null) { continue; } @@ -989,8 +992,8 @@ function RarWriteBuf() { continue; } - var blockStart = flt.BlockStart; - var blockLength = flt.BlockLength; + const blockStart = flt.BlockStart; + const blockLength = flt.BlockLength; // WrittenBorder = wBuffer.ptr if (((blockStart - wBuffer.ptr) & MAXWINMASK) < writeSize) { @@ -1000,17 +1003,17 @@ function RarWriteBuf() { writeSize = (rBuffer.ptr - wBuffer.ptr) & MAXWINMASK; } if (blockLength <= writeSize) { - var blockEnd = (blockStart + blockLength) & MAXWINMASK; + const blockEnd = (blockStart + blockLength) & MAXWINMASK; if (blockStart < blockEnd || blockEnd == 0) { VM.setMemory(0, rBuffer.data.subarray(blockStart, blockStart + blockLength), blockLength); } else { - var firstPartLength = MAXWINSIZE - blockStart; + const firstPartLength = MAXWINSIZE - blockStart; VM.setMemory(0, rBuffer.data.subarray(blockStart, blockStart + firstPartLength), firstPartLength); VM.setMemory(firstPartLength, rBuffer.data, blockEnd); } - var parentPrg = Filters[flt.ParentFilter].Prg; - var prg = flt.Prg; + const parentPrg = Filters[flt.ParentFilter].Prg; + const prg = flt.Prg; if (parentPrg.GlobalData.length > VM_FIXEDGLOBALSIZE) { // Copy global data from previous script execution if any. @@ -1021,7 +1024,7 @@ function RarWriteBuf() { if (prg.GlobalData.length > VM_FIXEDGLOBALSIZE) { // Save global data for next script execution. - var globalDataLen = prg.GlobalData.length; + const globalDataLen = prg.GlobalData.length; if (parentPrg.GlobalData.length < globalDataLen) { parentPrg.GlobalData = new Uint8Array(globalDataLen); } @@ -1032,11 +1035,11 @@ function RarWriteBuf() { parentPrg.GlobalData = new Uint8Array(0); } - var filteredData = prg.FilteredData; + let filteredData = prg.FilteredData; PrgStack[i] = null; while (i + 1 < PrgStack.length) { - var nextFilter = PrgStack[i + 1]; + const nextFilter = PrgStack[i + 1]; if (nextFilter == null || nextFilter.BlockStart != blockStart || nextFilter.BlockLength != filteredData.length || nextFilter.NextWindow) { break; @@ -1046,29 +1049,29 @@ function RarWriteBuf() { VM.setMemory(0, filteredData, filteredData.length); - var parentPrg = Filters[nextFilter.ParentFilter].Prg; - var nextPrg = nextFilter.Prg; + const innerParentPrg = Filters[nextFilter.ParentFilter].Prg; + const nextPrg = nextFilter.Prg; - var globalDataLen = parentPrg.GlobalData.length; + const globalDataLen = innerParentPrg.GlobalData.length; if (globalDataLen > VM_FIXEDGLOBALSIZE) { // Copy global data from previous script execution if any. nextPrg.GlobalData = new Uint8Array(globalDataLen); - nextPrg.GlobalData.set(parentPrg.GlobalData.subarray(VM_FIXEDGLOBALSIZE, VM_FIXEDGLOBALSIZE + globalDataLen), VM_FIXEDGLOBALSIZE); + nextPrg.GlobalData.set(innerParentPrg.GlobalData.subarray(VM_FIXEDGLOBALSIZE, VM_FIXEDGLOBALSIZE + globalDataLen), VM_FIXEDGLOBALSIZE); } RarExecuteCode(nextPrg); if (nextPrg.GlobalData.length > VM_GLOBALMEMSIZE) { // Save global data for next script execution. - var globalDataLen = nextPrg.GlobalData.length; - if (parentPrg.GlobalData.length < globalDataLen) { - parentPrg.GlobalData = new Uint8Array(globalDataLen); + const globalDataLen = nextPrg.GlobalData.length; + if (innerParentPrg.GlobalData.length < globalDataLen) { + innerParentPrg.GlobalData = new Uint8Array(globalDataLen); } - parentPrg.GlobalData.set( + innerParentPrg.GlobalData.set( this.mem_.subarray(VM_FIXEDGLOBALSIZE, VM_FIXEDGLOBALSIZE + globalDataLen), VM_FIXEDGLOBALSIZE); } else { - parentPrg.GlobalData = new Uint8Array(0); + innerParentPrg.GlobalData = new Uint8Array(0); } filteredData = nextPrg.FilteredData; @@ -1076,23 +1079,22 @@ function RarWriteBuf() { PrgStack[i] = null; } // while (i + 1 < PrgStack.length) - for (var j = 0; j < filteredData.length; ++j) { + for (let j = 0; j < filteredData.length; ++j) { wBuffer.insertByte(filteredData[j]); } writeSize = (rBuffer.ptr - wBuffer.ptr) & MAXWINMASK; } // if (blockLength <= writeSize) else { - for (var j = i; j < PrgStack.length; ++j) { - var flt = PrgStack[j]; - if (flt != null && flt.NextWindow) { - flt.NextWindow = false; + for (let j = i; j < PrgStack.length; ++j) { + const theFlt = PrgStack[j]; + if (theFlt != null && theFlt.NextWindow) { + theFlt.NextWindow = false; } } - //WrPtr=WrittenBorder; return; } } // if (((blockStart - wBuffer.ptr) & MAXWINMASK) < writeSize) - } // for (var i = 0; i < PrgStack.length; ++i) + } // for (let i = 0; i < PrgStack.length; ++i) // Write any remaining bytes from rBuffer to wBuffer; RarWriteArea(wBuffer.ptr, rBuffer.ptr); @@ -1126,11 +1128,11 @@ function RarWriteData(offset, numBytes) { if (wBuffer.ptr >= rBuffer.data.length) { return; } - var leftToWrite = rBuffer.data.length - wBuffer.ptr; + const leftToWrite = rBuffer.data.length - wBuffer.ptr; if (numBytes > leftToWrite) { numBytes = leftToWrite; } - for (var i = 0; i < numBytes; ++i) { + for (let i = 0; i < numBytes; ++i) { wBuffer.insertByte(rBuffer.data[offset + i]); } } @@ -1141,7 +1143,7 @@ function RarWriteData(offset, numBytes) { function RarExecuteCode(prg) { if (prg.GlobalData.length > 0) { - var writtenFileSize = wBuffer.ptr; + const writtenFileSize = wBuffer.ptr; prg.InitR[6] = writtenFileSize; VM.setLowEndianValue(prg.GlobalData, writtenFileSize, 0x24); VM.setLowEndianValue(prg.GlobalData, (writtenFileSize >>> 32) >> 0, 0x28); @@ -1152,7 +1154,8 @@ function RarExecuteCode(prg) function RarReadEndOfBlock(bstream) { RarUpdateProgress(); - var NewTable = false, NewFile = false; + let NewTable = false; + let NewFile = false; if (bstream.readBits(1)) { NewTable = true; } else { @@ -1181,9 +1184,9 @@ function RarInsertOldDist(distance) { * pointer to start copying from. */ function RarCopyString(len, distance) { - var srcPtr = rBuffer.ptr - distance; + let srcPtr = rBuffer.ptr - distance; if (srcPtr < 0) { - var l = rOldBuffers.length; + let l = rOldBuffers.length; while (srcPtr < 0) { srcPtr = rOldBuffers[--l].data.length + srcPtr; } @@ -1206,9 +1209,9 @@ function RarCopyString(len, distance) { */ function unpack(v) { // TODO: implement what happens when unpVer is < 15 - var Ver = v.header.unpVer <= 15 ? 15 : v.header.unpVer; - var Solid = v.header.LHD_SOLID; - var bstream = new bitjs.io.BitStream(v.fileData.buffer, true /* rtl */, v.fileData.byteOffset, v.fileData.byteLength ); + const Ver = v.header.unpVer <= 15 ? 15 : v.header.unpVer; + const Solid = v.header.LHD_SOLID; + const bstream = new bitjs.io.BitStream(v.fileData.buffer, true /* rtl */, v.fileData.byteOffset, v.fileData.byteLength ); rBuffer = new bitjs.io.ByteBuffer(v.header.unpackedSize); @@ -1235,7 +1238,7 @@ function unpack(v) { } // bstream is a bit stream -var RarLocalFile = function(bstream) { +const RarLocalFile = function(bstream) { this.header = new RarVolumeHeader(bstream); this.filename = this.header.filename; @@ -1264,8 +1267,8 @@ RarLocalFile.prototype.unrar = function() { currentBytesUnarchived += this.fileData.length; // Create a new buffer and copy it over. - var len = this.header.packSize; - var newBuffer = new bitjs.io.ByteBuffer(len); + const len = this.header.packSize; + const newBuffer = new bitjs.io.ByteBuffer(len); newBuffer.insertBytes(this.fileData); this.fileData = newBuffer.data; } else { @@ -1275,7 +1278,7 @@ RarLocalFile.prototype.unrar = function() { } } -var unrar = function(arrayBuffer) { +const unrar = function(arrayBuffer) { currentFilename = ""; currentFileNumber = 0; currentBytesUnarchivedInFile = 0; @@ -1284,22 +1287,22 @@ var unrar = function(arrayBuffer) { totalFilesInArchive = 0; postMessage(new bitjs.archive.UnarchiveStartEvent()); - var bstream = new bitjs.io.BitStream(arrayBuffer, false /* rtl */); + const bstream = new bitjs.io.BitStream(arrayBuffer, false /* rtl */); - var header = new RarVolumeHeader(bstream); + const header = new RarVolumeHeader(bstream); if (header.crc == 0x6152 && header.headType == 0x72 && header.flags.value == 0x1A21 && header.headSize == 7) { info("Found RAR signature"); - var mhead = new RarVolumeHeader(bstream); + const mhead = new RarVolumeHeader(bstream); if (mhead.headType != MAIN_HEAD) { info("Error! RAR did not include a MAIN_HEAD header"); } else { - var localFiles = [], - localFile = null; + let localFiles = []; + let localFile = null; do { try { localFile = new RarLocalFile(bstream); @@ -1320,14 +1323,12 @@ var unrar = function(arrayBuffer) { // now we have all information but things are unpacked // TODO: unpack localFiles = localFiles.sort(function(a,b) { - var aname = a.filename.toLowerCase(); - var bname = b.filename.toLowerCase(); - return aname > bname ? 1 : -1; + return a.filename.toLowerCase() > b.filename.toLowerCase() ? 1 : -1; }); info(localFiles.map(function(a){return a.filename}).join(', ')); - for (var i = 0; i < localFiles.length; ++i) { - var localfile = localFiles[i]; + for (let i = 0; i < localFiles.length; ++i) { + const localfile = localFiles[i]; // update progress currentFilename = localfile.header.filename; @@ -1335,7 +1336,7 @@ var unrar = function(arrayBuffer) { // actually do the unzipping localfile.unrar(); - + if (localfile.isValid) { postMessage(new bitjs.archive.UnarchiveExtractEvent(localfile)); postProgress(); @@ -1353,6 +1354,6 @@ var unrar = function(arrayBuffer) { // event.data.file has the ArrayBuffer. onmessage = function(event) { - var ab = event.data.file; + const ab = event.data.file; unrar(ab, true); }; diff --git a/archive/untar.js b/archive/untar.js index 1116d0b..3360cd8 100644 --- a/archive/untar.js +++ b/archive/untar.js @@ -15,21 +15,21 @@ importScripts('../io/bytestream.js'); importScripts('archive.js'); // Progress variables. -var currentFilename = ""; -var currentFileNumber = 0; -var currentBytesUnarchivedInFile = 0; -var currentBytesUnarchived = 0; -var totalUncompressedBytesInArchive = 0; -var totalFilesInArchive = 0; +let currentFilename = ""; +let currentFileNumber = 0; +let currentBytesUnarchivedInFile = 0; +let currentBytesUnarchived = 0; +let totalUncompressedBytesInArchive = 0; +let totalFilesInArchive = 0; // Helper functions. -var info = function(str) { +const info = function(str) { postMessage(new bitjs.archive.UnarchiveInfoEvent(str)); }; -var err = function(str) { +const err = function(str) { postMessage(new bitjs.archive.UnarchiveErrorEvent(str)); }; -var postProgress = function() { +const postProgress = function() { postMessage(new bitjs.archive.UnarchiveProgressEvent( currentFilename, currentFileNumber, @@ -40,14 +40,14 @@ var postProgress = function() { }; // Removes all characters from the first zero-byte in the string onwards. -var readCleanString = function(bstr, numBytes) { - var str = bstr.readString(numBytes); - var zIndex = str.indexOf(String.fromCharCode(0)); +const readCleanString = function(bstr, numBytes) { + const str = bstr.readString(numBytes); + const zIndex = str.indexOf(String.fromCharCode(0)); return zIndex != -1 ? str.substr(0, zIndex) : str; }; // takes a ByteStream and parses out the local file information -var TarLocalFile = function(bstream) { +const TarLocalFile = function(bstream) { this.isValid = false; // Read in the header block @@ -89,7 +89,7 @@ var TarLocalFile = function(bstream) { // A regular file. if (this.typeflag == 0) { info(" This is a regular file."); - var sizeInBytes = parseInt(this.size); + const sizeInBytes = parseInt(this.size); this.fileData = new Uint8Array(bstream.bytes.buffer, bstream.ptr, this.size); if (this.name.length > 0 && this.size > 0 && this.fileData && this.fileData.buffer) { this.isValid = true; @@ -98,7 +98,7 @@ var TarLocalFile = function(bstream) { bstream.readBytes(this.size); // Round up to 512-byte blocks. - var remaining = 512 - bstream.ptr % 512; + const remaining = 512 - bstream.ptr % 512; if (remaining > 0 && remaining < 512) { bstream.readBytes(remaining); } @@ -110,7 +110,7 @@ var TarLocalFile = function(bstream) { // Takes an ArrayBuffer of a tar file in // returns null on error // returns an array of DecompressedFile objects on success -var untar = function(arrayBuffer) { +const untar = function(arrayBuffer) { currentFilename = ""; currentFileNumber = 0; currentBytesUnarchivedInFile = 0; @@ -119,12 +119,12 @@ var untar = function(arrayBuffer) { totalFilesInArchive = 0; postMessage(new bitjs.archive.UnarchiveStartEvent()); - var bstream = new bitjs.io.ByteStream(arrayBuffer); - var localFiles = []; + const bstream = new bitjs.io.ByteStream(arrayBuffer); + const localFiles = []; // While we don't encounter an empty block, keep making TarLocalFiles. while (bstream.peekNumber(4) != 0) { - var oneLocalFile = new TarLocalFile(bstream); + const oneLocalFile = new TarLocalFile(bstream); if (oneLocalFile && oneLocalFile.isValid) { localFiles.push(oneLocalFile); totalUncompressedBytesInArchive += oneLocalFile.size; @@ -134,29 +134,7 @@ var untar = function(arrayBuffer) { // got all local files, now sort them localFiles.sort(function(a,b) { - var aname = a.filename; - var bname = b.filename; - return aname > bname ? 1 : -1; - - // extract the number at the end of both filenames - /* - var aname = a.filename; - var bname = b.filename; - var aindex = aname.length, bindex = bname.length; - - // Find the last number character from the back of the filename. - while (aname[aindex-1] < '0' || aname[aindex-1] > '9') --aindex; - while (bname[bindex-1] < '0' || bname[bindex-1] > '9') --bindex; - - // Find the first number character from the back of the filename - while (aname[aindex-1] >= '0' && aname[aindex-1] <= '9') --aindex; - while (bname[bindex-1] >= '0' && bname[bindex-1] <= '9') --bindex; - - // parse them into numbers and return comparison - var anum = parseInt(aname.substr(aindex), 10), - bnum = parseInt(bname.substr(bindex), 10); - return anum - bnum; - */ + return a.filename > b.filename ? 1 : -1; }); // report # files and total length @@ -165,8 +143,8 @@ var untar = function(arrayBuffer) { } // now do the shipping of each file - for (var i = 0; i < localFiles.length; ++i) { - var localfile = localFiles[i]; + for (let i = 0; i < localFiles.length; ++i) { + const localfile = localFiles[i]; info("Sending file '" + localfile.filename + "' up"); // update progress @@ -185,6 +163,6 @@ var untar = function(arrayBuffer) { // event.data.file has the ArrayBuffer. onmessage = function(event) { - var ab = event.data.file; + const ab = event.data.file; untar(ab); }; diff --git a/archive/unzip.js b/archive/unzip.js index 3349faf..40da6b3 100644 --- a/archive/unzip.js +++ b/archive/unzip.js @@ -19,21 +19,21 @@ importScripts('../io/bytestream.js'); importScripts('archive.js'); // Progress variables. -var currentFilename = ""; -var currentFileNumber = 0; -var currentBytesUnarchivedInFile = 0; -var currentBytesUnarchived = 0; -var totalUncompressedBytesInArchive = 0; -var totalFilesInArchive = 0; +let currentFilename = ""; +let currentFileNumber = 0; +let currentBytesUnarchivedInFile = 0; +let currentBytesUnarchived = 0; +let totalUncompressedBytesInArchive = 0; +let totalFilesInArchive = 0; // Helper functions. -var info = function(str) { +const info = function(str) { postMessage(new bitjs.archive.UnarchiveInfoEvent(str)); }; -var err = function(str) { +const err = function(str) { postMessage(new bitjs.archive.UnarchiveErrorEvent(str)); }; -var postProgress = function() { +const postProgress = function() { postMessage(new bitjs.archive.UnarchiveProgressEvent( currentFilename, currentFileNumber, @@ -43,15 +43,15 @@ var postProgress = function() { totalFilesInArchive)); }; -var zLocalFileHeaderSignature = 0x04034b50; -var zArchiveExtraDataSignature = 0x08064b50; -var zCentralFileHeaderSignature = 0x02014b50; -var zDigitalSignatureSignature = 0x05054b50; -var zEndOfCentralDirSignature = 0x06064b50; -var zEndOfCentralDirLocatorSignature = 0x07064b50; +const zLocalFileHeaderSignature = 0x04034b50; +const zArchiveExtraDataSignature = 0x08064b50; +const zCentralFileHeaderSignature = 0x02014b50; +const zDigitalSignatureSignature = 0x05054b50; +const zEndOfCentralDirSignature = 0x06064b50; +const zEndOfCentralDirLocatorSignature = 0x07064b50; // takes a ByteStream and parses out the local file information -var ZipLocalFile = function(bstream) { +const ZipLocalFile = function(bstream) { if (typeof bstream != typeof {} || !bstream.readNumber || typeof bstream.readNumber != typeof function(){}) { return null; } @@ -112,7 +112,6 @@ var ZipLocalFile = function(bstream) { // determine what kind of compressed data we have and decompress ZipLocalFile.prototype.unzip = function() { - // Zip Version 1.0, no compression (store only) if (this.compressionMethod == 0 ) { info("ZIP v"+this.version+", store only: " + this.filename + " (" + this.compressedSize + " bytes)"); @@ -134,7 +133,7 @@ ZipLocalFile.prototype.unzip = function() { // Takes an ArrayBuffer of a zip file in // returns null on error // returns an array of DecompressedFile objects on success -var unzip = function(arrayBuffer) { +const unzip = function(arrayBuffer) { postMessage(new bitjs.archive.UnarchiveStartEvent()); currentFilename = ""; @@ -145,13 +144,13 @@ var unzip = function(arrayBuffer) { totalFilesInArchive = 0; currentBytesUnarchived = 0; - var bstream = new bitjs.io.ByteStream(arrayBuffer); + const bstream = new bitjs.io.ByteStream(arrayBuffer); // detect local file header signature or return null if (bstream.peekNumber(4) == zLocalFileHeaderSignature) { - var localFiles = []; + const localFiles = []; // loop until we don't see any more local files while (bstream.peekNumber(4) == zLocalFileHeaderSignature) { - var oneLocalFile = new ZipLocalFile(bstream); + const oneLocalFile = new ZipLocalFile(bstream); // this should strip out directories/folders if (oneLocalFile && oneLocalFile.uncompressedSize > 0 && oneLocalFile.fileData) { localFiles.push(oneLocalFile); @@ -162,29 +161,7 @@ var unzip = function(arrayBuffer) { // got all local files, now sort them localFiles.sort(function(a,b) { - var aname = a.filename; - var bname = b.filename; - return aname > bname ? 1 : -1; - - // extract the number at the end of both filenames - /* - var aname = a.filename; - var bname = b.filename; - var aindex = aname.length, bindex = bname.length; - - // Find the last number character from the back of the filename. - while (aname[aindex-1] < '0' || aname[aindex-1] > '9') --aindex; - while (bname[bindex-1] < '0' || bname[bindex-1] > '9') --bindex; - - // Find the first number character from the back of the filename - while (aname[aindex-1] >= '0' && aname[aindex-1] <= '9') --aindex; - while (bname[bindex-1] >= '0' && bname[bindex-1] <= '9') --bindex; - - // parse them into numbers and return comparison - var anum = parseInt(aname.substr(aindex), 10), - bnum = parseInt(bname.substr(bindex), 10); - return anum - bnum; - */ + return a.filename > b.filename ? 1 : -1; }); // archive extra data record @@ -193,7 +170,7 @@ var unzip = function(arrayBuffer) { // skipping this record for now bstream.readNumber(4); - var archiveExtraFieldLength = bstream.readNumber(4); + const archiveExtraFieldLength = bstream.readNumber(4); bstream.readString(archiveExtraFieldLength); } @@ -214,9 +191,9 @@ var unzip = function(arrayBuffer) { bstream.readNumber(4); // crc32 bstream.readNumber(4); // compressed size bstream.readNumber(4); // uncompressed size - var fileNameLength = bstream.readNumber(2); // file name length - var extraFieldLength = bstream.readNumber(2); // extra field length - var fileCommentLength = bstream.readNumber(2); // file comment length + const fileNameLength = bstream.readNumber(2); // file name length + const extraFieldLength = bstream.readNumber(2); // extra field length + const fileCommentLength = bstream.readNumber(2); // file comment length bstream.readNumber(2); // disk number start bstream.readNumber(2); // internal file attributes bstream.readNumber(4); // external file attributes @@ -233,7 +210,7 @@ var unzip = function(arrayBuffer) { info(" Found a Digital Signature"); bstream.readNumber(4); - var sizeOfSignature = bstream.readNumber(2); + const sizeOfSignature = bstream.readNumber(2); bstream.readString(sizeOfSignature); // digital signature data } @@ -243,8 +220,8 @@ var unzip = function(arrayBuffer) { } // now do the unzipping of each file - for (var i = 0; i < localFiles.length; ++i) { - var localfile = localFiles[i]; + for (let i = 0; i < localFiles.length; ++i) { + const localfile = localFiles[i]; // update progress currentFilename = localfile.filename; @@ -275,13 +252,13 @@ function getHuffmanCodes(bitLengths) { } // Reference: http://tools.ietf.org/html/rfc1951#page-8 - var numLengths = bitLengths.length, - bl_count = [], - MAX_BITS = 1; + const numLengths = bitLengths.length; + const bl_count = []; + let MAX_BITS = 1; // Step 1: count up how many codes of each length we have - for (var i = 0; i < numLengths; ++i) { - var length = bitLengths[i]; + for (let i = 0; i < numLengths; ++i) { + const length = bitLengths[i]; // test to ensure each bit length is a positive, non-zero number if (typeof length != typeof 1 || length < 0) { err("bitLengths contained an invalid number in getHuffmanCodes(): " + length + " of type " + (typeof length)); @@ -296,10 +273,10 @@ function getHuffmanCodes(bitLengths) { } // Step 2: Find the numerical value of the smallest code for each code length - var next_code = [], - code = 0; - for (var bits = 1; bits <= MAX_BITS; ++bits) { - var length = bits-1; + const next_code = []; + let code = 0; + for (let bits = 1; bits <= MAX_BITS; ++bits) { + const length = bits-1; // ensure undefined lengths are zero if (bl_count[length] == undefined) bl_count[length] = 0; code = (code + bl_count[bits-1]) << 1; @@ -307,9 +284,10 @@ function getHuffmanCodes(bitLengths) { } // Step 3: Assign numerical values to all codes - var table = {}, tableLength = 0; - for (var n = 0; n < numLengths; ++n) { - var len = bitLengths[n]; + const table = {}; + let tableLength = 0; + for (let n = 0; n < numLengths; ++n) { + const len = bitLengths[n]; if (len != 0) { table[next_code[len]] = { length: len, symbol: n }; //, bitstring: binaryValueToString(next_code[len],len) }; tableLength++; @@ -338,16 +316,16 @@ function getHuffmanCodes(bitLengths) { 11000111 */ // fixed Huffman codes go from 7-9 bits, so we need an array whose index can hold up to 9 bits -var fixedHCtoLiteral = null; -var fixedHCtoDistance = null; +let fixedHCtoLiteral = null; +let fixedHCtoDistance = null; function getFixedLiteralTable() { // create once if (!fixedHCtoLiteral) { - var bitlengths = new Array(288); - for (var i = 0; i <= 143; ++i) bitlengths[i] = 8; - for (i = 144; i <= 255; ++i) bitlengths[i] = 9; - for (i = 256; i <= 279; ++i) bitlengths[i] = 7; - for (i = 280; i <= 287; ++i) bitlengths[i] = 8; + const bitlengths = new Array(288); + for (let i = 0; i <= 143; ++i) bitlengths[i] = 8; + for (let i = 144; i <= 255; ++i) bitlengths[i] = 9; + for (let i = 256; i <= 279; ++i) bitlengths[i] = 7; + for (let i = 280; i <= 287; ++i) bitlengths[i] = 8; // get huffman code table fixedHCtoLiteral = getHuffmanCodes(bitlengths); @@ -358,8 +336,8 @@ function getFixedLiteralTable() { function getFixedDistanceTable() { // create once if (!fixedHCtoDistance) { - var bitlengths = new Array(32); - for (var i = 0; i < 32; ++i) { bitlengths[i] = 5; } + const bitlengths = new Array(32); + for (let i = 0; i < 32; ++i) { bitlengths[i] = 5; } // get huffman code table fixedHCtoDistance = getHuffmanCodes(bitlengths); @@ -370,13 +348,14 @@ function getFixedDistanceTable() { // extract one bit at a time until we find a matching Huffman Code // then return that symbol function decodeSymbol(bstream, hcTable) { - var code = 0, len = 0; - var match = false; + let code = 0; + let len = 0; + let match = false; // loop until we match for (;;) { // read in next bit - var bit = bstream.readBits(1); + const bit = bstream.readBits(1); code = (code<<1) | bit; ++len; @@ -394,7 +373,7 @@ function decodeSymbol(bstream, hcTable) { } -var CodeLengthCodeOrder = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; +const CodeLengthCodeOrder = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; /* Extra Extra Extra Code Bits Length(s) Code Bits Lengths Code Bits Length(s) @@ -410,15 +389,15 @@ var CodeLengthCodeOrder = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2 265 1 11,12 275 3 51-58 285 0 258 266 1 13,14 276 3 59-66 */ -var LengthLookupTable = [ - [0,3], [0,4], [0,5], [0,6], - [0,7], [0,8], [0,9], [0,10], - [1,11], [1,13], [1,15], [1,17], - [2,19], [2,23], [2,27], [2,31], - [3,35], [3,43], [3,51], [3,59], - [4,67], [4,83], [4,99], [4,115], - [5,131], [5,163], [5,195], [5,227], - [0,258] +const LengthLookupTable = [ + [0,3], [0,4], [0,5], [0,6], + [0,7], [0,8], [0,9], [0,10], + [1,11], [1,13], [1,15], [1,17], + [2,19], [2,23], [2,27], [2,31], + [3,35], [3,43], [3,51], [3,59], + [4,67], [4,83], [4,99], [4,115], + [5,131], [5,163], [5,195], [5,227], + [0,258] ]; /* Extra Extra Extra @@ -435,7 +414,7 @@ var LengthLookupTable = [ 8 3 17-24 18 8 513-768 28 13 16385-24576 9 3 25-32 19 8 769-1024 29 13 24577-32768 */ -var DistLookupTable = [ +const DistLookupTable = [ [0,1], [0,2], [0,3], [0,4], [1,5], [1,7], [2,9], [2,13], @@ -468,9 +447,10 @@ function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) { stream, and copy length bytes from this position to the output stream. */ - var numSymbols = 0, blockSize = 0; + let numSymbols = 0; + let blockSize = 0; for (;;) { - var symbol = decodeSymbol(bstream, hcLiteralTable); + const symbol = decodeSymbol(bstream, hcLiteralTable); ++numSymbols; if (symbol < 256) { // copy literal byte to output @@ -483,10 +463,10 @@ function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) { break; } else { - var lengthLookup = LengthLookupTable[symbol-257], - length = lengthLookup[1] + bstream.readBits(lengthLookup[0]), - distLookup = DistLookupTable[decodeSymbol(bstream, hcDistanceTable)], - distance = distLookup[1] + bstream.readBits(distLookup[0]); + const lengthLookup = LengthLookupTable[symbol - 257]; + let length = lengthLookup[1] + bstream.readBits(lengthLookup[0]); + const distLookup = DistLookupTable[decodeSymbol(bstream, hcDistanceTable)]; + let distance = distLookup[1] + bstream.readBits(distLookup[0]); // now apply length and distance appropriately and copy to output @@ -499,10 +479,10 @@ function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) { // adds X,Y,X,Y,X to the output stream." // // loop for each character - var ch = buffer.ptr - distance; + let ch = buffer.ptr - distance; blockSize += length; if(length > distance) { - var data = buffer.data; + const data = buffer.data; while (length--) { buffer.insertByte(data[ch++]); } @@ -520,27 +500,29 @@ function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) { // deflate: http://tools.ietf.org/html/rfc1951 function inflate(compressedData, numDecompressedBytes) { // Bit stream representing the compressed data. - var bstream = new bitjs.io.BitStream(compressedData.buffer, + const bstream = new bitjs.io.BitStream(compressedData.buffer, false /* rtl */, compressedData.byteOffset, compressedData.byteLength); - var buffer = new bitjs.io.ByteBuffer(numDecompressedBytes); - var numBlocks = 0, blockSize = 0; + const buffer = new bitjs.io.ByteBuffer(numDecompressedBytes); + let numBlocks = 0; + let blockSize = 0; // block format: http://tools.ietf.org/html/rfc1951#page-9 + let bFinal = 0; do { - var bFinal = bstream.readBits(1), - bType = bstream.readBits(2); + bFinal = bstream.readBits(1); + let bType = bstream.readBits(2); blockSize = 0; ++numBlocks; // no compression if (bType == 0) { // skip remaining bits in this byte while (bstream.bitPtr != 0) bstream.readBits(1); - var len = bstream.readBits(16), - nlen = bstream.readBits(16); + const len = bstream.readBits(16); + const nlen = bstream.readBits(16); // TODO: check if nlen is the ones-complement of len? - if(len > 0) buffer.insertBytes(bstream.readBytes(len)); + if (len > 0) buffer.insertBytes(bstream.readBytes(len)); blockSize = len; } // fixed Huffman codes @@ -549,18 +531,18 @@ function inflate(compressedData, numDecompressedBytes) { } // dynamic Huffman codes else if(bType == 2) { - var numLiteralLengthCodes = bstream.readBits(5) + 257; - var numDistanceCodes = bstream.readBits(5) + 1, - numCodeLengthCodes = bstream.readBits(4) + 4; + const numLiteralLengthCodes = bstream.readBits(5) + 257; + const numDistanceCodes = bstream.readBits(5) + 1; + const numCodeLengthCodes = bstream.readBits(4) + 4; // populate the array of code length codes (first de-compaction) - var codeLengthsCodeLengths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; - for (var i = 0; i < numCodeLengthCodes; ++i) { + const codeLengthsCodeLengths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; + for (let i = 0; i < numCodeLengthCodes; ++i) { codeLengthsCodeLengths[ CodeLengthCodeOrder[i] ] = bstream.readBits(3); } // get the Huffman Codes for the code lengths - var codeLengthsCodes = getHuffmanCodes(codeLengthsCodeLengths); + const codeLengthsCodes = getHuffmanCodes(codeLengthsCodeLengths); // now follow this mapping /* @@ -578,28 +560,28 @@ function inflate(compressedData, numDecompressedBytes) { */ // to generate the true code lengths of the Huffman Codes for the literal // and distance tables together - var literalCodeLengths = []; - var prevCodeLength = 0; + const literalCodeLengths = []; + let prevCodeLength = 0; while (literalCodeLengths.length < numLiteralLengthCodes + numDistanceCodes) { - var symbol = decodeSymbol(bstream, codeLengthsCodes); + const symbol = decodeSymbol(bstream, codeLengthsCodes); if (symbol <= 15) { literalCodeLengths.push(symbol); prevCodeLength = symbol; } else if (symbol == 16) { - var repeat = bstream.readBits(2) + 3; + let repeat = bstream.readBits(2) + 3; while (repeat--) { literalCodeLengths.push(prevCodeLength); } } else if (symbol == 17) { - var repeat = bstream.readBits(3) + 3; + let repeat = bstream.readBits(3) + 3; while (repeat--) { literalCodeLengths.push(0); } } else if (symbol == 18) { - var repeat = bstream.readBits(7) + 11; + let repeat = bstream.readBits(7) + 11; while (repeat--) { literalCodeLengths.push(0); } @@ -607,11 +589,11 @@ function inflate(compressedData, numDecompressedBytes) { } // now split the distance code lengths out of the literal code array - var distanceCodeLengths = literalCodeLengths.splice(numLiteralLengthCodes, numDistanceCodes); + const distanceCodeLengths = literalCodeLengths.splice(numLiteralLengthCodes, numDistanceCodes); // now generate the true Huffman Code tables using these code lengths - var hcLiteralTable = getHuffmanCodes(literalCodeLengths), - hcDistanceTable = getHuffmanCodes(distanceCodeLengths); + const hcLiteralTable = getHuffmanCodes(literalCodeLengths); + const hcDistanceTable = getHuffmanCodes(distanceCodeLengths); blockSize = inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer); } // error diff --git a/io/bitstream.js b/io/bitstream.js index 47e7c9c..79b14c0 100644 --- a/io/bitstream.js +++ b/io/bitstream.js @@ -21,7 +21,7 @@ bitjs.BIT = [ 0x01, 0x02, 0x04, 0x08, 0x1000, 0x2000, 0x4000, 0x8000]; // mask for getting N number of bits (0-8) -var BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF ]; +const BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF ]; /** @@ -38,8 +38,8 @@ bitjs.io.BitStream = function(ab, rtl, opt_offset, opt_length) { throw "Error! BitArray constructed with an invalid ArrayBuffer object"; } - var offset = opt_offset || 0; - var length = opt_length || ab.byteLength; + const offset = opt_offset || 0; + const length = opt_length || ab.byteLength; this.bytes = new Uint8Array(ab, offset, length); this.bytePtr = 0; // tracks which byte we are on this.bitPtr = 0; // tracks which bit we are on (can have values 0 through 7) @@ -57,17 +57,17 @@ bitjs.io.BitStream = function(ab, rtl, opt_offset, opt_length) { * @param {boolean=} movePointers Whether to move the pointer, defaults false. * @return {number} The peeked bits, as an unsigned number. */ -bitjs.io.BitStream.prototype.peekBits_ltr = function(n, movePointers) { +bitjs.io.BitStream.prototype.peekBits_ltr = function(n, opt_movePointers) { if (n <= 0 || typeof n != typeof 1) { return 0; } - var movePointers = movePointers || false, - bytePtr = this.bytePtr, - bitPtr = this.bitPtr, - result = 0, - bitsIn = 0, - bytes = this.bytes; + const movePointers = opt_movePointers || false; + const bytes = this.bytes; + let bytePtr = this.bytePtr; + let bitPtr = this.bitPtr; + let result = 0; + let bitsIn = 0; // keep going until we have no more bits left to peek at // TODO: Consider putting all bits from bytes we will need into a variable and then @@ -80,9 +80,9 @@ bitjs.io.BitStream.prototype.peekBits_ltr = function(n, movePointers) { return -1; } - var numBitsLeftInThisByte = (8 - bitPtr); + const numBitsLeftInThisByte = (8 - bitPtr); if (n >= numBitsLeftInThisByte) { - var mask = (BITMASK[numBitsLeftInThisByte] << bitPtr); + const mask = (BITMASK[numBitsLeftInThisByte] << bitPtr); result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn); bytePtr++; @@ -91,7 +91,7 @@ bitjs.io.BitStream.prototype.peekBits_ltr = function(n, movePointers) { n -= numBitsLeftInThisByte; } else { - var mask = (BITMASK[n] << bitPtr); + const mask = (BITMASK[n] << bitPtr); result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn); bitPtr += n; @@ -119,16 +119,16 @@ bitjs.io.BitStream.prototype.peekBits_ltr = function(n, movePointers) { * @param {boolean=} movePointers Whether to move the pointer, defaults false. * @return {number} The peeked bits, as an unsigned number. */ -bitjs.io.BitStream.prototype.peekBits_rtl = function(n, movePointers) { +bitjs.io.BitStream.prototype.peekBits_rtl = function(n, opt_movePointers) { if (n <= 0 || typeof n != typeof 1) { return 0; } - var movePointers = movePointers || false, - bytePtr = this.bytePtr, - bitPtr = this.bitPtr, - result = 0, - bytes = this.bytes; + const movePointers = opt_movePointers || false; + const bytes = this.bytes; + let bytePtr = this.bytePtr; + let bitPtr = this.bitPtr; + let result = 0; // keep going until we have no more bits left to peek at // TODO: Consider putting all bits from bytes we will need into a variable and then @@ -142,7 +142,7 @@ bitjs.io.BitStream.prototype.peekBits_rtl = function(n, movePointers) { return -1; } - var numBitsLeftInThisByte = (8 - bitPtr); + const numBitsLeftInThisByte = (8 - bitPtr); if (n >= numBitsLeftInThisByte) { result <<= numBitsLeftInThisByte; result |= (BITMASK[numBitsLeftInThisByte] & bytes[bytePtr]); @@ -199,7 +199,7 @@ bitjs.io.BitStream.prototype.readBits = function(n) { * @param {boolean=} movePointers Whether to move the pointer, defaults false. * @return {Uint8Array} The subarray. */ -bitjs.io.BitStream.prototype.peekBytes = function(n, movePointers) { +bitjs.io.BitStream.prototype.peekBytes = function(n, opt_movePointers) { if (n <= 0 || typeof n != typeof 1) { return 0; } @@ -210,11 +210,11 @@ bitjs.io.BitStream.prototype.peekBytes = function(n, movePointers) { this.readBits(1); } - var movePointers = movePointers || false; - var bytePtr = this.bytePtr, - bitPtr = this.bitPtr; + const movePointers = opt_movePointers || false; + let bytePtr = this.bytePtr; + let bitPtr = this.bitPtr; - var result = this.bytes.subarray(bytePtr, bytePtr + n); + const result = this.bytes.subarray(bytePtr, bytePtr + n); if (movePointers) { this.bytePtr += n; diff --git a/io/bytebuffer.js b/io/bytebuffer.js index 811d0ab..937d43b 100644 --- a/io/bytebuffer.js +++ b/io/bytebuffer.js @@ -67,9 +67,9 @@ bitjs.io.ByteBuffer.prototype.writeNumber = function(num, numBytes) { } // Roll 8-bits at a time into an array of bytes. - var bytes = []; + const bytes = []; while (numBytes-- > 0) { - var eightBits = num & 255; + const eightBits = num & 255; bytes.push(eightBits); num >>= 8; } @@ -89,15 +89,15 @@ bitjs.io.ByteBuffer.prototype.writeSignedNumber = function(num, numBytes) { throw 'Trying to write into too few bytes: ' + numBytes; } - var HALF = Math.pow(2, (numBytes * 8) - 1); + const HALF = Math.pow(2, (numBytes * 8) - 1); if (num >= HALF || num < -HALF) { throw 'Trying to write ' + num + ' into only ' + numBytes + ' bytes'; } // Roll 8-bits at a time into an array of bytes. - var bytes = []; + const bytes = []; while (numBytes-- > 0) { - var eightBits = num & 255; + const eightBits = num & 255; bytes.push(eightBits); num >>= 8; } @@ -110,8 +110,8 @@ bitjs.io.ByteBuffer.prototype.writeSignedNumber = function(num, numBytes) { * @param {string} str The ASCII string to write. */ bitjs.io.ByteBuffer.prototype.writeASCIIString = function(str) { - for (var i = 0; i < str.length; ++i) { - var curByte = str.charCodeAt(i); + for (let i = 0; i < str.length; ++i) { + const curByte = str.charCodeAt(i); if (curByte < 0 || curByte > 255) { throw 'Trying to write a non-ASCII string!'; } diff --git a/io/bytestream.js b/io/bytestream.js index a67a712..570aed1 100644 --- a/io/bytestream.js +++ b/io/bytestream.js @@ -25,8 +25,8 @@ bitjs.io = bitjs.io || {}; * @constructor */ bitjs.io.ByteStream = function(ab, opt_offset, opt_length) { - var offset = opt_offset || 0; - var length = opt_length || ab.byteLength; + const offset = opt_offset || 0; + const length = opt_length || ab.byteLength; this.bytes = new Uint8Array(ab, offset, length); this.ptr = 0; }; @@ -44,9 +44,9 @@ bitjs.io.ByteStream.prototype.peekNumber = function(n) { if (n <= 0 || typeof n != typeof 1) return -1; - var result = 0; + let result = 0; // read from last byte to first byte and roll them in - var curByte = this.ptr + n - 1; + let curByte = this.ptr + n - 1; while (curByte >= this.ptr) { result <<= 8; result |= this.bytes[curByte]; @@ -63,7 +63,7 @@ bitjs.io.ByteStream.prototype.peekNumber = function(n) { * @return {number} The n bytes interpreted as an unsigned number. */ bitjs.io.ByteStream.prototype.readNumber = function(n) { - var num = this.peekNumber( n ); + const num = this.peekNumber( n ); this.ptr += n; return num; }; @@ -76,9 +76,9 @@ bitjs.io.ByteStream.prototype.readNumber = function(n) { * @return {number} The bytes interpreted as a signed number. */ bitjs.io.ByteStream.prototype.peekSignedNumber = function(n) { - var num = this.peekNumber(n); - var HALF = Math.pow(2, (n * 8) - 1); - var FULL = HALF * 2; + let num = this.peekNumber(n); + const HALF = Math.pow(2, (n * 8) - 1); + const FULL = HALF * 2; if (num >= HALF) num -= FULL; @@ -92,7 +92,7 @@ bitjs.io.ByteStream.prototype.peekSignedNumber = function(n) { * @return {number} The bytes interpreted as a signed number. */ bitjs.io.ByteStream.prototype.readSignedNumber = function(n) { - var num = this.peekSignedNumber(n); + const num = this.peekSignedNumber(n); this.ptr += n; return num; }; @@ -110,7 +110,7 @@ bitjs.io.ByteStream.prototype.peekBytes = function(n, movePointers) { return null; } - var result = this.bytes.subarray(this.ptr, this.ptr + n); + const result = this.bytes.subarray(this.ptr, this.ptr + n); if (movePointers) { this.ptr += n; @@ -140,8 +140,8 @@ bitjs.io.ByteStream.prototype.peekString = function(n) { return ""; } - var result = ""; - for (var p = this.ptr, end = this.ptr + n; p < end; ++p) { + let result = ""; + for (let p = this.ptr, end = this.ptr + n; p < end; ++p) { result += String.fromCharCode(this.bytes[p]); } return result; @@ -155,7 +155,7 @@ bitjs.io.ByteStream.prototype.peekString = function(n) { * @return {string} The next n bytes as a string. */ bitjs.io.ByteStream.prototype.readString = function(n) { - var strToReturn = this.peekString(n); + const strToReturn = this.peekString(n); this.ptr += n; return strToReturn; }; diff --git a/tests/archive-test.js b/tests/archive-test.js index d81598d..2c21c7d 100644 --- a/tests/archive-test.js +++ b/tests/archive-test.js @@ -6,8 +6,8 @@ * Copyright(c) 2017 Google Inc. */ -var assertEquals = muther.assertEquals; -var testInputs = { +const assertEquals = muther.assertEquals; +const testInputs = { 'testUnzipDeflate': 'archive-testfiles/test-unzip-deflate.json', 'testUnzipStore': 'archive-testfiles/test-unzip-store.json', 'testUnrarM1': 'archive-testfiles/test-unrar-m1.json', @@ -18,33 +18,33 @@ var testInputs = { 'testUnrarMA4': 'archive-testfiles/test-unrar-ma4.json', }; -var testSuite = {tests: {}}; -for (var testName in testInputs) { - var testInputFilename = testInputs[testName]; +const testSuite = {tests: {}}; +for (let testName in testInputs) { + const testInputFilename = testInputs[testName]; testSuite.tests[testName] = new Promise(function(resolve, reject) { - var scriptEl = document.createElement('script'); + const scriptEl = document.createElement('script'); scriptEl.setAttribute('src', testInputFilename); scriptEl.addEventListener('load', function(evt) { // document.body.removeChild(scriptEl); - var testFile = window.archiveTestFile; - var archivedFile = new Uint8Array( + const testFile = window.archiveTestFile; + const archivedFile = new Uint8Array( atob(testFile.archivedFile) .split(',') .map(function(str) { return parseInt(str); }) ); - var unarchivedFile = new Uint8Array( + const unarchivedFile = new Uint8Array( atob(testFile.unarchivedFile) .split(',') .map(function(str) { return parseInt(str); }) ); - var unarchiver = bitjs.archive.GetUnarchiver(archivedFile.buffer, '../archive/'); + const unarchiver = bitjs.archive.GetUnarchiver(archivedFile.buffer, '../'); unarchiver.addEventListener(bitjs.archive.UnarchiveEvent.Type.EXTRACT, function(evt) { - var theUnarchivedFile = evt.unarchivedFile.fileData; + const theUnarchivedFile = evt.unarchivedFile.fileData; try { assertEquals(theUnarchivedFile.length, unarchivedFile.length, 'The unarchived buffer was not the right length'); - for (var i = 0; i < theUnarchivedFile.length; ++i) { + for (let i = 0; i < theUnarchivedFile.length; ++i) { assertEquals(theUnarchivedFile[i], unarchivedFile[i], 'Byte #' + i + ' did not match'); } diff --git a/tests/muther.js b/tests/muther.js index 8ac0d99..90e36ef 100644 --- a/tests/muther.js +++ b/tests/muther.js @@ -18,18 +18,17 @@ muther.set_ = function(id, style, innerHTML) { muther.go = function(spec) { Object.keys(spec['tests']).forEach(function(testName) { - var test = spec['tests'][testName]; + const test = spec['tests'][testName]; if (test instanceof Promise) { muther.set_(testName, 'color:#F90', 'RUNNING: ' + testName); - // TODO: What if we want setup() and tearDown()? test.then(function() { muther.set_(testName, 'color:#090', 'PASS: ' + testName); }, function(err) { muther.set_(testName, 'color:#900', 'FAIL: ' + testName + ': ' + err); }); } else if (test instanceof Function) { - var setup = spec['setUp'] || function(){}; - var tearDown = spec['tearDown'] || function(){}; + const setup = spec['setUp'] || function(){}; + const tearDown = spec['tearDown'] || function(){}; try { setup(); test(); tearDown(); muther.set_(testName, 'color:#090', 'PASS: ' + testName); diff --git a/tests/test-uploader.js b/tests/test-uploader.js index 448f6f9..9bdddc5 100644 --- a/tests/test-uploader.js +++ b/tests/test-uploader.js @@ -20,10 +20,10 @@ } */ -var archiveUploaderEl = null; -var archivedFileAsText = null; -var unarchiveUploaderEl = null; -var unarchivedFileAsText = null; +let archiveUploaderEl = null; +let archivedFileAsText = null; +let unarchiveUploaderEl = null; +let unarchivedFileAsText = null; function init() { archiveUploaderEl = document.querySelector('#archive-uploader'); @@ -34,10 +34,10 @@ function init() { } function getArchivedFile(evt) { - var filelist = evt.target.files; - var fr = new FileReader(); + const filelist = evt.target.files; + const fr = new FileReader(); fr.onload = function() { - var arr = new Uint8Array(fr.result); + const arr = new Uint8Array(fr.result); archivedFileAsText = btoa(arr); archiveUploaderEl.setAttribute('disabled', 'true'); unarchiveUploaderEl.removeAttribute('disabled'); @@ -46,10 +46,10 @@ function getArchivedFile(evt) { } function getUnarchivedFile(evt) { - var filelist = evt.target.files; - var fr = new FileReader(); + const filelist = evt.target.files; + const fr = new FileReader(); fr.onload = function() { - var arr = new Uint8Array(fr.result); + const arr = new Uint8Array(fr.result); unarchivedFileAsText = btoa(arr); unarchiveUploaderEl.setAttribute('disabled', 'true'); output(); @@ -58,7 +58,7 @@ function getUnarchivedFile(evt) { } function output() { - var json = 'window.archiveTestFile = {\n'; + let json = 'window.archiveTestFile = {\n'; json += ' "archivedFile": "' + archivedFileAsText + '",\n'; json += ' "unarchivedFile": "' + unarchivedFileAsText + '"\n'; json += '}';