jszip.js 281 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000
  1. /*
  2. JSZip - A Javascript class for generating and reading zip files
  3. <http://stuartk.com/jszip>
  4. (c) 2009-2014 Stuart Knightley <stuart [at] stuartk.com>
  5. Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip/master/LICENSE.markdown.
  6. JSZip uses the library pako released under the MIT license :
  7. https://github.com/nodeca/pako/blob/master/LICENSE
  8. Note: since JSZip 3 removed critical functionality, this version assigns to the
  9. `JSZipSync` variable. Another JSZip version can be loaded in parallel.
  10. */
  11. (function(e){
  12. if("object"==typeof exports&&"undefined"!=typeof module&&"undefined"==typeof DO_NOT_EXPORT_JSZIP)module.exports=e();
  13. else if("function"==typeof define&&define.amd&&"undefined"==typeof DO_NOT_EXPORT_JSZIP){JSZipSync=e();define([],e);}
  14. else{
  15. var f;
  16. "undefined"!=typeof globalThis?f=globalThis:
  17. "undefined"!=typeof window?f=window:
  18. "undefined"!=typeof global?f=global:
  19. "undefined"!=typeof $ && $.global?f=$.global:
  20. "undefined"!=typeof self&&(f=self),f.JSZipSync=e()
  21. }
  22. }(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);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.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(_dereq_,module,exports){
  23. 'use strict';
  24. // private property
  25. var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  26. // public method for encoding
  27. exports.encode = function(input, utf8) {
  28. var output = "";
  29. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  30. var i = 0;
  31. while (i < input.length) {
  32. chr1 = input.charCodeAt(i++);
  33. chr2 = input.charCodeAt(i++);
  34. chr3 = input.charCodeAt(i++);
  35. enc1 = chr1 >> 2;
  36. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  37. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  38. enc4 = chr3 & 63;
  39. if (isNaN(chr2)) {
  40. enc3 = enc4 = 64;
  41. }
  42. else if (isNaN(chr3)) {
  43. enc4 = 64;
  44. }
  45. output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  46. }
  47. return output;
  48. };
  49. // public method for decoding
  50. exports.decode = function(input, utf8) {
  51. var output = "";
  52. var chr1, chr2, chr3;
  53. var enc1, enc2, enc3, enc4;
  54. var i = 0;
  55. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  56. while (i < input.length) {
  57. enc1 = _keyStr.indexOf(input.charAt(i++));
  58. enc2 = _keyStr.indexOf(input.charAt(i++));
  59. enc3 = _keyStr.indexOf(input.charAt(i++));
  60. enc4 = _keyStr.indexOf(input.charAt(i++));
  61. chr1 = (enc1 << 2) | (enc2 >> 4);
  62. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  63. chr3 = ((enc3 & 3) << 6) | enc4;
  64. output = output + String.fromCharCode(chr1);
  65. if (enc3 != 64) {
  66. output = output + String.fromCharCode(chr2);
  67. }
  68. if (enc4 != 64) {
  69. output = output + String.fromCharCode(chr3);
  70. }
  71. }
  72. return output;
  73. };
  74. },{}],2:[function(_dereq_,module,exports){
  75. 'use strict';
  76. function CompressedObject() {
  77. this.compressedSize = 0;
  78. this.uncompressedSize = 0;
  79. this.crc32 = 0;
  80. this.compressionMethod = null;
  81. this.compressedContent = null;
  82. }
  83. CompressedObject.prototype = {
  84. /**
  85. * Return the decompressed content in an unspecified format.
  86. * The format will depend on the decompressor.
  87. * @return {Object} the decompressed content.
  88. */
  89. getContent: function() {
  90. return null; // see implementation
  91. },
  92. /**
  93. * Return the compressed content in an unspecified format.
  94. * The format will depend on the compressed conten source.
  95. * @return {Object} the compressed content.
  96. */
  97. getCompressedContent: function() {
  98. return null; // see implementation
  99. }
  100. };
  101. module.exports = CompressedObject;
  102. },{}],3:[function(_dereq_,module,exports){
  103. 'use strict';
  104. exports.STORE = {
  105. magic: "\x00\x00",
  106. compress: function(content) {
  107. return content; // no compression
  108. },
  109. uncompress: function(content) {
  110. return content; // no compression
  111. },
  112. compressInputType: null,
  113. uncompressInputType: null
  114. };
  115. exports.DEFLATE = _dereq_('./flate');
  116. },{"./flate":8}],4:[function(_dereq_,module,exports){
  117. 'use strict';
  118. var utils = _dereq_('./utils');
  119. var table = [
  120. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
  121. 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
  122. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  123. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
  124. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
  125. 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  126. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
  127. 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
  128. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  129. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  130. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
  131. 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  132. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
  133. 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
  134. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  135. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
  136. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
  137. 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  138. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
  139. 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  140. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  141. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
  142. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
  143. 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  144. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
  145. 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
  146. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  147. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
  148. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
  149. 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  150. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
  151. 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
  152. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  153. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
  154. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
  155. 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  156. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
  157. 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
  158. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  159. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  160. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
  161. 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  162. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
  163. 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
  164. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  165. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
  166. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
  167. 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  168. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
  169. 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  170. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  171. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
  172. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
  173. 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  174. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
  175. 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
  176. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  177. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
  178. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
  179. 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  180. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
  181. 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
  182. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  183. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  184. ];
  185. /**
  186. *
  187. * Javascript crc32
  188. * http://www.webtoolkit.info/
  189. *
  190. */
  191. module.exports = function crc32(input, crc) {
  192. if (typeof input === "undefined" || !input.length) {
  193. return 0;
  194. }
  195. var isArray = utils.getTypeOf(input) !== "string";
  196. if (typeof(crc) == "undefined") {
  197. crc = 0;
  198. }
  199. var x = 0;
  200. var y = 0;
  201. var b = 0;
  202. crc = crc ^ (-1);
  203. for (var i = 0, iTop = input.length; i < iTop; i++) {
  204. b = isArray ? input[i] : input.charCodeAt(i);
  205. y = (crc ^ b) & 0xFF;
  206. x = table[y];
  207. crc = (crc >>> 8) ^ x;
  208. }
  209. return crc ^ (-1);
  210. };
  211. // vim: set shiftwidth=4 softtabstop=4:
  212. },{"./utils":21}],5:[function(_dereq_,module,exports){
  213. 'use strict';
  214. var utils = _dereq_('./utils');
  215. function DataReader(data) {
  216. this.data = null; // type : see implementation
  217. this.length = 0;
  218. this.index = 0;
  219. }
  220. DataReader.prototype = {
  221. /**
  222. * Check that the offset will not go too far.
  223. * @param {string} offset the additional offset to check.
  224. * @throws {Error} an Error if the offset is out of bounds.
  225. */
  226. checkOffset: function(offset) {
  227. this.checkIndex(this.index + offset);
  228. },
  229. /**
  230. * Check that the specifed index will not be too far.
  231. * @param {string} newIndex the index to check.
  232. * @throws {Error} an Error if the index is out of bounds.
  233. */
  234. checkIndex: function(newIndex) {
  235. if (this.length < newIndex || newIndex < 0) {
  236. throw new Error("End of data reached (data length = " + this.length + ", asked index = " + (newIndex) + "). Corrupted zip ?");
  237. }
  238. },
  239. /**
  240. * Change the index.
  241. * @param {number} newIndex The new index.
  242. * @throws {Error} if the new index is out of the data.
  243. */
  244. setIndex: function(newIndex) {
  245. this.checkIndex(newIndex);
  246. this.index = newIndex;
  247. },
  248. /**
  249. * Skip the next n bytes.
  250. * @param {number} n the number of bytes to skip.
  251. * @throws {Error} if the new index is out of the data.
  252. */
  253. skip: function(n) {
  254. this.setIndex(this.index + n);
  255. },
  256. /**
  257. * Get the byte at the specified index.
  258. * @param {number} i the index to use.
  259. * @return {number} a byte.
  260. */
  261. byteAt: function(i) {
  262. // see implementations
  263. },
  264. /**
  265. * Get the next number with a given byte size.
  266. * @param {number} size the number of bytes to read.
  267. * @return {number} the corresponding number.
  268. */
  269. readInt: function(size) {
  270. var result = 0,
  271. i;
  272. this.checkOffset(size);
  273. for (i = this.index + size - 1; i >= this.index; i--) {
  274. result = (result << 8) + this.byteAt(i);
  275. }
  276. this.index += size;
  277. return result;
  278. },
  279. /**
  280. * Get the next string with a given byte size.
  281. * @param {number} size the number of bytes to read.
  282. * @return {string} the corresponding string.
  283. */
  284. readString: function(size) {
  285. return utils.transformTo("string", this.readData(size));
  286. },
  287. /**
  288. * Get raw data without conversion, <size> bytes.
  289. * @param {number} size the number of bytes to read.
  290. * @return {Object} the raw data, implementation specific.
  291. */
  292. readData: function(size) {
  293. // see implementations
  294. },
  295. /**
  296. * Find the last occurence of a zip signature (4 bytes).
  297. * @param {string} sig the signature to find.
  298. * @return {number} the index of the last occurence, -1 if not found.
  299. */
  300. lastIndexOfSignature: function(sig) {
  301. // see implementations
  302. },
  303. /**
  304. * Get the next date.
  305. * @return {Date} the date.
  306. */
  307. readDate: function() {
  308. var dostime = this.readInt(4);
  309. return new Date(
  310. ((dostime >> 25) & 0x7f) + 1980, // year
  311. ((dostime >> 21) & 0x0f) - 1, // month
  312. (dostime >> 16) & 0x1f, // day
  313. (dostime >> 11) & 0x1f, // hour
  314. (dostime >> 5) & 0x3f, // minute
  315. (dostime & 0x1f) << 1); // second
  316. }
  317. };
  318. module.exports = DataReader;
  319. },{"./utils":21}],6:[function(_dereq_,module,exports){
  320. 'use strict';
  321. exports.base64 = false;
  322. exports.binary = false;
  323. exports.dir = false;
  324. exports.createFolders = false;
  325. exports.date = null;
  326. exports.compression = null;
  327. exports.comment = null;
  328. },{}],7:[function(_dereq_,module,exports){
  329. 'use strict';
  330. var utils = _dereq_('./utils');
  331. /**
  332. * @deprecated
  333. * This function will be removed in a future version without replacement.
  334. */
  335. exports.string2binary = function(str) {
  336. return utils.string2binary(str);
  337. };
  338. /**
  339. * @deprecated
  340. * This function will be removed in a future version without replacement.
  341. */
  342. exports.string2Uint8Array = function(str) {
  343. return utils.transformTo("uint8array", str);
  344. };
  345. /**
  346. * @deprecated
  347. * This function will be removed in a future version without replacement.
  348. */
  349. exports.uint8Array2String = function(array) {
  350. return utils.transformTo("string", array);
  351. };
  352. /**
  353. * @deprecated
  354. * This function will be removed in a future version without replacement.
  355. */
  356. exports.string2Blob = function(str) {
  357. var buffer = utils.transformTo("arraybuffer", str);
  358. return utils.arrayBuffer2Blob(buffer);
  359. };
  360. /**
  361. * @deprecated
  362. * This function will be removed in a future version without replacement.
  363. */
  364. exports.arrayBuffer2Blob = function(buffer) {
  365. return utils.arrayBuffer2Blob(buffer);
  366. };
  367. /**
  368. * @deprecated
  369. * This function will be removed in a future version without replacement.
  370. */
  371. exports.transformTo = function(outputType, input) {
  372. return utils.transformTo(outputType, input);
  373. };
  374. /**
  375. * @deprecated
  376. * This function will be removed in a future version without replacement.
  377. */
  378. exports.getTypeOf = function(input) {
  379. return utils.getTypeOf(input);
  380. };
  381. /**
  382. * @deprecated
  383. * This function will be removed in a future version without replacement.
  384. */
  385. exports.checkSupport = function(type) {
  386. return utils.checkSupport(type);
  387. };
  388. /**
  389. * @deprecated
  390. * This value will be removed in a future version without replacement.
  391. */
  392. exports.MAX_VALUE_16BITS = utils.MAX_VALUE_16BITS;
  393. /**
  394. * @deprecated
  395. * This value will be removed in a future version without replacement.
  396. */
  397. exports.MAX_VALUE_32BITS = utils.MAX_VALUE_32BITS;
  398. /**
  399. * @deprecated
  400. * This function will be removed in a future version without replacement.
  401. */
  402. exports.pretty = function(str) {
  403. return utils.pretty(str);
  404. };
  405. /**
  406. * @deprecated
  407. * This function will be removed in a future version without replacement.
  408. */
  409. exports.findCompression = function(compressionMethod) {
  410. return utils.findCompression(compressionMethod);
  411. };
  412. /**
  413. * @deprecated
  414. * This function will be removed in a future version without replacement.
  415. */
  416. exports.isRegExp = function (object) {
  417. return utils.isRegExp(object);
  418. };
  419. },{"./utils":21}],8:[function(_dereq_,module,exports){
  420. 'use strict';
  421. var USE_TYPEDARRAY = (typeof Uint8Array !== 'undefined') && (typeof Uint16Array !== 'undefined') && (typeof Uint32Array !== 'undefined');
  422. var pako = _dereq_("pako");
  423. exports.uncompressInputType = USE_TYPEDARRAY ? "uint8array" : "array";
  424. exports.compressInputType = USE_TYPEDARRAY ? "uint8array" : "array";
  425. exports.magic = "\x08\x00";
  426. exports.compress = function(input) {
  427. return pako.deflateRaw(input);
  428. };
  429. exports.uncompress = function(input) {
  430. return pako.inflateRaw(input);
  431. };
  432. },{"pako":24}],9:[function(_dereq_,module,exports){
  433. 'use strict';
  434. var base64 = _dereq_('./base64');
  435. /**
  436. Usage:
  437. zip = new JSZip();
  438. zip.file("hello.txt", "Hello, World!").file("tempfile", "nothing");
  439. zip.folder("images").file("smile.gif", base64Data, {base64: true});
  440. zip.file("Xmas.txt", "Ho ho ho !", {date : new Date("December 25, 2007 00:00:01")});
  441. zip.remove("tempfile");
  442. base64zip = zip.generate();
  443. **/
  444. /**
  445. * Representation a of zip file in js
  446. * @constructor
  447. * @param {String=|ArrayBuffer=|Uint8Array=} data the data to load, if any (optional).
  448. * @param {Object=} options the options for creating this objects (optional).
  449. */
  450. function JSZipSync(data, options) {
  451. // if this constructor is used without `new`, it adds `new` before itself:
  452. if(!(this instanceof JSZipSync)) return new JSZipSync(data, options);
  453. // object containing the files :
  454. // {
  455. // "folder/" : {...},
  456. // "folder/data.txt" : {...}
  457. // }
  458. this.files = {};
  459. this.comment = null;
  460. // Where we are in the hierarchy
  461. this.root = "";
  462. if (data) {
  463. this.load(data, options);
  464. }
  465. this.clone = function() {
  466. var newObj = new JSZipSync();
  467. for (var i in this) {
  468. if (typeof this[i] !== "function") {
  469. newObj[i] = this[i];
  470. }
  471. }
  472. return newObj;
  473. };
  474. }
  475. JSZipSync.prototype = _dereq_('./object');
  476. JSZipSync.prototype.load = _dereq_('./load');
  477. JSZipSync.support = _dereq_('./support');
  478. JSZipSync.defaults = _dereq_('./defaults');
  479. /**
  480. * @deprecated
  481. * This namespace will be removed in a future version without replacement.
  482. */
  483. JSZipSync.utils = _dereq_('./deprecatedPublicUtils');
  484. JSZipSync.base64 = {
  485. /**
  486. * @deprecated
  487. * This method will be removed in a future version without replacement.
  488. */
  489. encode : function(input) {
  490. return base64.encode(input);
  491. },
  492. /**
  493. * @deprecated
  494. * This method will be removed in a future version without replacement.
  495. */
  496. decode : function(input) {
  497. return base64.decode(input);
  498. }
  499. };
  500. JSZipSync.compressions = _dereq_('./compressions');
  501. module.exports = JSZipSync;
  502. },{"./base64":1,"./compressions":3,"./defaults":6,"./deprecatedPublicUtils":7,"./load":10,"./object":13,"./support":17}],10:[function(_dereq_,module,exports){
  503. 'use strict';
  504. var base64 = _dereq_('./base64');
  505. var ZipEntries = _dereq_('./zipEntries');
  506. module.exports = function(data, options) {
  507. var files, zipEntries, i, input;
  508. options = options || {};
  509. if (options.base64) {
  510. data = base64.decode(data);
  511. }
  512. zipEntries = new ZipEntries(data, options);
  513. files = zipEntries.files;
  514. for (i = 0; i < files.length; i++) {
  515. input = files[i];
  516. this.file(input.fileName, input.decompressed, {
  517. binary: true,
  518. optimizedBinaryString: true,
  519. date: input.date,
  520. dir: input.dir,
  521. comment : input.fileComment.length ? input.fileComment : null,
  522. createFolders: options.createFolders
  523. });
  524. }
  525. if (zipEntries.zipComment.length) {
  526. this.comment = zipEntries.zipComment;
  527. }
  528. return this;
  529. };
  530. },{"./base64":1,"./zipEntries":22}],11:[function(_dereq_,module,exports){
  531. (function (Buffer){
  532. 'use strict';
  533. var Buffer_from = /*::(*/function(){}/*:: :any)*/;
  534. if(typeof Buffer !== 'undefined') {
  535. var nbfs = !Buffer.from;
  536. if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
  537. Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
  538. // $FlowIgnore
  539. if(!Buffer.alloc) Buffer.alloc = function(n) { return new Buffer(n); };
  540. }
  541. module.exports = function(data, encoding){
  542. return typeof data == 'number' ? Buffer.alloc(data) : Buffer_from(data, encoding);
  543. };
  544. module.exports.test = function(b){
  545. return Buffer.isBuffer(b);
  546. };
  547. }).call(this,(typeof Buffer !== "undefined" ? Buffer : undefined))
  548. },{}],12:[function(_dereq_,module,exports){
  549. 'use strict';
  550. var Uint8ArrayReader = _dereq_('./uint8ArrayReader');
  551. function NodeBufferReader(data) {
  552. this.data = data;
  553. this.length = this.data.length;
  554. this.index = 0;
  555. }
  556. NodeBufferReader.prototype = new Uint8ArrayReader();
  557. /**
  558. * @see DataReader.readData
  559. */
  560. NodeBufferReader.prototype.readData = function(size) {
  561. this.checkOffset(size);
  562. var result = this.data.slice(this.index, this.index + size);
  563. this.index += size;
  564. return result;
  565. };
  566. module.exports = NodeBufferReader;
  567. },{"./uint8ArrayReader":18}],13:[function(_dereq_,module,exports){
  568. 'use strict';
  569. var support = _dereq_('./support');
  570. var utils = _dereq_('./utils');
  571. var crc32 = _dereq_('./crc32');
  572. var signature = _dereq_('./signature');
  573. var defaults = _dereq_('./defaults');
  574. var base64 = _dereq_('./base64');
  575. var compressions = _dereq_('./compressions');
  576. var CompressedObject = _dereq_('./compressedObject');
  577. var nodeBuffer = _dereq_('./nodeBuffer');
  578. var utf8 = _dereq_('./utf8');
  579. var StringWriter = _dereq_('./stringWriter');
  580. var Uint8ArrayWriter = _dereq_('./uint8ArrayWriter');
  581. /**
  582. * Returns the raw data of a ZipObject, decompress the content if necessary.
  583. * @param {ZipObject} file the file to use.
  584. * @return {String|ArrayBuffer|Uint8Array|Buffer} the data.
  585. */
  586. var getRawData = function(file) {
  587. if (file._data instanceof CompressedObject) {
  588. file._data = file._data.getContent();
  589. file.options.binary = true;
  590. file.options.base64 = false;
  591. if (utils.getTypeOf(file._data) === "uint8array") {
  592. var copy = file._data;
  593. // when reading an arraybuffer, the CompressedObject mechanism will keep it and subarray() a Uint8Array.
  594. // if we request a file in the same format, we might get the same Uint8Array or its ArrayBuffer (the original zip file).
  595. file._data = new Uint8Array(copy.length);
  596. // with an empty Uint8Array, Opera fails with a "Offset larger than array size"
  597. if (copy.length !== 0) {
  598. file._data.set(copy, 0);
  599. }
  600. }
  601. }
  602. return file._data;
  603. };
  604. /**
  605. * Returns the data of a ZipObject in a binary form. If the content is an unicode string, encode it.
  606. * @param {ZipObject} file the file to use.
  607. * @return {String|ArrayBuffer|Uint8Array|Buffer} the data.
  608. */
  609. var getBinaryData = function(file) {
  610. var result = getRawData(file),
  611. type = utils.getTypeOf(result);
  612. if (type === "string") {
  613. if (!file.options.binary) {
  614. // unicode text !
  615. // unicode string => binary string is a painful process, check if we can avoid it.
  616. if (support.nodebuffer) {
  617. return nodeBuffer(result, "utf-8");
  618. }
  619. }
  620. return file.asBinary();
  621. }
  622. return result;
  623. };
  624. /**
  625. * Transform this._data into a string.
  626. * @param {function} filter a function String -> String, applied if not null on the result.
  627. * @return {String} the string representing this._data.
  628. */
  629. var dataToString = function(asUTF8) {
  630. var result = getRawData(this);
  631. if (result === null || typeof result === "undefined") {
  632. return "";
  633. }
  634. // if the data is a base64 string, we decode it before checking the encoding !
  635. if (this.options.base64) {
  636. result = base64.decode(result);
  637. }
  638. if (asUTF8 && this.options.binary) {
  639. // JSZip.prototype.utf8decode supports arrays as input
  640. // skip to array => string step, utf8decode will do it.
  641. result = out.utf8decode(result);
  642. }
  643. else {
  644. // no utf8 transformation, do the array => string step.
  645. result = utils.transformTo("string", result);
  646. }
  647. if (!asUTF8 && !this.options.binary) {
  648. result = utils.transformTo("string", out.utf8encode(result));
  649. }
  650. return result;
  651. };
  652. /**
  653. * A simple object representing a file in the zip file.
  654. * @constructor
  655. * @param {string} name the name of the file
  656. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data
  657. * @param {Object} options the options of the file
  658. */
  659. var ZipObject = function(name, data, options) {
  660. this.name = name;
  661. this.dir = options.dir;
  662. this.date = options.date;
  663. this.comment = options.comment;
  664. this._data = data;
  665. this.options = options;
  666. /*
  667. * This object contains initial values for dir and date.
  668. * With them, we can check if the user changed the deprecated metadata in
  669. * `ZipObject#options` or not.
  670. */
  671. this._initialMetadata = {
  672. dir : options.dir,
  673. date : options.date
  674. };
  675. };
  676. ZipObject.prototype = {
  677. /**
  678. * Return the content as UTF8 string.
  679. * @return {string} the UTF8 string.
  680. */
  681. asText: function() {
  682. return dataToString.call(this, true);
  683. },
  684. /**
  685. * Returns the binary content.
  686. * @return {string} the content as binary.
  687. */
  688. asBinary: function() {
  689. return dataToString.call(this, false);
  690. },
  691. /**
  692. * Returns the content as a nodejs Buffer.
  693. * @return {Buffer} the content as a Buffer.
  694. */
  695. asNodeBuffer: function() {
  696. var result = getBinaryData(this);
  697. return utils.transformTo("nodebuffer", result);
  698. },
  699. /**
  700. * Returns the content as an Uint8Array.
  701. * @return {Uint8Array} the content as an Uint8Array.
  702. */
  703. asUint8Array: function() {
  704. var result = getBinaryData(this);
  705. return utils.transformTo("uint8array", result);
  706. },
  707. /**
  708. * Returns the content as an ArrayBuffer.
  709. * @return {ArrayBuffer} the content as an ArrayBufer.
  710. */
  711. asArrayBuffer: function() {
  712. return this.asUint8Array().buffer;
  713. }
  714. };
  715. /**
  716. * Transform an integer into a string in hexadecimal.
  717. * @private
  718. * @param {number} dec the number to convert.
  719. * @param {number} bytes the number of bytes to generate.
  720. * @returns {string} the result.
  721. */
  722. var decToHex = function(dec, bytes) {
  723. var hex = "",
  724. i;
  725. for (i = 0; i < bytes; i++) {
  726. hex += String.fromCharCode(dec & 0xff);
  727. dec = dec >>> 8;
  728. }
  729. return hex;
  730. };
  731. /**
  732. * Merge the objects passed as parameters into a new one.
  733. * @private
  734. * @param {...Object} var_args All objects to merge.
  735. * @return {Object} a new object with the data of the others.
  736. */
  737. var extend = function() {
  738. var result = {}, i, attr;
  739. for (i = 0; i < arguments.length; i++) { // arguments is not enumerable in some browsers
  740. for (attr in arguments[i]) {
  741. if (arguments[i].hasOwnProperty(attr) && typeof result[attr] === "undefined") {
  742. result[attr] = arguments[i][attr];
  743. }
  744. }
  745. }
  746. return result;
  747. };
  748. /**
  749. * Transforms the (incomplete) options from the user into the complete
  750. * set of options to create a file.
  751. * @private
  752. * @param {Object} o the options from the user.
  753. * @return {Object} the complete set of options.
  754. */
  755. var prepareFileAttrs = function(o) {
  756. o = o || {};
  757. if (o.base64 === true && (o.binary === null || o.binary === undefined)) {
  758. o.binary = true;
  759. }
  760. o = extend(o, defaults);
  761. o.date = o.date || new Date();
  762. if (o.compression !== null) o.compression = o.compression.toUpperCase();
  763. return o;
  764. };
  765. /**
  766. * Add a file in the current folder.
  767. * @private
  768. * @param {string} name the name of the file
  769. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data of the file
  770. * @param {Object} o the options of the file
  771. * @return {Object} the new file.
  772. */
  773. var fileAdd = function(name, data, o) {
  774. // be sure sub folders exist
  775. var dataType = utils.getTypeOf(data),
  776. parent;
  777. o = prepareFileAttrs(o);
  778. if (o.createFolders && (parent = parentFolder(name))) {
  779. folderAdd.call(this, parent, true);
  780. }
  781. if (o.dir || data === null || typeof data === "undefined") {
  782. o.base64 = false;
  783. o.binary = false;
  784. data = null;
  785. }
  786. else if (dataType === "string") {
  787. if (o.binary && !o.base64) {
  788. // optimizedBinaryString == true means that the file has already been filtered with a 0xFF mask
  789. if (o.optimizedBinaryString !== true) {
  790. // this is a string, not in a base64 format.
  791. // Be sure that this is a correct "binary string"
  792. data = utils.string2binary(data);
  793. }
  794. }
  795. }
  796. else { // arraybuffer, uint8array, ...
  797. o.base64 = false;
  798. o.binary = true;
  799. if (!dataType && !(data instanceof CompressedObject)) {
  800. throw new Error("The data of '" + name + "' is in an unsupported format !");
  801. }
  802. // special case : it's way easier to work with Uint8Array than with ArrayBuffer
  803. if (dataType === "arraybuffer") {
  804. data = utils.transformTo("uint8array", data);
  805. }
  806. }
  807. var object = new ZipObject(name, data, o);
  808. this.files[name] = object;
  809. return object;
  810. };
  811. /**
  812. * Find the parent folder of the path.
  813. * @private
  814. * @param {string} path the path to use
  815. * @return {string} the parent folder, or ""
  816. */
  817. var parentFolder = function (path) {
  818. if (path.slice(-1) == '/') {
  819. path = path.substring(0, path.length - 1);
  820. }
  821. var lastSlash = path.lastIndexOf('/');
  822. return (lastSlash > 0) ? path.substring(0, lastSlash) : "";
  823. };
  824. /**
  825. * Add a (sub) folder in the current folder.
  826. * @private
  827. * @param {string} name the folder's name
  828. * @param {boolean=} [createFolders] If true, automatically create sub
  829. * folders. Defaults to false.
  830. * @return {Object} the new folder.
  831. */
  832. var folderAdd = function(name, createFolders) {
  833. // Check the name ends with a /
  834. if (name.slice(-1) != "/") {
  835. name += "/"; // IE doesn't like substr(-1)
  836. }
  837. createFolders = (typeof createFolders !== 'undefined') ? createFolders : false;
  838. // Does this folder already exist?
  839. if (!this.files[name]) {
  840. fileAdd.call(this, name, null, {
  841. dir: true,
  842. createFolders: createFolders
  843. });
  844. }
  845. return this.files[name];
  846. };
  847. /**
  848. * Generate a JSZip.CompressedObject for a given zipOject.
  849. * @param {ZipObject} file the object to read.
  850. * @param {JSZip.compression} compression the compression to use.
  851. * @return {JSZip.CompressedObject} the compressed result.
  852. */
  853. var generateCompressedObjectFrom = function(file, compression) {
  854. var result = new CompressedObject(),
  855. content;
  856. // the data has not been decompressed, we might reuse things !
  857. if (file._data instanceof CompressedObject) {
  858. result.uncompressedSize = file._data.uncompressedSize;
  859. result.crc32 = file._data.crc32;
  860. if (result.uncompressedSize === 0 || file.dir) {
  861. compression = compressions['STORE'];
  862. result.compressedContent = "";
  863. result.crc32 = 0;
  864. }
  865. else if (file._data.compressionMethod === compression.magic) {
  866. result.compressedContent = file._data.getCompressedContent();
  867. }
  868. else {
  869. content = file._data.getContent();
  870. // need to decompress / recompress
  871. result.compressedContent = compression.compress(utils.transformTo(compression.compressInputType, content));
  872. }
  873. }
  874. else {
  875. // have uncompressed data
  876. content = getBinaryData(file);
  877. if (!content || content.length === 0 || file.dir) {
  878. compression = compressions['STORE'];
  879. content = "";
  880. }
  881. result.uncompressedSize = content.length;
  882. result.crc32 = crc32(content);
  883. result.compressedContent = compression.compress(utils.transformTo(compression.compressInputType, content));
  884. }
  885. result.compressedSize = result.compressedContent.length;
  886. result.compressionMethod = compression.magic;
  887. return result;
  888. };
  889. /**
  890. * Generate the various parts used in the construction of the final zip file.
  891. * @param {string} name the file name.
  892. * @param {ZipObject} file the file content.
  893. * @param {JSZip.CompressedObject} compressedObject the compressed object.
  894. * @param {number} offset the current offset from the start of the zip file.
  895. * @return {object} the zip parts.
  896. */
  897. var generateZipParts = function(name, file, compressedObject, offset) {
  898. var data = compressedObject.compressedContent,
  899. utfEncodedFileName = utils.transformTo("string", utf8.utf8encode(file.name)),
  900. comment = file.comment || "",
  901. utfEncodedComment = utils.transformTo("string", utf8.utf8encode(comment)),
  902. useUTF8ForFileName = utfEncodedFileName.length !== file.name.length,
  903. useUTF8ForComment = utfEncodedComment.length !== comment.length,
  904. o = file.options,
  905. dosTime,
  906. dosDate,
  907. extraFields = "",
  908. unicodePathExtraField = "",
  909. unicodeCommentExtraField = "",
  910. dir, date;
  911. // handle the deprecated options.dir
  912. if (file._initialMetadata.dir !== file.dir) {
  913. dir = file.dir;
  914. } else {
  915. dir = o.dir;
  916. }
  917. // handle the deprecated options.date
  918. if(file._initialMetadata.date !== file.date) {
  919. date = file.date;
  920. } else {
  921. date = o.date;
  922. }
  923. dosTime = date.getHours();
  924. dosTime = dosTime << 6;
  925. dosTime = dosTime | date.getMinutes();
  926. dosTime = dosTime << 5;
  927. dosTime = dosTime | date.getSeconds() / 2;
  928. dosDate = date.getFullYear() - 1980;
  929. dosDate = dosDate << 4;
  930. dosDate = dosDate | (date.getMonth() + 1);
  931. dosDate = dosDate << 5;
  932. dosDate = dosDate | date.getDate();
  933. if (useUTF8ForFileName) {
  934. // set the unicode path extra field. unzip needs at least one extra
  935. // field to correctly handle unicode path, so using the path is as good
  936. // as any other information. This could improve the situation with
  937. // other archive managers too.
  938. // This field is usually used without the utf8 flag, with a non
  939. // unicode path in the header (winrar, winzip). This helps (a bit)
  940. // with the messy Windows' default compressed folders feature but
  941. // breaks on p7zip which doesn't seek the unicode path extra field.
  942. // So for now, UTF-8 everywhere !
  943. unicodePathExtraField =
  944. // Version
  945. decToHex(1, 1) +
  946. // NameCRC32
  947. decToHex(crc32(utfEncodedFileName), 4) +
  948. // UnicodeName
  949. utfEncodedFileName;
  950. extraFields +=
  951. // Info-ZIP Unicode Path Extra Field
  952. "\x75\x70" +
  953. // size
  954. decToHex(unicodePathExtraField.length, 2) +
  955. // content
  956. unicodePathExtraField;
  957. }
  958. if(useUTF8ForComment) {
  959. unicodeCommentExtraField =
  960. // Version
  961. decToHex(1, 1) +
  962. // CommentCRC32
  963. decToHex(this.crc32(utfEncodedComment), 4) +
  964. // UnicodeName
  965. utfEncodedComment;
  966. extraFields +=
  967. // Info-ZIP Unicode Path Extra Field
  968. "\x75\x63" +
  969. // size
  970. decToHex(unicodeCommentExtraField.length, 2) +
  971. // content
  972. unicodeCommentExtraField;
  973. }
  974. var header = "";
  975. // version needed to extract
  976. header += "\x0A\x00";
  977. // general purpose bit flag
  978. // set bit 11 if utf8
  979. header += (useUTF8ForFileName || useUTF8ForComment) ? "\x00\x08" : "\x00\x00";
  980. // compression method
  981. header += compressedObject.compressionMethod;
  982. // last mod file time
  983. header += decToHex(dosTime, 2);
  984. // last mod file date
  985. header += decToHex(dosDate, 2);
  986. // crc-32
  987. header += decToHex(compressedObject.crc32, 4);
  988. // compressed size
  989. header += decToHex(compressedObject.compressedSize, 4);
  990. // uncompressed size
  991. header += decToHex(compressedObject.uncompressedSize, 4);
  992. // file name length
  993. header += decToHex(utfEncodedFileName.length, 2);
  994. // extra field length
  995. header += decToHex(extraFields.length, 2);
  996. var fileRecord = signature.LOCAL_FILE_HEADER + header + utfEncodedFileName + extraFields;
  997. var dirRecord = signature.CENTRAL_FILE_HEADER +
  998. // version made by (00: DOS)
  999. "\x14\x00" +
  1000. // file header (common to file and central directory)
  1001. header +
  1002. // file comment length
  1003. decToHex(utfEncodedComment.length, 2) +
  1004. // disk number start
  1005. "\x00\x00" +
  1006. // internal file attributes TODO
  1007. "\x00\x00" +
  1008. // external file attributes
  1009. (dir === true ? "\x10\x00\x00\x00" : "\x00\x00\x00\x00") +
  1010. // relative offset of local header
  1011. decToHex(offset, 4) +
  1012. // file name
  1013. utfEncodedFileName +
  1014. // extra field
  1015. extraFields +
  1016. // file comment
  1017. utfEncodedComment;
  1018. return {
  1019. fileRecord: fileRecord,
  1020. dirRecord: dirRecord,
  1021. compressedObject: compressedObject
  1022. };
  1023. };
  1024. // return the actual prototype of JSZip
  1025. var out = {
  1026. /**
  1027. * Read an existing zip and merge the data in the current JSZip object.
  1028. * The implementation is in jszip-load.js, don't forget to include it.
  1029. * @param {String|ArrayBuffer|Uint8Array|Buffer} stream The stream to load
  1030. * @param {Object} options Options for loading the stream.
  1031. * options.base64 : is the stream in base64 ? default : false
  1032. * @return {JSZip} the current JSZip object
  1033. */
  1034. load: function(stream, options) {
  1035. throw new Error("Load method is not defined. Is the file jszip-load.js included ?");
  1036. },
  1037. /**
  1038. * Filter nested files/folders with the specified function.
  1039. * @param {Function} search the predicate to use :
  1040. * function (relativePath, file) {...}
  1041. * It takes 2 arguments : the relative path and the file.
  1042. * @return {Array} An array of matching elements.
  1043. */
  1044. filter: function(search) {
  1045. var result = [],
  1046. filename, relativePath, file, fileClone;
  1047. for (filename in this.files) {
  1048. if (!this.files.hasOwnProperty(filename)) {
  1049. continue;
  1050. }
  1051. file = this.files[filename];
  1052. // return a new object, don't let the user mess with our internal objects :)
  1053. fileClone = new ZipObject(file.name, file._data, extend(file.options));
  1054. relativePath = filename.slice(this.root.length, filename.length);
  1055. if (filename.slice(0, this.root.length) === this.root && // the file is in the current root
  1056. search(relativePath, fileClone)) { // and the file matches the function
  1057. result.push(fileClone);
  1058. }
  1059. }
  1060. return result;
  1061. },
  1062. /**
  1063. * Add a file to the zip file, or search a file.
  1064. * @param {string|RegExp} name The name of the file to add (if data is defined),
  1065. * the name of the file to find (if no data) or a regex to match files.
  1066. * @param {String|ArrayBuffer|Uint8Array|Buffer} data The file data, either raw or base64 encoded
  1067. * @param {Object} o File options
  1068. * @return {JSZip|Object|Array} this JSZip object (when adding a file),
  1069. * a file (when searching by string) or an array of files (when searching by regex).
  1070. */
  1071. file: function(name, data, o) {
  1072. if (arguments.length === 1) {
  1073. if (utils.isRegExp(name)) {
  1074. var regexp = name;
  1075. return this.filter(function(relativePath, file) {
  1076. return !file.dir && regexp.test(relativePath);
  1077. });
  1078. }
  1079. else { // text
  1080. return this.filter(function(relativePath, file) {
  1081. return !file.dir && relativePath === name;
  1082. })[0] || null;
  1083. }
  1084. }
  1085. else { // more than one argument : we have data !
  1086. name = this.root + name;
  1087. fileAdd.call(this, name, data, o);
  1088. }
  1089. return this;
  1090. },
  1091. /**
  1092. * Add a directory to the zip file, or search.
  1093. * @param {String|RegExp} arg The name of the directory to add, or a regex to search folders.
  1094. * @return {JSZip} an object with the new directory as the root, or an array containing matching folders.
  1095. */
  1096. folder: function(arg) {
  1097. if (!arg) {
  1098. return this;
  1099. }
  1100. if (utils.isRegExp(arg)) {
  1101. return this.filter(function(relativePath, file) {
  1102. return file.dir && arg.test(relativePath);
  1103. });
  1104. }
  1105. // else, name is a new folder
  1106. var name = this.root + arg;
  1107. var newFolder = folderAdd.call(this, name);
  1108. // Allow chaining by returning a new object with this folder as the root
  1109. var ret = this.clone();
  1110. ret.root = newFolder.name;
  1111. return ret;
  1112. },
  1113. /**
  1114. * Delete a file, or a directory and all sub-files, from the zip
  1115. * @param {string} name the name of the file to delete
  1116. * @return {JSZip} this JSZip object
  1117. */
  1118. remove: function(name) {
  1119. name = this.root + name;
  1120. var file = this.files[name];
  1121. if (!file) {
  1122. // Look for any folders
  1123. if (name.slice(-1) != "/") {
  1124. name += "/";
  1125. }
  1126. file = this.files[name];
  1127. }
  1128. if (file && !file.dir) {
  1129. // file
  1130. delete this.files[name];
  1131. } else {
  1132. // maybe a folder, delete recursively
  1133. var kids = this.filter(function(relativePath, file) {
  1134. return file.name.slice(0, name.length) === name;
  1135. });
  1136. for (var i = 0; i < kids.length; i++) {
  1137. delete this.files[kids[i].name];
  1138. }
  1139. }
  1140. return this;
  1141. },
  1142. /**
  1143. * Generate the complete zip file
  1144. * @param {Object} options the options to generate the zip file :
  1145. * - base64, (deprecated, use type instead) true to generate base64.
  1146. * - compression, "STORE" by default.
  1147. * - type, "base64" by default. Values are : string, base64, uint8array, arraybuffer, blob.
  1148. * @return {String|Uint8Array|ArrayBuffer|Buffer|Blob} the zip file
  1149. */
  1150. generate: function(options) {
  1151. options = extend(options || {}, {
  1152. base64: true,
  1153. compression: "STORE",
  1154. type: "base64",
  1155. comment: null
  1156. });
  1157. utils.checkSupport(options.type);
  1158. var zipData = [],
  1159. localDirLength = 0,
  1160. centralDirLength = 0,
  1161. writer, i,
  1162. utfEncodedComment = utils.transformTo("string", this.utf8encode(options.comment || this.comment || ""));
  1163. // first, generate all the zip parts.
  1164. for (var name in this.files) {
  1165. if (!this.files.hasOwnProperty(name)) {
  1166. continue;
  1167. }
  1168. var file = this.files[name];
  1169. var compressionName = file.options.compression || options.compression.toUpperCase();
  1170. var compression = compressions[compressionName];
  1171. if (!compression) {
  1172. throw new Error(compressionName + " is not a valid compression method !");
  1173. }
  1174. var compressedObject = generateCompressedObjectFrom.call(this, file, compression);
  1175. var zipPart = generateZipParts.call(this, name, file, compressedObject, localDirLength);
  1176. localDirLength += zipPart.fileRecord.length + compressedObject.compressedSize;
  1177. centralDirLength += zipPart.dirRecord.length;
  1178. zipData.push(zipPart);
  1179. }
  1180. var dirEnd = "";
  1181. // end of central dir signature
  1182. dirEnd = signature.CENTRAL_DIRECTORY_END +
  1183. // number of this disk
  1184. "\x00\x00" +
  1185. // number of the disk with the start of the central directory
  1186. "\x00\x00" +
  1187. // total number of entries in the central directory on this disk
  1188. decToHex(zipData.length, 2) +
  1189. // total number of entries in the central directory
  1190. decToHex(zipData.length, 2) +
  1191. // size of the central directory 4 bytes
  1192. decToHex(centralDirLength, 4) +
  1193. // offset of start of central directory with respect to the starting disk number
  1194. decToHex(localDirLength, 4) +
  1195. // .ZIP file comment length
  1196. decToHex(utfEncodedComment.length, 2) +
  1197. // .ZIP file comment
  1198. utfEncodedComment;
  1199. // we have all the parts (and the total length)
  1200. // time to create a writer !
  1201. var typeName = options.type.toLowerCase();
  1202. if(typeName==="uint8array"||typeName==="arraybuffer"||typeName==="blob"||typeName==="nodebuffer") {
  1203. writer = new Uint8ArrayWriter(localDirLength + centralDirLength + dirEnd.length);
  1204. }else{
  1205. writer = new StringWriter(localDirLength + centralDirLength + dirEnd.length);
  1206. }
  1207. for (i = 0; i < zipData.length; i++) {
  1208. writer.append(zipData[i].fileRecord);
  1209. writer.append(zipData[i].compressedObject.compressedContent);
  1210. }
  1211. for (i = 0; i < zipData.length; i++) {
  1212. writer.append(zipData[i].dirRecord);
  1213. }
  1214. writer.append(dirEnd);
  1215. var zip = writer.finalize();
  1216. switch(options.type.toLowerCase()) {
  1217. // case "zip is an Uint8Array"
  1218. case "uint8array" :
  1219. case "arraybuffer" :
  1220. case "nodebuffer" :
  1221. return utils.transformTo(options.type.toLowerCase(), zip);
  1222. case "blob" :
  1223. return utils.arrayBuffer2Blob(utils.transformTo("arraybuffer", zip));
  1224. // case "zip is a string"
  1225. case "base64" :
  1226. return (options.base64) ? base64.encode(zip) : zip;
  1227. default : // case "string" :
  1228. return zip;
  1229. }
  1230. },
  1231. /**
  1232. * @deprecated
  1233. * This method will be removed in a future version without replacement.
  1234. */
  1235. crc32: function (input, crc) {
  1236. return crc32(input, crc);
  1237. },
  1238. /**
  1239. * @deprecated
  1240. * This method will be removed in a future version without replacement.
  1241. */
  1242. utf8encode: function (string) {
  1243. return utils.transformTo("string", utf8.utf8encode(string));
  1244. },
  1245. /**
  1246. * @deprecated
  1247. * This method will be removed in a future version without replacement.
  1248. */
  1249. utf8decode: function (input) {
  1250. return utf8.utf8decode(input);
  1251. }
  1252. };
  1253. module.exports = out;
  1254. },{"./base64":1,"./compressedObject":2,"./compressions":3,"./crc32":4,"./defaults":6,"./nodeBuffer":11,"./signature":14,"./stringWriter":16,"./support":17,"./uint8ArrayWriter":19,"./utf8":20,"./utils":21}],14:[function(_dereq_,module,exports){
  1255. 'use strict';
  1256. exports.LOCAL_FILE_HEADER = "PK\x03\x04";
  1257. exports.CENTRAL_FILE_HEADER = "PK\x01\x02";
  1258. exports.CENTRAL_DIRECTORY_END = "PK\x05\x06";
  1259. exports.ZIP64_CENTRAL_DIRECTORY_LOCATOR = "PK\x06\x07";
  1260. exports.ZIP64_CENTRAL_DIRECTORY_END = "PK\x06\x06";
  1261. exports.DATA_DESCRIPTOR = "PK\x07\x08";
  1262. },{}],15:[function(_dereq_,module,exports){
  1263. 'use strict';
  1264. var DataReader = _dereq_('./dataReader');
  1265. var utils = _dereq_('./utils');
  1266. function StringReader(data, optimizedBinaryString) {
  1267. this.data = data;
  1268. if (!optimizedBinaryString) {
  1269. this.data = utils.string2binary(this.data);
  1270. }
  1271. this.length = this.data.length;
  1272. this.index = 0;
  1273. }
  1274. StringReader.prototype = new DataReader();
  1275. /**
  1276. * @see DataReader.byteAt
  1277. */
  1278. StringReader.prototype.byteAt = function(i) {
  1279. return this.data.charCodeAt(i);
  1280. };
  1281. /**
  1282. * @see DataReader.lastIndexOfSignature
  1283. */
  1284. StringReader.prototype.lastIndexOfSignature = function(sig) {
  1285. return this.data.lastIndexOf(sig);
  1286. };
  1287. /**
  1288. * @see DataReader.readData
  1289. */
  1290. StringReader.prototype.readData = function(size) {
  1291. this.checkOffset(size);
  1292. // this will work because the constructor applied the "& 0xff" mask.
  1293. var result = this.data.slice(this.index, this.index + size);
  1294. this.index += size;
  1295. return result;
  1296. };
  1297. module.exports = StringReader;
  1298. },{"./dataReader":5,"./utils":21}],16:[function(_dereq_,module,exports){
  1299. 'use strict';
  1300. var utils = _dereq_('./utils');
  1301. /**
  1302. * An object to write any content to a string.
  1303. * @constructor
  1304. */
  1305. var StringWriter = function() {
  1306. this.data = [];
  1307. };
  1308. StringWriter.prototype = {
  1309. /**
  1310. * Append any content to the current string.
  1311. * @param {Object} input the content to add.
  1312. */
  1313. append: function(input) {
  1314. input = utils.transformTo("string", input);
  1315. this.data.push(input);
  1316. },
  1317. /**
  1318. * Finalize the construction an return the result.
  1319. * @return {string} the generated string.
  1320. */
  1321. finalize: function() {
  1322. return this.data.join("");
  1323. }
  1324. };
  1325. module.exports = StringWriter;
  1326. },{"./utils":21}],17:[function(_dereq_,module,exports){
  1327. (function (Buffer){
  1328. 'use strict';
  1329. exports.base64 = true;
  1330. exports.array = true;
  1331. exports.string = true;
  1332. exports.arraybuffer = typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined";
  1333. // contains true if JSZip can read/generate nodejs Buffer, false otherwise.
  1334. // Browserify will provide a Buffer implementation for browsers, which is
  1335. // an augmented Uint8Array (i.e., can be used as either Buffer or U8).
  1336. exports.nodebuffer = typeof Buffer !== "undefined";
  1337. // contains true if JSZip can read/generate Uint8Array, false otherwise.
  1338. exports.uint8array = typeof Uint8Array !== "undefined";
  1339. if (typeof ArrayBuffer === "undefined") {
  1340. exports.blob = false;
  1341. }
  1342. else {
  1343. var buffer = new ArrayBuffer(0);
  1344. try {
  1345. exports.blob = new Blob([buffer], {
  1346. type: "application/zip"
  1347. }).size === 0;
  1348. }
  1349. catch (e) {
  1350. try {
  1351. var Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
  1352. var builder = new Builder();
  1353. builder.append(buffer);
  1354. exports.blob = builder.getBlob('application/zip').size === 0;
  1355. }
  1356. catch (e) {
  1357. exports.blob = false;
  1358. }
  1359. }
  1360. }
  1361. }).call(this,(typeof Buffer !== "undefined" ? Buffer : undefined))
  1362. },{}],18:[function(_dereq_,module,exports){
  1363. 'use strict';
  1364. var DataReader = _dereq_('./dataReader');
  1365. function Uint8ArrayReader(data) {
  1366. if (data) {
  1367. this.data = data;
  1368. this.length = this.data.length;
  1369. this.index = 0;
  1370. }
  1371. }
  1372. Uint8ArrayReader.prototype = new DataReader();
  1373. /**
  1374. * @see DataReader.byteAt
  1375. */
  1376. Uint8ArrayReader.prototype.byteAt = function(i) {
  1377. return this.data[i];
  1378. };
  1379. /**
  1380. * @see DataReader.lastIndexOfSignature
  1381. */
  1382. Uint8ArrayReader.prototype.lastIndexOfSignature = function(sig) {
  1383. var sig0 = sig.charCodeAt(0),
  1384. sig1 = sig.charCodeAt(1),
  1385. sig2 = sig.charCodeAt(2),
  1386. sig3 = sig.charCodeAt(3);
  1387. for (var i = this.length - 4; i >= 0; --i) {
  1388. if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) {
  1389. return i;
  1390. }
  1391. }
  1392. return -1;
  1393. };
  1394. /**
  1395. * @see DataReader.readData
  1396. */
  1397. Uint8ArrayReader.prototype.readData = function(size) {
  1398. this.checkOffset(size);
  1399. if(size === 0) {
  1400. // in IE10, when using subarray(idx, idx), we get the array [0x00] instead of [].
  1401. return new Uint8Array(0);
  1402. }
  1403. var result = this.data.subarray(this.index, this.index + size);
  1404. this.index += size;
  1405. return result;
  1406. };
  1407. module.exports = Uint8ArrayReader;
  1408. },{"./dataReader":5}],19:[function(_dereq_,module,exports){
  1409. 'use strict';
  1410. var utils = _dereq_('./utils');
  1411. /**
  1412. * An object to write any content to an Uint8Array.
  1413. * @constructor
  1414. * @param {number} length The length of the array.
  1415. */
  1416. var Uint8ArrayWriter = function(length) {
  1417. this.data = new Uint8Array(length);
  1418. this.index = 0;
  1419. };
  1420. Uint8ArrayWriter.prototype = {
  1421. /**
  1422. * Append any content to the current array.
  1423. * @param {Object} input the content to add.
  1424. */
  1425. append: function(input) {
  1426. if (input.length !== 0) {
  1427. // with an empty Uint8Array, Opera fails with a "Offset larger than array size"
  1428. input = utils.transformTo("uint8array", input);
  1429. this.data.set(input, this.index);
  1430. this.index += input.length;
  1431. }
  1432. },
  1433. /**
  1434. * Finalize the construction an return the result.
  1435. * @return {Uint8Array} the generated array.
  1436. */
  1437. finalize: function() {
  1438. return this.data;
  1439. }
  1440. };
  1441. module.exports = Uint8ArrayWriter;
  1442. },{"./utils":21}],20:[function(_dereq_,module,exports){
  1443. 'use strict';
  1444. var utils = _dereq_('./utils');
  1445. var support = _dereq_('./support');
  1446. var nodeBuffer = _dereq_('./nodeBuffer');
  1447. /**
  1448. * The following functions come from pako, from pako/lib/utils/strings
  1449. * released under the MIT license, see pako https://github.com/nodeca/pako/
  1450. */
  1451. // Table with utf8 lengths (calculated by first byte of sequence)
  1452. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  1453. // because max possible codepoint is 0x10ffff
  1454. var _utf8len = new Array(256);
  1455. for (var i=0; i<256; i++) {
  1456. _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
  1457. }
  1458. _utf8len[254]=_utf8len[254]=1; // Invalid sequence start
  1459. // convert string to array (typed, when possible)
  1460. var string2buf = function (str) {
  1461. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  1462. // count binary size
  1463. for (m_pos = 0; m_pos < str_len; m_pos++) {
  1464. c = str.charCodeAt(m_pos);
  1465. if (((c & 0xfc00) === 0xd800) && (m_pos+1 < str_len)) {
  1466. c2 = str.charCodeAt(m_pos+1);
  1467. if ((c2 & 0xfc00) === 0xdc00) {
  1468. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  1469. m_pos++;
  1470. }
  1471. }
  1472. buf_len += (c < 0x80) ? 1 : ((c < 0x800) ? 2 : ((c < 0x10000) ? 3 : 4));
  1473. }
  1474. // allocate buffer
  1475. if (support.uint8array) {
  1476. buf = new Uint8Array(buf_len);
  1477. } else {
  1478. buf = new Array(buf_len);
  1479. }
  1480. // convert
  1481. for (i=0, m_pos = 0; i < buf_len; m_pos++) {
  1482. c = str.charCodeAt(m_pos);
  1483. if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
  1484. c2 = str.charCodeAt(m_pos+1);
  1485. if ((c2 & 0xfc00) === 0xdc00) {
  1486. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  1487. m_pos++;
  1488. }
  1489. }
  1490. if (c < 0x80) {
  1491. /* one byte */
  1492. buf[i++] = c;
  1493. } else if (c < 0x800) {
  1494. /* two bytes */
  1495. buf[i++] = 0xC0 | (c >>> 6);
  1496. buf[i++] = 0x80 | (c & 0x3f);
  1497. } else if (c < 0x10000) {
  1498. /* three bytes */
  1499. buf[i++] = 0xE0 | (c >>> 12);
  1500. buf[i++] = 0x80 | ((c >>> 6) & 0x3f);
  1501. buf[i++] = 0x80 | (c & 0x3f);
  1502. } else {
  1503. /* four bytes */
  1504. buf[i++] = 0xf0 | (c >>> 18);
  1505. buf[i++] = 0x80 | ((c >>> 12) & 0x3f);
  1506. buf[i++] = 0x80 | ((c >>> 6) & 0x3f);
  1507. buf[i++] = 0x80 | (c & 0x3f);
  1508. }
  1509. }
  1510. return buf;
  1511. };
  1512. // Calculate max possible position in utf8 buffer,
  1513. // that will not break sequence. If that's not possible
  1514. // - (very small limits) return max size as is.
  1515. //
  1516. // buf[] - utf8 bytes array
  1517. // max - length limit (mandatory);
  1518. var utf8border = function(buf, max) {
  1519. var pos;
  1520. max = max || buf.length;
  1521. if (max > buf.length) { max = buf.length; }
  1522. // go back from last position, until start of sequence found
  1523. pos = max-1;
  1524. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  1525. // Fuckup - very small and broken sequence,
  1526. // return max, because we should return something anyway.
  1527. if (pos < 0) { return max; }
  1528. // If we came to start of buffer - that means vuffer is too small,
  1529. // return max too.
  1530. if (pos === 0) { return max; }
  1531. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  1532. };
  1533. // convert array to string
  1534. var buf2string = function (buf) {
  1535. var str, i, out, c, c_len;
  1536. var len = buf.length;
  1537. // Reserve max possible length (2 words per char)
  1538. // NB: by unknown reasons, Array is significantly faster for
  1539. // String.fromCharCode.apply than Uint16Array.
  1540. var utf16buf = new Array(len*2);
  1541. for (out=0, i=0; i<len;) {
  1542. c = buf[i++];
  1543. // quick process ascii
  1544. if (c < 0x80) { utf16buf[out++] = c; continue; }
  1545. c_len = _utf8len[c];
  1546. // skip 5 & 6 byte codes
  1547. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
  1548. // apply mask on first byte
  1549. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  1550. // join the rest
  1551. while (c_len > 1 && i < len) {
  1552. c = (c << 6) | (buf[i++] & 0x3f);
  1553. c_len--;
  1554. }
  1555. // terminated by end of string?
  1556. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  1557. if (c < 0x10000) {
  1558. utf16buf[out++] = c;
  1559. } else {
  1560. c -= 0x10000;
  1561. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  1562. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  1563. }
  1564. }
  1565. // shrinkBuf(utf16buf, out)
  1566. if (utf16buf.length !== out) {
  1567. if(utf16buf.subarray) {
  1568. utf16buf = utf16buf.subarray(0, out);
  1569. } else {
  1570. utf16buf.length = out;
  1571. }
  1572. }
  1573. // return String.fromCharCode.apply(null, utf16buf);
  1574. return utils.applyFromCharCode(utf16buf);
  1575. };
  1576. // That's all for the pako functions.
  1577. /**
  1578. * Transform a javascript string into an array (typed if possible) of bytes,
  1579. * UTF-8 encoded.
  1580. * @param {String} str the string to encode
  1581. * @return {Array|Uint8Array|Buffer} the UTF-8 encoded string.
  1582. */
  1583. exports.utf8encode = function utf8encode(str) {
  1584. if (support.nodebuffer) {
  1585. return nodeBuffer(str, "utf-8");
  1586. }
  1587. return string2buf(str);
  1588. };
  1589. /**
  1590. * Transform a bytes array (or a representation) representing an UTF-8 encoded
  1591. * string into a javascript string.
  1592. * @param {Array|Uint8Array|Buffer} buf the data de decode
  1593. * @return {String} the decoded string.
  1594. */
  1595. exports.utf8decode = function utf8decode(buf) {
  1596. if (support.nodebuffer) {
  1597. return utils.transformTo("nodebuffer", buf).toString("utf-8");
  1598. }
  1599. buf = utils.transformTo(support.uint8array ? "uint8array" : "array", buf);
  1600. // return buf2string(buf);
  1601. // Chrome prefers to work with "small" chunks of data
  1602. // for the method buf2string.
  1603. // Firefox and Chrome has their own shortcut, IE doesn't seem to really care.
  1604. var result = [], k = 0, len = buf.length, chunk = 65536;
  1605. while (k < len) {
  1606. var nextBoundary = utf8border(buf, Math.min(k + chunk, len));
  1607. if (support.uint8array) {
  1608. result.push(buf2string(buf.subarray(k, nextBoundary)));
  1609. } else {
  1610. result.push(buf2string(buf.slice(k, nextBoundary)));
  1611. }
  1612. k = nextBoundary;
  1613. }
  1614. return result.join("");
  1615. };
  1616. // vim: set shiftwidth=4 softtabstop=4:
  1617. },{"./nodeBuffer":11,"./support":17,"./utils":21}],21:[function(_dereq_,module,exports){
  1618. 'use strict';
  1619. var support = _dereq_('./support');
  1620. var compressions = _dereq_('./compressions');
  1621. var nodeBuffer = _dereq_('./nodeBuffer');
  1622. /**
  1623. * Convert a string to a "binary string" : a string containing only char codes between 0 and 255.
  1624. * @param {string} str the string to transform.
  1625. * @return {String} the binary string.
  1626. */
  1627. exports.string2binary = function(str) {
  1628. var result = "";
  1629. for (var i = 0; i < str.length; i++) {
  1630. result += String.fromCharCode(str.charCodeAt(i) & 0xff);
  1631. }
  1632. return result;
  1633. };
  1634. exports.arrayBuffer2Blob = function(buffer) {
  1635. exports.checkSupport("blob");
  1636. try {
  1637. // Blob constructor
  1638. return new Blob([buffer], {
  1639. type: "application/zip"
  1640. });
  1641. }
  1642. catch (e) {
  1643. try {
  1644. // deprecated, browser only, old way
  1645. var Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
  1646. var builder = new Builder();
  1647. builder.append(buffer);
  1648. return builder.getBlob('application/zip');
  1649. }
  1650. catch (e) {
  1651. // well, fuck ?!
  1652. throw new Error("Bug : can't construct the Blob.");
  1653. }
  1654. }
  1655. };
  1656. /**
  1657. * The identity function.
  1658. * @param {Object} input the input.
  1659. * @return {Object} the same input.
  1660. */
  1661. function identity(input) {
  1662. return input;
  1663. }
  1664. /**
  1665. * Fill in an array with a string.
  1666. * @param {String} str the string to use.
  1667. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to fill in (will be mutated).
  1668. * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated array.
  1669. */
  1670. function stringToArrayLike(str, array) {
  1671. for (var i = 0; i < str.length; ++i) {
  1672. array[i] = str.charCodeAt(i) & 0xFF;
  1673. }
  1674. return array;
  1675. }
  1676. /**
  1677. * Transform an array-like object to a string.
  1678. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform.
  1679. * @return {String} the result.
  1680. */
  1681. function arrayLikeToString(array) {
  1682. // Performances notes :
  1683. // --------------------
  1684. // String.fromCharCode.apply(null, array) is the fastest, see
  1685. // see http://jsperf.com/converting-a-uint8array-to-a-string/2
  1686. // but the stack is limited (and we can get huge arrays !).
  1687. //
  1688. // result += String.fromCharCode(array[i]); generate too many strings !
  1689. //
  1690. // This code is inspired by http://jsperf.com/arraybuffer-to-string-apply-performance/2
  1691. var chunk = 65536;
  1692. var result = [],
  1693. len = array.length,
  1694. type = exports.getTypeOf(array),
  1695. k = 0,
  1696. canUseApply = true;
  1697. try {
  1698. switch(type) {
  1699. case "uint8array":
  1700. String.fromCharCode.apply(null, new Uint8Array(0));
  1701. break;
  1702. case "nodebuffer":
  1703. String.fromCharCode.apply(null, nodeBuffer(0));
  1704. break;
  1705. }
  1706. } catch(e) {
  1707. canUseApply = false;
  1708. }
  1709. // no apply : slow and painful algorithm
  1710. // default browser on android 4.*
  1711. if (!canUseApply) {
  1712. var resultStr = "";
  1713. for(var i = 0; i < array.length;i++) {
  1714. resultStr += String.fromCharCode(array[i]);
  1715. }
  1716. return resultStr;
  1717. }
  1718. while (k < len && chunk > 1) {
  1719. try {
  1720. if (type === "array" || type === "nodebuffer") {
  1721. result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len))));
  1722. }
  1723. else {
  1724. result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len))));
  1725. }
  1726. k += chunk;
  1727. }
  1728. catch (e) {
  1729. chunk = Math.floor(chunk / 2);
  1730. }
  1731. }
  1732. return result.join("");
  1733. }
  1734. exports.applyFromCharCode = arrayLikeToString;
  1735. /**
  1736. * Copy the data from an array-like to an other array-like.
  1737. * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayFrom the origin array.
  1738. * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayTo the destination array which will be mutated.
  1739. * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated destination array.
  1740. */
  1741. function arrayLikeToArrayLike(arrayFrom, arrayTo) {
  1742. for (var i = 0; i < arrayFrom.length; i++) {
  1743. arrayTo[i] = arrayFrom[i];
  1744. }
  1745. return arrayTo;
  1746. }
  1747. // a matrix containing functions to transform everything into everything.
  1748. var transform = {};
  1749. // string to ?
  1750. transform["string"] = {
  1751. "string": identity,
  1752. "array": function(input) {
  1753. return stringToArrayLike(input, new Array(input.length));
  1754. },
  1755. "arraybuffer": function(input) {
  1756. return transform["string"]["uint8array"](input).buffer;
  1757. },
  1758. "uint8array": function(input) {
  1759. return stringToArrayLike(input, new Uint8Array(input.length));
  1760. },
  1761. "nodebuffer": function(input) {
  1762. return stringToArrayLike(input, nodeBuffer(input.length));
  1763. }
  1764. };
  1765. // array to ?
  1766. transform["array"] = {
  1767. "string": arrayLikeToString,
  1768. "array": identity,
  1769. "arraybuffer": function(input) {
  1770. return (new Uint8Array(input)).buffer;
  1771. },
  1772. "uint8array": function(input) {
  1773. return new Uint8Array(input);
  1774. },
  1775. "nodebuffer": function(input) {
  1776. return nodeBuffer(input);
  1777. }
  1778. };
  1779. // arraybuffer to ?
  1780. transform["arraybuffer"] = {
  1781. "string": function(input) {
  1782. return arrayLikeToString(new Uint8Array(input));
  1783. },
  1784. "array": function(input) {
  1785. return arrayLikeToArrayLike(new Uint8Array(input), new Array(input.byteLength));
  1786. },
  1787. "arraybuffer": identity,
  1788. "uint8array": function(input) {
  1789. return new Uint8Array(input);
  1790. },
  1791. "nodebuffer": function(input) {
  1792. return nodeBuffer(new Uint8Array(input));
  1793. }
  1794. };
  1795. // uint8array to ?
  1796. transform["uint8array"] = {
  1797. "string": arrayLikeToString,
  1798. "array": function(input) {
  1799. return arrayLikeToArrayLike(input, new Array(input.length));
  1800. },
  1801. "arraybuffer": function(input) {
  1802. return input.buffer;
  1803. },
  1804. "uint8array": identity,
  1805. "nodebuffer": function(input) {
  1806. return nodeBuffer(input);
  1807. }
  1808. };
  1809. // nodebuffer to ?
  1810. transform["nodebuffer"] = {
  1811. "string": arrayLikeToString,
  1812. "array": function(input) {
  1813. return arrayLikeToArrayLike(input, new Array(input.length));
  1814. },
  1815. "arraybuffer": function(input) {
  1816. return transform["nodebuffer"]["uint8array"](input).buffer;
  1817. },
  1818. "uint8array": function(input) {
  1819. return arrayLikeToArrayLike(input, new Uint8Array(input.length));
  1820. },
  1821. "nodebuffer": identity
  1822. };
  1823. /**
  1824. * Transform an input into any type.
  1825. * The supported output type are : string, array, uint8array, arraybuffer, nodebuffer.
  1826. * If no output type is specified, the unmodified input will be returned.
  1827. * @param {String} outputType the output type.
  1828. * @param {String|Array|ArrayBuffer|Uint8Array|Buffer} input the input to convert.
  1829. * @throws {Error} an Error if the browser doesn't support the requested output type.
  1830. */
  1831. exports.transformTo = function(outputType, input) {
  1832. if (!input) {
  1833. // undefined, null, etc
  1834. // an empty string won't harm.
  1835. input = "";
  1836. }
  1837. if (!outputType) {
  1838. return input;
  1839. }
  1840. exports.checkSupport(outputType);
  1841. var inputType = exports.getTypeOf(input);
  1842. var result = transform[inputType][outputType](input);
  1843. return result;
  1844. };
  1845. /**
  1846. * Return the type of the input.
  1847. * The type will be in a format valid for JSZip.utils.transformTo : string, array, uint8array, arraybuffer.
  1848. * @param {Object} input the input to identify.
  1849. * @return {String} the (lowercase) type of the input.
  1850. */
  1851. exports.getTypeOf = function(input) {
  1852. if (typeof input === "string") {
  1853. return "string";
  1854. }
  1855. if (Object.prototype.toString.call(input) === "[object Array]") {
  1856. return "array";
  1857. }
  1858. if (support.nodebuffer && nodeBuffer.test(input)) {
  1859. return "nodebuffer";
  1860. }
  1861. if (support.uint8array && input instanceof Uint8Array) {
  1862. return "uint8array";
  1863. }
  1864. if (support.arraybuffer && input instanceof ArrayBuffer) {
  1865. return "arraybuffer";
  1866. }
  1867. };
  1868. /**
  1869. * Throw an exception if the type is not supported.
  1870. * @param {String} type the type to check.
  1871. * @throws {Error} an Error if the browser doesn't support the requested type.
  1872. */
  1873. exports.checkSupport = function(type) {
  1874. var supported = support[type.toLowerCase()];
  1875. if (!supported) {
  1876. throw new Error(type + " is not supported by this browser");
  1877. }
  1878. };
  1879. exports.MAX_VALUE_16BITS = 65535;
  1880. exports.MAX_VALUE_32BITS = -1; // well, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" is parsed as -1
  1881. /**
  1882. * Prettify a string read as binary.
  1883. * @param {string} str the string to prettify.
  1884. * @return {string} a pretty string.
  1885. */
  1886. exports.pretty = function(str) {
  1887. var res = '',
  1888. code, i;
  1889. for (i = 0; i < (str || "").length; i++) {
  1890. code = str.charCodeAt(i);
  1891. res += '\\x' + (code < 16 ? "0" : "") + code.toString(16).toUpperCase();
  1892. }
  1893. return res;
  1894. };
  1895. /**
  1896. * Find a compression registered in JSZip.
  1897. * @param {string} compressionMethod the method magic to find.
  1898. * @return {Object|null} the JSZip compression object, null if none found.
  1899. */
  1900. exports.findCompression = function(compressionMethod) {
  1901. for (var method in compressions) {
  1902. if (!compressions.hasOwnProperty(method)) {
  1903. continue;
  1904. }
  1905. if (compressions[method].magic === compressionMethod) {
  1906. return compressions[method];
  1907. }
  1908. }
  1909. return null;
  1910. };
  1911. /**
  1912. * Cross-window, cross-Node-context regular expression detection
  1913. * @param {Object} object Anything
  1914. * @return {Boolean} true if the object is a regular expression,
  1915. * false otherwise
  1916. */
  1917. exports.isRegExp = function (object) {
  1918. return Object.prototype.toString.call(object) === "[object RegExp]";
  1919. };
  1920. },{"./compressions":3,"./nodeBuffer":11,"./support":17}],22:[function(_dereq_,module,exports){
  1921. 'use strict';
  1922. var StringReader = _dereq_('./stringReader');
  1923. var NodeBufferReader = _dereq_('./nodeBufferReader');
  1924. var Uint8ArrayReader = _dereq_('./uint8ArrayReader');
  1925. var utils = _dereq_('./utils');
  1926. var sig = _dereq_('./signature');
  1927. var ZipEntry = _dereq_('./zipEntry');
  1928. var support = _dereq_('./support');
  1929. var jszipProto = _dereq_('./object');
  1930. // class ZipEntries {{{
  1931. /**
  1932. * All the entries in the zip file.
  1933. * @constructor
  1934. * @param {String|ArrayBuffer|Uint8Array} data the binary stream to load.
  1935. * @param {Object} loadOptions Options for loading the stream.
  1936. */
  1937. function ZipEntries(data, loadOptions) {
  1938. this.files = [];
  1939. this.loadOptions = loadOptions;
  1940. if (data) {
  1941. this.load(data);
  1942. }
  1943. }
  1944. ZipEntries.prototype = {
  1945. /**
  1946. * Check that the reader is on the speficied signature.
  1947. * @param {string} expectedSignature the expected signature.
  1948. * @throws {Error} if it is an other signature.
  1949. */
  1950. checkSignature: function(expectedSignature) {
  1951. var signature = this.reader.readString(4);
  1952. if (signature !== expectedSignature) {
  1953. throw new Error("Corrupted zip or bug : unexpected signature " + "(" + utils.pretty(signature) + ", expected " + utils.pretty(expectedSignature) + ")");
  1954. }
  1955. },
  1956. /**
  1957. * Read the end of the central directory.
  1958. */
  1959. readBlockEndOfCentral: function() {
  1960. this.diskNumber = this.reader.readInt(2);
  1961. this.diskWithCentralDirStart = this.reader.readInt(2);
  1962. this.centralDirRecordsOnThisDisk = this.reader.readInt(2);
  1963. this.centralDirRecords = this.reader.readInt(2);
  1964. this.centralDirSize = this.reader.readInt(4);
  1965. this.centralDirOffset = this.reader.readInt(4);
  1966. this.zipCommentLength = this.reader.readInt(2);
  1967. // warning : the encoding depends of the system locale
  1968. // On a linux machine with LANG=en_US.utf8, this field is utf8 encoded.
  1969. // On a windows machine, this field is encoded with the localized windows code page.
  1970. this.zipComment = this.reader.readString(this.zipCommentLength);
  1971. // To get consistent behavior with the generation part, we will assume that
  1972. // this is utf8 encoded.
  1973. this.zipComment = jszipProto.utf8decode(this.zipComment);
  1974. },
  1975. /**
  1976. * Read the end of the Zip 64 central directory.
  1977. * Not merged with the method readEndOfCentral :
  1978. * The end of central can coexist with its Zip64 brother,
  1979. * I don't want to read the wrong number of bytes !
  1980. */
  1981. readBlockZip64EndOfCentral: function() {
  1982. this.zip64EndOfCentralSize = this.reader.readInt(8);
  1983. this.versionMadeBy = this.reader.readString(2);
  1984. this.versionNeeded = this.reader.readInt(2);
  1985. this.diskNumber = this.reader.readInt(4);
  1986. this.diskWithCentralDirStart = this.reader.readInt(4);
  1987. this.centralDirRecordsOnThisDisk = this.reader.readInt(8);
  1988. this.centralDirRecords = this.reader.readInt(8);
  1989. this.centralDirSize = this.reader.readInt(8);
  1990. this.centralDirOffset = this.reader.readInt(8);
  1991. this.zip64ExtensibleData = {};
  1992. var extraDataSize = this.zip64EndOfCentralSize - 44,
  1993. index = 0,
  1994. extraFieldId,
  1995. extraFieldLength,
  1996. extraFieldValue;
  1997. while (index < extraDataSize) {
  1998. extraFieldId = this.reader.readInt(2);
  1999. extraFieldLength = this.reader.readInt(4);
  2000. extraFieldValue = this.reader.readString(extraFieldLength);
  2001. this.zip64ExtensibleData[extraFieldId] = {
  2002. id: extraFieldId,
  2003. length: extraFieldLength,
  2004. value: extraFieldValue
  2005. };
  2006. }
  2007. },
  2008. /**
  2009. * Read the end of the Zip 64 central directory locator.
  2010. */
  2011. readBlockZip64EndOfCentralLocator: function() {
  2012. this.diskWithZip64CentralDirStart = this.reader.readInt(4);
  2013. this.relativeOffsetEndOfZip64CentralDir = this.reader.readInt(8);
  2014. this.disksCount = this.reader.readInt(4);
  2015. if (this.disksCount > 1) {
  2016. throw new Error("Multi-volumes zip are not supported");
  2017. }
  2018. },
  2019. /**
  2020. * Read the local files, based on the offset read in the central part.
  2021. */
  2022. readLocalFiles: function() {
  2023. var i, file;
  2024. for (i = 0; i < this.files.length; i++) {
  2025. file = this.files[i];
  2026. this.reader.setIndex(file.localHeaderOffset);
  2027. this.checkSignature(sig.LOCAL_FILE_HEADER);
  2028. file.readLocalPart(this.reader);
  2029. file.handleUTF8();
  2030. }
  2031. },
  2032. /**
  2033. * Read the central directory.
  2034. */
  2035. readCentralDir: function() {
  2036. var file;
  2037. this.reader.setIndex(this.centralDirOffset);
  2038. while (this.reader.readString(4) === sig.CENTRAL_FILE_HEADER) {
  2039. file = new ZipEntry({
  2040. zip64: this.zip64
  2041. }, this.loadOptions);
  2042. file.readCentralPart(this.reader);
  2043. this.files.push(file);
  2044. }
  2045. },
  2046. /**
  2047. * Read the end of central directory.
  2048. */
  2049. readEndOfCentral: function() {
  2050. var offset = this.reader.lastIndexOfSignature(sig.CENTRAL_DIRECTORY_END);
  2051. if (offset === -1) {
  2052. throw new Error("Corrupted zip : can't find end of central directory");
  2053. }
  2054. this.reader.setIndex(offset);
  2055. this.checkSignature(sig.CENTRAL_DIRECTORY_END);
  2056. this.readBlockEndOfCentral();
  2057. /* extract from the zip spec :
  2058. 4) If one of the fields in the end of central directory
  2059. record is too small to hold required data, the field
  2060. should be set to -1 (0xFFFF or 0xFFFFFFFF) and the
  2061. ZIP64 format record should be created.
  2062. 5) The end of central directory record and the
  2063. Zip64 end of central directory locator record must
  2064. reside on the same disk when splitting or spanning
  2065. an archive.
  2066. */
  2067. if (this.diskNumber === utils.MAX_VALUE_16BITS || this.diskWithCentralDirStart === utils.MAX_VALUE_16BITS || this.centralDirRecordsOnThisDisk === utils.MAX_VALUE_16BITS || this.centralDirRecords === utils.MAX_VALUE_16BITS || this.centralDirSize === utils.MAX_VALUE_32BITS || this.centralDirOffset === utils.MAX_VALUE_32BITS) {
  2068. this.zip64 = true;
  2069. /*
  2070. Warning : the zip64 extension is supported, but ONLY if the 64bits integer read from
  2071. the zip file can fit into a 32bits integer. This cannot be solved : Javascript represents
  2072. all numbers as 64-bit double precision IEEE 754 floating point numbers.
  2073. So, we have 53bits for integers and bitwise operations treat everything as 32bits.
  2074. see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators
  2075. and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf section 8.5
  2076. */
  2077. // should look for a zip64 EOCD locator
  2078. offset = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
  2079. if (offset === -1) {
  2080. throw new Error("Corrupted zip : can't find the ZIP64 end of central directory locator");
  2081. }
  2082. this.reader.setIndex(offset);
  2083. this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
  2084. this.readBlockZip64EndOfCentralLocator();
  2085. // now the zip64 EOCD record
  2086. this.reader.setIndex(this.relativeOffsetEndOfZip64CentralDir);
  2087. this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_END);
  2088. this.readBlockZip64EndOfCentral();
  2089. }
  2090. },
  2091. prepareReader: function(data) {
  2092. var type = utils.getTypeOf(data);
  2093. if (type === "string" && !support.uint8array) {
  2094. this.reader = new StringReader(data, this.loadOptions.optimizedBinaryString);
  2095. }
  2096. else if (type === "nodebuffer") {
  2097. this.reader = new NodeBufferReader(data);
  2098. }
  2099. else {
  2100. this.reader = new Uint8ArrayReader(utils.transformTo("uint8array", data));
  2101. }
  2102. },
  2103. /**
  2104. * Read a zip file and create ZipEntries.
  2105. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the binary string representing a zip file.
  2106. */
  2107. load: function(data) {
  2108. this.prepareReader(data);
  2109. this.readEndOfCentral();
  2110. this.readCentralDir();
  2111. this.readLocalFiles();
  2112. }
  2113. };
  2114. // }}} end of ZipEntries
  2115. module.exports = ZipEntries;
  2116. },{"./nodeBufferReader":12,"./object":13,"./signature":14,"./stringReader":15,"./support":17,"./uint8ArrayReader":18,"./utils":21,"./zipEntry":23}],23:[function(_dereq_,module,exports){
  2117. 'use strict';
  2118. var StringReader = _dereq_('./stringReader');
  2119. var utils = _dereq_('./utils');
  2120. var CompressedObject = _dereq_('./compressedObject');
  2121. var jszipProto = _dereq_('./object');
  2122. // class ZipEntry {{{
  2123. /**
  2124. * An entry in the zip file.
  2125. * @constructor
  2126. * @param {Object} options Options of the current file.
  2127. * @param {Object} loadOptions Options for loading the stream.
  2128. */
  2129. function ZipEntry(options, loadOptions) {
  2130. this.options = options;
  2131. this.loadOptions = loadOptions;
  2132. }
  2133. ZipEntry.prototype = {
  2134. /**
  2135. * say if the file is encrypted.
  2136. * @return {boolean} true if the file is encrypted, false otherwise.
  2137. */
  2138. isEncrypted: function() {
  2139. // bit 1 is set
  2140. return (this.bitFlag & 0x0001) === 0x0001;
  2141. },
  2142. /**
  2143. * say if the file has utf-8 filename/comment.
  2144. * @return {boolean} true if the filename/comment is in utf-8, false otherwise.
  2145. */
  2146. useUTF8: function() {
  2147. // bit 11 is set
  2148. return (this.bitFlag & 0x0800) === 0x0800;
  2149. },
  2150. /**
  2151. * Prepare the function used to generate the compressed content from this ZipFile.
  2152. * @param {DataReader} reader the reader to use.
  2153. * @param {number} from the offset from where we should read the data.
  2154. * @param {number} length the length of the data to read.
  2155. * @return {Function} the callback to get the compressed content (the type depends of the DataReader class).
  2156. */
  2157. prepareCompressedContent: function(reader, from, length) {
  2158. return function() {
  2159. var previousIndex = reader.index;
  2160. reader.setIndex(from);
  2161. var compressedFileData = reader.readData(length);
  2162. reader.setIndex(previousIndex);
  2163. return compressedFileData;
  2164. };
  2165. },
  2166. /**
  2167. * Prepare the function used to generate the uncompressed content from this ZipFile.
  2168. * @param {DataReader} reader the reader to use.
  2169. * @param {number} from the offset from where we should read the data.
  2170. * @param {number} length the length of the data to read.
  2171. * @param {JSZip.compression} compression the compression used on this file.
  2172. * @param {number} uncompressedSize the uncompressed size to expect.
  2173. * @return {Function} the callback to get the uncompressed content (the type depends of the DataReader class).
  2174. */
  2175. prepareContent: function(reader, from, length, compression, uncompressedSize) {
  2176. return function() {
  2177. var compressedFileData = utils.transformTo(compression.uncompressInputType, this.getCompressedContent());
  2178. var uncompressedFileData = compression.uncompress(compressedFileData);
  2179. if (uncompressedFileData.length !== uncompressedSize) {
  2180. throw new Error("Bug : uncompressed data size mismatch");
  2181. }
  2182. return uncompressedFileData;
  2183. };
  2184. },
  2185. /**
  2186. * Read the local part of a zip file and add the info in this object.
  2187. * @param {DataReader} reader the reader to use.
  2188. */
  2189. readLocalPart: function(reader) {
  2190. var compression, localExtraFieldsLength;
  2191. // we already know everything from the central dir !
  2192. // If the central dir data are false, we are doomed.
  2193. // On the bright side, the local part is scary : zip64, data descriptors, both, etc.
  2194. // The less data we get here, the more reliable this should be.
  2195. // Let's skip the whole header and dash to the data !
  2196. reader.skip(22);
  2197. // in some zip created on windows, the filename stored in the central dir contains \ instead of /.
  2198. // Strangely, the filename here is OK.
  2199. // I would love to treat these zip files as corrupted (see http://www.info-zip.org/FAQ.html#backslashes
  2200. // or APPNOTE#4.4.17.1, "All slashes MUST be forward slashes '/'") but there are a lot of bad zip generators...
  2201. // Search "unzip mismatching "local" filename continuing with "central" filename version" on
  2202. // the internet.
  2203. //
  2204. // I think I see the logic here : the central directory is used to display
  2205. // content and the local directory is used to extract the files. Mixing / and \
  2206. // may be used to display \ to windows users and use / when extracting the files.
  2207. // Unfortunately, this lead also to some issues : http://seclists.org/fulldisclosure/2009/Sep/394
  2208. this.fileNameLength = reader.readInt(2);
  2209. localExtraFieldsLength = reader.readInt(2); // can't be sure this will be the same as the central dir
  2210. this.fileName = reader.readString(this.fileNameLength);
  2211. reader.skip(localExtraFieldsLength);
  2212. if (this.compressedSize == -1 || this.uncompressedSize == -1) {
  2213. throw new Error("Bug or corrupted zip : didn't get enough informations from the central directory " + "(compressedSize == -1 || uncompressedSize == -1)");
  2214. }
  2215. compression = utils.findCompression(this.compressionMethod);
  2216. if (compression === null) { // no compression found
  2217. throw new Error("Corrupted zip : compression " + utils.pretty(this.compressionMethod) + " unknown (inner file : " + this.fileName + ")");
  2218. }
  2219. this.decompressed = new CompressedObject();
  2220. this.decompressed.compressedSize = this.compressedSize;
  2221. this.decompressed.uncompressedSize = this.uncompressedSize;
  2222. this.decompressed.crc32 = this.crc32;
  2223. this.decompressed.compressionMethod = this.compressionMethod;
  2224. this.decompressed.getCompressedContent = this.prepareCompressedContent(reader, reader.index, this.compressedSize, compression);
  2225. this.decompressed.getContent = this.prepareContent(reader, reader.index, this.compressedSize, compression, this.uncompressedSize);
  2226. // we need to compute the crc32...
  2227. if (this.loadOptions.checkCRC32) {
  2228. this.decompressed = utils.transformTo("string", this.decompressed.getContent());
  2229. if (jszipProto.crc32(this.decompressed) !== this.crc32) {
  2230. throw new Error("Corrupted zip : CRC32 mismatch");
  2231. }
  2232. }
  2233. },
  2234. /**
  2235. * Read the central part of a zip file and add the info in this object.
  2236. * @param {DataReader} reader the reader to use.
  2237. */
  2238. readCentralPart: function(reader) {
  2239. this.versionMadeBy = reader.readString(2);
  2240. this.versionNeeded = reader.readInt(2);
  2241. this.bitFlag = reader.readInt(2);
  2242. this.compressionMethod = reader.readString(2);
  2243. this.date = reader.readDate();
  2244. this.crc32 = reader.readInt(4);
  2245. this.compressedSize = reader.readInt(4);
  2246. this.uncompressedSize = reader.readInt(4);
  2247. this.fileNameLength = reader.readInt(2);
  2248. this.extraFieldsLength = reader.readInt(2);
  2249. this.fileCommentLength = reader.readInt(2);
  2250. this.diskNumberStart = reader.readInt(2);
  2251. this.internalFileAttributes = reader.readInt(2);
  2252. this.externalFileAttributes = reader.readInt(4);
  2253. this.localHeaderOffset = reader.readInt(4);
  2254. if (this.isEncrypted()) {
  2255. throw new Error("Encrypted zip are not supported");
  2256. }
  2257. this.fileName = reader.readString(this.fileNameLength);
  2258. this.readExtraFields(reader);
  2259. this.parseZIP64ExtraField(reader);
  2260. this.fileComment = reader.readString(this.fileCommentLength);
  2261. // warning, this is true only for zip with madeBy == DOS (plateform dependent feature)
  2262. this.dir = this.externalFileAttributes & 0x00000010 ? true : false;
  2263. },
  2264. /**
  2265. * Parse the ZIP64 extra field and merge the info in the current ZipEntry.
  2266. * @param {DataReader} reader the reader to use.
  2267. */
  2268. parseZIP64ExtraField: function(reader) {
  2269. if (!this.extraFields[0x0001]) {
  2270. return;
  2271. }
  2272. // should be something, preparing the extra reader
  2273. var extraReader = new StringReader(this.extraFields[0x0001].value);
  2274. // I really hope that these 64bits integer can fit in 32 bits integer, because js
  2275. // won't let us have more.
  2276. if (this.uncompressedSize === utils.MAX_VALUE_32BITS) {
  2277. this.uncompressedSize = extraReader.readInt(8);
  2278. }
  2279. if (this.compressedSize === utils.MAX_VALUE_32BITS) {
  2280. this.compressedSize = extraReader.readInt(8);
  2281. }
  2282. if (this.localHeaderOffset === utils.MAX_VALUE_32BITS) {
  2283. this.localHeaderOffset = extraReader.readInt(8);
  2284. }
  2285. if (this.diskNumberStart === utils.MAX_VALUE_32BITS) {
  2286. this.diskNumberStart = extraReader.readInt(4);
  2287. }
  2288. },
  2289. /**
  2290. * Read the central part of a zip file and add the info in this object.
  2291. * @param {DataReader} reader the reader to use.
  2292. */
  2293. readExtraFields: function(reader) {
  2294. var start = reader.index,
  2295. extraFieldId,
  2296. extraFieldLength,
  2297. extraFieldValue;
  2298. this.extraFields = this.extraFields || {};
  2299. while (reader.index < start + this.extraFieldsLength) {
  2300. extraFieldId = reader.readInt(2);
  2301. extraFieldLength = reader.readInt(2);
  2302. extraFieldValue = reader.readString(extraFieldLength);
  2303. this.extraFields[extraFieldId] = {
  2304. id: extraFieldId,
  2305. length: extraFieldLength,
  2306. value: extraFieldValue
  2307. };
  2308. }
  2309. },
  2310. /**
  2311. * Apply an UTF8 transformation if needed.
  2312. */
  2313. handleUTF8: function() {
  2314. if (this.useUTF8()) {
  2315. this.fileName = jszipProto.utf8decode(this.fileName);
  2316. this.fileComment = jszipProto.utf8decode(this.fileComment);
  2317. } else {
  2318. var upath = this.findExtraFieldUnicodePath();
  2319. if (upath !== null) {
  2320. this.fileName = upath;
  2321. }
  2322. var ucomment = this.findExtraFieldUnicodeComment();
  2323. if (ucomment !== null) {
  2324. this.fileComment = ucomment;
  2325. }
  2326. }
  2327. },
  2328. /**
  2329. * Find the unicode path declared in the extra field, if any.
  2330. * @return {String} the unicode path, null otherwise.
  2331. */
  2332. findExtraFieldUnicodePath: function() {
  2333. var upathField = this.extraFields[0x7075];
  2334. if (upathField) {
  2335. var extraReader = new StringReader(upathField.value);
  2336. // wrong version
  2337. if (extraReader.readInt(1) !== 1) {
  2338. return null;
  2339. }
  2340. // the crc of the filename changed, this field is out of date.
  2341. if (jszipProto.crc32(this.fileName) !== extraReader.readInt(4)) {
  2342. return null;
  2343. }
  2344. return jszipProto.utf8decode(extraReader.readString(upathField.length - 5));
  2345. }
  2346. return null;
  2347. },
  2348. /**
  2349. * Find the unicode comment declared in the extra field, if any.
  2350. * @return {String} the unicode comment, null otherwise.
  2351. */
  2352. findExtraFieldUnicodeComment: function() {
  2353. var ucommentField = this.extraFields[0x6375];
  2354. if (ucommentField) {
  2355. var extraReader = new StringReader(ucommentField.value);
  2356. // wrong version
  2357. if (extraReader.readInt(1) !== 1) {
  2358. return null;
  2359. }
  2360. // the crc of the comment changed, this field is out of date.
  2361. if (jszipProto.crc32(this.fileComment) !== extraReader.readInt(4)) {
  2362. return null;
  2363. }
  2364. return jszipProto.utf8decode(extraReader.readString(ucommentField.length - 5));
  2365. }
  2366. return null;
  2367. }
  2368. };
  2369. module.exports = ZipEntry;
  2370. },{"./compressedObject":2,"./object":13,"./stringReader":15,"./utils":21}],24:[function(_dereq_,module,exports){
  2371. // Top level file is just a mixin of submodules & constants
  2372. 'use strict';
  2373. var assign = _dereq_('./lib/utils/common').assign;
  2374. var deflate = _dereq_('./lib/deflate');
  2375. var inflate = _dereq_('./lib/inflate');
  2376. var constants = _dereq_('./lib/zlib/constants');
  2377. var pako = {};
  2378. assign(pako, deflate, inflate, constants);
  2379. module.exports = pako;
  2380. },{"./lib/deflate":25,"./lib/inflate":26,"./lib/utils/common":27,"./lib/zlib/constants":30}],25:[function(_dereq_,module,exports){
  2381. 'use strict';
  2382. var zlib_deflate = _dereq_('./zlib/deflate.js');
  2383. var utils = _dereq_('./utils/common');
  2384. var strings = _dereq_('./utils/strings');
  2385. var msg = _dereq_('./zlib/messages');
  2386. var zstream = _dereq_('./zlib/zstream');
  2387. /* Public constants ==========================================================*/
  2388. /* ===========================================================================*/
  2389. var Z_NO_FLUSH = 0;
  2390. var Z_FINISH = 4;
  2391. var Z_OK = 0;
  2392. var Z_STREAM_END = 1;
  2393. var Z_DEFAULT_COMPRESSION = -1;
  2394. var Z_DEFAULT_STRATEGY = 0;
  2395. var Z_DEFLATED = 8;
  2396. /* ===========================================================================*/
  2397. /**
  2398. * class Deflate
  2399. *
  2400. * Generic JS-style wrapper for zlib calls. If you don't need
  2401. * streaming behaviour - use more simple functions: [[deflate]],
  2402. * [[deflateRaw]] and [[gzip]].
  2403. **/
  2404. /* internal
  2405. * Deflate.chunks -> Array
  2406. *
  2407. * Chunks of output data, if [[Deflate#onData]] not overriden.
  2408. **/
  2409. /**
  2410. * Deflate.result -> Uint8Array|Array
  2411. *
  2412. * Compressed result, generated by default [[Deflate#onData]]
  2413. * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
  2414. * (call [[Deflate#push]] with `Z_FINISH` / `true` param).
  2415. **/
  2416. /**
  2417. * Deflate.err -> Number
  2418. *
  2419. * Error code after deflate finished. 0 (Z_OK) on success.
  2420. * You will not need it in real life, because deflate errors
  2421. * are possible only on wrong options or bad `onData` / `onEnd`
  2422. * custom handlers.
  2423. **/
  2424. /**
  2425. * Deflate.msg -> String
  2426. *
  2427. * Error message, if [[Deflate.err]] != 0
  2428. **/
  2429. /**
  2430. * new Deflate(options)
  2431. * - options (Object): zlib deflate options.
  2432. *
  2433. * Creates new deflator instance with specified params. Throws exception
  2434. * on bad params. Supported options:
  2435. *
  2436. * - `level`
  2437. * - `windowBits`
  2438. * - `memLevel`
  2439. * - `strategy`
  2440. *
  2441. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  2442. * for more information on these.
  2443. *
  2444. * Additional options, for internal needs:
  2445. *
  2446. * - `chunkSize` - size of generated data chunks (16K by default)
  2447. * - `raw` (Boolean) - do raw deflate
  2448. * - `gzip` (Boolean) - create gzip wrapper
  2449. * - `to` (String) - if equal to 'string', then result will be "binary string"
  2450. * (each char code [0..255])
  2451. * - `header` (Object) - custom header for gzip
  2452. * - `text` (Boolean) - true if compressed data believed to be text
  2453. * - `time` (Number) - modification time, unix timestamp
  2454. * - `os` (Number) - operation system code
  2455. * - `extra` (Array) - array of bytes with extra data (max 65536)
  2456. * - `name` (String) - file name (binary string)
  2457. * - `comment` (String) - comment (binary string)
  2458. * - `hcrc` (Boolean) - true if header crc should be added
  2459. *
  2460. * ##### Example:
  2461. *
  2462. * ```javascript
  2463. * var pako = require('pako')
  2464. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  2465. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  2466. *
  2467. * var deflate = new pako.Deflate({ level: 3});
  2468. *
  2469. * deflate.push(chunk1, false);
  2470. * deflate.push(chunk2, true); // true -> last chunk
  2471. *
  2472. * if (deflate.err) { throw new Error(deflate.err); }
  2473. *
  2474. * console.log(deflate.result);
  2475. * ```
  2476. **/
  2477. var Deflate = function(options) {
  2478. this.options = utils.assign({
  2479. level: Z_DEFAULT_COMPRESSION,
  2480. method: Z_DEFLATED,
  2481. chunkSize: 16384,
  2482. windowBits: 15,
  2483. memLevel: 8,
  2484. strategy: Z_DEFAULT_STRATEGY,
  2485. to: ''
  2486. }, options || {});
  2487. var opt = this.options;
  2488. if (opt.raw && (opt.windowBits > 0)) {
  2489. opt.windowBits = -opt.windowBits;
  2490. }
  2491. else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
  2492. opt.windowBits += 16;
  2493. }
  2494. this.err = 0; // error code, if happens (0 = Z_OK)
  2495. this.msg = ''; // error message
  2496. this.ended = false; // used to avoid multiple onEnd() calls
  2497. this.chunks = []; // chunks of compressed data
  2498. this.strm = new zstream();
  2499. this.strm.avail_out = 0;
  2500. var status = zlib_deflate.deflateInit2(
  2501. this.strm,
  2502. opt.level,
  2503. opt.method,
  2504. opt.windowBits,
  2505. opt.memLevel,
  2506. opt.strategy
  2507. );
  2508. if (status !== Z_OK) {
  2509. throw new Error(msg[status]);
  2510. }
  2511. if (opt.header) {
  2512. zlib_deflate.deflateSetHeader(this.strm, opt.header);
  2513. }
  2514. };
  2515. /**
  2516. * Deflate#push(data[, mode]) -> Boolean
  2517. * - data (Uint8Array|Array|String): input data. Strings will be converted to
  2518. * utf8 byte sequence.
  2519. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  2520. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
  2521. *
  2522. * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
  2523. * new compressed chunks. Returns `true` on success. The last data block must have
  2524. * mode Z_FINISH (or `true`). That flush internal pending buffers and call
  2525. * [[Deflate#onEnd]].
  2526. *
  2527. * On fail call [[Deflate#onEnd]] with error code and return false.
  2528. *
  2529. * We strongly recommend to use `Uint8Array` on input for best speed (output
  2530. * array format is detected automatically). Also, don't skip last param and always
  2531. * use the same type in your code (boolean or number). That will improve JS speed.
  2532. *
  2533. * For regular `Array`-s make sure all elements are [0..255].
  2534. *
  2535. * ##### Example
  2536. *
  2537. * ```javascript
  2538. * push(chunk, false); // push one of data chunks
  2539. * ...
  2540. * push(chunk, true); // push last chunk
  2541. * ```
  2542. **/
  2543. Deflate.prototype.push = function(data, mode) {
  2544. var strm = this.strm;
  2545. var chunkSize = this.options.chunkSize;
  2546. var status, _mode;
  2547. if (this.ended) { return false; }
  2548. _mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
  2549. // Convert data if needed
  2550. if (typeof data === 'string') {
  2551. // If we need to compress text, change encoding to utf8.
  2552. strm.input = strings.string2buf(data);
  2553. } else {
  2554. strm.input = data;
  2555. }
  2556. strm.next_in = 0;
  2557. strm.avail_in = strm.input.length;
  2558. do {
  2559. if (strm.avail_out === 0) {
  2560. strm.output = new utils.Buf8(chunkSize);
  2561. strm.next_out = 0;
  2562. strm.avail_out = chunkSize;
  2563. }
  2564. status = zlib_deflate.deflate(strm, _mode); /* no bad return value */
  2565. if (status !== Z_STREAM_END && status !== Z_OK) {
  2566. this.onEnd(status);
  2567. this.ended = true;
  2568. return false;
  2569. }
  2570. if (strm.avail_out === 0 || (strm.avail_in === 0 && _mode === Z_FINISH)) {
  2571. if (this.options.to === 'string') {
  2572. this.onData(strings.buf2binstring(utils.shrinkBuf(strm.output, strm.next_out)));
  2573. } else {
  2574. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  2575. }
  2576. }
  2577. } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END);
  2578. // Finalize on the last chunk.
  2579. if (_mode === Z_FINISH) {
  2580. status = zlib_deflate.deflateEnd(this.strm);
  2581. this.onEnd(status);
  2582. this.ended = true;
  2583. return status === Z_OK;
  2584. }
  2585. return true;
  2586. };
  2587. /**
  2588. * Deflate#onData(chunk) -> Void
  2589. * - chunk (Uint8Array|Array|String): ouput data. Type of array depends
  2590. * on js engine support. When string output requested, each chunk
  2591. * will be string.
  2592. *
  2593. * By default, stores data blocks in `chunks[]` property and glue
  2594. * those in `onEnd`. Override this handler, if you need another behaviour.
  2595. **/
  2596. Deflate.prototype.onData = function(chunk) {
  2597. this.chunks.push(chunk);
  2598. };
  2599. /**
  2600. * Deflate#onEnd(status) -> Void
  2601. * - status (Number): deflate status. 0 (Z_OK) on success,
  2602. * other if not.
  2603. *
  2604. * Called once after you tell deflate that input stream complete
  2605. * or error happenned. By default - join collected chunks,
  2606. * free memory and fill `results` / `err` properties.
  2607. **/
  2608. Deflate.prototype.onEnd = function(status) {
  2609. // On success - join
  2610. if (status === Z_OK) {
  2611. if (this.options.to === 'string') {
  2612. this.result = this.chunks.join('');
  2613. } else {
  2614. this.result = utils.flattenChunks(this.chunks);
  2615. }
  2616. }
  2617. this.chunks = [];
  2618. this.err = status;
  2619. this.msg = this.strm.msg;
  2620. };
  2621. /**
  2622. * deflate(data[, options]) -> Uint8Array|Array|String
  2623. * - data (Uint8Array|Array|String): input data to compress.
  2624. * - options (Object): zlib deflate options.
  2625. *
  2626. * Compress `data` with deflate alrorythm and `options`.
  2627. *
  2628. * Supported options are:
  2629. *
  2630. * - level
  2631. * - windowBits
  2632. * - memLevel
  2633. * - strategy
  2634. *
  2635. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  2636. * for more information on these.
  2637. *
  2638. * Sugar (options):
  2639. *
  2640. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  2641. * negative windowBits implicitly.
  2642. * - `to` (String) - if equal to 'string', then result will be "binary string"
  2643. * (each char code [0..255])
  2644. *
  2645. * ##### Example:
  2646. *
  2647. * ```javascript
  2648. * var pako = require('pako')
  2649. * , data = Uint8Array([1,2,3,4,5,6,7,8,9]);
  2650. *
  2651. * console.log(pako.deflate(data));
  2652. * ```
  2653. **/
  2654. function deflate(input, options) {
  2655. var deflator = new Deflate(options);
  2656. deflator.push(input, true);
  2657. // That will never happens, if you don't cheat with options :)
  2658. if (deflator.err) { throw deflator.msg; }
  2659. return deflator.result;
  2660. }
  2661. /**
  2662. * deflateRaw(data[, options]) -> Uint8Array|Array|String
  2663. * - data (Uint8Array|Array|String): input data to compress.
  2664. * - options (Object): zlib deflate options.
  2665. *
  2666. * The same as [[deflate]], but creates raw data, without wrapper
  2667. * (header and adler32 crc).
  2668. **/
  2669. function deflateRaw(input, options) {
  2670. options = options || {};
  2671. options.raw = true;
  2672. return deflate(input, options);
  2673. }
  2674. /**
  2675. * gzip(data[, options]) -> Uint8Array|Array|String
  2676. * - data (Uint8Array|Array|String): input data to compress.
  2677. * - options (Object): zlib deflate options.
  2678. *
  2679. * The same as [[deflate]], but create gzip wrapper instead of
  2680. * deflate one.
  2681. **/
  2682. function gzip(input, options) {
  2683. options = options || {};
  2684. options.gzip = true;
  2685. return deflate(input, options);
  2686. }
  2687. exports.Deflate = Deflate;
  2688. exports.deflate = deflate;
  2689. exports.deflateRaw = deflateRaw;
  2690. exports.gzip = gzip;
  2691. },{"./utils/common":27,"./utils/strings":28,"./zlib/deflate.js":32,"./zlib/messages":37,"./zlib/zstream":39}],26:[function(_dereq_,module,exports){
  2692. 'use strict';
  2693. var zlib_inflate = _dereq_('./zlib/inflate.js');
  2694. var utils = _dereq_('./utils/common');
  2695. var strings = _dereq_('./utils/strings');
  2696. var c = _dereq_('./zlib/constants');
  2697. var msg = _dereq_('./zlib/messages');
  2698. var zstream = _dereq_('./zlib/zstream');
  2699. var gzheader = _dereq_('./zlib/gzheader');
  2700. /**
  2701. * class Inflate
  2702. *
  2703. * Generic JS-style wrapper for zlib calls. If you don't need
  2704. * streaming behaviour - use more simple functions: [[inflate]]
  2705. * and [[inflateRaw]].
  2706. **/
  2707. /* internal
  2708. * inflate.chunks -> Array
  2709. *
  2710. * Chunks of output data, if [[Inflate#onData]] not overriden.
  2711. **/
  2712. /**
  2713. * Inflate.result -> Uint8Array|Array|String
  2714. *
  2715. * Uncompressed result, generated by default [[Inflate#onData]]
  2716. * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
  2717. * (call [[Inflate#push]] with `Z_FINISH` / `true` param).
  2718. **/
  2719. /**
  2720. * Inflate.err -> Number
  2721. *
  2722. * Error code after inflate finished. 0 (Z_OK) on success.
  2723. * Should be checked if broken data possible.
  2724. **/
  2725. /**
  2726. * Inflate.msg -> String
  2727. *
  2728. * Error message, if [[Inflate.err]] != 0
  2729. **/
  2730. /**
  2731. * new Inflate(options)
  2732. * - options (Object): zlib inflate options.
  2733. *
  2734. * Creates new inflator instance with specified params. Throws exception
  2735. * on bad params. Supported options:
  2736. *
  2737. * - `windowBits`
  2738. *
  2739. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  2740. * for more information on these.
  2741. *
  2742. * Additional options, for internal needs:
  2743. *
  2744. * - `chunkSize` - size of generated data chunks (16K by default)
  2745. * - `raw` (Boolean) - do raw inflate
  2746. * - `to` (String) - if equal to 'string', then result will be converted
  2747. * from utf8 to utf16 (javascript) string. When string output requested,
  2748. * chunk length can differ from `chunkSize`, depending on content.
  2749. *
  2750. * By default, when no options set, autodetect deflate/gzip data format via
  2751. * wrapper header.
  2752. *
  2753. * ##### Example:
  2754. *
  2755. * ```javascript
  2756. * var pako = require('pako')
  2757. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  2758. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  2759. *
  2760. * var inflate = new pako.Inflate({ level: 3});
  2761. *
  2762. * inflate.push(chunk1, false);
  2763. * inflate.push(chunk2, true); // true -> last chunk
  2764. *
  2765. * if (inflate.err) { throw new Error(inflate.err); }
  2766. *
  2767. * console.log(inflate.result);
  2768. * ```
  2769. **/
  2770. var Inflate = function(options) {
  2771. this.options = utils.assign({
  2772. chunkSize: 16384,
  2773. windowBits: 0,
  2774. to: ''
  2775. }, options || {});
  2776. var opt = this.options;
  2777. // Force window size for `raw` data, if not set directly,
  2778. // because we have no header for autodetect.
  2779. if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
  2780. opt.windowBits = -opt.windowBits;
  2781. if (opt.windowBits === 0) { opt.windowBits = -15; }
  2782. }
  2783. // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
  2784. if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
  2785. !(options && options.windowBits)) {
  2786. opt.windowBits += 32;
  2787. }
  2788. // Gzip header has no info about windows size, we can do autodetect only
  2789. // for deflate. So, if window size not set, force it to max when gzip possible
  2790. if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
  2791. // bit 3 (16) -> gzipped data
  2792. // bit 4 (32) -> autodetect gzip/deflate
  2793. if ((opt.windowBits & 15) === 0) {
  2794. opt.windowBits |= 15;
  2795. }
  2796. }
  2797. this.err = 0; // error code, if happens (0 = Z_OK)
  2798. this.msg = ''; // error message
  2799. this.ended = false; // used to avoid multiple onEnd() calls
  2800. this.chunks = []; // chunks of compressed data
  2801. this.strm = new zstream();
  2802. this.strm.avail_out = 0;
  2803. var status = zlib_inflate.inflateInit2(
  2804. this.strm,
  2805. opt.windowBits
  2806. );
  2807. if (status !== c.Z_OK) {
  2808. throw new Error(msg[status]);
  2809. }
  2810. this.header = new gzheader();
  2811. zlib_inflate.inflateGetHeader(this.strm, this.header);
  2812. };
  2813. /**
  2814. * Inflate#push(data[, mode]) -> Boolean
  2815. * - data (Uint8Array|Array|String): input data
  2816. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  2817. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
  2818. *
  2819. * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
  2820. * new output chunks. Returns `true` on success. The last data block must have
  2821. * mode Z_FINISH (or `true`). That flush internal pending buffers and call
  2822. * [[Inflate#onEnd]].
  2823. *
  2824. * On fail call [[Inflate#onEnd]] with error code and return false.
  2825. *
  2826. * We strongly recommend to use `Uint8Array` on input for best speed (output
  2827. * format is detected automatically). Also, don't skip last param and always
  2828. * use the same type in your code (boolean or number). That will improve JS speed.
  2829. *
  2830. * For regular `Array`-s make sure all elements are [0..255].
  2831. *
  2832. * ##### Example
  2833. *
  2834. * ```javascript
  2835. * push(chunk, false); // push one of data chunks
  2836. * ...
  2837. * push(chunk, true); // push last chunk
  2838. * ```
  2839. **/
  2840. Inflate.prototype.push = function(data, mode) {
  2841. var strm = this.strm;
  2842. var chunkSize = this.options.chunkSize;
  2843. var status, _mode;
  2844. var next_out_utf8, tail, utf8str;
  2845. if (this.ended) { return false; }
  2846. _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
  2847. // Convert data if needed
  2848. if (typeof data === 'string') {
  2849. // Only binary strings can be decompressed on practice
  2850. strm.input = strings.binstring2buf(data);
  2851. } else {
  2852. strm.input = data;
  2853. }
  2854. strm.next_in = 0;
  2855. strm.avail_in = strm.input.length;
  2856. do {
  2857. if (strm.avail_out === 0) {
  2858. strm.output = new utils.Buf8(chunkSize);
  2859. strm.next_out = 0;
  2860. strm.avail_out = chunkSize;
  2861. }
  2862. status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
  2863. if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
  2864. this.onEnd(status);
  2865. this.ended = true;
  2866. return false;
  2867. }
  2868. if (strm.next_out) {
  2869. if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && _mode === c.Z_FINISH)) {
  2870. if (this.options.to === 'string') {
  2871. next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
  2872. tail = strm.next_out - next_out_utf8;
  2873. utf8str = strings.buf2string(strm.output, next_out_utf8);
  2874. // move tail
  2875. strm.next_out = tail;
  2876. strm.avail_out = chunkSize - tail;
  2877. if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
  2878. this.onData(utf8str);
  2879. } else {
  2880. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  2881. }
  2882. }
  2883. }
  2884. } while ((strm.avail_in > 0) && status !== c.Z_STREAM_END);
  2885. if (status === c.Z_STREAM_END) {
  2886. _mode = c.Z_FINISH;
  2887. }
  2888. // Finalize on the last chunk.
  2889. if (_mode === c.Z_FINISH) {
  2890. status = zlib_inflate.inflateEnd(this.strm);
  2891. this.onEnd(status);
  2892. this.ended = true;
  2893. return status === c.Z_OK;
  2894. }
  2895. return true;
  2896. };
  2897. /**
  2898. * Inflate#onData(chunk) -> Void
  2899. * - chunk (Uint8Array|Array|String): ouput data. Type of array depends
  2900. * on js engine support. When string output requested, each chunk
  2901. * will be string.
  2902. *
  2903. * By default, stores data blocks in `chunks[]` property and glue
  2904. * those in `onEnd`. Override this handler, if you need another behaviour.
  2905. **/
  2906. Inflate.prototype.onData = function(chunk) {
  2907. this.chunks.push(chunk);
  2908. };
  2909. /**
  2910. * Inflate#onEnd(status) -> Void
  2911. * - status (Number): inflate status. 0 (Z_OK) on success,
  2912. * other if not.
  2913. *
  2914. * Called once after you tell inflate that input stream complete
  2915. * or error happenned. By default - join collected chunks,
  2916. * free memory and fill `results` / `err` properties.
  2917. **/
  2918. Inflate.prototype.onEnd = function(status) {
  2919. // On success - join
  2920. if (status === c.Z_OK) {
  2921. if (this.options.to === 'string') {
  2922. // Glue & convert here, until we teach pako to send
  2923. // utf8 alligned strings to onData
  2924. this.result = this.chunks.join('');
  2925. } else {
  2926. this.result = utils.flattenChunks(this.chunks);
  2927. }
  2928. }
  2929. this.chunks = [];
  2930. this.err = status;
  2931. this.msg = this.strm.msg;
  2932. };
  2933. /**
  2934. * inflate(data[, options]) -> Uint8Array|Array|String
  2935. * - data (Uint8Array|Array|String): input data to decompress.
  2936. * - options (Object): zlib inflate options.
  2937. *
  2938. * Decompress `data` with inflate/ungzip and `options`. Autodetect
  2939. * format via wrapper header by default. That's why we don't provide
  2940. * separate `ungzip` method.
  2941. *
  2942. * Supported options are:
  2943. *
  2944. * - windowBits
  2945. *
  2946. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  2947. * for more information.
  2948. *
  2949. * Sugar (options):
  2950. *
  2951. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  2952. * negative windowBits implicitly.
  2953. * - `to` (String) - if equal to 'string', then result will be converted
  2954. * from utf8 to utf16 (javascript) string. When string output requested,
  2955. * chunk length can differ from `chunkSize`, depending on content.
  2956. *
  2957. *
  2958. * ##### Example:
  2959. *
  2960. * ```javascript
  2961. * var pako = require('pako')
  2962. * , input = pako.deflate([1,2,3,4,5,6,7,8,9])
  2963. * , output;
  2964. *
  2965. * try {
  2966. * output = pako.inflate(input);
  2967. * } catch (err)
  2968. * console.log(err);
  2969. * }
  2970. * ```
  2971. **/
  2972. function inflate(input, options) {
  2973. var inflator = new Inflate(options);
  2974. inflator.push(input, true);
  2975. // That will never happens, if you don't cheat with options :)
  2976. if (inflator.err) { throw inflator.msg; }
  2977. return inflator.result;
  2978. }
  2979. /**
  2980. * inflateRaw(data[, options]) -> Uint8Array|Array|String
  2981. * - data (Uint8Array|Array|String): input data to decompress.
  2982. * - options (Object): zlib inflate options.
  2983. *
  2984. * The same as [[inflate]], but creates raw data, without wrapper
  2985. * (header and adler32 crc).
  2986. **/
  2987. function inflateRaw(input, options) {
  2988. options = options || {};
  2989. options.raw = true;
  2990. return inflate(input, options);
  2991. }
  2992. /**
  2993. * ungzip(data[, options]) -> Uint8Array|Array|String
  2994. * - data (Uint8Array|Array|String): input data to decompress.
  2995. * - options (Object): zlib inflate options.
  2996. *
  2997. * Just shortcut to [[inflate]], because it autodetects format
  2998. * by header.content. Done for convenience.
  2999. **/
  3000. exports.Inflate = Inflate;
  3001. exports.inflate = inflate;
  3002. exports.inflateRaw = inflateRaw;
  3003. exports.ungzip = inflate;
  3004. },{"./utils/common":27,"./utils/strings":28,"./zlib/constants":30,"./zlib/gzheader":33,"./zlib/inflate.js":35,"./zlib/messages":37,"./zlib/zstream":39}],27:[function(_dereq_,module,exports){
  3005. 'use strict';
  3006. var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
  3007. (typeof Uint16Array !== 'undefined') &&
  3008. (typeof Int32Array !== 'undefined');
  3009. exports.assign = function (obj /*from1, from2, from3, ...*/) {
  3010. var sources = Array.prototype.slice.call(arguments, 1);
  3011. while (sources.length) {
  3012. var source = sources.shift();
  3013. if (!source) { continue; }
  3014. if (typeof(source) !== 'object') {
  3015. throw new TypeError(source + 'must be non-object');
  3016. }
  3017. for (var p in source) {
  3018. if (source.hasOwnProperty(p)) {
  3019. obj[p] = source[p];
  3020. }
  3021. }
  3022. }
  3023. return obj;
  3024. };
  3025. // reduce buffer size, avoiding mem copy
  3026. exports.shrinkBuf = function (buf, size) {
  3027. if (buf.length === size) { return buf; }
  3028. if (buf.subarray) { return buf.subarray(0, size); }
  3029. buf.length = size;
  3030. return buf;
  3031. };
  3032. var fnTyped = {
  3033. arraySet: function (dest, src, src_offs, len, dest_offs) {
  3034. if (src.subarray && dest.subarray) {
  3035. dest.set(src.subarray(src_offs, src_offs+len), dest_offs);
  3036. return;
  3037. }
  3038. // Fallback to ordinary array
  3039. for(var i=0; i<len; i++) {
  3040. dest[dest_offs + i] = src[src_offs + i];
  3041. }
  3042. },
  3043. // Join array of chunks to single array.
  3044. flattenChunks: function(chunks) {
  3045. var i, l, len, pos, chunk, result;
  3046. // calculate data length
  3047. len = 0;
  3048. for (i=0, l=chunks.length; i<l; i++) {
  3049. len += chunks[i].length;
  3050. }
  3051. // join chunks
  3052. result = new Uint8Array(len);
  3053. pos = 0;
  3054. for (i=0, l=chunks.length; i<l; i++) {
  3055. chunk = chunks[i];
  3056. result.set(chunk, pos);
  3057. pos += chunk.length;
  3058. }
  3059. return result;
  3060. }
  3061. };
  3062. var fnUntyped = {
  3063. arraySet: function (dest, src, src_offs, len, dest_offs) {
  3064. for(var i=0; i<len; i++) {
  3065. dest[dest_offs + i] = src[src_offs + i];
  3066. }
  3067. },
  3068. // Join array of chunks to single array.
  3069. flattenChunks: function(chunks) {
  3070. return [].concat.apply([], chunks);
  3071. }
  3072. };
  3073. // Enable/Disable typed arrays use, for testing
  3074. //
  3075. exports.setTyped = function (on) {
  3076. if (on) {
  3077. exports.Buf8 = Uint8Array;
  3078. exports.Buf16 = Uint16Array;
  3079. exports.Buf32 = Int32Array;
  3080. exports.assign(exports, fnTyped);
  3081. } else {
  3082. exports.Buf8 = Array;
  3083. exports.Buf16 = Array;
  3084. exports.Buf32 = Array;
  3085. exports.assign(exports, fnUntyped);
  3086. }
  3087. };
  3088. exports.setTyped(TYPED_OK);
  3089. },{}],28:[function(_dereq_,module,exports){
  3090. // String encode/decode helpers
  3091. 'use strict';
  3092. var utils = _dereq_('./common');
  3093. // Quick check if we can use fast array to bin string conversion
  3094. //
  3095. // - apply(Array) can fail on Android 2.2
  3096. // - apply(Uint8Array) can fail on iOS 5.1 Safary
  3097. //
  3098. var STR_APPLY_OK = true;
  3099. var STR_APPLY_UIA_OK = true;
  3100. try { String.fromCharCode.apply(null, [0]); } catch(__) { STR_APPLY_OK = false; }
  3101. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch(__) { STR_APPLY_UIA_OK = false; }
  3102. // Table with utf8 lengths (calculated by first byte of sequence)
  3103. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  3104. // because max possible codepoint is 0x10ffff
  3105. var _utf8len = new utils.Buf8(256);
  3106. for (var i=0; i<256; i++) {
  3107. _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
  3108. }
  3109. _utf8len[254]=_utf8len[254]=1; // Invalid sequence start
  3110. // convert string to array (typed, when possible)
  3111. exports.string2buf = function (str) {
  3112. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  3113. // count binary size
  3114. for (m_pos = 0; m_pos < str_len; m_pos++) {
  3115. c = str.charCodeAt(m_pos);
  3116. if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
  3117. c2 = str.charCodeAt(m_pos+1);
  3118. if ((c2 & 0xfc00) === 0xdc00) {
  3119. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  3120. m_pos++;
  3121. }
  3122. }
  3123. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  3124. }
  3125. // allocate buffer
  3126. buf = new utils.Buf8(buf_len);
  3127. // convert
  3128. for (i=0, m_pos = 0; i < buf_len; m_pos++) {
  3129. c = str.charCodeAt(m_pos);
  3130. if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
  3131. c2 = str.charCodeAt(m_pos+1);
  3132. if ((c2 & 0xfc00) === 0xdc00) {
  3133. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  3134. m_pos++;
  3135. }
  3136. }
  3137. if (c < 0x80) {
  3138. /* one byte */
  3139. buf[i++] = c;
  3140. } else if (c < 0x800) {
  3141. /* two bytes */
  3142. buf[i++] = 0xC0 | (c >>> 6);
  3143. buf[i++] = 0x80 | (c & 0x3f);
  3144. } else if (c < 0x10000) {
  3145. /* three bytes */
  3146. buf[i++] = 0xE0 | (c >>> 12);
  3147. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  3148. buf[i++] = 0x80 | (c & 0x3f);
  3149. } else {
  3150. /* four bytes */
  3151. buf[i++] = 0xf0 | (c >>> 18);
  3152. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  3153. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  3154. buf[i++] = 0x80 | (c & 0x3f);
  3155. }
  3156. }
  3157. return buf;
  3158. };
  3159. // Helper (used in 2 places)
  3160. function buf2binstring(buf, len) {
  3161. // use fallback for big arrays to avoid stack overflow
  3162. if (len < 65537) {
  3163. if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
  3164. return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
  3165. }
  3166. }
  3167. var result = '';
  3168. for(var i=0; i < len; i++) {
  3169. result += String.fromCharCode(buf[i]);
  3170. }
  3171. return result;
  3172. }
  3173. // Convert byte array to binary string
  3174. exports.buf2binstring = function(buf) {
  3175. return buf2binstring(buf, buf.length);
  3176. };
  3177. // Convert binary string (typed, when possible)
  3178. exports.binstring2buf = function(str) {
  3179. var buf = new utils.Buf8(str.length);
  3180. for(var i=0, len=buf.length; i < len; i++) {
  3181. buf[i] = str.charCodeAt(i);
  3182. }
  3183. return buf;
  3184. };
  3185. // convert array to string
  3186. exports.buf2string = function (buf, max) {
  3187. var i, out, c, c_len;
  3188. var len = max || buf.length;
  3189. // Reserve max possible length (2 words per char)
  3190. // NB: by unknown reasons, Array is significantly faster for
  3191. // String.fromCharCode.apply than Uint16Array.
  3192. var utf16buf = new Array(len*2);
  3193. for (out=0, i=0; i<len;) {
  3194. c = buf[i++];
  3195. // quick process ascii
  3196. if (c < 0x80) { utf16buf[out++] = c; continue; }
  3197. c_len = _utf8len[c];
  3198. // skip 5 & 6 byte codes
  3199. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
  3200. // apply mask on first byte
  3201. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  3202. // join the rest
  3203. while (c_len > 1 && i < len) {
  3204. c = (c << 6) | (buf[i++] & 0x3f);
  3205. c_len--;
  3206. }
  3207. // terminated by end of string?
  3208. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  3209. if (c < 0x10000) {
  3210. utf16buf[out++] = c;
  3211. } else {
  3212. c -= 0x10000;
  3213. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  3214. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  3215. }
  3216. }
  3217. return buf2binstring(utf16buf, out);
  3218. };
  3219. // Calculate max possible position in utf8 buffer,
  3220. // that will not break sequence. If that's not possible
  3221. // - (very small limits) return max size as is.
  3222. //
  3223. // buf[] - utf8 bytes array
  3224. // max - length limit (mandatory);
  3225. exports.utf8border = function(buf, max) {
  3226. var pos;
  3227. max = max || buf.length;
  3228. if (max > buf.length) { max = buf.length; }
  3229. // go back from last position, until start of sequence found
  3230. pos = max-1;
  3231. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  3232. // Fuckup - very small and broken sequence,
  3233. // return max, because we should return something anyway.
  3234. if (pos < 0) { return max; }
  3235. // If we came to start of buffer - that means vuffer is too small,
  3236. // return max too.
  3237. if (pos === 0) { return max; }
  3238. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  3239. };
  3240. },{"./common":27}],29:[function(_dereq_,module,exports){
  3241. 'use strict';
  3242. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  3243. // It doesn't worth to make additional optimizationa as in original.
  3244. // Small size is preferable.
  3245. function adler32(adler, buf, len, pos) {
  3246. var s1 = (adler & 0xffff) |0
  3247. , s2 = ((adler >>> 16) & 0xffff) |0
  3248. , n = 0;
  3249. while (len !== 0) {
  3250. // Set limit ~ twice less than 5552, to keep
  3251. // s2 in 31-bits, because we force signed ints.
  3252. // in other case %= will fail.
  3253. n = len > 2000 ? 2000 : len;
  3254. len -= n;
  3255. do {
  3256. s1 = (s1 + buf[pos++]) |0;
  3257. s2 = (s2 + s1) |0;
  3258. } while (--n);
  3259. s1 %= 65521;
  3260. s2 %= 65521;
  3261. }
  3262. return (s1 | (s2 << 16)) |0;
  3263. }
  3264. module.exports = adler32;
  3265. },{}],30:[function(_dereq_,module,exports){
  3266. module.exports = {
  3267. /* Allowed flush values; see deflate() and inflate() below for details */
  3268. Z_NO_FLUSH: 0,
  3269. Z_PARTIAL_FLUSH: 1,
  3270. Z_SYNC_FLUSH: 2,
  3271. Z_FULL_FLUSH: 3,
  3272. Z_FINISH: 4,
  3273. Z_BLOCK: 5,
  3274. Z_TREES: 6,
  3275. /* Return codes for the compression/decompression functions. Negative values
  3276. * are errors, positive values are used for special but normal events.
  3277. */
  3278. Z_OK: 0,
  3279. Z_STREAM_END: 1,
  3280. Z_NEED_DICT: 2,
  3281. Z_ERRNO: -1,
  3282. Z_STREAM_ERROR: -2,
  3283. Z_DATA_ERROR: -3,
  3284. //Z_MEM_ERROR: -4,
  3285. Z_BUF_ERROR: -5,
  3286. //Z_VERSION_ERROR: -6,
  3287. /* compression levels */
  3288. Z_NO_COMPRESSION: 0,
  3289. Z_BEST_SPEED: 1,
  3290. Z_BEST_COMPRESSION: 9,
  3291. Z_DEFAULT_COMPRESSION: -1,
  3292. Z_FILTERED: 1,
  3293. Z_HUFFMAN_ONLY: 2,
  3294. Z_RLE: 3,
  3295. Z_FIXED: 4,
  3296. Z_DEFAULT_STRATEGY: 0,
  3297. /* Possible values of the data_type field (though see inflate()) */
  3298. Z_BINARY: 0,
  3299. Z_TEXT: 1,
  3300. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  3301. Z_UNKNOWN: 2,
  3302. /* The deflate compression method */
  3303. Z_DEFLATED: 8
  3304. //Z_NULL: null // Use -1 or null inline, depending on var type
  3305. };
  3306. },{}],31:[function(_dereq_,module,exports){
  3307. 'use strict';
  3308. // Note: we can't get significant speed boost here.
  3309. // So write code to minimize size - no pregenerated tables
  3310. // and array tools dependencies.
  3311. // Use ordinary array, since untyped makes no boost here
  3312. function makeTable() {
  3313. var c, table = [];
  3314. for(var n =0; n < 256; n++){
  3315. c = n;
  3316. for(var k =0; k < 8; k++){
  3317. c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  3318. }
  3319. table[n] = c;
  3320. }
  3321. return table;
  3322. }
  3323. // Create table on load. Just 255 signed longs. Not a problem.
  3324. var crcTable = makeTable();
  3325. function crc32(crc, buf, len, pos) {
  3326. var t = crcTable
  3327. , end = pos + len;
  3328. crc = crc ^ (-1);
  3329. for (var i = pos; i < end; i++ ) {
  3330. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  3331. }
  3332. return (crc ^ (-1)); // >>> 0;
  3333. }
  3334. module.exports = crc32;
  3335. },{}],32:[function(_dereq_,module,exports){
  3336. 'use strict';
  3337. var utils = _dereq_('../utils/common');
  3338. var trees = _dereq_('./trees');
  3339. var adler32 = _dereq_('./adler32');
  3340. var crc32 = _dereq_('./crc32');
  3341. var msg = _dereq_('./messages');
  3342. /* Public constants ==========================================================*/
  3343. /* ===========================================================================*/
  3344. /* Allowed flush values; see deflate() and inflate() below for details */
  3345. var Z_NO_FLUSH = 0;
  3346. var Z_PARTIAL_FLUSH = 1;
  3347. //var Z_SYNC_FLUSH = 2;
  3348. var Z_FULL_FLUSH = 3;
  3349. var Z_FINISH = 4;
  3350. var Z_BLOCK = 5;
  3351. //var Z_TREES = 6;
  3352. /* Return codes for the compression/decompression functions. Negative values
  3353. * are errors, positive values are used for special but normal events.
  3354. */
  3355. var Z_OK = 0;
  3356. var Z_STREAM_END = 1;
  3357. //var Z_NEED_DICT = 2;
  3358. //var Z_ERRNO = -1;
  3359. var Z_STREAM_ERROR = -2;
  3360. var Z_DATA_ERROR = -3;
  3361. //var Z_MEM_ERROR = -4;
  3362. var Z_BUF_ERROR = -5;
  3363. //var Z_VERSION_ERROR = -6;
  3364. /* compression levels */
  3365. //var Z_NO_COMPRESSION = 0;
  3366. //var Z_BEST_SPEED = 1;
  3367. //var Z_BEST_COMPRESSION = 9;
  3368. var Z_DEFAULT_COMPRESSION = -1;
  3369. var Z_FILTERED = 1;
  3370. var Z_HUFFMAN_ONLY = 2;
  3371. var Z_RLE = 3;
  3372. var Z_FIXED = 4;
  3373. var Z_DEFAULT_STRATEGY = 0;
  3374. /* Possible values of the data_type field (though see inflate()) */
  3375. //var Z_BINARY = 0;
  3376. //var Z_TEXT = 1;
  3377. //var Z_ASCII = 1; // = Z_TEXT
  3378. var Z_UNKNOWN = 2;
  3379. /* The deflate compression method */
  3380. var Z_DEFLATED = 8;
  3381. /*============================================================================*/
  3382. var MAX_MEM_LEVEL = 9;
  3383. /* Maximum value for memLevel in deflateInit2 */
  3384. var MAX_WBITS = 15;
  3385. /* 32K LZ77 window */
  3386. var DEF_MEM_LEVEL = 8;
  3387. var LENGTH_CODES = 29;
  3388. /* number of length codes, not counting the special END_BLOCK code */
  3389. var LITERALS = 256;
  3390. /* number of literal bytes 0..255 */
  3391. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  3392. /* number of Literal or Length codes, including the END_BLOCK code */
  3393. var D_CODES = 30;
  3394. /* number of distance codes */
  3395. var BL_CODES = 19;
  3396. /* number of codes used to transfer the bit lengths */
  3397. var HEAP_SIZE = 2*L_CODES + 1;
  3398. /* maximum heap size */
  3399. var MAX_BITS = 15;
  3400. /* All codes must not exceed MAX_BITS bits */
  3401. var MIN_MATCH = 3;
  3402. var MAX_MATCH = 258;
  3403. var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  3404. var PRESET_DICT = 0x20;
  3405. var INIT_STATE = 42;
  3406. var EXTRA_STATE = 69;
  3407. var NAME_STATE = 73;
  3408. var COMMENT_STATE = 91;
  3409. var HCRC_STATE = 103;
  3410. var BUSY_STATE = 113;
  3411. var FINISH_STATE = 666;
  3412. var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
  3413. var BS_BLOCK_DONE = 2; /* block flush performed */
  3414. var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
  3415. var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
  3416. var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
  3417. function err(strm, errorCode) {
  3418. strm.msg = msg[errorCode];
  3419. return errorCode;
  3420. }
  3421. function rank(f) {
  3422. return ((f) << 1) - ((f) > 4 ? 9 : 0);
  3423. }
  3424. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  3425. /* =========================================================================
  3426. * Flush as much pending output as possible. All deflate() output goes
  3427. * through this function so some applications may wish to modify it
  3428. * to avoid allocating a large strm->output buffer and copying into it.
  3429. * (See also read_buf()).
  3430. */
  3431. function flush_pending(strm) {
  3432. var s = strm.state;
  3433. //_tr_flush_bits(s);
  3434. var len = s.pending;
  3435. if (len > strm.avail_out) {
  3436. len = strm.avail_out;
  3437. }
  3438. if (len === 0) { return; }
  3439. utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
  3440. strm.next_out += len;
  3441. s.pending_out += len;
  3442. strm.total_out += len;
  3443. strm.avail_out -= len;
  3444. s.pending -= len;
  3445. if (s.pending === 0) {
  3446. s.pending_out = 0;
  3447. }
  3448. }
  3449. function flush_block_only (s, last) {
  3450. trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
  3451. s.block_start = s.strstart;
  3452. flush_pending(s.strm);
  3453. }
  3454. function put_byte(s, b) {
  3455. s.pending_buf[s.pending++] = b;
  3456. }
  3457. /* =========================================================================
  3458. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  3459. * IN assertion: the stream state is correct and there is enough room in
  3460. * pending_buf.
  3461. */
  3462. function putShortMSB(s, b) {
  3463. // put_byte(s, (Byte)(b >> 8));
  3464. // put_byte(s, (Byte)(b & 0xff));
  3465. s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
  3466. s.pending_buf[s.pending++] = b & 0xff;
  3467. }
  3468. /* ===========================================================================
  3469. * Read a new buffer from the current input stream, update the adler32
  3470. * and total number of bytes read. All deflate() input goes through
  3471. * this function so some applications may wish to modify it to avoid
  3472. * allocating a large strm->input buffer and copying from it.
  3473. * (See also flush_pending()).
  3474. */
  3475. function read_buf(strm, buf, start, size) {
  3476. var len = strm.avail_in;
  3477. if (len > size) { len = size; }
  3478. if (len === 0) { return 0; }
  3479. strm.avail_in -= len;
  3480. utils.arraySet(buf, strm.input, strm.next_in, len, start);
  3481. if (strm.state.wrap === 1) {
  3482. strm.adler = adler32(strm.adler, buf, len, start);
  3483. }
  3484. else if (strm.state.wrap === 2) {
  3485. strm.adler = crc32(strm.adler, buf, len, start);
  3486. }
  3487. strm.next_in += len;
  3488. strm.total_in += len;
  3489. return len;
  3490. }
  3491. /* ===========================================================================
  3492. * Set match_start to the longest match starting at the given string and
  3493. * return its length. Matches shorter or equal to prev_length are discarded,
  3494. * in which case the result is equal to prev_length and match_start is
  3495. * garbage.
  3496. * IN assertions: cur_match is the head of the hash chain for the current
  3497. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  3498. * OUT assertion: the match length is not greater than s->lookahead.
  3499. */
  3500. function longest_match(s, cur_match) {
  3501. var chain_length = s.max_chain_length; /* max hash chain length */
  3502. var scan = s.strstart; /* current string */
  3503. var match; /* matched string */
  3504. var len; /* length of current match */
  3505. var best_len = s.prev_length; /* best match length so far */
  3506. var nice_match = s.nice_match; /* stop if match long enough */
  3507. var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
  3508. s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
  3509. var _win = s.window; // shortcut
  3510. var wmask = s.w_mask;
  3511. var prev = s.prev;
  3512. /* Stop when cur_match becomes <= limit. To simplify the code,
  3513. * we prevent matches with the string of window index 0.
  3514. */
  3515. var strend = s.strstart + MAX_MATCH;
  3516. var scan_end1 = _win[scan + best_len - 1];
  3517. var scan_end = _win[scan + best_len];
  3518. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  3519. * It is easy to get rid of this optimization if necessary.
  3520. */
  3521. // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  3522. /* Do not waste too much time if we already have a good match: */
  3523. if (s.prev_length >= s.good_match) {
  3524. chain_length >>= 2;
  3525. }
  3526. /* Do not look for matches beyond the end of the input. This is necessary
  3527. * to make deflate deterministic.
  3528. */
  3529. if (nice_match > s.lookahead) { nice_match = s.lookahead; }
  3530. // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  3531. do {
  3532. // Assert(cur_match < s->strstart, "no future");
  3533. match = cur_match;
  3534. /* Skip to next match if the match length cannot increase
  3535. * or if the match length is less than 2. Note that the checks below
  3536. * for insufficient lookahead only occur occasionally for performance
  3537. * reasons. Therefore uninitialized memory will be accessed, and
  3538. * conditional jumps will be made that depend on those values.
  3539. * However the length of the match is limited to the lookahead, so
  3540. * the output of deflate is not affected by the uninitialized values.
  3541. */
  3542. if (_win[match + best_len] !== scan_end ||
  3543. _win[match + best_len - 1] !== scan_end1 ||
  3544. _win[match] !== _win[scan] ||
  3545. _win[++match] !== _win[scan + 1]) {
  3546. continue;
  3547. }
  3548. /* The check at best_len-1 can be removed because it will be made
  3549. * again later. (This heuristic is not always a win.)
  3550. * It is not necessary to compare scan[2] and match[2] since they
  3551. * are always equal when the other bytes match, given that
  3552. * the hash keys are equal and that HASH_BITS >= 8.
  3553. */
  3554. scan += 2;
  3555. match++;
  3556. // Assert(*scan == *match, "match[2]?");
  3557. /* We check for insufficient lookahead only every 8th comparison;
  3558. * the 256th check will be made at strstart+258.
  3559. */
  3560. do {
  3561. /*jshint noempty:false*/
  3562. } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  3563. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  3564. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  3565. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  3566. scan < strend);
  3567. // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  3568. len = MAX_MATCH - (strend - scan);
  3569. scan = strend - MAX_MATCH;
  3570. if (len > best_len) {
  3571. s.match_start = cur_match;
  3572. best_len = len;
  3573. if (len >= nice_match) {
  3574. break;
  3575. }
  3576. scan_end1 = _win[scan + best_len - 1];
  3577. scan_end = _win[scan + best_len];
  3578. }
  3579. } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
  3580. if (best_len <= s.lookahead) {
  3581. return best_len;
  3582. }
  3583. return s.lookahead;
  3584. }
  3585. /* ===========================================================================
  3586. * Fill the window when the lookahead becomes insufficient.
  3587. * Updates strstart and lookahead.
  3588. *
  3589. * IN assertion: lookahead < MIN_LOOKAHEAD
  3590. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  3591. * At least one byte has been read, or avail_in == 0; reads are
  3592. * performed for at least two bytes (required for the zip translate_eol
  3593. * option -- not supported here).
  3594. */
  3595. function fill_window(s) {
  3596. var _w_size = s.w_size;
  3597. var p, n, m, more, str;
  3598. //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  3599. do {
  3600. more = s.window_size - s.lookahead - s.strstart;
  3601. // JS ints have 32 bit, block below not needed
  3602. /* Deal with !@#$% 64K limit: */
  3603. //if (sizeof(int) <= 2) {
  3604. // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  3605. // more = wsize;
  3606. //
  3607. // } else if (more == (unsigned)(-1)) {
  3608. // /* Very unlikely, but possible on 16 bit machine if
  3609. // * strstart == 0 && lookahead == 1 (input done a byte at time)
  3610. // */
  3611. // more--;
  3612. // }
  3613. //}
  3614. /* If the window is almost full and there is insufficient lookahead,
  3615. * move the upper half to the lower one to make room in the upper half.
  3616. */
  3617. if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
  3618. utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
  3619. s.match_start -= _w_size;
  3620. s.strstart -= _w_size;
  3621. /* we now have strstart >= MAX_DIST */
  3622. s.block_start -= _w_size;
  3623. /* Slide the hash table (could be avoided with 32 bit values
  3624. at the expense of memory usage). We slide even when level == 0
  3625. to keep the hash table consistent if we switch back to level > 0
  3626. later. (Using level 0 permanently is not an optimal usage of
  3627. zlib, so we don't care about this pathological case.)
  3628. */
  3629. n = s.hash_size;
  3630. p = n;
  3631. do {
  3632. m = s.head[--p];
  3633. s.head[p] = (m >= _w_size ? m - _w_size : 0);
  3634. } while (--n);
  3635. n = _w_size;
  3636. p = n;
  3637. do {
  3638. m = s.prev[--p];
  3639. s.prev[p] = (m >= _w_size ? m - _w_size : 0);
  3640. /* If n is not on any hash chain, prev[n] is garbage but
  3641. * its value will never be used.
  3642. */
  3643. } while (--n);
  3644. more += _w_size;
  3645. }
  3646. if (s.strm.avail_in === 0) {
  3647. break;
  3648. }
  3649. /* If there was no sliding:
  3650. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  3651. * more == window_size - lookahead - strstart
  3652. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  3653. * => more >= window_size - 2*WSIZE + 2
  3654. * In the BIG_MEM or MMAP case (not yet supported),
  3655. * window_size == input_size + MIN_LOOKAHEAD &&
  3656. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  3657. * Otherwise, window_size == 2*WSIZE so more >= 2.
  3658. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  3659. */
  3660. //Assert(more >= 2, "more < 2");
  3661. n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
  3662. s.lookahead += n;
  3663. /* Initialize the hash value now that we have some input: */
  3664. if (s.lookahead + s.insert >= MIN_MATCH) {
  3665. str = s.strstart - s.insert;
  3666. s.ins_h = s.window[str];
  3667. /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
  3668. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
  3669. //#if MIN_MATCH != 3
  3670. // Call update_hash() MIN_MATCH-3 more times
  3671. //#endif
  3672. while (s.insert) {
  3673. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  3674. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH-1]) & s.hash_mask;
  3675. s.prev[str & s.w_mask] = s.head[s.ins_h];
  3676. s.head[s.ins_h] = str;
  3677. str++;
  3678. s.insert--;
  3679. if (s.lookahead + s.insert < MIN_MATCH) {
  3680. break;
  3681. }
  3682. }
  3683. }
  3684. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  3685. * but this is not important since only literal bytes will be emitted.
  3686. */
  3687. } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
  3688. /* If the WIN_INIT bytes after the end of the current data have never been
  3689. * written, then zero those bytes in order to avoid memory check reports of
  3690. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  3691. * the longest match routines. Update the high water mark for the next
  3692. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  3693. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  3694. */
  3695. // if (s.high_water < s.window_size) {
  3696. // var curr = s.strstart + s.lookahead;
  3697. // var init = 0;
  3698. //
  3699. // if (s.high_water < curr) {
  3700. // /* Previous high water mark below current data -- zero WIN_INIT
  3701. // * bytes or up to end of window, whichever is less.
  3702. // */
  3703. // init = s.window_size - curr;
  3704. // if (init > WIN_INIT)
  3705. // init = WIN_INIT;
  3706. // zmemzero(s->window + curr, (unsigned)init);
  3707. // s->high_water = curr + init;
  3708. // }
  3709. // else if (s->high_water < (ulg)curr + WIN_INIT) {
  3710. // /* High water mark at or above current data, but below current data
  3711. // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  3712. // * to end of window, whichever is less.
  3713. // */
  3714. // init = (ulg)curr + WIN_INIT - s->high_water;
  3715. // if (init > s->window_size - s->high_water)
  3716. // init = s->window_size - s->high_water;
  3717. // zmemzero(s->window + s->high_water, (unsigned)init);
  3718. // s->high_water += init;
  3719. // }
  3720. // }
  3721. //
  3722. // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  3723. // "not enough room for search");
  3724. }
  3725. /* ===========================================================================
  3726. * Copy without compression as much as possible from the input stream, return
  3727. * the current block state.
  3728. * This function does not insert new strings in the dictionary since
  3729. * uncompressible data is probably not useful. This function is used
  3730. * only for the level=0 compression option.
  3731. * NOTE: this function should be optimized to avoid extra copying from
  3732. * window to pending_buf.
  3733. */
  3734. function deflate_stored(s, flush) {
  3735. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  3736. * to pending_buf_size, and each stored block has a 5 byte header:
  3737. */
  3738. var max_block_size = 0xffff;
  3739. if (max_block_size > s.pending_buf_size - 5) {
  3740. max_block_size = s.pending_buf_size - 5;
  3741. }
  3742. /* Copy as much as possible from input to output: */
  3743. for (;;) {
  3744. /* Fill the window as much as possible: */
  3745. if (s.lookahead <= 1) {
  3746. //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  3747. // s->block_start >= (long)s->w_size, "slide too late");
  3748. // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
  3749. // s.block_start >= s.w_size)) {
  3750. // throw new Error("slide too late");
  3751. // }
  3752. fill_window(s);
  3753. if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
  3754. return BS_NEED_MORE;
  3755. }
  3756. if (s.lookahead === 0) {
  3757. break;
  3758. }
  3759. /* flush the current block */
  3760. }
  3761. //Assert(s->block_start >= 0L, "block gone");
  3762. // if (s.block_start < 0) throw new Error("block gone");
  3763. s.strstart += s.lookahead;
  3764. s.lookahead = 0;
  3765. /* Emit a stored block if pending_buf will be full: */
  3766. var max_start = s.block_start + max_block_size;
  3767. if (s.strstart === 0 || s.strstart >= max_start) {
  3768. /* strstart == 0 is possible when wraparound on 16-bit machine */
  3769. s.lookahead = s.strstart - max_start;
  3770. s.strstart = max_start;
  3771. /*** FLUSH_BLOCK(s, 0); ***/
  3772. flush_block_only(s, false);
  3773. if (s.strm.avail_out === 0) {
  3774. return BS_NEED_MORE;
  3775. }
  3776. /***/
  3777. }
  3778. /* Flush if we may have to slide, otherwise block_start may become
  3779. * negative and the data will be gone:
  3780. */
  3781. if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
  3782. /*** FLUSH_BLOCK(s, 0); ***/
  3783. flush_block_only(s, false);
  3784. if (s.strm.avail_out === 0) {
  3785. return BS_NEED_MORE;
  3786. }
  3787. /***/
  3788. }
  3789. }
  3790. s.insert = 0;
  3791. if (flush === Z_FINISH) {
  3792. /*** FLUSH_BLOCK(s, 1); ***/
  3793. flush_block_only(s, true);
  3794. if (s.strm.avail_out === 0) {
  3795. return BS_FINISH_STARTED;
  3796. }
  3797. /***/
  3798. return BS_FINISH_DONE;
  3799. }
  3800. if (s.strstart > s.block_start) {
  3801. /*** FLUSH_BLOCK(s, 0); ***/
  3802. flush_block_only(s, false);
  3803. if (s.strm.avail_out === 0) {
  3804. return BS_NEED_MORE;
  3805. }
  3806. /***/
  3807. }
  3808. return BS_NEED_MORE;
  3809. }
  3810. /* ===========================================================================
  3811. * Compress as much as possible from the input stream, return the current
  3812. * block state.
  3813. * This function does not perform lazy evaluation of matches and inserts
  3814. * new strings in the dictionary only for unmatched strings or for short
  3815. * matches. It is used only for the fast compression options.
  3816. */
  3817. function deflate_fast(s, flush) {
  3818. var hash_head; /* head of the hash chain */
  3819. var bflush; /* set if current block must be flushed */
  3820. for (;;) {
  3821. /* Make sure that we always have enough lookahead, except
  3822. * at the end of the input file. We need MAX_MATCH bytes
  3823. * for the next match, plus MIN_MATCH bytes to insert the
  3824. * string following the next match.
  3825. */
  3826. if (s.lookahead < MIN_LOOKAHEAD) {
  3827. fill_window(s);
  3828. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  3829. return BS_NEED_MORE;
  3830. }
  3831. if (s.lookahead === 0) {
  3832. break; /* flush the current block */
  3833. }
  3834. }
  3835. /* Insert the string window[strstart .. strstart+2] in the
  3836. * dictionary, and set hash_head to the head of the hash chain:
  3837. */
  3838. hash_head = 0/*NIL*/;
  3839. if (s.lookahead >= MIN_MATCH) {
  3840. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  3841. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  3842. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  3843. s.head[s.ins_h] = s.strstart;
  3844. /***/
  3845. }
  3846. /* Find the longest match, discarding those <= prev_length.
  3847. * At this point we have always match_length < MIN_MATCH
  3848. */
  3849. if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
  3850. /* To simplify the code, we prevent matches with the string
  3851. * of window index 0 (in particular we have to avoid a match
  3852. * of the string with itself at the start of the input file).
  3853. */
  3854. s.match_length = longest_match(s, hash_head);
  3855. /* longest_match() sets match_start */
  3856. }
  3857. if (s.match_length >= MIN_MATCH) {
  3858. // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
  3859. /*** _tr_tally_dist(s, s.strstart - s.match_start,
  3860. s.match_length - MIN_MATCH, bflush); ***/
  3861. bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
  3862. s.lookahead -= s.match_length;
  3863. /* Insert new strings in the hash table only if the match length
  3864. * is not too large. This saves time but degrades compression.
  3865. */
  3866. if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
  3867. s.match_length--; /* string at strstart already in table */
  3868. do {
  3869. s.strstart++;
  3870. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  3871. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  3872. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  3873. s.head[s.ins_h] = s.strstart;
  3874. /***/
  3875. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  3876. * always MIN_MATCH bytes ahead.
  3877. */
  3878. } while (--s.match_length !== 0);
  3879. s.strstart++;
  3880. } else
  3881. {
  3882. s.strstart += s.match_length;
  3883. s.match_length = 0;
  3884. s.ins_h = s.window[s.strstart];
  3885. /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
  3886. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
  3887. //#if MIN_MATCH != 3
  3888. // Call UPDATE_HASH() MIN_MATCH-3 more times
  3889. //#endif
  3890. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  3891. * matter since it will be recomputed at next deflate call.
  3892. */
  3893. }
  3894. } else {
  3895. /* No match, output a literal byte */
  3896. //Tracevv((stderr,"%c", s.window[s.strstart]));
  3897. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  3898. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  3899. s.lookahead--;
  3900. s.strstart++;
  3901. }
  3902. if (bflush) {
  3903. /*** FLUSH_BLOCK(s, 0); ***/
  3904. flush_block_only(s, false);
  3905. if (s.strm.avail_out === 0) {
  3906. return BS_NEED_MORE;
  3907. }
  3908. /***/
  3909. }
  3910. }
  3911. s.insert = ((s.strstart < (MIN_MATCH-1)) ? s.strstart : MIN_MATCH-1);
  3912. if (flush === Z_FINISH) {
  3913. /*** FLUSH_BLOCK(s, 1); ***/
  3914. flush_block_only(s, true);
  3915. if (s.strm.avail_out === 0) {
  3916. return BS_FINISH_STARTED;
  3917. }
  3918. /***/
  3919. return BS_FINISH_DONE;
  3920. }
  3921. if (s.last_lit) {
  3922. /*** FLUSH_BLOCK(s, 0); ***/
  3923. flush_block_only(s, false);
  3924. if (s.strm.avail_out === 0) {
  3925. return BS_NEED_MORE;
  3926. }
  3927. /***/
  3928. }
  3929. return BS_BLOCK_DONE;
  3930. }
  3931. /* ===========================================================================
  3932. * Same as above, but achieves better compression. We use a lazy
  3933. * evaluation for matches: a match is finally adopted only if there is
  3934. * no better match at the next window position.
  3935. */
  3936. function deflate_slow(s, flush) {
  3937. var hash_head; /* head of hash chain */
  3938. var bflush; /* set if current block must be flushed */
  3939. var max_insert;
  3940. /* Process the input block. */
  3941. for (;;) {
  3942. /* Make sure that we always have enough lookahead, except
  3943. * at the end of the input file. We need MAX_MATCH bytes
  3944. * for the next match, plus MIN_MATCH bytes to insert the
  3945. * string following the next match.
  3946. */
  3947. if (s.lookahead < MIN_LOOKAHEAD) {
  3948. fill_window(s);
  3949. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  3950. return BS_NEED_MORE;
  3951. }
  3952. if (s.lookahead === 0) { break; } /* flush the current block */
  3953. }
  3954. /* Insert the string window[strstart .. strstart+2] in the
  3955. * dictionary, and set hash_head to the head of the hash chain:
  3956. */
  3957. hash_head = 0/*NIL*/;
  3958. if (s.lookahead >= MIN_MATCH) {
  3959. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  3960. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  3961. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  3962. s.head[s.ins_h] = s.strstart;
  3963. /***/
  3964. }
  3965. /* Find the longest match, discarding those <= prev_length.
  3966. */
  3967. s.prev_length = s.match_length;
  3968. s.prev_match = s.match_start;
  3969. s.match_length = MIN_MATCH-1;
  3970. if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
  3971. s.strstart - hash_head <= (s.w_size-MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
  3972. /* To simplify the code, we prevent matches with the string
  3973. * of window index 0 (in particular we have to avoid a match
  3974. * of the string with itself at the start of the input file).
  3975. */
  3976. s.match_length = longest_match(s, hash_head);
  3977. /* longest_match() sets match_start */
  3978. if (s.match_length <= 5 &&
  3979. (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
  3980. /* If prev_match is also MIN_MATCH, match_start is garbage
  3981. * but we will ignore the current match anyway.
  3982. */
  3983. s.match_length = MIN_MATCH-1;
  3984. }
  3985. }
  3986. /* If there was a match at the previous step and the current
  3987. * match is not better, output the previous match:
  3988. */
  3989. if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
  3990. max_insert = s.strstart + s.lookahead - MIN_MATCH;
  3991. /* Do not insert strings in hash table beyond this. */
  3992. //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  3993. /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
  3994. s.prev_length - MIN_MATCH, bflush);***/
  3995. bflush = trees._tr_tally(s, s.strstart - 1- s.prev_match, s.prev_length - MIN_MATCH);
  3996. /* Insert in hash table all strings up to the end of the match.
  3997. * strstart-1 and strstart are already inserted. If there is not
  3998. * enough lookahead, the last two strings are not inserted in
  3999. * the hash table.
  4000. */
  4001. s.lookahead -= s.prev_length-1;
  4002. s.prev_length -= 2;
  4003. do {
  4004. if (++s.strstart <= max_insert) {
  4005. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  4006. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  4007. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  4008. s.head[s.ins_h] = s.strstart;
  4009. /***/
  4010. }
  4011. } while (--s.prev_length !== 0);
  4012. s.match_available = 0;
  4013. s.match_length = MIN_MATCH-1;
  4014. s.strstart++;
  4015. if (bflush) {
  4016. /*** FLUSH_BLOCK(s, 0); ***/
  4017. flush_block_only(s, false);
  4018. if (s.strm.avail_out === 0) {
  4019. return BS_NEED_MORE;
  4020. }
  4021. /***/
  4022. }
  4023. } else if (s.match_available) {
  4024. /* If there was no match at the previous position, output a
  4025. * single literal. If there was a match but the current match
  4026. * is longer, truncate the previous match to a single literal.
  4027. */
  4028. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  4029. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  4030. bflush = trees._tr_tally(s, 0, s.window[s.strstart-1]);
  4031. if (bflush) {
  4032. /*** FLUSH_BLOCK_ONLY(s, 0) ***/
  4033. flush_block_only(s, false);
  4034. /***/
  4035. }
  4036. s.strstart++;
  4037. s.lookahead--;
  4038. if (s.strm.avail_out === 0) {
  4039. return BS_NEED_MORE;
  4040. }
  4041. } else {
  4042. /* There is no previous match to compare with, wait for
  4043. * the next step to decide.
  4044. */
  4045. s.match_available = 1;
  4046. s.strstart++;
  4047. s.lookahead--;
  4048. }
  4049. }
  4050. //Assert (flush != Z_NO_FLUSH, "no flush?");
  4051. if (s.match_available) {
  4052. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  4053. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  4054. bflush = trees._tr_tally(s, 0, s.window[s.strstart-1]);
  4055. s.match_available = 0;
  4056. }
  4057. s.insert = s.strstart < MIN_MATCH-1 ? s.strstart : MIN_MATCH-1;
  4058. if (flush === Z_FINISH) {
  4059. /*** FLUSH_BLOCK(s, 1); ***/
  4060. flush_block_only(s, true);
  4061. if (s.strm.avail_out === 0) {
  4062. return BS_FINISH_STARTED;
  4063. }
  4064. /***/
  4065. return BS_FINISH_DONE;
  4066. }
  4067. if (s.last_lit) {
  4068. /*** FLUSH_BLOCK(s, 0); ***/
  4069. flush_block_only(s, false);
  4070. if (s.strm.avail_out === 0) {
  4071. return BS_NEED_MORE;
  4072. }
  4073. /***/
  4074. }
  4075. return BS_BLOCK_DONE;
  4076. }
  4077. /* ===========================================================================
  4078. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  4079. * one. Do not maintain a hash table. (It will be regenerated if this run of
  4080. * deflate switches away from Z_RLE.)
  4081. */
  4082. function deflate_rle(s, flush) {
  4083. var bflush; /* set if current block must be flushed */
  4084. var prev; /* byte at distance one to match */
  4085. var scan, strend; /* scan goes up to strend for length of run */
  4086. var _win = s.window;
  4087. for (;;) {
  4088. /* Make sure that we always have enough lookahead, except
  4089. * at the end of the input file. We need MAX_MATCH bytes
  4090. * for the longest run, plus one for the unrolled loop.
  4091. */
  4092. if (s.lookahead <= MAX_MATCH) {
  4093. fill_window(s);
  4094. if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
  4095. return BS_NEED_MORE;
  4096. }
  4097. if (s.lookahead === 0) { break; } /* flush the current block */
  4098. }
  4099. /* See how many times the previous byte repeats */
  4100. s.match_length = 0;
  4101. if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
  4102. scan = s.strstart - 1;
  4103. prev = _win[scan];
  4104. if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
  4105. strend = s.strstart + MAX_MATCH;
  4106. do {
  4107. /*jshint noempty:false*/
  4108. } while (prev === _win[++scan] && prev === _win[++scan] &&
  4109. prev === _win[++scan] && prev === _win[++scan] &&
  4110. prev === _win[++scan] && prev === _win[++scan] &&
  4111. prev === _win[++scan] && prev === _win[++scan] &&
  4112. scan < strend);
  4113. s.match_length = MAX_MATCH - (strend - scan);
  4114. if (s.match_length > s.lookahead) {
  4115. s.match_length = s.lookahead;
  4116. }
  4117. }
  4118. //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
  4119. }
  4120. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  4121. if (s.match_length >= MIN_MATCH) {
  4122. //check_match(s, s.strstart, s.strstart - 1, s.match_length);
  4123. /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
  4124. bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
  4125. s.lookahead -= s.match_length;
  4126. s.strstart += s.match_length;
  4127. s.match_length = 0;
  4128. } else {
  4129. /* No match, output a literal byte */
  4130. //Tracevv((stderr,"%c", s->window[s->strstart]));
  4131. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  4132. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  4133. s.lookahead--;
  4134. s.strstart++;
  4135. }
  4136. if (bflush) {
  4137. /*** FLUSH_BLOCK(s, 0); ***/
  4138. flush_block_only(s, false);
  4139. if (s.strm.avail_out === 0) {
  4140. return BS_NEED_MORE;
  4141. }
  4142. /***/
  4143. }
  4144. }
  4145. s.insert = 0;
  4146. if (flush === Z_FINISH) {
  4147. /*** FLUSH_BLOCK(s, 1); ***/
  4148. flush_block_only(s, true);
  4149. if (s.strm.avail_out === 0) {
  4150. return BS_FINISH_STARTED;
  4151. }
  4152. /***/
  4153. return BS_FINISH_DONE;
  4154. }
  4155. if (s.last_lit) {
  4156. /*** FLUSH_BLOCK(s, 0); ***/
  4157. flush_block_only(s, false);
  4158. if (s.strm.avail_out === 0) {
  4159. return BS_NEED_MORE;
  4160. }
  4161. /***/
  4162. }
  4163. return BS_BLOCK_DONE;
  4164. }
  4165. /* ===========================================================================
  4166. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  4167. * (It will be regenerated if this run of deflate switches away from Huffman.)
  4168. */
  4169. function deflate_huff(s, flush) {
  4170. var bflush; /* set if current block must be flushed */
  4171. for (;;) {
  4172. /* Make sure that we have a literal to write. */
  4173. if (s.lookahead === 0) {
  4174. fill_window(s);
  4175. if (s.lookahead === 0) {
  4176. if (flush === Z_NO_FLUSH) {
  4177. return BS_NEED_MORE;
  4178. }
  4179. break; /* flush the current block */
  4180. }
  4181. }
  4182. /* Output a literal byte */
  4183. s.match_length = 0;
  4184. //Tracevv((stderr,"%c", s->window[s->strstart]));
  4185. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  4186. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  4187. s.lookahead--;
  4188. s.strstart++;
  4189. if (bflush) {
  4190. /*** FLUSH_BLOCK(s, 0); ***/
  4191. flush_block_only(s, false);
  4192. if (s.strm.avail_out === 0) {
  4193. return BS_NEED_MORE;
  4194. }
  4195. /***/
  4196. }
  4197. }
  4198. s.insert = 0;
  4199. if (flush === Z_FINISH) {
  4200. /*** FLUSH_BLOCK(s, 1); ***/
  4201. flush_block_only(s, true);
  4202. if (s.strm.avail_out === 0) {
  4203. return BS_FINISH_STARTED;
  4204. }
  4205. /***/
  4206. return BS_FINISH_DONE;
  4207. }
  4208. if (s.last_lit) {
  4209. /*** FLUSH_BLOCK(s, 0); ***/
  4210. flush_block_only(s, false);
  4211. if (s.strm.avail_out === 0) {
  4212. return BS_NEED_MORE;
  4213. }
  4214. /***/
  4215. }
  4216. return BS_BLOCK_DONE;
  4217. }
  4218. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  4219. * the desired pack level (0..9). The values given below have been tuned to
  4220. * exclude worst case performance for pathological files. Better values may be
  4221. * found for specific files.
  4222. */
  4223. var Config = function (good_length, max_lazy, nice_length, max_chain, func) {
  4224. this.good_length = good_length;
  4225. this.max_lazy = max_lazy;
  4226. this.nice_length = nice_length;
  4227. this.max_chain = max_chain;
  4228. this.func = func;
  4229. };
  4230. var configuration_table;
  4231. configuration_table = [
  4232. /* good lazy nice chain */
  4233. new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
  4234. new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
  4235. new Config(4, 5, 16, 8, deflate_fast), /* 2 */
  4236. new Config(4, 6, 32, 32, deflate_fast), /* 3 */
  4237. new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
  4238. new Config(8, 16, 32, 32, deflate_slow), /* 5 */
  4239. new Config(8, 16, 128, 128, deflate_slow), /* 6 */
  4240. new Config(8, 32, 128, 256, deflate_slow), /* 7 */
  4241. new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
  4242. new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
  4243. ];
  4244. /* ===========================================================================
  4245. * Initialize the "longest match" routines for a new zlib stream
  4246. */
  4247. function lm_init(s) {
  4248. s.window_size = 2 * s.w_size;
  4249. /*** CLEAR_HASH(s); ***/
  4250. zero(s.head); // Fill with NIL (= 0);
  4251. /* Set the default configuration parameters:
  4252. */
  4253. s.max_lazy_match = configuration_table[s.level].max_lazy;
  4254. s.good_match = configuration_table[s.level].good_length;
  4255. s.nice_match = configuration_table[s.level].nice_length;
  4256. s.max_chain_length = configuration_table[s.level].max_chain;
  4257. s.strstart = 0;
  4258. s.block_start = 0;
  4259. s.lookahead = 0;
  4260. s.insert = 0;
  4261. s.match_length = s.prev_length = MIN_MATCH - 1;
  4262. s.match_available = 0;
  4263. s.ins_h = 0;
  4264. }
  4265. function DeflateState() {
  4266. this.strm = null; /* pointer back to this zlib stream */
  4267. this.status = 0; /* as the name implies */
  4268. this.pending_buf = null; /* output still pending */
  4269. this.pending_buf_size = 0; /* size of pending_buf */
  4270. this.pending_out = 0; /* next pending byte to output to the stream */
  4271. this.pending = 0; /* nb of bytes in the pending buffer */
  4272. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  4273. this.gzhead = null; /* gzip header information to write */
  4274. this.gzindex = 0; /* where in extra, name, or comment */
  4275. this.method = Z_DEFLATED; /* can only be DEFLATED */
  4276. this.last_flush = -1; /* value of flush param for previous deflate call */
  4277. this.w_size = 0; /* LZ77 window size (32K by default) */
  4278. this.w_bits = 0; /* log2(w_size) (8..16) */
  4279. this.w_mask = 0; /* w_size - 1 */
  4280. this.window = null;
  4281. /* Sliding window. Input bytes are read into the second half of the window,
  4282. * and move to the first half later to keep a dictionary of at least wSize
  4283. * bytes. With this organization, matches are limited to a distance of
  4284. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  4285. * performed with a length multiple of the block size.
  4286. */
  4287. this.window_size = 0;
  4288. /* Actual size of window: 2*wSize, except when the user input buffer
  4289. * is directly used as sliding window.
  4290. */
  4291. this.prev = null;
  4292. /* Link to older string with same hash index. To limit the size of this
  4293. * array to 64K, this link is maintained only for the last 32K strings.
  4294. * An index in this array is thus a window index modulo 32K.
  4295. */
  4296. this.head = null; /* Heads of the hash chains or NIL. */
  4297. this.ins_h = 0; /* hash index of string to be inserted */
  4298. this.hash_size = 0; /* number of elements in hash table */
  4299. this.hash_bits = 0; /* log2(hash_size) */
  4300. this.hash_mask = 0; /* hash_size-1 */
  4301. this.hash_shift = 0;
  4302. /* Number of bits by which ins_h must be shifted at each input
  4303. * step. It must be such that after MIN_MATCH steps, the oldest
  4304. * byte no longer takes part in the hash key, that is:
  4305. * hash_shift * MIN_MATCH >= hash_bits
  4306. */
  4307. this.block_start = 0;
  4308. /* Window position at the beginning of the current output block. Gets
  4309. * negative when the window is moved backwards.
  4310. */
  4311. this.match_length = 0; /* length of best match */
  4312. this.prev_match = 0; /* previous match */
  4313. this.match_available = 0; /* set if previous match exists */
  4314. this.strstart = 0; /* start of string to insert */
  4315. this.match_start = 0; /* start of matching string */
  4316. this.lookahead = 0; /* number of valid bytes ahead in window */
  4317. this.prev_length = 0;
  4318. /* Length of the best match at previous step. Matches not greater than this
  4319. * are discarded. This is used in the lazy match evaluation.
  4320. */
  4321. this.max_chain_length = 0;
  4322. /* To speed up deflation, hash chains are never searched beyond this
  4323. * length. A higher limit improves compression ratio but degrades the
  4324. * speed.
  4325. */
  4326. this.max_lazy_match = 0;
  4327. /* Attempt to find a better match only when the current match is strictly
  4328. * smaller than this value. This mechanism is used only for compression
  4329. * levels >= 4.
  4330. */
  4331. // That's alias to max_lazy_match, don't use directly
  4332. //this.max_insert_length = 0;
  4333. /* Insert new strings in the hash table only if the match length is not
  4334. * greater than this length. This saves time but degrades compression.
  4335. * max_insert_length is used only for compression levels <= 3.
  4336. */
  4337. this.level = 0; /* compression level (1..9) */
  4338. this.strategy = 0; /* favor or force Huffman coding*/
  4339. this.good_match = 0;
  4340. /* Use a faster search when the previous match is longer than this */
  4341. this.nice_match = 0; /* Stop searching when current match exceeds this */
  4342. /* used by trees.c: */
  4343. /* Didn't use ct_data typedef below to suppress compiler warning */
  4344. // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  4345. // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  4346. // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  4347. // Use flat array of DOUBLE size, with interleaved fata,
  4348. // because JS does not support effective
  4349. this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
  4350. this.dyn_dtree = new utils.Buf16((2*D_CODES+1) * 2);
  4351. this.bl_tree = new utils.Buf16((2*BL_CODES+1) * 2);
  4352. zero(this.dyn_ltree);
  4353. zero(this.dyn_dtree);
  4354. zero(this.bl_tree);
  4355. this.l_desc = null; /* desc. for literal tree */
  4356. this.d_desc = null; /* desc. for distance tree */
  4357. this.bl_desc = null; /* desc. for bit length tree */
  4358. //ush bl_count[MAX_BITS+1];
  4359. this.bl_count = new utils.Buf16(MAX_BITS+1);
  4360. /* number of codes at each bit length for an optimal tree */
  4361. //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  4362. this.heap = new utils.Buf16(2*L_CODES+1); /* heap used to build the Huffman trees */
  4363. zero(this.heap);
  4364. this.heap_len = 0; /* number of elements in the heap */
  4365. this.heap_max = 0; /* element of largest frequency */
  4366. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  4367. * The same heap array is used to build all trees.
  4368. */
  4369. this.depth = new utils.Buf16(2*L_CODES+1); //uch depth[2*L_CODES+1];
  4370. zero(this.depth);
  4371. /* Depth of each subtree used as tie breaker for trees of equal frequency
  4372. */
  4373. this.l_buf = 0; /* buffer index for literals or lengths */
  4374. this.lit_bufsize = 0;
  4375. /* Size of match buffer for literals/lengths. There are 4 reasons for
  4376. * limiting lit_bufsize to 64K:
  4377. * - frequencies can be kept in 16 bit counters
  4378. * - if compression is not successful for the first block, all input
  4379. * data is still in the window so we can still emit a stored block even
  4380. * when input comes from standard input. (This can also be done for
  4381. * all blocks if lit_bufsize is not greater than 32K.)
  4382. * - if compression is not successful for a file smaller than 64K, we can
  4383. * even emit a stored file instead of a stored block (saving 5 bytes).
  4384. * This is applicable only for zip (not gzip or zlib).
  4385. * - creating new Huffman trees less frequently may not provide fast
  4386. * adaptation to changes in the input data statistics. (Take for
  4387. * example a binary file with poorly compressible code followed by
  4388. * a highly compressible string table.) Smaller buffer sizes give
  4389. * fast adaptation but have of course the overhead of transmitting
  4390. * trees more frequently.
  4391. * - I can't count above 4
  4392. */
  4393. this.last_lit = 0; /* running index in l_buf */
  4394. this.d_buf = 0;
  4395. /* Buffer index for distances. To simplify the code, d_buf and l_buf have
  4396. * the same number of elements. To use different lengths, an extra flag
  4397. * array would be necessary.
  4398. */
  4399. this.opt_len = 0; /* bit length of current block with optimal trees */
  4400. this.static_len = 0; /* bit length of current block with static trees */
  4401. this.matches = 0; /* number of string matches in current block */
  4402. this.insert = 0; /* bytes at end of window left to insert */
  4403. this.bi_buf = 0;
  4404. /* Output buffer. bits are inserted starting at the bottom (least
  4405. * significant bits).
  4406. */
  4407. this.bi_valid = 0;
  4408. /* Number of valid bits in bi_buf. All bits above the last valid bit
  4409. * are always zero.
  4410. */
  4411. // Used for window memory init. We safely ignore it for JS. That makes
  4412. // sense only for pointers and memory check tools.
  4413. //this.high_water = 0;
  4414. /* High water mark offset in window for initialized bytes -- bytes above
  4415. * this are set to zero in order to avoid memory check warnings when
  4416. * longest match routines access bytes past the input. This is then
  4417. * updated to the new high water mark.
  4418. */
  4419. }
  4420. function deflateResetKeep(strm) {
  4421. var s;
  4422. if (!strm || !strm.state) {
  4423. return err(strm, Z_STREAM_ERROR);
  4424. }
  4425. strm.total_in = strm.total_out = 0;
  4426. strm.data_type = Z_UNKNOWN;
  4427. s = strm.state;
  4428. s.pending = 0;
  4429. s.pending_out = 0;
  4430. if (s.wrap < 0) {
  4431. s.wrap = -s.wrap;
  4432. /* was made negative by deflate(..., Z_FINISH); */
  4433. }
  4434. s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
  4435. strm.adler = (s.wrap === 2) ?
  4436. 0 // crc32(0, Z_NULL, 0)
  4437. :
  4438. 1; // adler32(0, Z_NULL, 0)
  4439. s.last_flush = Z_NO_FLUSH;
  4440. trees._tr_init(s);
  4441. return Z_OK;
  4442. }
  4443. function deflateReset(strm) {
  4444. var ret = deflateResetKeep(strm);
  4445. if (ret === Z_OK) {
  4446. lm_init(strm.state);
  4447. }
  4448. return ret;
  4449. }
  4450. function deflateSetHeader(strm, head) {
  4451. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  4452. if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
  4453. strm.state.gzhead = head;
  4454. return Z_OK;
  4455. }
  4456. function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
  4457. if (!strm) { // === Z_NULL
  4458. return Z_STREAM_ERROR;
  4459. }
  4460. var wrap = 1;
  4461. if (level === Z_DEFAULT_COMPRESSION) {
  4462. level = 6;
  4463. }
  4464. if (windowBits < 0) { /* suppress zlib wrapper */
  4465. wrap = 0;
  4466. windowBits = -windowBits;
  4467. }
  4468. else if (windowBits > 15) {
  4469. wrap = 2; /* write gzip wrapper instead */
  4470. windowBits -= 16;
  4471. }
  4472. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
  4473. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  4474. strategy < 0 || strategy > Z_FIXED) {
  4475. return err(strm, Z_STREAM_ERROR);
  4476. }
  4477. if (windowBits === 8) {
  4478. windowBits = 9;
  4479. }
  4480. /* until 256-byte window bug fixed */
  4481. var s = new DeflateState();
  4482. strm.state = s;
  4483. s.strm = strm;
  4484. s.wrap = wrap;
  4485. s.gzhead = null;
  4486. s.w_bits = windowBits;
  4487. s.w_size = 1 << s.w_bits;
  4488. s.w_mask = s.w_size - 1;
  4489. s.hash_bits = memLevel + 7;
  4490. s.hash_size = 1 << s.hash_bits;
  4491. s.hash_mask = s.hash_size - 1;
  4492. s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  4493. s.window = new utils.Buf8(s.w_size * 2);
  4494. s.head = new utils.Buf16(s.hash_size);
  4495. s.prev = new utils.Buf16(s.w_size);
  4496. // Don't need mem init magic for JS.
  4497. //s.high_water = 0; /* nothing written to s->window yet */
  4498. s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  4499. s.pending_buf_size = s.lit_bufsize * 4;
  4500. s.pending_buf = new utils.Buf8(s.pending_buf_size);
  4501. s.d_buf = s.lit_bufsize >> 1;
  4502. s.l_buf = (1 + 2) * s.lit_bufsize;
  4503. s.level = level;
  4504. s.strategy = strategy;
  4505. s.method = method;
  4506. return deflateReset(strm);
  4507. }
  4508. function deflateInit(strm, level) {
  4509. return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  4510. }
  4511. function deflate(strm, flush) {
  4512. var old_flush, s;
  4513. var beg, val; // for gzip header write only
  4514. if (!strm || !strm.state ||
  4515. flush > Z_BLOCK || flush < 0) {
  4516. return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
  4517. }
  4518. s = strm.state;
  4519. if (!strm.output ||
  4520. (!strm.input && strm.avail_in !== 0) ||
  4521. (s.status === FINISH_STATE && flush !== Z_FINISH)) {
  4522. return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
  4523. }
  4524. s.strm = strm; /* just in case */
  4525. old_flush = s.last_flush;
  4526. s.last_flush = flush;
  4527. /* Write the header */
  4528. if (s.status === INIT_STATE) {
  4529. if (s.wrap === 2) { // GZIP header
  4530. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  4531. put_byte(s, 31);
  4532. put_byte(s, 139);
  4533. put_byte(s, 8);
  4534. if (!s.gzhead) { // s->gzhead == Z_NULL
  4535. put_byte(s, 0);
  4536. put_byte(s, 0);
  4537. put_byte(s, 0);
  4538. put_byte(s, 0);
  4539. put_byte(s, 0);
  4540. put_byte(s, s.level === 9 ? 2 :
  4541. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  4542. 4 : 0));
  4543. put_byte(s, OS_CODE);
  4544. s.status = BUSY_STATE;
  4545. }
  4546. else {
  4547. put_byte(s, (s.gzhead.text ? 1 : 0) +
  4548. (s.gzhead.hcrc ? 2 : 0) +
  4549. (!s.gzhead.extra ? 0 : 4) +
  4550. (!s.gzhead.name ? 0 : 8) +
  4551. (!s.gzhead.comment ? 0 : 16)
  4552. );
  4553. put_byte(s, s.gzhead.time & 0xff);
  4554. put_byte(s, (s.gzhead.time >> 8) & 0xff);
  4555. put_byte(s, (s.gzhead.time >> 16) & 0xff);
  4556. put_byte(s, (s.gzhead.time >> 24) & 0xff);
  4557. put_byte(s, s.level === 9 ? 2 :
  4558. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  4559. 4 : 0));
  4560. put_byte(s, s.gzhead.os & 0xff);
  4561. if (s.gzhead.extra && s.gzhead.extra.length) {
  4562. put_byte(s, s.gzhead.extra.length & 0xff);
  4563. put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
  4564. }
  4565. if (s.gzhead.hcrc) {
  4566. strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
  4567. }
  4568. s.gzindex = 0;
  4569. s.status = EXTRA_STATE;
  4570. }
  4571. }
  4572. else // DEFLATE header
  4573. {
  4574. var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
  4575. var level_flags = -1;
  4576. if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
  4577. level_flags = 0;
  4578. } else if (s.level < 6) {
  4579. level_flags = 1;
  4580. } else if (s.level === 6) {
  4581. level_flags = 2;
  4582. } else {
  4583. level_flags = 3;
  4584. }
  4585. header |= (level_flags << 6);
  4586. if (s.strstart !== 0) { header |= PRESET_DICT; }
  4587. header += 31 - (header % 31);
  4588. s.status = BUSY_STATE;
  4589. putShortMSB(s, header);
  4590. /* Save the adler32 of the preset dictionary: */
  4591. if (s.strstart !== 0) {
  4592. putShortMSB(s, strm.adler >>> 16);
  4593. putShortMSB(s, strm.adler & 0xffff);
  4594. }
  4595. strm.adler = 1; // adler32(0L, Z_NULL, 0);
  4596. }
  4597. }
  4598. //#ifdef GZIP
  4599. if (s.status === EXTRA_STATE) {
  4600. if (s.gzhead.extra/* != Z_NULL*/) {
  4601. beg = s.pending; /* start of bytes to update crc */
  4602. while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
  4603. if (s.pending === s.pending_buf_size) {
  4604. if (s.gzhead.hcrc && s.pending > beg) {
  4605. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  4606. }
  4607. flush_pending(strm);
  4608. beg = s.pending;
  4609. if (s.pending === s.pending_buf_size) {
  4610. break;
  4611. }
  4612. }
  4613. put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
  4614. s.gzindex++;
  4615. }
  4616. if (s.gzhead.hcrc && s.pending > beg) {
  4617. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  4618. }
  4619. if (s.gzindex === s.gzhead.extra.length) {
  4620. s.gzindex = 0;
  4621. s.status = NAME_STATE;
  4622. }
  4623. }
  4624. else {
  4625. s.status = NAME_STATE;
  4626. }
  4627. }
  4628. if (s.status === NAME_STATE) {
  4629. if (s.gzhead.name/* != Z_NULL*/) {
  4630. beg = s.pending; /* start of bytes to update crc */
  4631. //int val;
  4632. do {
  4633. if (s.pending === s.pending_buf_size) {
  4634. if (s.gzhead.hcrc && s.pending > beg) {
  4635. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  4636. }
  4637. flush_pending(strm);
  4638. beg = s.pending;
  4639. if (s.pending === s.pending_buf_size) {
  4640. val = 1;
  4641. break;
  4642. }
  4643. }
  4644. // JS specific: little magic to add zero terminator to end of string
  4645. if (s.gzindex < s.gzhead.name.length) {
  4646. val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
  4647. } else {
  4648. val = 0;
  4649. }
  4650. put_byte(s, val);
  4651. } while (val !== 0);
  4652. if (s.gzhead.hcrc && s.pending > beg){
  4653. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  4654. }
  4655. if (val === 0) {
  4656. s.gzindex = 0;
  4657. s.status = COMMENT_STATE;
  4658. }
  4659. }
  4660. else {
  4661. s.status = COMMENT_STATE;
  4662. }
  4663. }
  4664. if (s.status === COMMENT_STATE) {
  4665. if (s.gzhead.comment/* != Z_NULL*/) {
  4666. beg = s.pending; /* start of bytes to update crc */
  4667. //int val;
  4668. do {
  4669. if (s.pending === s.pending_buf_size) {
  4670. if (s.gzhead.hcrc && s.pending > beg) {
  4671. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  4672. }
  4673. flush_pending(strm);
  4674. beg = s.pending;
  4675. if (s.pending === s.pending_buf_size) {
  4676. val = 1;
  4677. break;
  4678. }
  4679. }
  4680. // JS specific: little magic to add zero terminator to end of string
  4681. if (s.gzindex < s.gzhead.comment.length) {
  4682. val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
  4683. } else {
  4684. val = 0;
  4685. }
  4686. put_byte(s, val);
  4687. } while (val !== 0);
  4688. if (s.gzhead.hcrc && s.pending > beg) {
  4689. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  4690. }
  4691. if (val === 0) {
  4692. s.status = HCRC_STATE;
  4693. }
  4694. }
  4695. else {
  4696. s.status = HCRC_STATE;
  4697. }
  4698. }
  4699. if (s.status === HCRC_STATE) {
  4700. if (s.gzhead.hcrc) {
  4701. if (s.pending + 2 > s.pending_buf_size) {
  4702. flush_pending(strm);
  4703. }
  4704. if (s.pending + 2 <= s.pending_buf_size) {
  4705. put_byte(s, strm.adler & 0xff);
  4706. put_byte(s, (strm.adler >> 8) & 0xff);
  4707. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  4708. s.status = BUSY_STATE;
  4709. }
  4710. }
  4711. else {
  4712. s.status = BUSY_STATE;
  4713. }
  4714. }
  4715. //#endif
  4716. /* Flush as much pending output as possible */
  4717. if (s.pending !== 0) {
  4718. flush_pending(strm);
  4719. if (strm.avail_out === 0) {
  4720. /* Since avail_out is 0, deflate will be called again with
  4721. * more output space, but possibly with both pending and
  4722. * avail_in equal to zero. There won't be anything to do,
  4723. * but this is not an error situation so make sure we
  4724. * return OK instead of BUF_ERROR at next call of deflate:
  4725. */
  4726. s.last_flush = -1;
  4727. return Z_OK;
  4728. }
  4729. /* Make sure there is something to do and avoid duplicate consecutive
  4730. * flushes. For repeated and useless calls with Z_FINISH, we keep
  4731. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  4732. */
  4733. } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
  4734. flush !== Z_FINISH) {
  4735. return err(strm, Z_BUF_ERROR);
  4736. }
  4737. /* User must not provide more input after the first FINISH: */
  4738. if (s.status === FINISH_STATE && strm.avail_in !== 0) {
  4739. return err(strm, Z_BUF_ERROR);
  4740. }
  4741. /* Start a new block or continue the current one.
  4742. */
  4743. if (strm.avail_in !== 0 || s.lookahead !== 0 ||
  4744. (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
  4745. var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
  4746. (s.strategy === Z_RLE ? deflate_rle(s, flush) :
  4747. configuration_table[s.level].func(s, flush));
  4748. if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
  4749. s.status = FINISH_STATE;
  4750. }
  4751. if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
  4752. if (strm.avail_out === 0) {
  4753. s.last_flush = -1;
  4754. /* avoid BUF_ERROR next call, see above */
  4755. }
  4756. return Z_OK;
  4757. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  4758. * of deflate should use the same flush parameter to make sure
  4759. * that the flush is complete. So we don't have to output an
  4760. * empty block here, this will be done at next call. This also
  4761. * ensures that for a very small output buffer, we emit at most
  4762. * one empty block.
  4763. */
  4764. }
  4765. if (bstate === BS_BLOCK_DONE) {
  4766. if (flush === Z_PARTIAL_FLUSH) {
  4767. trees._tr_align(s);
  4768. }
  4769. else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
  4770. trees._tr_stored_block(s, 0, 0, false);
  4771. /* For a full flush, this empty block will be recognized
  4772. * as a special marker by inflate_sync().
  4773. */
  4774. if (flush === Z_FULL_FLUSH) {
  4775. /*** CLEAR_HASH(s); ***/ /* forget history */
  4776. zero(s.head); // Fill with NIL (= 0);
  4777. if (s.lookahead === 0) {
  4778. s.strstart = 0;
  4779. s.block_start = 0;
  4780. s.insert = 0;
  4781. }
  4782. }
  4783. }
  4784. flush_pending(strm);
  4785. if (strm.avail_out === 0) {
  4786. s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  4787. return Z_OK;
  4788. }
  4789. }
  4790. }
  4791. //Assert(strm->avail_out > 0, "bug2");
  4792. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  4793. if (flush !== Z_FINISH) { return Z_OK; }
  4794. if (s.wrap <= 0) { return Z_STREAM_END; }
  4795. /* Write the trailer */
  4796. if (s.wrap === 2) {
  4797. put_byte(s, strm.adler & 0xff);
  4798. put_byte(s, (strm.adler >> 8) & 0xff);
  4799. put_byte(s, (strm.adler >> 16) & 0xff);
  4800. put_byte(s, (strm.adler >> 24) & 0xff);
  4801. put_byte(s, strm.total_in & 0xff);
  4802. put_byte(s, (strm.total_in >> 8) & 0xff);
  4803. put_byte(s, (strm.total_in >> 16) & 0xff);
  4804. put_byte(s, (strm.total_in >> 24) & 0xff);
  4805. }
  4806. else
  4807. {
  4808. putShortMSB(s, strm.adler >>> 16);
  4809. putShortMSB(s, strm.adler & 0xffff);
  4810. }
  4811. flush_pending(strm);
  4812. /* If avail_out is zero, the application will call deflate again
  4813. * to flush the rest.
  4814. */
  4815. if (s.wrap > 0) { s.wrap = -s.wrap; }
  4816. /* write the trailer only once! */
  4817. return s.pending !== 0 ? Z_OK : Z_STREAM_END;
  4818. }
  4819. function deflateEnd(strm) {
  4820. var status;
  4821. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  4822. return Z_STREAM_ERROR;
  4823. }
  4824. status = strm.state.status;
  4825. if (status !== INIT_STATE &&
  4826. status !== EXTRA_STATE &&
  4827. status !== NAME_STATE &&
  4828. status !== COMMENT_STATE &&
  4829. status !== HCRC_STATE &&
  4830. status !== BUSY_STATE &&
  4831. status !== FINISH_STATE
  4832. ) {
  4833. return err(strm, Z_STREAM_ERROR);
  4834. }
  4835. strm.state = null;
  4836. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
  4837. }
  4838. /* =========================================================================
  4839. * Copy the source state to the destination state
  4840. */
  4841. //function deflateCopy(dest, source) {
  4842. //
  4843. //}
  4844. exports.deflateInit = deflateInit;
  4845. exports.deflateInit2 = deflateInit2;
  4846. exports.deflateReset = deflateReset;
  4847. exports.deflateResetKeep = deflateResetKeep;
  4848. exports.deflateSetHeader = deflateSetHeader;
  4849. exports.deflate = deflate;
  4850. exports.deflateEnd = deflateEnd;
  4851. exports.deflateInfo = 'pako deflate (from Nodeca project)';
  4852. /* Not implemented
  4853. exports.deflateBound = deflateBound;
  4854. exports.deflateCopy = deflateCopy;
  4855. exports.deflateSetDictionary = deflateSetDictionary;
  4856. exports.deflateParams = deflateParams;
  4857. exports.deflatePending = deflatePending;
  4858. exports.deflatePrime = deflatePrime;
  4859. exports.deflateTune = deflateTune;
  4860. */
  4861. },{"../utils/common":27,"./adler32":29,"./crc32":31,"./messages":37,"./trees":38}],33:[function(_dereq_,module,exports){
  4862. 'use strict';
  4863. function GZheader() {
  4864. /* true if compressed data believed to be text */
  4865. this.text = 0;
  4866. /* modification time */
  4867. this.time = 0;
  4868. /* extra flags (not used when writing a gzip file) */
  4869. this.xflags = 0;
  4870. /* operating system */
  4871. this.os = 0;
  4872. /* pointer to extra field or Z_NULL if none */
  4873. this.extra = null;
  4874. /* extra field length (valid if extra != Z_NULL) */
  4875. this.extra_len = 0; // Actually, we don't need it in JS,
  4876. // but leave for few code modifications
  4877. //
  4878. // Setup limits is not necessary because in js we should not preallocate memory
  4879. // for inflate use constant limit in 65536 bytes
  4880. //
  4881. /* space at extra (only when reading header) */
  4882. // this.extra_max = 0;
  4883. /* pointer to zero-terminated file name or Z_NULL */
  4884. this.name = '';
  4885. /* space at name (only when reading header) */
  4886. // this.name_max = 0;
  4887. /* pointer to zero-terminated comment or Z_NULL */
  4888. this.comment = '';
  4889. /* space at comment (only when reading header) */
  4890. // this.comm_max = 0;
  4891. /* true if there was or will be a header crc */
  4892. this.hcrc = 0;
  4893. /* true when done reading gzip header (not used when writing a gzip file) */
  4894. this.done = false;
  4895. }
  4896. module.exports = GZheader;
  4897. },{}],34:[function(_dereq_,module,exports){
  4898. 'use strict';
  4899. // See state defs from inflate.js
  4900. var BAD = 30; /* got a data error -- remain here until reset */
  4901. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  4902. /*
  4903. Decode literal, length, and distance codes and write out the resulting
  4904. literal and match bytes until either not enough input or output is
  4905. available, an end-of-block is encountered, or a data error is encountered.
  4906. When large enough input and output buffers are supplied to inflate(), for
  4907. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  4908. inflate execution time is spent in this routine.
  4909. Entry assumptions:
  4910. state.mode === LEN
  4911. strm.avail_in >= 6
  4912. strm.avail_out >= 258
  4913. start >= strm.avail_out
  4914. state.bits < 8
  4915. On return, state.mode is one of:
  4916. LEN -- ran out of enough output space or enough available input
  4917. TYPE -- reached end of block code, inflate() to interpret next block
  4918. BAD -- error in block data
  4919. Notes:
  4920. - The maximum input bits used by a length/distance pair is 15 bits for the
  4921. length code, 5 bits for the length extra, 15 bits for the distance code,
  4922. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  4923. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  4924. checking for available input while decoding.
  4925. - The maximum bytes that a single length/distance pair can output is 258
  4926. bytes, which is the maximum length that can be coded. inflate_fast()
  4927. requires strm.avail_out >= 258 for each loop to avoid checking for
  4928. output space.
  4929. */
  4930. module.exports = function inflate_fast(strm, start) {
  4931. var state;
  4932. var _in; /* local strm.input */
  4933. var last; /* have enough input while in < last */
  4934. var _out; /* local strm.output */
  4935. var beg; /* inflate()'s initial strm.output */
  4936. var end; /* while out < end, enough space available */
  4937. //#ifdef INFLATE_STRICT
  4938. var dmax; /* maximum distance from zlib header */
  4939. //#endif
  4940. var wsize; /* window size or zero if not using window */
  4941. var whave; /* valid bytes in the window */
  4942. var wnext; /* window write index */
  4943. var window; /* allocated sliding window, if wsize != 0 */
  4944. var hold; /* local strm.hold */
  4945. var bits; /* local strm.bits */
  4946. var lcode; /* local strm.lencode */
  4947. var dcode; /* local strm.distcode */
  4948. var lmask; /* mask for first level of length codes */
  4949. var dmask; /* mask for first level of distance codes */
  4950. var here; /* retrieved table entry */
  4951. var op; /* code bits, operation, extra bits, or */
  4952. /* window position, window bytes to copy */
  4953. var len; /* match length, unused bytes */
  4954. var dist; /* match distance */
  4955. var from; /* where to copy match from */
  4956. var from_source;
  4957. var input, output; // JS specific, because we have no pointers
  4958. /* copy state to local variables */
  4959. state = strm.state;
  4960. //here = state.here;
  4961. _in = strm.next_in;
  4962. input = strm.input;
  4963. last = _in + (strm.avail_in - 5);
  4964. _out = strm.next_out;
  4965. output = strm.output;
  4966. beg = _out - (start - strm.avail_out);
  4967. end = _out + (strm.avail_out - 257);
  4968. //#ifdef INFLATE_STRICT
  4969. dmax = state.dmax;
  4970. //#endif
  4971. wsize = state.wsize;
  4972. whave = state.whave;
  4973. wnext = state.wnext;
  4974. window = state.window;
  4975. hold = state.hold;
  4976. bits = state.bits;
  4977. lcode = state.lencode;
  4978. dcode = state.distcode;
  4979. lmask = (1 << state.lenbits) - 1;
  4980. dmask = (1 << state.distbits) - 1;
  4981. /* decode literals and length/distances until end-of-block or not enough
  4982. input data or output space */
  4983. top:
  4984. do {
  4985. if (bits < 15) {
  4986. hold += input[_in++] << bits;
  4987. bits += 8;
  4988. hold += input[_in++] << bits;
  4989. bits += 8;
  4990. }
  4991. here = lcode[hold & lmask];
  4992. dolen:
  4993. for (;;) { // Goto emulation
  4994. op = here >>> 24/*here.bits*/;
  4995. hold >>>= op;
  4996. bits -= op;
  4997. op = (here >>> 16) & 0xff/*here.op*/;
  4998. if (op === 0) { /* literal */
  4999. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  5000. // "inflate: literal '%c'\n" :
  5001. // "inflate: literal 0x%02x\n", here.val));
  5002. output[_out++] = here & 0xffff/*here.val*/;
  5003. }
  5004. else if (op & 16) { /* length base */
  5005. len = here & 0xffff/*here.val*/;
  5006. op &= 15; /* number of extra bits */
  5007. if (op) {
  5008. if (bits < op) {
  5009. hold += input[_in++] << bits;
  5010. bits += 8;
  5011. }
  5012. len += hold & ((1 << op) - 1);
  5013. hold >>>= op;
  5014. bits -= op;
  5015. }
  5016. //Tracevv((stderr, "inflate: length %u\n", len));
  5017. if (bits < 15) {
  5018. hold += input[_in++] << bits;
  5019. bits += 8;
  5020. hold += input[_in++] << bits;
  5021. bits += 8;
  5022. }
  5023. here = dcode[hold & dmask];
  5024. dodist:
  5025. for (;;) { // goto emulation
  5026. op = here >>> 24/*here.bits*/;
  5027. hold >>>= op;
  5028. bits -= op;
  5029. op = (here >>> 16) & 0xff/*here.op*/;
  5030. if (op & 16) { /* distance base */
  5031. dist = here & 0xffff/*here.val*/;
  5032. op &= 15; /* number of extra bits */
  5033. if (bits < op) {
  5034. hold += input[_in++] << bits;
  5035. bits += 8;
  5036. if (bits < op) {
  5037. hold += input[_in++] << bits;
  5038. bits += 8;
  5039. }
  5040. }
  5041. dist += hold & ((1 << op) - 1);
  5042. //#ifdef INFLATE_STRICT
  5043. if (dist > dmax) {
  5044. strm.msg = 'invalid distance too far back';
  5045. state.mode = BAD;
  5046. break top;
  5047. }
  5048. //#endif
  5049. hold >>>= op;
  5050. bits -= op;
  5051. //Tracevv((stderr, "inflate: distance %u\n", dist));
  5052. op = _out - beg; /* max distance in output */
  5053. if (dist > op) { /* see if copy from window */
  5054. op = dist - op; /* distance back in window */
  5055. if (op > whave) {
  5056. if (state.sane) {
  5057. strm.msg = 'invalid distance too far back';
  5058. state.mode = BAD;
  5059. break top;
  5060. }
  5061. // (!) This block is disabled in zlib defailts,
  5062. // don't enable it for binary compatibility
  5063. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  5064. // if (len <= op - whave) {
  5065. // do {
  5066. // output[_out++] = 0;
  5067. // } while (--len);
  5068. // continue top;
  5069. // }
  5070. // len -= op - whave;
  5071. // do {
  5072. // output[_out++] = 0;
  5073. // } while (--op > whave);
  5074. // if (op === 0) {
  5075. // from = _out - dist;
  5076. // do {
  5077. // output[_out++] = output[from++];
  5078. // } while (--len);
  5079. // continue top;
  5080. // }
  5081. //#endif
  5082. }
  5083. from = 0; // window index
  5084. from_source = window;
  5085. if (wnext === 0) { /* very common case */
  5086. from += wsize - op;
  5087. if (op < len) { /* some from window */
  5088. len -= op;
  5089. do {
  5090. output[_out++] = window[from++];
  5091. } while (--op);
  5092. from = _out - dist; /* rest from output */
  5093. from_source = output;
  5094. }
  5095. }
  5096. else if (wnext < op) { /* wrap around window */
  5097. from += wsize + wnext - op;
  5098. op -= wnext;
  5099. if (op < len) { /* some from end of window */
  5100. len -= op;
  5101. do {
  5102. output[_out++] = window[from++];
  5103. } while (--op);
  5104. from = 0;
  5105. if (wnext < len) { /* some from start of window */
  5106. op = wnext;
  5107. len -= op;
  5108. do {
  5109. output[_out++] = window[from++];
  5110. } while (--op);
  5111. from = _out - dist; /* rest from output */
  5112. from_source = output;
  5113. }
  5114. }
  5115. }
  5116. else { /* contiguous in window */
  5117. from += wnext - op;
  5118. if (op < len) { /* some from window */
  5119. len -= op;
  5120. do {
  5121. output[_out++] = window[from++];
  5122. } while (--op);
  5123. from = _out - dist; /* rest from output */
  5124. from_source = output;
  5125. }
  5126. }
  5127. while (len > 2) {
  5128. output[_out++] = from_source[from++];
  5129. output[_out++] = from_source[from++];
  5130. output[_out++] = from_source[from++];
  5131. len -= 3;
  5132. }
  5133. if (len) {
  5134. output[_out++] = from_source[from++];
  5135. if (len > 1) {
  5136. output[_out++] = from_source[from++];
  5137. }
  5138. }
  5139. }
  5140. else {
  5141. from = _out - dist; /* copy direct from output */
  5142. do { /* minimum length is three */
  5143. output[_out++] = output[from++];
  5144. output[_out++] = output[from++];
  5145. output[_out++] = output[from++];
  5146. len -= 3;
  5147. } while (len > 2);
  5148. if (len) {
  5149. output[_out++] = output[from++];
  5150. if (len > 1) {
  5151. output[_out++] = output[from++];
  5152. }
  5153. }
  5154. }
  5155. }
  5156. else if ((op & 64) === 0) { /* 2nd level distance code */
  5157. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  5158. continue dodist;
  5159. }
  5160. else {
  5161. strm.msg = 'invalid distance code';
  5162. state.mode = BAD;
  5163. break top;
  5164. }
  5165. break; // need to emulate goto via "continue"
  5166. }
  5167. }
  5168. else if ((op & 64) === 0) { /* 2nd level length code */
  5169. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  5170. continue dolen;
  5171. }
  5172. else if (op & 32) { /* end-of-block */
  5173. //Tracevv((stderr, "inflate: end of block\n"));
  5174. state.mode = TYPE;
  5175. break top;
  5176. }
  5177. else {
  5178. strm.msg = 'invalid literal/length code';
  5179. state.mode = BAD;
  5180. break top;
  5181. }
  5182. break; // need to emulate goto via "continue"
  5183. }
  5184. } while (_in < last && _out < end);
  5185. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  5186. len = bits >> 3;
  5187. _in -= len;
  5188. bits -= len << 3;
  5189. hold &= (1 << bits) - 1;
  5190. /* update state and return */
  5191. strm.next_in = _in;
  5192. strm.next_out = _out;
  5193. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  5194. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  5195. state.hold = hold;
  5196. state.bits = bits;
  5197. return;
  5198. };
  5199. },{}],35:[function(_dereq_,module,exports){
  5200. 'use strict';
  5201. var utils = _dereq_('../utils/common');
  5202. var adler32 = _dereq_('./adler32');
  5203. var crc32 = _dereq_('./crc32');
  5204. var inflate_fast = _dereq_('./inffast');
  5205. var inflate_table = _dereq_('./inftrees');
  5206. var CODES = 0;
  5207. var LENS = 1;
  5208. var DISTS = 2;
  5209. /* Public constants ==========================================================*/
  5210. /* ===========================================================================*/
  5211. /* Allowed flush values; see deflate() and inflate() below for details */
  5212. //var Z_NO_FLUSH = 0;
  5213. //var Z_PARTIAL_FLUSH = 1;
  5214. //var Z_SYNC_FLUSH = 2;
  5215. //var Z_FULL_FLUSH = 3;
  5216. var Z_FINISH = 4;
  5217. var Z_BLOCK = 5;
  5218. var Z_TREES = 6;
  5219. /* Return codes for the compression/decompression functions. Negative values
  5220. * are errors, positive values are used for special but normal events.
  5221. */
  5222. var Z_OK = 0;
  5223. var Z_STREAM_END = 1;
  5224. var Z_NEED_DICT = 2;
  5225. //var Z_ERRNO = -1;
  5226. var Z_STREAM_ERROR = -2;
  5227. var Z_DATA_ERROR = -3;
  5228. var Z_MEM_ERROR = -4;
  5229. var Z_BUF_ERROR = -5;
  5230. //var Z_VERSION_ERROR = -6;
  5231. /* The deflate compression method */
  5232. var Z_DEFLATED = 8;
  5233. /* STATES ====================================================================*/
  5234. /* ===========================================================================*/
  5235. var HEAD = 1; /* i: waiting for magic header */
  5236. var FLAGS = 2; /* i: waiting for method and flags (gzip) */
  5237. var TIME = 3; /* i: waiting for modification time (gzip) */
  5238. var OS = 4; /* i: waiting for extra flags and operating system (gzip) */
  5239. var EXLEN = 5; /* i: waiting for extra length (gzip) */
  5240. var EXTRA = 6; /* i: waiting for extra bytes (gzip) */
  5241. var NAME = 7; /* i: waiting for end of file name (gzip) */
  5242. var COMMENT = 8; /* i: waiting for end of comment (gzip) */
  5243. var HCRC = 9; /* i: waiting for header crc (gzip) */
  5244. var DICTID = 10; /* i: waiting for dictionary check value */
  5245. var DICT = 11; /* waiting for inflateSetDictionary() call */
  5246. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  5247. var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
  5248. var STORED = 14; /* i: waiting for stored size (length and complement) */
  5249. var COPY_ = 15; /* i/o: same as COPY below, but only first time in */
  5250. var COPY = 16; /* i/o: waiting for input or output to copy stored block */
  5251. var TABLE = 17; /* i: waiting for dynamic block table lengths */
  5252. var LENLENS = 18; /* i: waiting for code length code lengths */
  5253. var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
  5254. var LEN_ = 20; /* i: same as LEN below, but only first time in */
  5255. var LEN = 21; /* i: waiting for length/lit/eob code */
  5256. var LENEXT = 22; /* i: waiting for length extra bits */
  5257. var DIST = 23; /* i: waiting for distance code */
  5258. var DISTEXT = 24; /* i: waiting for distance extra bits */
  5259. var MATCH = 25; /* o: waiting for output space to copy string */
  5260. var LIT = 26; /* o: waiting for output space to write literal */
  5261. var CHECK = 27; /* i: waiting for 32-bit check value */
  5262. var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
  5263. var DONE = 29; /* finished check, done -- remain here until reset */
  5264. var BAD = 30; /* got a data error -- remain here until reset */
  5265. var MEM = 31; /* got an inflate() memory error -- remain here until reset */
  5266. var SYNC = 32; /* looking for synchronization bytes to restart inflate() */
  5267. /* ===========================================================================*/
  5268. var ENOUGH_LENS = 852;
  5269. var ENOUGH_DISTS = 592;
  5270. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  5271. var MAX_WBITS = 15;
  5272. /* 32K LZ77 window */
  5273. var DEF_WBITS = MAX_WBITS;
  5274. function ZSWAP32(q) {
  5275. return (((q >>> 24) & 0xff) +
  5276. ((q >>> 8) & 0xff00) +
  5277. ((q & 0xff00) << 8) +
  5278. ((q & 0xff) << 24));
  5279. }
  5280. function InflateState() {
  5281. this.mode = 0; /* current inflate mode */
  5282. this.last = false; /* true if processing last block */
  5283. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  5284. this.havedict = false; /* true if dictionary provided */
  5285. this.flags = 0; /* gzip header method and flags (0 if zlib) */
  5286. this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */
  5287. this.check = 0; /* protected copy of check value */
  5288. this.total = 0; /* protected copy of output count */
  5289. // TODO: may be {}
  5290. this.head = null; /* where to save gzip header information */
  5291. /* sliding window */
  5292. this.wbits = 0; /* log base 2 of requested window size */
  5293. this.wsize = 0; /* window size or zero if not using window */
  5294. this.whave = 0; /* valid bytes in the window */
  5295. this.wnext = 0; /* window write index */
  5296. this.window = null; /* allocated sliding window, if needed */
  5297. /* bit accumulator */
  5298. this.hold = 0; /* input bit accumulator */
  5299. this.bits = 0; /* number of bits in "in" */
  5300. /* for string and stored block copying */
  5301. this.length = 0; /* literal or length of data to copy */
  5302. this.offset = 0; /* distance back to copy string from */
  5303. /* for table and code decoding */
  5304. this.extra = 0; /* extra bits needed */
  5305. /* fixed and dynamic code tables */
  5306. this.lencode = null; /* starting table for length/literal codes */
  5307. this.distcode = null; /* starting table for distance codes */
  5308. this.lenbits = 0; /* index bits for lencode */
  5309. this.distbits = 0; /* index bits for distcode */
  5310. /* dynamic table building */
  5311. this.ncode = 0; /* number of code length code lengths */
  5312. this.nlen = 0; /* number of length code lengths */
  5313. this.ndist = 0; /* number of distance code lengths */
  5314. this.have = 0; /* number of code lengths in lens[] */
  5315. this.next = null; /* next available space in codes[] */
  5316. this.lens = new utils.Buf16(320); /* temporary storage for code lengths */
  5317. this.work = new utils.Buf16(288); /* work area for code table building */
  5318. /*
  5319. because we don't have pointers in js, we use lencode and distcode directly
  5320. as buffers so we don't need codes
  5321. */
  5322. //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */
  5323. this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
  5324. this.distdyn = null; /* dynamic table for distance codes (JS specific) */
  5325. this.sane = 0; /* if false, allow invalid distance too far */
  5326. this.back = 0; /* bits back of last unprocessed length/lit */
  5327. this.was = 0; /* initial length of match */
  5328. }
  5329. function inflateResetKeep(strm) {
  5330. var state;
  5331. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  5332. state = strm.state;
  5333. strm.total_in = strm.total_out = state.total = 0;
  5334. strm.msg = ''; /*Z_NULL*/
  5335. if (state.wrap) { /* to support ill-conceived Java test suite */
  5336. strm.adler = state.wrap & 1;
  5337. }
  5338. state.mode = HEAD;
  5339. state.last = 0;
  5340. state.havedict = 0;
  5341. state.dmax = 32768;
  5342. state.head = null/*Z_NULL*/;
  5343. state.hold = 0;
  5344. state.bits = 0;
  5345. //state.lencode = state.distcode = state.next = state.codes;
  5346. state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS);
  5347. state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS);
  5348. state.sane = 1;
  5349. state.back = -1;
  5350. //Tracev((stderr, "inflate: reset\n"));
  5351. return Z_OK;
  5352. }
  5353. function inflateReset(strm) {
  5354. var state;
  5355. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  5356. state = strm.state;
  5357. state.wsize = 0;
  5358. state.whave = 0;
  5359. state.wnext = 0;
  5360. return inflateResetKeep(strm);
  5361. }
  5362. function inflateReset2(strm, windowBits) {
  5363. var wrap;
  5364. var state;
  5365. /* get the state */
  5366. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  5367. state = strm.state;
  5368. /* extract wrap request from windowBits parameter */
  5369. if (windowBits < 0) {
  5370. wrap = 0;
  5371. windowBits = -windowBits;
  5372. }
  5373. else {
  5374. wrap = (windowBits >> 4) + 1;
  5375. if (windowBits < 48) {
  5376. windowBits &= 15;
  5377. }
  5378. }
  5379. /* set number of window bits, free window if different */
  5380. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  5381. return Z_STREAM_ERROR;
  5382. }
  5383. if (state.window !== null && state.wbits !== windowBits) {
  5384. state.window = null;
  5385. }
  5386. /* update state and reset the rest of it */
  5387. state.wrap = wrap;
  5388. state.wbits = windowBits;
  5389. return inflateReset(strm);
  5390. }
  5391. function inflateInit2(strm, windowBits) {
  5392. var ret;
  5393. var state;
  5394. if (!strm) { return Z_STREAM_ERROR; }
  5395. //strm.msg = Z_NULL; /* in case we return an error */
  5396. state = new InflateState();
  5397. //if (state === Z_NULL) return Z_MEM_ERROR;
  5398. //Tracev((stderr, "inflate: allocated\n"));
  5399. strm.state = state;
  5400. state.window = null/*Z_NULL*/;
  5401. ret = inflateReset2(strm, windowBits);
  5402. if (ret !== Z_OK) {
  5403. strm.state = null/*Z_NULL*/;
  5404. }
  5405. return ret;
  5406. }
  5407. function inflateInit(strm) {
  5408. return inflateInit2(strm, DEF_WBITS);
  5409. }
  5410. /*
  5411. Return state with length and distance decoding tables and index sizes set to
  5412. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  5413. If BUILDFIXED is defined, then instead this routine builds the tables the
  5414. first time it's called, and returns those tables the first time and
  5415. thereafter. This reduces the size of the code by about 2K bytes, in
  5416. exchange for a little execution time. However, BUILDFIXED should not be
  5417. used for threaded applications, since the rewriting of the tables and virgin
  5418. may not be thread-safe.
  5419. */
  5420. var virgin = true;
  5421. var lenfix, distfix; // We have no pointers in JS, so keep tables separate
  5422. function fixedtables(state) {
  5423. /* build fixed huffman tables if first call (may not be thread safe) */
  5424. if (virgin) {
  5425. var sym;
  5426. lenfix = new utils.Buf32(512);
  5427. distfix = new utils.Buf32(32);
  5428. /* literal/length table */
  5429. sym = 0;
  5430. while (sym < 144) { state.lens[sym++] = 8; }
  5431. while (sym < 256) { state.lens[sym++] = 9; }
  5432. while (sym < 280) { state.lens[sym++] = 7; }
  5433. while (sym < 288) { state.lens[sym++] = 8; }
  5434. inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, {bits: 9});
  5435. /* distance table */
  5436. sym = 0;
  5437. while (sym < 32) { state.lens[sym++] = 5; }
  5438. inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, {bits: 5});
  5439. /* do this just once */
  5440. virgin = false;
  5441. }
  5442. state.lencode = lenfix;
  5443. state.lenbits = 9;
  5444. state.distcode = distfix;
  5445. state.distbits = 5;
  5446. }
  5447. /*
  5448. Update the window with the last wsize (normally 32K) bytes written before
  5449. returning. If window does not exist yet, create it. This is only called
  5450. when a window is already in use, or when output has been written during this
  5451. inflate call, but the end of the deflate stream has not been reached yet.
  5452. It is also called to create a window for dictionary data when a dictionary
  5453. is loaded.
  5454. Providing output buffers larger than 32K to inflate() should provide a speed
  5455. advantage, since only the last 32K of output is copied to the sliding window
  5456. upon return from inflate(), and since all distances after the first 32K of
  5457. output will fall in the output data, making match copies simpler and faster.
  5458. The advantage may be dependent on the size of the processor's data caches.
  5459. */
  5460. function updatewindow(strm, src, end, copy) {
  5461. var dist;
  5462. var state = strm.state;
  5463. /* if it hasn't been done already, allocate space for the window */
  5464. if (state.window === null) {
  5465. state.wsize = 1 << state.wbits;
  5466. state.wnext = 0;
  5467. state.whave = 0;
  5468. state.window = new utils.Buf8(state.wsize);
  5469. }
  5470. /* copy state->wsize or less output bytes into the circular window */
  5471. if (copy >= state.wsize) {
  5472. utils.arraySet(state.window,src, end - state.wsize, state.wsize, 0);
  5473. state.wnext = 0;
  5474. state.whave = state.wsize;
  5475. }
  5476. else {
  5477. dist = state.wsize - state.wnext;
  5478. if (dist > copy) {
  5479. dist = copy;
  5480. }
  5481. //zmemcpy(state->window + state->wnext, end - copy, dist);
  5482. utils.arraySet(state.window,src, end - copy, dist, state.wnext);
  5483. copy -= dist;
  5484. if (copy) {
  5485. //zmemcpy(state->window, end - copy, copy);
  5486. utils.arraySet(state.window,src, end - copy, copy, 0);
  5487. state.wnext = copy;
  5488. state.whave = state.wsize;
  5489. }
  5490. else {
  5491. state.wnext += dist;
  5492. if (state.wnext === state.wsize) { state.wnext = 0; }
  5493. if (state.whave < state.wsize) { state.whave += dist; }
  5494. }
  5495. }
  5496. return 0;
  5497. }
  5498. function inflate(strm, flush) {
  5499. var state;
  5500. var input, output; // input/output buffers
  5501. var next; /* next input INDEX */
  5502. var put; /* next output INDEX */
  5503. var have, left; /* available input and output */
  5504. var hold; /* bit buffer */
  5505. var bits; /* bits in bit buffer */
  5506. var _in, _out; /* save starting available input and output */
  5507. var copy; /* number of stored or match bytes to copy */
  5508. var from; /* where to copy match bytes from */
  5509. var from_source;
  5510. var here = 0; /* current decoding table entry */
  5511. var here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  5512. //var last; /* parent table entry */
  5513. var last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  5514. var len; /* length to copy for repeats, bits to drop */
  5515. var ret; /* return code */
  5516. var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */
  5517. var opts;
  5518. var n; // temporary var for NEED_BITS
  5519. var order = /* permutation of code lengths */
  5520. [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
  5521. if (!strm || !strm.state || !strm.output ||
  5522. (!strm.input && strm.avail_in !== 0)) {
  5523. return Z_STREAM_ERROR;
  5524. }
  5525. state = strm.state;
  5526. if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
  5527. //--- LOAD() ---
  5528. put = strm.next_out;
  5529. output = strm.output;
  5530. left = strm.avail_out;
  5531. next = strm.next_in;
  5532. input = strm.input;
  5533. have = strm.avail_in;
  5534. hold = state.hold;
  5535. bits = state.bits;
  5536. //---
  5537. _in = have;
  5538. _out = left;
  5539. ret = Z_OK;
  5540. inf_leave: // goto emulation
  5541. for (;;) {
  5542. switch (state.mode) {
  5543. case HEAD:
  5544. if (state.wrap === 0) {
  5545. state.mode = TYPEDO;
  5546. break;
  5547. }
  5548. //=== NEEDBITS(16);
  5549. while (bits < 16) {
  5550. if (have === 0) { break inf_leave; }
  5551. have--;
  5552. hold += input[next++] << bits;
  5553. bits += 8;
  5554. }
  5555. //===//
  5556. if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */
  5557. state.check = 0/*crc32(0L, Z_NULL, 0)*/;
  5558. //=== CRC2(state.check, hold);
  5559. hbuf[0] = hold & 0xff;
  5560. hbuf[1] = (hold >>> 8) & 0xff;
  5561. state.check = crc32(state.check, hbuf, 2, 0);
  5562. //===//
  5563. //=== INITBITS();
  5564. hold = 0;
  5565. bits = 0;
  5566. //===//
  5567. state.mode = FLAGS;
  5568. break;
  5569. }
  5570. state.flags = 0; /* expect zlib header */
  5571. if (state.head) {
  5572. state.head.done = false;
  5573. }
  5574. if (!(state.wrap & 1) || /* check if zlib header allowed */
  5575. (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
  5576. strm.msg = 'incorrect header check';
  5577. state.mode = BAD;
  5578. break;
  5579. }
  5580. if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
  5581. strm.msg = 'unknown compression method';
  5582. state.mode = BAD;
  5583. break;
  5584. }
  5585. //--- DROPBITS(4) ---//
  5586. hold >>>= 4;
  5587. bits -= 4;
  5588. //---//
  5589. len = (hold & 0x0f)/*BITS(4)*/ + 8;
  5590. if (state.wbits === 0) {
  5591. state.wbits = len;
  5592. }
  5593. else if (len > state.wbits) {
  5594. strm.msg = 'invalid window size';
  5595. state.mode = BAD;
  5596. break;
  5597. }
  5598. state.dmax = 1 << len;
  5599. //Tracev((stderr, "inflate: zlib header ok\n"));
  5600. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  5601. state.mode = hold & 0x200 ? DICTID : TYPE;
  5602. //=== INITBITS();
  5603. hold = 0;
  5604. bits = 0;
  5605. //===//
  5606. break;
  5607. case FLAGS:
  5608. //=== NEEDBITS(16); */
  5609. while (bits < 16) {
  5610. if (have === 0) { break inf_leave; }
  5611. have--;
  5612. hold += input[next++] << bits;
  5613. bits += 8;
  5614. }
  5615. //===//
  5616. state.flags = hold;
  5617. if ((state.flags & 0xff) !== Z_DEFLATED) {
  5618. strm.msg = 'unknown compression method';
  5619. state.mode = BAD;
  5620. break;
  5621. }
  5622. if (state.flags & 0xe000) {
  5623. strm.msg = 'unknown header flags set';
  5624. state.mode = BAD;
  5625. break;
  5626. }
  5627. if (state.head) {
  5628. state.head.text = ((hold >> 8) & 1);
  5629. }
  5630. if (state.flags & 0x0200) {
  5631. //=== CRC2(state.check, hold);
  5632. hbuf[0] = hold & 0xff;
  5633. hbuf[1] = (hold >>> 8) & 0xff;
  5634. state.check = crc32(state.check, hbuf, 2, 0);
  5635. //===//
  5636. }
  5637. //=== INITBITS();
  5638. hold = 0;
  5639. bits = 0;
  5640. //===//
  5641. state.mode = TIME;
  5642. /* falls through */
  5643. case TIME:
  5644. //=== NEEDBITS(32); */
  5645. while (bits < 32) {
  5646. if (have === 0) { break inf_leave; }
  5647. have--;
  5648. hold += input[next++] << bits;
  5649. bits += 8;
  5650. }
  5651. //===//
  5652. if (state.head) {
  5653. state.head.time = hold;
  5654. }
  5655. if (state.flags & 0x0200) {
  5656. //=== CRC4(state.check, hold)
  5657. hbuf[0] = hold & 0xff;
  5658. hbuf[1] = (hold >>> 8) & 0xff;
  5659. hbuf[2] = (hold >>> 16) & 0xff;
  5660. hbuf[3] = (hold >>> 24) & 0xff;
  5661. state.check = crc32(state.check, hbuf, 4, 0);
  5662. //===
  5663. }
  5664. //=== INITBITS();
  5665. hold = 0;
  5666. bits = 0;
  5667. //===//
  5668. state.mode = OS;
  5669. /* falls through */
  5670. case OS:
  5671. //=== NEEDBITS(16); */
  5672. while (bits < 16) {
  5673. if (have === 0) { break inf_leave; }
  5674. have--;
  5675. hold += input[next++] << bits;
  5676. bits += 8;
  5677. }
  5678. //===//
  5679. if (state.head) {
  5680. state.head.xflags = (hold & 0xff);
  5681. state.head.os = (hold >> 8);
  5682. }
  5683. if (state.flags & 0x0200) {
  5684. //=== CRC2(state.check, hold);
  5685. hbuf[0] = hold & 0xff;
  5686. hbuf[1] = (hold >>> 8) & 0xff;
  5687. state.check = crc32(state.check, hbuf, 2, 0);
  5688. //===//
  5689. }
  5690. //=== INITBITS();
  5691. hold = 0;
  5692. bits = 0;
  5693. //===//
  5694. state.mode = EXLEN;
  5695. /* falls through */
  5696. case EXLEN:
  5697. if (state.flags & 0x0400) {
  5698. //=== NEEDBITS(16); */
  5699. while (bits < 16) {
  5700. if (have === 0) { break inf_leave; }
  5701. have--;
  5702. hold += input[next++] << bits;
  5703. bits += 8;
  5704. }
  5705. //===//
  5706. state.length = hold;
  5707. if (state.head) {
  5708. state.head.extra_len = hold;
  5709. }
  5710. if (state.flags & 0x0200) {
  5711. //=== CRC2(state.check, hold);
  5712. hbuf[0] = hold & 0xff;
  5713. hbuf[1] = (hold >>> 8) & 0xff;
  5714. state.check = crc32(state.check, hbuf, 2, 0);
  5715. //===//
  5716. }
  5717. //=== INITBITS();
  5718. hold = 0;
  5719. bits = 0;
  5720. //===//
  5721. }
  5722. else if (state.head) {
  5723. state.head.extra = null/*Z_NULL*/;
  5724. }
  5725. state.mode = EXTRA;
  5726. /* falls through */
  5727. case EXTRA:
  5728. if (state.flags & 0x0400) {
  5729. copy = state.length;
  5730. if (copy > have) { copy = have; }
  5731. if (copy) {
  5732. if (state.head) {
  5733. len = state.head.extra_len - state.length;
  5734. if (!state.head.extra) {
  5735. // Use untyped array for more conveniend processing later
  5736. state.head.extra = new Array(state.head.extra_len);
  5737. }
  5738. utils.arraySet(
  5739. state.head.extra,
  5740. input,
  5741. next,
  5742. // extra field is limited to 65536 bytes
  5743. // - no need for additional size check
  5744. copy,
  5745. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  5746. len
  5747. );
  5748. //zmemcpy(state.head.extra + len, next,
  5749. // len + copy > state.head.extra_max ?
  5750. // state.head.extra_max - len : copy);
  5751. }
  5752. if (state.flags & 0x0200) {
  5753. state.check = crc32(state.check, input, copy, next);
  5754. }
  5755. have -= copy;
  5756. next += copy;
  5757. state.length -= copy;
  5758. }
  5759. if (state.length) { break inf_leave; }
  5760. }
  5761. state.length = 0;
  5762. state.mode = NAME;
  5763. /* falls through */
  5764. case NAME:
  5765. if (state.flags & 0x0800) {
  5766. if (have === 0) { break inf_leave; }
  5767. copy = 0;
  5768. do {
  5769. // TODO: 2 or 1 bytes?
  5770. len = input[next + copy++];
  5771. /* use constant limit because in js we should not preallocate memory */
  5772. if (state.head && len &&
  5773. (state.length < 65536 /*state.head.name_max*/)) {
  5774. state.head.name += String.fromCharCode(len);
  5775. }
  5776. } while (len && copy < have);
  5777. if (state.flags & 0x0200) {
  5778. state.check = crc32(state.check, input, copy, next);
  5779. }
  5780. have -= copy;
  5781. next += copy;
  5782. if (len) { break inf_leave; }
  5783. }
  5784. else if (state.head) {
  5785. state.head.name = null;
  5786. }
  5787. state.length = 0;
  5788. state.mode = COMMENT;
  5789. /* falls through */
  5790. case COMMENT:
  5791. if (state.flags & 0x1000) {
  5792. if (have === 0) { break inf_leave; }
  5793. copy = 0;
  5794. do {
  5795. len = input[next + copy++];
  5796. /* use constant limit because in js we should not preallocate memory */
  5797. if (state.head && len &&
  5798. (state.length < 65536 /*state.head.comm_max*/)) {
  5799. state.head.comment += String.fromCharCode(len);
  5800. }
  5801. } while (len && copy < have);
  5802. if (state.flags & 0x0200) {
  5803. state.check = crc32(state.check, input, copy, next);
  5804. }
  5805. have -= copy;
  5806. next += copy;
  5807. if (len) { break inf_leave; }
  5808. }
  5809. else if (state.head) {
  5810. state.head.comment = null;
  5811. }
  5812. state.mode = HCRC;
  5813. /* falls through */
  5814. case HCRC:
  5815. if (state.flags & 0x0200) {
  5816. //=== NEEDBITS(16); */
  5817. while (bits < 16) {
  5818. if (have === 0) { break inf_leave; }
  5819. have--;
  5820. hold += input[next++] << bits;
  5821. bits += 8;
  5822. }
  5823. //===//
  5824. if (hold !== (state.check & 0xffff)) {
  5825. strm.msg = 'header crc mismatch';
  5826. state.mode = BAD;
  5827. break;
  5828. }
  5829. //=== INITBITS();
  5830. hold = 0;
  5831. bits = 0;
  5832. //===//
  5833. }
  5834. if (state.head) {
  5835. state.head.hcrc = ((state.flags >> 9) & 1);
  5836. state.head.done = true;
  5837. }
  5838. strm.adler = state.check = 0 /*crc32(0L, Z_NULL, 0)*/;
  5839. state.mode = TYPE;
  5840. break;
  5841. case DICTID:
  5842. //=== NEEDBITS(32); */
  5843. while (bits < 32) {
  5844. if (have === 0) { break inf_leave; }
  5845. have--;
  5846. hold += input[next++] << bits;
  5847. bits += 8;
  5848. }
  5849. //===//
  5850. strm.adler = state.check = ZSWAP32(hold);
  5851. //=== INITBITS();
  5852. hold = 0;
  5853. bits = 0;
  5854. //===//
  5855. state.mode = DICT;
  5856. /* falls through */
  5857. case DICT:
  5858. if (state.havedict === 0) {
  5859. //--- RESTORE() ---
  5860. strm.next_out = put;
  5861. strm.avail_out = left;
  5862. strm.next_in = next;
  5863. strm.avail_in = have;
  5864. state.hold = hold;
  5865. state.bits = bits;
  5866. //---
  5867. return Z_NEED_DICT;
  5868. }
  5869. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  5870. state.mode = TYPE;
  5871. /* falls through */
  5872. case TYPE:
  5873. if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
  5874. /* falls through */
  5875. case TYPEDO:
  5876. if (state.last) {
  5877. //--- BYTEBITS() ---//
  5878. hold >>>= bits & 7;
  5879. bits -= bits & 7;
  5880. //---//
  5881. state.mode = CHECK;
  5882. break;
  5883. }
  5884. //=== NEEDBITS(3); */
  5885. while (bits < 3) {
  5886. if (have === 0) { break inf_leave; }
  5887. have--;
  5888. hold += input[next++] << bits;
  5889. bits += 8;
  5890. }
  5891. //===//
  5892. state.last = (hold & 0x01)/*BITS(1)*/;
  5893. //--- DROPBITS(1) ---//
  5894. hold >>>= 1;
  5895. bits -= 1;
  5896. //---//
  5897. switch ((hold & 0x03)/*BITS(2)*/) {
  5898. case 0: /* stored block */
  5899. //Tracev((stderr, "inflate: stored block%s\n",
  5900. // state.last ? " (last)" : ""));
  5901. state.mode = STORED;
  5902. break;
  5903. case 1: /* fixed block */
  5904. fixedtables(state);
  5905. //Tracev((stderr, "inflate: fixed codes block%s\n",
  5906. // state.last ? " (last)" : ""));
  5907. state.mode = LEN_; /* decode codes */
  5908. if (flush === Z_TREES) {
  5909. //--- DROPBITS(2) ---//
  5910. hold >>>= 2;
  5911. bits -= 2;
  5912. //---//
  5913. break inf_leave;
  5914. }
  5915. break;
  5916. case 2: /* dynamic block */
  5917. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  5918. // state.last ? " (last)" : ""));
  5919. state.mode = TABLE;
  5920. break;
  5921. case 3:
  5922. strm.msg = 'invalid block type';
  5923. state.mode = BAD;
  5924. }
  5925. //--- DROPBITS(2) ---//
  5926. hold >>>= 2;
  5927. bits -= 2;
  5928. //---//
  5929. break;
  5930. case STORED:
  5931. //--- BYTEBITS() ---// /* go to byte boundary */
  5932. hold >>>= bits & 7;
  5933. bits -= bits & 7;
  5934. //---//
  5935. //=== NEEDBITS(32); */
  5936. while (bits < 32) {
  5937. if (have === 0) { break inf_leave; }
  5938. have--;
  5939. hold += input[next++] << bits;
  5940. bits += 8;
  5941. }
  5942. //===//
  5943. if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
  5944. strm.msg = 'invalid stored block lengths';
  5945. state.mode = BAD;
  5946. break;
  5947. }
  5948. state.length = hold & 0xffff;
  5949. //Tracev((stderr, "inflate: stored length %u\n",
  5950. // state.length));
  5951. //=== INITBITS();
  5952. hold = 0;
  5953. bits = 0;
  5954. //===//
  5955. state.mode = COPY_;
  5956. if (flush === Z_TREES) { break inf_leave; }
  5957. /* falls through */
  5958. case COPY_:
  5959. state.mode = COPY;
  5960. /* falls through */
  5961. case COPY:
  5962. copy = state.length;
  5963. if (copy) {
  5964. if (copy > have) { copy = have; }
  5965. if (copy > left) { copy = left; }
  5966. if (copy === 0) { break inf_leave; }
  5967. //--- zmemcpy(put, next, copy); ---
  5968. utils.arraySet(output, input, next, copy, put);
  5969. //---//
  5970. have -= copy;
  5971. next += copy;
  5972. left -= copy;
  5973. put += copy;
  5974. state.length -= copy;
  5975. break;
  5976. }
  5977. //Tracev((stderr, "inflate: stored end\n"));
  5978. state.mode = TYPE;
  5979. break;
  5980. case TABLE:
  5981. //=== NEEDBITS(14); */
  5982. while (bits < 14) {
  5983. if (have === 0) { break inf_leave; }
  5984. have--;
  5985. hold += input[next++] << bits;
  5986. bits += 8;
  5987. }
  5988. //===//
  5989. state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
  5990. //--- DROPBITS(5) ---//
  5991. hold >>>= 5;
  5992. bits -= 5;
  5993. //---//
  5994. state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
  5995. //--- DROPBITS(5) ---//
  5996. hold >>>= 5;
  5997. bits -= 5;
  5998. //---//
  5999. state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
  6000. //--- DROPBITS(4) ---//
  6001. hold >>>= 4;
  6002. bits -= 4;
  6003. //---//
  6004. //#ifndef PKZIP_BUG_WORKAROUND
  6005. if (state.nlen > 286 || state.ndist > 30) {
  6006. strm.msg = 'too many length or distance symbols';
  6007. state.mode = BAD;
  6008. break;
  6009. }
  6010. //#endif
  6011. //Tracev((stderr, "inflate: table sizes ok\n"));
  6012. state.have = 0;
  6013. state.mode = LENLENS;
  6014. /* falls through */
  6015. case LENLENS:
  6016. while (state.have < state.ncode) {
  6017. //=== NEEDBITS(3);
  6018. while (bits < 3) {
  6019. if (have === 0) { break inf_leave; }
  6020. have--;
  6021. hold += input[next++] << bits;
  6022. bits += 8;
  6023. }
  6024. //===//
  6025. state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
  6026. //--- DROPBITS(3) ---//
  6027. hold >>>= 3;
  6028. bits -= 3;
  6029. //---//
  6030. }
  6031. while (state.have < 19) {
  6032. state.lens[order[state.have++]] = 0;
  6033. }
  6034. // We have separate tables & no pointers. 2 commented lines below not needed.
  6035. //state.next = state.codes;
  6036. //state.lencode = state.next;
  6037. // Switch to use dynamic table
  6038. state.lencode = state.lendyn;
  6039. state.lenbits = 7;
  6040. opts = {bits: state.lenbits};
  6041. ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  6042. state.lenbits = opts.bits;
  6043. if (ret) {
  6044. strm.msg = 'invalid code lengths set';
  6045. state.mode = BAD;
  6046. break;
  6047. }
  6048. //Tracev((stderr, "inflate: code lengths ok\n"));
  6049. state.have = 0;
  6050. state.mode = CODELENS;
  6051. /* falls through */
  6052. case CODELENS:
  6053. while (state.have < state.nlen + state.ndist) {
  6054. for (;;) {
  6055. here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/
  6056. here_bits = here >>> 24;
  6057. here_op = (here >>> 16) & 0xff;
  6058. here_val = here & 0xffff;
  6059. if ((here_bits) <= bits) { break; }
  6060. //--- PULLBYTE() ---//
  6061. if (have === 0) { break inf_leave; }
  6062. have--;
  6063. hold += input[next++] << bits;
  6064. bits += 8;
  6065. //---//
  6066. }
  6067. if (here_val < 16) {
  6068. //--- DROPBITS(here.bits) ---//
  6069. hold >>>= here_bits;
  6070. bits -= here_bits;
  6071. //---//
  6072. state.lens[state.have++] = here_val;
  6073. }
  6074. else {
  6075. if (here_val === 16) {
  6076. //=== NEEDBITS(here.bits + 2);
  6077. n = here_bits + 2;
  6078. while (bits < n) {
  6079. if (have === 0) { break inf_leave; }
  6080. have--;
  6081. hold += input[next++] << bits;
  6082. bits += 8;
  6083. }
  6084. //===//
  6085. //--- DROPBITS(here.bits) ---//
  6086. hold >>>= here_bits;
  6087. bits -= here_bits;
  6088. //---//
  6089. if (state.have === 0) {
  6090. strm.msg = 'invalid bit length repeat';
  6091. state.mode = BAD;
  6092. break;
  6093. }
  6094. len = state.lens[state.have - 1];
  6095. copy = 3 + (hold & 0x03);//BITS(2);
  6096. //--- DROPBITS(2) ---//
  6097. hold >>>= 2;
  6098. bits -= 2;
  6099. //---//
  6100. }
  6101. else if (here_val === 17) {
  6102. //=== NEEDBITS(here.bits + 3);
  6103. n = here_bits + 3;
  6104. while (bits < n) {
  6105. if (have === 0) { break inf_leave; }
  6106. have--;
  6107. hold += input[next++] << bits;
  6108. bits += 8;
  6109. }
  6110. //===//
  6111. //--- DROPBITS(here.bits) ---//
  6112. hold >>>= here_bits;
  6113. bits -= here_bits;
  6114. //---//
  6115. len = 0;
  6116. copy = 3 + (hold & 0x07);//BITS(3);
  6117. //--- DROPBITS(3) ---//
  6118. hold >>>= 3;
  6119. bits -= 3;
  6120. //---//
  6121. }
  6122. else {
  6123. //=== NEEDBITS(here.bits + 7);
  6124. n = here_bits + 7;
  6125. while (bits < n) {
  6126. if (have === 0) { break inf_leave; }
  6127. have--;
  6128. hold += input[next++] << bits;
  6129. bits += 8;
  6130. }
  6131. //===//
  6132. //--- DROPBITS(here.bits) ---//
  6133. hold >>>= here_bits;
  6134. bits -= here_bits;
  6135. //---//
  6136. len = 0;
  6137. copy = 11 + (hold & 0x7f);//BITS(7);
  6138. //--- DROPBITS(7) ---//
  6139. hold >>>= 7;
  6140. bits -= 7;
  6141. //---//
  6142. }
  6143. if (state.have + copy > state.nlen + state.ndist) {
  6144. strm.msg = 'invalid bit length repeat';
  6145. state.mode = BAD;
  6146. break;
  6147. }
  6148. while (copy--) {
  6149. state.lens[state.have++] = len;
  6150. }
  6151. }
  6152. }
  6153. /* handle error breaks in while */
  6154. if (state.mode === BAD) { break; }
  6155. /* check for end-of-block code (better have one) */
  6156. if (state.lens[256] === 0) {
  6157. strm.msg = 'invalid code -- missing end-of-block';
  6158. state.mode = BAD;
  6159. break;
  6160. }
  6161. /* build code tables -- note: do not change the lenbits or distbits
  6162. values here (9 and 6) without reading the comments in inftrees.h
  6163. concerning the ENOUGH constants, which depend on those values */
  6164. state.lenbits = 9;
  6165. opts = {bits: state.lenbits};
  6166. ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
  6167. // We have separate tables & no pointers. 2 commented lines below not needed.
  6168. // state.next_index = opts.table_index;
  6169. state.lenbits = opts.bits;
  6170. // state.lencode = state.next;
  6171. if (ret) {
  6172. strm.msg = 'invalid literal/lengths set';
  6173. state.mode = BAD;
  6174. break;
  6175. }
  6176. state.distbits = 6;
  6177. //state.distcode.copy(state.codes);
  6178. // Switch to use dynamic table
  6179. state.distcode = state.distdyn;
  6180. opts = {bits: state.distbits};
  6181. ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
  6182. // We have separate tables & no pointers. 2 commented lines below not needed.
  6183. // state.next_index = opts.table_index;
  6184. state.distbits = opts.bits;
  6185. // state.distcode = state.next;
  6186. if (ret) {
  6187. strm.msg = 'invalid distances set';
  6188. state.mode = BAD;
  6189. break;
  6190. }
  6191. //Tracev((stderr, 'inflate: codes ok\n'));
  6192. state.mode = LEN_;
  6193. if (flush === Z_TREES) { break inf_leave; }
  6194. /* falls through */
  6195. case LEN_:
  6196. state.mode = LEN;
  6197. /* falls through */
  6198. case LEN:
  6199. if (have >= 6 && left >= 258) {
  6200. //--- RESTORE() ---
  6201. strm.next_out = put;
  6202. strm.avail_out = left;
  6203. strm.next_in = next;
  6204. strm.avail_in = have;
  6205. state.hold = hold;
  6206. state.bits = bits;
  6207. //---
  6208. inflate_fast(strm, _out);
  6209. //--- LOAD() ---
  6210. put = strm.next_out;
  6211. output = strm.output;
  6212. left = strm.avail_out;
  6213. next = strm.next_in;
  6214. input = strm.input;
  6215. have = strm.avail_in;
  6216. hold = state.hold;
  6217. bits = state.bits;
  6218. //---
  6219. if (state.mode === TYPE) {
  6220. state.back = -1;
  6221. }
  6222. break;
  6223. }
  6224. state.back = 0;
  6225. for (;;) {
  6226. here = state.lencode[hold & ((1 << state.lenbits) -1)]; /*BITS(state.lenbits)*/
  6227. here_bits = here >>> 24;
  6228. here_op = (here >>> 16) & 0xff;
  6229. here_val = here & 0xffff;
  6230. if (here_bits <= bits) { break; }
  6231. //--- PULLBYTE() ---//
  6232. if (have === 0) { break inf_leave; }
  6233. have--;
  6234. hold += input[next++] << bits;
  6235. bits += 8;
  6236. //---//
  6237. }
  6238. if (here_op && (here_op & 0xf0) === 0) {
  6239. last_bits = here_bits;
  6240. last_op = here_op;
  6241. last_val = here_val;
  6242. for (;;) {
  6243. here = state.lencode[last_val +
  6244. ((hold & ((1 << (last_bits + last_op)) -1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  6245. here_bits = here >>> 24;
  6246. here_op = (here >>> 16) & 0xff;
  6247. here_val = here & 0xffff;
  6248. if ((last_bits + here_bits) <= bits) { break; }
  6249. //--- PULLBYTE() ---//
  6250. if (have === 0) { break inf_leave; }
  6251. have--;
  6252. hold += input[next++] << bits;
  6253. bits += 8;
  6254. //---//
  6255. }
  6256. //--- DROPBITS(last.bits) ---//
  6257. hold >>>= last_bits;
  6258. bits -= last_bits;
  6259. //---//
  6260. state.back += last_bits;
  6261. }
  6262. //--- DROPBITS(here.bits) ---//
  6263. hold >>>= here_bits;
  6264. bits -= here_bits;
  6265. //---//
  6266. state.back += here_bits;
  6267. state.length = here_val;
  6268. if (here_op === 0) {
  6269. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  6270. // "inflate: literal '%c'\n" :
  6271. // "inflate: literal 0x%02x\n", here.val));
  6272. state.mode = LIT;
  6273. break;
  6274. }
  6275. if (here_op & 32) {
  6276. //Tracevv((stderr, "inflate: end of block\n"));
  6277. state.back = -1;
  6278. state.mode = TYPE;
  6279. break;
  6280. }
  6281. if (here_op & 64) {
  6282. strm.msg = 'invalid literal/length code';
  6283. state.mode = BAD;
  6284. break;
  6285. }
  6286. state.extra = here_op & 15;
  6287. state.mode = LENEXT;
  6288. /* falls through */
  6289. case LENEXT:
  6290. if (state.extra) {
  6291. //=== NEEDBITS(state.extra);
  6292. n = state.extra;
  6293. while (bits < n) {
  6294. if (have === 0) { break inf_leave; }
  6295. have--;
  6296. hold += input[next++] << bits;
  6297. bits += 8;
  6298. }
  6299. //===//
  6300. state.length += hold & ((1 << state.extra) -1)/*BITS(state.extra)*/;
  6301. //--- DROPBITS(state.extra) ---//
  6302. hold >>>= state.extra;
  6303. bits -= state.extra;
  6304. //---//
  6305. state.back += state.extra;
  6306. }
  6307. //Tracevv((stderr, "inflate: length %u\n", state.length));
  6308. state.was = state.length;
  6309. state.mode = DIST;
  6310. /* falls through */
  6311. case DIST:
  6312. for (;;) {
  6313. here = state.distcode[hold & ((1 << state.distbits) -1)];/*BITS(state.distbits)*/
  6314. here_bits = here >>> 24;
  6315. here_op = (here >>> 16) & 0xff;
  6316. here_val = here & 0xffff;
  6317. if ((here_bits) <= bits) { break; }
  6318. //--- PULLBYTE() ---//
  6319. if (have === 0) { break inf_leave; }
  6320. have--;
  6321. hold += input[next++] << bits;
  6322. bits += 8;
  6323. //---//
  6324. }
  6325. if ((here_op & 0xf0) === 0) {
  6326. last_bits = here_bits;
  6327. last_op = here_op;
  6328. last_val = here_val;
  6329. for (;;) {
  6330. here = state.distcode[last_val +
  6331. ((hold & ((1 << (last_bits + last_op)) -1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  6332. here_bits = here >>> 24;
  6333. here_op = (here >>> 16) & 0xff;
  6334. here_val = here & 0xffff;
  6335. if ((last_bits + here_bits) <= bits) { break; }
  6336. //--- PULLBYTE() ---//
  6337. if (have === 0) { break inf_leave; }
  6338. have--;
  6339. hold += input[next++] << bits;
  6340. bits += 8;
  6341. //---//
  6342. }
  6343. //--- DROPBITS(last.bits) ---//
  6344. hold >>>= last_bits;
  6345. bits -= last_bits;
  6346. //---//
  6347. state.back += last_bits;
  6348. }
  6349. //--- DROPBITS(here.bits) ---//
  6350. hold >>>= here_bits;
  6351. bits -= here_bits;
  6352. //---//
  6353. state.back += here_bits;
  6354. if (here_op & 64) {
  6355. strm.msg = 'invalid distance code';
  6356. state.mode = BAD;
  6357. break;
  6358. }
  6359. state.offset = here_val;
  6360. state.extra = (here_op) & 15;
  6361. state.mode = DISTEXT;
  6362. /* falls through */
  6363. case DISTEXT:
  6364. if (state.extra) {
  6365. //=== NEEDBITS(state.extra);
  6366. n = state.extra;
  6367. while (bits < n) {
  6368. if (have === 0) { break inf_leave; }
  6369. have--;
  6370. hold += input[next++] << bits;
  6371. bits += 8;
  6372. }
  6373. //===//
  6374. state.offset += hold & ((1 << state.extra) -1)/*BITS(state.extra)*/;
  6375. //--- DROPBITS(state.extra) ---//
  6376. hold >>>= state.extra;
  6377. bits -= state.extra;
  6378. //---//
  6379. state.back += state.extra;
  6380. }
  6381. //#ifdef INFLATE_STRICT
  6382. if (state.offset > state.dmax) {
  6383. strm.msg = 'invalid distance too far back';
  6384. state.mode = BAD;
  6385. break;
  6386. }
  6387. //#endif
  6388. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  6389. state.mode = MATCH;
  6390. /* falls through */
  6391. case MATCH:
  6392. if (left === 0) { break inf_leave; }
  6393. copy = _out - left;
  6394. if (state.offset > copy) { /* copy from window */
  6395. copy = state.offset - copy;
  6396. if (copy > state.whave) {
  6397. if (state.sane) {
  6398. strm.msg = 'invalid distance too far back';
  6399. state.mode = BAD;
  6400. break;
  6401. }
  6402. // (!) This block is disabled in zlib defailts,
  6403. // don't enable it for binary compatibility
  6404. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  6405. // Trace((stderr, "inflate.c too far\n"));
  6406. // copy -= state.whave;
  6407. // if (copy > state.length) { copy = state.length; }
  6408. // if (copy > left) { copy = left; }
  6409. // left -= copy;
  6410. // state.length -= copy;
  6411. // do {
  6412. // output[put++] = 0;
  6413. // } while (--copy);
  6414. // if (state.length === 0) { state.mode = LEN; }
  6415. // break;
  6416. //#endif
  6417. }
  6418. if (copy > state.wnext) {
  6419. copy -= state.wnext;
  6420. from = state.wsize - copy;
  6421. }
  6422. else {
  6423. from = state.wnext - copy;
  6424. }
  6425. if (copy > state.length) { copy = state.length; }
  6426. from_source = state.window;
  6427. }
  6428. else { /* copy from output */
  6429. from_source = output;
  6430. from = put - state.offset;
  6431. copy = state.length;
  6432. }
  6433. if (copy > left) { copy = left; }
  6434. left -= copy;
  6435. state.length -= copy;
  6436. do {
  6437. output[put++] = from_source[from++];
  6438. } while (--copy);
  6439. if (state.length === 0) { state.mode = LEN; }
  6440. break;
  6441. case LIT:
  6442. if (left === 0) { break inf_leave; }
  6443. output[put++] = state.length;
  6444. left--;
  6445. state.mode = LEN;
  6446. break;
  6447. case CHECK:
  6448. if (state.wrap) {
  6449. //=== NEEDBITS(32);
  6450. while (bits < 32) {
  6451. if (have === 0) { break inf_leave; }
  6452. have--;
  6453. // Use '|' insdead of '+' to make sure that result is signed
  6454. hold |= input[next++] << bits;
  6455. bits += 8;
  6456. }
  6457. //===//
  6458. _out -= left;
  6459. strm.total_out += _out;
  6460. state.total += _out;
  6461. if (_out) {
  6462. strm.adler = state.check =
  6463. /*UPDATE(state.check, put - _out, _out);*/
  6464. (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out));
  6465. }
  6466. _out = left;
  6467. // NB: crc32 stored as signed 32-bit int, ZSWAP32 returns signed too
  6468. if ((state.flags ? hold : ZSWAP32(hold)) !== state.check) {
  6469. strm.msg = 'incorrect data check';
  6470. state.mode = BAD;
  6471. break;
  6472. }
  6473. //=== INITBITS();
  6474. hold = 0;
  6475. bits = 0;
  6476. //===//
  6477. //Tracev((stderr, "inflate: check matches trailer\n"));
  6478. }
  6479. state.mode = LENGTH;
  6480. /* falls through */
  6481. case LENGTH:
  6482. if (state.wrap && state.flags) {
  6483. //=== NEEDBITS(32);
  6484. while (bits < 32) {
  6485. if (have === 0) { break inf_leave; }
  6486. have--;
  6487. hold += input[next++] << bits;
  6488. bits += 8;
  6489. }
  6490. //===//
  6491. if (hold !== (state.total & 0xffffffff)) {
  6492. strm.msg = 'incorrect length check';
  6493. state.mode = BAD;
  6494. break;
  6495. }
  6496. //=== INITBITS();
  6497. hold = 0;
  6498. bits = 0;
  6499. //===//
  6500. //Tracev((stderr, "inflate: length matches trailer\n"));
  6501. }
  6502. state.mode = DONE;
  6503. /* falls through */
  6504. case DONE:
  6505. ret = Z_STREAM_END;
  6506. break inf_leave;
  6507. case BAD:
  6508. ret = Z_DATA_ERROR;
  6509. break inf_leave;
  6510. case MEM:
  6511. return Z_MEM_ERROR;
  6512. case SYNC:
  6513. /* falls through */
  6514. default:
  6515. return Z_STREAM_ERROR;
  6516. }
  6517. }
  6518. // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  6519. /*
  6520. Return from inflate(), updating the total counts and the check value.
  6521. If there was no progress during the inflate() call, return a buffer
  6522. error. Call updatewindow() to create and/or update the window state.
  6523. Note: a memory error from inflate() is non-recoverable.
  6524. */
  6525. //--- RESTORE() ---
  6526. strm.next_out = put;
  6527. strm.avail_out = left;
  6528. strm.next_in = next;
  6529. strm.avail_in = have;
  6530. state.hold = hold;
  6531. state.bits = bits;
  6532. //---
  6533. if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
  6534. (state.mode < CHECK || flush !== Z_FINISH))) {
  6535. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) {
  6536. state.mode = MEM;
  6537. return Z_MEM_ERROR;
  6538. }
  6539. }
  6540. _in -= strm.avail_in;
  6541. _out -= strm.avail_out;
  6542. strm.total_in += _in;
  6543. strm.total_out += _out;
  6544. state.total += _out;
  6545. if (state.wrap && _out) {
  6546. strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/
  6547. (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out));
  6548. }
  6549. strm.data_type = state.bits + (state.last ? 64 : 0) +
  6550. (state.mode === TYPE ? 128 : 0) +
  6551. (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  6552. if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) {
  6553. ret = Z_BUF_ERROR;
  6554. }
  6555. return ret;
  6556. }
  6557. function inflateEnd(strm) {
  6558. if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
  6559. return Z_STREAM_ERROR;
  6560. }
  6561. var state = strm.state;
  6562. if (state.window) {
  6563. state.window = null;
  6564. }
  6565. strm.state = null;
  6566. return Z_OK;
  6567. }
  6568. function inflateGetHeader(strm, head) {
  6569. var state;
  6570. /* check state */
  6571. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  6572. state = strm.state;
  6573. if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
  6574. /* save header structure */
  6575. state.head = head;
  6576. head.done = false;
  6577. return Z_OK;
  6578. }
  6579. exports.inflateReset = inflateReset;
  6580. exports.inflateReset2 = inflateReset2;
  6581. exports.inflateResetKeep = inflateResetKeep;
  6582. exports.inflateInit = inflateInit;
  6583. exports.inflateInit2 = inflateInit2;
  6584. exports.inflate = inflate;
  6585. exports.inflateEnd = inflateEnd;
  6586. exports.inflateGetHeader = inflateGetHeader;
  6587. exports.inflateInfo = 'pako inflate (from Nodeca project)';
  6588. /* Not implemented
  6589. exports.inflateCopy = inflateCopy;
  6590. exports.inflateGetDictionary = inflateGetDictionary;
  6591. exports.inflateMark = inflateMark;
  6592. exports.inflatePrime = inflatePrime;
  6593. exports.inflateSetDictionary = inflateSetDictionary;
  6594. exports.inflateSync = inflateSync;
  6595. exports.inflateSyncPoint = inflateSyncPoint;
  6596. exports.inflateUndermine = inflateUndermine;
  6597. */
  6598. },{"../utils/common":27,"./adler32":29,"./crc32":31,"./inffast":34,"./inftrees":36}],36:[function(_dereq_,module,exports){
  6599. 'use strict';
  6600. var utils = _dereq_('../utils/common');
  6601. var MAXBITS = 15;
  6602. var ENOUGH_LENS = 852;
  6603. var ENOUGH_DISTS = 592;
  6604. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  6605. var CODES = 0;
  6606. var LENS = 1;
  6607. var DISTS = 2;
  6608. var lbase = [ /* Length codes 257..285 base */
  6609. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  6610. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  6611. ];
  6612. var lext = [ /* Length codes 257..285 extra */
  6613. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  6614. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
  6615. ];
  6616. var dbase = [ /* Distance codes 0..29 base */
  6617. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  6618. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  6619. 8193, 12289, 16385, 24577, 0, 0
  6620. ];
  6621. var dext = [ /* Distance codes 0..29 extra */
  6622. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  6623. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  6624. 28, 28, 29, 29, 64, 64
  6625. ];
  6626. module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)
  6627. {
  6628. var bits = opts.bits;
  6629. //here = opts.here; /* table entry for duplication */
  6630. var len = 0; /* a code's length in bits */
  6631. var sym = 0; /* index of code symbols */
  6632. var min = 0, max = 0; /* minimum and maximum code lengths */
  6633. var root = 0; /* number of index bits for root table */
  6634. var curr = 0; /* number of index bits for current table */
  6635. var drop = 0; /* code bits to drop for sub-table */
  6636. var left = 0; /* number of prefix codes available */
  6637. var used = 0; /* code entries in table used */
  6638. var huff = 0; /* Huffman code */
  6639. var incr; /* for incrementing code, index */
  6640. var fill; /* index for replicating entries */
  6641. var low; /* low bits for current root entry */
  6642. var mask; /* mask for low root bits */
  6643. var next; /* next available space in table */
  6644. var base = null; /* base value table to use */
  6645. var base_index = 0;
  6646. // var shoextra; /* extra bits table to use */
  6647. var end; /* use base and extra for symbol > end */
  6648. var count = new utils.Buf16(MAXBITS+1); //[MAXBITS+1]; /* number of codes of each length */
  6649. var offs = new utils.Buf16(MAXBITS+1); //[MAXBITS+1]; /* offsets in table for each length */
  6650. var extra = null;
  6651. var extra_index = 0;
  6652. var here_bits, here_op, here_val;
  6653. /*
  6654. Process a set of code lengths to create a canonical Huffman code. The
  6655. code lengths are lens[0..codes-1]. Each length corresponds to the
  6656. symbols 0..codes-1. The Huffman code is generated by first sorting the
  6657. symbols by length from short to long, and retaining the symbol order
  6658. for codes with equal lengths. Then the code starts with all zero bits
  6659. for the first code of the shortest length, and the codes are integer
  6660. increments for the same length, and zeros are appended as the length
  6661. increases. For the deflate format, these bits are stored backwards
  6662. from their more natural integer increment ordering, and so when the
  6663. decoding tables are built in the large loop below, the integer codes
  6664. are incremented backwards.
  6665. This routine assumes, but does not check, that all of the entries in
  6666. lens[] are in the range 0..MAXBITS. The caller must assure this.
  6667. 1..MAXBITS is interpreted as that code length. zero means that that
  6668. symbol does not occur in this code.
  6669. The codes are sorted by computing a count of codes for each length,
  6670. creating from that a table of starting indices for each length in the
  6671. sorted table, and then entering the symbols in order in the sorted
  6672. table. The sorted table is work[], with that space being provided by
  6673. the caller.
  6674. The length counts are used for other purposes as well, i.e. finding
  6675. the minimum and maximum length codes, determining if there are any
  6676. codes at all, checking for a valid set of lengths, and looking ahead
  6677. at length counts to determine sub-table sizes when building the
  6678. decoding tables.
  6679. */
  6680. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  6681. for (len = 0; len <= MAXBITS; len++) {
  6682. count[len] = 0;
  6683. }
  6684. for (sym = 0; sym < codes; sym++) {
  6685. count[lens[lens_index + sym]]++;
  6686. }
  6687. /* bound code lengths, force root to be within code lengths */
  6688. root = bits;
  6689. for (max = MAXBITS; max >= 1; max--) {
  6690. if (count[max] !== 0) { break; }
  6691. }
  6692. if (root > max) {
  6693. root = max;
  6694. }
  6695. if (max === 0) { /* no symbols to code at all */
  6696. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  6697. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  6698. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  6699. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  6700. //table.op[opts.table_index] = 64;
  6701. //table.bits[opts.table_index] = 1;
  6702. //table.val[opts.table_index++] = 0;
  6703. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  6704. opts.bits = 1;
  6705. return 0; /* no symbols, but wait for decoding to report error */
  6706. }
  6707. for (min = 1; min < max; min++) {
  6708. if (count[min] !== 0) { break; }
  6709. }
  6710. if (root < min) {
  6711. root = min;
  6712. }
  6713. /* check for an over-subscribed or incomplete set of lengths */
  6714. left = 1;
  6715. for (len = 1; len <= MAXBITS; len++) {
  6716. left <<= 1;
  6717. left -= count[len];
  6718. if (left < 0) {
  6719. return -1;
  6720. } /* over-subscribed */
  6721. }
  6722. if (left > 0 && (type === CODES || max !== 1)) {
  6723. return -1; /* incomplete set */
  6724. }
  6725. /* generate offsets into symbol table for each length for sorting */
  6726. offs[1] = 0;
  6727. for (len = 1; len < MAXBITS; len++) {
  6728. offs[len + 1] = offs[len] + count[len];
  6729. }
  6730. /* sort symbols by length, by symbol order within each length */
  6731. for (sym = 0; sym < codes; sym++) {
  6732. if (lens[lens_index + sym] !== 0) {
  6733. work[offs[lens[lens_index + sym]]++] = sym;
  6734. }
  6735. }
  6736. /*
  6737. Create and fill in decoding tables. In this loop, the table being
  6738. filled is at next and has curr index bits. The code being used is huff
  6739. with length len. That code is converted to an index by dropping drop
  6740. bits off of the bottom. For codes where len is less than drop + curr,
  6741. those top drop + curr - len bits are incremented through all values to
  6742. fill the table with replicated entries.
  6743. root is the number of index bits for the root table. When len exceeds
  6744. root, sub-tables are created pointed to by the root entry with an index
  6745. of the low root bits of huff. This is saved in low to check for when a
  6746. new sub-table should be started. drop is zero when the root table is
  6747. being filled, and drop is root when sub-tables are being filled.
  6748. When a new sub-table is needed, it is necessary to look ahead in the
  6749. code lengths to determine what size sub-table is needed. The length
  6750. counts are used for this, and so count[] is decremented as codes are
  6751. entered in the tables.
  6752. used keeps track of how many table entries have been allocated from the
  6753. provided *table space. It is checked for LENS and DIST tables against
  6754. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  6755. the initial root table size constants. See the comments in inftrees.h
  6756. for more information.
  6757. sym increments through all symbols, and the loop terminates when
  6758. all codes of length max, i.e. all codes, have been processed. This
  6759. routine permits incomplete codes, so another loop after this one fills
  6760. in the rest of the decoding tables with invalid code markers.
  6761. */
  6762. /* set up for code type */
  6763. // poor man optimization - use if-else instead of switch,
  6764. // to avoid deopts in old v8
  6765. if (type === CODES) {
  6766. base = extra = work; /* dummy value--not used */
  6767. end = 19;
  6768. } else if (type === LENS) {
  6769. base = lbase;
  6770. base_index -= 257;
  6771. extra = lext;
  6772. extra_index -= 257;
  6773. end = 256;
  6774. } else { /* DISTS */
  6775. base = dbase;
  6776. extra = dext;
  6777. end = -1;
  6778. }
  6779. /* initialize opts for loop */
  6780. huff = 0; /* starting code */
  6781. sym = 0; /* starting code symbol */
  6782. len = min; /* starting code length */
  6783. next = table_index; /* current table to fill in */
  6784. curr = root; /* current table index bits */
  6785. drop = 0; /* current bits to drop from code for index */
  6786. low = -1; /* trigger new sub-table when len > root */
  6787. used = 1 << root; /* use root table entries */
  6788. mask = used - 1; /* mask for comparing low */
  6789. /* check available table space */
  6790. if ((type === LENS && used > ENOUGH_LENS) ||
  6791. (type === DISTS && used > ENOUGH_DISTS)) {
  6792. return 1;
  6793. }
  6794. var i=0;
  6795. /* process all codes and make table entries */
  6796. for (;;) {
  6797. i++;
  6798. /* create table entry */
  6799. here_bits = len - drop;
  6800. if (work[sym] < end) {
  6801. here_op = 0;
  6802. here_val = work[sym];
  6803. }
  6804. else if (work[sym] > end) {
  6805. here_op = extra[extra_index + work[sym]];
  6806. here_val = base[base_index + work[sym]];
  6807. }
  6808. else {
  6809. here_op = 32 + 64; /* end of block */
  6810. here_val = 0;
  6811. }
  6812. /* replicate for those indices with low len bits equal to huff */
  6813. incr = 1 << (len - drop);
  6814. fill = 1 << curr;
  6815. min = fill; /* save offset to next table */
  6816. do {
  6817. fill -= incr;
  6818. table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
  6819. } while (fill !== 0);
  6820. /* backwards increment the len-bit code huff */
  6821. incr = 1 << (len - 1);
  6822. while (huff & incr) {
  6823. incr >>= 1;
  6824. }
  6825. if (incr !== 0) {
  6826. huff &= incr - 1;
  6827. huff += incr;
  6828. } else {
  6829. huff = 0;
  6830. }
  6831. /* go to next symbol, update count, len */
  6832. sym++;
  6833. if (--count[len] === 0) {
  6834. if (len === max) { break; }
  6835. len = lens[lens_index + work[sym]];
  6836. }
  6837. /* create new sub-table if needed */
  6838. if (len > root && (huff & mask) !== low) {
  6839. /* if first time, transition to sub-tables */
  6840. if (drop === 0) {
  6841. drop = root;
  6842. }
  6843. /* increment past last table */
  6844. next += min; /* here min is 1 << curr */
  6845. /* determine length of next table */
  6846. curr = len - drop;
  6847. left = 1 << curr;
  6848. while (curr + drop < max) {
  6849. left -= count[curr + drop];
  6850. if (left <= 0) { break; }
  6851. curr++;
  6852. left <<= 1;
  6853. }
  6854. /* check for enough space */
  6855. used += 1 << curr;
  6856. if ((type === LENS && used > ENOUGH_LENS) ||
  6857. (type === DISTS && used > ENOUGH_DISTS)) {
  6858. return 1;
  6859. }
  6860. /* point entry in root table to sub-table */
  6861. low = huff & mask;
  6862. /*table.op[low] = curr;
  6863. table.bits[low] = root;
  6864. table.val[low] = next - opts.table_index;*/
  6865. table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
  6866. }
  6867. }
  6868. /* fill in remaining table entry if code is incomplete (guaranteed to have
  6869. at most one remaining entry, since if the code is incomplete, the
  6870. maximum code length that was allowed to get this far is one bit) */
  6871. if (huff !== 0) {
  6872. //table.op[next + huff] = 64; /* invalid code marker */
  6873. //table.bits[next + huff] = len - drop;
  6874. //table.val[next + huff] = 0;
  6875. table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
  6876. }
  6877. /* set return parameters */
  6878. //opts.table_index += used;
  6879. opts.bits = root;
  6880. return 0;
  6881. };
  6882. },{"../utils/common":27}],37:[function(_dereq_,module,exports){
  6883. 'use strict';
  6884. module.exports = {
  6885. '2': 'need dictionary', /* Z_NEED_DICT 2 */
  6886. '1': 'stream end', /* Z_STREAM_END 1 */
  6887. '0': '', /* Z_OK 0 */
  6888. '-1': 'file error', /* Z_ERRNO (-1) */
  6889. '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
  6890. '-3': 'data error', /* Z_DATA_ERROR (-3) */
  6891. '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
  6892. '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
  6893. '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
  6894. };
  6895. },{}],38:[function(_dereq_,module,exports){
  6896. 'use strict';
  6897. var utils = _dereq_('../utils/common');
  6898. /* Public constants ==========================================================*/
  6899. /* ===========================================================================*/
  6900. //var Z_FILTERED = 1;
  6901. //var Z_HUFFMAN_ONLY = 2;
  6902. //var Z_RLE = 3;
  6903. var Z_FIXED = 4;
  6904. //var Z_DEFAULT_STRATEGY = 0;
  6905. /* Possible values of the data_type field (though see inflate()) */
  6906. var Z_BINARY = 0;
  6907. var Z_TEXT = 1;
  6908. //var Z_ASCII = 1; // = Z_TEXT
  6909. var Z_UNKNOWN = 2;
  6910. /*============================================================================*/
  6911. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  6912. // From zutil.h
  6913. var STORED_BLOCK = 0;
  6914. var STATIC_TREES = 1;
  6915. var DYN_TREES = 2;
  6916. /* The three kinds of block type */
  6917. var MIN_MATCH = 3;
  6918. var MAX_MATCH = 258;
  6919. /* The minimum and maximum match lengths */
  6920. // From deflate.h
  6921. /* ===========================================================================
  6922. * Internal compression state.
  6923. */
  6924. var LENGTH_CODES = 29;
  6925. /* number of length codes, not counting the special END_BLOCK code */
  6926. var LITERALS = 256;
  6927. /* number of literal bytes 0..255 */
  6928. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  6929. /* number of Literal or Length codes, including the END_BLOCK code */
  6930. var D_CODES = 30;
  6931. /* number of distance codes */
  6932. var BL_CODES = 19;
  6933. /* number of codes used to transfer the bit lengths */
  6934. var HEAP_SIZE = 2*L_CODES + 1;
  6935. /* maximum heap size */
  6936. var MAX_BITS = 15;
  6937. /* All codes must not exceed MAX_BITS bits */
  6938. var Buf_size = 16;
  6939. /* size of bit buffer in bi_buf */
  6940. /* ===========================================================================
  6941. * Constants
  6942. */
  6943. var MAX_BL_BITS = 7;
  6944. /* Bit length codes must not exceed MAX_BL_BITS bits */
  6945. var END_BLOCK = 256;
  6946. /* end of block literal code */
  6947. var REP_3_6 = 16;
  6948. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  6949. var REPZ_3_10 = 17;
  6950. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  6951. var REPZ_11_138 = 18;
  6952. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  6953. var extra_lbits = /* extra bits for each length code */
  6954. [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];
  6955. var extra_dbits = /* extra bits for each distance code */
  6956. [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];
  6957. var extra_blbits = /* extra bits for each bit length code */
  6958. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7];
  6959. var bl_order =
  6960. [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];
  6961. /* The lengths of the bit length codes are sent in order of decreasing
  6962. * probability, to avoid transmitting the lengths for unused bit length codes.
  6963. */
  6964. /* ===========================================================================
  6965. * Local data. These are initialized only once.
  6966. */
  6967. // We pre-fill arrays with 0 to avoid uninitialized gaps
  6968. var DIST_CODE_LEN = 512; /* see definition of array dist_code below */
  6969. // !!!! Use flat array insdead of structure, Freq = i*2, Len = i*2+1
  6970. var static_ltree = new Array((L_CODES+2) * 2);
  6971. zero(static_ltree);
  6972. /* The static literal tree. Since the bit lengths are imposed, there is no
  6973. * need for the L_CODES extra codes used during heap construction. However
  6974. * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  6975. * below).
  6976. */
  6977. var static_dtree = new Array(D_CODES * 2);
  6978. zero(static_dtree);
  6979. /* The static distance tree. (Actually a trivial tree since all codes use
  6980. * 5 bits.)
  6981. */
  6982. var _dist_code = new Array(DIST_CODE_LEN);
  6983. zero(_dist_code);
  6984. /* Distance codes. The first 256 values correspond to the distances
  6985. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  6986. * the 15 bit distances.
  6987. */
  6988. var _length_code = new Array(MAX_MATCH-MIN_MATCH+1);
  6989. zero(_length_code);
  6990. /* length code for each normalized match length (0 == MIN_MATCH) */
  6991. var base_length = new Array(LENGTH_CODES);
  6992. zero(base_length);
  6993. /* First normalized length for each code (0 = MIN_MATCH) */
  6994. var base_dist = new Array(D_CODES);
  6995. zero(base_dist);
  6996. /* First normalized distance for each code (0 = distance of 1) */
  6997. var StaticTreeDesc = function (static_tree, extra_bits, extra_base, elems, max_length) {
  6998. this.static_tree = static_tree; /* static tree or NULL */
  6999. this.extra_bits = extra_bits; /* extra bits for each code or NULL */
  7000. this.extra_base = extra_base; /* base index for extra_bits */
  7001. this.elems = elems; /* max number of elements in the tree */
  7002. this.max_length = max_length; /* max bit length for the codes */
  7003. // show if `static_tree` has data or dummy - needed for monomorphic objects
  7004. this.has_stree = static_tree && static_tree.length;
  7005. };
  7006. var static_l_desc;
  7007. var static_d_desc;
  7008. var static_bl_desc;
  7009. var TreeDesc = function(dyn_tree, stat_desc) {
  7010. this.dyn_tree = dyn_tree; /* the dynamic tree */
  7011. this.max_code = 0; /* largest code with non zero frequency */
  7012. this.stat_desc = stat_desc; /* the corresponding static tree */
  7013. };
  7014. function d_code(dist) {
  7015. return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
  7016. }
  7017. /* ===========================================================================
  7018. * Output a short LSB first on the stream.
  7019. * IN assertion: there is enough room in pendingBuf.
  7020. */
  7021. function put_short (s, w) {
  7022. // put_byte(s, (uch)((w) & 0xff));
  7023. // put_byte(s, (uch)((ush)(w) >> 8));
  7024. s.pending_buf[s.pending++] = (w) & 0xff;
  7025. s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
  7026. }
  7027. /* ===========================================================================
  7028. * Send a value on a given number of bits.
  7029. * IN assertion: length <= 16 and value fits in length bits.
  7030. */
  7031. function send_bits(s, value, length) {
  7032. if (s.bi_valid > (Buf_size - length)) {
  7033. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  7034. put_short(s, s.bi_buf);
  7035. s.bi_buf = value >> (Buf_size - s.bi_valid);
  7036. s.bi_valid += length - Buf_size;
  7037. } else {
  7038. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  7039. s.bi_valid += length;
  7040. }
  7041. }
  7042. function send_code(s, c, tree) {
  7043. send_bits(s, tree[c*2]/*.Code*/, tree[c*2 + 1]/*.Len*/);
  7044. }
  7045. /* ===========================================================================
  7046. * Reverse the first len bits of a code, using straightforward code (a faster
  7047. * method would use a table)
  7048. * IN assertion: 1 <= len <= 15
  7049. */
  7050. function bi_reverse(code, len) {
  7051. var res = 0;
  7052. do {
  7053. res |= code & 1;
  7054. code >>>= 1;
  7055. res <<= 1;
  7056. } while (--len > 0);
  7057. return res >>> 1;
  7058. }
  7059. /* ===========================================================================
  7060. * Flush the bit buffer, keeping at most 7 bits in it.
  7061. */
  7062. function bi_flush(s) {
  7063. if (s.bi_valid === 16) {
  7064. put_short(s, s.bi_buf);
  7065. s.bi_buf = 0;
  7066. s.bi_valid = 0;
  7067. } else if (s.bi_valid >= 8) {
  7068. s.pending_buf[s.pending++] = s.bi_buf & 0xff;
  7069. s.bi_buf >>= 8;
  7070. s.bi_valid -= 8;
  7071. }
  7072. }
  7073. /* ===========================================================================
  7074. * Compute the optimal bit lengths for a tree and update the total bit length
  7075. * for the current block.
  7076. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  7077. * above are the tree nodes sorted by increasing frequency.
  7078. * OUT assertions: the field len is set to the optimal bit length, the
  7079. * array bl_count contains the frequencies for each bit length.
  7080. * The length opt_len is updated; static_len is also updated if stree is
  7081. * not null.
  7082. */
  7083. function gen_bitlen(s, desc)
  7084. // deflate_state *s;
  7085. // tree_desc *desc; /* the tree descriptor */
  7086. {
  7087. var tree = desc.dyn_tree;
  7088. var max_code = desc.max_code;
  7089. var stree = desc.stat_desc.static_tree;
  7090. var has_stree = desc.stat_desc.has_stree;
  7091. var extra = desc.stat_desc.extra_bits;
  7092. var base = desc.stat_desc.extra_base;
  7093. var max_length = desc.stat_desc.max_length;
  7094. var h; /* heap index */
  7095. var n, m; /* iterate over the tree elements */
  7096. var bits; /* bit length */
  7097. var xbits; /* extra bits */
  7098. var f; /* frequency */
  7099. var overflow = 0; /* number of elements with bit length too large */
  7100. for (bits = 0; bits <= MAX_BITS; bits++) {
  7101. s.bl_count[bits] = 0;
  7102. }
  7103. /* In a first pass, compute the optimal bit lengths (which may
  7104. * overflow in the case of the bit length tree).
  7105. */
  7106. tree[s.heap[s.heap_max]*2 + 1]/*.Len*/ = 0; /* root of the heap */
  7107. for (h = s.heap_max+1; h < HEAP_SIZE; h++) {
  7108. n = s.heap[h];
  7109. bits = tree[tree[n*2 +1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
  7110. if (bits > max_length) {
  7111. bits = max_length;
  7112. overflow++;
  7113. }
  7114. tree[n*2 + 1]/*.Len*/ = bits;
  7115. /* We overwrite tree[n].Dad which is no longer needed */
  7116. if (n > max_code) { continue; } /* not a leaf node */
  7117. s.bl_count[bits]++;
  7118. xbits = 0;
  7119. if (n >= base) {
  7120. xbits = extra[n-base];
  7121. }
  7122. f = tree[n * 2]/*.Freq*/;
  7123. s.opt_len += f * (bits + xbits);
  7124. if (has_stree) {
  7125. s.static_len += f * (stree[n*2 + 1]/*.Len*/ + xbits);
  7126. }
  7127. }
  7128. if (overflow === 0) { return; }
  7129. // Trace((stderr,"\nbit length overflow\n"));
  7130. /* This happens for example on obj2 and pic of the Calgary corpus */
  7131. /* Find the first bit length which could increase: */
  7132. do {
  7133. bits = max_length-1;
  7134. while (s.bl_count[bits] === 0) { bits--; }
  7135. s.bl_count[bits]--; /* move one leaf down the tree */
  7136. s.bl_count[bits+1] += 2; /* move one overflow item as its brother */
  7137. s.bl_count[max_length]--;
  7138. /* The brother of the overflow item also moves one step up,
  7139. * but this does not affect bl_count[max_length]
  7140. */
  7141. overflow -= 2;
  7142. } while (overflow > 0);
  7143. /* Now recompute all bit lengths, scanning in increasing frequency.
  7144. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  7145. * lengths instead of fixing only the wrong ones. This idea is taken
  7146. * from 'ar' written by Haruhiko Okumura.)
  7147. */
  7148. for (bits = max_length; bits !== 0; bits--) {
  7149. n = s.bl_count[bits];
  7150. while (n !== 0) {
  7151. m = s.heap[--h];
  7152. if (m > max_code) { continue; }
  7153. if (tree[m*2 + 1]/*.Len*/ !== bits) {
  7154. // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  7155. s.opt_len += (bits - tree[m*2 + 1]/*.Len*/)*tree[m*2]/*.Freq*/;
  7156. tree[m*2 + 1]/*.Len*/ = bits;
  7157. }
  7158. n--;
  7159. }
  7160. }
  7161. }
  7162. /* ===========================================================================
  7163. * Generate the codes for a given tree and bit counts (which need not be
  7164. * optimal).
  7165. * IN assertion: the array bl_count contains the bit length statistics for
  7166. * the given tree and the field len is set for all tree elements.
  7167. * OUT assertion: the field code is set for all tree elements of non
  7168. * zero code length.
  7169. */
  7170. function gen_codes(tree, max_code, bl_count)
  7171. // ct_data *tree; /* the tree to decorate */
  7172. // int max_code; /* largest code with non zero frequency */
  7173. // ushf *bl_count; /* number of codes at each bit length */
  7174. {
  7175. var next_code = new Array(MAX_BITS+1); /* next code value for each bit length */
  7176. var code = 0; /* running code value */
  7177. var bits; /* bit index */
  7178. var n; /* code index */
  7179. /* The distribution counts are first used to generate the code values
  7180. * without bit reversal.
  7181. */
  7182. for (bits = 1; bits <= MAX_BITS; bits++) {
  7183. next_code[bits] = code = (code + bl_count[bits-1]) << 1;
  7184. }
  7185. /* Check that the bit counts in bl_count are consistent. The last code
  7186. * must be all ones.
  7187. */
  7188. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  7189. // "inconsistent bit counts");
  7190. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  7191. for (n = 0; n <= max_code; n++) {
  7192. var len = tree[n*2 + 1]/*.Len*/;
  7193. if (len === 0) { continue; }
  7194. /* Now reverse the bits */
  7195. tree[n*2]/*.Code*/ = bi_reverse(next_code[len]++, len);
  7196. //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  7197. // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  7198. }
  7199. }
  7200. /* ===========================================================================
  7201. * Initialize the various 'constant' tables.
  7202. */
  7203. function tr_static_init() {
  7204. var n; /* iterates over tree elements */
  7205. var bits; /* bit counter */
  7206. var length; /* length value */
  7207. var code; /* code value */
  7208. var dist; /* distance index */
  7209. var bl_count = new Array(MAX_BITS+1);
  7210. /* number of codes at each bit length for an optimal tree */
  7211. // do check in _tr_init()
  7212. //if (static_init_done) return;
  7213. /* For some embedded targets, global variables are not initialized: */
  7214. /*#ifdef NO_INIT_GLOBAL_POINTERS
  7215. static_l_desc.static_tree = static_ltree;
  7216. static_l_desc.extra_bits = extra_lbits;
  7217. static_d_desc.static_tree = static_dtree;
  7218. static_d_desc.extra_bits = extra_dbits;
  7219. static_bl_desc.extra_bits = extra_blbits;
  7220. #endif*/
  7221. /* Initialize the mapping length (0..255) -> length code (0..28) */
  7222. length = 0;
  7223. for (code = 0; code < LENGTH_CODES-1; code++) {
  7224. base_length[code] = length;
  7225. for (n = 0; n < (1<<extra_lbits[code]); n++) {
  7226. _length_code[length++] = code;
  7227. }
  7228. }
  7229. //Assert (length == 256, "tr_static_init: length != 256");
  7230. /* Note that the length 255 (match length 258) can be represented
  7231. * in two different ways: code 284 + 5 bits or code 285, so we
  7232. * overwrite length_code[255] to use the best encoding:
  7233. */
  7234. _length_code[length-1] = code;
  7235. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  7236. dist = 0;
  7237. for (code = 0 ; code < 16; code++) {
  7238. base_dist[code] = dist;
  7239. for (n = 0; n < (1<<extra_dbits[code]); n++) {
  7240. _dist_code[dist++] = code;
  7241. }
  7242. }
  7243. //Assert (dist == 256, "tr_static_init: dist != 256");
  7244. dist >>= 7; /* from now on, all distances are divided by 128 */
  7245. for ( ; code < D_CODES; code++) {
  7246. base_dist[code] = dist << 7;
  7247. for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
  7248. _dist_code[256 + dist++] = code;
  7249. }
  7250. }
  7251. //Assert (dist == 256, "tr_static_init: 256+dist != 512");
  7252. /* Construct the codes of the static literal tree */
  7253. for (bits = 0; bits <= MAX_BITS; bits++) {
  7254. bl_count[bits] = 0;
  7255. }
  7256. n = 0;
  7257. while (n <= 143) {
  7258. static_ltree[n*2 + 1]/*.Len*/ = 8;
  7259. n++;
  7260. bl_count[8]++;
  7261. }
  7262. while (n <= 255) {
  7263. static_ltree[n*2 + 1]/*.Len*/ = 9;
  7264. n++;
  7265. bl_count[9]++;
  7266. }
  7267. while (n <= 279) {
  7268. static_ltree[n*2 + 1]/*.Len*/ = 7;
  7269. n++;
  7270. bl_count[7]++;
  7271. }
  7272. while (n <= 287) {
  7273. static_ltree[n*2 + 1]/*.Len*/ = 8;
  7274. n++;
  7275. bl_count[8]++;
  7276. }
  7277. /* Codes 286 and 287 do not exist, but we must include them in the
  7278. * tree construction to get a canonical Huffman tree (longest code
  7279. * all ones)
  7280. */
  7281. gen_codes(static_ltree, L_CODES+1, bl_count);
  7282. /* The static distance tree is trivial: */
  7283. for (n = 0; n < D_CODES; n++) {
  7284. static_dtree[n*2 + 1]/*.Len*/ = 5;
  7285. static_dtree[n*2]/*.Code*/ = bi_reverse(n, 5);
  7286. }
  7287. // Now data ready and we can init static trees
  7288. static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS);
  7289. static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
  7290. static_bl_desc =new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
  7291. //static_init_done = true;
  7292. }
  7293. /* ===========================================================================
  7294. * Initialize a new block.
  7295. */
  7296. function init_block(s) {
  7297. var n; /* iterates over tree elements */
  7298. /* Initialize the trees. */
  7299. for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n*2]/*.Freq*/ = 0; }
  7300. for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n*2]/*.Freq*/ = 0; }
  7301. for (n = 0; n < BL_CODES; n++) { s.bl_tree[n*2]/*.Freq*/ = 0; }
  7302. s.dyn_ltree[END_BLOCK*2]/*.Freq*/ = 1;
  7303. s.opt_len = s.static_len = 0;
  7304. s.last_lit = s.matches = 0;
  7305. }
  7306. /* ===========================================================================
  7307. * Flush the bit buffer and align the output on a byte boundary
  7308. */
  7309. function bi_windup(s)
  7310. {
  7311. if (s.bi_valid > 8) {
  7312. put_short(s, s.bi_buf);
  7313. } else if (s.bi_valid > 0) {
  7314. //put_byte(s, (Byte)s->bi_buf);
  7315. s.pending_buf[s.pending++] = s.bi_buf;
  7316. }
  7317. s.bi_buf = 0;
  7318. s.bi_valid = 0;
  7319. }
  7320. /* ===========================================================================
  7321. * Copy a stored block, storing first the length and its
  7322. * one's complement if requested.
  7323. */
  7324. function copy_block(s, buf, len, header)
  7325. //DeflateState *s;
  7326. //charf *buf; /* the input data */
  7327. //unsigned len; /* its length */
  7328. //int header; /* true if block header must be written */
  7329. {
  7330. bi_windup(s); /* align on byte boundary */
  7331. if (header) {
  7332. put_short(s, len);
  7333. put_short(s, ~len);
  7334. }
  7335. // while (len--) {
  7336. // put_byte(s, *buf++);
  7337. // }
  7338. utils.arraySet(s.pending_buf, s.window, buf, len, s.pending);
  7339. s.pending += len;
  7340. }
  7341. /* ===========================================================================
  7342. * Compares to subtrees, using the tree depth as tie breaker when
  7343. * the subtrees have equal frequency. This minimizes the worst case length.
  7344. */
  7345. function smaller(tree, n, m, depth) {
  7346. var _n2 = n*2;
  7347. var _m2 = m*2;
  7348. return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
  7349. (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
  7350. }
  7351. /* ===========================================================================
  7352. * Restore the heap property by moving down the tree starting at node k,
  7353. * exchanging a node with the smallest of its two sons if necessary, stopping
  7354. * when the heap property is re-established (each father smaller than its
  7355. * two sons).
  7356. */
  7357. function pqdownheap(s, tree, k)
  7358. // deflate_state *s;
  7359. // ct_data *tree; /* the tree to restore */
  7360. // int k; /* node to move down */
  7361. {
  7362. var v = s.heap[k];
  7363. var j = k << 1; /* left son of k */
  7364. while (j <= s.heap_len) {
  7365. /* Set j to the smallest of the two sons: */
  7366. if (j < s.heap_len &&
  7367. smaller(tree, s.heap[j+1], s.heap[j], s.depth)) {
  7368. j++;
  7369. }
  7370. /* Exit if v is smaller than both sons */
  7371. if (smaller(tree, v, s.heap[j], s.depth)) { break; }
  7372. /* Exchange v with the smallest son */
  7373. s.heap[k] = s.heap[j];
  7374. k = j;
  7375. /* And continue down the tree, setting j to the left son of k */
  7376. j <<= 1;
  7377. }
  7378. s.heap[k] = v;
  7379. }
  7380. // inlined manually
  7381. // var SMALLEST = 1;
  7382. /* ===========================================================================
  7383. * Send the block data compressed using the given Huffman trees
  7384. */
  7385. function compress_block(s, ltree, dtree)
  7386. // deflate_state *s;
  7387. // const ct_data *ltree; /* literal tree */
  7388. // const ct_data *dtree; /* distance tree */
  7389. {
  7390. var dist; /* distance of matched string */
  7391. var lc; /* match length or unmatched char (if dist == 0) */
  7392. var lx = 0; /* running index in l_buf */
  7393. var code; /* the code to send */
  7394. var extra; /* number of extra bits to send */
  7395. if (s.last_lit !== 0) {
  7396. do {
  7397. dist = (s.pending_buf[s.d_buf + lx*2] << 8) | (s.pending_buf[s.d_buf + lx*2 + 1]);
  7398. lc = s.pending_buf[s.l_buf + lx];
  7399. lx++;
  7400. if (dist === 0) {
  7401. send_code(s, lc, ltree); /* send a literal byte */
  7402. //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  7403. } else {
  7404. /* Here, lc is the match length - MIN_MATCH */
  7405. code = _length_code[lc];
  7406. send_code(s, code+LITERALS+1, ltree); /* send the length code */
  7407. extra = extra_lbits[code];
  7408. if (extra !== 0) {
  7409. lc -= base_length[code];
  7410. send_bits(s, lc, extra); /* send the extra length bits */
  7411. }
  7412. dist--; /* dist is now the match distance - 1 */
  7413. code = d_code(dist);
  7414. //Assert (code < D_CODES, "bad d_code");
  7415. send_code(s, code, dtree); /* send the distance code */
  7416. extra = extra_dbits[code];
  7417. if (extra !== 0) {
  7418. dist -= base_dist[code];
  7419. send_bits(s, dist, extra); /* send the extra distance bits */
  7420. }
  7421. } /* literal or match pair ? */
  7422. /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  7423. //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
  7424. // "pendingBuf overflow");
  7425. } while (lx < s.last_lit);
  7426. }
  7427. send_code(s, END_BLOCK, ltree);
  7428. }
  7429. /* ===========================================================================
  7430. * Construct one Huffman tree and assigns the code bit strings and lengths.
  7431. * Update the total bit length for the current block.
  7432. * IN assertion: the field freq is set for all tree elements.
  7433. * OUT assertions: the fields len and code are set to the optimal bit length
  7434. * and corresponding code. The length opt_len is updated; static_len is
  7435. * also updated if stree is not null. The field max_code is set.
  7436. */
  7437. function build_tree(s, desc)
  7438. // deflate_state *s;
  7439. // tree_desc *desc; /* the tree descriptor */
  7440. {
  7441. var tree = desc.dyn_tree;
  7442. var stree = desc.stat_desc.static_tree;
  7443. var has_stree = desc.stat_desc.has_stree;
  7444. var elems = desc.stat_desc.elems;
  7445. var n, m; /* iterate over heap elements */
  7446. var max_code = -1; /* largest code with non zero frequency */
  7447. var node; /* new node being created */
  7448. /* Construct the initial heap, with least frequent element in
  7449. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  7450. * heap[0] is not used.
  7451. */
  7452. s.heap_len = 0;
  7453. s.heap_max = HEAP_SIZE;
  7454. for (n = 0; n < elems; n++) {
  7455. if (tree[n * 2]/*.Freq*/ !== 0) {
  7456. s.heap[++s.heap_len] = max_code = n;
  7457. s.depth[n] = 0;
  7458. } else {
  7459. tree[n*2 + 1]/*.Len*/ = 0;
  7460. }
  7461. }
  7462. /* The pkzip format requires that at least one distance code exists,
  7463. * and that at least one bit should be sent even if there is only one
  7464. * possible code. So to avoid special checks later on we force at least
  7465. * two codes of non zero frequency.
  7466. */
  7467. while (s.heap_len < 2) {
  7468. node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
  7469. tree[node * 2]/*.Freq*/ = 1;
  7470. s.depth[node] = 0;
  7471. s.opt_len--;
  7472. if (has_stree) {
  7473. s.static_len -= stree[node*2 + 1]/*.Len*/;
  7474. }
  7475. /* node is 0 or 1 so it does not have extra bits */
  7476. }
  7477. desc.max_code = max_code;
  7478. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  7479. * establish sub-heaps of increasing lengths:
  7480. */
  7481. for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
  7482. /* Construct the Huffman tree by repeatedly combining the least two
  7483. * frequent nodes.
  7484. */
  7485. node = elems; /* next internal node of the tree */
  7486. do {
  7487. //pqremove(s, tree, n); /* n = node of least frequency */
  7488. /*** pqremove ***/
  7489. n = s.heap[1/*SMALLEST*/];
  7490. s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
  7491. pqdownheap(s, tree, 1/*SMALLEST*/);
  7492. /***/
  7493. m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
  7494. s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
  7495. s.heap[--s.heap_max] = m;
  7496. /* Create a new node father of n and m */
  7497. tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
  7498. s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
  7499. tree[n*2 + 1]/*.Dad*/ = tree[m*2 + 1]/*.Dad*/ = node;
  7500. /* and insert the new node in the heap */
  7501. s.heap[1/*SMALLEST*/] = node++;
  7502. pqdownheap(s, tree, 1/*SMALLEST*/);
  7503. } while (s.heap_len >= 2);
  7504. s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
  7505. /* At this point, the fields freq and dad are set. We can now
  7506. * generate the bit lengths.
  7507. */
  7508. gen_bitlen(s, desc);
  7509. /* The field len is now set, we can generate the bit codes */
  7510. gen_codes(tree, max_code, s.bl_count);
  7511. }
  7512. /* ===========================================================================
  7513. * Scan a literal or distance tree to determine the frequencies of the codes
  7514. * in the bit length tree.
  7515. */
  7516. function scan_tree(s, tree, max_code)
  7517. // deflate_state *s;
  7518. // ct_data *tree; /* the tree to be scanned */
  7519. // int max_code; /* and its largest code of non zero frequency */
  7520. {
  7521. var n; /* iterates over all tree elements */
  7522. var prevlen = -1; /* last emitted length */
  7523. var curlen; /* length of current code */
  7524. var nextlen = tree[0*2 + 1]/*.Len*/; /* length of next code */
  7525. var count = 0; /* repeat count of the current code */
  7526. var max_count = 7; /* max repeat count */
  7527. var min_count = 4; /* min repeat count */
  7528. if (nextlen === 0) {
  7529. max_count = 138;
  7530. min_count = 3;
  7531. }
  7532. tree[(max_code+1)*2 + 1]/*.Len*/ = 0xffff; /* guard */
  7533. for (n = 0; n <= max_code; n++) {
  7534. curlen = nextlen;
  7535. nextlen = tree[(n+1)*2 + 1]/*.Len*/;
  7536. if (++count < max_count && curlen === nextlen) {
  7537. continue;
  7538. } else if (count < min_count) {
  7539. s.bl_tree[curlen * 2]/*.Freq*/ += count;
  7540. } else if (curlen !== 0) {
  7541. if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
  7542. s.bl_tree[REP_3_6*2]/*.Freq*/++;
  7543. } else if (count <= 10) {
  7544. s.bl_tree[REPZ_3_10*2]/*.Freq*/++;
  7545. } else {
  7546. s.bl_tree[REPZ_11_138*2]/*.Freq*/++;
  7547. }
  7548. count = 0;
  7549. prevlen = curlen;
  7550. if (nextlen === 0) {
  7551. max_count = 138;
  7552. min_count = 3;
  7553. } else if (curlen === nextlen) {
  7554. max_count = 6;
  7555. min_count = 3;
  7556. } else {
  7557. max_count = 7;
  7558. min_count = 4;
  7559. }
  7560. }
  7561. }
  7562. /* ===========================================================================
  7563. * Send a literal or distance tree in compressed form, using the codes in
  7564. * bl_tree.
  7565. */
  7566. function send_tree(s, tree, max_code)
  7567. // deflate_state *s;
  7568. // ct_data *tree; /* the tree to be scanned */
  7569. // int max_code; /* and its largest code of non zero frequency */
  7570. {
  7571. var n; /* iterates over all tree elements */
  7572. var prevlen = -1; /* last emitted length */
  7573. var curlen; /* length of current code */
  7574. var nextlen = tree[0*2 + 1]/*.Len*/; /* length of next code */
  7575. var count = 0; /* repeat count of the current code */
  7576. var max_count = 7; /* max repeat count */
  7577. var min_count = 4; /* min repeat count */
  7578. /* tree[max_code+1].Len = -1; */ /* guard already set */
  7579. if (nextlen === 0) {
  7580. max_count = 138;
  7581. min_count = 3;
  7582. }
  7583. for (n = 0; n <= max_code; n++) {
  7584. curlen = nextlen;
  7585. nextlen = tree[(n+1)*2 + 1]/*.Len*/;
  7586. if (++count < max_count && curlen === nextlen) {
  7587. continue;
  7588. } else if (count < min_count) {
  7589. do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
  7590. } else if (curlen !== 0) {
  7591. if (curlen !== prevlen) {
  7592. send_code(s, curlen, s.bl_tree);
  7593. count--;
  7594. }
  7595. //Assert(count >= 3 && count <= 6, " 3_6?");
  7596. send_code(s, REP_3_6, s.bl_tree);
  7597. send_bits(s, count-3, 2);
  7598. } else if (count <= 10) {
  7599. send_code(s, REPZ_3_10, s.bl_tree);
  7600. send_bits(s, count-3, 3);
  7601. } else {
  7602. send_code(s, REPZ_11_138, s.bl_tree);
  7603. send_bits(s, count-11, 7);
  7604. }
  7605. count = 0;
  7606. prevlen = curlen;
  7607. if (nextlen === 0) {
  7608. max_count = 138;
  7609. min_count = 3;
  7610. } else if (curlen === nextlen) {
  7611. max_count = 6;
  7612. min_count = 3;
  7613. } else {
  7614. max_count = 7;
  7615. min_count = 4;
  7616. }
  7617. }
  7618. }
  7619. /* ===========================================================================
  7620. * Construct the Huffman tree for the bit lengths and return the index in
  7621. * bl_order of the last bit length code to send.
  7622. */
  7623. function build_bl_tree(s) {
  7624. var max_blindex; /* index of last bit length code of non zero freq */
  7625. /* Determine the bit length frequencies for literal and distance trees */
  7626. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  7627. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  7628. /* Build the bit length tree: */
  7629. build_tree(s, s.bl_desc);
  7630. /* opt_len now includes the length of the tree representations, except
  7631. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  7632. */
  7633. /* Determine the number of bit length codes to send. The pkzip format
  7634. * requires that at least 4 bit length codes be sent. (appnote.txt says
  7635. * 3 but the actual value used is 4.)
  7636. */
  7637. for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  7638. if (s.bl_tree[bl_order[max_blindex]*2 + 1]/*.Len*/ !== 0) {
  7639. break;
  7640. }
  7641. }
  7642. /* Update opt_len to include the bit length tree and counts */
  7643. s.opt_len += 3*(max_blindex+1) + 5+5+4;
  7644. //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  7645. // s->opt_len, s->static_len));
  7646. return max_blindex;
  7647. }
  7648. /* ===========================================================================
  7649. * Send the header for a block using dynamic Huffman trees: the counts, the
  7650. * lengths of the bit length codes, the literal tree and the distance tree.
  7651. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  7652. */
  7653. function send_all_trees(s, lcodes, dcodes, blcodes)
  7654. // deflate_state *s;
  7655. // int lcodes, dcodes, blcodes; /* number of codes for each tree */
  7656. {
  7657. var rank; /* index in bl_order */
  7658. //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  7659. //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  7660. // "too many codes");
  7661. //Tracev((stderr, "\nbl counts: "));
  7662. send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
  7663. send_bits(s, dcodes-1, 5);
  7664. send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
  7665. for (rank = 0; rank < blcodes; rank++) {
  7666. //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  7667. send_bits(s, s.bl_tree[bl_order[rank]*2 + 1]/*.Len*/, 3);
  7668. }
  7669. //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  7670. send_tree(s, s.dyn_ltree, lcodes-1); /* literal tree */
  7671. //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  7672. send_tree(s, s.dyn_dtree, dcodes-1); /* distance tree */
  7673. //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  7674. }
  7675. /* ===========================================================================
  7676. * Check if the data type is TEXT or BINARY, using the following algorithm:
  7677. * - TEXT if the two conditions below are satisfied:
  7678. * a) There are no non-portable control characters belonging to the
  7679. * "black list" (0..6, 14..25, 28..31).
  7680. * b) There is at least one printable character belonging to the
  7681. * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
  7682. * - BINARY otherwise.
  7683. * - The following partially-portable control characters form a
  7684. * "gray list" that is ignored in this detection algorithm:
  7685. * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  7686. * IN assertion: the fields Freq of dyn_ltree are set.
  7687. */
  7688. function detect_data_type(s) {
  7689. /* black_mask is the bit mask of black-listed bytes
  7690. * set bits 0..6, 14..25, and 28..31
  7691. * 0xf3ffc07f = binary 11110011111111111100000001111111
  7692. */
  7693. var black_mask = 0xf3ffc07f;
  7694. var n;
  7695. /* Check for non-textual ("black-listed") bytes. */
  7696. for (n = 0; n <= 31; n++, black_mask >>>= 1) {
  7697. if ((black_mask & 1) && (s.dyn_ltree[n*2]/*.Freq*/ !== 0)) {
  7698. return Z_BINARY;
  7699. }
  7700. }
  7701. /* Check for textual ("white-listed") bytes. */
  7702. if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
  7703. s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
  7704. return Z_TEXT;
  7705. }
  7706. for (n = 32; n < LITERALS; n++) {
  7707. if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
  7708. return Z_TEXT;
  7709. }
  7710. }
  7711. /* There are no "black-listed" or "white-listed" bytes:
  7712. * this stream either is empty or has tolerated ("gray-listed") bytes only.
  7713. */
  7714. return Z_BINARY;
  7715. }
  7716. var static_init_done = false;
  7717. /* ===========================================================================
  7718. * Initialize the tree data structures for a new zlib stream.
  7719. */
  7720. function _tr_init(s)
  7721. {
  7722. if (!static_init_done) {
  7723. tr_static_init();
  7724. static_init_done = true;
  7725. }
  7726. s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
  7727. s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
  7728. s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
  7729. s.bi_buf = 0;
  7730. s.bi_valid = 0;
  7731. /* Initialize the first block of the first file: */
  7732. init_block(s);
  7733. }
  7734. /* ===========================================================================
  7735. * Send a stored block
  7736. */
  7737. function _tr_stored_block(s, buf, stored_len, last)
  7738. //DeflateState *s;
  7739. //charf *buf; /* input block */
  7740. //ulg stored_len; /* length of input block */
  7741. //int last; /* one if this is the last block for a file */
  7742. {
  7743. send_bits(s, (STORED_BLOCK<<1)+(last ? 1 : 0), 3); /* send block type */
  7744. copy_block(s, buf, stored_len, true); /* with header */
  7745. }
  7746. /* ===========================================================================
  7747. * Send one empty static block to give enough lookahead for inflate.
  7748. * This takes 10 bits, of which 7 may remain in the bit buffer.
  7749. */
  7750. function _tr_align(s) {
  7751. send_bits(s, STATIC_TREES<<1, 3);
  7752. send_code(s, END_BLOCK, static_ltree);
  7753. bi_flush(s);
  7754. }
  7755. /* ===========================================================================
  7756. * Determine the best encoding for the current block: dynamic trees, static
  7757. * trees or store, and output the encoded block to the zip file.
  7758. */
  7759. function _tr_flush_block(s, buf, stored_len, last)
  7760. //DeflateState *s;
  7761. //charf *buf; /* input block, or NULL if too old */
  7762. //ulg stored_len; /* length of input block */
  7763. //int last; /* one if this is the last block for a file */
  7764. {
  7765. var opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  7766. var max_blindex = 0; /* index of last bit length code of non zero freq */
  7767. /* Build the Huffman trees unless a stored block is forced */
  7768. if (s.level > 0) {
  7769. /* Check if the file is binary or text */
  7770. if (s.strm.data_type === Z_UNKNOWN) {
  7771. s.strm.data_type = detect_data_type(s);
  7772. }
  7773. /* Construct the literal and distance trees */
  7774. build_tree(s, s.l_desc);
  7775. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  7776. // s->static_len));
  7777. build_tree(s, s.d_desc);
  7778. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  7779. // s->static_len));
  7780. /* At this point, opt_len and static_len are the total bit lengths of
  7781. * the compressed block data, excluding the tree representations.
  7782. */
  7783. /* Build the bit length tree for the above two trees, and get the index
  7784. * in bl_order of the last bit length code to send.
  7785. */
  7786. max_blindex = build_bl_tree(s);
  7787. /* Determine the best encoding. Compute the block lengths in bytes. */
  7788. opt_lenb = (s.opt_len+3+7) >>> 3;
  7789. static_lenb = (s.static_len+3+7) >>> 3;
  7790. // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  7791. // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  7792. // s->last_lit));
  7793. if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
  7794. } else {
  7795. // Assert(buf != (char*)0, "lost buf");
  7796. opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  7797. }
  7798. if ((stored_len+4 <= opt_lenb) && (buf !== -1)) {
  7799. /* 4: two words for the lengths */
  7800. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  7801. * Otherwise we can't have processed more than WSIZE input bytes since
  7802. * the last block flush, because compression would have been
  7803. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  7804. * transform a block into a stored block.
  7805. */
  7806. _tr_stored_block(s, buf, stored_len, last);
  7807. } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
  7808. send_bits(s, (STATIC_TREES<<1) + (last ? 1 : 0), 3);
  7809. compress_block(s, static_ltree, static_dtree);
  7810. } else {
  7811. send_bits(s, (DYN_TREES<<1) + (last ? 1 : 0), 3);
  7812. send_all_trees(s, s.l_desc.max_code+1, s.d_desc.max_code+1, max_blindex+1);
  7813. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  7814. }
  7815. // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  7816. /* The above check is made mod 2^32, for files larger than 512 MB
  7817. * and uLong implemented on 32 bits.
  7818. */
  7819. init_block(s);
  7820. if (last) {
  7821. bi_windup(s);
  7822. }
  7823. // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  7824. // s->compressed_len-7*last));
  7825. }
  7826. /* ===========================================================================
  7827. * Save the match info and tally the frequency counts. Return true if
  7828. * the current block must be flushed.
  7829. */
  7830. function _tr_tally(s, dist, lc)
  7831. // deflate_state *s;
  7832. // unsigned dist; /* distance of matched string */
  7833. // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  7834. {
  7835. //var out_length, in_length, dcode;
  7836. s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff;
  7837. s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
  7838. s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
  7839. s.last_lit++;
  7840. if (dist === 0) {
  7841. /* lc is the unmatched char */
  7842. s.dyn_ltree[lc*2]/*.Freq*/++;
  7843. } else {
  7844. s.matches++;
  7845. /* Here, lc is the match length - MIN_MATCH */
  7846. dist--; /* dist = match distance - 1 */
  7847. //Assert((ush)dist < (ush)MAX_DIST(s) &&
  7848. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  7849. // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  7850. s.dyn_ltree[(_length_code[lc]+LITERALS+1) * 2]/*.Freq*/++;
  7851. s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
  7852. }
  7853. // (!) This block is disabled in zlib defailts,
  7854. // don't enable it for binary compatibility
  7855. //#ifdef TRUNCATE_BLOCK
  7856. // /* Try to guess if it is profitable to stop the current block here */
  7857. // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
  7858. // /* Compute an upper bound for the compressed length */
  7859. // out_length = s.last_lit*8;
  7860. // in_length = s.strstart - s.block_start;
  7861. //
  7862. // for (dcode = 0; dcode < D_CODES; dcode++) {
  7863. // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
  7864. // }
  7865. // out_length >>>= 3;
  7866. // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  7867. // // s->last_lit, in_length, out_length,
  7868. // // 100L - out_length*100L/in_length));
  7869. // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
  7870. // return true;
  7871. // }
  7872. // }
  7873. //#endif
  7874. return (s.last_lit === s.lit_bufsize-1);
  7875. /* We avoid equality with lit_bufsize because of wraparound at 64K
  7876. * on 16 bit machines and because stored blocks are restricted to
  7877. * 64K-1 bytes.
  7878. */
  7879. }
  7880. exports._tr_init = _tr_init;
  7881. exports._tr_stored_block = _tr_stored_block;
  7882. exports._tr_flush_block = _tr_flush_block;
  7883. exports._tr_tally = _tr_tally;
  7884. exports._tr_align = _tr_align;
  7885. },{"../utils/common":27}],39:[function(_dereq_,module,exports){
  7886. 'use strict';
  7887. function ZStream() {
  7888. /* next input byte */
  7889. this.input = null; // JS specific, because we have no pointers
  7890. this.next_in = 0;
  7891. /* number of bytes available at input */
  7892. this.avail_in = 0;
  7893. /* total number of input bytes read so far */
  7894. this.total_in = 0;
  7895. /* next output byte should be put there */
  7896. this.output = null; // JS specific, because we have no pointers
  7897. this.next_out = 0;
  7898. /* remaining free space at output */
  7899. this.avail_out = 0;
  7900. /* total number of bytes output so far */
  7901. this.total_out = 0;
  7902. /* last error message, NULL if no error */
  7903. this.msg = ''/*Z_NULL*/;
  7904. /* not visible by applications */
  7905. this.state = null;
  7906. /* best guess about the data type: binary or text */
  7907. this.data_type = 2/*Z_UNKNOWN*/;
  7908. /* adler32 value of the uncompressed data */
  7909. this.adler = 0;
  7910. }
  7911. module.exports = ZStream;
  7912. },{}]},{},[9])
  7913. (9)
  7914. }));