1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-12-26 01:50:31 +00:00

Beautify bitjs

This commit is contained in:
subdiox 2019-05-14 04:28:06 +09:00
parent 7982ed877c
commit 867aa2f0bd
8 changed files with 2651 additions and 2564 deletions

View File

@ -7,7 +7,6 @@
* *
* Copyright(c) 2011 Google Inc. * Copyright(c) 2011 Google Inc.
*/ */
var bitjs = bitjs || {}; var bitjs = bitjs || {};
bitjs.archive = bitjs.archive || {}; bitjs.archive = bitjs.archive || {};
@ -25,8 +24,7 @@ bitjs.base = function(me, opt_methodName, var_args) {
var args = Array.prototype.slice.call(arguments, 2); var args = Array.prototype.slice.call(arguments, 2);
var foundCaller = false; var foundCaller = false;
for (var ctor = me.constructor; for (var ctor = me.constructor; ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
if (ctor.prototype[opt_methodName] === caller) { if (ctor.prototype[opt_methodName] === caller) {
foundCaller = true; foundCaller = true;
} else if (foundCaller) { } else if (foundCaller) {
@ -269,7 +267,9 @@ bitjs.archive.Unarchiver.prototype.removeEventListener = function(type, listener
bitjs.archive.Unarchiver.prototype.handleWorkerEvent_ = function(e) { bitjs.archive.Unarchiver.prototype.handleWorkerEvent_ = function(e) {
if ((e instanceof bitjs.archive.UnarchiveEvent || e.type) && if ((e instanceof bitjs.archive.UnarchiveEvent || e.type) &&
this.listeners_[e.type] instanceof Array) { this.listeners_[e.type] instanceof Array) {
this.listeners_[e.type].forEach(function (listener) { listener(e) }); this.listeners_[e.type].forEach(function(listener) {
listener(e)
});
if (e.type == bitjs.archive.UnarchiveEvent.Type.FINISH) { if (e.type == bitjs.archive.UnarchiveEvent.Type.FINISH) {
this.worker_.terminate(); this.worker_.terminate();
} }
@ -303,7 +303,9 @@ bitjs.archive.Unarchiver.prototype.handleWorkerEvent_ = function(e) {
} }
}; };
this.worker_.postMessage({file: this.ab}); this.worker_.postMessage({
file: this.ab
});
} }
}; };
@ -326,7 +328,9 @@ bitjs.archive.Unzipper = function(arrayBuffer, opt_pathToBitJS) {
bitjs.base(this, arrayBuffer, opt_pathToBitJS); bitjs.base(this, arrayBuffer, opt_pathToBitJS);
}; };
bitjs.inherits(bitjs.archive.Unzipper, bitjs.archive.Unarchiver); bitjs.inherits(bitjs.archive.Unzipper, bitjs.archive.Unarchiver);
bitjs.archive.Unzipper.prototype.getScriptFileName = function() { return 'unzip.js' }; bitjs.archive.Unzipper.prototype.getScriptFileName = function() {
return 'unzip.js'
};
/** /**
* Unrarrer * Unrarrer
@ -337,7 +341,9 @@ bitjs.archive.Unrarrer = function(arrayBuffer, opt_pathToBitJS) {
bitjs.base(this, arrayBuffer, opt_pathToBitJS); bitjs.base(this, arrayBuffer, opt_pathToBitJS);
}; };
bitjs.inherits(bitjs.archive.Unrarrer, bitjs.archive.Unarchiver); bitjs.inherits(bitjs.archive.Unrarrer, bitjs.archive.Unarchiver);
bitjs.archive.Unrarrer.prototype.getScriptFileName = function() { return 'unrar.js' }; bitjs.archive.Unrarrer.prototype.getScriptFileName = function() {
return 'unrar.js'
};
/** /**
* Untarrer * Untarrer
@ -348,7 +354,9 @@ bitjs.archive.Untarrer = function(arrayBuffer, opt_pathToBitJS) {
bitjs.base(this, arrayBuffer, opt_pathToBitJS); bitjs.base(this, arrayBuffer, opt_pathToBitJS);
}; };
bitjs.inherits(bitjs.archive.Untarrer, bitjs.archive.Unarchiver); bitjs.inherits(bitjs.archive.Untarrer, bitjs.archive.Unarchiver);
bitjs.archive.Untarrer.prototype.getScriptFileName = function() { return 'untar.js' }; bitjs.archive.Untarrer.prototype.getScriptFileName = function() {
return 'untar.js'
};
/** /**
* Factory method that creates an unarchiver based on the byte signature found * Factory method that creates an unarchiver based on the byte signature found

View File

@ -224,11 +224,11 @@ VM_PreparedOperand.prototype.toString = function() {
if (this.Type === null) { if (this.Type === null) {
return 'Error: Type was null in VM_PreparedOperand'; return 'Error: Type was null in VM_PreparedOperand';
} }
return '{ ' return '{ ' +
+ 'Type: ' + getDebugString(VM_OpType, this.Type) 'Type: ' + getDebugString(VM_OpType, this.Type) +
+ ', Data: ' + this.Data ', Data: ' + this.Data +
+ ', Base: ' + this.Base ', Base: ' + this.Base +
+ ' }'; ' }';
}; };
/** /**
@ -255,12 +255,12 @@ VM_PreparedCommand.prototype.toString = function(indent) {
return 'Error: OpCode was null in VM_PreparedCommand'; return 'Error: OpCode was null in VM_PreparedCommand';
} }
indent = indent || ''; indent = indent || '';
return indent + '{\n' return indent + '{\n' +
+ indent + ' OpCode: ' + getDebugString(VM_Commands, this.OpCode) + ',\n' indent + ' OpCode: ' + getDebugString(VM_Commands, this.OpCode) + ',\n' +
+ indent + ' ByteMode: ' + this.ByteMode + ',\n' indent + ' ByteMode: ' + this.ByteMode + ',\n' +
+ indent + ' Op1: ' + this.Op1.toString() + ',\n' indent + ' Op1: ' + this.Op1.toString() + ',\n' +
+ indent + ' Op2: ' + this.Op2.toString() + ',\n' indent + ' Op2: ' + this.Op2.toString() + ',\n' +
+ indent + '}'; indent + '}';
}; };
/** /**
@ -339,46 +339,86 @@ var VMCF_USEFLAGS = 32;
var VMCF_CHFLAGS = 64; var VMCF_CHFLAGS = 64;
var VM_CmdFlags = [ var VM_CmdFlags = [
/* VM_MOV */ VMCF_OP2 | VMCF_BYTEMODE , /* VM_MOV */
/* VM_CMP */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , VMCF_OP2 | VMCF_BYTEMODE,
/* VM_ADD */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , /* VM_CMP */
/* VM_SUB */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_JZ */ VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS , /* VM_ADD */
/* VM_JNZ */ VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS , VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_INC */ VMCF_OP1 | VMCF_BYTEMODE | VMCF_CHFLAGS , /* VM_SUB */
/* VM_DEC */ VMCF_OP1 | VMCF_BYTEMODE | VMCF_CHFLAGS , VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_JMP */ VMCF_OP1 | VMCF_JUMP , /* VM_JZ */
/* VM_XOR */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS,
/* VM_AND */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , /* VM_JNZ */
/* VM_OR */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS,
/* VM_TEST */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , /* VM_INC */
/* VM_JS */ VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS , VMCF_OP1 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_JNS */ VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS , /* VM_DEC */
/* VM_JB */ VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS , VMCF_OP1 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_JBE */ VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS , /* VM_JMP */
/* VM_JA */ VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS , VMCF_OP1 | VMCF_JUMP,
/* VM_JAE */ VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS , /* VM_XOR */
/* VM_PUSH */ VMCF_OP1 , VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_POP */ VMCF_OP1 , /* VM_AND */
/* VM_CALL */ VMCF_OP1 | VMCF_PROC , VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_RET */ VMCF_OP0 | VMCF_PROC , /* VM_OR */
/* VM_NOT */ VMCF_OP1 | VMCF_BYTEMODE , VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_SHL */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , /* VM_TEST */
/* VM_SHR */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_SAR */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS , /* VM_JS */
/* VM_NEG */ VMCF_OP1 | VMCF_BYTEMODE | VMCF_CHFLAGS , VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS,
/* VM_PUSHA */ VMCF_OP0 , /* VM_JNS */
/* VM_POPA */ VMCF_OP0 , VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS,
/* VM_PUSHF */ VMCF_OP0 | VMCF_USEFLAGS , /* VM_JB */
/* VM_POPF */ VMCF_OP0 | VMCF_CHFLAGS , VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS,
/* VM_MOVZX */ VMCF_OP2 , /* VM_JBE */
/* VM_MOVSX */ VMCF_OP2 , VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS,
/* VM_XCHG */ VMCF_OP2 | VMCF_BYTEMODE , /* VM_JA */
/* VM_MUL */ VMCF_OP2 | VMCF_BYTEMODE , VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS,
/* VM_DIV */ VMCF_OP2 | VMCF_BYTEMODE , /* VM_JAE */
/* VM_ADC */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_USEFLAGS | VMCF_CHFLAGS , VMCF_OP1 | VMCF_JUMP | VMCF_USEFLAGS,
/* VM_SBB */ VMCF_OP2 | VMCF_BYTEMODE | VMCF_USEFLAGS | VMCF_CHFLAGS , /* VM_PUSH */
/* VM_PRINT */ VMCF_OP0 , VMCF_OP1,
/* VM_POP */
VMCF_OP1,
/* VM_CALL */
VMCF_OP1 | VMCF_PROC,
/* VM_RET */
VMCF_OP0 | VMCF_PROC,
/* VM_NOT */
VMCF_OP1 | VMCF_BYTEMODE,
/* VM_SHL */
VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_SHR */
VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_SAR */
VMCF_OP2 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_NEG */
VMCF_OP1 | VMCF_BYTEMODE | VMCF_CHFLAGS,
/* VM_PUSHA */
VMCF_OP0,
/* VM_POPA */
VMCF_OP0,
/* VM_PUSHF */
VMCF_OP0 | VMCF_USEFLAGS,
/* VM_POPF */
VMCF_OP0 | VMCF_CHFLAGS,
/* VM_MOVZX */
VMCF_OP2,
/* VM_MOVSX */
VMCF_OP2,
/* VM_XCHG */
VMCF_OP2 | VMCF_BYTEMODE,
/* VM_MUL */
VMCF_OP2 | VMCF_BYTEMODE,
/* VM_DIV */
VMCF_OP2 | VMCF_BYTEMODE,
/* VM_ADC */
VMCF_OP2 | VMCF_BYTEMODE | VMCF_USEFLAGS | VMCF_CHFLAGS,
/* VM_SBB */
VMCF_OP2 | VMCF_BYTEMODE | VMCF_USEFLAGS | VMCF_CHFLAGS,
/* VM_PRINT */
VMCF_OP0,
]; ];

