12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874 |
- 'use strict';
- var utils = require('../utils/common');
- var trees = require('./trees');
- var adler32 = require('./adler32');
- var crc32 = require('./crc32');
- var msg = require('./messages');
- var Z_NO_FLUSH = 0;
- var Z_PARTIAL_FLUSH = 1;
- var Z_FULL_FLUSH = 3;
- var Z_FINISH = 4;
- var Z_BLOCK = 5;
- var Z_OK = 0;
- var Z_STREAM_END = 1;
- var Z_STREAM_ERROR = -2;
- var Z_DATA_ERROR = -3;
- var Z_BUF_ERROR = -5;
- var Z_DEFAULT_COMPRESSION = -1;
- var Z_FILTERED = 1;
- var Z_HUFFMAN_ONLY = 2;
- var Z_RLE = 3;
- var Z_FIXED = 4;
- var Z_DEFAULT_STRATEGY = 0;
- var Z_UNKNOWN = 2;
- var Z_DEFLATED = 8;
- var MAX_MEM_LEVEL = 9;
- var MAX_WBITS = 15;
- var DEF_MEM_LEVEL = 8;
- var LENGTH_CODES = 29;
- var LITERALS = 256;
- var L_CODES = LITERALS + 1 + LENGTH_CODES;
- var D_CODES = 30;
- var BL_CODES = 19;
- var HEAP_SIZE = 2 * L_CODES + 1;
- var MAX_BITS = 15;
- var MIN_MATCH = 3;
- var MAX_MATCH = 258;
- var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
- var PRESET_DICT = 0x20;
- var INIT_STATE = 42;
- var EXTRA_STATE = 69;
- var NAME_STATE = 73;
- var COMMENT_STATE = 91;
- var HCRC_STATE = 103;
- var BUSY_STATE = 113;
- var FINISH_STATE = 666;
- var BS_NEED_MORE = 1;
- var BS_BLOCK_DONE = 2;
- var BS_FINISH_STARTED = 3;
- var BS_FINISH_DONE = 4;
- var OS_CODE = 0x03;
- function err(strm, errorCode) {
- strm.msg = msg[errorCode];
- return errorCode;
- }
- function rank(f) {
- return ((f) << 1) - ((f) > 4 ? 9 : 0);
- }
- function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
- function flush_pending(strm) {
- var s = strm.state;
-
- var len = s.pending;
- if (len > strm.avail_out) {
- len = strm.avail_out;
- }
- if (len === 0) { return; }
- utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
- strm.next_out += len;
- s.pending_out += len;
- strm.total_out += len;
- strm.avail_out -= len;
- s.pending -= len;
- if (s.pending === 0) {
- s.pending_out = 0;
- }
- }
- function flush_block_only(s, last) {
- trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
- s.block_start = s.strstart;
- flush_pending(s.strm);
- }
- function put_byte(s, b) {
- s.pending_buf[s.pending++] = b;
- }
- function putShortMSB(s, b) {
- s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
- s.pending_buf[s.pending++] = b & 0xff;
- }
- function read_buf(strm, buf, start, size) {
- var len = strm.avail_in;
- if (len > size) { len = size; }
- if (len === 0) { return 0; }
- strm.avail_in -= len;
-
- utils.arraySet(buf, strm.input, strm.next_in, len, start);
- if (strm.state.wrap === 1) {
- strm.adler = adler32(strm.adler, buf, len, start);
- }
- else if (strm.state.wrap === 2) {
- strm.adler = crc32(strm.adler, buf, len, start);
- }
- strm.next_in += len;
- strm.total_in += len;
- return len;
- }
- function longest_match(s, cur_match) {
- var chain_length = s.max_chain_length;
- var scan = s.strstart;
- var match;
- var len;
- var best_len = s.prev_length;
- var nice_match = s.nice_match;
- var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
- s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0;
- var _win = s.window;
- var wmask = s.w_mask;
- var prev = s.prev;
-
- var strend = s.strstart + MAX_MATCH;
- var scan_end1 = _win[scan + best_len - 1];
- var scan_end = _win[scan + best_len];
-
-
-
- if (s.prev_length >= s.good_match) {
- chain_length >>= 2;
- }
-
- if (nice_match > s.lookahead) { nice_match = s.lookahead; }
-
- do {
-
- match = cur_match;
-
- if (_win[match + best_len] !== scan_end ||
- _win[match + best_len - 1] !== scan_end1 ||
- _win[match] !== _win[scan] ||
- _win[++match] !== _win[scan + 1]) {
- continue;
- }
-
- scan += 2;
- match++;
-
-
- do {
-
- } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
- _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
- _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
- _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
- scan < strend);
-
- len = MAX_MATCH - (strend - scan);
- scan = strend - MAX_MATCH;
- if (len > best_len) {
- s.match_start = cur_match;
- best_len = len;
- if (len >= nice_match) {
- break;
- }
- scan_end1 = _win[scan + best_len - 1];
- scan_end = _win[scan + best_len];
- }
- } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
- if (best_len <= s.lookahead) {
- return best_len;
- }
- return s.lookahead;
- }
- function fill_window(s) {
- var _w_size = s.w_size;
- var p, n, m, more, str;
-
- do {
- more = s.window_size - s.lookahead - s.strstart;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
- utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
- s.match_start -= _w_size;
- s.strstart -= _w_size;
-
- s.block_start -= _w_size;
-
- n = s.hash_size;
- p = n;
- do {
- m = s.head[--p];
- s.head[p] = (m >= _w_size ? m - _w_size : 0);
- } while (--n);
- n = _w_size;
- p = n;
- do {
- m = s.prev[--p];
- s.prev[p] = (m >= _w_size ? m - _w_size : 0);
-
- } while (--n);
- more += _w_size;
- }
- if (s.strm.avail_in === 0) {
- break;
- }
-
-
- n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
- s.lookahead += n;
-
- if (s.lookahead + s.insert >= MIN_MATCH) {
- str = s.strstart - s.insert;
- s.ins_h = s.window[str];
-
- s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
- while (s.insert) {
-
- s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
- s.prev[str & s.w_mask] = s.head[s.ins_h];
- s.head[s.ins_h] = str;
- str++;
- s.insert--;
- if (s.lookahead + s.insert < MIN_MATCH) {
- break;
- }
- }
- }
-
- } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
-
- }
- function deflate_stored(s, flush) {
-
- var max_block_size = 0xffff;
- if (max_block_size > s.pending_buf_size - 5) {
- max_block_size = s.pending_buf_size - 5;
- }
-
- for (;;) {
-
- if (s.lookahead <= 1) {
-
-
- fill_window(s);
- if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
- return BS_NEED_MORE;
- }
- if (s.lookahead === 0) {
- break;
- }
-
- }
-
- s.strstart += s.lookahead;
- s.lookahead = 0;
-
- var max_start = s.block_start + max_block_size;
- if (s.strstart === 0 || s.strstart >= max_start) {
-
- s.lookahead = s.strstart - max_start;
- s.strstart = max_start;
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
-
- if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- }
- s.insert = 0;
- if (flush === Z_FINISH) {
-
- flush_block_only(s, true);
- if (s.strm.avail_out === 0) {
- return BS_FINISH_STARTED;
- }
-
- return BS_FINISH_DONE;
- }
- if (s.strstart > s.block_start) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- return BS_NEED_MORE;
- }
- function deflate_fast(s, flush) {
- var hash_head;
- var bflush;
- for (;;) {
-
- if (s.lookahead < MIN_LOOKAHEAD) {
- fill_window(s);
- if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
- return BS_NEED_MORE;
- }
- if (s.lookahead === 0) {
- break;
- }
- }
-
- hash_head = 0;
- if (s.lookahead >= MIN_MATCH) {
-
- s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
- hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
- s.head[s.ins_h] = s.strstart;
-
- }
-
- if (hash_head !== 0 && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
-
- s.match_length = longest_match(s, hash_head);
-
- }
- if (s.match_length >= MIN_MATCH) {
-
-
- bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
- s.lookahead -= s.match_length;
-
- if (s.match_length <= s.max_lazy_match && s.lookahead >= MIN_MATCH) {
- s.match_length--;
- do {
- s.strstart++;
-
- s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
- hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
- s.head[s.ins_h] = s.strstart;
-
-
- } while (--s.match_length !== 0);
- s.strstart++;
- } else
- {
- s.strstart += s.match_length;
- s.match_length = 0;
- s.ins_h = s.window[s.strstart];
-
- s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
-
- }
- } else {
-
-
-
- bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
- s.lookahead--;
- s.strstart++;
- }
- if (bflush) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- }
- s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
- if (flush === Z_FINISH) {
-
- flush_block_only(s, true);
- if (s.strm.avail_out === 0) {
- return BS_FINISH_STARTED;
- }
-
- return BS_FINISH_DONE;
- }
- if (s.last_lit) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- return BS_BLOCK_DONE;
- }
- function deflate_slow(s, flush) {
- var hash_head;
- var bflush;
- var max_insert;
-
- for (;;) {
-
- if (s.lookahead < MIN_LOOKAHEAD) {
- fill_window(s);
- if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
- return BS_NEED_MORE;
- }
- if (s.lookahead === 0) { break; }
- }
-
- hash_head = 0;
- if (s.lookahead >= MIN_MATCH) {
-
- s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
- hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
- s.head[s.ins_h] = s.strstart;
-
- }
-
- s.prev_length = s.match_length;
- s.prev_match = s.match_start;
- s.match_length = MIN_MATCH - 1;
- if (hash_head !== 0 && s.prev_length < s.max_lazy_match &&
- s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)) {
-
- s.match_length = longest_match(s, hash_head);
-
- if (s.match_length <= 5 &&
- (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096))) {
-
- s.match_length = MIN_MATCH - 1;
- }
- }
-
- if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
- max_insert = s.strstart + s.lookahead - MIN_MATCH;
-
-
-
- bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
-
- s.lookahead -= s.prev_length - 1;
- s.prev_length -= 2;
- do {
- if (++s.strstart <= max_insert) {
-
- s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
- hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
- s.head[s.ins_h] = s.strstart;
-
- }
- } while (--s.prev_length !== 0);
- s.match_available = 0;
- s.match_length = MIN_MATCH - 1;
- s.strstart++;
- if (bflush) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- } else if (s.match_available) {
-
-
-
- bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
- if (bflush) {
-
- flush_block_only(s, false);
-
- }
- s.strstart++;
- s.lookahead--;
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
- } else {
-
- s.match_available = 1;
- s.strstart++;
- s.lookahead--;
- }
- }
-
- if (s.match_available) {
-
-
- bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
- s.match_available = 0;
- }
- s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
- if (flush === Z_FINISH) {
-
- flush_block_only(s, true);
- if (s.strm.avail_out === 0) {
- return BS_FINISH_STARTED;
- }
-
- return BS_FINISH_DONE;
- }
- if (s.last_lit) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- return BS_BLOCK_DONE;
- }
- function deflate_rle(s, flush) {
- var bflush;
- var prev;
- var scan, strend;
- var _win = s.window;
- for (;;) {
-
- if (s.lookahead <= MAX_MATCH) {
- fill_window(s);
- if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
- return BS_NEED_MORE;
- }
- if (s.lookahead === 0) { break; }
- }
-
- s.match_length = 0;
- if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
- scan = s.strstart - 1;
- prev = _win[scan];
- if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
- strend = s.strstart + MAX_MATCH;
- do {
-
- } while (prev === _win[++scan] && prev === _win[++scan] &&
- prev === _win[++scan] && prev === _win[++scan] &&
- prev === _win[++scan] && prev === _win[++scan] &&
- prev === _win[++scan] && prev === _win[++scan] &&
- scan < strend);
- s.match_length = MAX_MATCH - (strend - scan);
- if (s.match_length > s.lookahead) {
- s.match_length = s.lookahead;
- }
- }
-
- }
-
- if (s.match_length >= MIN_MATCH) {
-
-
- bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
- s.lookahead -= s.match_length;
- s.strstart += s.match_length;
- s.match_length = 0;
- } else {
-
-
-
- bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
- s.lookahead--;
- s.strstart++;
- }
- if (bflush) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- }
- s.insert = 0;
- if (flush === Z_FINISH) {
-
- flush_block_only(s, true);
- if (s.strm.avail_out === 0) {
- return BS_FINISH_STARTED;
- }
-
- return BS_FINISH_DONE;
- }
- if (s.last_lit) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- return BS_BLOCK_DONE;
- }
- function deflate_huff(s, flush) {
- var bflush;
- for (;;) {
-
- if (s.lookahead === 0) {
- fill_window(s);
- if (s.lookahead === 0) {
- if (flush === Z_NO_FLUSH) {
- return BS_NEED_MORE;
- }
- break;
- }
- }
-
- s.match_length = 0;
-
-
- bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
- s.lookahead--;
- s.strstart++;
- if (bflush) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- }
- s.insert = 0;
- if (flush === Z_FINISH) {
-
- flush_block_only(s, true);
- if (s.strm.avail_out === 0) {
- return BS_FINISH_STARTED;
- }
-
- return BS_FINISH_DONE;
- }
- if (s.last_lit) {
-
- flush_block_only(s, false);
- if (s.strm.avail_out === 0) {
- return BS_NEED_MORE;
- }
-
- }
- return BS_BLOCK_DONE;
- }
- function Config(good_length, max_lazy, nice_length, max_chain, func) {
- this.good_length = good_length;
- this.max_lazy = max_lazy;
- this.nice_length = nice_length;
- this.max_chain = max_chain;
- this.func = func;
- }
- var configuration_table;
- configuration_table = [
-
- new Config(0, 0, 0, 0, deflate_stored),
- new Config(4, 4, 8, 4, deflate_fast),
- new Config(4, 5, 16, 8, deflate_fast),
- new Config(4, 6, 32, 32, deflate_fast),
- new Config(4, 4, 16, 16, deflate_slow),
- new Config(8, 16, 32, 32, deflate_slow),
- new Config(8, 16, 128, 128, deflate_slow),
- new Config(8, 32, 128, 256, deflate_slow),
- new Config(32, 128, 258, 1024, deflate_slow),
- new Config(32, 258, 258, 4096, deflate_slow)
- ];
- function lm_init(s) {
- s.window_size = 2 * s.w_size;
-
- zero(s.head);
-
- s.max_lazy_match = configuration_table[s.level].max_lazy;
- s.good_match = configuration_table[s.level].good_length;
- s.nice_match = configuration_table[s.level].nice_length;
- s.max_chain_length = configuration_table[s.level].max_chain;
- s.strstart = 0;
- s.block_start = 0;
- s.lookahead = 0;
- s.insert = 0;
- s.match_length = s.prev_length = MIN_MATCH - 1;
- s.match_available = 0;
- s.ins_h = 0;
- }
- function DeflateState() {
- this.strm = null;
- this.status = 0;
- this.pending_buf = null;
- this.pending_buf_size = 0;
- this.pending_out = 0;
- this.pending = 0;
- this.wrap = 0;
- this.gzhead = null;
- this.gzindex = 0;
- this.method = Z_DEFLATED;
- this.last_flush = -1;
- this.w_size = 0;
- this.w_bits = 0;
- this.w_mask = 0;
- this.window = null;
-
- this.window_size = 0;
-
- this.prev = null;
-
- this.head = null;
- this.ins_h = 0;
- this.hash_size = 0;
- this.hash_bits = 0;
- this.hash_mask = 0;
- this.hash_shift = 0;
-
- this.block_start = 0;
-
- this.match_length = 0;
- this.prev_match = 0;
- this.match_available = 0;
- this.strstart = 0;
- this.match_start = 0;
- this.lookahead = 0;
- this.prev_length = 0;
-
- this.max_chain_length = 0;
-
- this.max_lazy_match = 0;
-
-
-
-
- this.level = 0;
- this.strategy = 0;
- this.good_match = 0;
-
- this.nice_match = 0;
-
-
-
-
-
-
-
- this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
- this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2);
- this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2);
- zero(this.dyn_ltree);
- zero(this.dyn_dtree);
- zero(this.bl_tree);
- this.l_desc = null;
- this.d_desc = null;
- this.bl_desc = null;
-
- this.bl_count = new utils.Buf16(MAX_BITS + 1);
-
-
- this.heap = new utils.Buf16(2 * L_CODES + 1);
- zero(this.heap);
- this.heap_len = 0;
- this.heap_max = 0;
-
- this.depth = new utils.Buf16(2 * L_CODES + 1);
- zero(this.depth);
-
- this.l_buf = 0;
- this.lit_bufsize = 0;
-
- this.last_lit = 0;
- this.d_buf = 0;
-
- this.opt_len = 0;
- this.static_len = 0;
- this.matches = 0;
- this.insert = 0;
- this.bi_buf = 0;
-
- this.bi_valid = 0;
-
-
-
-
-
- }
- function deflateResetKeep(strm) {
- var s;
- if (!strm || !strm.state) {
- return err(strm, Z_STREAM_ERROR);
- }
- strm.total_in = strm.total_out = 0;
- strm.data_type = Z_UNKNOWN;
- s = strm.state;
- s.pending = 0;
- s.pending_out = 0;
- if (s.wrap < 0) {
- s.wrap = -s.wrap;
-
- }
- s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
- strm.adler = (s.wrap === 2) ?
- 0
- :
- 1;
- s.last_flush = Z_NO_FLUSH;
- trees._tr_init(s);
- return Z_OK;
- }
- function deflateReset(strm) {
- var ret = deflateResetKeep(strm);
- if (ret === Z_OK) {
- lm_init(strm.state);
- }
- return ret;
- }
- function deflateSetHeader(strm, head) {
- if (!strm || !strm.state) { return Z_STREAM_ERROR; }
- if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
- strm.state.gzhead = head;
- return Z_OK;
- }
- function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
- if (!strm) {
- return Z_STREAM_ERROR;
- }
- var wrap = 1;
- if (level === Z_DEFAULT_COMPRESSION) {
- level = 6;
- }
- if (windowBits < 0) {
- wrap = 0;
- windowBits = -windowBits;
- }
- else if (windowBits > 15) {
- wrap = 2;
- windowBits -= 16;
- }
- if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
- windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
- strategy < 0 || strategy > Z_FIXED) {
- return err(strm, Z_STREAM_ERROR);
- }
- if (windowBits === 8) {
- windowBits = 9;
- }
-
- var s = new DeflateState();
- strm.state = s;
- s.strm = strm;
- s.wrap = wrap;
- s.gzhead = null;
- s.w_bits = windowBits;
- s.w_size = 1 << s.w_bits;
- s.w_mask = s.w_size - 1;
- s.hash_bits = memLevel + 7;
- s.hash_size = 1 << s.hash_bits;
- s.hash_mask = s.hash_size - 1;
- s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
- s.window = new utils.Buf8(s.w_size * 2);
- s.head = new utils.Buf16(s.hash_size);
- s.prev = new utils.Buf16(s.w_size);
-
-
- s.lit_bufsize = 1 << (memLevel + 6);
- s.pending_buf_size = s.lit_bufsize * 4;
-
-
- s.pending_buf = new utils.Buf8(s.pending_buf_size);
-
-
- s.d_buf = 1 * s.lit_bufsize;
-
- s.l_buf = (1 + 2) * s.lit_bufsize;
- s.level = level;
- s.strategy = strategy;
- s.method = method;
- return deflateReset(strm);
- }
- function deflateInit(strm, level) {
- return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
- }
- function deflate(strm, flush) {
- var old_flush, s;
- var beg, val;
- if (!strm || !strm.state ||
- flush > Z_BLOCK || flush < 0) {
- return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
- }
- s = strm.state;
- if (!strm.output ||
- (!strm.input && strm.avail_in !== 0) ||
- (s.status === FINISH_STATE && flush !== Z_FINISH)) {
- return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
- }
- s.strm = strm;
- old_flush = s.last_flush;
- s.last_flush = flush;
-
- if (s.status === INIT_STATE) {
- if (s.wrap === 2) {
- strm.adler = 0;
- put_byte(s, 31);
- put_byte(s, 139);
- put_byte(s, 8);
- if (!s.gzhead) {
- put_byte(s, 0);
- put_byte(s, 0);
- put_byte(s, 0);
- put_byte(s, 0);
- put_byte(s, 0);
- put_byte(s, s.level === 9 ? 2 :
- (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
- 4 : 0));
- put_byte(s, OS_CODE);
- s.status = BUSY_STATE;
- }
- else {
- put_byte(s, (s.gzhead.text ? 1 : 0) +
- (s.gzhead.hcrc ? 2 : 0) +
- (!s.gzhead.extra ? 0 : 4) +
- (!s.gzhead.name ? 0 : 8) +
- (!s.gzhead.comment ? 0 : 16)
- );
- put_byte(s, s.gzhead.time & 0xff);
- put_byte(s, (s.gzhead.time >> 8) & 0xff);
- put_byte(s, (s.gzhead.time >> 16) & 0xff);
- put_byte(s, (s.gzhead.time >> 24) & 0xff);
- put_byte(s, s.level === 9 ? 2 :
- (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
- 4 : 0));
- put_byte(s, s.gzhead.os & 0xff);
- if (s.gzhead.extra && s.gzhead.extra.length) {
- put_byte(s, s.gzhead.extra.length & 0xff);
- put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
- }
- if (s.gzhead.hcrc) {
- strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
- }
- s.gzindex = 0;
- s.status = EXTRA_STATE;
- }
- }
- else
- {
- var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
- var level_flags = -1;
- if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
- level_flags = 0;
- } else if (s.level < 6) {
- level_flags = 1;
- } else if (s.level === 6) {
- level_flags = 2;
- } else {
- level_flags = 3;
- }
- header |= (level_flags << 6);
- if (s.strstart !== 0) { header |= PRESET_DICT; }
- header += 31 - (header % 31);
- s.status = BUSY_STATE;
- putShortMSB(s, header);
-
- if (s.strstart !== 0) {
- putShortMSB(s, strm.adler >>> 16);
- putShortMSB(s, strm.adler & 0xffff);
- }
- strm.adler = 1;
- }
- }
- if (s.status === EXTRA_STATE) {
- if (s.gzhead.extra) {
- beg = s.pending;
- while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
- if (s.pending === s.pending_buf_size) {
- if (s.gzhead.hcrc && s.pending > beg) {
- strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
- }
- flush_pending(strm);
- beg = s.pending;
- if (s.pending === s.pending_buf_size) {
- break;
- }
- }
- put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
- s.gzindex++;
- }
- if (s.gzhead.hcrc && s.pending > beg) {
- strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
- }
- if (s.gzindex === s.gzhead.extra.length) {
- s.gzindex = 0;
- s.status = NAME_STATE;
- }
- }
- else {
- s.status = NAME_STATE;
- }
- }
- if (s.status === NAME_STATE) {
- if (s.gzhead.name) {
- beg = s.pending;
-
- do {
- if (s.pending === s.pending_buf_size) {
- if (s.gzhead.hcrc && s.pending > beg) {
- strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
- }
- flush_pending(strm);
- beg = s.pending;
- if (s.pending === s.pending_buf_size) {
- val = 1;
- break;
- }
- }
-
- if (s.gzindex < s.gzhead.name.length) {
- val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
- } else {
- val = 0;
- }
- put_byte(s, val);
- } while (val !== 0);
- if (s.gzhead.hcrc && s.pending > beg) {
- strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
- }
- if (val === 0) {
- s.gzindex = 0;
- s.status = COMMENT_STATE;
- }
- }
- else {
- s.status = COMMENT_STATE;
- }
- }
- if (s.status === COMMENT_STATE) {
- if (s.gzhead.comment) {
- beg = s.pending;
-
- do {
- if (s.pending === s.pending_buf_size) {
- if (s.gzhead.hcrc && s.pending > beg) {
- strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
- }
- flush_pending(strm);
- beg = s.pending;
- if (s.pending === s.pending_buf_size) {
- val = 1;
- break;
- }
- }
-
- if (s.gzindex < s.gzhead.comment.length) {
- val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
- } else {
- val = 0;
- }
- put_byte(s, val);
- } while (val !== 0);
- if (s.gzhead.hcrc && s.pending > beg) {
- strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
- }
- if (val === 0) {
- s.status = HCRC_STATE;
- }
- }
- else {
- s.status = HCRC_STATE;
- }
- }
- if (s.status === HCRC_STATE) {
- if (s.gzhead.hcrc) {
- if (s.pending + 2 > s.pending_buf_size) {
- flush_pending(strm);
- }
- if (s.pending + 2 <= s.pending_buf_size) {
- put_byte(s, strm.adler & 0xff);
- put_byte(s, (strm.adler >> 8) & 0xff);
- strm.adler = 0;
- s.status = BUSY_STATE;
- }
- }
- else {
- s.status = BUSY_STATE;
- }
- }
-
- if (s.pending !== 0) {
- flush_pending(strm);
- if (strm.avail_out === 0) {
-
- s.last_flush = -1;
- return Z_OK;
- }
-
- } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
- flush !== Z_FINISH) {
- return err(strm, Z_BUF_ERROR);
- }
-
- if (s.status === FINISH_STATE && strm.avail_in !== 0) {
- return err(strm, Z_BUF_ERROR);
- }
-
- if (strm.avail_in !== 0 || s.lookahead !== 0 ||
- (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
- var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
- (s.strategy === Z_RLE ? deflate_rle(s, flush) :
- configuration_table[s.level].func(s, flush));
- if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
- s.status = FINISH_STATE;
- }
- if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
- if (strm.avail_out === 0) {
- s.last_flush = -1;
-
- }
- return Z_OK;
-
- }
- if (bstate === BS_BLOCK_DONE) {
- if (flush === Z_PARTIAL_FLUSH) {
- trees._tr_align(s);
- }
- else if (flush !== Z_BLOCK) {
- trees._tr_stored_block(s, 0, 0, false);
-
- if (flush === Z_FULL_FLUSH) {
-
- zero(s.head);
- if (s.lookahead === 0) {
- s.strstart = 0;
- s.block_start = 0;
- s.insert = 0;
- }
- }
- }
- flush_pending(strm);
- if (strm.avail_out === 0) {
- s.last_flush = -1;
- return Z_OK;
- }
- }
- }
-
-
- if (flush !== Z_FINISH) { return Z_OK; }
- if (s.wrap <= 0) { return Z_STREAM_END; }
-
- if (s.wrap === 2) {
- put_byte(s, strm.adler & 0xff);
- put_byte(s, (strm.adler >> 8) & 0xff);
- put_byte(s, (strm.adler >> 16) & 0xff);
- put_byte(s, (strm.adler >> 24) & 0xff);
- put_byte(s, strm.total_in & 0xff);
- put_byte(s, (strm.total_in >> 8) & 0xff);
- put_byte(s, (strm.total_in >> 16) & 0xff);
- put_byte(s, (strm.total_in >> 24) & 0xff);
- }
- else
- {
- putShortMSB(s, strm.adler >>> 16);
- putShortMSB(s, strm.adler & 0xffff);
- }
- flush_pending(strm);
-
- if (s.wrap > 0) { s.wrap = -s.wrap; }
-
- return s.pending !== 0 ? Z_OK : Z_STREAM_END;
- }
- function deflateEnd(strm) {
- var status;
- if (!strm || !strm.state) {
- return Z_STREAM_ERROR;
- }
- status = strm.state.status;
- if (status !== INIT_STATE &&
- status !== EXTRA_STATE &&
- status !== NAME_STATE &&
- status !== COMMENT_STATE &&
- status !== HCRC_STATE &&
- status !== BUSY_STATE &&
- status !== FINISH_STATE
- ) {
- return err(strm, Z_STREAM_ERROR);
- }
- strm.state = null;
- return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
- }
- function deflateSetDictionary(strm, dictionary) {
- var dictLength = dictionary.length;
- var s;
- var str, n;
- var wrap;
- var avail;
- var next;
- var input;
- var tmpDict;
- if (!strm || !strm.state) {
- return Z_STREAM_ERROR;
- }
- s = strm.state;
- wrap = s.wrap;
- if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
- return Z_STREAM_ERROR;
- }
-
- if (wrap === 1) {
-
- strm.adler = adler32(strm.adler, dictionary, dictLength, 0);
- }
- s.wrap = 0;
-
- if (dictLength >= s.w_size) {
- if (wrap === 0) {
-
- zero(s.head);
- s.strstart = 0;
- s.block_start = 0;
- s.insert = 0;
- }
-
-
- tmpDict = new utils.Buf8(s.w_size);
- utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0);
- dictionary = tmpDict;
- dictLength = s.w_size;
- }
-
- avail = strm.avail_in;
- next = strm.next_in;
- input = strm.input;
- strm.avail_in = dictLength;
- strm.next_in = 0;
- strm.input = dictionary;
- fill_window(s);
- while (s.lookahead >= MIN_MATCH) {
- str = s.strstart;
- n = s.lookahead - (MIN_MATCH - 1);
- do {
-
- s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
- s.prev[str & s.w_mask] = s.head[s.ins_h];
- s.head[s.ins_h] = str;
- str++;
- } while (--n);
- s.strstart = str;
- s.lookahead = MIN_MATCH - 1;
- fill_window(s);
- }
- s.strstart += s.lookahead;
- s.block_start = s.strstart;
- s.insert = s.lookahead;
- s.lookahead = 0;
- s.match_length = s.prev_length = MIN_MATCH - 1;
- s.match_available = 0;
- strm.next_in = next;
- strm.input = input;
- strm.avail_in = avail;
- s.wrap = wrap;
- return Z_OK;
- }
- exports.deflateInit = deflateInit;
- exports.deflateInit2 = deflateInit2;
- exports.deflateReset = deflateReset;
- exports.deflateResetKeep = deflateResetKeep;
- exports.deflateSetHeader = deflateSetHeader;
- exports.deflate = deflate;
- exports.deflateEnd = deflateEnd;
- exports.deflateSetDictionary = deflateSetDictionary;
- exports.deflateInfo = 'pako deflate (from Nodeca project)';
|