pako.js 204 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606
  1. /* pako 0.2.9 nodeca/pako */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pako = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. 'use strict';
  3. var zlib_deflate = require('./zlib/deflate');
  4. var utils = require('./utils/common');
  5. var strings = require('./utils/strings');
  6. var msg = require('./zlib/messages');
  7. var ZStream = require('./zlib/zstream');
  8. var toString = Object.prototype.toString;
  9. /* Public constants ==========================================================*/
  10. /* ===========================================================================*/
  11. var Z_NO_FLUSH = 0;
  12. var Z_FINISH = 4;
  13. var Z_OK = 0;
  14. var Z_STREAM_END = 1;
  15. var Z_SYNC_FLUSH = 2;
  16. var Z_DEFAULT_COMPRESSION = -1;
  17. var Z_DEFAULT_STRATEGY = 0;
  18. var Z_DEFLATED = 8;
  19. /* ===========================================================================*/
  20. /**
  21. * class Deflate
  22. *
  23. * Generic JS-style wrapper for zlib calls. If you don't need
  24. * streaming behaviour - use more simple functions: [[deflate]],
  25. * [[deflateRaw]] and [[gzip]].
  26. **/
  27. /* internal
  28. * Deflate.chunks -> Array
  29. *
  30. * Chunks of output data, if [[Deflate#onData]] not overriden.
  31. **/
  32. /**
  33. * Deflate.result -> Uint8Array|Array
  34. *
  35. * Compressed result, generated by default [[Deflate#onData]]
  36. * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
  37. * (call [[Deflate#push]] with `Z_FINISH` / `true` param) or if you
  38. * push a chunk with explicit flush (call [[Deflate#push]] with
  39. * `Z_SYNC_FLUSH` param).
  40. **/
  41. /**
  42. * Deflate.err -> Number
  43. *
  44. * Error code after deflate finished. 0 (Z_OK) on success.
  45. * You will not need it in real life, because deflate errors
  46. * are possible only on wrong options or bad `onData` / `onEnd`
  47. * custom handlers.
  48. **/
  49. /**
  50. * Deflate.msg -> String
  51. *
  52. * Error message, if [[Deflate.err]] != 0
  53. **/
  54. /**
  55. * new Deflate(options)
  56. * - options (Object): zlib deflate options.
  57. *
  58. * Creates new deflator instance with specified params. Throws exception
  59. * on bad params. Supported options:
  60. *
  61. * - `level`
  62. * - `windowBits`
  63. * - `memLevel`
  64. * - `strategy`
  65. * - `dictionary`
  66. *
  67. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  68. * for more information on these.
  69. *
  70. * Additional options, for internal needs:
  71. *
  72. * - `chunkSize` - size of generated data chunks (16K by default)
  73. * - `raw` (Boolean) - do raw deflate
  74. * - `gzip` (Boolean) - create gzip wrapper
  75. * - `to` (String) - if equal to 'string', then result will be "binary string"
  76. * (each char code [0..255])
  77. * - `header` (Object) - custom header for gzip
  78. * - `text` (Boolean) - true if compressed data believed to be text
  79. * - `time` (Number) - modification time, unix timestamp
  80. * - `os` (Number) - operation system code
  81. * - `extra` (Array) - array of bytes with extra data (max 65536)
  82. * - `name` (String) - file name (binary string)
  83. * - `comment` (String) - comment (binary string)
  84. * - `hcrc` (Boolean) - true if header crc should be added
  85. *
  86. * ##### Example:
  87. *
  88. * ```javascript
  89. * var pako = require('pako')
  90. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  91. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  92. *
  93. * var deflate = new pako.Deflate({ level: 3});
  94. *
  95. * deflate.push(chunk1, false);
  96. * deflate.push(chunk2, true); // true -> last chunk
  97. *
  98. * if (deflate.err) { throw new Error(deflate.err); }
  99. *
  100. * console.log(deflate.result);
  101. * ```
  102. **/
  103. function Deflate(options) {
  104. if (!(this instanceof Deflate)) return new Deflate(options);
  105. this.options = utils.assign({
  106. level: Z_DEFAULT_COMPRESSION,
  107. method: Z_DEFLATED,
  108. chunkSize: 16384,
  109. windowBits: 15,
  110. memLevel: 8,
  111. strategy: Z_DEFAULT_STRATEGY,
  112. to: ''
  113. }, options || {});
  114. var opt = this.options;
  115. if (opt.raw && (opt.windowBits > 0)) {
  116. opt.windowBits = -opt.windowBits;
  117. }
  118. else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
  119. opt.windowBits += 16;
  120. }
  121. this.err = 0; // error code, if happens (0 = Z_OK)
  122. this.msg = ''; // error message
  123. this.ended = false; // used to avoid multiple onEnd() calls
  124. this.chunks = []; // chunks of compressed data
  125. this.strm = new ZStream();
  126. this.strm.avail_out = 0;
  127. var status = zlib_deflate.deflateInit2(
  128. this.strm,
  129. opt.level,
  130. opt.method,
  131. opt.windowBits,
  132. opt.memLevel,
  133. opt.strategy
  134. );
  135. if (status !== Z_OK) {
  136. throw new Error(msg[status]);
  137. }
  138. if (opt.header) {
  139. zlib_deflate.deflateSetHeader(this.strm, opt.header);
  140. }
  141. if (opt.dictionary) {
  142. var dict;
  143. // Convert data if needed
  144. if (typeof opt.dictionary === 'string') {
  145. // If we need to compress text, change encoding to utf8.
  146. dict = strings.string2buf(opt.dictionary);
  147. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  148. dict = new Uint8Array(opt.dictionary);
  149. } else {
  150. dict = opt.dictionary;
  151. }
  152. status = zlib_deflate.deflateSetDictionary(this.strm, dict);
  153. if (status !== Z_OK) {
  154. throw new Error(msg[status]);
  155. }
  156. this._dict_set = true;
  157. }
  158. }
  159. /**
  160. * Deflate#push(data[, mode]) -> Boolean
  161. * - data (Uint8Array|Array|ArrayBuffer|String): input data. Strings will be
  162. * converted to utf8 byte sequence.
  163. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  164. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
  165. *
  166. * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
  167. * new compressed chunks. Returns `true` on success. The last data block must have
  168. * mode Z_FINISH (or `true`). That will flush internal pending buffers and call
  169. * [[Deflate#onEnd]]. For interim explicit flushes (without ending the stream) you
  170. * can use mode Z_SYNC_FLUSH, keeping the compression context.
  171. *
  172. * On fail call [[Deflate#onEnd]] with error code and return false.
  173. *
  174. * We strongly recommend to use `Uint8Array` on input for best speed (output
  175. * array format is detected automatically). Also, don't skip last param and always
  176. * use the same type in your code (boolean or number). That will improve JS speed.
  177. *
  178. * For regular `Array`-s make sure all elements are [0..255].
  179. *
  180. * ##### Example
  181. *
  182. * ```javascript
  183. * push(chunk, false); // push one of data chunks
  184. * ...
  185. * push(chunk, true); // push last chunk
  186. * ```
  187. **/
  188. Deflate.prototype.push = function (data, mode) {
  189. var strm = this.strm;
  190. var chunkSize = this.options.chunkSize;
  191. var status, _mode;
  192. if (this.ended) { return false; }
  193. _mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
  194. // Convert data if needed
  195. if (typeof data === 'string') {
  196. // If we need to compress text, change encoding to utf8.
  197. strm.input = strings.string2buf(data);
  198. } else if (toString.call(data) === '[object ArrayBuffer]') {
  199. strm.input = new Uint8Array(data);
  200. } else {
  201. strm.input = data;
  202. }
  203. strm.next_in = 0;
  204. strm.avail_in = strm.input.length;
  205. do {
  206. if (strm.avail_out === 0) {
  207. strm.output = new utils.Buf8(chunkSize);
  208. strm.next_out = 0;
  209. strm.avail_out = chunkSize;
  210. }
  211. status = zlib_deflate.deflate(strm, _mode); /* no bad return value */
  212. if (status !== Z_STREAM_END && status !== Z_OK) {
  213. this.onEnd(status);
  214. this.ended = true;
  215. return false;
  216. }
  217. if (strm.avail_out === 0 || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) {
  218. if (this.options.to === 'string') {
  219. this.onData(strings.buf2binstring(utils.shrinkBuf(strm.output, strm.next_out)));
  220. } else {
  221. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  222. }
  223. }
  224. } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END);
  225. // Finalize on the last chunk.
  226. if (_mode === Z_FINISH) {
  227. status = zlib_deflate.deflateEnd(this.strm);
  228. this.onEnd(status);
  229. this.ended = true;
  230. return status === Z_OK;
  231. }
  232. // callback interim results if Z_SYNC_FLUSH.
  233. if (_mode === Z_SYNC_FLUSH) {
  234. this.onEnd(Z_OK);
  235. strm.avail_out = 0;
  236. return true;
  237. }
  238. return true;
  239. };
  240. /**
  241. * Deflate#onData(chunk) -> Void
  242. * - chunk (Uint8Array|Array|String): ouput data. Type of array depends
  243. * on js engine support. When string output requested, each chunk
  244. * will be string.
  245. *
  246. * By default, stores data blocks in `chunks[]` property and glue
  247. * those in `onEnd`. Override this handler, if you need another behaviour.
  248. **/
  249. Deflate.prototype.onData = function (chunk) {
  250. this.chunks.push(chunk);
  251. };
  252. /**
  253. * Deflate#onEnd(status) -> Void
  254. * - status (Number): deflate status. 0 (Z_OK) on success,
  255. * other if not.
  256. *
  257. * Called once after you tell deflate that the input stream is
  258. * complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
  259. * or if an error happened. By default - join collected chunks,
  260. * free memory and fill `results` / `err` properties.
  261. **/
  262. Deflate.prototype.onEnd = function (status) {
  263. // On success - join
  264. if (status === Z_OK) {
  265. if (this.options.to === 'string') {
  266. this.result = this.chunks.join('');
  267. } else {
  268. this.result = utils.flattenChunks(this.chunks);
  269. }
  270. }
  271. this.chunks = [];
  272. this.err = status;
  273. this.msg = this.strm.msg;
  274. };
  275. /**
  276. * deflate(data[, options]) -> Uint8Array|Array|String
  277. * - data (Uint8Array|Array|String): input data to compress.
  278. * - options (Object): zlib deflate options.
  279. *
  280. * Compress `data` with deflate algorithm and `options`.
  281. *
  282. * Supported options are:
  283. *
  284. * - level
  285. * - windowBits
  286. * - memLevel
  287. * - strategy
  288. * - dictionary
  289. *
  290. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  291. * for more information on these.
  292. *
  293. * Sugar (options):
  294. *
  295. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  296. * negative windowBits implicitly.
  297. * - `to` (String) - if equal to 'string', then result will be "binary string"
  298. * (each char code [0..255])
  299. *
  300. * ##### Example:
  301. *
  302. * ```javascript
  303. * var pako = require('pako')
  304. * , data = Uint8Array([1,2,3,4,5,6,7,8,9]);
  305. *
  306. * console.log(pako.deflate(data));
  307. * ```
  308. **/
  309. function deflate(input, options) {
  310. var deflator = new Deflate(options);
  311. deflator.push(input, true);
  312. // That will never happens, if you don't cheat with options :)
  313. if (deflator.err) { throw deflator.msg; }
  314. return deflator.result;
  315. }
  316. /**
  317. * deflateRaw(data[, options]) -> Uint8Array|Array|String
  318. * - data (Uint8Array|Array|String): input data to compress.
  319. * - options (Object): zlib deflate options.
  320. *
  321. * The same as [[deflate]], but creates raw data, without wrapper
  322. * (header and adler32 crc).
  323. **/
  324. function deflateRaw(input, options) {
  325. options = options || {};
  326. options.raw = true;
  327. return deflate(input, options);
  328. }
  329. /**
  330. * gzip(data[, options]) -> Uint8Array|Array|String
  331. * - data (Uint8Array|Array|String): input data to compress.
  332. * - options (Object): zlib deflate options.
  333. *
  334. * The same as [[deflate]], but create gzip wrapper instead of
  335. * deflate one.
  336. **/
  337. function gzip(input, options) {
  338. options = options || {};
  339. options.gzip = true;
  340. return deflate(input, options);
  341. }
  342. exports.Deflate = Deflate;
  343. exports.deflate = deflate;
  344. exports.deflateRaw = deflateRaw;
  345. exports.gzip = gzip;
  346. },{"./utils/common":3,"./utils/strings":4,"./zlib/deflate":8,"./zlib/messages":13,"./zlib/zstream":15}],2:[function(require,module,exports){
  347. 'use strict';
  348. var zlib_inflate = require('./zlib/inflate');
  349. var utils = require('./utils/common');
  350. var strings = require('./utils/strings');
  351. var c = require('./zlib/constants');
  352. var msg = require('./zlib/messages');
  353. var ZStream = require('./zlib/zstream');
  354. var GZheader = require('./zlib/gzheader');
  355. var toString = Object.prototype.toString;
  356. /**
  357. * class Inflate
  358. *
  359. * Generic JS-style wrapper for zlib calls. If you don't need
  360. * streaming behaviour - use more simple functions: [[inflate]]
  361. * and [[inflateRaw]].
  362. **/
  363. /* internal
  364. * inflate.chunks -> Array
  365. *
  366. * Chunks of output data, if [[Inflate#onData]] not overriden.
  367. **/
  368. /**
  369. * Inflate.result -> Uint8Array|Array|String
  370. *
  371. * Uncompressed result, generated by default [[Inflate#onData]]
  372. * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
  373. * (call [[Inflate#push]] with `Z_FINISH` / `true` param) or if you
  374. * push a chunk with explicit flush (call [[Inflate#push]] with
  375. * `Z_SYNC_FLUSH` param).
  376. **/
  377. /**
  378. * Inflate.err -> Number
  379. *
  380. * Error code after inflate finished. 0 (Z_OK) on success.
  381. * Should be checked if broken data possible.
  382. **/
  383. /**
  384. * Inflate.msg -> String
  385. *
  386. * Error message, if [[Inflate.err]] != 0
  387. **/
  388. /**
  389. * new Inflate(options)
  390. * - options (Object): zlib inflate options.
  391. *
  392. * Creates new inflator instance with specified params. Throws exception
  393. * on bad params. Supported options:
  394. *
  395. * - `windowBits`
  396. * - `dictionary`
  397. *
  398. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  399. * for more information on these.
  400. *
  401. * Additional options, for internal needs:
  402. *
  403. * - `chunkSize` - size of generated data chunks (16K by default)
  404. * - `raw` (Boolean) - do raw inflate
  405. * - `to` (String) - if equal to 'string', then result will be converted
  406. * from utf8 to utf16 (javascript) string. When string output requested,
  407. * chunk length can differ from `chunkSize`, depending on content.
  408. *
  409. * By default, when no options set, autodetect deflate/gzip data format via
  410. * wrapper header.
  411. *
  412. * ##### Example:
  413. *
  414. * ```javascript
  415. * var pako = require('pako')
  416. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  417. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  418. *
  419. * var inflate = new pako.Inflate({ level: 3});
  420. *
  421. * inflate.push(chunk1, false);
  422. * inflate.push(chunk2, true); // true -> last chunk
  423. *
  424. * if (inflate.err) { throw new Error(inflate.err); }
  425. *
  426. * console.log(inflate.result);
  427. * ```
  428. **/
  429. function Inflate(options) {
  430. if (!(this instanceof Inflate)) return new Inflate(options);
  431. this.options = utils.assign({
  432. chunkSize: 16384,
  433. windowBits: 0,
  434. to: ''
  435. }, options || {});
  436. var opt = this.options;
  437. // Force window size for `raw` data, if not set directly,
  438. // because we have no header for autodetect.
  439. if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
  440. opt.windowBits = -opt.windowBits;
  441. if (opt.windowBits === 0) { opt.windowBits = -15; }
  442. }
  443. // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
  444. if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
  445. !(options && options.windowBits)) {
  446. opt.windowBits += 32;
  447. }
  448. // Gzip header has no info about windows size, we can do autodetect only
  449. // for deflate. So, if window size not set, force it to max when gzip possible
  450. if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
  451. // bit 3 (16) -> gzipped data
  452. // bit 4 (32) -> autodetect gzip/deflate
  453. if ((opt.windowBits & 15) === 0) {
  454. opt.windowBits |= 15;
  455. }
  456. }
  457. this.err = 0; // error code, if happens (0 = Z_OK)
  458. this.msg = ''; // error message
  459. this.ended = false; // used to avoid multiple onEnd() calls
  460. this.chunks = []; // chunks of compressed data
  461. this.strm = new ZStream();
  462. this.strm.avail_out = 0;
  463. var status = zlib_inflate.inflateInit2(
  464. this.strm,
  465. opt.windowBits
  466. );
  467. if (status !== c.Z_OK) {
  468. throw new Error(msg[status]);
  469. }
  470. this.header = new GZheader();
  471. zlib_inflate.inflateGetHeader(this.strm, this.header);
  472. }
  473. /**
  474. * Inflate#push(data[, mode]) -> Boolean
  475. * - data (Uint8Array|Array|ArrayBuffer|String): input data
  476. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  477. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
  478. *
  479. * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
  480. * new output chunks. Returns `true` on success. The last data block must have
  481. * mode Z_FINISH (or `true`). That will flush internal pending buffers and call
  482. * [[Inflate#onEnd]]. For interim explicit flushes (without ending the stream) you
  483. * can use mode Z_SYNC_FLUSH, keeping the decompression context.
  484. *
  485. * On fail call [[Inflate#onEnd]] with error code and return false.
  486. *
  487. * We strongly recommend to use `Uint8Array` on input for best speed (output
  488. * format is detected automatically). Also, don't skip last param and always
  489. * use the same type in your code (boolean or number). That will improve JS speed.
  490. *
  491. * For regular `Array`-s make sure all elements are [0..255].
  492. *
  493. * ##### Example
  494. *
  495. * ```javascript
  496. * push(chunk, false); // push one of data chunks
  497. * ...
  498. * push(chunk, true); // push last chunk
  499. * ```
  500. **/
  501. Inflate.prototype.push = function (data, mode) {
  502. var strm = this.strm;
  503. var chunkSize = this.options.chunkSize;
  504. var dictionary = this.options.dictionary;
  505. var status, _mode;
  506. var next_out_utf8, tail, utf8str;
  507. var dict;
  508. // Flag to properly process Z_BUF_ERROR on testing inflate call
  509. // when we check that all output data was flushed.
  510. var allowBufError = false;
  511. if (this.ended) { return false; }
  512. _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
  513. // Convert data if needed
  514. if (typeof data === 'string') {
  515. // Only binary strings can be decompressed on practice
  516. strm.input = strings.binstring2buf(data);
  517. } else if (toString.call(data) === '[object ArrayBuffer]') {
  518. strm.input = new Uint8Array(data);
  519. } else {
  520. strm.input = data;
  521. }
  522. strm.next_in = 0;
  523. strm.avail_in = strm.input.length;
  524. do {
  525. if (strm.avail_out === 0) {
  526. strm.output = new utils.Buf8(chunkSize);
  527. strm.next_out = 0;
  528. strm.avail_out = chunkSize;
  529. }
  530. status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
  531. if (status === c.Z_NEED_DICT && dictionary) {
  532. // Convert data if needed
  533. if (typeof dictionary === 'string') {
  534. dict = strings.string2buf(dictionary);
  535. } else if (toString.call(dictionary) === '[object ArrayBuffer]') {
  536. dict = new Uint8Array(dictionary);
  537. } else {
  538. dict = dictionary;
  539. }
  540. status = zlib_inflate.inflateSetDictionary(this.strm, dict);
  541. }
  542. if (status === c.Z_BUF_ERROR && allowBufError === true) {
  543. status = c.Z_OK;
  544. allowBufError = false;
  545. }
  546. if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
  547. this.onEnd(status);
  548. this.ended = true;
  549. return false;
  550. }
  551. if (strm.next_out) {
  552. if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && (_mode === c.Z_FINISH || _mode === c.Z_SYNC_FLUSH))) {
  553. if (this.options.to === 'string') {
  554. next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
  555. tail = strm.next_out - next_out_utf8;
  556. utf8str = strings.buf2string(strm.output, next_out_utf8);
  557. // move tail
  558. strm.next_out = tail;
  559. strm.avail_out = chunkSize - tail;
  560. if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
  561. this.onData(utf8str);
  562. } else {
  563. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  564. }
  565. }
  566. }
  567. // When no more input data, we should check that internal inflate buffers
  568. // are flushed. The only way to do it when avail_out = 0 - run one more
  569. // inflate pass. But if output data not exists, inflate return Z_BUF_ERROR.
  570. // Here we set flag to process this error properly.
  571. //
  572. // NOTE. Deflate does not return error in this case and does not needs such
  573. // logic.
  574. if (strm.avail_in === 0 && strm.avail_out === 0) {
  575. allowBufError = true;
  576. }
  577. } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== c.Z_STREAM_END);
  578. if (status === c.Z_STREAM_END) {
  579. _mode = c.Z_FINISH;
  580. }
  581. // Finalize on the last chunk.
  582. if (_mode === c.Z_FINISH) {
  583. status = zlib_inflate.inflateEnd(this.strm);
  584. this.onEnd(status);
  585. this.ended = true;
  586. return status === c.Z_OK;
  587. }
  588. // callback interim results if Z_SYNC_FLUSH.
  589. if (_mode === c.Z_SYNC_FLUSH) {
  590. this.onEnd(c.Z_OK);
  591. strm.avail_out = 0;
  592. return true;
  593. }
  594. return true;
  595. };
  596. /**
  597. * Inflate#onData(chunk) -> Void
  598. * - chunk (Uint8Array|Array|String): ouput data. Type of array depends
  599. * on js engine support. When string output requested, each chunk
  600. * will be string.
  601. *
  602. * By default, stores data blocks in `chunks[]` property and glue
  603. * those in `onEnd`. Override this handler, if you need another behaviour.
  604. **/
  605. Inflate.prototype.onData = function (chunk) {
  606. this.chunks.push(chunk);
  607. };
  608. /**
  609. * Inflate#onEnd(status) -> Void
  610. * - status (Number): inflate status. 0 (Z_OK) on success,
  611. * other if not.
  612. *
  613. * Called either after you tell inflate that the input stream is
  614. * complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
  615. * or if an error happened. By default - join collected chunks,
  616. * free memory and fill `results` / `err` properties.
  617. **/
  618. Inflate.prototype.onEnd = function (status) {
  619. // On success - join
  620. if (status === c.Z_OK) {
  621. if (this.options.to === 'string') {
  622. // Glue & convert here, until we teach pako to send
  623. // utf8 alligned strings to onData
  624. this.result = this.chunks.join('');
  625. } else {
  626. this.result = utils.flattenChunks(this.chunks);
  627. }
  628. }
  629. this.chunks = [];
  630. this.err = status;
  631. this.msg = this.strm.msg;
  632. };
  633. /**
  634. * inflate(data[, options]) -> Uint8Array|Array|String
  635. * - data (Uint8Array|Array|String): input data to decompress.
  636. * - options (Object): zlib inflate options.
  637. *
  638. * Decompress `data` with inflate/ungzip and `options`. Autodetect
  639. * format via wrapper header by default. That's why we don't provide
  640. * separate `ungzip` method.
  641. *
  642. * Supported options are:
  643. *
  644. * - windowBits
  645. *
  646. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  647. * for more information.
  648. *
  649. * Sugar (options):
  650. *
  651. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  652. * negative windowBits implicitly.
  653. * - `to` (String) - if equal to 'string', then result will be converted
  654. * from utf8 to utf16 (javascript) string. When string output requested,
  655. * chunk length can differ from `chunkSize`, depending on content.
  656. *
  657. *
  658. * ##### Example:
  659. *
  660. * ```javascript
  661. * var pako = require('pako')
  662. * , input = pako.deflate([1,2,3,4,5,6,7,8,9])
  663. * , output;
  664. *
  665. * try {
  666. * output = pako.inflate(input);
  667. * } catch (err)
  668. * console.log(err);
  669. * }
  670. * ```
  671. **/
  672. function inflate(input, options) {
  673. var inflator = new Inflate(options);
  674. inflator.push(input, true);
  675. // That will never happens, if you don't cheat with options :)
  676. if (inflator.err) { throw inflator.msg; }
  677. return inflator.result;
  678. }
  679. /**
  680. * inflateRaw(data[, options]) -> Uint8Array|Array|String
  681. * - data (Uint8Array|Array|String): input data to decompress.
  682. * - options (Object): zlib inflate options.
  683. *
  684. * The same as [[inflate]], but creates raw data, without wrapper
  685. * (header and adler32 crc).
  686. **/
  687. function inflateRaw(input, options) {
  688. options = options || {};
  689. options.raw = true;
  690. return inflate(input, options);
  691. }
  692. /**
  693. * ungzip(data[, options]) -> Uint8Array|Array|String
  694. * - data (Uint8Array|Array|String): input data to decompress.
  695. * - options (Object): zlib inflate options.
  696. *
  697. * Just shortcut to [[inflate]], because it autodetects format
  698. * by header.content. Done for convenience.
  699. **/
  700. exports.Inflate = Inflate;
  701. exports.inflate = inflate;
  702. exports.inflateRaw = inflateRaw;
  703. exports.ungzip = inflate;
  704. },{"./utils/common":3,"./utils/strings":4,"./zlib/constants":6,"./zlib/gzheader":9,"./zlib/inflate":11,"./zlib/messages":13,"./zlib/zstream":15}],3:[function(require,module,exports){
  705. 'use strict';
  706. var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
  707. (typeof Uint16Array !== 'undefined') &&
  708. (typeof Int32Array !== 'undefined');
  709. exports.assign = function (obj /*from1, from2, from3, ...*/) {
  710. var sources = Array.prototype.slice.call(arguments, 1);
  711. while (sources.length) {
  712. var source = sources.shift();
  713. if (!source) { continue; }
  714. if (typeof source !== 'object') {
  715. throw new TypeError(source + 'must be non-object');
  716. }
  717. for (var p in source) {
  718. if (source.hasOwnProperty(p)) {
  719. obj[p] = source[p];
  720. }
  721. }
  722. }
  723. return obj;
  724. };
  725. // reduce buffer size, avoiding mem copy
  726. exports.shrinkBuf = function (buf, size) {
  727. if (buf.length === size) { return buf; }
  728. if (buf.subarray) { return buf.subarray(0, size); }
  729. buf.length = size;
  730. return buf;
  731. };
  732. var fnTyped = {
  733. arraySet: function (dest, src, src_offs, len, dest_offs) {
  734. if (src.subarray && dest.subarray) {
  735. dest.set(src.subarray(src_offs, src_offs + len), dest_offs);
  736. return;
  737. }
  738. // Fallback to ordinary array
  739. for (var i = 0; i < len; i++) {
  740. dest[dest_offs + i] = src[src_offs + i];
  741. }
  742. },
  743. // Join array of chunks to single array.
  744. flattenChunks: function (chunks) {
  745. var i, l, len, pos, chunk, result;
  746. // calculate data length
  747. len = 0;
  748. for (i = 0, l = chunks.length; i < l; i++) {
  749. len += chunks[i].length;
  750. }
  751. // join chunks
  752. result = new Uint8Array(len);
  753. pos = 0;
  754. for (i = 0, l = chunks.length; i < l; i++) {
  755. chunk = chunks[i];
  756. result.set(chunk, pos);
  757. pos += chunk.length;
  758. }
  759. return result;
  760. }
  761. };
  762. var fnUntyped = {
  763. arraySet: function (dest, src, src_offs, len, dest_offs) {
  764. for (var i = 0; i < len; i++) {
  765. dest[dest_offs + i] = src[src_offs + i];
  766. }
  767. },
  768. // Join array of chunks to single array.
  769. flattenChunks: function (chunks) {
  770. return [].concat.apply([], chunks);
  771. }
  772. };
  773. // Enable/Disable typed arrays use, for testing
  774. //
  775. exports.setTyped = function (on) {
  776. if (on) {
  777. exports.Buf8 = Uint8Array;
  778. exports.Buf16 = Uint16Array;
  779. exports.Buf32 = Int32Array;
  780. exports.assign(exports, fnTyped);
  781. } else {
  782. exports.Buf8 = Array;
  783. exports.Buf16 = Array;
  784. exports.Buf32 = Array;
  785. exports.assign(exports, fnUntyped);
  786. }
  787. };
  788. exports.setTyped(TYPED_OK);
  789. },{}],4:[function(require,module,exports){
  790. // String encode/decode helpers
  791. 'use strict';
  792. var utils = require('./common');
  793. // Quick check if we can use fast array to bin string conversion
  794. //
  795. // - apply(Array) can fail on Android 2.2
  796. // - apply(Uint8Array) can fail on iOS 5.1 Safary
  797. //
  798. var STR_APPLY_OK = true;
  799. var STR_APPLY_UIA_OK = true;
  800. try { String.fromCharCode.apply(null, [ 0 ]); } catch (__) { STR_APPLY_OK = false; }
  801. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
  802. // Table with utf8 lengths (calculated by first byte of sequence)
  803. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  804. // because max possible codepoint is 0x10ffff
  805. var _utf8len = new utils.Buf8(256);
  806. for (var q = 0; q < 256; q++) {
  807. _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
  808. }
  809. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  810. // convert string to array (typed, when possible)
  811. exports.string2buf = function (str) {
  812. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  813. // count binary size
  814. for (m_pos = 0; m_pos < str_len; m_pos++) {
  815. c = str.charCodeAt(m_pos);
  816. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  817. c2 = str.charCodeAt(m_pos + 1);
  818. if ((c2 & 0xfc00) === 0xdc00) {
  819. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  820. m_pos++;
  821. }
  822. }
  823. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  824. }
  825. // allocate buffer
  826. buf = new utils.Buf8(buf_len);
  827. // convert
  828. for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
  829. c = str.charCodeAt(m_pos);
  830. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  831. c2 = str.charCodeAt(m_pos + 1);
  832. if ((c2 & 0xfc00) === 0xdc00) {
  833. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  834. m_pos++;
  835. }
  836. }
  837. if (c < 0x80) {
  838. /* one byte */
  839. buf[i++] = c;
  840. } else if (c < 0x800) {
  841. /* two bytes */
  842. buf[i++] = 0xC0 | (c >>> 6);
  843. buf[i++] = 0x80 | (c & 0x3f);
  844. } else if (c < 0x10000) {
  845. /* three bytes */
  846. buf[i++] = 0xE0 | (c >>> 12);
  847. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  848. buf[i++] = 0x80 | (c & 0x3f);
  849. } else {
  850. /* four bytes */
  851. buf[i++] = 0xf0 | (c >>> 18);
  852. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  853. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  854. buf[i++] = 0x80 | (c & 0x3f);
  855. }
  856. }
  857. return buf;
  858. };
  859. // Helper (used in 2 places)
  860. function buf2binstring(buf, len) {
  861. // use fallback for big arrays to avoid stack overflow
  862. if (len < 65537) {
  863. if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
  864. return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
  865. }
  866. }
  867. var result = '';
  868. for (var i = 0; i < len; i++) {
  869. result += String.fromCharCode(buf[i]);
  870. }
  871. return result;
  872. }
  873. // Convert byte array to binary string
  874. exports.buf2binstring = function (buf) {
  875. return buf2binstring(buf, buf.length);
  876. };
  877. // Convert binary string (typed, when possible)
  878. exports.binstring2buf = function (str) {
  879. var buf = new utils.Buf8(str.length);
  880. for (var i = 0, len = buf.length; i < len; i++) {
  881. buf[i] = str.charCodeAt(i);
  882. }
  883. return buf;
  884. };
  885. // convert array to string
  886. exports.buf2string = function (buf, max) {
  887. var i, out, c, c_len;
  888. var len = max || buf.length;
  889. // Reserve max possible length (2 words per char)
  890. // NB: by unknown reasons, Array is significantly faster for
  891. // String.fromCharCode.apply than Uint16Array.
  892. var utf16buf = new Array(len * 2);
  893. for (out = 0, i = 0; i < len;) {
  894. c = buf[i++];
  895. // quick process ascii
  896. if (c < 0x80) { utf16buf[out++] = c; continue; }
  897. c_len = _utf8len[c];
  898. // skip 5 & 6 byte codes
  899. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
  900. // apply mask on first byte
  901. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  902. // join the rest
  903. while (c_len > 1 && i < len) {
  904. c = (c << 6) | (buf[i++] & 0x3f);
  905. c_len--;
  906. }
  907. // terminated by end of string?
  908. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  909. if (c < 0x10000) {
  910. utf16buf[out++] = c;
  911. } else {
  912. c -= 0x10000;
  913. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  914. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  915. }
  916. }
  917. return buf2binstring(utf16buf, out);
  918. };
  919. // Calculate max possible position in utf8 buffer,
  920. // that will not break sequence. If that's not possible
  921. // - (very small limits) return max size as is.
  922. //
  923. // buf[] - utf8 bytes array
  924. // max - length limit (mandatory);
  925. exports.utf8border = function (buf, max) {
  926. var pos;
  927. max = max || buf.length;
  928. if (max > buf.length) { max = buf.length; }
  929. // go back from last position, until start of sequence found
  930. pos = max - 1;
  931. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  932. // Fuckup - very small and broken sequence,
  933. // return max, because we should return something anyway.
  934. if (pos < 0) { return max; }
  935. // If we came to start of buffer - that means vuffer is too small,
  936. // return max too.
  937. if (pos === 0) { return max; }
  938. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  939. };
  940. },{"./common":3}],5:[function(require,module,exports){
  941. 'use strict';
  942. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  943. // It doesn't worth to make additional optimizationa as in original.
  944. // Small size is preferable.
  945. function adler32(adler, buf, len, pos) {
  946. var s1 = (adler & 0xffff) |0,
  947. s2 = ((adler >>> 16) & 0xffff) |0,
  948. n = 0;
  949. while (len !== 0) {
  950. // Set limit ~ twice less than 5552, to keep
  951. // s2 in 31-bits, because we force signed ints.
  952. // in other case %= will fail.
  953. n = len > 2000 ? 2000 : len;
  954. len -= n;
  955. do {
  956. s1 = (s1 + buf[pos++]) |0;
  957. s2 = (s2 + s1) |0;
  958. } while (--n);
  959. s1 %= 65521;
  960. s2 %= 65521;
  961. }
  962. return (s1 | (s2 << 16)) |0;
  963. }
  964. module.exports = adler32;
  965. },{}],6:[function(require,module,exports){
  966. 'use strict';
  967. module.exports = {
  968. /* Allowed flush values; see deflate() and inflate() below for details */
  969. Z_NO_FLUSH: 0,
  970. Z_PARTIAL_FLUSH: 1,
  971. Z_SYNC_FLUSH: 2,
  972. Z_FULL_FLUSH: 3,
  973. Z_FINISH: 4,
  974. Z_BLOCK: 5,
  975. Z_TREES: 6,
  976. /* Return codes for the compression/decompression functions. Negative values
  977. * are errors, positive values are used for special but normal events.
  978. */
  979. Z_OK: 0,
  980. Z_STREAM_END: 1,
  981. Z_NEED_DICT: 2,
  982. Z_ERRNO: -1,
  983. Z_STREAM_ERROR: -2,
  984. Z_DATA_ERROR: -3,
  985. //Z_MEM_ERROR: -4,
  986. Z_BUF_ERROR: -5,
  987. //Z_VERSION_ERROR: -6,
  988. /* compression levels */
  989. Z_NO_COMPRESSION: 0,
  990. Z_BEST_SPEED: 1,
  991. Z_BEST_COMPRESSION: 9,
  992. Z_DEFAULT_COMPRESSION: -1,
  993. Z_FILTERED: 1,
  994. Z_HUFFMAN_ONLY: 2,
  995. Z_RLE: 3,
  996. Z_FIXED: 4,
  997. Z_DEFAULT_STRATEGY: 0,
  998. /* Possible values of the data_type field (though see inflate()) */
  999. Z_BINARY: 0,
  1000. Z_TEXT: 1,
  1001. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  1002. Z_UNKNOWN: 2,
  1003. /* The deflate compression method */
  1004. Z_DEFLATED: 8
  1005. //Z_NULL: null // Use -1 or null inline, depending on var type
  1006. };
  1007. },{}],7:[function(require,module,exports){
  1008. 'use strict';
  1009. // Note: we can't get significant speed boost here.
  1010. // So write code to minimize size - no pregenerated tables
  1011. // and array tools dependencies.
  1012. // Use ordinary array, since untyped makes no boost here
  1013. function makeTable() {
  1014. var c, table = [];
  1015. for (var n = 0; n < 256; n++) {
  1016. c = n;
  1017. for (var k = 0; k < 8; k++) {
  1018. c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  1019. }
  1020. table[n] = c;
  1021. }
  1022. return table;
  1023. }
  1024. // Create table on load. Just 255 signed longs. Not a problem.
  1025. var crcTable = makeTable();
  1026. function crc32(crc, buf, len, pos) {
  1027. var t = crcTable,
  1028. end = pos + len;
  1029. crc ^= -1;
  1030. for (var i = pos; i < end; i++) {
  1031. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  1032. }
  1033. return (crc ^ (-1)); // >>> 0;
  1034. }
  1035. module.exports = crc32;
  1036. },{}],8:[function(require,module,exports){
  1037. 'use strict';
  1038. var utils = require('../utils/common');
  1039. var trees = require('./trees');
  1040. var adler32 = require('./adler32');
  1041. var crc32 = require('./crc32');
  1042. var msg = require('./messages');
  1043. /* Public constants ==========================================================*/
  1044. /* ===========================================================================*/
  1045. /* Allowed flush values; see deflate() and inflate() below for details */
  1046. var Z_NO_FLUSH = 0;
  1047. var Z_PARTIAL_FLUSH = 1;
  1048. //var Z_SYNC_FLUSH = 2;
  1049. var Z_FULL_FLUSH = 3;
  1050. var Z_FINISH = 4;
  1051. var Z_BLOCK = 5;
  1052. //var Z_TREES = 6;
  1053. /* Return codes for the compression/decompression functions. Negative values
  1054. * are errors, positive values are used for special but normal events.
  1055. */
  1056. var Z_OK = 0;
  1057. var Z_STREAM_END = 1;
  1058. //var Z_NEED_DICT = 2;
  1059. //var Z_ERRNO = -1;
  1060. var Z_STREAM_ERROR = -2;
  1061. var Z_DATA_ERROR = -3;
  1062. //var Z_MEM_ERROR = -4;
  1063. var Z_BUF_ERROR = -5;
  1064. //var Z_VERSION_ERROR = -6;
  1065. /* compression levels */
  1066. //var Z_NO_COMPRESSION = 0;
  1067. //var Z_BEST_SPEED = 1;
  1068. //var Z_BEST_COMPRESSION = 9;
  1069. var Z_DEFAULT_COMPRESSION = -1;
  1070. var Z_FILTERED = 1;
  1071. var Z_HUFFMAN_ONLY = 2;
  1072. var Z_RLE = 3;
  1073. var Z_FIXED = 4;
  1074. var Z_DEFAULT_STRATEGY = 0;
  1075. /* Possible values of the data_type field (though see inflate()) */
  1076. //var Z_BINARY = 0;
  1077. //var Z_TEXT = 1;
  1078. //var Z_ASCII = 1; // = Z_TEXT
  1079. var Z_UNKNOWN = 2;
  1080. /* The deflate compression method */
  1081. var Z_DEFLATED = 8;
  1082. /*============================================================================*/
  1083. var MAX_MEM_LEVEL = 9;
  1084. /* Maximum value for memLevel in deflateInit2 */
  1085. var MAX_WBITS = 15;
  1086. /* 32K LZ77 window */
  1087. var DEF_MEM_LEVEL = 8;
  1088. var LENGTH_CODES = 29;
  1089. /* number of length codes, not counting the special END_BLOCK code */
  1090. var LITERALS = 256;
  1091. /* number of literal bytes 0..255 */
  1092. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  1093. /* number of Literal or Length codes, including the END_BLOCK code */
  1094. var D_CODES = 30;
  1095. /* number of distance codes */
  1096. var BL_CODES = 19;
  1097. /* number of codes used to transfer the bit lengths */
  1098. var HEAP_SIZE = 2 * L_CODES + 1;
  1099. /* maximum heap size */
  1100. var MAX_BITS = 15;
  1101. /* All codes must not exceed MAX_BITS bits */
  1102. var MIN_MATCH = 3;
  1103. var MAX_MATCH = 258;
  1104. var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  1105. var PRESET_DICT = 0x20;
  1106. var INIT_STATE = 42;
  1107. var EXTRA_STATE = 69;
  1108. var NAME_STATE = 73;
  1109. var COMMENT_STATE = 91;
  1110. var HCRC_STATE = 103;
  1111. var BUSY_STATE = 113;
  1112. var FINISH_STATE = 666;
  1113. var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
  1114. var BS_BLOCK_DONE = 2; /* block flush performed */
  1115. var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
  1116. var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
  1117. var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
  1118. function err(strm, errorCode) {
  1119. strm.msg = msg[errorCode];
  1120. return errorCode;
  1121. }
  1122. function rank(f) {
  1123. return ((f) << 1) - ((f) > 4 ? 9 : 0);
  1124. }
  1125. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  1126. /* =========================================================================
  1127. * Flush as much pending output as possible. All deflate() output goes
  1128. * through this function so some applications may wish to modify it
  1129. * to avoid allocating a large strm->output buffer and copying into it.
  1130. * (See also read_buf()).
  1131. */
  1132. function flush_pending(strm) {
  1133. var s = strm.state;
  1134. //_tr_flush_bits(s);
  1135. var len = s.pending;
  1136. if (len > strm.avail_out) {
  1137. len = strm.avail_out;
  1138. }
  1139. if (len === 0) { return; }
  1140. utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
  1141. strm.next_out += len;
  1142. s.pending_out += len;
  1143. strm.total_out += len;
  1144. strm.avail_out -= len;
  1145. s.pending -= len;
  1146. if (s.pending === 0) {
  1147. s.pending_out = 0;
  1148. }
  1149. }
  1150. function flush_block_only(s, last) {
  1151. trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
  1152. s.block_start = s.strstart;
  1153. flush_pending(s.strm);
  1154. }
  1155. function put_byte(s, b) {
  1156. s.pending_buf[s.pending++] = b;
  1157. }
  1158. /* =========================================================================
  1159. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  1160. * IN assertion: the stream state is correct and there is enough room in
  1161. * pending_buf.
  1162. */
  1163. function putShortMSB(s, b) {
  1164. // put_byte(s, (Byte)(b >> 8));
  1165. // put_byte(s, (Byte)(b & 0xff));
  1166. s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
  1167. s.pending_buf[s.pending++] = b & 0xff;
  1168. }
  1169. /* ===========================================================================
  1170. * Read a new buffer from the current input stream, update the adler32
  1171. * and total number of bytes read. All deflate() input goes through
  1172. * this function so some applications may wish to modify it to avoid
  1173. * allocating a large strm->input buffer and copying from it.
  1174. * (See also flush_pending()).
  1175. */
  1176. function read_buf(strm, buf, start, size) {
  1177. var len = strm.avail_in;
  1178. if (len > size) { len = size; }
  1179. if (len === 0) { return 0; }
  1180. strm.avail_in -= len;
  1181. // zmemcpy(buf, strm->next_in, len);
  1182. utils.arraySet(buf, strm.input, strm.next_in, len, start);
  1183. if (strm.state.wrap === 1) {
  1184. strm.adler = adler32(strm.adler, buf, len, start);
  1185. }
  1186. else if (strm.state.wrap === 2) {
  1187. strm.adler = crc32(strm.adler, buf, len, start);
  1188. }
  1189. strm.next_in += len;
  1190. strm.total_in += len;
  1191. return len;
  1192. }
  1193. /* ===========================================================================
  1194. * Set match_start to the longest match starting at the given string and
  1195. * return its length. Matches shorter or equal to prev_length are discarded,
  1196. * in which case the result is equal to prev_length and match_start is
  1197. * garbage.
  1198. * IN assertions: cur_match is the head of the hash chain for the current
  1199. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1200. * OUT assertion: the match length is not greater than s->lookahead.
  1201. */
  1202. function longest_match(s, cur_match) {
  1203. var chain_length = s.max_chain_length; /* max hash chain length */
  1204. var scan = s.strstart; /* current string */
  1205. var match; /* matched string */
  1206. var len; /* length of current match */
  1207. var best_len = s.prev_length; /* best match length so far */
  1208. var nice_match = s.nice_match; /* stop if match long enough */
  1209. var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
  1210. s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
  1211. var _win = s.window; // shortcut
  1212. var wmask = s.w_mask;
  1213. var prev = s.prev;
  1214. /* Stop when cur_match becomes <= limit. To simplify the code,
  1215. * we prevent matches with the string of window index 0.
  1216. */
  1217. var strend = s.strstart + MAX_MATCH;
  1218. var scan_end1 = _win[scan + best_len - 1];
  1219. var scan_end = _win[scan + best_len];
  1220. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1221. * It is easy to get rid of this optimization if necessary.
  1222. */
  1223. // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  1224. /* Do not waste too much time if we already have a good match: */
  1225. if (s.prev_length >= s.good_match) {
  1226. chain_length >>= 2;
  1227. }
  1228. /* Do not look for matches beyond the end of the input. This is necessary
  1229. * to make deflate deterministic.
  1230. */
  1231. if (nice_match > s.lookahead) { nice_match = s.lookahead; }
  1232. // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  1233. do {
  1234. // Assert(cur_match < s->strstart, "no future");
  1235. match = cur_match;
  1236. /* Skip to next match if the match length cannot increase
  1237. * or if the match length is less than 2. Note that the checks below
  1238. * for insufficient lookahead only occur occasionally for performance
  1239. * reasons. Therefore uninitialized memory will be accessed, and
  1240. * conditional jumps will be made that depend on those values.
  1241. * However the length of the match is limited to the lookahead, so
  1242. * the output of deflate is not affected by the uninitialized values.
  1243. */
  1244. if (_win[match + best_len] !== scan_end ||
  1245. _win[match + best_len - 1] !== scan_end1 ||
  1246. _win[match] !== _win[scan] ||
  1247. _win[++match] !== _win[scan + 1]) {
  1248. continue;
  1249. }
  1250. /* The check at best_len-1 can be removed because it will be made
  1251. * again later. (This heuristic is not always a win.)
  1252. * It is not necessary to compare scan[2] and match[2] since they
  1253. * are always equal when the other bytes match, given that
  1254. * the hash keys are equal and that HASH_BITS >= 8.
  1255. */
  1256. scan += 2;
  1257. match++;
  1258. // Assert(*scan == *match, "match[2]?");
  1259. /* We check for insufficient lookahead only every 8th comparison;
  1260. * the 256th check will be made at strstart+258.
  1261. */
  1262. do {
  1263. /*jshint noempty:false*/
  1264. } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1265. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1266. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1267. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1268. scan < strend);
  1269. // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  1270. len = MAX_MATCH - (strend - scan);
  1271. scan = strend - MAX_MATCH;
  1272. if (len > best_len) {
  1273. s.match_start = cur_match;
  1274. best_len = len;
  1275. if (len >= nice_match) {
  1276. break;
  1277. }
  1278. scan_end1 = _win[scan + best_len - 1];
  1279. scan_end = _win[scan + best_len];
  1280. }
  1281. } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
  1282. if (best_len <= s.lookahead) {
  1283. return best_len;
  1284. }
  1285. return s.lookahead;
  1286. }
  1287. /* ===========================================================================
  1288. * Fill the window when the lookahead becomes insufficient.
  1289. * Updates strstart and lookahead.
  1290. *
  1291. * IN assertion: lookahead < MIN_LOOKAHEAD
  1292. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1293. * At least one byte has been read, or avail_in == 0; reads are
  1294. * performed for at least two bytes (required for the zip translate_eol
  1295. * option -- not supported here).
  1296. */
  1297. function fill_window(s) {
  1298. var _w_size = s.w_size;
  1299. var p, n, m, more, str;
  1300. //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  1301. do {
  1302. more = s.window_size - s.lookahead - s.strstart;
  1303. // JS ints have 32 bit, block below not needed
  1304. /* Deal with !@#$% 64K limit: */
  1305. //if (sizeof(int) <= 2) {
  1306. // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  1307. // more = wsize;
  1308. //
  1309. // } else if (more == (unsigned)(-1)) {
  1310. // /* Very unlikely, but possible on 16 bit machine if
  1311. // * strstart == 0 && lookahead == 1 (input done a byte at time)
  1312. // */
  1313. // more--;
  1314. // }
  1315. //}
  1316. /* If the window is almost full and there is insufficient lookahead,
  1317. * move the upper half to the lower one to make room in the upper half.
  1318. */
  1319. if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
  1320. utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
  1321. s.match_start -= _w_size;
  1322. s.strstart -= _w_size;
  1323. /* we now have strstart >= MAX_DIST */
  1324. s.block_start -= _w_size;
  1325. /* Slide the hash table (could be avoided with 32 bit values
  1326. at the expense of memory usage). We slide even when level == 0
  1327. to keep the hash table consistent if we switch back to level > 0
  1328. later. (Using level 0 permanently is not an optimal usage of
  1329. zlib, so we don't care about this pathological case.)
  1330. */
  1331. n = s.hash_size;
  1332. p = n;
  1333. do {
  1334. m = s.head[--p];
  1335. s.head[p] = (m >= _w_size ? m - _w_size : 0);
  1336. } while (--n);
  1337. n = _w_size;
  1338. p = n;
  1339. do {
  1340. m = s.prev[--p];
  1341. s.prev[p] = (m >= _w_size ? m - _w_size : 0);
  1342. /* If n is not on any hash chain, prev[n] is garbage but
  1343. * its value will never be used.
  1344. */
  1345. } while (--n);
  1346. more += _w_size;
  1347. }
  1348. if (s.strm.avail_in === 0) {
  1349. break;
  1350. }
  1351. /* If there was no sliding:
  1352. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1353. * more == window_size - lookahead - strstart
  1354. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1355. * => more >= window_size - 2*WSIZE + 2
  1356. * In the BIG_MEM or MMAP case (not yet supported),
  1357. * window_size == input_size + MIN_LOOKAHEAD &&
  1358. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1359. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1360. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1361. */
  1362. //Assert(more >= 2, "more < 2");
  1363. n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
  1364. s.lookahead += n;
  1365. /* Initialize the hash value now that we have some input: */
  1366. if (s.lookahead + s.insert >= MIN_MATCH) {
  1367. str = s.strstart - s.insert;
  1368. s.ins_h = s.window[str];
  1369. /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
  1370. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
  1371. //#if MIN_MATCH != 3
  1372. // Call update_hash() MIN_MATCH-3 more times
  1373. //#endif
  1374. while (s.insert) {
  1375. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  1376. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
  1377. s.prev[str & s.w_mask] = s.head[s.ins_h];
  1378. s.head[s.ins_h] = str;
  1379. str++;
  1380. s.insert--;
  1381. if (s.lookahead + s.insert < MIN_MATCH) {
  1382. break;
  1383. }
  1384. }
  1385. }
  1386. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1387. * but this is not important since only literal bytes will be emitted.
  1388. */
  1389. } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
  1390. /* If the WIN_INIT bytes after the end of the current data have never been
  1391. * written, then zero those bytes in order to avoid memory check reports of
  1392. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  1393. * the longest match routines. Update the high water mark for the next
  1394. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  1395. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  1396. */
  1397. // if (s.high_water < s.window_size) {
  1398. // var curr = s.strstart + s.lookahead;
  1399. // var init = 0;
  1400. //
  1401. // if (s.high_water < curr) {
  1402. // /* Previous high water mark below current data -- zero WIN_INIT
  1403. // * bytes or up to end of window, whichever is less.
  1404. // */
  1405. // init = s.window_size - curr;
  1406. // if (init > WIN_INIT)
  1407. // init = WIN_INIT;
  1408. // zmemzero(s->window + curr, (unsigned)init);
  1409. // s->high_water = curr + init;
  1410. // }
  1411. // else if (s->high_water < (ulg)curr + WIN_INIT) {
  1412. // /* High water mark at or above current data, but below current data
  1413. // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  1414. // * to end of window, whichever is less.
  1415. // */
  1416. // init = (ulg)curr + WIN_INIT - s->high_water;
  1417. // if (init > s->window_size - s->high_water)
  1418. // init = s->window_size - s->high_water;
  1419. // zmemzero(s->window + s->high_water, (unsigned)init);
  1420. // s->high_water += init;
  1421. // }
  1422. // }
  1423. //
  1424. // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  1425. // "not enough room for search");
  1426. }
  1427. /* ===========================================================================
  1428. * Copy without compression as much as possible from the input stream, return
  1429. * the current block state.
  1430. * This function does not insert new strings in the dictionary since
  1431. * uncompressible data is probably not useful. This function is used
  1432. * only for the level=0 compression option.
  1433. * NOTE: this function should be optimized to avoid extra copying from
  1434. * window to pending_buf.
  1435. */
  1436. function deflate_stored(s, flush) {
  1437. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1438. * to pending_buf_size, and each stored block has a 5 byte header:
  1439. */
  1440. var max_block_size = 0xffff;
  1441. if (max_block_size > s.pending_buf_size - 5) {
  1442. max_block_size = s.pending_buf_size - 5;
  1443. }
  1444. /* Copy as much as possible from input to output: */
  1445. for (;;) {
  1446. /* Fill the window as much as possible: */
  1447. if (s.lookahead <= 1) {
  1448. //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  1449. // s->block_start >= (long)s->w_size, "slide too late");
  1450. // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
  1451. // s.block_start >= s.w_size)) {
  1452. // throw new Error("slide too late");
  1453. // }
  1454. fill_window(s);
  1455. if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
  1456. return BS_NEED_MORE;
  1457. }
  1458. if (s.lookahead === 0) {
  1459. break;
  1460. }
  1461. /* flush the current block */
  1462. }
  1463. //Assert(s->block_start >= 0L, "block gone");
  1464. // if (s.block_start < 0) throw new Error("block gone");
  1465. s.strstart += s.lookahead;
  1466. s.lookahead = 0;
  1467. /* Emit a stored block if pending_buf will be full: */
  1468. var max_start = s.block_start + max_block_size;
  1469. if (s.strstart === 0 || s.strstart >= max_start) {
  1470. /* strstart == 0 is possible when wraparound on 16-bit machine */
  1471. s.lookahead = s.strstart - max_start;
  1472. s.strstart = max_start;
  1473. /*** FLUSH_BLOCK(s, 0); ***/
  1474. flush_block_only(s, false);
  1475. if (s.strm.avail_out === 0) {
  1476. return BS_NEED_MORE;
  1477. }
  1478. /***/
  1479. }
  1480. /* Flush if we may have to slide, otherwise block_start may become
  1481. * negative and the data will be gone:
  1482. */
  1483. if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
  1484. /*** FLUSH_BLOCK(s, 0); ***/
  1485. flush_block_only(s, false);
  1486. if (s.strm.avail_out === 0) {
  1487. return BS_NEED_MORE;
  1488. }
  1489. /***/
  1490. }
  1491. }
  1492. s.insert = 0;
  1493. if (flush === Z_FINISH) {
  1494. /*** FLUSH_BLOCK(s, 1); ***/
  1495. flush_block_only(s, true);
  1496. if (s.strm.avail_out === 0) {
  1497. return BS_FINISH_STARTED;
  1498. }
  1499. /***/
  1500. return BS_FINISH_DONE;
  1501. }
  1502. if (s.strstart > s.block_start) {
  1503. /*** FLUSH_BLOCK(s, 0); ***/
  1504. flush_block_only(s, false);
  1505. if (s.strm.avail_out === 0) {
  1506. return BS_NEED_MORE;
  1507. }
  1508. /***/
  1509. }
  1510. return BS_NEED_MORE;
  1511. }
  1512. /* ===========================================================================
  1513. * Compress as much as possible from the input stream, return the current
  1514. * block state.
  1515. * This function does not perform lazy evaluation of matches and inserts
  1516. * new strings in the dictionary only for unmatched strings or for short
  1517. * matches. It is used only for the fast compression options.
  1518. */
  1519. function deflate_fast(s, flush) {
  1520. var hash_head; /* head of the hash chain */
  1521. var bflush; /* set if current block must be flushed */
  1522. for (;;) {
  1523. /* Make sure that we always have enough lookahead, except
  1524. * at the end of the input file. We need MAX_MATCH bytes
  1525. * for the next match, plus MIN_MATCH bytes to insert the
  1526. * string following the next match.
  1527. */
  1528. if (s.lookahead < MIN_LOOKAHEAD) {
  1529. fill_window(s);
  1530. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  1531. return BS_NEED_MORE;
  1532. }
  1533. if (s.lookahead === 0) {
  1534. break; /* flush the current block */
  1535. }
  1536. }
  1537. /* Insert the string window[strstart .. strstart+2] in the
  1538. * dictionary, and set hash_head to the head of the hash chain:
  1539. */
  1540. hash_head = 0/*NIL*/;
  1541. if (s.lookahead >= MIN_MATCH) {
  1542. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1543. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1544. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1545. s.head[s.ins_h] = s.strstart;
  1546. /***/
  1547. }
  1548. /* Find the longest match, discarding those <= prev_length.
  1549. * At this point we have always match_length < MIN_MATCH
  1550. */
  1551. if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
  1552. /* To simplify the code, we prevent matches with the string
  1553. * of window index 0 (in particular we have to avoid a match
  1554. * of the string with itself at the start of the input file).
  1555. */
  1556. s.match_length = longest_match(s, hash_head);
  1557. /* longest_match() sets match_start */
  1558. }
  1559. if (s.match_length >= MIN_MATCH) {
  1560. // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
  1561. /*** _tr_tally_dist(s, s.strstart - s.match_start,
  1562. s.match_length - MIN_MATCH, bflush); ***/
  1563. bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
  1564. s.lookahead -= s.match_length;
  1565. /* Insert new strings in the hash table only if the match length
  1566. * is not too large. This saves time but degrades compression.
  1567. */
  1568. if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
  1569. s.match_length--; /* string at strstart already in table */
  1570. do {
  1571. s.strstart++;
  1572. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1573. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1574. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1575. s.head[s.ins_h] = s.strstart;
  1576. /***/
  1577. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1578. * always MIN_MATCH bytes ahead.
  1579. */
  1580. } while (--s.match_length !== 0);
  1581. s.strstart++;
  1582. } else
  1583. {
  1584. s.strstart += s.match_length;
  1585. s.match_length = 0;
  1586. s.ins_h = s.window[s.strstart];
  1587. /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
  1588. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
  1589. //#if MIN_MATCH != 3
  1590. // Call UPDATE_HASH() MIN_MATCH-3 more times
  1591. //#endif
  1592. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1593. * matter since it will be recomputed at next deflate call.
  1594. */
  1595. }
  1596. } else {
  1597. /* No match, output a literal byte */
  1598. //Tracevv((stderr,"%c", s.window[s.strstart]));
  1599. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1600. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1601. s.lookahead--;
  1602. s.strstart++;
  1603. }
  1604. if (bflush) {
  1605. /*** FLUSH_BLOCK(s, 0); ***/
  1606. flush_block_only(s, false);
  1607. if (s.strm.avail_out === 0) {
  1608. return BS_NEED_MORE;
  1609. }
  1610. /***/
  1611. }
  1612. }
  1613. s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
  1614. if (flush === Z_FINISH) {
  1615. /*** FLUSH_BLOCK(s, 1); ***/
  1616. flush_block_only(s, true);
  1617. if (s.strm.avail_out === 0) {
  1618. return BS_FINISH_STARTED;
  1619. }
  1620. /***/
  1621. return BS_FINISH_DONE;
  1622. }
  1623. if (s.last_lit) {
  1624. /*** FLUSH_BLOCK(s, 0); ***/
  1625. flush_block_only(s, false);
  1626. if (s.strm.avail_out === 0) {
  1627. return BS_NEED_MORE;
  1628. }
  1629. /***/
  1630. }
  1631. return BS_BLOCK_DONE;
  1632. }
  1633. /* ===========================================================================
  1634. * Same as above, but achieves better compression. We use a lazy
  1635. * evaluation for matches: a match is finally adopted only if there is
  1636. * no better match at the next window position.
  1637. */
  1638. function deflate_slow(s, flush) {
  1639. var hash_head; /* head of hash chain */
  1640. var bflush; /* set if current block must be flushed */
  1641. var max_insert;
  1642. /* Process the input block. */
  1643. for (;;) {
  1644. /* Make sure that we always have enough lookahead, except
  1645. * at the end of the input file. We need MAX_MATCH bytes
  1646. * for the next match, plus MIN_MATCH bytes to insert the
  1647. * string following the next match.
  1648. */
  1649. if (s.lookahead < MIN_LOOKAHEAD) {
  1650. fill_window(s);
  1651. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  1652. return BS_NEED_MORE;
  1653. }
  1654. if (s.lookahead === 0) { break; } /* flush the current block */
  1655. }
  1656. /* Insert the string window[strstart .. strstart+2] in the
  1657. * dictionary, and set hash_head to the head of the hash chain:
  1658. */
  1659. hash_head = 0/*NIL*/;
  1660. if (s.lookahead >= MIN_MATCH) {
  1661. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1662. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1663. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1664. s.head[s.ins_h] = s.strstart;
  1665. /***/
  1666. }
  1667. /* Find the longest match, discarding those <= prev_length.
  1668. */
  1669. s.prev_length = s.match_length;
  1670. s.prev_match = s.match_start;
  1671. s.match_length = MIN_MATCH - 1;
  1672. if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
  1673. s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
  1674. /* To simplify the code, we prevent matches with the string
  1675. * of window index 0 (in particular we have to avoid a match
  1676. * of the string with itself at the start of the input file).
  1677. */
  1678. s.match_length = longest_match(s, hash_head);
  1679. /* longest_match() sets match_start */
  1680. if (s.match_length <= 5 &&
  1681. (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
  1682. /* If prev_match is also MIN_MATCH, match_start is garbage
  1683. * but we will ignore the current match anyway.
  1684. */
  1685. s.match_length = MIN_MATCH - 1;
  1686. }
  1687. }
  1688. /* If there was a match at the previous step and the current
  1689. * match is not better, output the previous match:
  1690. */
  1691. if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
  1692. max_insert = s.strstart + s.lookahead - MIN_MATCH;
  1693. /* Do not insert strings in hash table beyond this. */
  1694. //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  1695. /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
  1696. s.prev_length - MIN_MATCH, bflush);***/
  1697. bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
  1698. /* Insert in hash table all strings up to the end of the match.
  1699. * strstart-1 and strstart are already inserted. If there is not
  1700. * enough lookahead, the last two strings are not inserted in
  1701. * the hash table.
  1702. */
  1703. s.lookahead -= s.prev_length - 1;
  1704. s.prev_length -= 2;
  1705. do {
  1706. if (++s.strstart <= max_insert) {
  1707. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1708. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1709. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1710. s.head[s.ins_h] = s.strstart;
  1711. /***/
  1712. }
  1713. } while (--s.prev_length !== 0);
  1714. s.match_available = 0;
  1715. s.match_length = MIN_MATCH - 1;
  1716. s.strstart++;
  1717. if (bflush) {
  1718. /*** FLUSH_BLOCK(s, 0); ***/
  1719. flush_block_only(s, false);
  1720. if (s.strm.avail_out === 0) {
  1721. return BS_NEED_MORE;
  1722. }
  1723. /***/
  1724. }
  1725. } else if (s.match_available) {
  1726. /* If there was no match at the previous position, output a
  1727. * single literal. If there was a match but the current match
  1728. * is longer, truncate the previous match to a single literal.
  1729. */
  1730. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1731. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  1732. bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
  1733. if (bflush) {
  1734. /*** FLUSH_BLOCK_ONLY(s, 0) ***/
  1735. flush_block_only(s, false);
  1736. /***/
  1737. }
  1738. s.strstart++;
  1739. s.lookahead--;
  1740. if (s.strm.avail_out === 0) {
  1741. return BS_NEED_MORE;
  1742. }
  1743. } else {
  1744. /* There is no previous match to compare with, wait for
  1745. * the next step to decide.
  1746. */
  1747. s.match_available = 1;
  1748. s.strstart++;
  1749. s.lookahead--;
  1750. }
  1751. }
  1752. //Assert (flush != Z_NO_FLUSH, "no flush?");
  1753. if (s.match_available) {
  1754. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1755. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  1756. bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
  1757. s.match_available = 0;
  1758. }
  1759. s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
  1760. if (flush === Z_FINISH) {
  1761. /*** FLUSH_BLOCK(s, 1); ***/
  1762. flush_block_only(s, true);
  1763. if (s.strm.avail_out === 0) {
  1764. return BS_FINISH_STARTED;
  1765. }
  1766. /***/
  1767. return BS_FINISH_DONE;
  1768. }
  1769. if (s.last_lit) {
  1770. /*** FLUSH_BLOCK(s, 0); ***/
  1771. flush_block_only(s, false);
  1772. if (s.strm.avail_out === 0) {
  1773. return BS_NEED_MORE;
  1774. }
  1775. /***/
  1776. }
  1777. return BS_BLOCK_DONE;
  1778. }
  1779. /* ===========================================================================
  1780. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  1781. * one. Do not maintain a hash table. (It will be regenerated if this run of
  1782. * deflate switches away from Z_RLE.)
  1783. */
  1784. function deflate_rle(s, flush) {
  1785. var bflush; /* set if current block must be flushed */
  1786. var prev; /* byte at distance one to match */
  1787. var scan, strend; /* scan goes up to strend for length of run */
  1788. var _win = s.window;
  1789. for (;;) {
  1790. /* Make sure that we always have enough lookahead, except
  1791. * at the end of the input file. We need MAX_MATCH bytes
  1792. * for the longest run, plus one for the unrolled loop.
  1793. */
  1794. if (s.lookahead <= MAX_MATCH) {
  1795. fill_window(s);
  1796. if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
  1797. return BS_NEED_MORE;
  1798. }
  1799. if (s.lookahead === 0) { break; } /* flush the current block */
  1800. }
  1801. /* See how many times the previous byte repeats */
  1802. s.match_length = 0;
  1803. if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
  1804. scan = s.strstart - 1;
  1805. prev = _win[scan];
  1806. if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
  1807. strend = s.strstart + MAX_MATCH;
  1808. do {
  1809. /*jshint noempty:false*/
  1810. } while (prev === _win[++scan] && prev === _win[++scan] &&
  1811. prev === _win[++scan] && prev === _win[++scan] &&
  1812. prev === _win[++scan] && prev === _win[++scan] &&
  1813. prev === _win[++scan] && prev === _win[++scan] &&
  1814. scan < strend);
  1815. s.match_length = MAX_MATCH - (strend - scan);
  1816. if (s.match_length > s.lookahead) {
  1817. s.match_length = s.lookahead;
  1818. }
  1819. }
  1820. //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
  1821. }
  1822. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  1823. if (s.match_length >= MIN_MATCH) {
  1824. //check_match(s, s.strstart, s.strstart - 1, s.match_length);
  1825. /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
  1826. bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
  1827. s.lookahead -= s.match_length;
  1828. s.strstart += s.match_length;
  1829. s.match_length = 0;
  1830. } else {
  1831. /* No match, output a literal byte */
  1832. //Tracevv((stderr,"%c", s->window[s->strstart]));
  1833. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1834. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1835. s.lookahead--;
  1836. s.strstart++;
  1837. }
  1838. if (bflush) {
  1839. /*** FLUSH_BLOCK(s, 0); ***/
  1840. flush_block_only(s, false);
  1841. if (s.strm.avail_out === 0) {
  1842. return BS_NEED_MORE;
  1843. }
  1844. /***/
  1845. }
  1846. }
  1847. s.insert = 0;
  1848. if (flush === Z_FINISH) {
  1849. /*** FLUSH_BLOCK(s, 1); ***/
  1850. flush_block_only(s, true);
  1851. if (s.strm.avail_out === 0) {
  1852. return BS_FINISH_STARTED;
  1853. }
  1854. /***/
  1855. return BS_FINISH_DONE;
  1856. }
  1857. if (s.last_lit) {
  1858. /*** FLUSH_BLOCK(s, 0); ***/
  1859. flush_block_only(s, false);
  1860. if (s.strm.avail_out === 0) {
  1861. return BS_NEED_MORE;
  1862. }
  1863. /***/
  1864. }
  1865. return BS_BLOCK_DONE;
  1866. }
  1867. /* ===========================================================================
  1868. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  1869. * (It will be regenerated if this run of deflate switches away from Huffman.)
  1870. */
  1871. function deflate_huff(s, flush) {
  1872. var bflush; /* set if current block must be flushed */
  1873. for (;;) {
  1874. /* Make sure that we have a literal to write. */
  1875. if (s.lookahead === 0) {
  1876. fill_window(s);
  1877. if (s.lookahead === 0) {
  1878. if (flush === Z_NO_FLUSH) {
  1879. return BS_NEED_MORE;
  1880. }
  1881. break; /* flush the current block */
  1882. }
  1883. }
  1884. /* Output a literal byte */
  1885. s.match_length = 0;
  1886. //Tracevv((stderr,"%c", s->window[s->strstart]));
  1887. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1888. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1889. s.lookahead--;
  1890. s.strstart++;
  1891. if (bflush) {
  1892. /*** FLUSH_BLOCK(s, 0); ***/
  1893. flush_block_only(s, false);
  1894. if (s.strm.avail_out === 0) {
  1895. return BS_NEED_MORE;
  1896. }
  1897. /***/
  1898. }
  1899. }
  1900. s.insert = 0;
  1901. if (flush === Z_FINISH) {
  1902. /*** FLUSH_BLOCK(s, 1); ***/
  1903. flush_block_only(s, true);
  1904. if (s.strm.avail_out === 0) {
  1905. return BS_FINISH_STARTED;
  1906. }
  1907. /***/
  1908. return BS_FINISH_DONE;
  1909. }
  1910. if (s.last_lit) {
  1911. /*** FLUSH_BLOCK(s, 0); ***/
  1912. flush_block_only(s, false);
  1913. if (s.strm.avail_out === 0) {
  1914. return BS_NEED_MORE;
  1915. }
  1916. /***/
  1917. }
  1918. return BS_BLOCK_DONE;
  1919. }
  1920. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  1921. * the desired pack level (0..9). The values given below have been tuned to
  1922. * exclude worst case performance for pathological files. Better values may be
  1923. * found for specific files.
  1924. */
  1925. function Config(good_length, max_lazy, nice_length, max_chain, func) {
  1926. this.good_length = good_length;
  1927. this.max_lazy = max_lazy;
  1928. this.nice_length = nice_length;
  1929. this.max_chain = max_chain;
  1930. this.func = func;
  1931. }
  1932. var configuration_table;
  1933. configuration_table = [
  1934. /* good lazy nice chain */
  1935. new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
  1936. new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
  1937. new Config(4, 5, 16, 8, deflate_fast), /* 2 */
  1938. new Config(4, 6, 32, 32, deflate_fast), /* 3 */
  1939. new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
  1940. new Config(8, 16, 32, 32, deflate_slow), /* 5 */
  1941. new Config(8, 16, 128, 128, deflate_slow), /* 6 */
  1942. new Config(8, 32, 128, 256, deflate_slow), /* 7 */
  1943. new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
  1944. new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
  1945. ];
  1946. /* ===========================================================================
  1947. * Initialize the "longest match" routines for a new zlib stream
  1948. */
  1949. function lm_init(s) {
  1950. s.window_size = 2 * s.w_size;
  1951. /*** CLEAR_HASH(s); ***/
  1952. zero(s.head); // Fill with NIL (= 0);
  1953. /* Set the default configuration parameters:
  1954. */
  1955. s.max_lazy_match = configuration_table[s.level].max_lazy;
  1956. s.good_match = configuration_table[s.level].good_length;
  1957. s.nice_match = configuration_table[s.level].nice_length;
  1958. s.max_chain_length = configuration_table[s.level].max_chain;
  1959. s.strstart = 0;
  1960. s.block_start = 0;
  1961. s.lookahead = 0;
  1962. s.insert = 0;
  1963. s.match_length = s.prev_length = MIN_MATCH - 1;
  1964. s.match_available = 0;
  1965. s.ins_h = 0;
  1966. }
  1967. function DeflateState() {
  1968. this.strm = null; /* pointer back to this zlib stream */
  1969. this.status = 0; /* as the name implies */
  1970. this.pending_buf = null; /* output still pending */
  1971. this.pending_buf_size = 0; /* size of pending_buf */
  1972. this.pending_out = 0; /* next pending byte to output to the stream */
  1973. this.pending = 0; /* nb of bytes in the pending buffer */
  1974. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  1975. this.gzhead = null; /* gzip header information to write */
  1976. this.gzindex = 0; /* where in extra, name, or comment */
  1977. this.method = Z_DEFLATED; /* can only be DEFLATED */
  1978. this.last_flush = -1; /* value of flush param for previous deflate call */
  1979. this.w_size = 0; /* LZ77 window size (32K by default) */
  1980. this.w_bits = 0; /* log2(w_size) (8..16) */
  1981. this.w_mask = 0; /* w_size - 1 */
  1982. this.window = null;
  1983. /* Sliding window. Input bytes are read into the second half of the window,
  1984. * and move to the first half later to keep a dictionary of at least wSize
  1985. * bytes. With this organization, matches are limited to a distance of
  1986. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  1987. * performed with a length multiple of the block size.
  1988. */
  1989. this.window_size = 0;
  1990. /* Actual size of window: 2*wSize, except when the user input buffer
  1991. * is directly used as sliding window.
  1992. */
  1993. this.prev = null;
  1994. /* Link to older string with same hash index. To limit the size of this
  1995. * array to 64K, this link is maintained only for the last 32K strings.
  1996. * An index in this array is thus a window index modulo 32K.
  1997. */
  1998. this.head = null; /* Heads of the hash chains or NIL. */
  1999. this.ins_h = 0; /* hash index of string to be inserted */
  2000. this.hash_size = 0; /* number of elements in hash table */
  2001. this.hash_bits = 0; /* log2(hash_size) */
  2002. this.hash_mask = 0; /* hash_size-1 */
  2003. this.hash_shift = 0;
  2004. /* Number of bits by which ins_h must be shifted at each input
  2005. * step. It must be such that after MIN_MATCH steps, the oldest
  2006. * byte no longer takes part in the hash key, that is:
  2007. * hash_shift * MIN_MATCH >= hash_bits
  2008. */
  2009. this.block_start = 0;
  2010. /* Window position at the beginning of the current output block. Gets
  2011. * negative when the window is moved backwards.
  2012. */
  2013. this.match_length = 0; /* length of best match */
  2014. this.prev_match = 0; /* previous match */
  2015. this.match_available = 0; /* set if previous match exists */
  2016. this.strstart = 0; /* start of string to insert */
  2017. this.match_start = 0; /* start of matching string */
  2018. this.lookahead = 0; /* number of valid bytes ahead in window */
  2019. this.prev_length = 0;
  2020. /* Length of the best match at previous step. Matches not greater than this
  2021. * are discarded. This is used in the lazy match evaluation.
  2022. */
  2023. this.max_chain_length = 0;
  2024. /* To speed up deflation, hash chains are never searched beyond this
  2025. * length. A higher limit improves compression ratio but degrades the
  2026. * speed.
  2027. */
  2028. this.max_lazy_match = 0;
  2029. /* Attempt to find a better match only when the current match is strictly
  2030. * smaller than this value. This mechanism is used only for compression
  2031. * levels >= 4.
  2032. */
  2033. // That's alias to max_lazy_match, don't use directly
  2034. //this.max_insert_length = 0;
  2035. /* Insert new strings in the hash table only if the match length is not
  2036. * greater than this length. This saves time but degrades compression.
  2037. * max_insert_length is used only for compression levels <= 3.
  2038. */
  2039. this.level = 0; /* compression level (1..9) */
  2040. this.strategy = 0; /* favor or force Huffman coding*/
  2041. this.good_match = 0;
  2042. /* Use a faster search when the previous match is longer than this */
  2043. this.nice_match = 0; /* Stop searching when current match exceeds this */
  2044. /* used by trees.c: */
  2045. /* Didn't use ct_data typedef below to suppress compiler warning */
  2046. // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  2047. // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  2048. // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  2049. // Use flat array of DOUBLE size, with interleaved fata,
  2050. // because JS does not support effective
  2051. this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
  2052. this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2);
  2053. this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2);
  2054. zero(this.dyn_ltree);
  2055. zero(this.dyn_dtree);
  2056. zero(this.bl_tree);
  2057. this.l_desc = null; /* desc. for literal tree */
  2058. this.d_desc = null; /* desc. for distance tree */
  2059. this.bl_desc = null; /* desc. for bit length tree */
  2060. //ush bl_count[MAX_BITS+1];
  2061. this.bl_count = new utils.Buf16(MAX_BITS + 1);
  2062. /* number of codes at each bit length for an optimal tree */
  2063. //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  2064. this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */
  2065. zero(this.heap);
  2066. this.heap_len = 0; /* number of elements in the heap */
  2067. this.heap_max = 0; /* element of largest frequency */
  2068. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  2069. * The same heap array is used to build all trees.
  2070. */
  2071. this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
  2072. zero(this.depth);
  2073. /* Depth of each subtree used as tie breaker for trees of equal frequency
  2074. */
  2075. this.l_buf = 0; /* buffer index for literals or lengths */
  2076. this.lit_bufsize = 0;
  2077. /* Size of match buffer for literals/lengths. There are 4 reasons for
  2078. * limiting lit_bufsize to 64K:
  2079. * - frequencies can be kept in 16 bit counters
  2080. * - if compression is not successful for the first block, all input
  2081. * data is still in the window so we can still emit a stored block even
  2082. * when input comes from standard input. (This can also be done for
  2083. * all blocks if lit_bufsize is not greater than 32K.)
  2084. * - if compression is not successful for a file smaller than 64K, we can
  2085. * even emit a stored file instead of a stored block (saving 5 bytes).
  2086. * This is applicable only for zip (not gzip or zlib).
  2087. * - creating new Huffman trees less frequently may not provide fast
  2088. * adaptation to changes in the input data statistics. (Take for
  2089. * example a binary file with poorly compressible code followed by
  2090. * a highly compressible string table.) Smaller buffer sizes give
  2091. * fast adaptation but have of course the overhead of transmitting
  2092. * trees more frequently.
  2093. * - I can't count above 4
  2094. */
  2095. this.last_lit = 0; /* running index in l_buf */
  2096. this.d_buf = 0;
  2097. /* Buffer index for distances. To simplify the code, d_buf and l_buf have
  2098. * the same number of elements. To use different lengths, an extra flag
  2099. * array would be necessary.
  2100. */
  2101. this.opt_len = 0; /* bit length of current block with optimal trees */
  2102. this.static_len = 0; /* bit length of current block with static trees */
  2103. this.matches = 0; /* number of string matches in current block */
  2104. this.insert = 0; /* bytes at end of window left to insert */
  2105. this.bi_buf = 0;
  2106. /* Output buffer. bits are inserted starting at the bottom (least
  2107. * significant bits).
  2108. */
  2109. this.bi_valid = 0;
  2110. /* Number of valid bits in bi_buf. All bits above the last valid bit
  2111. * are always zero.
  2112. */
  2113. // Used for window memory init. We safely ignore it for JS. That makes
  2114. // sense only for pointers and memory check tools.
  2115. //this.high_water = 0;
  2116. /* High water mark offset in window for initialized bytes -- bytes above
  2117. * this are set to zero in order to avoid memory check warnings when
  2118. * longest match routines access bytes past the input. This is then
  2119. * updated to the new high water mark.
  2120. */
  2121. }
  2122. function deflateResetKeep(strm) {
  2123. var s;
  2124. if (!strm || !strm.state) {
  2125. return err(strm, Z_STREAM_ERROR);
  2126. }
  2127. strm.total_in = strm.total_out = 0;
  2128. strm.data_type = Z_UNKNOWN;
  2129. s = strm.state;
  2130. s.pending = 0;
  2131. s.pending_out = 0;
  2132. if (s.wrap < 0) {
  2133. s.wrap = -s.wrap;
  2134. /* was made negative by deflate(..., Z_FINISH); */
  2135. }
  2136. s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
  2137. strm.adler = (s.wrap === 2) ?
  2138. 0 // crc32(0, Z_NULL, 0)
  2139. :
  2140. 1; // adler32(0, Z_NULL, 0)
  2141. s.last_flush = Z_NO_FLUSH;
  2142. trees._tr_init(s);
  2143. return Z_OK;
  2144. }
  2145. function deflateReset(strm) {
  2146. var ret = deflateResetKeep(strm);
  2147. if (ret === Z_OK) {
  2148. lm_init(strm.state);
  2149. }
  2150. return ret;
  2151. }
  2152. function deflateSetHeader(strm, head) {
  2153. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  2154. if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
  2155. strm.state.gzhead = head;
  2156. return Z_OK;
  2157. }
  2158. function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
  2159. if (!strm) { // === Z_NULL
  2160. return Z_STREAM_ERROR;
  2161. }
  2162. var wrap = 1;
  2163. if (level === Z_DEFAULT_COMPRESSION) {
  2164. level = 6;
  2165. }
  2166. if (windowBits < 0) { /* suppress zlib wrapper */
  2167. wrap = 0;
  2168. windowBits = -windowBits;
  2169. }
  2170. else if (windowBits > 15) {
  2171. wrap = 2; /* write gzip wrapper instead */
  2172. windowBits -= 16;
  2173. }
  2174. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
  2175. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  2176. strategy < 0 || strategy > Z_FIXED) {
  2177. return err(strm, Z_STREAM_ERROR);
  2178. }
  2179. if (windowBits === 8) {
  2180. windowBits = 9;
  2181. }
  2182. /* until 256-byte window bug fixed */
  2183. var s = new DeflateState();
  2184. strm.state = s;
  2185. s.strm = strm;
  2186. s.wrap = wrap;
  2187. s.gzhead = null;
  2188. s.w_bits = windowBits;
  2189. s.w_size = 1 << s.w_bits;
  2190. s.w_mask = s.w_size - 1;
  2191. s.hash_bits = memLevel + 7;
  2192. s.hash_size = 1 << s.hash_bits;
  2193. s.hash_mask = s.hash_size - 1;
  2194. s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  2195. s.window = new utils.Buf8(s.w_size * 2);
  2196. s.head = new utils.Buf16(s.hash_size);
  2197. s.prev = new utils.Buf16(s.w_size);
  2198. // Don't need mem init magic for JS.
  2199. //s.high_water = 0; /* nothing written to s->window yet */
  2200. s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  2201. s.pending_buf_size = s.lit_bufsize * 4;
  2202. //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  2203. //s->pending_buf = (uchf *) overlay;
  2204. s.pending_buf = new utils.Buf8(s.pending_buf_size);
  2205. // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
  2206. //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  2207. s.d_buf = 1 * s.lit_bufsize;
  2208. //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  2209. s.l_buf = (1 + 2) * s.lit_bufsize;
  2210. s.level = level;
  2211. s.strategy = strategy;
  2212. s.method = method;
  2213. return deflateReset(strm);
  2214. }
  2215. function deflateInit(strm, level) {
  2216. return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  2217. }
  2218. function deflate(strm, flush) {
  2219. var old_flush, s;
  2220. var beg, val; // for gzip header write only
  2221. if (!strm || !strm.state ||
  2222. flush > Z_BLOCK || flush < 0) {
  2223. return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
  2224. }
  2225. s = strm.state;
  2226. if (!strm.output ||
  2227. (!strm.input && strm.avail_in !== 0) ||
  2228. (s.status === FINISH_STATE && flush !== Z_FINISH)) {
  2229. return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
  2230. }
  2231. s.strm = strm; /* just in case */
  2232. old_flush = s.last_flush;
  2233. s.last_flush = flush;
  2234. /* Write the header */
  2235. if (s.status === INIT_STATE) {
  2236. if (s.wrap === 2) { // GZIP header
  2237. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2238. put_byte(s, 31);
  2239. put_byte(s, 139);
  2240. put_byte(s, 8);
  2241. if (!s.gzhead) { // s->gzhead == Z_NULL
  2242. put_byte(s, 0);
  2243. put_byte(s, 0);
  2244. put_byte(s, 0);
  2245. put_byte(s, 0);
  2246. put_byte(s, 0);
  2247. put_byte(s, s.level === 9 ? 2 :
  2248. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  2249. 4 : 0));
  2250. put_byte(s, OS_CODE);
  2251. s.status = BUSY_STATE;
  2252. }
  2253. else {
  2254. put_byte(s, (s.gzhead.text ? 1 : 0) +
  2255. (s.gzhead.hcrc ? 2 : 0) +
  2256. (!s.gzhead.extra ? 0 : 4) +
  2257. (!s.gzhead.name ? 0 : 8) +
  2258. (!s.gzhead.comment ? 0 : 16)
  2259. );
  2260. put_byte(s, s.gzhead.time & 0xff);
  2261. put_byte(s, (s.gzhead.time >> 8) & 0xff);
  2262. put_byte(s, (s.gzhead.time >> 16) & 0xff);
  2263. put_byte(s, (s.gzhead.time >> 24) & 0xff);
  2264. put_byte(s, s.level === 9 ? 2 :
  2265. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  2266. 4 : 0));
  2267. put_byte(s, s.gzhead.os & 0xff);
  2268. if (s.gzhead.extra && s.gzhead.extra.length) {
  2269. put_byte(s, s.gzhead.extra.length & 0xff);
  2270. put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
  2271. }
  2272. if (s.gzhead.hcrc) {
  2273. strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
  2274. }
  2275. s.gzindex = 0;
  2276. s.status = EXTRA_STATE;
  2277. }
  2278. }
  2279. else // DEFLATE header
  2280. {
  2281. var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
  2282. var level_flags = -1;
  2283. if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
  2284. level_flags = 0;
  2285. } else if (s.level < 6) {
  2286. level_flags = 1;
  2287. } else if (s.level === 6) {
  2288. level_flags = 2;
  2289. } else {
  2290. level_flags = 3;
  2291. }
  2292. header |= (level_flags << 6);
  2293. if (s.strstart !== 0) { header |= PRESET_DICT; }
  2294. header += 31 - (header % 31);
  2295. s.status = BUSY_STATE;
  2296. putShortMSB(s, header);
  2297. /* Save the adler32 of the preset dictionary: */
  2298. if (s.strstart !== 0) {
  2299. putShortMSB(s, strm.adler >>> 16);
  2300. putShortMSB(s, strm.adler & 0xffff);
  2301. }
  2302. strm.adler = 1; // adler32(0L, Z_NULL, 0);
  2303. }
  2304. }
  2305. //#ifdef GZIP
  2306. if (s.status === EXTRA_STATE) {
  2307. if (s.gzhead.extra/* != Z_NULL*/) {
  2308. beg = s.pending; /* start of bytes to update crc */
  2309. while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
  2310. if (s.pending === s.pending_buf_size) {
  2311. if (s.gzhead.hcrc && s.pending > beg) {
  2312. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2313. }
  2314. flush_pending(strm);
  2315. beg = s.pending;
  2316. if (s.pending === s.pending_buf_size) {
  2317. break;
  2318. }
  2319. }
  2320. put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
  2321. s.gzindex++;
  2322. }
  2323. if (s.gzhead.hcrc && s.pending > beg) {
  2324. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2325. }
  2326. if (s.gzindex === s.gzhead.extra.length) {
  2327. s.gzindex = 0;
  2328. s.status = NAME_STATE;
  2329. }
  2330. }
  2331. else {
  2332. s.status = NAME_STATE;
  2333. }
  2334. }
  2335. if (s.status === NAME_STATE) {
  2336. if (s.gzhead.name/* != Z_NULL*/) {
  2337. beg = s.pending; /* start of bytes to update crc */
  2338. //int val;
  2339. do {
  2340. if (s.pending === s.pending_buf_size) {
  2341. if (s.gzhead.hcrc && s.pending > beg) {
  2342. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2343. }
  2344. flush_pending(strm);
  2345. beg = s.pending;
  2346. if (s.pending === s.pending_buf_size) {
  2347. val = 1;
  2348. break;
  2349. }
  2350. }
  2351. // JS specific: little magic to add zero terminator to end of string
  2352. if (s.gzindex < s.gzhead.name.length) {
  2353. val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
  2354. } else {
  2355. val = 0;
  2356. }
  2357. put_byte(s, val);
  2358. } while (val !== 0);
  2359. if (s.gzhead.hcrc && s.pending > beg) {
  2360. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2361. }
  2362. if (val === 0) {
  2363. s.gzindex = 0;
  2364. s.status = COMMENT_STATE;
  2365. }
  2366. }
  2367. else {
  2368. s.status = COMMENT_STATE;
  2369. }
  2370. }
  2371. if (s.status === COMMENT_STATE) {
  2372. if (s.gzhead.comment/* != Z_NULL*/) {
  2373. beg = s.pending; /* start of bytes to update crc */
  2374. //int val;
  2375. do {
  2376. if (s.pending === s.pending_buf_size) {
  2377. if (s.gzhead.hcrc && s.pending > beg) {
  2378. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2379. }
  2380. flush_pending(strm);
  2381. beg = s.pending;
  2382. if (s.pending === s.pending_buf_size) {
  2383. val = 1;
  2384. break;
  2385. }
  2386. }
  2387. // JS specific: little magic to add zero terminator to end of string
  2388. if (s.gzindex < s.gzhead.comment.length) {
  2389. val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
  2390. } else {
  2391. val = 0;
  2392. }
  2393. put_byte(s, val);
  2394. } while (val !== 0);
  2395. if (s.gzhead.hcrc && s.pending > beg) {
  2396. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2397. }
  2398. if (val === 0) {
  2399. s.status = HCRC_STATE;
  2400. }
  2401. }
  2402. else {
  2403. s.status = HCRC_STATE;
  2404. }
  2405. }
  2406. if (s.status === HCRC_STATE) {
  2407. if (s.gzhead.hcrc) {
  2408. if (s.pending + 2 > s.pending_buf_size) {
  2409. flush_pending(strm);
  2410. }
  2411. if (s.pending + 2 <= s.pending_buf_size) {
  2412. put_byte(s, strm.adler & 0xff);
  2413. put_byte(s, (strm.adler >> 8) & 0xff);
  2414. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2415. s.status = BUSY_STATE;
  2416. }
  2417. }
  2418. else {
  2419. s.status = BUSY_STATE;
  2420. }
  2421. }
  2422. //#endif
  2423. /* Flush as much pending output as possible */
  2424. if (s.pending !== 0) {
  2425. flush_pending(strm);
  2426. if (strm.avail_out === 0) {
  2427. /* Since avail_out is 0, deflate will be called again with
  2428. * more output space, but possibly with both pending and
  2429. * avail_in equal to zero. There won't be anything to do,
  2430. * but this is not an error situation so make sure we
  2431. * return OK instead of BUF_ERROR at next call of deflate:
  2432. */
  2433. s.last_flush = -1;
  2434. return Z_OK;
  2435. }
  2436. /* Make sure there is something to do and avoid duplicate consecutive
  2437. * flushes. For repeated and useless calls with Z_FINISH, we keep
  2438. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  2439. */
  2440. } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
  2441. flush !== Z_FINISH) {
  2442. return err(strm, Z_BUF_ERROR);
  2443. }
  2444. /* User must not provide more input after the first FINISH: */
  2445. if (s.status === FINISH_STATE && strm.avail_in !== 0) {
  2446. return err(strm, Z_BUF_ERROR);
  2447. }
  2448. /* Start a new block or continue the current one.
  2449. */
  2450. if (strm.avail_in !== 0 || s.lookahead !== 0 ||
  2451. (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
  2452. var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
  2453. (s.strategy === Z_RLE ? deflate_rle(s, flush) :
  2454. configuration_table[s.level].func(s, flush));
  2455. if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
  2456. s.status = FINISH_STATE;
  2457. }
  2458. if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
  2459. if (strm.avail_out === 0) {
  2460. s.last_flush = -1;
  2461. /* avoid BUF_ERROR next call, see above */
  2462. }
  2463. return Z_OK;
  2464. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  2465. * of deflate should use the same flush parameter to make sure
  2466. * that the flush is complete. So we don't have to output an
  2467. * empty block here, this will be done at next call. This also
  2468. * ensures that for a very small output buffer, we emit at most
  2469. * one empty block.
  2470. */
  2471. }
  2472. if (bstate === BS_BLOCK_DONE) {
  2473. if (flush === Z_PARTIAL_FLUSH) {
  2474. trees._tr_align(s);
  2475. }
  2476. else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
  2477. trees._tr_stored_block(s, 0, 0, false);
  2478. /* For a full flush, this empty block will be recognized
  2479. * as a special marker by inflate_sync().
  2480. */
  2481. if (flush === Z_FULL_FLUSH) {
  2482. /*** CLEAR_HASH(s); ***/ /* forget history */
  2483. zero(s.head); // Fill with NIL (= 0);
  2484. if (s.lookahead === 0) {
  2485. s.strstart = 0;
  2486. s.block_start = 0;
  2487. s.insert = 0;
  2488. }
  2489. }
  2490. }
  2491. flush_pending(strm);
  2492. if (strm.avail_out === 0) {
  2493. s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  2494. return Z_OK;
  2495. }
  2496. }
  2497. }
  2498. //Assert(strm->avail_out > 0, "bug2");
  2499. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  2500. if (flush !== Z_FINISH) { return Z_OK; }
  2501. if (s.wrap <= 0) { return Z_STREAM_END; }
  2502. /* Write the trailer */
  2503. if (s.wrap === 2) {
  2504. put_byte(s, strm.adler & 0xff);
  2505. put_byte(s, (strm.adler >> 8) & 0xff);
  2506. put_byte(s, (strm.adler >> 16) & 0xff);
  2507. put_byte(s, (strm.adler >> 24) & 0xff);
  2508. put_byte(s, strm.total_in & 0xff);
  2509. put_byte(s, (strm.total_in >> 8) & 0xff);
  2510. put_byte(s, (strm.total_in >> 16) & 0xff);
  2511. put_byte(s, (strm.total_in >> 24) & 0xff);
  2512. }
  2513. else
  2514. {
  2515. putShortMSB(s, strm.adler >>> 16);
  2516. putShortMSB(s, strm.adler & 0xffff);
  2517. }
  2518. flush_pending(strm);
  2519. /* If avail_out is zero, the application will call deflate again
  2520. * to flush the rest.
  2521. */
  2522. if (s.wrap > 0) { s.wrap = -s.wrap; }
  2523. /* write the trailer only once! */
  2524. return s.pending !== 0 ? Z_OK : Z_STREAM_END;
  2525. }
  2526. function deflateEnd(strm) {
  2527. var status;
  2528. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  2529. return Z_STREAM_ERROR;
  2530. }
  2531. status = strm.state.status;
  2532. if (status !== INIT_STATE &&
  2533. status !== EXTRA_STATE &&
  2534. status !== NAME_STATE &&
  2535. status !== COMMENT_STATE &&
  2536. status !== HCRC_STATE &&
  2537. status !== BUSY_STATE &&
  2538. status !== FINISH_STATE
  2539. ) {
  2540. return err(strm, Z_STREAM_ERROR);
  2541. }
  2542. strm.state = null;
  2543. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
  2544. }
  2545. /* =========================================================================
  2546. * Initializes the compression dictionary from the given byte
  2547. * sequence without producing any compressed output.
  2548. */
  2549. function deflateSetDictionary(strm, dictionary) {
  2550. var dictLength = dictionary.length;
  2551. var s;
  2552. var str, n;
  2553. var wrap;
  2554. var avail;
  2555. var next;
  2556. var input;
  2557. var tmpDict;
  2558. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  2559. return Z_STREAM_ERROR;
  2560. }
  2561. s = strm.state;
  2562. wrap = s.wrap;
  2563. if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
  2564. return Z_STREAM_ERROR;
  2565. }
  2566. /* when using zlib wrappers, compute Adler-32 for provided dictionary */
  2567. if (wrap === 1) {
  2568. /* adler32(strm->adler, dictionary, dictLength); */
  2569. strm.adler = adler32(strm.adler, dictionary, dictLength, 0);
  2570. }
  2571. s.wrap = 0; /* avoid computing Adler-32 in read_buf */
  2572. /* if dictionary would fill window, just replace the history */
  2573. if (dictLength >= s.w_size) {
  2574. if (wrap === 0) { /* already empty otherwise */
  2575. /*** CLEAR_HASH(s); ***/
  2576. zero(s.head); // Fill with NIL (= 0);
  2577. s.strstart = 0;
  2578. s.block_start = 0;
  2579. s.insert = 0;
  2580. }
  2581. /* use the tail */
  2582. // dictionary = dictionary.slice(dictLength - s.w_size);
  2583. tmpDict = new utils.Buf8(s.w_size);
  2584. utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0);
  2585. dictionary = tmpDict;
  2586. dictLength = s.w_size;
  2587. }
  2588. /* insert dictionary into window and hash */
  2589. avail = strm.avail_in;
  2590. next = strm.next_in;
  2591. input = strm.input;
  2592. strm.avail_in = dictLength;
  2593. strm.next_in = 0;
  2594. strm.input = dictionary;
  2595. fill_window(s);
  2596. while (s.lookahead >= MIN_MATCH) {
  2597. str = s.strstart;
  2598. n = s.lookahead - (MIN_MATCH - 1);
  2599. do {
  2600. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  2601. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
  2602. s.prev[str & s.w_mask] = s.head[s.ins_h];
  2603. s.head[s.ins_h] = str;
  2604. str++;
  2605. } while (--n);
  2606. s.strstart = str;
  2607. s.lookahead = MIN_MATCH - 1;
  2608. fill_window(s);
  2609. }
  2610. s.strstart += s.lookahead;
  2611. s.block_start = s.strstart;
  2612. s.insert = s.lookahead;
  2613. s.lookahead = 0;
  2614. s.match_length = s.prev_length = MIN_MATCH - 1;
  2615. s.match_available = 0;
  2616. strm.next_in = next;
  2617. strm.input = input;
  2618. strm.avail_in = avail;
  2619. s.wrap = wrap;
  2620. return Z_OK;
  2621. }
  2622. exports.deflateInit = deflateInit;
  2623. exports.deflateInit2 = deflateInit2;
  2624. exports.deflateReset = deflateReset;
  2625. exports.deflateResetKeep = deflateResetKeep;
  2626. exports.deflateSetHeader = deflateSetHeader;
  2627. exports.deflate = deflate;
  2628. exports.deflateEnd = deflateEnd;
  2629. exports.deflateSetDictionary = deflateSetDictionary;
  2630. exports.deflateInfo = 'pako deflate (from Nodeca project)';
  2631. /* Not implemented
  2632. exports.deflateBound = deflateBound;
  2633. exports.deflateCopy = deflateCopy;
  2634. exports.deflateParams = deflateParams;
  2635. exports.deflatePending = deflatePending;
  2636. exports.deflatePrime = deflatePrime;
  2637. exports.deflateTune = deflateTune;
  2638. */
  2639. },{"../utils/common":3,"./adler32":5,"./crc32":7,"./messages":13,"./trees":14}],9:[function(require,module,exports){
  2640. 'use strict';
  2641. function GZheader() {
  2642. /* true if compressed data believed to be text */
  2643. this.text = 0;
  2644. /* modification time */
  2645. this.time = 0;
  2646. /* extra flags (not used when writing a gzip file) */
  2647. this.xflags = 0;
  2648. /* operating system */
  2649. this.os = 0;
  2650. /* pointer to extra field or Z_NULL if none */
  2651. this.extra = null;
  2652. /* extra field length (valid if extra != Z_NULL) */
  2653. this.extra_len = 0; // Actually, we don't need it in JS,
  2654. // but leave for few code modifications
  2655. //
  2656. // Setup limits is not necessary because in js we should not preallocate memory
  2657. // for inflate use constant limit in 65536 bytes
  2658. //
  2659. /* space at extra (only when reading header) */
  2660. // this.extra_max = 0;
  2661. /* pointer to zero-terminated file name or Z_NULL */
  2662. this.name = '';
  2663. /* space at name (only when reading header) */
  2664. // this.name_max = 0;
  2665. /* pointer to zero-terminated comment or Z_NULL */
  2666. this.comment = '';
  2667. /* space at comment (only when reading header) */
  2668. // this.comm_max = 0;
  2669. /* true if there was or will be a header crc */
  2670. this.hcrc = 0;
  2671. /* true when done reading gzip header (not used when writing a gzip file) */
  2672. this.done = false;
  2673. }
  2674. module.exports = GZheader;
  2675. },{}],10:[function(require,module,exports){
  2676. 'use strict';
  2677. // See state defs from inflate.js
  2678. var BAD = 30; /* got a data error -- remain here until reset */
  2679. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  2680. /*
  2681. Decode literal, length, and distance codes and write out the resulting
  2682. literal and match bytes until either not enough input or output is
  2683. available, an end-of-block is encountered, or a data error is encountered.
  2684. When large enough input and output buffers are supplied to inflate(), for
  2685. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  2686. inflate execution time is spent in this routine.
  2687. Entry assumptions:
  2688. state.mode === LEN
  2689. strm.avail_in >= 6
  2690. strm.avail_out >= 258
  2691. start >= strm.avail_out
  2692. state.bits < 8
  2693. On return, state.mode is one of:
  2694. LEN -- ran out of enough output space or enough available input
  2695. TYPE -- reached end of block code, inflate() to interpret next block
  2696. BAD -- error in block data
  2697. Notes:
  2698. - The maximum input bits used by a length/distance pair is 15 bits for the
  2699. length code, 5 bits for the length extra, 15 bits for the distance code,
  2700. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  2701. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  2702. checking for available input while decoding.
  2703. - The maximum bytes that a single length/distance pair can output is 258
  2704. bytes, which is the maximum length that can be coded. inflate_fast()
  2705. requires strm.avail_out >= 258 for each loop to avoid checking for
  2706. output space.
  2707. */
  2708. module.exports = function inflate_fast(strm, start) {
  2709. var state;
  2710. var _in; /* local strm.input */
  2711. var last; /* have enough input while in < last */
  2712. var _out; /* local strm.output */
  2713. var beg; /* inflate()'s initial strm.output */
  2714. var end; /* while out < end, enough space available */
  2715. //#ifdef INFLATE_STRICT
  2716. var dmax; /* maximum distance from zlib header */
  2717. //#endif
  2718. var wsize; /* window size or zero if not using window */
  2719. var whave; /* valid bytes in the window */
  2720. var wnext; /* window write index */
  2721. // Use `s_window` instead `window`, avoid conflict with instrumentation tools
  2722. var s_window; /* allocated sliding window, if wsize != 0 */
  2723. var hold; /* local strm.hold */
  2724. var bits; /* local strm.bits */
  2725. var lcode; /* local strm.lencode */
  2726. var dcode; /* local strm.distcode */
  2727. var lmask; /* mask for first level of length codes */
  2728. var dmask; /* mask for first level of distance codes */
  2729. var here; /* retrieved table entry */
  2730. var op; /* code bits, operation, extra bits, or */
  2731. /* window position, window bytes to copy */
  2732. var len; /* match length, unused bytes */
  2733. var dist; /* match distance */
  2734. var from; /* where to copy match from */
  2735. var from_source;
  2736. var input, output; // JS specific, because we have no pointers
  2737. /* copy state to local variables */
  2738. state = strm.state;
  2739. //here = state.here;
  2740. _in = strm.next_in;
  2741. input = strm.input;
  2742. last = _in + (strm.avail_in - 5);
  2743. _out = strm.next_out;
  2744. output = strm.output;
  2745. beg = _out - (start - strm.avail_out);
  2746. end = _out + (strm.avail_out - 257);
  2747. //#ifdef INFLATE_STRICT
  2748. dmax = state.dmax;
  2749. //#endif
  2750. wsize = state.wsize;
  2751. whave = state.whave;
  2752. wnext = state.wnext;
  2753. s_window = state.window;
  2754. hold = state.hold;
  2755. bits = state.bits;
  2756. lcode = state.lencode;
  2757. dcode = state.distcode;
  2758. lmask = (1 << state.lenbits) - 1;
  2759. dmask = (1 << state.distbits) - 1;
  2760. /* decode literals and length/distances until end-of-block or not enough
  2761. input data or output space */
  2762. top:
  2763. do {
  2764. if (bits < 15) {
  2765. hold += input[_in++] << bits;
  2766. bits += 8;
  2767. hold += input[_in++] << bits;
  2768. bits += 8;
  2769. }
  2770. here = lcode[hold & lmask];
  2771. dolen:
  2772. for (;;) { // Goto emulation
  2773. op = here >>> 24/*here.bits*/;
  2774. hold >>>= op;
  2775. bits -= op;
  2776. op = (here >>> 16) & 0xff/*here.op*/;
  2777. if (op === 0) { /* literal */
  2778. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  2779. // "inflate: literal '%c'\n" :
  2780. // "inflate: literal 0x%02x\n", here.val));
  2781. output[_out++] = here & 0xffff/*here.val*/;
  2782. }
  2783. else if (op & 16) { /* length base */
  2784. len = here & 0xffff/*here.val*/;
  2785. op &= 15; /* number of extra bits */
  2786. if (op) {
  2787. if (bits < op) {
  2788. hold += input[_in++] << bits;
  2789. bits += 8;
  2790. }
  2791. len += hold & ((1 << op) - 1);
  2792. hold >>>= op;
  2793. bits -= op;
  2794. }
  2795. //Tracevv((stderr, "inflate: length %u\n", len));
  2796. if (bits < 15) {
  2797. hold += input[_in++] << bits;
  2798. bits += 8;
  2799. hold += input[_in++] << bits;
  2800. bits += 8;
  2801. }
  2802. here = dcode[hold & dmask];
  2803. dodist:
  2804. for (;;) { // goto emulation
  2805. op = here >>> 24/*here.bits*/;
  2806. hold >>>= op;
  2807. bits -= op;
  2808. op = (here >>> 16) & 0xff/*here.op*/;
  2809. if (op & 16) { /* distance base */
  2810. dist = here & 0xffff/*here.val*/;
  2811. op &= 15; /* number of extra bits */
  2812. if (bits < op) {
  2813. hold += input[_in++] << bits;
  2814. bits += 8;
  2815. if (bits < op) {
  2816. hold += input[_in++] << bits;
  2817. bits += 8;
  2818. }
  2819. }
  2820. dist += hold & ((1 << op) - 1);
  2821. //#ifdef INFLATE_STRICT
  2822. if (dist > dmax) {
  2823. strm.msg = 'invalid distance too far back';
  2824. state.mode = BAD;
  2825. break top;
  2826. }
  2827. //#endif
  2828. hold >>>= op;
  2829. bits -= op;
  2830. //Tracevv((stderr, "inflate: distance %u\n", dist));
  2831. op = _out - beg; /* max distance in output */
  2832. if (dist > op) { /* see if copy from window */
  2833. op = dist - op; /* distance back in window */
  2834. if (op > whave) {
  2835. if (state.sane) {
  2836. strm.msg = 'invalid distance too far back';
  2837. state.mode = BAD;
  2838. break top;
  2839. }
  2840. // (!) This block is disabled in zlib defailts,
  2841. // don't enable it for binary compatibility
  2842. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  2843. // if (len <= op - whave) {
  2844. // do {
  2845. // output[_out++] = 0;
  2846. // } while (--len);
  2847. // continue top;
  2848. // }
  2849. // len -= op - whave;
  2850. // do {
  2851. // output[_out++] = 0;
  2852. // } while (--op > whave);
  2853. // if (op === 0) {
  2854. // from = _out - dist;
  2855. // do {
  2856. // output[_out++] = output[from++];
  2857. // } while (--len);
  2858. // continue top;
  2859. // }
  2860. //#endif
  2861. }
  2862. from = 0; // window index
  2863. from_source = s_window;
  2864. if (wnext === 0) { /* very common case */
  2865. from += wsize - op;
  2866. if (op < len) { /* some from window */
  2867. len -= op;
  2868. do {
  2869. output[_out++] = s_window[from++];
  2870. } while (--op);
  2871. from = _out - dist; /* rest from output */
  2872. from_source = output;
  2873. }
  2874. }
  2875. else if (wnext < op) { /* wrap around window */
  2876. from += wsize + wnext - op;
  2877. op -= wnext;
  2878. if (op < len) { /* some from end of window */
  2879. len -= op;
  2880. do {
  2881. output[_out++] = s_window[from++];
  2882. } while (--op);
  2883. from = 0;
  2884. if (wnext < len) { /* some from start of window */
  2885. op = wnext;
  2886. len -= op;
  2887. do {
  2888. output[_out++] = s_window[from++];
  2889. } while (--op);
  2890. from = _out - dist; /* rest from output */
  2891. from_source = output;
  2892. }
  2893. }
  2894. }
  2895. else { /* contiguous in window */
  2896. from += wnext - op;
  2897. if (op < len) { /* some from window */
  2898. len -= op;
  2899. do {
  2900. output[_out++] = s_window[from++];
  2901. } while (--op);
  2902. from = _out - dist; /* rest from output */
  2903. from_source = output;
  2904. }
  2905. }
  2906. while (len > 2) {
  2907. output[_out++] = from_source[from++];
  2908. output[_out++] = from_source[from++];
  2909. output[_out++] = from_source[from++];
  2910. len -= 3;
  2911. }
  2912. if (len) {
  2913. output[_out++] = from_source[from++];
  2914. if (len > 1) {
  2915. output[_out++] = from_source[from++];
  2916. }
  2917. }
  2918. }
  2919. else {
  2920. from = _out - dist; /* copy direct from output */
  2921. do { /* minimum length is three */
  2922. output[_out++] = output[from++];
  2923. output[_out++] = output[from++];
  2924. output[_out++] = output[from++];
  2925. len -= 3;
  2926. } while (len > 2);
  2927. if (len) {
  2928. output[_out++] = output[from++];
  2929. if (len > 1) {
  2930. output[_out++] = output[from++];
  2931. }
  2932. }
  2933. }
  2934. }
  2935. else if ((op & 64) === 0) { /* 2nd level distance code */
  2936. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  2937. continue dodist;
  2938. }
  2939. else {
  2940. strm.msg = 'invalid distance code';
  2941. state.mode = BAD;
  2942. break top;
  2943. }
  2944. break; // need to emulate goto via "continue"
  2945. }
  2946. }
  2947. else if ((op & 64) === 0) { /* 2nd level length code */
  2948. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  2949. continue dolen;
  2950. }
  2951. else if (op & 32) { /* end-of-block */
  2952. //Tracevv((stderr, "inflate: end of block\n"));
  2953. state.mode = TYPE;
  2954. break top;
  2955. }
  2956. else {
  2957. strm.msg = 'invalid literal/length code';
  2958. state.mode = BAD;
  2959. break top;
  2960. }
  2961. break; // need to emulate goto via "continue"
  2962. }
  2963. } while (_in < last && _out < end);
  2964. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  2965. len = bits >> 3;
  2966. _in -= len;
  2967. bits -= len << 3;
  2968. hold &= (1 << bits) - 1;
  2969. /* update state and return */
  2970. strm.next_in = _in;
  2971. strm.next_out = _out;
  2972. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  2973. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  2974. state.hold = hold;
  2975. state.bits = bits;
  2976. return;
  2977. };
  2978. },{}],11:[function(require,module,exports){
  2979. 'use strict';
  2980. var utils = require('../utils/common');
  2981. var adler32 = require('./adler32');
  2982. var crc32 = require('./crc32');
  2983. var inflate_fast = require('./inffast');
  2984. var inflate_table = require('./inftrees');
  2985. var CODES = 0;
  2986. var LENS = 1;
  2987. var DISTS = 2;
  2988. /* Public constants ==========================================================*/
  2989. /* ===========================================================================*/
  2990. /* Allowed flush values; see deflate() and inflate() below for details */
  2991. //var Z_NO_FLUSH = 0;
  2992. //var Z_PARTIAL_FLUSH = 1;
  2993. //var Z_SYNC_FLUSH = 2;
  2994. //var Z_FULL_FLUSH = 3;
  2995. var Z_FINISH = 4;
  2996. var Z_BLOCK = 5;
  2997. var Z_TREES = 6;
  2998. /* Return codes for the compression/decompression functions. Negative values
  2999. * are errors, positive values are used for special but normal events.
  3000. */
  3001. var Z_OK = 0;
  3002. var Z_STREAM_END = 1;
  3003. var Z_NEED_DICT = 2;
  3004. //var Z_ERRNO = -1;
  3005. var Z_STREAM_ERROR = -2;
  3006. var Z_DATA_ERROR = -3;
  3007. var Z_MEM_ERROR = -4;
  3008. var Z_BUF_ERROR = -5;
  3009. //var Z_VERSION_ERROR = -6;
  3010. /* The deflate compression method */
  3011. var Z_DEFLATED = 8;
  3012. /* STATES ====================================================================*/
  3013. /* ===========================================================================*/
  3014. var HEAD = 1; /* i: waiting for magic header */
  3015. var FLAGS = 2; /* i: waiting for method and flags (gzip) */
  3016. var TIME = 3; /* i: waiting for modification time (gzip) */
  3017. var OS = 4; /* i: waiting for extra flags and operating system (gzip) */
  3018. var EXLEN = 5; /* i: waiting for extra length (gzip) */
  3019. var EXTRA = 6; /* i: waiting for extra bytes (gzip) */
  3020. var NAME = 7; /* i: waiting for end of file name (gzip) */
  3021. var COMMENT = 8; /* i: waiting for end of comment (gzip) */
  3022. var HCRC = 9; /* i: waiting for header crc (gzip) */
  3023. var DICTID = 10; /* i: waiting for dictionary check value */
  3024. var DICT = 11; /* waiting for inflateSetDictionary() call */
  3025. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  3026. var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
  3027. var STORED = 14; /* i: waiting for stored size (length and complement) */
  3028. var COPY_ = 15; /* i/o: same as COPY below, but only first time in */
  3029. var COPY = 16; /* i/o: waiting for input or output to copy stored block */
  3030. var TABLE = 17; /* i: waiting for dynamic block table lengths */
  3031. var LENLENS = 18; /* i: waiting for code length code lengths */
  3032. var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
  3033. var LEN_ = 20; /* i: same as LEN below, but only first time in */
  3034. var LEN = 21; /* i: waiting for length/lit/eob code */
  3035. var LENEXT = 22; /* i: waiting for length extra bits */
  3036. var DIST = 23; /* i: waiting for distance code */
  3037. var DISTEXT = 24; /* i: waiting for distance extra bits */
  3038. var MATCH = 25; /* o: waiting for output space to copy string */
  3039. var LIT = 26; /* o: waiting for output space to write literal */
  3040. var CHECK = 27; /* i: waiting for 32-bit check value */
  3041. var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
  3042. var DONE = 29; /* finished check, done -- remain here until reset */
  3043. var BAD = 30; /* got a data error -- remain here until reset */
  3044. var MEM = 31; /* got an inflate() memory error -- remain here until reset */
  3045. var SYNC = 32; /* looking for synchronization bytes to restart inflate() */
  3046. /* ===========================================================================*/
  3047. var ENOUGH_LENS = 852;
  3048. var ENOUGH_DISTS = 592;
  3049. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  3050. var MAX_WBITS = 15;
  3051. /* 32K LZ77 window */
  3052. var DEF_WBITS = MAX_WBITS;
  3053. function zswap32(q) {
  3054. return (((q >>> 24) & 0xff) +
  3055. ((q >>> 8) & 0xff00) +
  3056. ((q & 0xff00) << 8) +
  3057. ((q & 0xff) << 24));
  3058. }
  3059. function InflateState() {
  3060. this.mode = 0; /* current inflate mode */
  3061. this.last = false; /* true if processing last block */
  3062. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  3063. this.havedict = false; /* true if dictionary provided */
  3064. this.flags = 0; /* gzip header method and flags (0 if zlib) */
  3065. this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */
  3066. this.check = 0; /* protected copy of check value */
  3067. this.total = 0; /* protected copy of output count */
  3068. // TODO: may be {}
  3069. this.head = null; /* where to save gzip header information */
  3070. /* sliding window */
  3071. this.wbits = 0; /* log base 2 of requested window size */
  3072. this.wsize = 0; /* window size or zero if not using window */
  3073. this.whave = 0; /* valid bytes in the window */
  3074. this.wnext = 0; /* window write index */
  3075. this.window = null; /* allocated sliding window, if needed */
  3076. /* bit accumulator */
  3077. this.hold = 0; /* input bit accumulator */
  3078. this.bits = 0; /* number of bits in "in" */
  3079. /* for string and stored block copying */
  3080. this.length = 0; /* literal or length of data to copy */
  3081. this.offset = 0; /* distance back to copy string from */
  3082. /* for table and code decoding */
  3083. this.extra = 0; /* extra bits needed */
  3084. /* fixed and dynamic code tables */
  3085. this.lencode = null; /* starting table for length/literal codes */
  3086. this.distcode = null; /* starting table for distance codes */
  3087. this.lenbits = 0; /* index bits for lencode */
  3088. this.distbits = 0; /* index bits for distcode */
  3089. /* dynamic table building */
  3090. this.ncode = 0; /* number of code length code lengths */
  3091. this.nlen = 0; /* number of length code lengths */
  3092. this.ndist = 0; /* number of distance code lengths */
  3093. this.have = 0; /* number of code lengths in lens[] */
  3094. this.next = null; /* next available space in codes[] */
  3095. this.lens = new utils.Buf16(320); /* temporary storage for code lengths */
  3096. this.work = new utils.Buf16(288); /* work area for code table building */
  3097. /*
  3098. because we don't have pointers in js, we use lencode and distcode directly
  3099. as buffers so we don't need codes
  3100. */
  3101. //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */
  3102. this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
  3103. this.distdyn = null; /* dynamic table for distance codes (JS specific) */
  3104. this.sane = 0; /* if false, allow invalid distance too far */
  3105. this.back = 0; /* bits back of last unprocessed length/lit */
  3106. this.was = 0; /* initial length of match */
  3107. }
  3108. function inflateResetKeep(strm) {
  3109. var state;
  3110. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  3111. state = strm.state;
  3112. strm.total_in = strm.total_out = state.total = 0;
  3113. strm.msg = ''; /*Z_NULL*/
  3114. if (state.wrap) { /* to support ill-conceived Java test suite */
  3115. strm.adler = state.wrap & 1;
  3116. }
  3117. state.mode = HEAD;
  3118. state.last = 0;
  3119. state.havedict = 0;
  3120. state.dmax = 32768;
  3121. state.head = null/*Z_NULL*/;
  3122. state.hold = 0;
  3123. state.bits = 0;
  3124. //state.lencode = state.distcode = state.next = state.codes;
  3125. state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS);
  3126. state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS);
  3127. state.sane = 1;
  3128. state.back = -1;
  3129. //Tracev((stderr, "inflate: reset\n"));
  3130. return Z_OK;
  3131. }
  3132. function inflateReset(strm) {
  3133. var state;
  3134. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  3135. state = strm.state;
  3136. state.wsize = 0;
  3137. state.whave = 0;
  3138. state.wnext = 0;
  3139. return inflateResetKeep(strm);
  3140. }
  3141. function inflateReset2(strm, windowBits) {
  3142. var wrap;
  3143. var state;
  3144. /* get the state */
  3145. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  3146. state = strm.state;
  3147. /* extract wrap request from windowBits parameter */
  3148. if (windowBits < 0) {
  3149. wrap = 0;
  3150. windowBits = -windowBits;
  3151. }
  3152. else {
  3153. wrap = (windowBits >> 4) + 1;
  3154. if (windowBits < 48) {
  3155. windowBits &= 15;
  3156. }
  3157. }
  3158. /* set number of window bits, free window if different */
  3159. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  3160. return Z_STREAM_ERROR;
  3161. }
  3162. if (state.window !== null && state.wbits !== windowBits) {
  3163. state.window = null;
  3164. }
  3165. /* update state and reset the rest of it */
  3166. state.wrap = wrap;
  3167. state.wbits = windowBits;
  3168. return inflateReset(strm);
  3169. }
  3170. function inflateInit2(strm, windowBits) {
  3171. var ret;
  3172. var state;
  3173. if (!strm) { return Z_STREAM_ERROR; }
  3174. //strm.msg = Z_NULL; /* in case we return an error */
  3175. state = new InflateState();
  3176. //if (state === Z_NULL) return Z_MEM_ERROR;
  3177. //Tracev((stderr, "inflate: allocated\n"));
  3178. strm.state = state;
  3179. state.window = null/*Z_NULL*/;
  3180. ret = inflateReset2(strm, windowBits);
  3181. if (ret !== Z_OK) {
  3182. strm.state = null/*Z_NULL*/;
  3183. }
  3184. return ret;
  3185. }
  3186. function inflateInit(strm) {
  3187. return inflateInit2(strm, DEF_WBITS);
  3188. }
  3189. /*
  3190. Return state with length and distance decoding tables and index sizes set to
  3191. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  3192. If BUILDFIXED is defined, then instead this routine builds the tables the
  3193. first time it's called, and returns those tables the first time and
  3194. thereafter. This reduces the size of the code by about 2K bytes, in
  3195. exchange for a little execution time. However, BUILDFIXED should not be
  3196. used for threaded applications, since the rewriting of the tables and virgin
  3197. may not be thread-safe.
  3198. */
  3199. var virgin = true;
  3200. var lenfix, distfix; // We have no pointers in JS, so keep tables separate
  3201. function fixedtables(state) {
  3202. /* build fixed huffman tables if first call (may not be thread safe) */
  3203. if (virgin) {
  3204. var sym;
  3205. lenfix = new utils.Buf32(512);
  3206. distfix = new utils.Buf32(32);
  3207. /* literal/length table */
  3208. sym = 0;
  3209. while (sym < 144) { state.lens[sym++] = 8; }
  3210. while (sym < 256) { state.lens[sym++] = 9; }
  3211. while (sym < 280) { state.lens[sym++] = 7; }
  3212. while (sym < 288) { state.lens[sym++] = 8; }
  3213. inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
  3214. /* distance table */
  3215. sym = 0;
  3216. while (sym < 32) { state.lens[sym++] = 5; }
  3217. inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
  3218. /* do this just once */
  3219. virgin = false;
  3220. }
  3221. state.lencode = lenfix;
  3222. state.lenbits = 9;
  3223. state.distcode = distfix;
  3224. state.distbits = 5;
  3225. }
  3226. /*
  3227. Update the window with the last wsize (normally 32K) bytes written before
  3228. returning. If window does not exist yet, create it. This is only called
  3229. when a window is already in use, or when output has been written during this
  3230. inflate call, but the end of the deflate stream has not been reached yet.
  3231. It is also called to create a window for dictionary data when a dictionary
  3232. is loaded.
  3233. Providing output buffers larger than 32K to inflate() should provide a speed
  3234. advantage, since only the last 32K of output is copied to the sliding window
  3235. upon return from inflate(), and since all distances after the first 32K of
  3236. output will fall in the output data, making match copies simpler and faster.
  3237. The advantage may be dependent on the size of the processor's data caches.
  3238. */
  3239. function updatewindow(strm, src, end, copy) {
  3240. var dist;
  3241. var state = strm.state;
  3242. /* if it hasn't been done already, allocate space for the window */
  3243. if (state.window === null) {
  3244. state.wsize = 1 << state.wbits;
  3245. state.wnext = 0;
  3246. state.whave = 0;
  3247. state.window = new utils.Buf8(state.wsize);
  3248. }
  3249. /* copy state->wsize or less output bytes into the circular window */
  3250. if (copy >= state.wsize) {
  3251. utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0);
  3252. state.wnext = 0;
  3253. state.whave = state.wsize;
  3254. }
  3255. else {
  3256. dist = state.wsize - state.wnext;
  3257. if (dist > copy) {
  3258. dist = copy;
  3259. }
  3260. //zmemcpy(state->window + state->wnext, end - copy, dist);
  3261. utils.arraySet(state.window, src, end - copy, dist, state.wnext);
  3262. copy -= dist;
  3263. if (copy) {
  3264. //zmemcpy(state->window, end - copy, copy);
  3265. utils.arraySet(state.window, src, end - copy, copy, 0);
  3266. state.wnext = copy;
  3267. state.whave = state.wsize;
  3268. }
  3269. else {
  3270. state.wnext += dist;
  3271. if (state.wnext === state.wsize) { state.wnext = 0; }
  3272. if (state.whave < state.wsize) { state.whave += dist; }
  3273. }
  3274. }
  3275. return 0;
  3276. }
  3277. function inflate(strm, flush) {
  3278. var state;
  3279. var input, output; // input/output buffers
  3280. var next; /* next input INDEX */
  3281. var put; /* next output INDEX */
  3282. var have, left; /* available input and output */
  3283. var hold; /* bit buffer */
  3284. var bits; /* bits in bit buffer */
  3285. var _in, _out; /* save starting available input and output */
  3286. var copy; /* number of stored or match bytes to copy */
  3287. var from; /* where to copy match bytes from */
  3288. var from_source;
  3289. var here = 0; /* current decoding table entry */
  3290. var here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  3291. //var last; /* parent table entry */
  3292. var last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  3293. var len; /* length to copy for repeats, bits to drop */
  3294. var ret; /* return code */
  3295. var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */
  3296. var opts;
  3297. var n; // temporary var for NEED_BITS
  3298. var order = /* permutation of code lengths */
  3299. [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  3300. if (!strm || !strm.state || !strm.output ||
  3301. (!strm.input && strm.avail_in !== 0)) {
  3302. return Z_STREAM_ERROR;
  3303. }
  3304. state = strm.state;
  3305. if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
  3306. //--- LOAD() ---
  3307. put = strm.next_out;
  3308. output = strm.output;
  3309. left = strm.avail_out;
  3310. next = strm.next_in;
  3311. input = strm.input;
  3312. have = strm.avail_in;
  3313. hold = state.hold;
  3314. bits = state.bits;
  3315. //---
  3316. _in = have;
  3317. _out = left;
  3318. ret = Z_OK;
  3319. inf_leave: // goto emulation
  3320. for (;;) {
  3321. switch (state.mode) {
  3322. case HEAD:
  3323. if (state.wrap === 0) {
  3324. state.mode = TYPEDO;
  3325. break;
  3326. }
  3327. //=== NEEDBITS(16);
  3328. while (bits < 16) {
  3329. if (have === 0) { break inf_leave; }
  3330. have--;
  3331. hold += input[next++] << bits;
  3332. bits += 8;
  3333. }
  3334. //===//
  3335. if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */
  3336. state.check = 0/*crc32(0L, Z_NULL, 0)*/;
  3337. //=== CRC2(state.check, hold);
  3338. hbuf[0] = hold & 0xff;
  3339. hbuf[1] = (hold >>> 8) & 0xff;
  3340. state.check = crc32(state.check, hbuf, 2, 0);
  3341. //===//
  3342. //=== INITBITS();
  3343. hold = 0;
  3344. bits = 0;
  3345. //===//
  3346. state.mode = FLAGS;
  3347. break;
  3348. }
  3349. state.flags = 0; /* expect zlib header */
  3350. if (state.head) {
  3351. state.head.done = false;
  3352. }
  3353. if (!(state.wrap & 1) || /* check if zlib header allowed */
  3354. (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
  3355. strm.msg = 'incorrect header check';
  3356. state.mode = BAD;
  3357. break;
  3358. }
  3359. if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
  3360. strm.msg = 'unknown compression method';
  3361. state.mode = BAD;
  3362. break;
  3363. }
  3364. //--- DROPBITS(4) ---//
  3365. hold >>>= 4;
  3366. bits -= 4;
  3367. //---//
  3368. len = (hold & 0x0f)/*BITS(4)*/ + 8;
  3369. if (state.wbits === 0) {
  3370. state.wbits = len;
  3371. }
  3372. else if (len > state.wbits) {
  3373. strm.msg = 'invalid window size';
  3374. state.mode = BAD;
  3375. break;
  3376. }
  3377. state.dmax = 1 << len;
  3378. //Tracev((stderr, "inflate: zlib header ok\n"));
  3379. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  3380. state.mode = hold & 0x200 ? DICTID : TYPE;
  3381. //=== INITBITS();
  3382. hold = 0;
  3383. bits = 0;
  3384. //===//
  3385. break;
  3386. case FLAGS:
  3387. //=== NEEDBITS(16); */
  3388. while (bits < 16) {
  3389. if (have === 0) { break inf_leave; }
  3390. have--;
  3391. hold += input[next++] << bits;
  3392. bits += 8;
  3393. }
  3394. //===//
  3395. state.flags = hold;
  3396. if ((state.flags & 0xff) !== Z_DEFLATED) {
  3397. strm.msg = 'unknown compression method';
  3398. state.mode = BAD;
  3399. break;
  3400. }
  3401. if (state.flags & 0xe000) {
  3402. strm.msg = 'unknown header flags set';
  3403. state.mode = BAD;
  3404. break;
  3405. }
  3406. if (state.head) {
  3407. state.head.text = ((hold >> 8) & 1);
  3408. }
  3409. if (state.flags & 0x0200) {
  3410. //=== CRC2(state.check, hold);
  3411. hbuf[0] = hold & 0xff;
  3412. hbuf[1] = (hold >>> 8) & 0xff;
  3413. state.check = crc32(state.check, hbuf, 2, 0);
  3414. //===//
  3415. }
  3416. //=== INITBITS();
  3417. hold = 0;
  3418. bits = 0;
  3419. //===//
  3420. state.mode = TIME;
  3421. /* falls through */
  3422. case TIME:
  3423. //=== NEEDBITS(32); */
  3424. while (bits < 32) {
  3425. if (have === 0) { break inf_leave; }
  3426. have--;
  3427. hold += input[next++] << bits;
  3428. bits += 8;
  3429. }
  3430. //===//
  3431. if (state.head) {
  3432. state.head.time = hold;
  3433. }
  3434. if (state.flags & 0x0200) {
  3435. //=== CRC4(state.check, hold)
  3436. hbuf[0] = hold & 0xff;
  3437. hbuf[1] = (hold >>> 8) & 0xff;
  3438. hbuf[2] = (hold >>> 16) & 0xff;
  3439. hbuf[3] = (hold >>> 24) & 0xff;
  3440. state.check = crc32(state.check, hbuf, 4, 0);
  3441. //===
  3442. }
  3443. //=== INITBITS();
  3444. hold = 0;
  3445. bits = 0;
  3446. //===//
  3447. state.mode = OS;
  3448. /* falls through */
  3449. case OS:
  3450. //=== NEEDBITS(16); */
  3451. while (bits < 16) {
  3452. if (have === 0) { break inf_leave; }
  3453. have--;
  3454. hold += input[next++] << bits;
  3455. bits += 8;
  3456. }
  3457. //===//
  3458. if (state.head) {
  3459. state.head.xflags = (hold & 0xff);
  3460. state.head.os = (hold >> 8);
  3461. }
  3462. if (state.flags & 0x0200) {
  3463. //=== CRC2(state.check, hold);
  3464. hbuf[0] = hold & 0xff;
  3465. hbuf[1] = (hold >>> 8) & 0xff;
  3466. state.check = crc32(state.check, hbuf, 2, 0);
  3467. //===//
  3468. }
  3469. //=== INITBITS();
  3470. hold = 0;
  3471. bits = 0;
  3472. //===//
  3473. state.mode = EXLEN;
  3474. /* falls through */
  3475. case EXLEN:
  3476. if (state.flags & 0x0400) {
  3477. //=== NEEDBITS(16); */
  3478. while (bits < 16) {
  3479. if (have === 0) { break inf_leave; }
  3480. have--;
  3481. hold += input[next++] << bits;
  3482. bits += 8;
  3483. }
  3484. //===//
  3485. state.length = hold;
  3486. if (state.head) {
  3487. state.head.extra_len = hold;
  3488. }
  3489. if (state.flags & 0x0200) {
  3490. //=== CRC2(state.check, hold);
  3491. hbuf[0] = hold & 0xff;
  3492. hbuf[1] = (hold >>> 8) & 0xff;
  3493. state.check = crc32(state.check, hbuf, 2, 0);
  3494. //===//
  3495. }
  3496. //=== INITBITS();
  3497. hold = 0;
  3498. bits = 0;
  3499. //===//
  3500. }
  3501. else if (state.head) {
  3502. state.head.extra = null/*Z_NULL*/;
  3503. }
  3504. state.mode = EXTRA;
  3505. /* falls through */
  3506. case EXTRA:
  3507. if (state.flags & 0x0400) {
  3508. copy = state.length;
  3509. if (copy > have) { copy = have; }
  3510. if (copy) {
  3511. if (state.head) {
  3512. len = state.head.extra_len - state.length;
  3513. if (!state.head.extra) {
  3514. // Use untyped array for more conveniend processing later
  3515. state.head.extra = new Array(state.head.extra_len);
  3516. }
  3517. utils.arraySet(
  3518. state.head.extra,
  3519. input,
  3520. next,
  3521. // extra field is limited to 65536 bytes
  3522. // - no need for additional size check
  3523. copy,
  3524. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  3525. len
  3526. );
  3527. //zmemcpy(state.head.extra + len, next,
  3528. // len + copy > state.head.extra_max ?
  3529. // state.head.extra_max - len : copy);
  3530. }
  3531. if (state.flags & 0x0200) {
  3532. state.check = crc32(state.check, input, copy, next);
  3533. }
  3534. have -= copy;
  3535. next += copy;
  3536. state.length -= copy;
  3537. }
  3538. if (state.length) { break inf_leave; }
  3539. }
  3540. state.length = 0;
  3541. state.mode = NAME;
  3542. /* falls through */
  3543. case NAME:
  3544. if (state.flags & 0x0800) {
  3545. if (have === 0) { break inf_leave; }
  3546. copy = 0;
  3547. do {
  3548. // TODO: 2 or 1 bytes?
  3549. len = input[next + copy++];
  3550. /* use constant limit because in js we should not preallocate memory */
  3551. if (state.head && len &&
  3552. (state.length < 65536 /*state.head.name_max*/)) {
  3553. state.head.name += String.fromCharCode(len);
  3554. }
  3555. } while (len && copy < have);
  3556. if (state.flags & 0x0200) {
  3557. state.check = crc32(state.check, input, copy, next);
  3558. }
  3559. have -= copy;
  3560. next += copy;
  3561. if (len) { break inf_leave; }
  3562. }
  3563. else if (state.head) {
  3564. state.head.name = null;
  3565. }
  3566. state.length = 0;
  3567. state.mode = COMMENT;
  3568. /* falls through */
  3569. case COMMENT:
  3570. if (state.flags & 0x1000) {
  3571. if (have === 0) { break inf_leave; }
  3572. copy = 0;
  3573. do {
  3574. len = input[next + copy++];
  3575. /* use constant limit because in js we should not preallocate memory */
  3576. if (state.head && len &&
  3577. (state.length < 65536 /*state.head.comm_max*/)) {
  3578. state.head.comment += String.fromCharCode(len);
  3579. }
  3580. } while (len && copy < have);
  3581. if (state.flags & 0x0200) {
  3582. state.check = crc32(state.check, input, copy, next);
  3583. }
  3584. have -= copy;
  3585. next += copy;
  3586. if (len) { break inf_leave; }
  3587. }
  3588. else if (state.head) {
  3589. state.head.comment = null;
  3590. }
  3591. state.mode = HCRC;
  3592. /* falls through */
  3593. case HCRC:
  3594. if (state.flags & 0x0200) {
  3595. //=== NEEDBITS(16); */
  3596. while (bits < 16) {
  3597. if (have === 0) { break inf_leave; }
  3598. have--;
  3599. hold += input[next++] << bits;
  3600. bits += 8;
  3601. }
  3602. //===//
  3603. if (hold !== (state.check & 0xffff)) {
  3604. strm.msg = 'header crc mismatch';
  3605. state.mode = BAD;
  3606. break;
  3607. }
  3608. //=== INITBITS();
  3609. hold = 0;
  3610. bits = 0;
  3611. //===//
  3612. }
  3613. if (state.head) {
  3614. state.head.hcrc = ((state.flags >> 9) & 1);
  3615. state.head.done = true;
  3616. }
  3617. strm.adler = state.check = 0;
  3618. state.mode = TYPE;
  3619. break;
  3620. case DICTID:
  3621. //=== NEEDBITS(32); */
  3622. while (bits < 32) {
  3623. if (have === 0) { break inf_leave; }
  3624. have--;
  3625. hold += input[next++] << bits;
  3626. bits += 8;
  3627. }
  3628. //===//
  3629. strm.adler = state.check = zswap32(hold);
  3630. //=== INITBITS();
  3631. hold = 0;
  3632. bits = 0;
  3633. //===//
  3634. state.mode = DICT;
  3635. /* falls through */
  3636. case DICT:
  3637. if (state.havedict === 0) {
  3638. //--- RESTORE() ---
  3639. strm.next_out = put;
  3640. strm.avail_out = left;
  3641. strm.next_in = next;
  3642. strm.avail_in = have;
  3643. state.hold = hold;
  3644. state.bits = bits;
  3645. //---
  3646. return Z_NEED_DICT;
  3647. }
  3648. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  3649. state.mode = TYPE;
  3650. /* falls through */
  3651. case TYPE:
  3652. if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
  3653. /* falls through */
  3654. case TYPEDO:
  3655. if (state.last) {
  3656. //--- BYTEBITS() ---//
  3657. hold >>>= bits & 7;
  3658. bits -= bits & 7;
  3659. //---//
  3660. state.mode = CHECK;
  3661. break;
  3662. }
  3663. //=== NEEDBITS(3); */
  3664. while (bits < 3) {
  3665. if (have === 0) { break inf_leave; }
  3666. have--;
  3667. hold += input[next++] << bits;
  3668. bits += 8;
  3669. }
  3670. //===//
  3671. state.last = (hold & 0x01)/*BITS(1)*/;
  3672. //--- DROPBITS(1) ---//
  3673. hold >>>= 1;
  3674. bits -= 1;
  3675. //---//
  3676. switch ((hold & 0x03)/*BITS(2)*/) {
  3677. case 0: /* stored block */
  3678. //Tracev((stderr, "inflate: stored block%s\n",
  3679. // state.last ? " (last)" : ""));
  3680. state.mode = STORED;
  3681. break;
  3682. case 1: /* fixed block */
  3683. fixedtables(state);
  3684. //Tracev((stderr, "inflate: fixed codes block%s\n",
  3685. // state.last ? " (last)" : ""));
  3686. state.mode = LEN_; /* decode codes */
  3687. if (flush === Z_TREES) {
  3688. //--- DROPBITS(2) ---//
  3689. hold >>>= 2;
  3690. bits -= 2;
  3691. //---//
  3692. break inf_leave;
  3693. }
  3694. break;
  3695. case 2: /* dynamic block */
  3696. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  3697. // state.last ? " (last)" : ""));
  3698. state.mode = TABLE;
  3699. break;
  3700. case 3:
  3701. strm.msg = 'invalid block type';
  3702. state.mode = BAD;
  3703. }
  3704. //--- DROPBITS(2) ---//
  3705. hold >>>= 2;
  3706. bits -= 2;
  3707. //---//
  3708. break;
  3709. case STORED:
  3710. //--- BYTEBITS() ---// /* go to byte boundary */
  3711. hold >>>= bits & 7;
  3712. bits -= bits & 7;
  3713. //---//
  3714. //=== NEEDBITS(32); */
  3715. while (bits < 32) {
  3716. if (have === 0) { break inf_leave; }
  3717. have--;
  3718. hold += input[next++] << bits;
  3719. bits += 8;
  3720. }
  3721. //===//
  3722. if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
  3723. strm.msg = 'invalid stored block lengths';
  3724. state.mode = BAD;
  3725. break;
  3726. }
  3727. state.length = hold & 0xffff;
  3728. //Tracev((stderr, "inflate: stored length %u\n",
  3729. // state.length));
  3730. //=== INITBITS();
  3731. hold = 0;
  3732. bits = 0;
  3733. //===//
  3734. state.mode = COPY_;
  3735. if (flush === Z_TREES) { break inf_leave; }
  3736. /* falls through */
  3737. case COPY_:
  3738. state.mode = COPY;
  3739. /* falls through */
  3740. case COPY:
  3741. copy = state.length;
  3742. if (copy) {
  3743. if (copy > have) { copy = have; }
  3744. if (copy > left) { copy = left; }
  3745. if (copy === 0) { break inf_leave; }
  3746. //--- zmemcpy(put, next, copy); ---
  3747. utils.arraySet(output, input, next, copy, put);
  3748. //---//
  3749. have -= copy;
  3750. next += copy;
  3751. left -= copy;
  3752. put += copy;
  3753. state.length -= copy;
  3754. break;
  3755. }
  3756. //Tracev((stderr, "inflate: stored end\n"));
  3757. state.mode = TYPE;
  3758. break;
  3759. case TABLE:
  3760. //=== NEEDBITS(14); */
  3761. while (bits < 14) {
  3762. if (have === 0) { break inf_leave; }
  3763. have--;
  3764. hold += input[next++] << bits;
  3765. bits += 8;
  3766. }
  3767. //===//
  3768. state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
  3769. //--- DROPBITS(5) ---//
  3770. hold >>>= 5;
  3771. bits -= 5;
  3772. //---//
  3773. state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
  3774. //--- DROPBITS(5) ---//
  3775. hold >>>= 5;
  3776. bits -= 5;
  3777. //---//
  3778. state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
  3779. //--- DROPBITS(4) ---//
  3780. hold >>>= 4;
  3781. bits -= 4;
  3782. //---//
  3783. //#ifndef PKZIP_BUG_WORKAROUND
  3784. if (state.nlen > 286 || state.ndist > 30) {
  3785. strm.msg = 'too many length or distance symbols';
  3786. state.mode = BAD;
  3787. break;
  3788. }
  3789. //#endif
  3790. //Tracev((stderr, "inflate: table sizes ok\n"));
  3791. state.have = 0;
  3792. state.mode = LENLENS;
  3793. /* falls through */
  3794. case LENLENS:
  3795. while (state.have < state.ncode) {
  3796. //=== NEEDBITS(3);
  3797. while (bits < 3) {
  3798. if (have === 0) { break inf_leave; }
  3799. have--;
  3800. hold += input[next++] << bits;
  3801. bits += 8;
  3802. }
  3803. //===//
  3804. state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
  3805. //--- DROPBITS(3) ---//
  3806. hold >>>= 3;
  3807. bits -= 3;
  3808. //---//
  3809. }
  3810. while (state.have < 19) {
  3811. state.lens[order[state.have++]] = 0;
  3812. }
  3813. // We have separate tables & no pointers. 2 commented lines below not needed.
  3814. //state.next = state.codes;
  3815. //state.lencode = state.next;
  3816. // Switch to use dynamic table
  3817. state.lencode = state.lendyn;
  3818. state.lenbits = 7;
  3819. opts = { bits: state.lenbits };
  3820. ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  3821. state.lenbits = opts.bits;
  3822. if (ret) {
  3823. strm.msg = 'invalid code lengths set';
  3824. state.mode = BAD;
  3825. break;
  3826. }
  3827. //Tracev((stderr, "inflate: code lengths ok\n"));
  3828. state.have = 0;
  3829. state.mode = CODELENS;
  3830. /* falls through */
  3831. case CODELENS:
  3832. while (state.have < state.nlen + state.ndist) {
  3833. for (;;) {
  3834. here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/
  3835. here_bits = here >>> 24;
  3836. here_op = (here >>> 16) & 0xff;
  3837. here_val = here & 0xffff;
  3838. if ((here_bits) <= bits) { break; }
  3839. //--- PULLBYTE() ---//
  3840. if (have === 0) { break inf_leave; }
  3841. have--;
  3842. hold += input[next++] << bits;
  3843. bits += 8;
  3844. //---//
  3845. }
  3846. if (here_val < 16) {
  3847. //--- DROPBITS(here.bits) ---//
  3848. hold >>>= here_bits;
  3849. bits -= here_bits;
  3850. //---//
  3851. state.lens[state.have++] = here_val;
  3852. }
  3853. else {
  3854. if (here_val === 16) {
  3855. //=== NEEDBITS(here.bits + 2);
  3856. n = here_bits + 2;
  3857. while (bits < n) {
  3858. if (have === 0) { break inf_leave; }
  3859. have--;
  3860. hold += input[next++] << bits;
  3861. bits += 8;
  3862. }
  3863. //===//
  3864. //--- DROPBITS(here.bits) ---//
  3865. hold >>>= here_bits;
  3866. bits -= here_bits;
  3867. //---//
  3868. if (state.have === 0) {
  3869. strm.msg = 'invalid bit length repeat';
  3870. state.mode = BAD;
  3871. break;
  3872. }
  3873. len = state.lens[state.have - 1];
  3874. copy = 3 + (hold & 0x03);//BITS(2);
  3875. //--- DROPBITS(2) ---//
  3876. hold >>>= 2;
  3877. bits -= 2;
  3878. //---//
  3879. }
  3880. else if (here_val === 17) {
  3881. //=== NEEDBITS(here.bits + 3);
  3882. n = here_bits + 3;
  3883. while (bits < n) {
  3884. if (have === 0) { break inf_leave; }
  3885. have--;
  3886. hold += input[next++] << bits;
  3887. bits += 8;
  3888. }
  3889. //===//
  3890. //--- DROPBITS(here.bits) ---//
  3891. hold >>>= here_bits;
  3892. bits -= here_bits;
  3893. //---//
  3894. len = 0;
  3895. copy = 3 + (hold & 0x07);//BITS(3);
  3896. //--- DROPBITS(3) ---//
  3897. hold >>>= 3;
  3898. bits -= 3;
  3899. //---//
  3900. }
  3901. else {
  3902. //=== NEEDBITS(here.bits + 7);
  3903. n = here_bits + 7;
  3904. while (bits < n) {
  3905. if (have === 0) { break inf_leave; }
  3906. have--;
  3907. hold += input[next++] << bits;
  3908. bits += 8;
  3909. }
  3910. //===//
  3911. //--- DROPBITS(here.bits) ---//
  3912. hold >>>= here_bits;
  3913. bits -= here_bits;
  3914. //---//
  3915. len = 0;
  3916. copy = 11 + (hold & 0x7f);//BITS(7);
  3917. //--- DROPBITS(7) ---//
  3918. hold >>>= 7;
  3919. bits -= 7;
  3920. //---//
  3921. }
  3922. if (state.have + copy > state.nlen + state.ndist) {
  3923. strm.msg = 'invalid bit length repeat';
  3924. state.mode = BAD;
  3925. break;
  3926. }
  3927. while (copy--) {
  3928. state.lens[state.have++] = len;
  3929. }
  3930. }
  3931. }
  3932. /* handle error breaks in while */
  3933. if (state.mode === BAD) { break; }
  3934. /* check for end-of-block code (better have one) */
  3935. if (state.lens[256] === 0) {
  3936. strm.msg = 'invalid code -- missing end-of-block';
  3937. state.mode = BAD;
  3938. break;
  3939. }
  3940. /* build code tables -- note: do not change the lenbits or distbits
  3941. values here (9 and 6) without reading the comments in inftrees.h
  3942. concerning the ENOUGH constants, which depend on those values */
  3943. state.lenbits = 9;
  3944. opts = { bits: state.lenbits };
  3945. ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
  3946. // We have separate tables & no pointers. 2 commented lines below not needed.
  3947. // state.next_index = opts.table_index;
  3948. state.lenbits = opts.bits;
  3949. // state.lencode = state.next;
  3950. if (ret) {
  3951. strm.msg = 'invalid literal/lengths set';
  3952. state.mode = BAD;
  3953. break;
  3954. }
  3955. state.distbits = 6;
  3956. //state.distcode.copy(state.codes);
  3957. // Switch to use dynamic table
  3958. state.distcode = state.distdyn;
  3959. opts = { bits: state.distbits };
  3960. ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
  3961. // We have separate tables & no pointers. 2 commented lines below not needed.
  3962. // state.next_index = opts.table_index;
  3963. state.distbits = opts.bits;
  3964. // state.distcode = state.next;
  3965. if (ret) {
  3966. strm.msg = 'invalid distances set';
  3967. state.mode = BAD;
  3968. break;
  3969. }
  3970. //Tracev((stderr, 'inflate: codes ok\n'));
  3971. state.mode = LEN_;
  3972. if (flush === Z_TREES) { break inf_leave; }
  3973. /* falls through */
  3974. case LEN_:
  3975. state.mode = LEN;
  3976. /* falls through */
  3977. case LEN:
  3978. if (have >= 6 && left >= 258) {
  3979. //--- RESTORE() ---
  3980. strm.next_out = put;
  3981. strm.avail_out = left;
  3982. strm.next_in = next;
  3983. strm.avail_in = have;
  3984. state.hold = hold;
  3985. state.bits = bits;
  3986. //---
  3987. inflate_fast(strm, _out);
  3988. //--- LOAD() ---
  3989. put = strm.next_out;
  3990. output = strm.output;
  3991. left = strm.avail_out;
  3992. next = strm.next_in;
  3993. input = strm.input;
  3994. have = strm.avail_in;
  3995. hold = state.hold;
  3996. bits = state.bits;
  3997. //---
  3998. if (state.mode === TYPE) {
  3999. state.back = -1;
  4000. }
  4001. break;
  4002. }
  4003. state.back = 0;
  4004. for (;;) {
  4005. here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/
  4006. here_bits = here >>> 24;
  4007. here_op = (here >>> 16) & 0xff;
  4008. here_val = here & 0xffff;
  4009. if (here_bits <= bits) { break; }
  4010. //--- PULLBYTE() ---//
  4011. if (have === 0) { break inf_leave; }
  4012. have--;
  4013. hold += input[next++] << bits;
  4014. bits += 8;
  4015. //---//
  4016. }
  4017. if (here_op && (here_op & 0xf0) === 0) {
  4018. last_bits = here_bits;
  4019. last_op = here_op;
  4020. last_val = here_val;
  4021. for (;;) {
  4022. here = state.lencode[last_val +
  4023. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  4024. here_bits = here >>> 24;
  4025. here_op = (here >>> 16) & 0xff;
  4026. here_val = here & 0xffff;
  4027. if ((last_bits + here_bits) <= bits) { break; }
  4028. //--- PULLBYTE() ---//
  4029. if (have === 0) { break inf_leave; }
  4030. have--;
  4031. hold += input[next++] << bits;
  4032. bits += 8;
  4033. //---//
  4034. }
  4035. //--- DROPBITS(last.bits) ---//
  4036. hold >>>= last_bits;
  4037. bits -= last_bits;
  4038. //---//
  4039. state.back += last_bits;
  4040. }
  4041. //--- DROPBITS(here.bits) ---//
  4042. hold >>>= here_bits;
  4043. bits -= here_bits;
  4044. //---//
  4045. state.back += here_bits;
  4046. state.length = here_val;
  4047. if (here_op === 0) {
  4048. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  4049. // "inflate: literal '%c'\n" :
  4050. // "inflate: literal 0x%02x\n", here.val));
  4051. state.mode = LIT;
  4052. break;
  4053. }
  4054. if (here_op & 32) {
  4055. //Tracevv((stderr, "inflate: end of block\n"));
  4056. state.back = -1;
  4057. state.mode = TYPE;
  4058. break;
  4059. }
  4060. if (here_op & 64) {
  4061. strm.msg = 'invalid literal/length code';
  4062. state.mode = BAD;
  4063. break;
  4064. }
  4065. state.extra = here_op & 15;
  4066. state.mode = LENEXT;
  4067. /* falls through */
  4068. case LENEXT:
  4069. if (state.extra) {
  4070. //=== NEEDBITS(state.extra);
  4071. n = state.extra;
  4072. while (bits < n) {
  4073. if (have === 0) { break inf_leave; }
  4074. have--;
  4075. hold += input[next++] << bits;
  4076. bits += 8;
  4077. }
  4078. //===//
  4079. state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  4080. //--- DROPBITS(state.extra) ---//
  4081. hold >>>= state.extra;
  4082. bits -= state.extra;
  4083. //---//
  4084. state.back += state.extra;
  4085. }
  4086. //Tracevv((stderr, "inflate: length %u\n", state.length));
  4087. state.was = state.length;
  4088. state.mode = DIST;
  4089. /* falls through */
  4090. case DIST:
  4091. for (;;) {
  4092. here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/
  4093. here_bits = here >>> 24;
  4094. here_op = (here >>> 16) & 0xff;
  4095. here_val = here & 0xffff;
  4096. if ((here_bits) <= bits) { break; }
  4097. //--- PULLBYTE() ---//
  4098. if (have === 0) { break inf_leave; }
  4099. have--;
  4100. hold += input[next++] << bits;
  4101. bits += 8;
  4102. //---//
  4103. }
  4104. if ((here_op & 0xf0) === 0) {
  4105. last_bits = here_bits;
  4106. last_op = here_op;
  4107. last_val = here_val;
  4108. for (;;) {
  4109. here = state.distcode[last_val +
  4110. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  4111. here_bits = here >>> 24;
  4112. here_op = (here >>> 16) & 0xff;
  4113. here_val = here & 0xffff;
  4114. if ((last_bits + here_bits) <= bits) { break; }
  4115. //--- PULLBYTE() ---//
  4116. if (have === 0) { break inf_leave; }
  4117. have--;
  4118. hold += input[next++] << bits;
  4119. bits += 8;
  4120. //---//
  4121. }
  4122. //--- DROPBITS(last.bits) ---//
  4123. hold >>>= last_bits;
  4124. bits -= last_bits;
  4125. //---//
  4126. state.back += last_bits;
  4127. }
  4128. //--- DROPBITS(here.bits) ---//
  4129. hold >>>= here_bits;
  4130. bits -= here_bits;
  4131. //---//
  4132. state.back += here_bits;
  4133. if (here_op & 64) {
  4134. strm.msg = 'invalid distance code';
  4135. state.mode = BAD;
  4136. break;
  4137. }
  4138. state.offset = here_val;
  4139. state.extra = (here_op) & 15;
  4140. state.mode = DISTEXT;
  4141. /* falls through */
  4142. case DISTEXT:
  4143. if (state.extra) {
  4144. //=== NEEDBITS(state.extra);
  4145. n = state.extra;
  4146. while (bits < n) {
  4147. if (have === 0) { break inf_leave; }
  4148. have--;
  4149. hold += input[next++] << bits;
  4150. bits += 8;
  4151. }
  4152. //===//
  4153. state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  4154. //--- DROPBITS(state.extra) ---//
  4155. hold >>>= state.extra;
  4156. bits -= state.extra;
  4157. //---//
  4158. state.back += state.extra;
  4159. }
  4160. //#ifdef INFLATE_STRICT
  4161. if (state.offset > state.dmax) {
  4162. strm.msg = 'invalid distance too far back';
  4163. state.mode = BAD;
  4164. break;
  4165. }
  4166. //#endif
  4167. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  4168. state.mode = MATCH;
  4169. /* falls through */
  4170. case MATCH:
  4171. if (left === 0) { break inf_leave; }
  4172. copy = _out - left;
  4173. if (state.offset > copy) { /* copy from window */
  4174. copy = state.offset - copy;
  4175. if (copy > state.whave) {
  4176. if (state.sane) {
  4177. strm.msg = 'invalid distance too far back';
  4178. state.mode = BAD;
  4179. break;
  4180. }
  4181. // (!) This block is disabled in zlib defailts,
  4182. // don't enable it for binary compatibility
  4183. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  4184. // Trace((stderr, "inflate.c too far\n"));
  4185. // copy -= state.whave;
  4186. // if (copy > state.length) { copy = state.length; }
  4187. // if (copy > left) { copy = left; }
  4188. // left -= copy;
  4189. // state.length -= copy;
  4190. // do {
  4191. // output[put++] = 0;
  4192. // } while (--copy);
  4193. // if (state.length === 0) { state.mode = LEN; }
  4194. // break;
  4195. //#endif
  4196. }
  4197. if (copy > state.wnext) {
  4198. copy -= state.wnext;
  4199. from = state.wsize - copy;
  4200. }
  4201. else {
  4202. from = state.wnext - copy;
  4203. }
  4204. if (copy > state.length) { copy = state.length; }
  4205. from_source = state.window;
  4206. }
  4207. else { /* copy from output */
  4208. from_source = output;
  4209. from = put - state.offset;
  4210. copy = state.length;
  4211. }
  4212. if (copy > left) { copy = left; }
  4213. left -= copy;
  4214. state.length -= copy;
  4215. do {
  4216. output[put++] = from_source[from++];
  4217. } while (--copy);
  4218. if (state.length === 0) { state.mode = LEN; }
  4219. break;
  4220. case LIT:
  4221. if (left === 0) { break inf_leave; }
  4222. output[put++] = state.length;
  4223. left--;
  4224. state.mode = LEN;
  4225. break;
  4226. case CHECK:
  4227. if (state.wrap) {
  4228. //=== NEEDBITS(32);
  4229. while (bits < 32) {
  4230. if (have === 0) { break inf_leave; }
  4231. have--;
  4232. // Use '|' insdead of '+' to make sure that result is signed
  4233. hold |= input[next++] << bits;
  4234. bits += 8;
  4235. }
  4236. //===//
  4237. _out -= left;
  4238. strm.total_out += _out;
  4239. state.total += _out;
  4240. if (_out) {
  4241. strm.adler = state.check =
  4242. /*UPDATE(state.check, put - _out, _out);*/
  4243. (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out));
  4244. }
  4245. _out = left;
  4246. // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
  4247. if ((state.flags ? hold : zswap32(hold)) !== state.check) {
  4248. strm.msg = 'incorrect data check';
  4249. state.mode = BAD;
  4250. break;
  4251. }
  4252. //=== INITBITS();
  4253. hold = 0;
  4254. bits = 0;
  4255. //===//
  4256. //Tracev((stderr, "inflate: check matches trailer\n"));
  4257. }
  4258. state.mode = LENGTH;
  4259. /* falls through */
  4260. case LENGTH:
  4261. if (state.wrap && state.flags) {
  4262. //=== NEEDBITS(32);
  4263. while (bits < 32) {
  4264. if (have === 0) { break inf_leave; }
  4265. have--;
  4266. hold += input[next++] << bits;
  4267. bits += 8;
  4268. }
  4269. //===//
  4270. if (hold !== (state.total & 0xffffffff)) {
  4271. strm.msg = 'incorrect length check';
  4272. state.mode = BAD;
  4273. break;
  4274. }
  4275. //=== INITBITS();
  4276. hold = 0;
  4277. bits = 0;
  4278. //===//
  4279. //Tracev((stderr, "inflate: length matches trailer\n"));
  4280. }
  4281. state.mode = DONE;
  4282. /* falls through */
  4283. case DONE:
  4284. ret = Z_STREAM_END;
  4285. break inf_leave;
  4286. case BAD:
  4287. ret = Z_DATA_ERROR;
  4288. break inf_leave;
  4289. case MEM:
  4290. return Z_MEM_ERROR;
  4291. case SYNC:
  4292. /* falls through */
  4293. default:
  4294. return Z_STREAM_ERROR;
  4295. }
  4296. }
  4297. // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  4298. /*
  4299. Return from inflate(), updating the total counts and the check value.
  4300. If there was no progress during the inflate() call, return a buffer
  4301. error. Call updatewindow() to create and/or update the window state.
  4302. Note: a memory error from inflate() is non-recoverable.
  4303. */
  4304. //--- RESTORE() ---
  4305. strm.next_out = put;
  4306. strm.avail_out = left;
  4307. strm.next_in = next;
  4308. strm.avail_in = have;
  4309. state.hold = hold;
  4310. state.bits = bits;
  4311. //---
  4312. if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
  4313. (state.mode < CHECK || flush !== Z_FINISH))) {
  4314. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) {
  4315. state.mode = MEM;
  4316. return Z_MEM_ERROR;
  4317. }
  4318. }
  4319. _in -= strm.avail_in;
  4320. _out -= strm.avail_out;
  4321. strm.total_in += _in;
  4322. strm.total_out += _out;
  4323. state.total += _out;
  4324. if (state.wrap && _out) {
  4325. strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/
  4326. (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out));
  4327. }
  4328. strm.data_type = state.bits + (state.last ? 64 : 0) +
  4329. (state.mode === TYPE ? 128 : 0) +
  4330. (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  4331. if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) {
  4332. ret = Z_BUF_ERROR;
  4333. }
  4334. return ret;
  4335. }
  4336. function inflateEnd(strm) {
  4337. if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
  4338. return Z_STREAM_ERROR;
  4339. }
  4340. var state = strm.state;
  4341. if (state.window) {
  4342. state.window = null;
  4343. }
  4344. strm.state = null;
  4345. return Z_OK;
  4346. }
  4347. function inflateGetHeader(strm, head) {
  4348. var state;
  4349. /* check state */
  4350. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  4351. state = strm.state;
  4352. if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
  4353. /* save header structure */
  4354. state.head = head;
  4355. head.done = false;
  4356. return Z_OK;
  4357. }
  4358. function inflateSetDictionary(strm, dictionary) {
  4359. var dictLength = dictionary.length;
  4360. var state;
  4361. var dictid;
  4362. var ret;
  4363. /* check state */
  4364. if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR; }
  4365. state = strm.state;
  4366. if (state.wrap !== 0 && state.mode !== DICT) {
  4367. return Z_STREAM_ERROR;
  4368. }
  4369. /* check for correct dictionary identifier */
  4370. if (state.mode === DICT) {
  4371. dictid = 1; /* adler32(0, null, 0)*/
  4372. /* dictid = adler32(dictid, dictionary, dictLength); */
  4373. dictid = adler32(dictid, dictionary, dictLength, 0);
  4374. if (dictid !== state.check) {
  4375. return Z_DATA_ERROR;
  4376. }
  4377. }
  4378. /* copy dictionary to window using updatewindow(), which will amend the
  4379. existing dictionary if appropriate */
  4380. ret = updatewindow(strm, dictionary, dictLength, dictLength);
  4381. if (ret) {
  4382. state.mode = MEM;
  4383. return Z_MEM_ERROR;
  4384. }
  4385. state.havedict = 1;
  4386. // Tracev((stderr, "inflate: dictionary set\n"));
  4387. return Z_OK;
  4388. }
  4389. exports.inflateReset = inflateReset;
  4390. exports.inflateReset2 = inflateReset2;
  4391. exports.inflateResetKeep = inflateResetKeep;
  4392. exports.inflateInit = inflateInit;
  4393. exports.inflateInit2 = inflateInit2;
  4394. exports.inflate = inflate;
  4395. exports.inflateEnd = inflateEnd;
  4396. exports.inflateGetHeader = inflateGetHeader;
  4397. exports.inflateSetDictionary = inflateSetDictionary;
  4398. exports.inflateInfo = 'pako inflate (from Nodeca project)';
  4399. /* Not implemented
  4400. exports.inflateCopy = inflateCopy;
  4401. exports.inflateGetDictionary = inflateGetDictionary;
  4402. exports.inflateMark = inflateMark;
  4403. exports.inflatePrime = inflatePrime;
  4404. exports.inflateSync = inflateSync;
  4405. exports.inflateSyncPoint = inflateSyncPoint;
  4406. exports.inflateUndermine = inflateUndermine;
  4407. */
  4408. },{"../utils/common":3,"./adler32":5,"./crc32":7,"./inffast":10,"./inftrees":12}],12:[function(require,module,exports){
  4409. 'use strict';
  4410. var utils = require('../utils/common');
  4411. var MAXBITS = 15;
  4412. var ENOUGH_LENS = 852;
  4413. var ENOUGH_DISTS = 592;
  4414. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  4415. var CODES = 0;
  4416. var LENS = 1;
  4417. var DISTS = 2;
  4418. var lbase = [ /* Length codes 257..285 base */
  4419. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  4420. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  4421. ];
  4422. var lext = [ /* Length codes 257..285 extra */
  4423. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  4424. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
  4425. ];
  4426. var dbase = [ /* Distance codes 0..29 base */
  4427. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  4428. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  4429. 8193, 12289, 16385, 24577, 0, 0
  4430. ];
  4431. var dext = [ /* Distance codes 0..29 extra */
  4432. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  4433. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  4434. 28, 28, 29, 29, 64, 64
  4435. ];
  4436. module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)
  4437. {
  4438. var bits = opts.bits;
  4439. //here = opts.here; /* table entry for duplication */
  4440. var len = 0; /* a code's length in bits */
  4441. var sym = 0; /* index of code symbols */
  4442. var min = 0, max = 0; /* minimum and maximum code lengths */
  4443. var root = 0; /* number of index bits for root table */
  4444. var curr = 0; /* number of index bits for current table */
  4445. var drop = 0; /* code bits to drop for sub-table */
  4446. var left = 0; /* number of prefix codes available */
  4447. var used = 0; /* code entries in table used */
  4448. var huff = 0; /* Huffman code */
  4449. var incr; /* for incrementing code, index */
  4450. var fill; /* index for replicating entries */
  4451. var low; /* low bits for current root entry */
  4452. var mask; /* mask for low root bits */
  4453. var next; /* next available space in table */
  4454. var base = null; /* base value table to use */
  4455. var base_index = 0;
  4456. // var shoextra; /* extra bits table to use */
  4457. var end; /* use base and extra for symbol > end */
  4458. var count = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */
  4459. var offs = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */
  4460. var extra = null;
  4461. var extra_index = 0;
  4462. var here_bits, here_op, here_val;
  4463. /*
  4464. Process a set of code lengths to create a canonical Huffman code. The
  4465. code lengths are lens[0..codes-1]. Each length corresponds to the
  4466. symbols 0..codes-1. The Huffman code is generated by first sorting the
  4467. symbols by length from short to long, and retaining the symbol order
  4468. for codes with equal lengths. Then the code starts with all zero bits
  4469. for the first code of the shortest length, and the codes are integer
  4470. increments for the same length, and zeros are appended as the length
  4471. increases. For the deflate format, these bits are stored backwards
  4472. from their more natural integer increment ordering, and so when the
  4473. decoding tables are built in the large loop below, the integer codes
  4474. are incremented backwards.
  4475. This routine assumes, but does not check, that all of the entries in
  4476. lens[] are in the range 0..MAXBITS. The caller must assure this.
  4477. 1..MAXBITS is interpreted as that code length. zero means that that
  4478. symbol does not occur in this code.
  4479. The codes are sorted by computing a count of codes for each length,
  4480. creating from that a table of starting indices for each length in the
  4481. sorted table, and then entering the symbols in order in the sorted
  4482. table. The sorted table is work[], with that space being provided by
  4483. the caller.
  4484. The length counts are used for other purposes as well, i.e. finding
  4485. the minimum and maximum length codes, determining if there are any
  4486. codes at all, checking for a valid set of lengths, and looking ahead
  4487. at length counts to determine sub-table sizes when building the
  4488. decoding tables.
  4489. */
  4490. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  4491. for (len = 0; len <= MAXBITS; len++) {
  4492. count[len] = 0;
  4493. }
  4494. for (sym = 0; sym < codes; sym++) {
  4495. count[lens[lens_index + sym]]++;
  4496. }
  4497. /* bound code lengths, force root to be within code lengths */
  4498. root = bits;
  4499. for (max = MAXBITS; max >= 1; max--) {
  4500. if (count[max] !== 0) { break; }
  4501. }
  4502. if (root > max) {
  4503. root = max;
  4504. }
  4505. if (max === 0) { /* no symbols to code at all */
  4506. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  4507. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  4508. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  4509. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  4510. //table.op[opts.table_index] = 64;
  4511. //table.bits[opts.table_index] = 1;
  4512. //table.val[opts.table_index++] = 0;
  4513. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  4514. opts.bits = 1;
  4515. return 0; /* no symbols, but wait for decoding to report error */
  4516. }
  4517. for (min = 1; min < max; min++) {
  4518. if (count[min] !== 0) { break; }
  4519. }
  4520. if (root < min) {
  4521. root = min;
  4522. }
  4523. /* check for an over-subscribed or incomplete set of lengths */
  4524. left = 1;
  4525. for (len = 1; len <= MAXBITS; len++) {
  4526. left <<= 1;
  4527. left -= count[len];
  4528. if (left < 0) {
  4529. return -1;
  4530. } /* over-subscribed */
  4531. }
  4532. if (left > 0 && (type === CODES || max !== 1)) {
  4533. return -1; /* incomplete set */
  4534. }
  4535. /* generate offsets into symbol table for each length for sorting */
  4536. offs[1] = 0;
  4537. for (len = 1; len < MAXBITS; len++) {
  4538. offs[len + 1] = offs[len] + count[len];
  4539. }
  4540. /* sort symbols by length, by symbol order within each length */
  4541. for (sym = 0; sym < codes; sym++) {
  4542. if (lens[lens_index + sym] !== 0) {
  4543. work[offs[lens[lens_index + sym]]++] = sym;
  4544. }
  4545. }
  4546. /*
  4547. Create and fill in decoding tables. In this loop, the table being
  4548. filled is at next and has curr index bits. The code being used is huff
  4549. with length len. That code is converted to an index by dropping drop
  4550. bits off of the bottom. For codes where len is less than drop + curr,
  4551. those top drop + curr - len bits are incremented through all values to
  4552. fill the table with replicated entries.
  4553. root is the number of index bits for the root table. When len exceeds
  4554. root, sub-tables are created pointed to by the root entry with an index
  4555. of the low root bits of huff. This is saved in low to check for when a
  4556. new sub-table should be started. drop is zero when the root table is
  4557. being filled, and drop is root when sub-tables are being filled.
  4558. When a new sub-table is needed, it is necessary to look ahead in the
  4559. code lengths to determine what size sub-table is needed. The length
  4560. counts are used for this, and so count[] is decremented as codes are
  4561. entered in the tables.
  4562. used keeps track of how many table entries have been allocated from the
  4563. provided *table space. It is checked for LENS and DIST tables against
  4564. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  4565. the initial root table size constants. See the comments in inftrees.h
  4566. for more information.
  4567. sym increments through all symbols, and the loop terminates when
  4568. all codes of length max, i.e. all codes, have been processed. This
  4569. routine permits incomplete codes, so another loop after this one fills
  4570. in the rest of the decoding tables with invalid code markers.
  4571. */
  4572. /* set up for code type */
  4573. // poor man optimization - use if-else instead of switch,
  4574. // to avoid deopts in old v8
  4575. if (type === CODES) {
  4576. base = extra = work; /* dummy value--not used */
  4577. end = 19;
  4578. } else if (type === LENS) {
  4579. base = lbase;
  4580. base_index -= 257;
  4581. extra = lext;
  4582. extra_index -= 257;
  4583. end = 256;
  4584. } else { /* DISTS */
  4585. base = dbase;
  4586. extra = dext;
  4587. end = -1;
  4588. }
  4589. /* initialize opts for loop */
  4590. huff = 0; /* starting code */
  4591. sym = 0; /* starting code symbol */
  4592. len = min; /* starting code length */
  4593. next = table_index; /* current table to fill in */
  4594. curr = root; /* current table index bits */
  4595. drop = 0; /* current bits to drop from code for index */
  4596. low = -1; /* trigger new sub-table when len > root */
  4597. used = 1 << root; /* use root table entries */
  4598. mask = used - 1; /* mask for comparing low */
  4599. /* check available table space */
  4600. if ((type === LENS && used > ENOUGH_LENS) ||
  4601. (type === DISTS && used > ENOUGH_DISTS)) {
  4602. return 1;
  4603. }
  4604. var i = 0;
  4605. /* process all codes and make table entries */
  4606. for (;;) {
  4607. i++;
  4608. /* create table entry */
  4609. here_bits = len - drop;
  4610. if (work[sym] < end) {
  4611. here_op = 0;
  4612. here_val = work[sym];
  4613. }
  4614. else if (work[sym] > end) {
  4615. here_op = extra[extra_index + work[sym]];
  4616. here_val = base[base_index + work[sym]];
  4617. }
  4618. else {
  4619. here_op = 32 + 64; /* end of block */
  4620. here_val = 0;
  4621. }
  4622. /* replicate for those indices with low len bits equal to huff */
  4623. incr = 1 << (len - drop);
  4624. fill = 1 << curr;
  4625. min = fill; /* save offset to next table */
  4626. do {
  4627. fill -= incr;
  4628. table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
  4629. } while (fill !== 0);
  4630. /* backwards increment the len-bit code huff */
  4631. incr = 1 << (len - 1);
  4632. while (huff & incr) {
  4633. incr >>= 1;
  4634. }
  4635. if (incr !== 0) {
  4636. huff &= incr - 1;
  4637. huff += incr;
  4638. } else {
  4639. huff = 0;
  4640. }
  4641. /* go to next symbol, update count, len */
  4642. sym++;
  4643. if (--count[len] === 0) {
  4644. if (len === max) { break; }
  4645. len = lens[lens_index + work[sym]];
  4646. }
  4647. /* create new sub-table if needed */
  4648. if (len > root && (huff & mask) !== low) {
  4649. /* if first time, transition to sub-tables */
  4650. if (drop === 0) {
  4651. drop = root;
  4652. }
  4653. /* increment past last table */
  4654. next += min; /* here min is 1 << curr */
  4655. /* determine length of next table */
  4656. curr = len - drop;
  4657. left = 1 << curr;
  4658. while (curr + drop < max) {
  4659. left -= count[curr + drop];
  4660. if (left <= 0) { break; }
  4661. curr++;
  4662. left <<= 1;
  4663. }
  4664. /* check for enough space */
  4665. used += 1 << curr;
  4666. if ((type === LENS && used > ENOUGH_LENS) ||
  4667. (type === DISTS && used > ENOUGH_DISTS)) {
  4668. return 1;
  4669. }
  4670. /* point entry in root table to sub-table */
  4671. low = huff & mask;
  4672. /*table.op[low] = curr;
  4673. table.bits[low] = root;
  4674. table.val[low] = next - opts.table_index;*/
  4675. table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
  4676. }
  4677. }
  4678. /* fill in remaining table entry if code is incomplete (guaranteed to have
  4679. at most one remaining entry, since if the code is incomplete, the
  4680. maximum code length that was allowed to get this far is one bit) */
  4681. if (huff !== 0) {
  4682. //table.op[next + huff] = 64; /* invalid code marker */
  4683. //table.bits[next + huff] = len - drop;
  4684. //table.val[next + huff] = 0;
  4685. table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
  4686. }
  4687. /* set return parameters */
  4688. //opts.table_index += used;
  4689. opts.bits = root;
  4690. return 0;
  4691. };
  4692. },{"../utils/common":3}],13:[function(require,module,exports){
  4693. 'use strict';
  4694. module.exports = {
  4695. 2: 'need dictionary', /* Z_NEED_DICT 2 */
  4696. 1: 'stream end', /* Z_STREAM_END 1 */
  4697. 0: '', /* Z_OK 0 */
  4698. '-1': 'file error', /* Z_ERRNO (-1) */
  4699. '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
  4700. '-3': 'data error', /* Z_DATA_ERROR (-3) */
  4701. '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
  4702. '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
  4703. '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
  4704. };
  4705. },{}],14:[function(require,module,exports){
  4706. 'use strict';
  4707. var utils = require('../utils/common');
  4708. /* Public constants ==========================================================*/
  4709. /* ===========================================================================*/
  4710. //var Z_FILTERED = 1;
  4711. //var Z_HUFFMAN_ONLY = 2;
  4712. //var Z_RLE = 3;
  4713. var Z_FIXED = 4;
  4714. //var Z_DEFAULT_STRATEGY = 0;
  4715. /* Possible values of the data_type field (though see inflate()) */
  4716. var Z_BINARY = 0;
  4717. var Z_TEXT = 1;
  4718. //var Z_ASCII = 1; // = Z_TEXT
  4719. var Z_UNKNOWN = 2;
  4720. /*============================================================================*/
  4721. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  4722. // From zutil.h
  4723. var STORED_BLOCK = 0;
  4724. var STATIC_TREES = 1;
  4725. var DYN_TREES = 2;
  4726. /* The three kinds of block type */
  4727. var MIN_MATCH = 3;
  4728. var MAX_MATCH = 258;
  4729. /* The minimum and maximum match lengths */
  4730. // From deflate.h
  4731. /* ===========================================================================
  4732. * Internal compression state.
  4733. */
  4734. var LENGTH_CODES = 29;
  4735. /* number of length codes, not counting the special END_BLOCK code */
  4736. var LITERALS = 256;
  4737. /* number of literal bytes 0..255 */
  4738. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  4739. /* number of Literal or Length codes, including the END_BLOCK code */
  4740. var D_CODES = 30;
  4741. /* number of distance codes */
  4742. var BL_CODES = 19;
  4743. /* number of codes used to transfer the bit lengths */
  4744. var HEAP_SIZE = 2 * L_CODES + 1;
  4745. /* maximum heap size */
  4746. var MAX_BITS = 15;
  4747. /* All codes must not exceed MAX_BITS bits */
  4748. var Buf_size = 16;
  4749. /* size of bit buffer in bi_buf */
  4750. /* ===========================================================================
  4751. * Constants
  4752. */
  4753. var MAX_BL_BITS = 7;
  4754. /* Bit length codes must not exceed MAX_BL_BITS bits */
  4755. var END_BLOCK = 256;
  4756. /* end of block literal code */
  4757. var REP_3_6 = 16;
  4758. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  4759. var REPZ_3_10 = 17;
  4760. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  4761. var REPZ_11_138 = 18;
  4762. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  4763. /* eslint-disable comma-spacing,array-bracket-spacing */
  4764. var extra_lbits = /* extra bits for each length code */
  4765. [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,0];
  4766. var extra_dbits = /* extra bits for each distance code */
  4767. [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];
  4768. var extra_blbits = /* extra bits for each bit length code */
  4769. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7];
  4770. var bl_order =
  4771. [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];
  4772. /* eslint-enable comma-spacing,array-bracket-spacing */
  4773. /* The lengths of the bit length codes are sent in order of decreasing
  4774. * probability, to avoid transmitting the lengths for unused bit length codes.
  4775. */
  4776. /* ===========================================================================
  4777. * Local data. These are initialized only once.
  4778. */
  4779. // We pre-fill arrays with 0 to avoid uninitialized gaps
  4780. var DIST_CODE_LEN = 512; /* see definition of array dist_code below */
  4781. // !!!! Use flat array insdead of structure, Freq = i*2, Len = i*2+1
  4782. var static_ltree = new Array((L_CODES + 2) * 2);
  4783. zero(static_ltree);
  4784. /* The static literal tree. Since the bit lengths are imposed, there is no
  4785. * need for the L_CODES extra codes used during heap construction. However
  4786. * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  4787. * below).
  4788. */
  4789. var static_dtree = new Array(D_CODES * 2);
  4790. zero(static_dtree);
  4791. /* The static distance tree. (Actually a trivial tree since all codes use
  4792. * 5 bits.)
  4793. */
  4794. var _dist_code = new Array(DIST_CODE_LEN);
  4795. zero(_dist_code);
  4796. /* Distance codes. The first 256 values correspond to the distances
  4797. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  4798. * the 15 bit distances.
  4799. */
  4800. var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1);
  4801. zero(_length_code);
  4802. /* length code for each normalized match length (0 == MIN_MATCH) */
  4803. var base_length = new Array(LENGTH_CODES);
  4804. zero(base_length);
  4805. /* First normalized length for each code (0 = MIN_MATCH) */
  4806. var base_dist = new Array(D_CODES);
  4807. zero(base_dist);
  4808. /* First normalized distance for each code (0 = distance of 1) */
  4809. function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
  4810. this.static_tree = static_tree; /* static tree or NULL */
  4811. this.extra_bits = extra_bits; /* extra bits for each code or NULL */
  4812. this.extra_base = extra_base; /* base index for extra_bits */
  4813. this.elems = elems; /* max number of elements in the tree */
  4814. this.max_length = max_length; /* max bit length for the codes */
  4815. // show if `static_tree` has data or dummy - needed for monomorphic objects
  4816. this.has_stree = static_tree && static_tree.length;
  4817. }
  4818. var static_l_desc;
  4819. var static_d_desc;
  4820. var static_bl_desc;
  4821. function TreeDesc(dyn_tree, stat_desc) {
  4822. this.dyn_tree = dyn_tree; /* the dynamic tree */
  4823. this.max_code = 0; /* largest code with non zero frequency */
  4824. this.stat_desc = stat_desc; /* the corresponding static tree */
  4825. }
  4826. function d_code(dist) {
  4827. return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
  4828. }
  4829. /* ===========================================================================
  4830. * Output a short LSB first on the stream.
  4831. * IN assertion: there is enough room in pendingBuf.
  4832. */
  4833. function put_short(s, w) {
  4834. // put_byte(s, (uch)((w) & 0xff));
  4835. // put_byte(s, (uch)((ush)(w) >> 8));
  4836. s.pending_buf[s.pending++] = (w) & 0xff;
  4837. s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
  4838. }
  4839. /* ===========================================================================
  4840. * Send a value on a given number of bits.
  4841. * IN assertion: length <= 16 and value fits in length bits.
  4842. */
  4843. function send_bits(s, value, length) {
  4844. if (s.bi_valid > (Buf_size - length)) {
  4845. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  4846. put_short(s, s.bi_buf);
  4847. s.bi_buf = value >> (Buf_size - s.bi_valid);
  4848. s.bi_valid += length - Buf_size;
  4849. } else {
  4850. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  4851. s.bi_valid += length;
  4852. }
  4853. }
  4854. function send_code(s, c, tree) {
  4855. send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/);
  4856. }
  4857. /* ===========================================================================
  4858. * Reverse the first len bits of a code, using straightforward code (a faster
  4859. * method would use a table)
  4860. * IN assertion: 1 <= len <= 15
  4861. */
  4862. function bi_reverse(code, len) {
  4863. var res = 0;
  4864. do {
  4865. res |= code & 1;
  4866. code >>>= 1;
  4867. res <<= 1;
  4868. } while (--len > 0);
  4869. return res >>> 1;
  4870. }
  4871. /* ===========================================================================
  4872. * Flush the bit buffer, keeping at most 7 bits in it.
  4873. */
  4874. function bi_flush(s) {
  4875. if (s.bi_valid === 16) {
  4876. put_short(s, s.bi_buf);
  4877. s.bi_buf = 0;
  4878. s.bi_valid = 0;
  4879. } else if (s.bi_valid >= 8) {
  4880. s.pending_buf[s.pending++] = s.bi_buf & 0xff;
  4881. s.bi_buf >>= 8;
  4882. s.bi_valid -= 8;
  4883. }
  4884. }
  4885. /* ===========================================================================
  4886. * Compute the optimal bit lengths for a tree and update the total bit length
  4887. * for the current block.
  4888. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  4889. * above are the tree nodes sorted by increasing frequency.
  4890. * OUT assertions: the field len is set to the optimal bit length, the
  4891. * array bl_count contains the frequencies for each bit length.
  4892. * The length opt_len is updated; static_len is also updated if stree is
  4893. * not null.
  4894. */
  4895. function gen_bitlen(s, desc)
  4896. // deflate_state *s;
  4897. // tree_desc *desc; /* the tree descriptor */
  4898. {
  4899. var tree = desc.dyn_tree;
  4900. var max_code = desc.max_code;
  4901. var stree = desc.stat_desc.static_tree;
  4902. var has_stree = desc.stat_desc.has_stree;
  4903. var extra = desc.stat_desc.extra_bits;
  4904. var base = desc.stat_desc.extra_base;
  4905. var max_length = desc.stat_desc.max_length;
  4906. var h; /* heap index */
  4907. var n, m; /* iterate over the tree elements */
  4908. var bits; /* bit length */
  4909. var xbits; /* extra bits */
  4910. var f; /* frequency */
  4911. var overflow = 0; /* number of elements with bit length too large */
  4912. for (bits = 0; bits <= MAX_BITS; bits++) {
  4913. s.bl_count[bits] = 0;
  4914. }
  4915. /* In a first pass, compute the optimal bit lengths (which may
  4916. * overflow in the case of the bit length tree).
  4917. */
  4918. tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */
  4919. for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
  4920. n = s.heap[h];
  4921. bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
  4922. if (bits > max_length) {
  4923. bits = max_length;
  4924. overflow++;
  4925. }
  4926. tree[n * 2 + 1]/*.Len*/ = bits;
  4927. /* We overwrite tree[n].Dad which is no longer needed */
  4928. if (n > max_code) { continue; } /* not a leaf node */
  4929. s.bl_count[bits]++;
  4930. xbits = 0;
  4931. if (n >= base) {
  4932. xbits = extra[n - base];
  4933. }
  4934. f = tree[n * 2]/*.Freq*/;
  4935. s.opt_len += f * (bits + xbits);
  4936. if (has_stree) {
  4937. s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits);
  4938. }
  4939. }
  4940. if (overflow === 0) { return; }
  4941. // Trace((stderr,"\nbit length overflow\n"));
  4942. /* This happens for example on obj2 and pic of the Calgary corpus */
  4943. /* Find the first bit length which could increase: */
  4944. do {
  4945. bits = max_length - 1;
  4946. while (s.bl_count[bits] === 0) { bits--; }
  4947. s.bl_count[bits]--; /* move one leaf down the tree */
  4948. s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
  4949. s.bl_count[max_length]--;
  4950. /* The brother of the overflow item also moves one step up,
  4951. * but this does not affect bl_count[max_length]
  4952. */
  4953. overflow -= 2;
  4954. } while (overflow > 0);
  4955. /* Now recompute all bit lengths, scanning in increasing frequency.
  4956. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  4957. * lengths instead of fixing only the wrong ones. This idea is taken
  4958. * from 'ar' written by Haruhiko Okumura.)
  4959. */
  4960. for (bits = max_length; bits !== 0; bits--) {
  4961. n = s.bl_count[bits];
  4962. while (n !== 0) {
  4963. m = s.heap[--h];
  4964. if (m > max_code) { continue; }
  4965. if (tree[m * 2 + 1]/*.Len*/ !== bits) {
  4966. // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  4967. s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/;
  4968. tree[m * 2 + 1]/*.Len*/ = bits;
  4969. }
  4970. n--;
  4971. }
  4972. }
  4973. }
  4974. /* ===========================================================================
  4975. * Generate the codes for a given tree and bit counts (which need not be
  4976. * optimal).
  4977. * IN assertion: the array bl_count contains the bit length statistics for
  4978. * the given tree and the field len is set for all tree elements.
  4979. * OUT assertion: the field code is set for all tree elements of non
  4980. * zero code length.
  4981. */
  4982. function gen_codes(tree, max_code, bl_count)
  4983. // ct_data *tree; /* the tree to decorate */
  4984. // int max_code; /* largest code with non zero frequency */
  4985. // ushf *bl_count; /* number of codes at each bit length */
  4986. {
  4987. var next_code = new Array(MAX_BITS + 1); /* next code value for each bit length */
  4988. var code = 0; /* running code value */
  4989. var bits; /* bit index */
  4990. var n; /* code index */
  4991. /* The distribution counts are first used to generate the code values
  4992. * without bit reversal.
  4993. */
  4994. for (bits = 1; bits <= MAX_BITS; bits++) {
  4995. next_code[bits] = code = (code + bl_count[bits - 1]) << 1;
  4996. }
  4997. /* Check that the bit counts in bl_count are consistent. The last code
  4998. * must be all ones.
  4999. */
  5000. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  5001. // "inconsistent bit counts");
  5002. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  5003. for (n = 0; n <= max_code; n++) {
  5004. var len = tree[n * 2 + 1]/*.Len*/;
  5005. if (len === 0) { continue; }
  5006. /* Now reverse the bits */
  5007. tree[n * 2]/*.Code*/ = bi_reverse(next_code[len]++, len);
  5008. //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  5009. // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  5010. }
  5011. }
  5012. /* ===========================================================================
  5013. * Initialize the various 'constant' tables.
  5014. */
  5015. function tr_static_init() {
  5016. var n; /* iterates over tree elements */
  5017. var bits; /* bit counter */
  5018. var length; /* length value */
  5019. var code; /* code value */
  5020. var dist; /* distance index */
  5021. var bl_count = new Array(MAX_BITS + 1);
  5022. /* number of codes at each bit length for an optimal tree */
  5023. // do check in _tr_init()
  5024. //if (static_init_done) return;
  5025. /* For some embedded targets, global variables are not initialized: */
  5026. /*#ifdef NO_INIT_GLOBAL_POINTERS
  5027. static_l_desc.static_tree = static_ltree;
  5028. static_l_desc.extra_bits = extra_lbits;
  5029. static_d_desc.static_tree = static_dtree;
  5030. static_d_desc.extra_bits = extra_dbits;
  5031. static_bl_desc.extra_bits = extra_blbits;
  5032. #endif*/
  5033. /* Initialize the mapping length (0..255) -> length code (0..28) */
  5034. length = 0;
  5035. for (code = 0; code < LENGTH_CODES - 1; code++) {
  5036. base_length[code] = length;
  5037. for (n = 0; n < (1 << extra_lbits[code]); n++) {
  5038. _length_code[length++] = code;
  5039. }
  5040. }
  5041. //Assert (length == 256, "tr_static_init: length != 256");
  5042. /* Note that the length 255 (match length 258) can be represented
  5043. * in two different ways: code 284 + 5 bits or code 285, so we
  5044. * overwrite length_code[255] to use the best encoding:
  5045. */
  5046. _length_code[length - 1] = code;
  5047. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  5048. dist = 0;
  5049. for (code = 0; code < 16; code++) {
  5050. base_dist[code] = dist;
  5051. for (n = 0; n < (1 << extra_dbits[code]); n++) {
  5052. _dist_code[dist++] = code;
  5053. }
  5054. }
  5055. //Assert (dist == 256, "tr_static_init: dist != 256");
  5056. dist >>= 7; /* from now on, all distances are divided by 128 */
  5057. for (; code < D_CODES; code++) {
  5058. base_dist[code] = dist << 7;
  5059. for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
  5060. _dist_code[256 + dist++] = code;
  5061. }
  5062. }
  5063. //Assert (dist == 256, "tr_static_init: 256+dist != 512");
  5064. /* Construct the codes of the static literal tree */
  5065. for (bits = 0; bits <= MAX_BITS; bits++) {
  5066. bl_count[bits] = 0;
  5067. }
  5068. n = 0;
  5069. while (n <= 143) {
  5070. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  5071. n++;
  5072. bl_count[8]++;
  5073. }
  5074. while (n <= 255) {
  5075. static_ltree[n * 2 + 1]/*.Len*/ = 9;
  5076. n++;
  5077. bl_count[9]++;
  5078. }
  5079. while (n <= 279) {
  5080. static_ltree[n * 2 + 1]/*.Len*/ = 7;
  5081. n++;
  5082. bl_count[7]++;
  5083. }
  5084. while (n <= 287) {
  5085. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  5086. n++;
  5087. bl_count[8]++;
  5088. }
  5089. /* Codes 286 and 287 do not exist, but we must include them in the
  5090. * tree construction to get a canonical Huffman tree (longest code
  5091. * all ones)
  5092. */
  5093. gen_codes(static_ltree, L_CODES + 1, bl_count);
  5094. /* The static distance tree is trivial: */
  5095. for (n = 0; n < D_CODES; n++) {
  5096. static_dtree[n * 2 + 1]/*.Len*/ = 5;
  5097. static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5);
  5098. }
  5099. // Now data ready and we can init static trees
  5100. static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
  5101. static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
  5102. static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
  5103. //static_init_done = true;
  5104. }
  5105. /* ===========================================================================
  5106. * Initialize a new block.
  5107. */
  5108. function init_block(s) {
  5109. var n; /* iterates over tree elements */
  5110. /* Initialize the trees. */
  5111. for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; }
  5112. for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; }
  5113. for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; }
  5114. s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1;
  5115. s.opt_len = s.static_len = 0;
  5116. s.last_lit = s.matches = 0;
  5117. }
  5118. /* ===========================================================================
  5119. * Flush the bit buffer and align the output on a byte boundary
  5120. */
  5121. function bi_windup(s)
  5122. {
  5123. if (s.bi_valid > 8) {
  5124. put_short(s, s.bi_buf);
  5125. } else if (s.bi_valid > 0) {
  5126. //put_byte(s, (Byte)s->bi_buf);
  5127. s.pending_buf[s.pending++] = s.bi_buf;
  5128. }
  5129. s.bi_buf = 0;
  5130. s.bi_valid = 0;
  5131. }
  5132. /* ===========================================================================
  5133. * Copy a stored block, storing first the length and its
  5134. * one's complement if requested.
  5135. */
  5136. function copy_block(s, buf, len, header)
  5137. //DeflateState *s;
  5138. //charf *buf; /* the input data */
  5139. //unsigned len; /* its length */
  5140. //int header; /* true if block header must be written */
  5141. {
  5142. bi_windup(s); /* align on byte boundary */
  5143. if (header) {
  5144. put_short(s, len);
  5145. put_short(s, ~len);
  5146. }
  5147. // while (len--) {
  5148. // put_byte(s, *buf++);
  5149. // }
  5150. utils.arraySet(s.pending_buf, s.window, buf, len, s.pending);
  5151. s.pending += len;
  5152. }
  5153. /* ===========================================================================
  5154. * Compares to subtrees, using the tree depth as tie breaker when
  5155. * the subtrees have equal frequency. This minimizes the worst case length.
  5156. */
  5157. function smaller(tree, n, m, depth) {
  5158. var _n2 = n * 2;
  5159. var _m2 = m * 2;
  5160. return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
  5161. (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
  5162. }
  5163. /* ===========================================================================
  5164. * Restore the heap property by moving down the tree starting at node k,
  5165. * exchanging a node with the smallest of its two sons if necessary, stopping
  5166. * when the heap property is re-established (each father smaller than its
  5167. * two sons).
  5168. */
  5169. function pqdownheap(s, tree, k)
  5170. // deflate_state *s;
  5171. // ct_data *tree; /* the tree to restore */
  5172. // int k; /* node to move down */
  5173. {
  5174. var v = s.heap[k];
  5175. var j = k << 1; /* left son of k */
  5176. while (j <= s.heap_len) {
  5177. /* Set j to the smallest of the two sons: */
  5178. if (j < s.heap_len &&
  5179. smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
  5180. j++;
  5181. }
  5182. /* Exit if v is smaller than both sons */
  5183. if (smaller(tree, v, s.heap[j], s.depth)) { break; }
  5184. /* Exchange v with the smallest son */
  5185. s.heap[k] = s.heap[j];
  5186. k = j;
  5187. /* And continue down the tree, setting j to the left son of k */
  5188. j <<= 1;
  5189. }
  5190. s.heap[k] = v;
  5191. }
  5192. // inlined manually
  5193. // var SMALLEST = 1;
  5194. /* ===========================================================================
  5195. * Send the block data compressed using the given Huffman trees
  5196. */
  5197. function compress_block(s, ltree, dtree)
  5198. // deflate_state *s;
  5199. // const ct_data *ltree; /* literal tree */
  5200. // const ct_data *dtree; /* distance tree */
  5201. {
  5202. var dist; /* distance of matched string */
  5203. var lc; /* match length or unmatched char (if dist == 0) */
  5204. var lx = 0; /* running index in l_buf */
  5205. var code; /* the code to send */
  5206. var extra; /* number of extra bits to send */
  5207. if (s.last_lit !== 0) {
  5208. do {
  5209. dist = (s.pending_buf[s.d_buf + lx * 2] << 8) | (s.pending_buf[s.d_buf + lx * 2 + 1]);
  5210. lc = s.pending_buf[s.l_buf + lx];
  5211. lx++;
  5212. if (dist === 0) {
  5213. send_code(s, lc, ltree); /* send a literal byte */
  5214. //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  5215. } else {
  5216. /* Here, lc is the match length - MIN_MATCH */
  5217. code = _length_code[lc];
  5218. send_code(s, code + LITERALS + 1, ltree); /* send the length code */
  5219. extra = extra_lbits[code];
  5220. if (extra !== 0) {
  5221. lc -= base_length[code];
  5222. send_bits(s, lc, extra); /* send the extra length bits */
  5223. }
  5224. dist--; /* dist is now the match distance - 1 */
  5225. code = d_code(dist);
  5226. //Assert (code < D_CODES, "bad d_code");
  5227. send_code(s, code, dtree); /* send the distance code */
  5228. extra = extra_dbits[code];
  5229. if (extra !== 0) {
  5230. dist -= base_dist[code];
  5231. send_bits(s, dist, extra); /* send the extra distance bits */
  5232. }
  5233. } /* literal or match pair ? */
  5234. /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  5235. //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
  5236. // "pendingBuf overflow");
  5237. } while (lx < s.last_lit);
  5238. }
  5239. send_code(s, END_BLOCK, ltree);
  5240. }
  5241. /* ===========================================================================
  5242. * Construct one Huffman tree and assigns the code bit strings and lengths.
  5243. * Update the total bit length for the current block.
  5244. * IN assertion: the field freq is set for all tree elements.
  5245. * OUT assertions: the fields len and code are set to the optimal bit length
  5246. * and corresponding code. The length opt_len is updated; static_len is
  5247. * also updated if stree is not null. The field max_code is set.
  5248. */
  5249. function build_tree(s, desc)
  5250. // deflate_state *s;
  5251. // tree_desc *desc; /* the tree descriptor */
  5252. {
  5253. var tree = desc.dyn_tree;
  5254. var stree = desc.stat_desc.static_tree;
  5255. var has_stree = desc.stat_desc.has_stree;
  5256. var elems = desc.stat_desc.elems;
  5257. var n, m; /* iterate over heap elements */
  5258. var max_code = -1; /* largest code with non zero frequency */
  5259. var node; /* new node being created */
  5260. /* Construct the initial heap, with least frequent element in
  5261. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  5262. * heap[0] is not used.
  5263. */
  5264. s.heap_len = 0;
  5265. s.heap_max = HEAP_SIZE;
  5266. for (n = 0; n < elems; n++) {
  5267. if (tree[n * 2]/*.Freq*/ !== 0) {
  5268. s.heap[++s.heap_len] = max_code = n;
  5269. s.depth[n] = 0;
  5270. } else {
  5271. tree[n * 2 + 1]/*.Len*/ = 0;
  5272. }
  5273. }
  5274. /* The pkzip format requires that at least one distance code exists,
  5275. * and that at least one bit should be sent even if there is only one
  5276. * possible code. So to avoid special checks later on we force at least
  5277. * two codes of non zero frequency.
  5278. */
  5279. while (s.heap_len < 2) {
  5280. node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
  5281. tree[node * 2]/*.Freq*/ = 1;
  5282. s.depth[node] = 0;
  5283. s.opt_len--;
  5284. if (has_stree) {
  5285. s.static_len -= stree[node * 2 + 1]/*.Len*/;
  5286. }
  5287. /* node is 0 or 1 so it does not have extra bits */
  5288. }
  5289. desc.max_code = max_code;
  5290. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  5291. * establish sub-heaps of increasing lengths:
  5292. */
  5293. for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
  5294. /* Construct the Huffman tree by repeatedly combining the least two
  5295. * frequent nodes.
  5296. */
  5297. node = elems; /* next internal node of the tree */
  5298. do {
  5299. //pqremove(s, tree, n); /* n = node of least frequency */
  5300. /*** pqremove ***/
  5301. n = s.heap[1/*SMALLEST*/];
  5302. s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
  5303. pqdownheap(s, tree, 1/*SMALLEST*/);
  5304. /***/
  5305. m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
  5306. s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
  5307. s.heap[--s.heap_max] = m;
  5308. /* Create a new node father of n and m */
  5309. tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
  5310. s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
  5311. tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node;
  5312. /* and insert the new node in the heap */
  5313. s.heap[1/*SMALLEST*/] = node++;
  5314. pqdownheap(s, tree, 1/*SMALLEST*/);
  5315. } while (s.heap_len >= 2);
  5316. s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
  5317. /* At this point, the fields freq and dad are set. We can now
  5318. * generate the bit lengths.
  5319. */
  5320. gen_bitlen(s, desc);
  5321. /* The field len is now set, we can generate the bit codes */
  5322. gen_codes(tree, max_code, s.bl_count);
  5323. }
  5324. /* ===========================================================================
  5325. * Scan a literal or distance tree to determine the frequencies of the codes
  5326. * in the bit length tree.
  5327. */
  5328. function scan_tree(s, tree, max_code)
  5329. // deflate_state *s;
  5330. // ct_data *tree; /* the tree to be scanned */
  5331. // int max_code; /* and its largest code of non zero frequency */
  5332. {
  5333. var n; /* iterates over all tree elements */
  5334. var prevlen = -1; /* last emitted length */
  5335. var curlen; /* length of current code */
  5336. var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  5337. var count = 0; /* repeat count of the current code */
  5338. var max_count = 7; /* max repeat count */
  5339. var min_count = 4; /* min repeat count */
  5340. if (nextlen === 0) {
  5341. max_count = 138;
  5342. min_count = 3;
  5343. }
  5344. tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */
  5345. for (n = 0; n <= max_code; n++) {
  5346. curlen = nextlen;
  5347. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  5348. if (++count < max_count && curlen === nextlen) {
  5349. continue;
  5350. } else if (count < min_count) {
  5351. s.bl_tree[curlen * 2]/*.Freq*/ += count;
  5352. } else if (curlen !== 0) {
  5353. if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
  5354. s.bl_tree[REP_3_6 * 2]/*.Freq*/++;
  5355. } else if (count <= 10) {
  5356. s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++;
  5357. } else {
  5358. s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++;
  5359. }
  5360. count = 0;
  5361. prevlen = curlen;
  5362. if (nextlen === 0) {
  5363. max_count = 138;
  5364. min_count = 3;
  5365. } else if (curlen === nextlen) {
  5366. max_count = 6;
  5367. min_count = 3;
  5368. } else {
  5369. max_count = 7;
  5370. min_count = 4;
  5371. }
  5372. }
  5373. }
  5374. /* ===========================================================================
  5375. * Send a literal or distance tree in compressed form, using the codes in
  5376. * bl_tree.
  5377. */
  5378. function send_tree(s, tree, max_code)
  5379. // deflate_state *s;
  5380. // ct_data *tree; /* the tree to be scanned */
  5381. // int max_code; /* and its largest code of non zero frequency */
  5382. {
  5383. var n; /* iterates over all tree elements */
  5384. var prevlen = -1; /* last emitted length */
  5385. var curlen; /* length of current code */
  5386. var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  5387. var count = 0; /* repeat count of the current code */
  5388. var max_count = 7; /* max repeat count */
  5389. var min_count = 4; /* min repeat count */
  5390. /* tree[max_code+1].Len = -1; */ /* guard already set */
  5391. if (nextlen === 0) {
  5392. max_count = 138;
  5393. min_count = 3;
  5394. }
  5395. for (n = 0; n <= max_code; n++) {
  5396. curlen = nextlen;
  5397. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  5398. if (++count < max_count && curlen === nextlen) {
  5399. continue;
  5400. } else if (count < min_count) {
  5401. do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
  5402. } else if (curlen !== 0) {
  5403. if (curlen !== prevlen) {
  5404. send_code(s, curlen, s.bl_tree);
  5405. count--;
  5406. }
  5407. //Assert(count >= 3 && count <= 6, " 3_6?");
  5408. send_code(s, REP_3_6, s.bl_tree);
  5409. send_bits(s, count - 3, 2);
  5410. } else if (count <= 10) {
  5411. send_code(s, REPZ_3_10, s.bl_tree);
  5412. send_bits(s, count - 3, 3);
  5413. } else {
  5414. send_code(s, REPZ_11_138, s.bl_tree);
  5415. send_bits(s, count - 11, 7);
  5416. }
  5417. count = 0;
  5418. prevlen = curlen;
  5419. if (nextlen === 0) {
  5420. max_count = 138;
  5421. min_count = 3;
  5422. } else if (curlen === nextlen) {
  5423. max_count = 6;
  5424. min_count = 3;
  5425. } else {
  5426. max_count = 7;
  5427. min_count = 4;
  5428. }
  5429. }
  5430. }
  5431. /* ===========================================================================
  5432. * Construct the Huffman tree for the bit lengths and return the index in
  5433. * bl_order of the last bit length code to send.
  5434. */
  5435. function build_bl_tree(s) {
  5436. var max_blindex; /* index of last bit length code of non zero freq */
  5437. /* Determine the bit length frequencies for literal and distance trees */
  5438. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  5439. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  5440. /* Build the bit length tree: */
  5441. build_tree(s, s.bl_desc);
  5442. /* opt_len now includes the length of the tree representations, except
  5443. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  5444. */
  5445. /* Determine the number of bit length codes to send. The pkzip format
  5446. * requires that at least 4 bit length codes be sent. (appnote.txt says
  5447. * 3 but the actual value used is 4.)
  5448. */
  5449. for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
  5450. if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) {
  5451. break;
  5452. }
  5453. }
  5454. /* Update opt_len to include the bit length tree and counts */
  5455. s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
  5456. //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  5457. // s->opt_len, s->static_len));
  5458. return max_blindex;
  5459. }
  5460. /* ===========================================================================
  5461. * Send the header for a block using dynamic Huffman trees: the counts, the
  5462. * lengths of the bit length codes, the literal tree and the distance tree.
  5463. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  5464. */
  5465. function send_all_trees(s, lcodes, dcodes, blcodes)
  5466. // deflate_state *s;
  5467. // int lcodes, dcodes, blcodes; /* number of codes for each tree */
  5468. {
  5469. var rank; /* index in bl_order */
  5470. //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  5471. //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  5472. // "too many codes");
  5473. //Tracev((stderr, "\nbl counts: "));
  5474. send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
  5475. send_bits(s, dcodes - 1, 5);
  5476. send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
  5477. for (rank = 0; rank < blcodes; rank++) {
  5478. //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  5479. send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3);
  5480. }
  5481. //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  5482. send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */
  5483. //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  5484. send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */
  5485. //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  5486. }
  5487. /* ===========================================================================
  5488. * Check if the data type is TEXT or BINARY, using the following algorithm:
  5489. * - TEXT if the two conditions below are satisfied:
  5490. * a) There are no non-portable control characters belonging to the
  5491. * "black list" (0..6, 14..25, 28..31).
  5492. * b) There is at least one printable character belonging to the
  5493. * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
  5494. * - BINARY otherwise.
  5495. * - The following partially-portable control characters form a
  5496. * "gray list" that is ignored in this detection algorithm:
  5497. * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  5498. * IN assertion: the fields Freq of dyn_ltree are set.
  5499. */
  5500. function detect_data_type(s) {
  5501. /* black_mask is the bit mask of black-listed bytes
  5502. * set bits 0..6, 14..25, and 28..31
  5503. * 0xf3ffc07f = binary 11110011111111111100000001111111
  5504. */
  5505. var black_mask = 0xf3ffc07f;
  5506. var n;
  5507. /* Check for non-textual ("black-listed") bytes. */
  5508. for (n = 0; n <= 31; n++, black_mask >>>= 1) {
  5509. if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) {
  5510. return Z_BINARY;
  5511. }
  5512. }
  5513. /* Check for textual ("white-listed") bytes. */
  5514. if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
  5515. s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
  5516. return Z_TEXT;
  5517. }
  5518. for (n = 32; n < LITERALS; n++) {
  5519. if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
  5520. return Z_TEXT;
  5521. }
  5522. }
  5523. /* There are no "black-listed" or "white-listed" bytes:
  5524. * this stream either is empty or has tolerated ("gray-listed") bytes only.
  5525. */
  5526. return Z_BINARY;
  5527. }
  5528. var static_init_done = false;
  5529. /* ===========================================================================
  5530. * Initialize the tree data structures for a new zlib stream.
  5531. */
  5532. function _tr_init(s)
  5533. {
  5534. if (!static_init_done) {
  5535. tr_static_init();
  5536. static_init_done = true;
  5537. }
  5538. s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
  5539. s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
  5540. s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
  5541. s.bi_buf = 0;
  5542. s.bi_valid = 0;
  5543. /* Initialize the first block of the first file: */
  5544. init_block(s);
  5545. }
  5546. /* ===========================================================================
  5547. * Send a stored block
  5548. */
  5549. function _tr_stored_block(s, buf, stored_len, last)
  5550. //DeflateState *s;
  5551. //charf *buf; /* input block */
  5552. //ulg stored_len; /* length of input block */
  5553. //int last; /* one if this is the last block for a file */
  5554. {
  5555. send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */
  5556. copy_block(s, buf, stored_len, true); /* with header */
  5557. }
  5558. /* ===========================================================================
  5559. * Send one empty static block to give enough lookahead for inflate.
  5560. * This takes 10 bits, of which 7 may remain in the bit buffer.
  5561. */
  5562. function _tr_align(s) {
  5563. send_bits(s, STATIC_TREES << 1, 3);
  5564. send_code(s, END_BLOCK, static_ltree);
  5565. bi_flush(s);
  5566. }
  5567. /* ===========================================================================
  5568. * Determine the best encoding for the current block: dynamic trees, static
  5569. * trees or store, and output the encoded block to the zip file.
  5570. */
  5571. function _tr_flush_block(s, buf, stored_len, last)
  5572. //DeflateState *s;
  5573. //charf *buf; /* input block, or NULL if too old */
  5574. //ulg stored_len; /* length of input block */
  5575. //int last; /* one if this is the last block for a file */
  5576. {
  5577. var opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  5578. var max_blindex = 0; /* index of last bit length code of non zero freq */
  5579. /* Build the Huffman trees unless a stored block is forced */
  5580. if (s.level > 0) {
  5581. /* Check if the file is binary or text */
  5582. if (s.strm.data_type === Z_UNKNOWN) {
  5583. s.strm.data_type = detect_data_type(s);
  5584. }
  5585. /* Construct the literal and distance trees */
  5586. build_tree(s, s.l_desc);
  5587. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  5588. // s->static_len));
  5589. build_tree(s, s.d_desc);
  5590. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  5591. // s->static_len));
  5592. /* At this point, opt_len and static_len are the total bit lengths of
  5593. * the compressed block data, excluding the tree representations.
  5594. */
  5595. /* Build the bit length tree for the above two trees, and get the index
  5596. * in bl_order of the last bit length code to send.
  5597. */
  5598. max_blindex = build_bl_tree(s);
  5599. /* Determine the best encoding. Compute the block lengths in bytes. */
  5600. opt_lenb = (s.opt_len + 3 + 7) >>> 3;
  5601. static_lenb = (s.static_len + 3 + 7) >>> 3;
  5602. // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  5603. // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  5604. // s->last_lit));
  5605. if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
  5606. } else {
  5607. // Assert(buf != (char*)0, "lost buf");
  5608. opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  5609. }
  5610. if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) {
  5611. /* 4: two words for the lengths */
  5612. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  5613. * Otherwise we can't have processed more than WSIZE input bytes since
  5614. * the last block flush, because compression would have been
  5615. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  5616. * transform a block into a stored block.
  5617. */
  5618. _tr_stored_block(s, buf, stored_len, last);
  5619. } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
  5620. send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
  5621. compress_block(s, static_ltree, static_dtree);
  5622. } else {
  5623. send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
  5624. send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
  5625. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  5626. }
  5627. // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  5628. /* The above check is made mod 2^32, for files larger than 512 MB
  5629. * and uLong implemented on 32 bits.
  5630. */
  5631. init_block(s);
  5632. if (last) {
  5633. bi_windup(s);
  5634. }
  5635. // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  5636. // s->compressed_len-7*last));
  5637. }
  5638. /* ===========================================================================
  5639. * Save the match info and tally the frequency counts. Return true if
  5640. * the current block must be flushed.
  5641. */
  5642. function _tr_tally(s, dist, lc)
  5643. // deflate_state *s;
  5644. // unsigned dist; /* distance of matched string */
  5645. // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  5646. {
  5647. //var out_length, in_length, dcode;
  5648. s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff;
  5649. s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
  5650. s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
  5651. s.last_lit++;
  5652. if (dist === 0) {
  5653. /* lc is the unmatched char */
  5654. s.dyn_ltree[lc * 2]/*.Freq*/++;
  5655. } else {
  5656. s.matches++;
  5657. /* Here, lc is the match length - MIN_MATCH */
  5658. dist--; /* dist = match distance - 1 */
  5659. //Assert((ush)dist < (ush)MAX_DIST(s) &&
  5660. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  5661. // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  5662. s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++;
  5663. s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
  5664. }
  5665. // (!) This block is disabled in zlib defailts,
  5666. // don't enable it for binary compatibility
  5667. //#ifdef TRUNCATE_BLOCK
  5668. // /* Try to guess if it is profitable to stop the current block here */
  5669. // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
  5670. // /* Compute an upper bound for the compressed length */
  5671. // out_length = s.last_lit*8;
  5672. // in_length = s.strstart - s.block_start;
  5673. //
  5674. // for (dcode = 0; dcode < D_CODES; dcode++) {
  5675. // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
  5676. // }
  5677. // out_length >>>= 3;
  5678. // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  5679. // // s->last_lit, in_length, out_length,
  5680. // // 100L - out_length*100L/in_length));
  5681. // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
  5682. // return true;
  5683. // }
  5684. // }
  5685. //#endif
  5686. return (s.last_lit === s.lit_bufsize - 1);
  5687. /* We avoid equality with lit_bufsize because of wraparound at 64K
  5688. * on 16 bit machines and because stored blocks are restricted to
  5689. * 64K-1 bytes.
  5690. */
  5691. }
  5692. exports._tr_init = _tr_init;
  5693. exports._tr_stored_block = _tr_stored_block;
  5694. exports._tr_flush_block = _tr_flush_block;
  5695. exports._tr_tally = _tr_tally;
  5696. exports._tr_align = _tr_align;
  5697. },{"../utils/common":3}],15:[function(require,module,exports){
  5698. 'use strict';
  5699. function ZStream() {
  5700. /* next input byte */
  5701. this.input = null; // JS specific, because we have no pointers
  5702. this.next_in = 0;
  5703. /* number of bytes available at input */
  5704. this.avail_in = 0;
  5705. /* total number of input bytes read so far */
  5706. this.total_in = 0;
  5707. /* next output byte should be put there */
  5708. this.output = null; // JS specific, because we have no pointers
  5709. this.next_out = 0;
  5710. /* remaining free space at output */
  5711. this.avail_out = 0;
  5712. /* total number of bytes output so far */
  5713. this.total_out = 0;
  5714. /* last error message, NULL if no error */
  5715. this.msg = ''/*Z_NULL*/;
  5716. /* not visible by applications */
  5717. this.state = null;
  5718. /* best guess about the data type: binary or text */
  5719. this.data_type = 2/*Z_UNKNOWN*/;
  5720. /* adler32 value of the uncompressed data */
  5721. this.adler = 0;
  5722. }
  5723. module.exports = ZStream;
  5724. },{}],"/":[function(require,module,exports){
  5725. // Top level file is just a mixin of submodules & constants
  5726. 'use strict';
  5727. var assign = require('./lib/utils/common').assign;
  5728. var deflate = require('./lib/deflate');
  5729. var inflate = require('./lib/inflate');
  5730. var constants = require('./lib/zlib/constants');
  5731. var pako = {};
  5732. assign(pako, deflate, inflate, constants);
  5733. module.exports = pako;
  5734. },{"./lib/deflate":1,"./lib/inflate":2,"./lib/utils/common":3,"./lib/zlib/constants":6}]},{},[])("/")
  5735. });