Skip to content

Commit

Permalink
Bugfix & code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
fengdh committed Aug 25, 2015
1 parent 6f3edcf commit a38cd76
Showing 1 changed file with 175 additions and 149 deletions.
324 changes: 175 additions & 149 deletions minilzo-decompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,196 +57,222 @@
return lzo1x;
});

var lzo1x = (function () {

// Auto expandable read/write buffer of TypedArray
function FlexBuffer(bufType, blockSize) {
var buf, l, c = 0;
blockSize = blockSize || 4096;

function FlexBuffer() {
return {
alloc: function(initSize) {
return buf = new bufType(l = initSize || 8192);
},
require: function(n) {
if (l - c < n) {
var buf2 = new bufType(l += blockSize * Math.ceil( (l - c + n ) / blockSize ));
buf2.set(buf);
buf = buf2;
var r = this.c - this.l + n;
if (r > 0) {
var tmp = new Uint8Array(this.l += this.blockSize * Math.ceil(r / this.blockSize ));
tmp.set(this.buf);
this.buf = tmp;
}
c += n;
return buf;
this.c += n;
return this.buf;
},
alloc: function(initSize, blockSize) {
this.blockSize = this.roundUp(blockSize || 4096);
this.c = 0;
this.l = this.roundUp(initSize)|0;
this.l += this.blockSize - (this.l % this.blockSize);
this.buf = new Uint8Array(this.l);
return this.buf;
},
pack: function(size) { return buf.subarray(0, size || c); },
roundUp: function (n) {
var r = n % 4;
return r === 0 ? n : n + 4 - r;
},
reset: function() {
this.c = 0;
this.l = this.buf.length;
},
pack: function(size) { return this.buf.subarray(0, size); },
};
}


var c_top_loop=1;
var c_first_literal_run=2;
var c_match=3;
var c_copy_match=4;
var c_match_done=5;
var c_match_next=6;
var lzo1x = (function () {

function decompress(inBuf, bufInitSize, bufBlockSize) {
var buf = new FlexBuffer(Uint8Array, bufBlockSize),
out = buf.alloc(bufInitSize);

var op=0,
ip=0,
function decompress(inBuf, outBuf) {
var c_top_loop = 1;
var c_first_literal_run = 2;
var c_match = 3;
var c_copy_match = 4;
var c_match_done = 5;
var c_match_next = 6;

var out = outBuf.buf,
op = 0,
ip = 0,
t = inBuf[ip],
state = c_top_loop,
m_pos=0,
ip_end = inBuf.byteLength;

state = c_top_loop,
m_pos = 0,
ip_end = inBuf.length;
if (t > 17) {
ip++;

t -= 17;
if (t < 4) {
state=c_match_next; //goto match_next;
}else{
out = buf.require(t);
state = c_match_next; // goto match_next;
} else {
out = outBuf.require(t);
do {
out[op++] = inBuf[ip++];
out[op++] = inBuf[ip++];
} while (--t > 0);
state=c_first_literal_run;//goto first_literal_run;
state = c_first_literal_run; // goto first_literal_run;
}
}

top_loop_ori: do{
var if_block=false;
switch(state) { //while (true) top_loop_ori
case c_top_loop:
t = inBuf[ip++];
if (t >= 16){
state=c_match; continue top_loop_ori; //goto match;
}
if (t === 0) {
while (inBuf[ip] === 0) {
t += 255;
ip++;

top_loop_ori: do {
var if_block = false;
switch (state) { // while (true) top_loop_ori
case c_top_loop:
t = inBuf[ip++];
if (t >= 16) {
state = c_match;
continue top_loop_ori; //goto match;
}
t += 15 + inBuf[ip++];
}

//s=3; do out[op++] = inBuf[ip++]; while(--s > 0);//* (lzo_uint32 *)(op) = * (const lzo_uint32 *)(ip);op += 4; ip += 4;
out = buf.require(4);
out[op++] = inBuf[ip++];
out[op++] = inBuf[ip++];
out[op++] = inBuf[ip++];
out[op++] = inBuf[ip++];

if (--t > 0) {
out = buf.require(t);
do out[op++] = inBuf[ip++]; while (--t > 0);
}
case c_first_literal_run: /*first_literal_run: */
t = inBuf[ip++];
if (t >= 16) {
state=c_match; continue top_loop_ori; //goto match;
}
m_pos = op - 0x801 - (t >> 2) - (inBuf[ip++] << 2);
out = buf.require(3);
out[op++] = out[m_pos++]; out[op++] = out[m_pos++]; out[op++] = out[m_pos];

state = c_match_done; continue top_loop_ori;//goto match_done;
case c_match:
//do {
//match:
if (t >= 64) {
m_pos = op - 1;
m_pos -= (t >> 2) & 7;
m_pos -= inBuf[ip++] << 3;
t = (t >> 5) - 1;
state = c_copy_match; continue top_loop_ori;//goto copy_match;

} else if (t >= 32) {
t &= 31;
if (t === 0) {
while (inBuf[ip] === 0) {
t += 255;
ip++;
}
t += 31 + inBuf[ip++];
t += 15 + inBuf[ip++];
}
m_pos = op - 1;
m_pos -= (( inBuf[ip] + ( inBuf[ip+1] << 8) ) >> 2);//m_pos -= (* (const unsigned short *) ip) >> 2;

ip += 2;
} else if (t >= 16) {
m_pos = op;
m_pos -= (t & 8) << 11;

t &= 7;
if (t === 0) {
while (inBuf[ip] === 0) {
t += 255;
ip++;

//s=3; do out[op++] = inBuf[ip++]; while(--s > 0); //* (lzo_uint32 *)(op) = * (const lzo_uint32 *)(ip);op += 4; ip += 4;

t += 3;
out = outBuf.require(t);
do out[op++] = inBuf[ip++]; while (--t > 0);

case c_first_literal_run: /*first_literal_run: */
t = inBuf[ip++];
if (t >= 16) {
state = c_match;
continue top_loop_ori; //goto match;
}
m_pos = op - 0x801 - (t >> 2) - (inBuf[ip++] << 2);
out = outBuf.require(3);
out[op++] = out[m_pos++];
out[op++] = out[m_pos++];
out[op++] = out[m_pos];

state = c_match_done;
continue top_loop_ori; //goto match_done;

case c_match:
//do {
//match:
if (t >= 64) {
m_pos = op - 1 - ((t >> 2) & 7) - (inBuf[ip++] << 3);
t = (t >> 5) - 1;
state = c_copy_match;
continue top_loop_ori; //goto copy_match;

} else if (t >= 32) {
t &= 31;
if (t === 0) {
while (inBuf[ip] === 0) {
t += 255;
ip++;
}
t += 31 + inBuf[ip++];
}
m_pos = op - 1 - ((inBuf[ip] + (inBuf[ip + 1] << 8)) >> 2);

ip += 2;
} else if (t >= 16) {
m_pos = op - ((t & 8) << 11);
t &= 7;
if (t === 0) {
while (inBuf[ip] === 0) {
t += 255;
ip++;
}
t += 7 + inBuf[ip++];
}
m_pos -= ((inBuf[ip] + (inBuf[ip + 1] << 8)) >> 2); //m_pos -= (* (const unsigned short *) ip) >> 2;
ip += 2;
if (m_pos === op) {
break top_loop_ori; //goto eof_found;
}
t += 7 + inBuf[ip++];
m_pos -= 0x4000;
} else {
m_pos = op - 1 - (t >> 2) - (inBuf[ip++] << 2);
out = outBuf.require(2);
out[op++] = out[m_pos++];
out[op++] = out[m_pos];
state = c_match_done;
continue top_loop_ori; //goto match_done;
}
m_pos -= (( inBuf[ip] + ( inBuf[ip+1] << 8) ) >> 2);//m_pos -= (* (const unsigned short *) ip) >> 2;
ip += 2;
if (m_pos === op){
break top_loop_ori;//goto eof_found;
if (t >= 6 && (op - m_pos) >= 4) {
if_block = true;
t += 2;
out = outBuf.require(t);
do out[op++] = out[m_pos++]; while (--t > 0);
}
m_pos -= 0x4000;
} else {
m_pos = op - 1;
m_pos -= t >> 2;
m_pos -= inBuf[ip++] << 2;

out = buf.require(2);
out[op++] = out[m_pos++]; out[op++] = out[m_pos];
state=c_match_done;continue top_loop_ori;//goto match_done;
}
if (t >= 6 && (op - m_pos) >= 4) {
t += 2;
out = buf.require(t);
if_block=true;
do out[op++] = out[m_pos++]; while (--t > 0);
}
case c_copy_match: if(!if_block){
t += 2;
out = buf.require(t);
do out[op++] = out[m_pos++]; while( --t > 0) ;
}

case c_copy_match:
if (!if_block) {
t += 2;
out = outBuf.require(t);
do out[op++] = out[m_pos++]; while (--t > 0);
}

case c_match_done:
t = inBuf[ip-2] & 3;
if (t === 0){
state=c_top_loop; continue top_loop_ori; //break;
}
case c_match_next:
out = buf.require(1);
out[op++] = inBuf[ip++];
if (t > 1) {
out = buf.require(1);
out[op++] = inBuf[ip++];
if (t > 2) {
out = buf.require(1);
out[op++] = inBuf[ip++];
}
}
t = inBuf[ip++];
state=c_match; continue top_loop_ori;
}
}while(true);
t = inBuf[ip - 2] & 3;
if (t === 0) {
state = c_top_loop;
continue top_loop_ori; //break;
}

case c_match_next:
out = outBuf.require(1);
out[op++] = inBuf[ip++];
if (t > 1) {
out = outBuf.require(1);
out[op++] = inBuf[ip++];
if (t > 2) {
out = outBuf.require(1);
out[op++] = inBuf[ip++];
}
}
t = inBuf[ip++];
state = c_match;
continue top_loop_ori;

}
} while (true);

return buf.pack(op);
return outBuf.pack(op);
}


return {
/**
* Decompress LZO compressed data.
* @param buf ArrayBuffer or Uint8Array object containing LZO compressed data.
* @param initBufSize initial size for output ArrayBuffer, optional with default value of 8192 (8K) bytes
* @param initSize initial size for output ArrayBuffer, optional with default value of 8192 (8K) bytes
* @param blockSize block size used to extend output ArrayBuffer, optional with default value of 4096 (4K) bytes
* @return Uint8Array containing decompressed data. For performance reason, its underground ArrayBuffer
* is not truncated to the same size of output data, which can be accessed through TypedArray.prototype.buffer.
*/
decompress: function(buf, initBufSize, blockSize) {
return decompress(new Uint8Array(buf), initBufSize, blockSize);
decompress: function(input, options) {
var output;
if (options.require instanceof Function) {
output = options;
output.reset();
} else {
output = this.createFlexBuffer(options.initSize || input.length, options.blockSize || 8192);
}
return decompress(input, output);
},
createFlexBuffer: function(initSize, blockSize) {
var output = FlexBuffer();
output.alloc(initSize, blockSize);
return output;
}
};
})();
Expand Down

0 comments on commit a38cd76

Please sign in to comment.