View File

@ -226,11 +226,13 @@ var 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, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072,
4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, 65536, 98304, 4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, 65536, 98304,
131072, 196608, 262144, 327680, 393216, 458752, 524288, 589824, 131072, 196608, 262144, 327680, 393216, 458752, 524288, 589824,
655360, 720896, 786432, 851968, 917504, 983040]; 655360, 720896, 786432, 851968, 917504, 983040
];
var rDBits = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, var 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, 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]; 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16
];
var rLOW_DIST_REP_COUNT = 16; var rLOW_DIST_REP_COUNT = 16;
@ -328,15 +330,13 @@ function RarReadTables(bstream) {
var ZeroCount = bstream.readBits(4); var ZeroCount = bstream.readBits(4);
if (ZeroCount == 0) { if (ZeroCount == 0) {
BitLength[I] = 15; BitLength[I] = 15;
} } else {
else {
ZeroCount += 2; ZeroCount += 2;
while (ZeroCount-- > 0 && I < rBC) while (ZeroCount-- > 0 && I < rBC)
BitLength[I++] = 0; BitLength[I++] = 0;
--I; --I;
} }
} } else {
else {
BitLength[I] = Length; BitLength[I] = Length;
} }
} }
@ -381,24 +381,26 @@ function RarReadTables(bstream) {
function RarDecodeNumber(bstream, dec) { function RarDecodeNumber(bstream, dec) {
var DecodeLen = dec.DecodeLen, DecodePos = dec.DecodePos, DecodeNum = dec.DecodeNum; var DecodeLen = dec.DecodeLen,
DecodePos = dec.DecodePos,
DecodeNum = dec.DecodeNum;
var bitField = bstream.getBits() & 0xfffe; var bitField = bstream.getBits() & 0xfffe;
//some sort of rolled out binary search //some sort of rolled out binary search
var bits = ((bitField < DecodeLen[8]) ? var bits = ((bitField < DecodeLen[8]) ?
((bitField < DecodeLen[4]) ? ((bitField < DecodeLen[4]) ?
((bitField < DecodeLen[2]) ? ((bitField < DecodeLen[2]) ?
((bitField < DecodeLen[1])?1:2) ((bitField < DecodeLen[1]) ? 1 : 2) :
:((bitField < DecodeLen[3])?3:4)) ((bitField < DecodeLen[3]) ? 3 : 4)) :
:(bitField < DecodeLen[6])? (bitField < DecodeLen[6]) ?
((bitField < DecodeLen[5])?5:6) ((bitField < DecodeLen[5]) ? 5 : 6) :
:((bitField < DecodeLen[7])?7:8)) ((bitField < DecodeLen[7]) ? 7 : 8)) :
:((bitField < DecodeLen[12])? ((bitField < DecodeLen[12]) ?
((bitField < DecodeLen[10]) ? ((bitField < DecodeLen[10]) ?
((bitField < DecodeLen[9])?9:10) ((bitField < DecodeLen[9]) ? 9 : 10) :
:((bitField < DecodeLen[11])?11:12)) ((bitField < DecodeLen[11]) ? 11 : 12)) :
:(bitField < DecodeLen[14])? (bitField < DecodeLen[14]) ?
((bitField < DecodeLen[13])?13:14) ((bitField < DecodeLen[13]) ? 13 : 14) :
:15)); 15));
bstream.readBits(bits); bstream.readBits(bits);
var N = DecodePos[bits] + ((bitField - DecodeLen[bits - 1]) >>> (16 - bits)); var N = DecodePos[bits] + ((bitField - DecodeLen[bits - 1]) >>> (16 - bits));
@ -716,9 +718,9 @@ function RarAddVMCode(firstByte, vmCode) {
if (firstByte & 0x20) { if (firstByte & 0x20) {
stackFilter.BlockLength = RarVM.readData(bstream); stackFilter.BlockLength = RarVM.readData(bstream);
} else { } else {
stackFilter.BlockLength = filtPos < OldFilterLengths.length stackFilter.BlockLength = filtPos < OldFilterLengths.length ?
? OldFilterLengths[filtPos] OldFilterLengths[filtPos] :
: 0; 0;
} }
stackFilter.NextWindow = (wBuffer.ptr != rBuffer.ptr) && stackFilter.NextWindow = (wBuffer.ptr != rBuffer.ptr) &&
(((wBuffer.ptr - rBuffer.ptr) & MAXWINMASK) <= blockStart); (((wBuffer.ptr - rBuffer.ptr) & MAXWINMASK) <= blockStart);
@ -1138,8 +1140,7 @@ function RarWriteData(offset, numBytes) {
/** /**
* @param {VM_PreparedProgram} prg * @param {VM_PreparedProgram} prg
*/ */
function RarExecuteCode(prg) function RarExecuteCode(prg) {
{
if (prg.GlobalData.length > 0) { if (prg.GlobalData.length > 0) {
var writtenFileSize = wBuffer.ptr; var writtenFileSize = wBuffer.ptr;
prg.InitR[6] = writtenFileSize; prg.InitR[6] = writtenFileSize;
@ -1152,7 +1153,8 @@ function RarExecuteCode(prg)
function RarReadEndOfBlock(bstream) { function RarReadEndOfBlock(bstream) {
RarUpdateProgress(); RarUpdateProgress();
var NewTable = false, NewFile = false; var NewTable = false,
NewFile = false;
if (bstream.readBits(1)) { if (bstream.readBits(1)) {
NewTable = true; NewTable = true;
} else { } else {
@ -1242,8 +1244,7 @@ var RarLocalFile = function(bstream) {
if (this.header.headType != FILE_HEAD && this.header.headType != ENDARC_HEAD) { if (this.header.headType != FILE_HEAD && this.header.headType != ENDARC_HEAD) {
this.isValid = false; this.isValid = false;
info("Error! RAR Volume did not include a FILE_HEAD header "); info("Error! RAR Volume did not include a FILE_HEAD header ");
} } else {
else {
// read in the compressed data // read in the compressed data
this.fileData = null; this.fileData = null;
if (this.header.packSize > 0) { if (this.header.packSize > 0) {
@ -1296,8 +1297,7 @@ var unrar = function(arrayBuffer) {
var mhead = new RarVolumeHeader(bstream); var mhead = new RarVolumeHeader(bstream);
if (mhead.headType != MAIN_HEAD) { if (mhead.headType != MAIN_HEAD) {
info("Error! RAR did not include a MAIN_HEAD header"); info("Error! RAR did not include a MAIN_HEAD header");
} } else {
else {
var localFiles = [], var localFiles = [],
localFile = null; localFile = null;
do { do {
@ -1325,7 +1325,9 @@ var unrar = function(arrayBuffer) {
return aname > bname ? 1 : -1; return aname > bname ? 1 : -1;
}); });
info(localFiles.map(function(a){return a.filename}).join(', ')); info(localFiles.map(function(a) {
return a.filename
}).join(', '));
for (var i = 0; i < localFiles.length; ++i) { for (var i = 0; i < localFiles.length; ++i) {
var localfile = localFiles[i]; var localfile = localFiles[i];
@ -1344,8 +1346,7 @@ var unrar = function(arrayBuffer) {
postProgress(); postProgress();
} }
} } else {
else {
err("Invalid RAR file"); err("Invalid RAR file");
} }
postMessage(new bitjs.archive.UnarchiveFinishEvent()); postMessage(new bitjs.archive.UnarchiveFinishEvent());

View File

@ -52,7 +52,8 @@ var zEndOfCentralDirLocatorSignature = 0x07064b50;
// takes a ByteStream and parses out the local file information // takes a ByteStream and parses out the local file information
var ZipLocalFile = function(bstream) { var ZipLocalFile = function(bstream) {
if (typeof bstream != typeof {} || !bstream.readNumber || typeof bstream.readNumber != typeof function(){}) { if (typeof bstream != typeof {} || !bstream.readNumber || typeof bstream.readNumber != typeof
function() {}) {
return null; return null;
} }
@ -123,8 +124,7 @@ ZipLocalFile.prototype.unzip = function() {
else if (this.compressionMethod == 8) { else if (this.compressionMethod == 8) {
info("ZIP v2.0, DEFLATE: " + this.filename + " (" + this.compressedSize + " bytes)"); info("ZIP v2.0, DEFLATE: " + this.filename + " (" + this.compressedSize + " bytes)");
this.fileData = inflate(this.fileData, this.uncompressedSize); this.fileData = inflate(this.fileData, this.uncompressedSize);
} } else {
else {
err("UNSUPPORTED VERSION/FORMAT: ZIP v" + this.version + ", compression method=" + this.compressionMethod + ": " + this.filename + " (" + this.compressedSize + " bytes)"); err("UNSUPPORTED VERSION/FORMAT: ZIP v" + this.version + ", compression method=" + this.compressionMethod + ": " + this.filename + " (" + this.compressedSize + " bytes)");
this.fileData = null; this.fileData = null;
} }
@ -307,11 +307,15 @@ function getHuffmanCodes(bitLengths) {
} }
// Step 3: Assign numerical values to all codes // Step 3: Assign numerical values to all codes
var table = {}, tableLength = 0; var table = {},
tableLength = 0;
for (var n = 0; n < numLengths; ++n) { for (var n = 0; n < numLengths; ++n) {
var len = bitLengths[n]; var len = bitLengths[n];
if (len != 0) { if (len != 0) {
table[next_code[len]] = { length: len, symbol: n }; //, bitstring: binaryValueToString(next_code[len],len) }; table[next_code[len]] = {
length: len,
symbol: n
}; //, bitstring: binaryValueToString(next_code[len],len) };
tableLength++; tableLength++;
next_code[len]++; next_code[len]++;
} }
@ -340,6 +344,7 @@ function getHuffmanCodes(bitLengths) {
// fixed Huffman codes go from 7-9 bits, so we need an array whose index can hold up to 9 bits // 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 fixedHCtoLiteral = null;
var fixedHCtoDistance = null; var fixedHCtoDistance = null;
function getFixedLiteralTable() { function getFixedLiteralTable() {
// create once // create once
if (!fixedHCtoLiteral) { if (!fixedHCtoLiteral) {
@ -359,7 +364,9 @@ function getFixedDistanceTable() {
// create once // create once
if (!fixedHCtoDistance) { if (!fixedHCtoDistance) {
var bitlengths = new Array(32); var bitlengths = new Array(32);
for (var i = 0; i < 32; ++i) { bitlengths[i] = 5; } for (var i = 0; i < 32; ++i) {
bitlengths[i] = 5;
}
// get huffman code table // get huffman code table
fixedHCtoDistance = getHuffmanCodes(bitlengths); fixedHCtoDistance = getHuffmanCodes(bitlengths);
@ -370,7 +377,8 @@ function getFixedDistanceTable() {
// extract one bit at a time until we find a matching Huffman Code // extract one bit at a time until we find a matching Huffman Code
// then return that symbol // then return that symbol
function decodeSymbol(bstream, hcTable) { function decodeSymbol(bstream, hcTable) {
var code = 0, len = 0; var code = 0,
len = 0;
var match = false; var match = false;
// loop until we match // loop until we match
@ -411,13 +419,34 @@ var CodeLengthCodeOrder = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2
266 1 13,14 276 3 59-66 266 1 13,14 276 3 59-66
*/ */
var LengthLookupTable = [ var LengthLookupTable = [
[0,3], [0,4], [0,5], [0,6], [0, 3],
[0,7], [0,8], [0,9], [0,10], [0, 4],
[1,11], [1,13], [1,15], [1,17], [0, 5],
[2,19], [2,23], [2,27], [2,31], [0, 6],
[3,35], [3,43], [3,51], [3,59], [0, 7],
[4,67], [4,83], [4,99], [4,115], [0, 8],
[5,131], [5,163], [5,195], [5,227], [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] [0, 258]
]; ];
/* /*
@ -436,20 +465,36 @@ var LengthLookupTable = [
9 3 25-32 19 8 769-1024 29 13 24577-32768 9 3 25-32 19 8 769-1024 29 13 24577-32768
*/ */
var DistLookupTable = [ var DistLookupTable = [
[0,1], [0,2], [0,3], [0,4], [0, 1],
[1,5], [1,7], [0, 2],
[2,9], [2,13], [0, 3],
[3,17], [3,25], [0, 4],
[4,33], [4,49], [1, 5],
[5,65], [5,97], [1, 7],
[6,129], [6,193], [2, 9],
[7,257], [7,385], [2, 13],
[8,513], [8,769], [3, 17],
[9,1025], [9,1537], [3, 25],
[10,2049], [10,3073], [4, 33],
[11,4097], [11,6145], [4, 49],
[12,8193], [12,12289], [5, 65],
[13,16385], [13,24577] [5, 97],
[6, 129],
[6, 193],
[7, 257],
[7, 385],
[8, 513],
[8, 769],
[9, 1025],
[9, 1537],
[10, 2049],
[10, 3073],
[11, 4097],
[11, 6145],
[12, 8193],
[12, 12289],
[13, 16385],
[13, 24577]
]; ];
function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) { function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) {
@ -468,7 +513,8 @@ function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) {
stream, and copy length bytes from this stream, and copy length bytes from this
position to the output stream. position to the output stream.
*/ */
var numSymbols = 0, blockSize = 0; var numSymbols = 0,
blockSize = 0;
for (;;) { for (;;) {
var symbol = decodeSymbol(bstream, hcLiteralTable); var symbol = decodeSymbol(bstream, hcLiteralTable);
++numSymbols; ++numSymbols;
@ -476,13 +522,11 @@ function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) {
// copy literal byte to output // copy literal byte to output
buffer.insertByte(symbol); buffer.insertByte(symbol);
blockSize++; blockSize++;
} } else {
else {
// end of block reached // end of block reached
if (symbol == 256) { if (symbol == 256) {
break; break;
} } else {
else {
var lengthLookup = LengthLookupTable[symbol - 257], var lengthLookup = LengthLookupTable[symbol - 257],
length = lengthLookup[1] + bstream.readBits(lengthLookup[0]), length = lengthLookup[1] + bstream.readBits(lengthLookup[0]),
distLookup = DistLookupTable[decodeSymbol(bstream, hcDistanceTable)], distLookup = DistLookupTable[decodeSymbol(bstream, hcDistanceTable)],
@ -525,7 +569,8 @@ function inflate(compressedData, numDecompressedBytes) {
compressedData.byteOffset, compressedData.byteOffset,
compressedData.byteLength); compressedData.byteLength);
var buffer = new bitjs.io.ByteBuffer(numDecompressedBytes); var buffer = new bitjs.io.ByteBuffer(numDecompressedBytes);
var numBlocks = 0, blockSize = 0; var numBlocks = 0,
blockSize = 0;
// block format: http://tools.ietf.org/html/rfc1951#page-9 // block format: http://tools.ietf.org/html/rfc1951#page-9
do { do {
@ -585,20 +630,17 @@ function inflate(compressedData, numDecompressedBytes) {
if (symbol <= 15) { if (symbol <= 15) {
literalCodeLengths.push(symbol); literalCodeLengths.push(symbol);
prevCodeLength = symbol; prevCodeLength = symbol;
} } else if (symbol == 16) {
else if (symbol == 16) {
var repeat = bstream.readBits(2) + 3; var repeat = bstream.readBits(2) + 3;
while (repeat--) { while (repeat--) {
literalCodeLengths.push(prevCodeLength); literalCodeLengths.push(prevCodeLength);
} }
} } else if (symbol == 17) {
else if (symbol == 17) {
var repeat = bstream.readBits(3) + 3; var repeat = bstream.readBits(3) + 3;
while (repeat--) { while (repeat--) {
literalCodeLengths.push(0); literalCodeLengths.push(0);
} }
} } else if (symbol == 18) {
else if (symbol == 18) {
var repeat = bstream.readBits(7) + 11; var repeat = bstream.readBits(7) + 11;
while (repeat--) { while (repeat--) {
literalCodeLengths.push(0); literalCodeLengths.push(0);

View File

@ -8,7 +8,6 @@
* Copyright(c) 2011 Google Inc. * Copyright(c) 2011 Google Inc.
* Copyright(c) 2011 antimatter15 * Copyright(c) 2011 antimatter15
*/ */
var bitjs = bitjs || {}; var bitjs = bitjs || {};
bitjs.io = bitjs.io || {}; bitjs.io = bitjs.io || {};
@ -18,7 +17,8 @@ bitjs.io = bitjs.io || {};
bitjs.BIT = [0x01, 0x02, 0x04, 0x08, bitjs.BIT = [0x01, 0x02, 0x04, 0x08,
0x10, 0x20, 0x40, 0x80, 0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800, 0x100, 0x200, 0x400, 0x800,
0x1000, 0x2000, 0x4000, 0x8000]; 0x1000, 0x2000, 0x4000, 0x8000
];
// mask for getting N number of bits (0-8) // mask for getting N number of bits (0-8)
var BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF]; var BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF];
@ -89,8 +89,7 @@ bitjs.io.BitStream.prototype.peekBits_ltr = function(n, movePointers) {
bitPtr = 0; bitPtr = 0;
bitsIn += numBitsLeftInThisByte; bitsIn += numBitsLeftInThisByte;
n -= numBitsLeftInThisByte; n -= numBitsLeftInThisByte;
} } else {
else {
var mask = (BITMASK[n] << bitPtr); var mask = (BITMASK[n] << bitPtr);
result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn); result |= (((bytes[bytePtr] & mask) >> bitPtr) << bitsIn);
@ -149,8 +148,7 @@ bitjs.io.BitStream.prototype.peekBits_rtl = function(n, movePointers) {
bytePtr++; bytePtr++;
bitPtr = 0; bitPtr = 0;
n -= numBitsLeftInThisByte; n -= numBitsLeftInThisByte;
} } else {
else {
result <<= n; result <<= n;
result |= ((bytes[bytePtr] & (BITMASK[n] << (8 - n - bitPtr))) >> (8 - n - bitPtr)); result |= ((bytes[bytePtr] & (BITMASK[n] << (8 - n - bitPtr))) >> (8 - n - bitPtr));

View File

@ -8,7 +8,6 @@
* Copyright(c) 2011 Google Inc. * Copyright(c) 2011 Google Inc.
* Copyright(c) 2011 antimatter15 * Copyright(c) 2011 antimatter15
*/ */
var bitjs = bitjs || {}; var bitjs = bitjs || {};
bitjs.io = bitjs.io || {}; bitjs.io = bitjs.io || {};

View File

@ -8,7 +8,6 @@
* Copyright(c) 2011 Google Inc. * Copyright(c) 2011 Google Inc.
* Copyright(c) 2011 antimatter15 * Copyright(c) 2011 antimatter15
*/ */
var bitjs = bitjs || {}; var bitjs = bitjs || {};
bitjs.io = bitjs.io || {}; bitjs.io = bitjs.io || {};