diff --git a/lib/zlib/inflate.js b/lib/zlib/inflate.js index 8e3f348..d9b695d 100644 --- a/lib/zlib/inflate.js +++ b/lib/zlib/inflate.js @@ -91,6 +91,15 @@ var MAX_WBITS = 15; /* 32K LZ77 window */ var DEF_WBITS = MAX_WBITS; + +function ZSWAP32(q) { + return (((q >>> 24) & 0xff) + + ((q >>> 8) & 0xff00) + + ((q & 0xff00) << 8) + + ((q & 0xff) << 24)); +} + + function Code() { this.op = 0; /* operation, extra bits, table bits */ this.bits = 0; /* bits in this part of the code */ @@ -482,65 +491,6 @@ function inflate(strm, flush) { var order = /* permutation of code lengths */ [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; - function LOAD() { - put = strm.next_out_index; - output = strm.next_out; - left = strm.avail_out; - next = strm.next_in_index; - input = strm.next_in; - have = strm.avail_in; - hold = state.hold; - bits = state.bits; - } - - function RESTORE() { - strm.next_out_index = put; - strm.avail_out = left; - strm.next_in_index = next; - strm.avail_in = have; - state.hold = hold; - state.bits = bits; - } - - function ZSWAP32(q) { - return (((q >>> 24) & 0xff) + - ((q >>> 8) & 0xff00) + - ((q & 0xff00) << 8) + - ((q & 0xff) << 24)); - } - - /* - Return from inflate(), updating the total counts and the check value. - If there was no progress during the inflate() call, return a buffer - error. Call updatewindow() to create and/or update the window state. - Note: a memory error from inflate() is non-recoverable. - */ - function inf_leave() { - RESTORE(); - if (state.wsize || (_out !== strm.avail_out && state.mode < BAD && - (state.mode < CHECK || flush !== Z_FINISH))) { - if (updatewindow(strm, strm.next_out, strm.next_out_index, _out - strm.avail_out)) { - state.mode = MEM; - return Z_MEM_ERROR; - } - } - _in -= strm.avail_in; - _out -= strm.avail_out; - strm.total_in += _in; - strm.total_out += _out; - state.total += _out; - if (state.wrap && _out) { - strm.adler = state.check = /*UPDATE(state.check, strm.next_out_index - _out, _out);*/ - (state.flags ? crc32(state.check, output, _out, strm.next_out_index - _out) : adler32(state.check, output, _out, strm.next_out_index - _out)); - } - strm.data_type = state.bits + (state.last ? 64 : 0) + - (state.mode === TYPE ? 128 : 0) + - (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0); - if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) { - ret = Z_BUF_ERROR; - } - return ret; - } // TODO: check if needed and don't affect speed //if (strm === Z_NULL || strm.state === Z_NULL || strm.next_out === Z_NULL || @@ -551,11 +501,22 @@ function inflate(strm, flush) { if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */ - LOAD(); + //--- LOAD() --- + put = strm.next_out_index; + output = strm.next_out; + left = strm.avail_out; + next = strm.next_in_index; + input = strm.next_in; + have = strm.avail_in; + hold = state.hold; + bits = state.bits; + //--- _in = have; _out = left; ret = Z_OK; + + inf_leave: // goto emulation for (;;) { switch (state.mode) { case HEAD: @@ -565,7 +526,7 @@ function inflate(strm, flush) { } //=== NEEDBITS(16); while (bits < 16) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -626,7 +587,7 @@ function inflate(strm, flush) { case FLAGS: //=== NEEDBITS(16); */ while (bits < 16) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -662,7 +623,7 @@ function inflate(strm, flush) { case TIME: //=== NEEDBITS(32); */ while (bits < 32) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -689,7 +650,7 @@ function inflate(strm, flush) { case OS: //=== NEEDBITS(16); */ while (bits < 16) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -716,7 +677,7 @@ function inflate(strm, flush) { if (state.flags & 0x0400) { //=== NEEDBITS(16); */ while (bits < 16) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -763,14 +724,14 @@ function inflate(strm, flush) { next += copy; state.length -= copy; } - if (state.length) { return inf_leave(); } + if (state.length) { break inf_leave; } } state.length = 0; state.mode = NAME; /* falls through */ case NAME: if (state.flags & 0x0800) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } copy = 0; do { // TODO: 2 or 1 bytes? @@ -785,7 +746,7 @@ function inflate(strm, flush) { } have -= copy; next += copy; - if (len) { return inf_leave(); } + if (len) { break inf_leave; } } else if (state.head) { state.head.name = null; @@ -795,7 +756,7 @@ function inflate(strm, flush) { /* falls through */ case COMMENT: if (state.flags & 0x1000) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } copy = 0; do { len = input[next + copy++]; @@ -809,7 +770,7 @@ function inflate(strm, flush) { } have -= copy; next += copy; - if (len) { return inf_leave(); } + if (len) { break inf_leave; } } else if (state.head) { state.head.comment = null; @@ -820,7 +781,7 @@ function inflate(strm, flush) { if (state.flags & 0x0200) { //=== NEEDBITS(16); */ while (bits < 16) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -846,7 +807,7 @@ function inflate(strm, flush) { case DICTID: //=== NEEDBITS(32); */ while (bits < 32) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -861,14 +822,21 @@ function inflate(strm, flush) { /* falls through */ case DICT: if (state.havedict === 0) { - RESTORE(); + //--- RESTORE() --- + strm.next_out_index = put; + strm.avail_out = left; + strm.next_in_index = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; + //--- return Z_NEED_DICT; } strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; state.mode = TYPE; /* falls through */ case TYPE: - if (flush === Z_BLOCK || flush === Z_TREES) { return inf_leave(); } + if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; } /* falls through */ case TYPEDO: if (state.last) { @@ -881,7 +849,7 @@ function inflate(strm, flush) { } //=== NEEDBITS(3); */ while (bits < 3) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -909,7 +877,7 @@ function inflate(strm, flush) { hold >>>= 2; bits -= 2; //---// - return inf_leave(); + break inf_leave; } break; case 2: /* dynamic block */ @@ -933,7 +901,7 @@ function inflate(strm, flush) { //---// //=== NEEDBITS(32); */ while (bits < 32) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -952,7 +920,7 @@ function inflate(strm, flush) { bits = 0; //===// state.mode = COPY_; - if (flush === Z_TREES) { return inf_leave(); } + if (flush === Z_TREES) { break inf_leave; } /* falls through */ case COPY_: state.mode = COPY; @@ -962,7 +930,7 @@ function inflate(strm, flush) { if (copy) { if (copy > have) { copy = have; } if (copy > left) { copy = left; } - if (copy === 0) { return inf_leave(); } + if (copy === 0) { break inf_leave; } //--- zmemcpy(put, next, copy); --- utils.arraySet(output, input, next, copy, put); //---// @@ -979,7 +947,7 @@ function inflate(strm, flush) { case TABLE: //=== NEEDBITS(14); */ while (bits < 14) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1015,7 +983,7 @@ function inflate(strm, flush) { while (state.have < state.ncode) { //=== NEEDBITS(3); while (bits < 3) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1055,7 +1023,7 @@ function inflate(strm, flush) { state.lencode.fill(hold & ((1 << state.lenbits) - 1), here);/*BITS(state.lenbits)*/ if ((here.bits) <= bits) { break; } //--- PULLBYTE() ---// - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1073,7 +1041,7 @@ function inflate(strm, flush) { //=== NEEDBITS(here.bits + 2); n = here.bits + 2; while (bits < n) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1099,7 +1067,7 @@ function inflate(strm, flush) { //=== NEEDBITS(here.bits + 3); n = here.bits + 3; while (bits < n) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1120,7 +1088,7 @@ function inflate(strm, flush) { //=== NEEDBITS(here.bits + 7); n = here.bits + 7; while (bits < n) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1191,16 +1159,33 @@ function inflate(strm, flush) { } //Tracev((stderr, 'inflate: codes ok\n')); state.mode = LEN_; - if (flush === Z_TREES) { return inf_leave(); } + if (flush === Z_TREES) { break inf_leave; } /* falls through */ case LEN_: state.mode = LEN; /* falls through */ case LEN: if (have >= 6 && left >= 258) { - RESTORE(); + //--- RESTORE() --- + strm.next_out_index = put; + strm.avail_out = left; + strm.next_in_index = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; + //--- inflate_fast(strm, _out); - LOAD(); + //--- LOAD() --- + put = strm.next_out_index; + output = strm.next_out; + left = strm.avail_out; + next = strm.next_in_index; + input = strm.next_in; + have = strm.avail_in; + hold = state.hold; + bits = state.bits; + //--- + if (state.mode === TYPE) { state.back = -1; } @@ -1211,7 +1196,7 @@ function inflate(strm, flush) { state.lencode.fill(hold & ((1 << state.lenbits) -1),here); /*BITS(state.lenbits)*/ if (here.bits <= bits) { break; } //--- PULLBYTE() ---// - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1224,7 +1209,7 @@ function inflate(strm, flush) { ((hold & ((1 << (last.bits + last.op)) -1))/*BITS(last.bits + last.op)*/ >> last.bits), here); if ((last.bits + here.bits) <= bits) { break; } //--- PULLBYTE() ---// - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1268,7 +1253,7 @@ function inflate(strm, flush) { //=== NEEDBITS(state.extra); n = state.extra; while (bits < n) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1290,7 +1275,7 @@ function inflate(strm, flush) { state.distcode.fill(hold & ((1 << state.distbits) -1), here);/*BITS(state.distbits)*/ if ((here.bits) <= bits) { break; } //--- PULLBYTE() ---// - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1303,7 +1288,7 @@ function inflate(strm, flush) { ((hold & ((1 << (last.bits + last.op)) -1))/*BITS(last.bits + last.op)*/ >> last.bits), here); if ((last.bits + here.bits) <= bits) { break; } //--- PULLBYTE() ---// - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1334,7 +1319,7 @@ function inflate(strm, flush) { //=== NEEDBITS(state.extra); n = state.extra; while (bits < n) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1358,7 +1343,7 @@ function inflate(strm, flush) { state.mode = MATCH; /* falls through */ case MATCH: - if (left === 0) { return inf_leave(); } + if (left === 0) { break inf_leave; } copy = _out - left; if (state.offset > copy) { /* copy from window */ copy = state.offset - copy; @@ -1406,7 +1391,7 @@ function inflate(strm, flush) { if (state.length === 0) { state.mode = LEN; } break; case LIT: - if (left === 0) { return inf_leave(); } + if (left === 0) { break inf_leave; } output[put++] = state.length; left--; state.mode = LEN; @@ -1415,7 +1400,7 @@ function inflate(strm, flush) { if (state.wrap) { //=== NEEDBITS(32); while (bits < 32) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; hold >>>= 0; @@ -1449,7 +1434,7 @@ function inflate(strm, flush) { if (state.wrap && state.flags) { //=== NEEDBITS(32); while (bits < 32) { - if (have === 0) { return inf_leave(); } + if (have === 0) { break inf_leave; } have--; hold += input[next++] << bits; bits += 8; @@ -1470,10 +1455,10 @@ function inflate(strm, flush) { /* falls through */ case DONE: ret = Z_STREAM_END; - return inf_leave(); + break inf_leave; case BAD: ret = Z_DATA_ERROR; - return inf_leave(); + break inf_leave; case MEM: return Z_MEM_ERROR; case SYNC: @@ -1482,6 +1467,48 @@ function inflate(strm, flush) { return Z_STREAM_ERROR; } } + + // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave" + + /* + Return from inflate(), updating the total counts and the check value. + If there was no progress during the inflate() call, return a buffer + error. Call updatewindow() to create and/or update the window state. + Note: a memory error from inflate() is non-recoverable. + */ + + //--- RESTORE() --- + strm.next_out_index = put; + strm.avail_out = left; + strm.next_in_index = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; + //--- + + if (state.wsize || (_out !== strm.avail_out && state.mode < BAD && + (state.mode < CHECK || flush !== Z_FINISH))) { + if (updatewindow(strm, strm.next_out, strm.next_out_index, _out - strm.avail_out)) { + state.mode = MEM; + return Z_MEM_ERROR; + } + } + _in -= strm.avail_in; + _out -= strm.avail_out; + strm.total_in += _in; + strm.total_out += _out; + state.total += _out; + if (state.wrap && _out) { + strm.adler = state.check = /*UPDATE(state.check, strm.next_out_index - _out, _out);*/ + (state.flags ? crc32(state.check, output, _out, strm.next_out_index - _out) : adler32(state.check, output, _out, strm.next_out_index - _out)); + } + strm.data_type = state.bits + (state.last ? 64 : 0) + + (state.mode === TYPE ? 128 : 0) + + (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0); + if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) { + ret = Z_BUF_ERROR; + } + return ret; } function inflateEnd(strm) {