jszip.js 281 KB

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