jszip.js 281 KB

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