jszip.js 287 KB

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