jszip.js 295 KB

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