jszip.js 281 KB

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