pako.js 216 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818
  1. /* pako 1.0.11 nodeca/pako */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pako = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. 'use strict';
  3. var zlib_deflate = require('./zlib/deflate');
  4. var utils = require('./utils/common');
  5. var strings = require('./utils/strings');
  6. var msg = require('./zlib/messages');
  7. var ZStream = require('./zlib/zstream');
  8. var toString = Object.prototype.toString;
  9. /* Public constants ==========================================================*/
  10. /* ===========================================================================*/
  11. var Z_NO_FLUSH = 0;
  12. var Z_FINISH = 4;
  13. var Z_OK = 0;
  14. var Z_STREAM_END = 1;
  15. var Z_SYNC_FLUSH = 2;
  16. var Z_DEFAULT_COMPRESSION = -1;
  17. var Z_DEFAULT_STRATEGY = 0;
  18. var Z_DEFLATED = 8;
  19. /* ===========================================================================*/
  20. /**
  21. * class Deflate
  22. *
  23. * Generic JS-style wrapper for zlib calls. If you don't need
  24. * streaming behaviour - use more simple functions: [[deflate]],
  25. * [[deflateRaw]] and [[gzip]].
  26. **/
  27. /* internal
  28. * Deflate.chunks -> Array
  29. *
  30. * Chunks of output data, if [[Deflate#onData]] not overridden.
  31. **/
  32. /**
  33. * Deflate.result -> Uint8Array|Array
  34. *
  35. * Compressed result, generated by default [[Deflate#onData]]
  36. * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
  37. * (call [[Deflate#push]] with `Z_FINISH` / `true` param) or if you
  38. * push a chunk with explicit flush (call [[Deflate#push]] with
  39. * `Z_SYNC_FLUSH` param).
  40. **/
  41. /**
  42. * Deflate.err -> Number
  43. *
  44. * Error code after deflate finished. 0 (Z_OK) on success.
  45. * You will not need it in real life, because deflate errors
  46. * are possible only on wrong options or bad `onData` / `onEnd`
  47. * custom handlers.
  48. **/
  49. /**
  50. * Deflate.msg -> String
  51. *
  52. * Error message, if [[Deflate.err]] != 0
  53. **/
  54. /**
  55. * new Deflate(options)
  56. * - options (Object): zlib deflate options.
  57. *
  58. * Creates new deflator instance with specified params. Throws exception
  59. * on bad params. Supported options:
  60. *
  61. * - `level`
  62. * - `windowBits`
  63. * - `memLevel`
  64. * - `strategy`
  65. * - `dictionary`
  66. *
  67. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  68. * for more information on these.
  69. *
  70. * Additional options, for internal needs:
  71. *
  72. * - `chunkSize` - size of generated data chunks (16K by default)
  73. * - `raw` (Boolean) - do raw deflate
  74. * - `gzip` (Boolean) - create gzip wrapper
  75. * - `to` (String) - if equal to 'string', then result will be "binary string"
  76. * (each char code [0..255])
  77. * - `header` (Object) - custom header for gzip
  78. * - `text` (Boolean) - true if compressed data believed to be text
  79. * - `time` (Number) - modification time, unix timestamp
  80. * - `os` (Number) - operation system code
  81. * - `extra` (Array) - array of bytes with extra data (max 65536)
  82. * - `name` (String) - file name (binary string)
  83. * - `comment` (String) - comment (binary string)
  84. * - `hcrc` (Boolean) - true if header crc should be added
  85. *
  86. * ##### Example:
  87. *
  88. * ```javascript
  89. * var pako = require('pako')
  90. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  91. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  92. *
  93. * var deflate = new pako.Deflate({ level: 3});
  94. *
  95. * deflate.push(chunk1, false);
  96. * deflate.push(chunk2, true); // true -> last chunk
  97. *
  98. * if (deflate.err) { throw new Error(deflate.err); }
  99. *
  100. * console.log(deflate.result);
  101. * ```
  102. **/
  103. function Deflate(options) {
  104. if (!(this instanceof Deflate)) return new Deflate(options);
  105. this.options = utils.assign({
  106. level: Z_DEFAULT_COMPRESSION,
  107. method: Z_DEFLATED,
  108. chunkSize: 16384,
  109. windowBits: 15,
  110. memLevel: 8,
  111. strategy: Z_DEFAULT_STRATEGY,
  112. to: ''
  113. }, options || {});
  114. var opt = this.options;
  115. if (opt.raw && (opt.windowBits > 0)) {
  116. opt.windowBits = -opt.windowBits;
  117. }
  118. else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
  119. opt.windowBits += 16;
  120. }
  121. this.err = 0; // error code, if happens (0 = Z_OK)
  122. this.msg = ''; // error message
  123. this.ended = false; // used to avoid multiple onEnd() calls
  124. this.chunks = []; // chunks of compressed data
  125. this.strm = new ZStream();
  126. this.strm.avail_out = 0;
  127. var status = zlib_deflate.deflateInit2(
  128. this.strm,
  129. opt.level,
  130. opt.method,
  131. opt.windowBits,
  132. opt.memLevel,
  133. opt.strategy
  134. );
  135. if (status !== Z_OK) {
  136. throw new Error(msg[status]);
  137. }
  138. if (opt.header) {
  139. zlib_deflate.deflateSetHeader(this.strm, opt.header);
  140. }
  141. if (opt.dictionary) {
  142. var dict;
  143. // Convert data if needed
  144. if (typeof opt.dictionary === 'string') {
  145. // If we need to compress text, change encoding to utf8.
  146. dict = strings.string2buf(opt.dictionary);
  147. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  148. dict = new Uint8Array(opt.dictionary);
  149. } else {
  150. dict = opt.dictionary;
  151. }
  152. status = zlib_deflate.deflateSetDictionary(this.strm, dict);
  153. if (status !== Z_OK) {
  154. throw new Error(msg[status]);
  155. }
  156. this._dict_set = true;
  157. }
  158. }
  159. /**
  160. * Deflate#push(data[, mode]) -> Boolean
  161. * - data (Uint8Array|Array|ArrayBuffer|String): input data. Strings will be
  162. * converted to utf8 byte sequence.
  163. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  164. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
  165. *
  166. * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
  167. * new compressed chunks. Returns `true` on success. The last data block must have
  168. * mode Z_FINISH (or `true`). That will flush internal pending buffers and call
  169. * [[Deflate#onEnd]]. For interim explicit flushes (without ending the stream) you
  170. * can use mode Z_SYNC_FLUSH, keeping the compression context.
  171. *
  172. * On fail call [[Deflate#onEnd]] with error code and return false.
  173. *
  174. * We strongly recommend to use `Uint8Array` on input for best speed (output
  175. * array format is detected automatically). Also, don't skip last param and always
  176. * use the same type in your code (boolean or number). That will improve JS speed.
  177. *
  178. * For regular `Array`-s make sure all elements are [0..255].
  179. *
  180. * ##### Example
  181. *
  182. * ```javascript
  183. * push(chunk, false); // push one of data chunks
  184. * ...
  185. * push(chunk, true); // push last chunk
  186. * ```
  187. **/
  188. Deflate.prototype.push = function (data, mode) {
  189. var strm = this.strm;
  190. var chunkSize = this.options.chunkSize;
  191. var status, _mode;
  192. if (this.ended) { return false; }
  193. _mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
  194. // Convert data if needed
  195. if (typeof data === 'string') {
  196. // If we need to compress text, change encoding to utf8.
  197. strm.input = strings.string2buf(data);
  198. } else if (toString.call(data) === '[object ArrayBuffer]') {
  199. strm.input = new Uint8Array(data);
  200. } else {
  201. strm.input = data;
  202. }
  203. strm.next_in = 0;
  204. strm.avail_in = strm.input.length;
  205. do {
  206. if (strm.avail_out === 0) {
  207. strm.output = new utils.Buf8(chunkSize);
  208. strm.next_out = 0;
  209. strm.avail_out = chunkSize;
  210. }
  211. status = zlib_deflate.deflate(strm, _mode); /* no bad return value */
  212. if (status !== Z_STREAM_END && status !== Z_OK) {
  213. this.onEnd(status);
  214. this.ended = true;
  215. return false;
  216. }
  217. if (strm.avail_out === 0 || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) {
  218. if (this.options.to === 'string') {
  219. this.onData(strings.buf2binstring(utils.shrinkBuf(strm.output, strm.next_out)));
  220. } else {
  221. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  222. }
  223. }
  224. } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END);
  225. // Finalize on the last chunk.
  226. if (_mode === Z_FINISH) {
  227. status = zlib_deflate.deflateEnd(this.strm);
  228. this.onEnd(status);
  229. this.ended = true;
  230. return status === Z_OK;
  231. }
  232. // callback interim results if Z_SYNC_FLUSH.
  233. if (_mode === Z_SYNC_FLUSH) {
  234. this.onEnd(Z_OK);
  235. strm.avail_out = 0;
  236. return true;
  237. }
  238. return true;
  239. };
  240. /**
  241. * Deflate#onData(chunk) -> Void
  242. * - chunk (Uint8Array|Array|String): output data. Type of array depends
  243. * on js engine support. When string output requested, each chunk
  244. * will be string.
  245. *
  246. * By default, stores data blocks in `chunks[]` property and glue
  247. * those in `onEnd`. Override this handler, if you need another behaviour.
  248. **/
  249. Deflate.prototype.onData = function (chunk) {
  250. this.chunks.push(chunk);
  251. };
  252. /**
  253. * Deflate#onEnd(status) -> Void
  254. * - status (Number): deflate status. 0 (Z_OK) on success,
  255. * other if not.
  256. *
  257. * Called once after you tell deflate that the input stream is
  258. * complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
  259. * or if an error happened. By default - join collected chunks,
  260. * free memory and fill `results` / `err` properties.
  261. **/
  262. Deflate.prototype.onEnd = function (status) {
  263. // On success - join
  264. if (status === Z_OK) {
  265. if (this.options.to === 'string') {
  266. this.result = this.chunks.join('');
  267. } else {
  268. this.result = utils.flattenChunks(this.chunks);
  269. }
  270. }
  271. this.chunks = [];
  272. this.err = status;
  273. this.msg = this.strm.msg;
  274. };
  275. /**
  276. * deflate(data[, options]) -> Uint8Array|Array|String
  277. * - data (Uint8Array|Array|String): input data to compress.
  278. * - options (Object): zlib deflate options.
  279. *
  280. * Compress `data` with deflate algorithm and `options`.
  281. *
  282. * Supported options are:
  283. *
  284. * - level
  285. * - windowBits
  286. * - memLevel
  287. * - strategy
  288. * - dictionary
  289. *
  290. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  291. * for more information on these.
  292. *
  293. * Sugar (options):
  294. *
  295. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  296. * negative windowBits implicitly.
  297. * - `to` (String) - if equal to 'string', then result will be "binary string"
  298. * (each char code [0..255])
  299. *
  300. * ##### Example:
  301. *
  302. * ```javascript
  303. * var pako = require('pako')
  304. * , data = Uint8Array([1,2,3,4,5,6,7,8,9]);
  305. *
  306. * console.log(pako.deflate(data));
  307. * ```
  308. **/
  309. function deflate(input, options) {
  310. var deflator = new Deflate(options);
  311. deflator.push(input, true);
  312. // That will never happens, if you don't cheat with options :)
  313. if (deflator.err) { throw deflator.msg || msg[deflator.err]; }
  314. return deflator.result;
  315. }
  316. /**
  317. * deflateRaw(data[, options]) -> Uint8Array|Array|String
  318. * - data (Uint8Array|Array|String): input data to compress.
  319. * - options (Object): zlib deflate options.
  320. *
  321. * The same as [[deflate]], but creates raw data, without wrapper
  322. * (header and adler32 crc).
  323. **/
  324. function deflateRaw(input, options) {
  325. options = options || {};
  326. options.raw = true;
  327. return deflate(input, options);
  328. }
  329. /**
  330. * gzip(data[, options]) -> Uint8Array|Array|String
  331. * - data (Uint8Array|Array|String): input data to compress.
  332. * - options (Object): zlib deflate options.
  333. *
  334. * The same as [[deflate]], but create gzip wrapper instead of
  335. * deflate one.
  336. **/
  337. function gzip(input, options) {
  338. options = options || {};
  339. options.gzip = true;
  340. return deflate(input, options);
  341. }
  342. exports.Deflate = Deflate;
  343. exports.deflate = deflate;
  344. exports.deflateRaw = deflateRaw;
  345. exports.gzip = gzip;
  346. },{"./utils/common":3,"./utils/strings":4,"./zlib/deflate":8,"./zlib/messages":13,"./zlib/zstream":15}],2:[function(require,module,exports){
  347. 'use strict';
  348. var zlib_inflate = require('./zlib/inflate');
  349. var utils = require('./utils/common');
  350. var strings = require('./utils/strings');
  351. var c = require('./zlib/constants');
  352. var msg = require('./zlib/messages');
  353. var ZStream = require('./zlib/zstream');
  354. var GZheader = require('./zlib/gzheader');
  355. var toString = Object.prototype.toString;
  356. /**
  357. * class Inflate
  358. *
  359. * Generic JS-style wrapper for zlib calls. If you don't need
  360. * streaming behaviour - use more simple functions: [[inflate]]
  361. * and [[inflateRaw]].
  362. **/
  363. /* internal
  364. * inflate.chunks -> Array
  365. *
  366. * Chunks of output data, if [[Inflate#onData]] not overridden.
  367. **/
  368. /**
  369. * Inflate.result -> Uint8Array|Array|String
  370. *
  371. * Uncompressed result, generated by default [[Inflate#onData]]
  372. * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
  373. * (call [[Inflate#push]] with `Z_FINISH` / `true` param) or if you
  374. * push a chunk with explicit flush (call [[Inflate#push]] with
  375. * `Z_SYNC_FLUSH` param).
  376. **/
  377. /**
  378. * Inflate.err -> Number
  379. *
  380. * Error code after inflate finished. 0 (Z_OK) on success.
  381. * Should be checked if broken data possible.
  382. **/
  383. /**
  384. * Inflate.msg -> String
  385. *
  386. * Error message, if [[Inflate.err]] != 0
  387. **/
  388. /**
  389. * new Inflate(options)
  390. * - options (Object): zlib inflate options.
  391. *
  392. * Creates new inflator instance with specified params. Throws exception
  393. * on bad params. Supported options:
  394. *
  395. * - `windowBits`
  396. * - `dictionary`
  397. *
  398. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  399. * for more information on these.
  400. *
  401. * Additional options, for internal needs:
  402. *
  403. * - `chunkSize` - size of generated data chunks (16K by default)
  404. * - `raw` (Boolean) - do raw inflate
  405. * - `to` (String) - if equal to 'string', then result will be converted
  406. * from utf8 to utf16 (javascript) string. When string output requested,
  407. * chunk length can differ from `chunkSize`, depending on content.
  408. *
  409. * By default, when no options set, autodetect deflate/gzip data format via
  410. * wrapper header.
  411. *
  412. * ##### Example:
  413. *
  414. * ```javascript
  415. * var pako = require('pako')
  416. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  417. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  418. *
  419. * var inflate = new pako.Inflate({ level: 3});
  420. *
  421. * inflate.push(chunk1, false);
  422. * inflate.push(chunk2, true); // true -> last chunk
  423. *
  424. * if (inflate.err) { throw new Error(inflate.err); }
  425. *
  426. * console.log(inflate.result);
  427. * ```
  428. **/
  429. function Inflate(options) {
  430. if (!(this instanceof Inflate)) return new Inflate(options);
  431. this.options = utils.assign({
  432. chunkSize: 16384,
  433. windowBits: 0,
  434. to: ''
  435. }, options || {});
  436. var opt = this.options;
  437. // Force window size for `raw` data, if not set directly,
  438. // because we have no header for autodetect.
  439. if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
  440. opt.windowBits = -opt.windowBits;
  441. if (opt.windowBits === 0) { opt.windowBits = -15; }
  442. }
  443. // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
  444. if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
  445. !(options && options.windowBits)) {
  446. opt.windowBits += 32;
  447. }
  448. // Gzip header has no info about windows size, we can do autodetect only
  449. // for deflate. So, if window size not set, force it to max when gzip possible
  450. if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
  451. // bit 3 (16) -> gzipped data
  452. // bit 4 (32) -> autodetect gzip/deflate
  453. if ((opt.windowBits & 15) === 0) {
  454. opt.windowBits |= 15;
  455. }
  456. }
  457. this.err = 0; // error code, if happens (0 = Z_OK)
  458. this.msg = ''; // error message
  459. this.ended = false; // used to avoid multiple onEnd() calls
  460. this.chunks = []; // chunks of compressed data
  461. this.strm = new ZStream();
  462. this.strm.avail_out = 0;
  463. var status = zlib_inflate.inflateInit2(
  464. this.strm,
  465. opt.windowBits
  466. );
  467. if (status !== c.Z_OK) {
  468. throw new Error(msg[status]);
  469. }
  470. this.header = new GZheader();
  471. zlib_inflate.inflateGetHeader(this.strm, this.header);
  472. // Setup dictionary
  473. if (opt.dictionary) {
  474. // Convert data if needed
  475. if (typeof opt.dictionary === 'string') {
  476. opt.dictionary = strings.string2buf(opt.dictionary);
  477. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  478. opt.dictionary = new Uint8Array(opt.dictionary);
  479. }
  480. if (opt.raw) { //In raw mode we need to set the dictionary early
  481. status = zlib_inflate.inflateSetDictionary(this.strm, opt.dictionary);
  482. if (status !== c.Z_OK) {
  483. throw new Error(msg[status]);
  484. }
  485. }
  486. }
  487. }
  488. /**
  489. * Inflate#push(data[, mode]) -> Boolean
  490. * - data (Uint8Array|Array|ArrayBuffer|String): input data
  491. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  492. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
  493. *
  494. * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
  495. * new output chunks. Returns `true` on success. The last data block must have
  496. * mode Z_FINISH (or `true`). That will flush internal pending buffers and call
  497. * [[Inflate#onEnd]]. For interim explicit flushes (without ending the stream) you
  498. * can use mode Z_SYNC_FLUSH, keeping the decompression context.
  499. *
  500. * On fail call [[Inflate#onEnd]] with error code and return false.
  501. *
  502. * We strongly recommend to use `Uint8Array` on input for best speed (output
  503. * format is detected automatically). Also, don't skip last param and always
  504. * use the same type in your code (boolean or number). That will improve JS speed.
  505. *
  506. * For regular `Array`-s make sure all elements are [0..255].
  507. *
  508. * ##### Example
  509. *
  510. * ```javascript
  511. * push(chunk, false); // push one of data chunks
  512. * ...
  513. * push(chunk, true); // push last chunk
  514. * ```
  515. **/
  516. Inflate.prototype.push = function (data, mode) {
  517. var strm = this.strm;
  518. var chunkSize = this.options.chunkSize;
  519. var dictionary = this.options.dictionary;
  520. var status, _mode;
  521. var next_out_utf8, tail, utf8str;
  522. // Flag to properly process Z_BUF_ERROR on testing inflate call
  523. // when we check that all output data was flushed.
  524. var allowBufError = false;
  525. if (this.ended) { return false; }
  526. _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
  527. // Convert data if needed
  528. if (typeof data === 'string') {
  529. // Only binary strings can be decompressed on practice
  530. strm.input = strings.binstring2buf(data);
  531. } else if (toString.call(data) === '[object ArrayBuffer]') {
  532. strm.input = new Uint8Array(data);
  533. } else {
  534. strm.input = data;
  535. }
  536. strm.next_in = 0;
  537. strm.avail_in = strm.input.length;
  538. do {
  539. if (strm.avail_out === 0) {
  540. strm.output = new utils.Buf8(chunkSize);
  541. strm.next_out = 0;
  542. strm.avail_out = chunkSize;
  543. }
  544. status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
  545. if (status === c.Z_NEED_DICT && dictionary) {
  546. status = zlib_inflate.inflateSetDictionary(this.strm, dictionary);
  547. }
  548. if (status === c.Z_BUF_ERROR && allowBufError === true) {
  549. status = c.Z_OK;
  550. allowBufError = false;
  551. }
  552. if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
  553. this.onEnd(status);
  554. this.ended = true;
  555. return false;
  556. }
  557. if (strm.next_out) {
  558. if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && (_mode === c.Z_FINISH || _mode === c.Z_SYNC_FLUSH))) {
  559. if (this.options.to === 'string') {
  560. next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
  561. tail = strm.next_out - next_out_utf8;
  562. utf8str = strings.buf2string(strm.output, next_out_utf8);
  563. // move tail
  564. strm.next_out = tail;
  565. strm.avail_out = chunkSize - tail;
  566. if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
  567. this.onData(utf8str);
  568. } else {
  569. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  570. }
  571. }
  572. }
  573. // When no more input data, we should check that internal inflate buffers
  574. // are flushed. The only way to do it when avail_out = 0 - run one more
  575. // inflate pass. But if output data not exists, inflate return Z_BUF_ERROR.
  576. // Here we set flag to process this error properly.
  577. //
  578. // NOTE. Deflate does not return error in this case and does not needs such
  579. // logic.
  580. if (strm.avail_in === 0 && strm.avail_out === 0) {
  581. allowBufError = true;
  582. }
  583. } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== c.Z_STREAM_END);
  584. if (status === c.Z_STREAM_END) {
  585. _mode = c.Z_FINISH;
  586. }
  587. // Finalize on the last chunk.
  588. if (_mode === c.Z_FINISH) {
  589. status = zlib_inflate.inflateEnd(this.strm);
  590. this.onEnd(status);
  591. this.ended = true;
  592. return status === c.Z_OK;
  593. }
  594. // callback interim results if Z_SYNC_FLUSH.
  595. if (_mode === c.Z_SYNC_FLUSH) {
  596. this.onEnd(c.Z_OK);
  597. strm.avail_out = 0;
  598. return true;
  599. }
  600. return true;
  601. };
  602. /**
  603. * Inflate#onData(chunk) -> Void
  604. * - chunk (Uint8Array|Array|String): output data. Type of array depends
  605. * on js engine support. When string output requested, each chunk
  606. * will be string.
  607. *
  608. * By default, stores data blocks in `chunks[]` property and glue
  609. * those in `onEnd`. Override this handler, if you need another behaviour.
  610. **/
  611. Inflate.prototype.onData = function (chunk) {
  612. this.chunks.push(chunk);
  613. };
  614. /**
  615. * Inflate#onEnd(status) -> Void
  616. * - status (Number): inflate status. 0 (Z_OK) on success,
  617. * other if not.
  618. *
  619. * Called either after you tell inflate that the input stream is
  620. * complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
  621. * or if an error happened. By default - join collected chunks,
  622. * free memory and fill `results` / `err` properties.
  623. **/
  624. Inflate.prototype.onEnd = function (status) {
  625. // On success - join
  626. if (status === c.Z_OK) {
  627. if (this.options.to === 'string') {
  628. // Glue & convert here, until we teach pako to send
  629. // utf8 aligned strings to onData
  630. this.result = this.chunks.join('');
  631. } else {
  632. this.result = utils.flattenChunks(this.chunks);
  633. }
  634. }
  635. this.chunks = [];
  636. this.err = status;
  637. this.msg = this.strm.msg;
  638. };
  639. /**
  640. * inflate(data[, options]) -> Uint8Array|Array|String
  641. * - data (Uint8Array|Array|String): input data to decompress.
  642. * - options (Object): zlib inflate options.
  643. *
  644. * Decompress `data` with inflate/ungzip and `options`. Autodetect
  645. * format via wrapper header by default. That's why we don't provide
  646. * separate `ungzip` method.
  647. *
  648. * Supported options are:
  649. *
  650. * - windowBits
  651. *
  652. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  653. * for more information.
  654. *
  655. * Sugar (options):
  656. *
  657. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  658. * negative windowBits implicitly.
  659. * - `to` (String) - if equal to 'string', then result will be converted
  660. * from utf8 to utf16 (javascript) string. When string output requested,
  661. * chunk length can differ from `chunkSize`, depending on content.
  662. *
  663. *
  664. * ##### Example:
  665. *
  666. * ```javascript
  667. * var pako = require('pako')
  668. * , input = pako.deflate([1,2,3,4,5,6,7,8,9])
  669. * , output;
  670. *
  671. * try {
  672. * output = pako.inflate(input);
  673. * } catch (err)
  674. * console.log(err);
  675. * }
  676. * ```
  677. **/
  678. function inflate(input, options) {
  679. var inflator = new Inflate(options);
  680. inflator.push(input, true);
  681. // That will never happens, if you don't cheat with options :)
  682. if (inflator.err) { throw inflator.msg || msg[inflator.err]; }
  683. return inflator.result;
  684. }
  685. /**
  686. * inflateRaw(data[, options]) -> Uint8Array|Array|String
  687. * - data (Uint8Array|Array|String): input data to decompress.
  688. * - options (Object): zlib inflate options.
  689. *
  690. * The same as [[inflate]], but creates raw data, without wrapper
  691. * (header and adler32 crc).
  692. **/
  693. function inflateRaw(input, options) {
  694. options = options || {};
  695. options.raw = true;
  696. return inflate(input, options);
  697. }
  698. /**
  699. * ungzip(data[, options]) -> Uint8Array|Array|String
  700. * - data (Uint8Array|Array|String): input data to decompress.
  701. * - options (Object): zlib inflate options.
  702. *
  703. * Just shortcut to [[inflate]], because it autodetects format
  704. * by header.content. Done for convenience.
  705. **/
  706. exports.Inflate = Inflate;
  707. exports.inflate = inflate;
  708. exports.inflateRaw = inflateRaw;
  709. exports.ungzip = inflate;
  710. },{"./utils/common":3,"./utils/strings":4,"./zlib/constants":6,"./zlib/gzheader":9,"./zlib/inflate":11,"./zlib/messages":13,"./zlib/zstream":15}],3:[function(require,module,exports){
  711. 'use strict';
  712. var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
  713. (typeof Uint16Array !== 'undefined') &&
  714. (typeof Int32Array !== 'undefined');
  715. function _has(obj, key) {
  716. return Object.prototype.hasOwnProperty.call(obj, key);
  717. }
  718. exports.assign = function (obj /*from1, from2, from3, ...*/) {
  719. var sources = Array.prototype.slice.call(arguments, 1);
  720. while (sources.length) {
  721. var source = sources.shift();
  722. if (!source) { continue; }
  723. if (typeof source !== 'object') {
  724. throw new TypeError(source + 'must be non-object');
  725. }
  726. for (var p in source) {
  727. if (_has(source, p)) {
  728. obj[p] = source[p];
  729. }
  730. }
  731. }
  732. return obj;
  733. };
  734. // reduce buffer size, avoiding mem copy
  735. exports.shrinkBuf = function (buf, size) {
  736. if (buf.length === size) { return buf; }
  737. if (buf.subarray) { return buf.subarray(0, size); }
  738. buf.length = size;
  739. return buf;
  740. };
  741. var fnTyped = {
  742. arraySet: function (dest, src, src_offs, len, dest_offs) {
  743. if (src.subarray && dest.subarray) {
  744. dest.set(src.subarray(src_offs, src_offs + len), dest_offs);
  745. return;
  746. }
  747. // Fallback to ordinary array
  748. for (var i = 0; i < len; i++) {
  749. dest[dest_offs + i] = src[src_offs + i];
  750. }
  751. },
  752. // Join array of chunks to single array.
  753. flattenChunks: function (chunks) {
  754. var i, l, len, pos, chunk, result;
  755. // calculate data length
  756. len = 0;
  757. for (i = 0, l = chunks.length; i < l; i++) {
  758. len += chunks[i].length;
  759. }
  760. // join chunks
  761. result = new Uint8Array(len);
  762. pos = 0;
  763. for (i = 0, l = chunks.length; i < l; i++) {
  764. chunk = chunks[i];
  765. result.set(chunk, pos);
  766. pos += chunk.length;
  767. }
  768. return result;
  769. }
  770. };
  771. var fnUntyped = {
  772. arraySet: function (dest, src, src_offs, len, dest_offs) {
  773. for (var i = 0; i < len; i++) {
  774. dest[dest_offs + i] = src[src_offs + i];
  775. }
  776. },
  777. // Join array of chunks to single array.
  778. flattenChunks: function (chunks) {
  779. return [].concat.apply([], chunks);
  780. }
  781. };
  782. // Enable/Disable typed arrays use, for testing
  783. //
  784. exports.setTyped = function (on) {
  785. if (on) {
  786. exports.Buf8 = Uint8Array;
  787. exports.Buf16 = Uint16Array;
  788. exports.Buf32 = Int32Array;
  789. exports.assign(exports, fnTyped);
  790. } else {
  791. exports.Buf8 = Array;
  792. exports.Buf16 = Array;
  793. exports.Buf32 = Array;
  794. exports.assign(exports, fnUntyped);
  795. }
  796. };
  797. exports.setTyped(TYPED_OK);
  798. },{}],4:[function(require,module,exports){
  799. // String encode/decode helpers
  800. 'use strict';
  801. var utils = require('./common');
  802. // Quick check if we can use fast array to bin string conversion
  803. //
  804. // - apply(Array) can fail on Android 2.2
  805. // - apply(Uint8Array) can fail on iOS 5.1 Safari
  806. //
  807. var STR_APPLY_OK = true;
  808. var STR_APPLY_UIA_OK = true;
  809. try { String.fromCharCode.apply(null, [ 0 ]); } catch (__) { STR_APPLY_OK = false; }
  810. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
  811. // Table with utf8 lengths (calculated by first byte of sequence)
  812. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  813. // because max possible codepoint is 0x10ffff
  814. var _utf8len = new utils.Buf8(256);
  815. for (var q = 0; q < 256; q++) {
  816. _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
  817. }
  818. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  819. // convert string to array (typed, when possible)
  820. exports.string2buf = function (str) {
  821. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  822. // count binary size
  823. for (m_pos = 0; m_pos < str_len; m_pos++) {
  824. c = str.charCodeAt(m_pos);
  825. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  826. c2 = str.charCodeAt(m_pos + 1);
  827. if ((c2 & 0xfc00) === 0xdc00) {
  828. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  829. m_pos++;
  830. }
  831. }
  832. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  833. }
  834. // allocate buffer
  835. buf = new utils.Buf8(buf_len);
  836. // convert
  837. for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
  838. c = str.charCodeAt(m_pos);
  839. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  840. c2 = str.charCodeAt(m_pos + 1);
  841. if ((c2 & 0xfc00) === 0xdc00) {
  842. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  843. m_pos++;
  844. }
  845. }
  846. if (c < 0x80) {
  847. /* one byte */
  848. buf[i++] = c;
  849. } else if (c < 0x800) {
  850. /* two bytes */
  851. buf[i++] = 0xC0 | (c >>> 6);
  852. buf[i++] = 0x80 | (c & 0x3f);
  853. } else if (c < 0x10000) {
  854. /* three bytes */
  855. buf[i++] = 0xE0 | (c >>> 12);
  856. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  857. buf[i++] = 0x80 | (c & 0x3f);
  858. } else {
  859. /* four bytes */
  860. buf[i++] = 0xf0 | (c >>> 18);
  861. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  862. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  863. buf[i++] = 0x80 | (c & 0x3f);
  864. }
  865. }
  866. return buf;
  867. };
  868. // Helper (used in 2 places)
  869. function buf2binstring(buf, len) {
  870. // On Chrome, the arguments in a function call that are allowed is `65534`.
  871. // If the length of the buffer is smaller than that, we can use this optimization,
  872. // otherwise we will take a slower path.
  873. if (len < 65534) {
  874. if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
  875. return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
  876. }
  877. }
  878. var result = '';
  879. for (var i = 0; i < len; i++) {
  880. result += String.fromCharCode(buf[i]);
  881. }
  882. return result;
  883. }
  884. // Convert byte array to binary string
  885. exports.buf2binstring = function (buf) {
  886. return buf2binstring(buf, buf.length);
  887. };
  888. // Convert binary string (typed, when possible)
  889. exports.binstring2buf = function (str) {
  890. var buf = new utils.Buf8(str.length);
  891. for (var i = 0, len = buf.length; i < len; i++) {
  892. buf[i] = str.charCodeAt(i);
  893. }
  894. return buf;
  895. };
  896. // convert array to string
  897. exports.buf2string = function (buf, max) {
  898. var i, out, c, c_len;
  899. var len = max || buf.length;
  900. // Reserve max possible length (2 words per char)
  901. // NB: by unknown reasons, Array is significantly faster for
  902. // String.fromCharCode.apply than Uint16Array.
  903. var utf16buf = new Array(len * 2);
  904. for (out = 0, i = 0; i < len;) {
  905. c = buf[i++];
  906. // quick process ascii
  907. if (c < 0x80) { utf16buf[out++] = c; continue; }
  908. c_len = _utf8len[c];
  909. // skip 5 & 6 byte codes
  910. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
  911. // apply mask on first byte
  912. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  913. // join the rest
  914. while (c_len > 1 && i < len) {
  915. c = (c << 6) | (buf[i++] & 0x3f);
  916. c_len--;
  917. }
  918. // terminated by end of string?
  919. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  920. if (c < 0x10000) {
  921. utf16buf[out++] = c;
  922. } else {
  923. c -= 0x10000;
  924. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  925. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  926. }
  927. }
  928. return buf2binstring(utf16buf, out);
  929. };
  930. // Calculate max possible position in utf8 buffer,
  931. // that will not break sequence. If that's not possible
  932. // - (very small limits) return max size as is.
  933. //
  934. // buf[] - utf8 bytes array
  935. // max - length limit (mandatory);
  936. exports.utf8border = function (buf, max) {
  937. var pos;
  938. max = max || buf.length;
  939. if (max > buf.length) { max = buf.length; }
  940. // go back from last position, until start of sequence found
  941. pos = max - 1;
  942. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  943. // Very small and broken sequence,
  944. // return max, because we should return something anyway.
  945. if (pos < 0) { return max; }
  946. // If we came to start of buffer - that means buffer is too small,
  947. // return max too.
  948. if (pos === 0) { return max; }
  949. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  950. };
  951. },{"./common":3}],5:[function(require,module,exports){
  952. 'use strict';
  953. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  954. // It isn't worth it to make additional optimizations as in original.
  955. // Small size is preferable.
  956. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  957. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  958. //
  959. // This software is provided 'as-is', without any express or implied
  960. // warranty. In no event will the authors be held liable for any damages
  961. // arising from the use of this software.
  962. //
  963. // Permission is granted to anyone to use this software for any purpose,
  964. // including commercial applications, and to alter it and redistribute it
  965. // freely, subject to the following restrictions:
  966. //
  967. // 1. The origin of this software must not be misrepresented; you must not
  968. // claim that you wrote the original software. If you use this software
  969. // in a product, an acknowledgment in the product documentation would be
  970. // appreciated but is not required.
  971. // 2. Altered source versions must be plainly marked as such, and must not be
  972. // misrepresented as being the original software.
  973. // 3. This notice may not be removed or altered from any source distribution.
  974. function adler32(adler, buf, len, pos) {
  975. var s1 = (adler & 0xffff) |0,
  976. s2 = ((adler >>> 16) & 0xffff) |0,
  977. n = 0;
  978. while (len !== 0) {
  979. // Set limit ~ twice less than 5552, to keep
  980. // s2 in 31-bits, because we force signed ints.
  981. // in other case %= will fail.
  982. n = len > 2000 ? 2000 : len;
  983. len -= n;
  984. do {
  985. s1 = (s1 + buf[pos++]) |0;
  986. s2 = (s2 + s1) |0;
  987. } while (--n);
  988. s1 %= 65521;
  989. s2 %= 65521;
  990. }
  991. return (s1 | (s2 << 16)) |0;
  992. }
  993. module.exports = adler32;
  994. },{}],6:[function(require,module,exports){
  995. 'use strict';
  996. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  997. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  998. //
  999. // This software is provided 'as-is', without any express or implied
  1000. // warranty. In no event will the authors be held liable for any damages
  1001. // arising from the use of this software.
  1002. //
  1003. // Permission is granted to anyone to use this software for any purpose,
  1004. // including commercial applications, and to alter it and redistribute it
  1005. // freely, subject to the following restrictions:
  1006. //
  1007. // 1. The origin of this software must not be misrepresented; you must not
  1008. // claim that you wrote the original software. If you use this software
  1009. // in a product, an acknowledgment in the product documentation would be
  1010. // appreciated but is not required.
  1011. // 2. Altered source versions must be plainly marked as such, and must not be
  1012. // misrepresented as being the original software.
  1013. // 3. This notice may not be removed or altered from any source distribution.
  1014. module.exports = {
  1015. /* Allowed flush values; see deflate() and inflate() below for details */
  1016. Z_NO_FLUSH: 0,
  1017. Z_PARTIAL_FLUSH: 1,
  1018. Z_SYNC_FLUSH: 2,
  1019. Z_FULL_FLUSH: 3,
  1020. Z_FINISH: 4,
  1021. Z_BLOCK: 5,
  1022. Z_TREES: 6,
  1023. /* Return codes for the compression/decompression functions. Negative values
  1024. * are errors, positive values are used for special but normal events.
  1025. */
  1026. Z_OK: 0,
  1027. Z_STREAM_END: 1,
  1028. Z_NEED_DICT: 2,
  1029. Z_ERRNO: -1,
  1030. Z_STREAM_ERROR: -2,
  1031. Z_DATA_ERROR: -3,
  1032. //Z_MEM_ERROR: -4,
  1033. Z_BUF_ERROR: -5,
  1034. //Z_VERSION_ERROR: -6,
  1035. /* compression levels */
  1036. Z_NO_COMPRESSION: 0,
  1037. Z_BEST_SPEED: 1,
  1038. Z_BEST_COMPRESSION: 9,
  1039. Z_DEFAULT_COMPRESSION: -1,
  1040. Z_FILTERED: 1,
  1041. Z_HUFFMAN_ONLY: 2,
  1042. Z_RLE: 3,
  1043. Z_FIXED: 4,
  1044. Z_DEFAULT_STRATEGY: 0,
  1045. /* Possible values of the data_type field (though see inflate()) */
  1046. Z_BINARY: 0,
  1047. Z_TEXT: 1,
  1048. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  1049. Z_UNKNOWN: 2,
  1050. /* The deflate compression method */
  1051. Z_DEFLATED: 8
  1052. //Z_NULL: null // Use -1 or null inline, depending on var type
  1053. };
  1054. },{}],7:[function(require,module,exports){
  1055. 'use strict';
  1056. // Note: we can't get significant speed boost here.
  1057. // So write code to minimize size - no pregenerated tables
  1058. // and array tools dependencies.
  1059. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1060. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1061. //
  1062. // This software is provided 'as-is', without any express or implied
  1063. // warranty. In no event will the authors be held liable for any damages
  1064. // arising from the use of this software.
  1065. //
  1066. // Permission is granted to anyone to use this software for any purpose,
  1067. // including commercial applications, and to alter it and redistribute it
  1068. // freely, subject to the following restrictions:
  1069. //
  1070. // 1. The origin of this software must not be misrepresented; you must not
  1071. // claim that you wrote the original software. If you use this software
  1072. // in a product, an acknowledgment in the product documentation would be
  1073. // appreciated but is not required.
  1074. // 2. Altered source versions must be plainly marked as such, and must not be
  1075. // misrepresented as being the original software.
  1076. // 3. This notice may not be removed or altered from any source distribution.
  1077. // Use ordinary array, since untyped makes no boost here
  1078. function makeTable() {
  1079. var c, table = [];
  1080. for (var n = 0; n < 256; n++) {
  1081. c = n;
  1082. for (var k = 0; k < 8; k++) {
  1083. c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  1084. }
  1085. table[n] = c;
  1086. }
  1087. return table;
  1088. }
  1089. // Create table on load. Just 255 signed longs. Not a problem.
  1090. var crcTable = makeTable();
  1091. function crc32(crc, buf, len, pos) {
  1092. var t = crcTable,
  1093. end = pos + len;
  1094. crc ^= -1;
  1095. for (var i = pos; i < end; i++) {
  1096. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  1097. }
  1098. return (crc ^ (-1)); // >>> 0;
  1099. }
  1100. module.exports = crc32;
  1101. },{}],8:[function(require,module,exports){
  1102. 'use strict';
  1103. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1104. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1105. //
  1106. // This software is provided 'as-is', without any express or implied
  1107. // warranty. In no event will the authors be held liable for any damages
  1108. // arising from the use of this software.
  1109. //
  1110. // Permission is granted to anyone to use this software for any purpose,
  1111. // including commercial applications, and to alter it and redistribute it
  1112. // freely, subject to the following restrictions:
  1113. //
  1114. // 1. The origin of this software must not be misrepresented; you must not
  1115. // claim that you wrote the original software. If you use this software
  1116. // in a product, an acknowledgment in the product documentation would be
  1117. // appreciated but is not required.
  1118. // 2. Altered source versions must be plainly marked as such, and must not be
  1119. // misrepresented as being the original software.
  1120. // 3. This notice may not be removed or altered from any source distribution.
  1121. var utils = require('../utils/common');
  1122. var trees = require('./trees');
  1123. var adler32 = require('./adler32');
  1124. var crc32 = require('./crc32');
  1125. var msg = require('./messages');
  1126. /* Public constants ==========================================================*/
  1127. /* ===========================================================================*/
  1128. /* Allowed flush values; see deflate() and inflate() below for details */
  1129. var Z_NO_FLUSH = 0;
  1130. var Z_PARTIAL_FLUSH = 1;
  1131. //var Z_SYNC_FLUSH = 2;
  1132. var Z_FULL_FLUSH = 3;
  1133. var Z_FINISH = 4;
  1134. var Z_BLOCK = 5;
  1135. //var Z_TREES = 6;
  1136. /* Return codes for the compression/decompression functions. Negative values
  1137. * are errors, positive values are used for special but normal events.
  1138. */
  1139. var Z_OK = 0;
  1140. var Z_STREAM_END = 1;
  1141. //var Z_NEED_DICT = 2;
  1142. //var Z_ERRNO = -1;
  1143. var Z_STREAM_ERROR = -2;
  1144. var Z_DATA_ERROR = -3;
  1145. //var Z_MEM_ERROR = -4;
  1146. var Z_BUF_ERROR = -5;
  1147. //var Z_VERSION_ERROR = -6;
  1148. /* compression levels */
  1149. //var Z_NO_COMPRESSION = 0;
  1150. //var Z_BEST_SPEED = 1;
  1151. //var Z_BEST_COMPRESSION = 9;
  1152. var Z_DEFAULT_COMPRESSION = -1;
  1153. var Z_FILTERED = 1;
  1154. var Z_HUFFMAN_ONLY = 2;
  1155. var Z_RLE = 3;
  1156. var Z_FIXED = 4;
  1157. var Z_DEFAULT_STRATEGY = 0;
  1158. /* Possible values of the data_type field (though see inflate()) */
  1159. //var Z_BINARY = 0;
  1160. //var Z_TEXT = 1;
  1161. //var Z_ASCII = 1; // = Z_TEXT
  1162. var Z_UNKNOWN = 2;
  1163. /* The deflate compression method */
  1164. var Z_DEFLATED = 8;
  1165. /*============================================================================*/
  1166. var MAX_MEM_LEVEL = 9;
  1167. /* Maximum value for memLevel in deflateInit2 */
  1168. var MAX_WBITS = 15;
  1169. /* 32K LZ77 window */
  1170. var DEF_MEM_LEVEL = 8;
  1171. var LENGTH_CODES = 29;
  1172. /* number of length codes, not counting the special END_BLOCK code */
  1173. var LITERALS = 256;
  1174. /* number of literal bytes 0..255 */
  1175. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  1176. /* number of Literal or Length codes, including the END_BLOCK code */
  1177. var D_CODES = 30;
  1178. /* number of distance codes */
  1179. var BL_CODES = 19;
  1180. /* number of codes used to transfer the bit lengths */
  1181. var HEAP_SIZE = 2 * L_CODES + 1;
  1182. /* maximum heap size */
  1183. var MAX_BITS = 15;
  1184. /* All codes must not exceed MAX_BITS bits */
  1185. var MIN_MATCH = 3;
  1186. var MAX_MATCH = 258;
  1187. var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  1188. var PRESET_DICT = 0x20;
  1189. var INIT_STATE = 42;
  1190. var EXTRA_STATE = 69;
  1191. var NAME_STATE = 73;
  1192. var COMMENT_STATE = 91;
  1193. var HCRC_STATE = 103;
  1194. var BUSY_STATE = 113;
  1195. var FINISH_STATE = 666;
  1196. var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
  1197. var BS_BLOCK_DONE = 2; /* block flush performed */
  1198. var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
  1199. var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
  1200. var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
  1201. function err(strm, errorCode) {
  1202. strm.msg = msg[errorCode];
  1203. return errorCode;
  1204. }
  1205. function rank(f) {
  1206. return ((f) << 1) - ((f) > 4 ? 9 : 0);
  1207. }
  1208. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  1209. /* =========================================================================
  1210. * Flush as much pending output as possible. All deflate() output goes
  1211. * through this function so some applications may wish to modify it
  1212. * to avoid allocating a large strm->output buffer and copying into it.
  1213. * (See also read_buf()).
  1214. */
  1215. function flush_pending(strm) {
  1216. var s = strm.state;
  1217. //_tr_flush_bits(s);
  1218. var len = s.pending;
  1219. if (len > strm.avail_out) {
  1220. len = strm.avail_out;
  1221. }
  1222. if (len === 0) { return; }
  1223. utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
  1224. strm.next_out += len;
  1225. s.pending_out += len;
  1226. strm.total_out += len;
  1227. strm.avail_out -= len;
  1228. s.pending -= len;
  1229. if (s.pending === 0) {
  1230. s.pending_out = 0;
  1231. }
  1232. }
  1233. function flush_block_only(s, last) {
  1234. trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
  1235. s.block_start = s.strstart;
  1236. flush_pending(s.strm);
  1237. }
  1238. function put_byte(s, b) {
  1239. s.pending_buf[s.pending++] = b;
  1240. }
  1241. /* =========================================================================
  1242. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  1243. * IN assertion: the stream state is correct and there is enough room in
  1244. * pending_buf.
  1245. */
  1246. function putShortMSB(s, b) {
  1247. // put_byte(s, (Byte)(b >> 8));
  1248. // put_byte(s, (Byte)(b & 0xff));
  1249. s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
  1250. s.pending_buf[s.pending++] = b & 0xff;
  1251. }
  1252. /* ===========================================================================
  1253. * Read a new buffer from the current input stream, update the adler32
  1254. * and total number of bytes read. All deflate() input goes through
  1255. * this function so some applications may wish to modify it to avoid
  1256. * allocating a large strm->input buffer and copying from it.
  1257. * (See also flush_pending()).
  1258. */
  1259. function read_buf(strm, buf, start, size) {
  1260. var len = strm.avail_in;
  1261. if (len > size) { len = size; }
  1262. if (len === 0) { return 0; }
  1263. strm.avail_in -= len;
  1264. // zmemcpy(buf, strm->next_in, len);
  1265. utils.arraySet(buf, strm.input, strm.next_in, len, start);
  1266. if (strm.state.wrap === 1) {
  1267. strm.adler = adler32(strm.adler, buf, len, start);
  1268. }
  1269. else if (strm.state.wrap === 2) {
  1270. strm.adler = crc32(strm.adler, buf, len, start);
  1271. }
  1272. strm.next_in += len;
  1273. strm.total_in += len;
  1274. return len;
  1275. }
  1276. /* ===========================================================================
  1277. * Set match_start to the longest match starting at the given string and
  1278. * return its length. Matches shorter or equal to prev_length are discarded,
  1279. * in which case the result is equal to prev_length and match_start is
  1280. * garbage.
  1281. * IN assertions: cur_match is the head of the hash chain for the current
  1282. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1283. * OUT assertion: the match length is not greater than s->lookahead.
  1284. */
  1285. function longest_match(s, cur_match) {
  1286. var chain_length = s.max_chain_length; /* max hash chain length */
  1287. var scan = s.strstart; /* current string */
  1288. var match; /* matched string */
  1289. var len; /* length of current match */
  1290. var best_len = s.prev_length; /* best match length so far */
  1291. var nice_match = s.nice_match; /* stop if match long enough */
  1292. var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
  1293. s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
  1294. var _win = s.window; // shortcut
  1295. var wmask = s.w_mask;
  1296. var prev = s.prev;
  1297. /* Stop when cur_match becomes <= limit. To simplify the code,
  1298. * we prevent matches with the string of window index 0.
  1299. */
  1300. var strend = s.strstart + MAX_MATCH;
  1301. var scan_end1 = _win[scan + best_len - 1];
  1302. var scan_end = _win[scan + best_len];
  1303. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1304. * It is easy to get rid of this optimization if necessary.
  1305. */
  1306. // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  1307. /* Do not waste too much time if we already have a good match: */
  1308. if (s.prev_length >= s.good_match) {
  1309. chain_length >>= 2;
  1310. }
  1311. /* Do not look for matches beyond the end of the input. This is necessary
  1312. * to make deflate deterministic.
  1313. */
  1314. if (nice_match > s.lookahead) { nice_match = s.lookahead; }
  1315. // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  1316. do {
  1317. // Assert(cur_match < s->strstart, "no future");
  1318. match = cur_match;
  1319. /* Skip to next match if the match length cannot increase
  1320. * or if the match length is less than 2. Note that the checks below
  1321. * for insufficient lookahead only occur occasionally for performance
  1322. * reasons. Therefore uninitialized memory will be accessed, and
  1323. * conditional jumps will be made that depend on those values.
  1324. * However the length of the match is limited to the lookahead, so
  1325. * the output of deflate is not affected by the uninitialized values.
  1326. */
  1327. if (_win[match + best_len] !== scan_end ||
  1328. _win[match + best_len - 1] !== scan_end1 ||
  1329. _win[match] !== _win[scan] ||
  1330. _win[++match] !== _win[scan + 1]) {
  1331. continue;
  1332. }
  1333. /* The check at best_len-1 can be removed because it will be made
  1334. * again later. (This heuristic is not always a win.)
  1335. * It is not necessary to compare scan[2] and match[2] since they
  1336. * are always equal when the other bytes match, given that
  1337. * the hash keys are equal and that HASH_BITS >= 8.
  1338. */
  1339. scan += 2;
  1340. match++;
  1341. // Assert(*scan == *match, "match[2]?");
  1342. /* We check for insufficient lookahead only every 8th comparison;
  1343. * the 256th check will be made at strstart+258.
  1344. */
  1345. do {
  1346. /*jshint noempty:false*/
  1347. } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1348. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1349. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1350. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1351. scan < strend);
  1352. // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  1353. len = MAX_MATCH - (strend - scan);
  1354. scan = strend - MAX_MATCH;
  1355. if (len > best_len) {
  1356. s.match_start = cur_match;
  1357. best_len = len;
  1358. if (len >= nice_match) {
  1359. break;
  1360. }
  1361. scan_end1 = _win[scan + best_len - 1];
  1362. scan_end = _win[scan + best_len];
  1363. }
  1364. } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
  1365. if (best_len <= s.lookahead) {
  1366. return best_len;
  1367. }
  1368. return s.lookahead;
  1369. }
  1370. /* ===========================================================================
  1371. * Fill the window when the lookahead becomes insufficient.
  1372. * Updates strstart and lookahead.
  1373. *
  1374. * IN assertion: lookahead < MIN_LOOKAHEAD
  1375. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1376. * At least one byte has been read, or avail_in == 0; reads are
  1377. * performed for at least two bytes (required for the zip translate_eol
  1378. * option -- not supported here).
  1379. */
  1380. function fill_window(s) {
  1381. var _w_size = s.w_size;
  1382. var p, n, m, more, str;
  1383. //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  1384. do {
  1385. more = s.window_size - s.lookahead - s.strstart;
  1386. // JS ints have 32 bit, block below not needed
  1387. /* Deal with !@#$% 64K limit: */
  1388. //if (sizeof(int) <= 2) {
  1389. // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  1390. // more = wsize;
  1391. //
  1392. // } else if (more == (unsigned)(-1)) {
  1393. // /* Very unlikely, but possible on 16 bit machine if
  1394. // * strstart == 0 && lookahead == 1 (input done a byte at time)
  1395. // */
  1396. // more--;
  1397. // }
  1398. //}
  1399. /* If the window is almost full and there is insufficient lookahead,
  1400. * move the upper half to the lower one to make room in the upper half.
  1401. */
  1402. if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
  1403. utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
  1404. s.match_start -= _w_size;
  1405. s.strstart -= _w_size;
  1406. /* we now have strstart >= MAX_DIST */
  1407. s.block_start -= _w_size;
  1408. /* Slide the hash table (could be avoided with 32 bit values
  1409. at the expense of memory usage). We slide even when level == 0
  1410. to keep the hash table consistent if we switch back to level > 0
  1411. later. (Using level 0 permanently is not an optimal usage of
  1412. zlib, so we don't care about this pathological case.)
  1413. */
  1414. n = s.hash_size;
  1415. p = n;
  1416. do {
  1417. m = s.head[--p];
  1418. s.head[p] = (m >= _w_size ? m - _w_size : 0);
  1419. } while (--n);
  1420. n = _w_size;
  1421. p = n;
  1422. do {
  1423. m = s.prev[--p];
  1424. s.prev[p] = (m >= _w_size ? m - _w_size : 0);
  1425. /* If n is not on any hash chain, prev[n] is garbage but
  1426. * its value will never be used.
  1427. */
  1428. } while (--n);
  1429. more += _w_size;
  1430. }
  1431. if (s.strm.avail_in === 0) {
  1432. break;
  1433. }
  1434. /* If there was no sliding:
  1435. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1436. * more == window_size - lookahead - strstart
  1437. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1438. * => more >= window_size - 2*WSIZE + 2
  1439. * In the BIG_MEM or MMAP case (not yet supported),
  1440. * window_size == input_size + MIN_LOOKAHEAD &&
  1441. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1442. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1443. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1444. */
  1445. //Assert(more >= 2, "more < 2");
  1446. n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
  1447. s.lookahead += n;
  1448. /* Initialize the hash value now that we have some input: */
  1449. if (s.lookahead + s.insert >= MIN_MATCH) {
  1450. str = s.strstart - s.insert;
  1451. s.ins_h = s.window[str];
  1452. /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
  1453. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
  1454. //#if MIN_MATCH != 3
  1455. // Call update_hash() MIN_MATCH-3 more times
  1456. //#endif
  1457. while (s.insert) {
  1458. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  1459. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
  1460. s.prev[str & s.w_mask] = s.head[s.ins_h];
  1461. s.head[s.ins_h] = str;
  1462. str++;
  1463. s.insert--;
  1464. if (s.lookahead + s.insert < MIN_MATCH) {
  1465. break;
  1466. }
  1467. }
  1468. }
  1469. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1470. * but this is not important since only literal bytes will be emitted.
  1471. */
  1472. } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
  1473. /* If the WIN_INIT bytes after the end of the current data have never been
  1474. * written, then zero those bytes in order to avoid memory check reports of
  1475. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  1476. * the longest match routines. Update the high water mark for the next
  1477. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  1478. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  1479. */
  1480. // if (s.high_water < s.window_size) {
  1481. // var curr = s.strstart + s.lookahead;
  1482. // var init = 0;
  1483. //
  1484. // if (s.high_water < curr) {
  1485. // /* Previous high water mark below current data -- zero WIN_INIT
  1486. // * bytes or up to end of window, whichever is less.
  1487. // */
  1488. // init = s.window_size - curr;
  1489. // if (init > WIN_INIT)
  1490. // init = WIN_INIT;
  1491. // zmemzero(s->window + curr, (unsigned)init);
  1492. // s->high_water = curr + init;
  1493. // }
  1494. // else if (s->high_water < (ulg)curr + WIN_INIT) {
  1495. // /* High water mark at or above current data, but below current data
  1496. // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  1497. // * to end of window, whichever is less.
  1498. // */
  1499. // init = (ulg)curr + WIN_INIT - s->high_water;
  1500. // if (init > s->window_size - s->high_water)
  1501. // init = s->window_size - s->high_water;
  1502. // zmemzero(s->window + s->high_water, (unsigned)init);
  1503. // s->high_water += init;
  1504. // }
  1505. // }
  1506. //
  1507. // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  1508. // "not enough room for search");
  1509. }
  1510. /* ===========================================================================
  1511. * Copy without compression as much as possible from the input stream, return
  1512. * the current block state.
  1513. * This function does not insert new strings in the dictionary since
  1514. * uncompressible data is probably not useful. This function is used
  1515. * only for the level=0 compression option.
  1516. * NOTE: this function should be optimized to avoid extra copying from
  1517. * window to pending_buf.
  1518. */
  1519. function deflate_stored(s, flush) {
  1520. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1521. * to pending_buf_size, and each stored block has a 5 byte header:
  1522. */
  1523. var max_block_size = 0xffff;
  1524. if (max_block_size > s.pending_buf_size - 5) {
  1525. max_block_size = s.pending_buf_size - 5;
  1526. }
  1527. /* Copy as much as possible from input to output: */
  1528. for (;;) {
  1529. /* Fill the window as much as possible: */
  1530. if (s.lookahead <= 1) {
  1531. //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  1532. // s->block_start >= (long)s->w_size, "slide too late");
  1533. // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
  1534. // s.block_start >= s.w_size)) {
  1535. // throw new Error("slide too late");
  1536. // }
  1537. fill_window(s);
  1538. if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
  1539. return BS_NEED_MORE;
  1540. }
  1541. if (s.lookahead === 0) {
  1542. break;
  1543. }
  1544. /* flush the current block */
  1545. }
  1546. //Assert(s->block_start >= 0L, "block gone");
  1547. // if (s.block_start < 0) throw new Error("block gone");
  1548. s.strstart += s.lookahead;
  1549. s.lookahead = 0;
  1550. /* Emit a stored block if pending_buf will be full: */
  1551. var max_start = s.block_start + max_block_size;
  1552. if (s.strstart === 0 || s.strstart >= max_start) {
  1553. /* strstart == 0 is possible when wraparound on 16-bit machine */
  1554. s.lookahead = s.strstart - max_start;
  1555. s.strstart = max_start;
  1556. /*** FLUSH_BLOCK(s, 0); ***/
  1557. flush_block_only(s, false);
  1558. if (s.strm.avail_out === 0) {
  1559. return BS_NEED_MORE;
  1560. }
  1561. /***/
  1562. }
  1563. /* Flush if we may have to slide, otherwise block_start may become
  1564. * negative and the data will be gone:
  1565. */
  1566. if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
  1567. /*** FLUSH_BLOCK(s, 0); ***/
  1568. flush_block_only(s, false);
  1569. if (s.strm.avail_out === 0) {
  1570. return BS_NEED_MORE;
  1571. }
  1572. /***/
  1573. }
  1574. }
  1575. s.insert = 0;
  1576. if (flush === Z_FINISH) {
  1577. /*** FLUSH_BLOCK(s, 1); ***/
  1578. flush_block_only(s, true);
  1579. if (s.strm.avail_out === 0) {
  1580. return BS_FINISH_STARTED;
  1581. }
  1582. /***/
  1583. return BS_FINISH_DONE;
  1584. }
  1585. if (s.strstart > s.block_start) {
  1586. /*** FLUSH_BLOCK(s, 0); ***/
  1587. flush_block_only(s, false);
  1588. if (s.strm.avail_out === 0) {
  1589. return BS_NEED_MORE;
  1590. }
  1591. /***/
  1592. }
  1593. return BS_NEED_MORE;
  1594. }
  1595. /* ===========================================================================
  1596. * Compress as much as possible from the input stream, return the current
  1597. * block state.
  1598. * This function does not perform lazy evaluation of matches and inserts
  1599. * new strings in the dictionary only for unmatched strings or for short
  1600. * matches. It is used only for the fast compression options.
  1601. */
  1602. function deflate_fast(s, flush) {
  1603. var hash_head; /* head of the hash chain */
  1604. var bflush; /* set if current block must be flushed */
  1605. for (;;) {
  1606. /* Make sure that we always have enough lookahead, except
  1607. * at the end of the input file. We need MAX_MATCH bytes
  1608. * for the next match, plus MIN_MATCH bytes to insert the
  1609. * string following the next match.
  1610. */
  1611. if (s.lookahead < MIN_LOOKAHEAD) {
  1612. fill_window(s);
  1613. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  1614. return BS_NEED_MORE;
  1615. }
  1616. if (s.lookahead === 0) {
  1617. break; /* flush the current block */
  1618. }
  1619. }
  1620. /* Insert the string window[strstart .. strstart+2] in the
  1621. * dictionary, and set hash_head to the head of the hash chain:
  1622. */
  1623. hash_head = 0/*NIL*/;
  1624. if (s.lookahead >= MIN_MATCH) {
  1625. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1626. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1627. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1628. s.head[s.ins_h] = s.strstart;
  1629. /***/
  1630. }
  1631. /* Find the longest match, discarding those <= prev_length.
  1632. * At this point we have always match_length < MIN_MATCH
  1633. */
  1634. if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
  1635. /* To simplify the code, we prevent matches with the string
  1636. * of window index 0 (in particular we have to avoid a match
  1637. * of the string with itself at the start of the input file).
  1638. */
  1639. s.match_length = longest_match(s, hash_head);
  1640. /* longest_match() sets match_start */
  1641. }
  1642. if (s.match_length >= MIN_MATCH) {
  1643. // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
  1644. /*** _tr_tally_dist(s, s.strstart - s.match_start,
  1645. s.match_length - MIN_MATCH, bflush); ***/
  1646. bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
  1647. s.lookahead -= s.match_length;
  1648. /* Insert new strings in the hash table only if the match length
  1649. * is not too large. This saves time but degrades compression.
  1650. */
  1651. if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
  1652. s.match_length--; /* string at strstart already in table */
  1653. do {
  1654. s.strstart++;
  1655. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1656. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1657. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1658. s.head[s.ins_h] = s.strstart;
  1659. /***/
  1660. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1661. * always MIN_MATCH bytes ahead.
  1662. */
  1663. } while (--s.match_length !== 0);
  1664. s.strstart++;
  1665. } else
  1666. {
  1667. s.strstart += s.match_length;
  1668. s.match_length = 0;
  1669. s.ins_h = s.window[s.strstart];
  1670. /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
  1671. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
  1672. //#if MIN_MATCH != 3
  1673. // Call UPDATE_HASH() MIN_MATCH-3 more times
  1674. //#endif
  1675. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1676. * matter since it will be recomputed at next deflate call.
  1677. */
  1678. }
  1679. } else {
  1680. /* No match, output a literal byte */
  1681. //Tracevv((stderr,"%c", s.window[s.strstart]));
  1682. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1683. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1684. s.lookahead--;
  1685. s.strstart++;
  1686. }
  1687. if (bflush) {
  1688. /*** FLUSH_BLOCK(s, 0); ***/
  1689. flush_block_only(s, false);
  1690. if (s.strm.avail_out === 0) {
  1691. return BS_NEED_MORE;
  1692. }
  1693. /***/
  1694. }
  1695. }
  1696. s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
  1697. if (flush === Z_FINISH) {
  1698. /*** FLUSH_BLOCK(s, 1); ***/
  1699. flush_block_only(s, true);
  1700. if (s.strm.avail_out === 0) {
  1701. return BS_FINISH_STARTED;
  1702. }
  1703. /***/
  1704. return BS_FINISH_DONE;
  1705. }
  1706. if (s.last_lit) {
  1707. /*** FLUSH_BLOCK(s, 0); ***/
  1708. flush_block_only(s, false);
  1709. if (s.strm.avail_out === 0) {
  1710. return BS_NEED_MORE;
  1711. }
  1712. /***/
  1713. }
  1714. return BS_BLOCK_DONE;
  1715. }
  1716. /* ===========================================================================
  1717. * Same as above, but achieves better compression. We use a lazy
  1718. * evaluation for matches: a match is finally adopted only if there is
  1719. * no better match at the next window position.
  1720. */
  1721. function deflate_slow(s, flush) {
  1722. var hash_head; /* head of hash chain */
  1723. var bflush; /* set if current block must be flushed */
  1724. var max_insert;
  1725. /* Process the input block. */
  1726. for (;;) {
  1727. /* Make sure that we always have enough lookahead, except
  1728. * at the end of the input file. We need MAX_MATCH bytes
  1729. * for the next match, plus MIN_MATCH bytes to insert the
  1730. * string following the next match.
  1731. */
  1732. if (s.lookahead < MIN_LOOKAHEAD) {
  1733. fill_window(s);
  1734. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  1735. return BS_NEED_MORE;
  1736. }
  1737. if (s.lookahead === 0) { break; } /* flush the current block */
  1738. }
  1739. /* Insert the string window[strstart .. strstart+2] in the
  1740. * dictionary, and set hash_head to the head of the hash chain:
  1741. */
  1742. hash_head = 0/*NIL*/;
  1743. if (s.lookahead >= MIN_MATCH) {
  1744. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1745. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1746. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1747. s.head[s.ins_h] = s.strstart;
  1748. /***/
  1749. }
  1750. /* Find the longest match, discarding those <= prev_length.
  1751. */
  1752. s.prev_length = s.match_length;
  1753. s.prev_match = s.match_start;
  1754. s.match_length = MIN_MATCH - 1;
  1755. if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
  1756. s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
  1757. /* To simplify the code, we prevent matches with the string
  1758. * of window index 0 (in particular we have to avoid a match
  1759. * of the string with itself at the start of the input file).
  1760. */
  1761. s.match_length = longest_match(s, hash_head);
  1762. /* longest_match() sets match_start */
  1763. if (s.match_length <= 5 &&
  1764. (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
  1765. /* If prev_match is also MIN_MATCH, match_start is garbage
  1766. * but we will ignore the current match anyway.
  1767. */
  1768. s.match_length = MIN_MATCH - 1;
  1769. }
  1770. }
  1771. /* If there was a match at the previous step and the current
  1772. * match is not better, output the previous match:
  1773. */
  1774. if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
  1775. max_insert = s.strstart + s.lookahead - MIN_MATCH;
  1776. /* Do not insert strings in hash table beyond this. */
  1777. //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  1778. /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
  1779. s.prev_length - MIN_MATCH, bflush);***/
  1780. bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
  1781. /* Insert in hash table all strings up to the end of the match.
  1782. * strstart-1 and strstart are already inserted. If there is not
  1783. * enough lookahead, the last two strings are not inserted in
  1784. * the hash table.
  1785. */
  1786. s.lookahead -= s.prev_length - 1;
  1787. s.prev_length -= 2;
  1788. do {
  1789. if (++s.strstart <= max_insert) {
  1790. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1791. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  1792. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1793. s.head[s.ins_h] = s.strstart;
  1794. /***/
  1795. }
  1796. } while (--s.prev_length !== 0);
  1797. s.match_available = 0;
  1798. s.match_length = MIN_MATCH - 1;
  1799. s.strstart++;
  1800. if (bflush) {
  1801. /*** FLUSH_BLOCK(s, 0); ***/
  1802. flush_block_only(s, false);
  1803. if (s.strm.avail_out === 0) {
  1804. return BS_NEED_MORE;
  1805. }
  1806. /***/
  1807. }
  1808. } else if (s.match_available) {
  1809. /* If there was no match at the previous position, output a
  1810. * single literal. If there was a match but the current match
  1811. * is longer, truncate the previous match to a single literal.
  1812. */
  1813. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1814. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  1815. bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
  1816. if (bflush) {
  1817. /*** FLUSH_BLOCK_ONLY(s, 0) ***/
  1818. flush_block_only(s, false);
  1819. /***/
  1820. }
  1821. s.strstart++;
  1822. s.lookahead--;
  1823. if (s.strm.avail_out === 0) {
  1824. return BS_NEED_MORE;
  1825. }
  1826. } else {
  1827. /* There is no previous match to compare with, wait for
  1828. * the next step to decide.
  1829. */
  1830. s.match_available = 1;
  1831. s.strstart++;
  1832. s.lookahead--;
  1833. }
  1834. }
  1835. //Assert (flush != Z_NO_FLUSH, "no flush?");
  1836. if (s.match_available) {
  1837. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1838. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  1839. bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]);
  1840. s.match_available = 0;
  1841. }
  1842. s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
  1843. if (flush === Z_FINISH) {
  1844. /*** FLUSH_BLOCK(s, 1); ***/
  1845. flush_block_only(s, true);
  1846. if (s.strm.avail_out === 0) {
  1847. return BS_FINISH_STARTED;
  1848. }
  1849. /***/
  1850. return BS_FINISH_DONE;
  1851. }
  1852. if (s.last_lit) {
  1853. /*** FLUSH_BLOCK(s, 0); ***/
  1854. flush_block_only(s, false);
  1855. if (s.strm.avail_out === 0) {
  1856. return BS_NEED_MORE;
  1857. }
  1858. /***/
  1859. }
  1860. return BS_BLOCK_DONE;
  1861. }
  1862. /* ===========================================================================
  1863. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  1864. * one. Do not maintain a hash table. (It will be regenerated if this run of
  1865. * deflate switches away from Z_RLE.)
  1866. */
  1867. function deflate_rle(s, flush) {
  1868. var bflush; /* set if current block must be flushed */
  1869. var prev; /* byte at distance one to match */
  1870. var scan, strend; /* scan goes up to strend for length of run */
  1871. var _win = s.window;
  1872. for (;;) {
  1873. /* Make sure that we always have enough lookahead, except
  1874. * at the end of the input file. We need MAX_MATCH bytes
  1875. * for the longest run, plus one for the unrolled loop.
  1876. */
  1877. if (s.lookahead <= MAX_MATCH) {
  1878. fill_window(s);
  1879. if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
  1880. return BS_NEED_MORE;
  1881. }
  1882. if (s.lookahead === 0) { break; } /* flush the current block */
  1883. }
  1884. /* See how many times the previous byte repeats */
  1885. s.match_length = 0;
  1886. if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
  1887. scan = s.strstart - 1;
  1888. prev = _win[scan];
  1889. if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
  1890. strend = s.strstart + MAX_MATCH;
  1891. do {
  1892. /*jshint noempty:false*/
  1893. } while (prev === _win[++scan] && prev === _win[++scan] &&
  1894. prev === _win[++scan] && prev === _win[++scan] &&
  1895. prev === _win[++scan] && prev === _win[++scan] &&
  1896. prev === _win[++scan] && prev === _win[++scan] &&
  1897. scan < strend);
  1898. s.match_length = MAX_MATCH - (strend - scan);
  1899. if (s.match_length > s.lookahead) {
  1900. s.match_length = s.lookahead;
  1901. }
  1902. }
  1903. //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
  1904. }
  1905. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  1906. if (s.match_length >= MIN_MATCH) {
  1907. //check_match(s, s.strstart, s.strstart - 1, s.match_length);
  1908. /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
  1909. bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
  1910. s.lookahead -= s.match_length;
  1911. s.strstart += s.match_length;
  1912. s.match_length = 0;
  1913. } else {
  1914. /* No match, output a literal byte */
  1915. //Tracevv((stderr,"%c", s->window[s->strstart]));
  1916. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1917. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1918. s.lookahead--;
  1919. s.strstart++;
  1920. }
  1921. if (bflush) {
  1922. /*** FLUSH_BLOCK(s, 0); ***/
  1923. flush_block_only(s, false);
  1924. if (s.strm.avail_out === 0) {
  1925. return BS_NEED_MORE;
  1926. }
  1927. /***/
  1928. }
  1929. }
  1930. s.insert = 0;
  1931. if (flush === Z_FINISH) {
  1932. /*** FLUSH_BLOCK(s, 1); ***/
  1933. flush_block_only(s, true);
  1934. if (s.strm.avail_out === 0) {
  1935. return BS_FINISH_STARTED;
  1936. }
  1937. /***/
  1938. return BS_FINISH_DONE;
  1939. }
  1940. if (s.last_lit) {
  1941. /*** FLUSH_BLOCK(s, 0); ***/
  1942. flush_block_only(s, false);
  1943. if (s.strm.avail_out === 0) {
  1944. return BS_NEED_MORE;
  1945. }
  1946. /***/
  1947. }
  1948. return BS_BLOCK_DONE;
  1949. }
  1950. /* ===========================================================================
  1951. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  1952. * (It will be regenerated if this run of deflate switches away from Huffman.)
  1953. */
  1954. function deflate_huff(s, flush) {
  1955. var bflush; /* set if current block must be flushed */
  1956. for (;;) {
  1957. /* Make sure that we have a literal to write. */
  1958. if (s.lookahead === 0) {
  1959. fill_window(s);
  1960. if (s.lookahead === 0) {
  1961. if (flush === Z_NO_FLUSH) {
  1962. return BS_NEED_MORE;
  1963. }
  1964. break; /* flush the current block */
  1965. }
  1966. }
  1967. /* Output a literal byte */
  1968. s.match_length = 0;
  1969. //Tracevv((stderr,"%c", s->window[s->strstart]));
  1970. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1971. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  1972. s.lookahead--;
  1973. s.strstart++;
  1974. if (bflush) {
  1975. /*** FLUSH_BLOCK(s, 0); ***/
  1976. flush_block_only(s, false);
  1977. if (s.strm.avail_out === 0) {
  1978. return BS_NEED_MORE;
  1979. }
  1980. /***/
  1981. }
  1982. }
  1983. s.insert = 0;
  1984. if (flush === Z_FINISH) {
  1985. /*** FLUSH_BLOCK(s, 1); ***/
  1986. flush_block_only(s, true);
  1987. if (s.strm.avail_out === 0) {
  1988. return BS_FINISH_STARTED;
  1989. }
  1990. /***/
  1991. return BS_FINISH_DONE;
  1992. }
  1993. if (s.last_lit) {
  1994. /*** FLUSH_BLOCK(s, 0); ***/
  1995. flush_block_only(s, false);
  1996. if (s.strm.avail_out === 0) {
  1997. return BS_NEED_MORE;
  1998. }
  1999. /***/
  2000. }
  2001. return BS_BLOCK_DONE;
  2002. }
  2003. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  2004. * the desired pack level (0..9). The values given below have been tuned to
  2005. * exclude worst case performance for pathological files. Better values may be
  2006. * found for specific files.
  2007. */
  2008. function Config(good_length, max_lazy, nice_length, max_chain, func) {
  2009. this.good_length = good_length;
  2010. this.max_lazy = max_lazy;
  2011. this.nice_length = nice_length;
  2012. this.max_chain = max_chain;
  2013. this.func = func;
  2014. }
  2015. var configuration_table;
  2016. configuration_table = [
  2017. /* good lazy nice chain */
  2018. new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
  2019. new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
  2020. new Config(4, 5, 16, 8, deflate_fast), /* 2 */
  2021. new Config(4, 6, 32, 32, deflate_fast), /* 3 */
  2022. new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
  2023. new Config(8, 16, 32, 32, deflate_slow), /* 5 */
  2024. new Config(8, 16, 128, 128, deflate_slow), /* 6 */
  2025. new Config(8, 32, 128, 256, deflate_slow), /* 7 */
  2026. new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
  2027. new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
  2028. ];
  2029. /* ===========================================================================
  2030. * Initialize the "longest match" routines for a new zlib stream
  2031. */
  2032. function lm_init(s) {
  2033. s.window_size = 2 * s.w_size;
  2034. /*** CLEAR_HASH(s); ***/
  2035. zero(s.head); // Fill with NIL (= 0);
  2036. /* Set the default configuration parameters:
  2037. */
  2038. s.max_lazy_match = configuration_table[s.level].max_lazy;
  2039. s.good_match = configuration_table[s.level].good_length;
  2040. s.nice_match = configuration_table[s.level].nice_length;
  2041. s.max_chain_length = configuration_table[s.level].max_chain;
  2042. s.strstart = 0;
  2043. s.block_start = 0;
  2044. s.lookahead = 0;
  2045. s.insert = 0;
  2046. s.match_length = s.prev_length = MIN_MATCH - 1;
  2047. s.match_available = 0;
  2048. s.ins_h = 0;
  2049. }
  2050. function DeflateState() {
  2051. this.strm = null; /* pointer back to this zlib stream */
  2052. this.status = 0; /* as the name implies */
  2053. this.pending_buf = null; /* output still pending */
  2054. this.pending_buf_size = 0; /* size of pending_buf */
  2055. this.pending_out = 0; /* next pending byte to output to the stream */
  2056. this.pending = 0; /* nb of bytes in the pending buffer */
  2057. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  2058. this.gzhead = null; /* gzip header information to write */
  2059. this.gzindex = 0; /* where in extra, name, or comment */
  2060. this.method = Z_DEFLATED; /* can only be DEFLATED */
  2061. this.last_flush = -1; /* value of flush param for previous deflate call */
  2062. this.w_size = 0; /* LZ77 window size (32K by default) */
  2063. this.w_bits = 0; /* log2(w_size) (8..16) */
  2064. this.w_mask = 0; /* w_size - 1 */
  2065. this.window = null;
  2066. /* Sliding window. Input bytes are read into the second half of the window,
  2067. * and move to the first half later to keep a dictionary of at least wSize
  2068. * bytes. With this organization, matches are limited to a distance of
  2069. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  2070. * performed with a length multiple of the block size.
  2071. */
  2072. this.window_size = 0;
  2073. /* Actual size of window: 2*wSize, except when the user input buffer
  2074. * is directly used as sliding window.
  2075. */
  2076. this.prev = null;
  2077. /* Link to older string with same hash index. To limit the size of this
  2078. * array to 64K, this link is maintained only for the last 32K strings.
  2079. * An index in this array is thus a window index modulo 32K.
  2080. */
  2081. this.head = null; /* Heads of the hash chains or NIL. */
  2082. this.ins_h = 0; /* hash index of string to be inserted */
  2083. this.hash_size = 0; /* number of elements in hash table */
  2084. this.hash_bits = 0; /* log2(hash_size) */
  2085. this.hash_mask = 0; /* hash_size-1 */
  2086. this.hash_shift = 0;
  2087. /* Number of bits by which ins_h must be shifted at each input
  2088. * step. It must be such that after MIN_MATCH steps, the oldest
  2089. * byte no longer takes part in the hash key, that is:
  2090. * hash_shift * MIN_MATCH >= hash_bits
  2091. */
  2092. this.block_start = 0;
  2093. /* Window position at the beginning of the current output block. Gets
  2094. * negative when the window is moved backwards.
  2095. */
  2096. this.match_length = 0; /* length of best match */
  2097. this.prev_match = 0; /* previous match */
  2098. this.match_available = 0; /* set if previous match exists */
  2099. this.strstart = 0; /* start of string to insert */
  2100. this.match_start = 0; /* start of matching string */
  2101. this.lookahead = 0; /* number of valid bytes ahead in window */
  2102. this.prev_length = 0;
  2103. /* Length of the best match at previous step. Matches not greater than this
  2104. * are discarded. This is used in the lazy match evaluation.
  2105. */
  2106. this.max_chain_length = 0;
  2107. /* To speed up deflation, hash chains are never searched beyond this
  2108. * length. A higher limit improves compression ratio but degrades the
  2109. * speed.
  2110. */
  2111. this.max_lazy_match = 0;
  2112. /* Attempt to find a better match only when the current match is strictly
  2113. * smaller than this value. This mechanism is used only for compression
  2114. * levels >= 4.
  2115. */
  2116. // That's alias to max_lazy_match, don't use directly
  2117. //this.max_insert_length = 0;
  2118. /* Insert new strings in the hash table only if the match length is not
  2119. * greater than this length. This saves time but degrades compression.
  2120. * max_insert_length is used only for compression levels <= 3.
  2121. */
  2122. this.level = 0; /* compression level (1..9) */
  2123. this.strategy = 0; /* favor or force Huffman coding*/
  2124. this.good_match = 0;
  2125. /* Use a faster search when the previous match is longer than this */
  2126. this.nice_match = 0; /* Stop searching when current match exceeds this */
  2127. /* used by trees.c: */
  2128. /* Didn't use ct_data typedef below to suppress compiler warning */
  2129. // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  2130. // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  2131. // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  2132. // Use flat array of DOUBLE size, with interleaved fata,
  2133. // because JS does not support effective
  2134. this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
  2135. this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2);
  2136. this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2);
  2137. zero(this.dyn_ltree);
  2138. zero(this.dyn_dtree);
  2139. zero(this.bl_tree);
  2140. this.l_desc = null; /* desc. for literal tree */
  2141. this.d_desc = null; /* desc. for distance tree */
  2142. this.bl_desc = null; /* desc. for bit length tree */
  2143. //ush bl_count[MAX_BITS+1];
  2144. this.bl_count = new utils.Buf16(MAX_BITS + 1);
  2145. /* number of codes at each bit length for an optimal tree */
  2146. //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  2147. this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */
  2148. zero(this.heap);
  2149. this.heap_len = 0; /* number of elements in the heap */
  2150. this.heap_max = 0; /* element of largest frequency */
  2151. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  2152. * The same heap array is used to build all trees.
  2153. */
  2154. this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
  2155. zero(this.depth);
  2156. /* Depth of each subtree used as tie breaker for trees of equal frequency
  2157. */
  2158. this.l_buf = 0; /* buffer index for literals or lengths */
  2159. this.lit_bufsize = 0;
  2160. /* Size of match buffer for literals/lengths. There are 4 reasons for
  2161. * limiting lit_bufsize to 64K:
  2162. * - frequencies can be kept in 16 bit counters
  2163. * - if compression is not successful for the first block, all input
  2164. * data is still in the window so we can still emit a stored block even
  2165. * when input comes from standard input. (This can also be done for
  2166. * all blocks if lit_bufsize is not greater than 32K.)
  2167. * - if compression is not successful for a file smaller than 64K, we can
  2168. * even emit a stored file instead of a stored block (saving 5 bytes).
  2169. * This is applicable only for zip (not gzip or zlib).
  2170. * - creating new Huffman trees less frequently may not provide fast
  2171. * adaptation to changes in the input data statistics. (Take for
  2172. * example a binary file with poorly compressible code followed by
  2173. * a highly compressible string table.) Smaller buffer sizes give
  2174. * fast adaptation but have of course the overhead of transmitting
  2175. * trees more frequently.
  2176. * - I can't count above 4
  2177. */
  2178. this.last_lit = 0; /* running index in l_buf */
  2179. this.d_buf = 0;
  2180. /* Buffer index for distances. To simplify the code, d_buf and l_buf have
  2181. * the same number of elements. To use different lengths, an extra flag
  2182. * array would be necessary.
  2183. */
  2184. this.opt_len = 0; /* bit length of current block with optimal trees */
  2185. this.static_len = 0; /* bit length of current block with static trees */
  2186. this.matches = 0; /* number of string matches in current block */
  2187. this.insert = 0; /* bytes at end of window left to insert */
  2188. this.bi_buf = 0;
  2189. /* Output buffer. bits are inserted starting at the bottom (least
  2190. * significant bits).
  2191. */
  2192. this.bi_valid = 0;
  2193. /* Number of valid bits in bi_buf. All bits above the last valid bit
  2194. * are always zero.
  2195. */
  2196. // Used for window memory init. We safely ignore it for JS. That makes
  2197. // sense only for pointers and memory check tools.
  2198. //this.high_water = 0;
  2199. /* High water mark offset in window for initialized bytes -- bytes above
  2200. * this are set to zero in order to avoid memory check warnings when
  2201. * longest match routines access bytes past the input. This is then
  2202. * updated to the new high water mark.
  2203. */
  2204. }
  2205. function deflateResetKeep(strm) {
  2206. var s;
  2207. if (!strm || !strm.state) {
  2208. return err(strm, Z_STREAM_ERROR);
  2209. }
  2210. strm.total_in = strm.total_out = 0;
  2211. strm.data_type = Z_UNKNOWN;
  2212. s = strm.state;
  2213. s.pending = 0;
  2214. s.pending_out = 0;
  2215. if (s.wrap < 0) {
  2216. s.wrap = -s.wrap;
  2217. /* was made negative by deflate(..., Z_FINISH); */
  2218. }
  2219. s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
  2220. strm.adler = (s.wrap === 2) ?
  2221. 0 // crc32(0, Z_NULL, 0)
  2222. :
  2223. 1; // adler32(0, Z_NULL, 0)
  2224. s.last_flush = Z_NO_FLUSH;
  2225. trees._tr_init(s);
  2226. return Z_OK;
  2227. }
  2228. function deflateReset(strm) {
  2229. var ret = deflateResetKeep(strm);
  2230. if (ret === Z_OK) {
  2231. lm_init(strm.state);
  2232. }
  2233. return ret;
  2234. }
  2235. function deflateSetHeader(strm, head) {
  2236. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  2237. if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
  2238. strm.state.gzhead = head;
  2239. return Z_OK;
  2240. }
  2241. function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
  2242. if (!strm) { // === Z_NULL
  2243. return Z_STREAM_ERROR;
  2244. }
  2245. var wrap = 1;
  2246. if (level === Z_DEFAULT_COMPRESSION) {
  2247. level = 6;
  2248. }
  2249. if (windowBits < 0) { /* suppress zlib wrapper */
  2250. wrap = 0;
  2251. windowBits = -windowBits;
  2252. }
  2253. else if (windowBits > 15) {
  2254. wrap = 2; /* write gzip wrapper instead */
  2255. windowBits -= 16;
  2256. }
  2257. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
  2258. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  2259. strategy < 0 || strategy > Z_FIXED) {
  2260. return err(strm, Z_STREAM_ERROR);
  2261. }
  2262. if (windowBits === 8) {
  2263. windowBits = 9;
  2264. }
  2265. /* until 256-byte window bug fixed */
  2266. var s = new DeflateState();
  2267. strm.state = s;
  2268. s.strm = strm;
  2269. s.wrap = wrap;
  2270. s.gzhead = null;
  2271. s.w_bits = windowBits;
  2272. s.w_size = 1 << s.w_bits;
  2273. s.w_mask = s.w_size - 1;
  2274. s.hash_bits = memLevel + 7;
  2275. s.hash_size = 1 << s.hash_bits;
  2276. s.hash_mask = s.hash_size - 1;
  2277. s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  2278. s.window = new utils.Buf8(s.w_size * 2);
  2279. s.head = new utils.Buf16(s.hash_size);
  2280. s.prev = new utils.Buf16(s.w_size);
  2281. // Don't need mem init magic for JS.
  2282. //s.high_water = 0; /* nothing written to s->window yet */
  2283. s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  2284. s.pending_buf_size = s.lit_bufsize * 4;
  2285. //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  2286. //s->pending_buf = (uchf *) overlay;
  2287. s.pending_buf = new utils.Buf8(s.pending_buf_size);
  2288. // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
  2289. //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  2290. s.d_buf = 1 * s.lit_bufsize;
  2291. //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  2292. s.l_buf = (1 + 2) * s.lit_bufsize;
  2293. s.level = level;
  2294. s.strategy = strategy;
  2295. s.method = method;
  2296. return deflateReset(strm);
  2297. }
  2298. function deflateInit(strm, level) {
  2299. return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  2300. }
  2301. function deflate(strm, flush) {
  2302. var old_flush, s;
  2303. var beg, val; // for gzip header write only
  2304. if (!strm || !strm.state ||
  2305. flush > Z_BLOCK || flush < 0) {
  2306. return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
  2307. }
  2308. s = strm.state;
  2309. if (!strm.output ||
  2310. (!strm.input && strm.avail_in !== 0) ||
  2311. (s.status === FINISH_STATE && flush !== Z_FINISH)) {
  2312. return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
  2313. }
  2314. s.strm = strm; /* just in case */
  2315. old_flush = s.last_flush;
  2316. s.last_flush = flush;
  2317. /* Write the header */
  2318. if (s.status === INIT_STATE) {
  2319. if (s.wrap === 2) { // GZIP header
  2320. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2321. put_byte(s, 31);
  2322. put_byte(s, 139);
  2323. put_byte(s, 8);
  2324. if (!s.gzhead) { // s->gzhead == Z_NULL
  2325. put_byte(s, 0);
  2326. put_byte(s, 0);
  2327. put_byte(s, 0);
  2328. put_byte(s, 0);
  2329. put_byte(s, 0);
  2330. put_byte(s, s.level === 9 ? 2 :
  2331. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  2332. 4 : 0));
  2333. put_byte(s, OS_CODE);
  2334. s.status = BUSY_STATE;
  2335. }
  2336. else {
  2337. put_byte(s, (s.gzhead.text ? 1 : 0) +
  2338. (s.gzhead.hcrc ? 2 : 0) +
  2339. (!s.gzhead.extra ? 0 : 4) +
  2340. (!s.gzhead.name ? 0 : 8) +
  2341. (!s.gzhead.comment ? 0 : 16)
  2342. );
  2343. put_byte(s, s.gzhead.time & 0xff);
  2344. put_byte(s, (s.gzhead.time >> 8) & 0xff);
  2345. put_byte(s, (s.gzhead.time >> 16) & 0xff);
  2346. put_byte(s, (s.gzhead.time >> 24) & 0xff);
  2347. put_byte(s, s.level === 9 ? 2 :
  2348. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  2349. 4 : 0));
  2350. put_byte(s, s.gzhead.os & 0xff);
  2351. if (s.gzhead.extra && s.gzhead.extra.length) {
  2352. put_byte(s, s.gzhead.extra.length & 0xff);
  2353. put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
  2354. }
  2355. if (s.gzhead.hcrc) {
  2356. strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
  2357. }
  2358. s.gzindex = 0;
  2359. s.status = EXTRA_STATE;
  2360. }
  2361. }
  2362. else // DEFLATE header
  2363. {
  2364. var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
  2365. var level_flags = -1;
  2366. if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
  2367. level_flags = 0;
  2368. } else if (s.level < 6) {
  2369. level_flags = 1;
  2370. } else if (s.level === 6) {
  2371. level_flags = 2;
  2372. } else {
  2373. level_flags = 3;
  2374. }
  2375. header |= (level_flags << 6);
  2376. if (s.strstart !== 0) { header |= PRESET_DICT; }
  2377. header += 31 - (header % 31);
  2378. s.status = BUSY_STATE;
  2379. putShortMSB(s, header);
  2380. /* Save the adler32 of the preset dictionary: */
  2381. if (s.strstart !== 0) {
  2382. putShortMSB(s, strm.adler >>> 16);
  2383. putShortMSB(s, strm.adler & 0xffff);
  2384. }
  2385. strm.adler = 1; // adler32(0L, Z_NULL, 0);
  2386. }
  2387. }
  2388. //#ifdef GZIP
  2389. if (s.status === EXTRA_STATE) {
  2390. if (s.gzhead.extra/* != Z_NULL*/) {
  2391. beg = s.pending; /* start of bytes to update crc */
  2392. while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
  2393. if (s.pending === s.pending_buf_size) {
  2394. if (s.gzhead.hcrc && s.pending > beg) {
  2395. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2396. }
  2397. flush_pending(strm);
  2398. beg = s.pending;
  2399. if (s.pending === s.pending_buf_size) {
  2400. break;
  2401. }
  2402. }
  2403. put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
  2404. s.gzindex++;
  2405. }
  2406. if (s.gzhead.hcrc && s.pending > beg) {
  2407. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2408. }
  2409. if (s.gzindex === s.gzhead.extra.length) {
  2410. s.gzindex = 0;
  2411. s.status = NAME_STATE;
  2412. }
  2413. }
  2414. else {
  2415. s.status = NAME_STATE;
  2416. }
  2417. }
  2418. if (s.status === NAME_STATE) {
  2419. if (s.gzhead.name/* != Z_NULL*/) {
  2420. beg = s.pending; /* start of bytes to update crc */
  2421. //int val;
  2422. do {
  2423. if (s.pending === s.pending_buf_size) {
  2424. if (s.gzhead.hcrc && s.pending > beg) {
  2425. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2426. }
  2427. flush_pending(strm);
  2428. beg = s.pending;
  2429. if (s.pending === s.pending_buf_size) {
  2430. val = 1;
  2431. break;
  2432. }
  2433. }
  2434. // JS specific: little magic to add zero terminator to end of string
  2435. if (s.gzindex < s.gzhead.name.length) {
  2436. val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
  2437. } else {
  2438. val = 0;
  2439. }
  2440. put_byte(s, val);
  2441. } while (val !== 0);
  2442. if (s.gzhead.hcrc && s.pending > beg) {
  2443. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2444. }
  2445. if (val === 0) {
  2446. s.gzindex = 0;
  2447. s.status = COMMENT_STATE;
  2448. }
  2449. }
  2450. else {
  2451. s.status = COMMENT_STATE;
  2452. }
  2453. }
  2454. if (s.status === COMMENT_STATE) {
  2455. if (s.gzhead.comment/* != Z_NULL*/) {
  2456. beg = s.pending; /* start of bytes to update crc */
  2457. //int val;
  2458. do {
  2459. if (s.pending === s.pending_buf_size) {
  2460. if (s.gzhead.hcrc && s.pending > beg) {
  2461. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2462. }
  2463. flush_pending(strm);
  2464. beg = s.pending;
  2465. if (s.pending === s.pending_buf_size) {
  2466. val = 1;
  2467. break;
  2468. }
  2469. }
  2470. // JS specific: little magic to add zero terminator to end of string
  2471. if (s.gzindex < s.gzhead.comment.length) {
  2472. val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
  2473. } else {
  2474. val = 0;
  2475. }
  2476. put_byte(s, val);
  2477. } while (val !== 0);
  2478. if (s.gzhead.hcrc && s.pending > beg) {
  2479. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  2480. }
  2481. if (val === 0) {
  2482. s.status = HCRC_STATE;
  2483. }
  2484. }
  2485. else {
  2486. s.status = HCRC_STATE;
  2487. }
  2488. }
  2489. if (s.status === HCRC_STATE) {
  2490. if (s.gzhead.hcrc) {
  2491. if (s.pending + 2 > s.pending_buf_size) {
  2492. flush_pending(strm);
  2493. }
  2494. if (s.pending + 2 <= s.pending_buf_size) {
  2495. put_byte(s, strm.adler & 0xff);
  2496. put_byte(s, (strm.adler >> 8) & 0xff);
  2497. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2498. s.status = BUSY_STATE;
  2499. }
  2500. }
  2501. else {
  2502. s.status = BUSY_STATE;
  2503. }
  2504. }
  2505. //#endif
  2506. /* Flush as much pending output as possible */
  2507. if (s.pending !== 0) {
  2508. flush_pending(strm);
  2509. if (strm.avail_out === 0) {
  2510. /* Since avail_out is 0, deflate will be called again with
  2511. * more output space, but possibly with both pending and
  2512. * avail_in equal to zero. There won't be anything to do,
  2513. * but this is not an error situation so make sure we
  2514. * return OK instead of BUF_ERROR at next call of deflate:
  2515. */
  2516. s.last_flush = -1;
  2517. return Z_OK;
  2518. }
  2519. /* Make sure there is something to do and avoid duplicate consecutive
  2520. * flushes. For repeated and useless calls with Z_FINISH, we keep
  2521. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  2522. */
  2523. } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
  2524. flush !== Z_FINISH) {
  2525. return err(strm, Z_BUF_ERROR);
  2526. }
  2527. /* User must not provide more input after the first FINISH: */
  2528. if (s.status === FINISH_STATE && strm.avail_in !== 0) {
  2529. return err(strm, Z_BUF_ERROR);
  2530. }
  2531. /* Start a new block or continue the current one.
  2532. */
  2533. if (strm.avail_in !== 0 || s.lookahead !== 0 ||
  2534. (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
  2535. var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
  2536. (s.strategy === Z_RLE ? deflate_rle(s, flush) :
  2537. configuration_table[s.level].func(s, flush));
  2538. if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
  2539. s.status = FINISH_STATE;
  2540. }
  2541. if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
  2542. if (strm.avail_out === 0) {
  2543. s.last_flush = -1;
  2544. /* avoid BUF_ERROR next call, see above */
  2545. }
  2546. return Z_OK;
  2547. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  2548. * of deflate should use the same flush parameter to make sure
  2549. * that the flush is complete. So we don't have to output an
  2550. * empty block here, this will be done at next call. This also
  2551. * ensures that for a very small output buffer, we emit at most
  2552. * one empty block.
  2553. */
  2554. }
  2555. if (bstate === BS_BLOCK_DONE) {
  2556. if (flush === Z_PARTIAL_FLUSH) {
  2557. trees._tr_align(s);
  2558. }
  2559. else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
  2560. trees._tr_stored_block(s, 0, 0, false);
  2561. /* For a full flush, this empty block will be recognized
  2562. * as a special marker by inflate_sync().
  2563. */
  2564. if (flush === Z_FULL_FLUSH) {
  2565. /*** CLEAR_HASH(s); ***/ /* forget history */
  2566. zero(s.head); // Fill with NIL (= 0);
  2567. if (s.lookahead === 0) {
  2568. s.strstart = 0;
  2569. s.block_start = 0;
  2570. s.insert = 0;
  2571. }
  2572. }
  2573. }
  2574. flush_pending(strm);
  2575. if (strm.avail_out === 0) {
  2576. s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  2577. return Z_OK;
  2578. }
  2579. }
  2580. }
  2581. //Assert(strm->avail_out > 0, "bug2");
  2582. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  2583. if (flush !== Z_FINISH) { return Z_OK; }
  2584. if (s.wrap <= 0) { return Z_STREAM_END; }
  2585. /* Write the trailer */
  2586. if (s.wrap === 2) {
  2587. put_byte(s, strm.adler & 0xff);
  2588. put_byte(s, (strm.adler >> 8) & 0xff);
  2589. put_byte(s, (strm.adler >> 16) & 0xff);
  2590. put_byte(s, (strm.adler >> 24) & 0xff);
  2591. put_byte(s, strm.total_in & 0xff);
  2592. put_byte(s, (strm.total_in >> 8) & 0xff);
  2593. put_byte(s, (strm.total_in >> 16) & 0xff);
  2594. put_byte(s, (strm.total_in >> 24) & 0xff);
  2595. }
  2596. else
  2597. {
  2598. putShortMSB(s, strm.adler >>> 16);
  2599. putShortMSB(s, strm.adler & 0xffff);
  2600. }
  2601. flush_pending(strm);
  2602. /* If avail_out is zero, the application will call deflate again
  2603. * to flush the rest.
  2604. */
  2605. if (s.wrap > 0) { s.wrap = -s.wrap; }
  2606. /* write the trailer only once! */
  2607. return s.pending !== 0 ? Z_OK : Z_STREAM_END;
  2608. }
  2609. function deflateEnd(strm) {
  2610. var status;
  2611. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  2612. return Z_STREAM_ERROR;
  2613. }
  2614. status = strm.state.status;
  2615. if (status !== INIT_STATE &&
  2616. status !== EXTRA_STATE &&
  2617. status !== NAME_STATE &&
  2618. status !== COMMENT_STATE &&
  2619. status !== HCRC_STATE &&
  2620. status !== BUSY_STATE &&
  2621. status !== FINISH_STATE
  2622. ) {
  2623. return err(strm, Z_STREAM_ERROR);
  2624. }
  2625. strm.state = null;
  2626. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
  2627. }
  2628. /* =========================================================================
  2629. * Initializes the compression dictionary from the given byte
  2630. * sequence without producing any compressed output.
  2631. */
  2632. function deflateSetDictionary(strm, dictionary) {
  2633. var dictLength = dictionary.length;
  2634. var s;
  2635. var str, n;
  2636. var wrap;
  2637. var avail;
  2638. var next;
  2639. var input;
  2640. var tmpDict;
  2641. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  2642. return Z_STREAM_ERROR;
  2643. }
  2644. s = strm.state;
  2645. wrap = s.wrap;
  2646. if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
  2647. return Z_STREAM_ERROR;
  2648. }
  2649. /* when using zlib wrappers, compute Adler-32 for provided dictionary */
  2650. if (wrap === 1) {
  2651. /* adler32(strm->adler, dictionary, dictLength); */
  2652. strm.adler = adler32(strm.adler, dictionary, dictLength, 0);
  2653. }
  2654. s.wrap = 0; /* avoid computing Adler-32 in read_buf */
  2655. /* if dictionary would fill window, just replace the history */
  2656. if (dictLength >= s.w_size) {
  2657. if (wrap === 0) { /* already empty otherwise */
  2658. /*** CLEAR_HASH(s); ***/
  2659. zero(s.head); // Fill with NIL (= 0);
  2660. s.strstart = 0;
  2661. s.block_start = 0;
  2662. s.insert = 0;
  2663. }
  2664. /* use the tail */
  2665. // dictionary = dictionary.slice(dictLength - s.w_size);
  2666. tmpDict = new utils.Buf8(s.w_size);
  2667. utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0);
  2668. dictionary = tmpDict;
  2669. dictLength = s.w_size;
  2670. }
  2671. /* insert dictionary into window and hash */
  2672. avail = strm.avail_in;
  2673. next = strm.next_in;
  2674. input = strm.input;
  2675. strm.avail_in = dictLength;
  2676. strm.next_in = 0;
  2677. strm.input = dictionary;
  2678. fill_window(s);
  2679. while (s.lookahead >= MIN_MATCH) {
  2680. str = s.strstart;
  2681. n = s.lookahead - (MIN_MATCH - 1);
  2682. do {
  2683. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  2684. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask;
  2685. s.prev[str & s.w_mask] = s.head[s.ins_h];
  2686. s.head[s.ins_h] = str;
  2687. str++;
  2688. } while (--n);
  2689. s.strstart = str;
  2690. s.lookahead = MIN_MATCH - 1;
  2691. fill_window(s);
  2692. }
  2693. s.strstart += s.lookahead;
  2694. s.block_start = s.strstart;
  2695. s.insert = s.lookahead;
  2696. s.lookahead = 0;
  2697. s.match_length = s.prev_length = MIN_MATCH - 1;
  2698. s.match_available = 0;
  2699. strm.next_in = next;
  2700. strm.input = input;
  2701. strm.avail_in = avail;
  2702. s.wrap = wrap;
  2703. return Z_OK;
  2704. }
  2705. exports.deflateInit = deflateInit;
  2706. exports.deflateInit2 = deflateInit2;
  2707. exports.deflateReset = deflateReset;
  2708. exports.deflateResetKeep = deflateResetKeep;
  2709. exports.deflateSetHeader = deflateSetHeader;
  2710. exports.deflate = deflate;
  2711. exports.deflateEnd = deflateEnd;
  2712. exports.deflateSetDictionary = deflateSetDictionary;
  2713. exports.deflateInfo = 'pako deflate (from Nodeca project)';
  2714. /* Not implemented
  2715. exports.deflateBound = deflateBound;
  2716. exports.deflateCopy = deflateCopy;
  2717. exports.deflateParams = deflateParams;
  2718. exports.deflatePending = deflatePending;
  2719. exports.deflatePrime = deflatePrime;
  2720. exports.deflateTune = deflateTune;
  2721. */
  2722. },{"../utils/common":3,"./adler32":5,"./crc32":7,"./messages":13,"./trees":14}],9:[function(require,module,exports){
  2723. 'use strict';
  2724. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2725. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2726. //
  2727. // This software is provided 'as-is', without any express or implied
  2728. // warranty. In no event will the authors be held liable for any damages
  2729. // arising from the use of this software.
  2730. //
  2731. // Permission is granted to anyone to use this software for any purpose,
  2732. // including commercial applications, and to alter it and redistribute it
  2733. // freely, subject to the following restrictions:
  2734. //
  2735. // 1. The origin of this software must not be misrepresented; you must not
  2736. // claim that you wrote the original software. If you use this software
  2737. // in a product, an acknowledgment in the product documentation would be
  2738. // appreciated but is not required.
  2739. // 2. Altered source versions must be plainly marked as such, and must not be
  2740. // misrepresented as being the original software.
  2741. // 3. This notice may not be removed or altered from any source distribution.
  2742. function GZheader() {
  2743. /* true if compressed data believed to be text */
  2744. this.text = 0;
  2745. /* modification time */
  2746. this.time = 0;
  2747. /* extra flags (not used when writing a gzip file) */
  2748. this.xflags = 0;
  2749. /* operating system */
  2750. this.os = 0;
  2751. /* pointer to extra field or Z_NULL if none */
  2752. this.extra = null;
  2753. /* extra field length (valid if extra != Z_NULL) */
  2754. this.extra_len = 0; // Actually, we don't need it in JS,
  2755. // but leave for few code modifications
  2756. //
  2757. // Setup limits is not necessary because in js we should not preallocate memory
  2758. // for inflate use constant limit in 65536 bytes
  2759. //
  2760. /* space at extra (only when reading header) */
  2761. // this.extra_max = 0;
  2762. /* pointer to zero-terminated file name or Z_NULL */
  2763. this.name = '';
  2764. /* space at name (only when reading header) */
  2765. // this.name_max = 0;
  2766. /* pointer to zero-terminated comment or Z_NULL */
  2767. this.comment = '';
  2768. /* space at comment (only when reading header) */
  2769. // this.comm_max = 0;
  2770. /* true if there was or will be a header crc */
  2771. this.hcrc = 0;
  2772. /* true when done reading gzip header (not used when writing a gzip file) */
  2773. this.done = false;
  2774. }
  2775. module.exports = GZheader;
  2776. },{}],10:[function(require,module,exports){
  2777. 'use strict';
  2778. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2779. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2780. //
  2781. // This software is provided 'as-is', without any express or implied
  2782. // warranty. In no event will the authors be held liable for any damages
  2783. // arising from the use of this software.
  2784. //
  2785. // Permission is granted to anyone to use this software for any purpose,
  2786. // including commercial applications, and to alter it and redistribute it
  2787. // freely, subject to the following restrictions:
  2788. //
  2789. // 1. The origin of this software must not be misrepresented; you must not
  2790. // claim that you wrote the original software. If you use this software
  2791. // in a product, an acknowledgment in the product documentation would be
  2792. // appreciated but is not required.
  2793. // 2. Altered source versions must be plainly marked as such, and must not be
  2794. // misrepresented as being the original software.
  2795. // 3. This notice may not be removed or altered from any source distribution.
  2796. // See state defs from inflate.js
  2797. var BAD = 30; /* got a data error -- remain here until reset */
  2798. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  2799. /*
  2800. Decode literal, length, and distance codes and write out the resulting
  2801. literal and match bytes until either not enough input or output is
  2802. available, an end-of-block is encountered, or a data error is encountered.
  2803. When large enough input and output buffers are supplied to inflate(), for
  2804. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  2805. inflate execution time is spent in this routine.
  2806. Entry assumptions:
  2807. state.mode === LEN
  2808. strm.avail_in >= 6
  2809. strm.avail_out >= 258
  2810. start >= strm.avail_out
  2811. state.bits < 8
  2812. On return, state.mode is one of:
  2813. LEN -- ran out of enough output space or enough available input
  2814. TYPE -- reached end of block code, inflate() to interpret next block
  2815. BAD -- error in block data
  2816. Notes:
  2817. - The maximum input bits used by a length/distance pair is 15 bits for the
  2818. length code, 5 bits for the length extra, 15 bits for the distance code,
  2819. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  2820. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  2821. checking for available input while decoding.
  2822. - The maximum bytes that a single length/distance pair can output is 258
  2823. bytes, which is the maximum length that can be coded. inflate_fast()
  2824. requires strm.avail_out >= 258 for each loop to avoid checking for
  2825. output space.
  2826. */
  2827. module.exports = function inflate_fast(strm, start) {
  2828. var state;
  2829. var _in; /* local strm.input */
  2830. var last; /* have enough input while in < last */
  2831. var _out; /* local strm.output */
  2832. var beg; /* inflate()'s initial strm.output */
  2833. var end; /* while out < end, enough space available */
  2834. //#ifdef INFLATE_STRICT
  2835. var dmax; /* maximum distance from zlib header */
  2836. //#endif
  2837. var wsize; /* window size or zero if not using window */
  2838. var whave; /* valid bytes in the window */
  2839. var wnext; /* window write index */
  2840. // Use `s_window` instead `window`, avoid conflict with instrumentation tools
  2841. var s_window; /* allocated sliding window, if wsize != 0 */
  2842. var hold; /* local strm.hold */
  2843. var bits; /* local strm.bits */
  2844. var lcode; /* local strm.lencode */
  2845. var dcode; /* local strm.distcode */
  2846. var lmask; /* mask for first level of length codes */
  2847. var dmask; /* mask for first level of distance codes */
  2848. var here; /* retrieved table entry */
  2849. var op; /* code bits, operation, extra bits, or */
  2850. /* window position, window bytes to copy */
  2851. var len; /* match length, unused bytes */
  2852. var dist; /* match distance */
  2853. var from; /* where to copy match from */
  2854. var from_source;
  2855. var input, output; // JS specific, because we have no pointers
  2856. /* copy state to local variables */
  2857. state = strm.state;
  2858. //here = state.here;
  2859. _in = strm.next_in;
  2860. input = strm.input;
  2861. last = _in + (strm.avail_in - 5);
  2862. _out = strm.next_out;
  2863. output = strm.output;
  2864. beg = _out - (start - strm.avail_out);
  2865. end = _out + (strm.avail_out - 257);
  2866. //#ifdef INFLATE_STRICT
  2867. dmax = state.dmax;
  2868. //#endif
  2869. wsize = state.wsize;
  2870. whave = state.whave;
  2871. wnext = state.wnext;
  2872. s_window = state.window;
  2873. hold = state.hold;
  2874. bits = state.bits;
  2875. lcode = state.lencode;
  2876. dcode = state.distcode;
  2877. lmask = (1 << state.lenbits) - 1;
  2878. dmask = (1 << state.distbits) - 1;
  2879. /* decode literals and length/distances until end-of-block or not enough
  2880. input data or output space */
  2881. top:
  2882. do {
  2883. if (bits < 15) {
  2884. hold += input[_in++] << bits;
  2885. bits += 8;
  2886. hold += input[_in++] << bits;
  2887. bits += 8;
  2888. }
  2889. here = lcode[hold & lmask];
  2890. dolen:
  2891. for (;;) { // Goto emulation
  2892. op = here >>> 24/*here.bits*/;
  2893. hold >>>= op;
  2894. bits -= op;
  2895. op = (here >>> 16) & 0xff/*here.op*/;
  2896. if (op === 0) { /* literal */
  2897. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  2898. // "inflate: literal '%c'\n" :
  2899. // "inflate: literal 0x%02x\n", here.val));
  2900. output[_out++] = here & 0xffff/*here.val*/;
  2901. }
  2902. else if (op & 16) { /* length base */
  2903. len = here & 0xffff/*here.val*/;
  2904. op &= 15; /* number of extra bits */
  2905. if (op) {
  2906. if (bits < op) {
  2907. hold += input[_in++] << bits;
  2908. bits += 8;
  2909. }
  2910. len += hold & ((1 << op) - 1);
  2911. hold >>>= op;
  2912. bits -= op;
  2913. }
  2914. //Tracevv((stderr, "inflate: length %u\n", len));
  2915. if (bits < 15) {
  2916. hold += input[_in++] << bits;
  2917. bits += 8;
  2918. hold += input[_in++] << bits;
  2919. bits += 8;
  2920. }
  2921. here = dcode[hold & dmask];
  2922. dodist:
  2923. for (;;) { // goto emulation
  2924. op = here >>> 24/*here.bits*/;
  2925. hold >>>= op;
  2926. bits -= op;
  2927. op = (here >>> 16) & 0xff/*here.op*/;
  2928. if (op & 16) { /* distance base */
  2929. dist = here & 0xffff/*here.val*/;
  2930. op &= 15; /* number of extra bits */
  2931. if (bits < op) {
  2932. hold += input[_in++] << bits;
  2933. bits += 8;
  2934. if (bits < op) {
  2935. hold += input[_in++] << bits;
  2936. bits += 8;
  2937. }
  2938. }
  2939. dist += hold & ((1 << op) - 1);
  2940. //#ifdef INFLATE_STRICT
  2941. if (dist > dmax) {
  2942. strm.msg = 'invalid distance too far back';
  2943. state.mode = BAD;
  2944. break top;
  2945. }
  2946. //#endif
  2947. hold >>>= op;
  2948. bits -= op;
  2949. //Tracevv((stderr, "inflate: distance %u\n", dist));
  2950. op = _out - beg; /* max distance in output */
  2951. if (dist > op) { /* see if copy from window */
  2952. op = dist - op; /* distance back in window */
  2953. if (op > whave) {
  2954. if (state.sane) {
  2955. strm.msg = 'invalid distance too far back';
  2956. state.mode = BAD;
  2957. break top;
  2958. }
  2959. // (!) This block is disabled in zlib defaults,
  2960. // don't enable it for binary compatibility
  2961. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  2962. // if (len <= op - whave) {
  2963. // do {
  2964. // output[_out++] = 0;
  2965. // } while (--len);
  2966. // continue top;
  2967. // }
  2968. // len -= op - whave;
  2969. // do {
  2970. // output[_out++] = 0;
  2971. // } while (--op > whave);
  2972. // if (op === 0) {
  2973. // from = _out - dist;
  2974. // do {
  2975. // output[_out++] = output[from++];
  2976. // } while (--len);
  2977. // continue top;
  2978. // }
  2979. //#endif
  2980. }
  2981. from = 0; // window index
  2982. from_source = s_window;
  2983. if (wnext === 0) { /* very common case */
  2984. from += wsize - op;
  2985. if (op < len) { /* some from window */
  2986. len -= op;
  2987. do {
  2988. output[_out++] = s_window[from++];
  2989. } while (--op);
  2990. from = _out - dist; /* rest from output */
  2991. from_source = output;
  2992. }
  2993. }
  2994. else if (wnext < op) { /* wrap around window */
  2995. from += wsize + wnext - op;
  2996. op -= wnext;
  2997. if (op < len) { /* some from end of window */
  2998. len -= op;
  2999. do {
  3000. output[_out++] = s_window[from++];
  3001. } while (--op);
  3002. from = 0;
  3003. if (wnext < len) { /* some from start of window */
  3004. op = wnext;
  3005. len -= op;
  3006. do {
  3007. output[_out++] = s_window[from++];
  3008. } while (--op);
  3009. from = _out - dist; /* rest from output */
  3010. from_source = output;
  3011. }
  3012. }
  3013. }
  3014. else { /* contiguous in window */
  3015. from += wnext - op;
  3016. if (op < len) { /* some from window */
  3017. len -= op;
  3018. do {
  3019. output[_out++] = s_window[from++];
  3020. } while (--op);
  3021. from = _out - dist; /* rest from output */
  3022. from_source = output;
  3023. }
  3024. }
  3025. while (len > 2) {
  3026. output[_out++] = from_source[from++];
  3027. output[_out++] = from_source[from++];
  3028. output[_out++] = from_source[from++];
  3029. len -= 3;
  3030. }
  3031. if (len) {
  3032. output[_out++] = from_source[from++];
  3033. if (len > 1) {
  3034. output[_out++] = from_source[from++];
  3035. }
  3036. }
  3037. }
  3038. else {
  3039. from = _out - dist; /* copy direct from output */
  3040. do { /* minimum length is three */
  3041. output[_out++] = output[from++];
  3042. output[_out++] = output[from++];
  3043. output[_out++] = output[from++];
  3044. len -= 3;
  3045. } while (len > 2);
  3046. if (len) {
  3047. output[_out++] = output[from++];
  3048. if (len > 1) {
  3049. output[_out++] = output[from++];
  3050. }
  3051. }
  3052. }
  3053. }
  3054. else if ((op & 64) === 0) { /* 2nd level distance code */
  3055. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  3056. continue dodist;
  3057. }
  3058. else {
  3059. strm.msg = 'invalid distance code';
  3060. state.mode = BAD;
  3061. break top;
  3062. }
  3063. break; // need to emulate goto via "continue"
  3064. }
  3065. }
  3066. else if ((op & 64) === 0) { /* 2nd level length code */
  3067. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  3068. continue dolen;
  3069. }
  3070. else if (op & 32) { /* end-of-block */
  3071. //Tracevv((stderr, "inflate: end of block\n"));
  3072. state.mode = TYPE;
  3073. break top;
  3074. }
  3075. else {
  3076. strm.msg = 'invalid literal/length code';
  3077. state.mode = BAD;
  3078. break top;
  3079. }
  3080. break; // need to emulate goto via "continue"
  3081. }
  3082. } while (_in < last && _out < end);
  3083. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  3084. len = bits >> 3;
  3085. _in -= len;
  3086. bits -= len << 3;
  3087. hold &= (1 << bits) - 1;
  3088. /* update state and return */
  3089. strm.next_in = _in;
  3090. strm.next_out = _out;
  3091. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  3092. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  3093. state.hold = hold;
  3094. state.bits = bits;
  3095. return;
  3096. };
  3097. },{}],11:[function(require,module,exports){
  3098. 'use strict';
  3099. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  3100. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  3101. //
  3102. // This software is provided 'as-is', without any express or implied
  3103. // warranty. In no event will the authors be held liable for any damages
  3104. // arising from the use of this software.
  3105. //
  3106. // Permission is granted to anyone to use this software for any purpose,
  3107. // including commercial applications, and to alter it and redistribute it
  3108. // freely, subject to the following restrictions:
  3109. //
  3110. // 1. The origin of this software must not be misrepresented; you must not
  3111. // claim that you wrote the original software. If you use this software
  3112. // in a product, an acknowledgment in the product documentation would be
  3113. // appreciated but is not required.
  3114. // 2. Altered source versions must be plainly marked as such, and must not be
  3115. // misrepresented as being the original software.
  3116. // 3. This notice may not be removed or altered from any source distribution.
  3117. var utils = require('../utils/common');
  3118. var adler32 = require('./adler32');
  3119. var crc32 = require('./crc32');
  3120. var inflate_fast = require('./inffast');
  3121. var inflate_table = require('./inftrees');
  3122. var CODES = 0;
  3123. var LENS = 1;
  3124. var DISTS = 2;
  3125. /* Public constants ==========================================================*/
  3126. /* ===========================================================================*/
  3127. /* Allowed flush values; see deflate() and inflate() below for details */
  3128. //var Z_NO_FLUSH = 0;
  3129. //var Z_PARTIAL_FLUSH = 1;
  3130. //var Z_SYNC_FLUSH = 2;
  3131. //var Z_FULL_FLUSH = 3;
  3132. var Z_FINISH = 4;
  3133. var Z_BLOCK = 5;
  3134. var Z_TREES = 6;
  3135. /* Return codes for the compression/decompression functions. Negative values
  3136. * are errors, positive values are used for special but normal events.
  3137. */
  3138. var Z_OK = 0;
  3139. var Z_STREAM_END = 1;
  3140. var Z_NEED_DICT = 2;
  3141. //var Z_ERRNO = -1;
  3142. var Z_STREAM_ERROR = -2;
  3143. var Z_DATA_ERROR = -3;
  3144. var Z_MEM_ERROR = -4;
  3145. var Z_BUF_ERROR = -5;
  3146. //var Z_VERSION_ERROR = -6;
  3147. /* The deflate compression method */
  3148. var Z_DEFLATED = 8;
  3149. /* STATES ====================================================================*/
  3150. /* ===========================================================================*/
  3151. var HEAD = 1; /* i: waiting for magic header */
  3152. var FLAGS = 2; /* i: waiting for method and flags (gzip) */
  3153. var TIME = 3; /* i: waiting for modification time (gzip) */
  3154. var OS = 4; /* i: waiting for extra flags and operating system (gzip) */
  3155. var EXLEN = 5; /* i: waiting for extra length (gzip) */
  3156. var EXTRA = 6; /* i: waiting for extra bytes (gzip) */
  3157. var NAME = 7; /* i: waiting for end of file name (gzip) */
  3158. var COMMENT = 8; /* i: waiting for end of comment (gzip) */
  3159. var HCRC = 9; /* i: waiting for header crc (gzip) */
  3160. var DICTID = 10; /* i: waiting for dictionary check value */
  3161. var DICT = 11; /* waiting for inflateSetDictionary() call */
  3162. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  3163. var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
  3164. var STORED = 14; /* i: waiting for stored size (length and complement) */
  3165. var COPY_ = 15; /* i/o: same as COPY below, but only first time in */
  3166. var COPY = 16; /* i/o: waiting for input or output to copy stored block */
  3167. var TABLE = 17; /* i: waiting for dynamic block table lengths */
  3168. var LENLENS = 18; /* i: waiting for code length code lengths */
  3169. var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
  3170. var LEN_ = 20; /* i: same as LEN below, but only first time in */
  3171. var LEN = 21; /* i: waiting for length/lit/eob code */
  3172. var LENEXT = 22; /* i: waiting for length extra bits */
  3173. var DIST = 23; /* i: waiting for distance code */
  3174. var DISTEXT = 24; /* i: waiting for distance extra bits */
  3175. var MATCH = 25; /* o: waiting for output space to copy string */
  3176. var LIT = 26; /* o: waiting for output space to write literal */
  3177. var CHECK = 27; /* i: waiting for 32-bit check value */
  3178. var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
  3179. var DONE = 29; /* finished check, done -- remain here until reset */
  3180. var BAD = 30; /* got a data error -- remain here until reset */
  3181. var MEM = 31; /* got an inflate() memory error -- remain here until reset */
  3182. var SYNC = 32; /* looking for synchronization bytes to restart inflate() */
  3183. /* ===========================================================================*/
  3184. var ENOUGH_LENS = 852;
  3185. var ENOUGH_DISTS = 592;
  3186. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  3187. var MAX_WBITS = 15;
  3188. /* 32K LZ77 window */
  3189. var DEF_WBITS = MAX_WBITS;
  3190. function zswap32(q) {
  3191. return (((q >>> 24) & 0xff) +
  3192. ((q >>> 8) & 0xff00) +
  3193. ((q & 0xff00) << 8) +
  3194. ((q & 0xff) << 24));
  3195. }
  3196. function InflateState() {
  3197. this.mode = 0; /* current inflate mode */
  3198. this.last = false; /* true if processing last block */
  3199. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  3200. this.havedict = false; /* true if dictionary provided */
  3201. this.flags = 0; /* gzip header method and flags (0 if zlib) */
  3202. this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */
  3203. this.check = 0; /* protected copy of check value */
  3204. this.total = 0; /* protected copy of output count */
  3205. // TODO: may be {}
  3206. this.head = null; /* where to save gzip header information */
  3207. /* sliding window */
  3208. this.wbits = 0; /* log base 2 of requested window size */
  3209. this.wsize = 0; /* window size or zero if not using window */
  3210. this.whave = 0; /* valid bytes in the window */
  3211. this.wnext = 0; /* window write index */
  3212. this.window = null; /* allocated sliding window, if needed */
  3213. /* bit accumulator */
  3214. this.hold = 0; /* input bit accumulator */
  3215. this.bits = 0; /* number of bits in "in" */
  3216. /* for string and stored block copying */
  3217. this.length = 0; /* literal or length of data to copy */
  3218. this.offset = 0; /* distance back to copy string from */
  3219. /* for table and code decoding */
  3220. this.extra = 0; /* extra bits needed */
  3221. /* fixed and dynamic code tables */
  3222. this.lencode = null; /* starting table for length/literal codes */
  3223. this.distcode = null; /* starting table for distance codes */
  3224. this.lenbits = 0; /* index bits for lencode */
  3225. this.distbits = 0; /* index bits for distcode */
  3226. /* dynamic table building */
  3227. this.ncode = 0; /* number of code length code lengths */
  3228. this.nlen = 0; /* number of length code lengths */
  3229. this.ndist = 0; /* number of distance code lengths */
  3230. this.have = 0; /* number of code lengths in lens[] */
  3231. this.next = null; /* next available space in codes[] */
  3232. this.lens = new utils.Buf16(320); /* temporary storage for code lengths */
  3233. this.work = new utils.Buf16(288); /* work area for code table building */
  3234. /*
  3235. because we don't have pointers in js, we use lencode and distcode directly
  3236. as buffers so we don't need codes
  3237. */
  3238. //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */
  3239. this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
  3240. this.distdyn = null; /* dynamic table for distance codes (JS specific) */
  3241. this.sane = 0; /* if false, allow invalid distance too far */
  3242. this.back = 0; /* bits back of last unprocessed length/lit */
  3243. this.was = 0; /* initial length of match */
  3244. }
  3245. function inflateResetKeep(strm) {
  3246. var state;
  3247. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  3248. state = strm.state;
  3249. strm.total_in = strm.total_out = state.total = 0;
  3250. strm.msg = ''; /*Z_NULL*/
  3251. if (state.wrap) { /* to support ill-conceived Java test suite */
  3252. strm.adler = state.wrap & 1;
  3253. }
  3254. state.mode = HEAD;
  3255. state.last = 0;
  3256. state.havedict = 0;
  3257. state.dmax = 32768;
  3258. state.head = null/*Z_NULL*/;
  3259. state.hold = 0;
  3260. state.bits = 0;
  3261. //state.lencode = state.distcode = state.next = state.codes;
  3262. state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS);
  3263. state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS);
  3264. state.sane = 1;
  3265. state.back = -1;
  3266. //Tracev((stderr, "inflate: reset\n"));
  3267. return Z_OK;
  3268. }
  3269. function inflateReset(strm) {
  3270. var state;
  3271. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  3272. state = strm.state;
  3273. state.wsize = 0;
  3274. state.whave = 0;
  3275. state.wnext = 0;
  3276. return inflateResetKeep(strm);
  3277. }
  3278. function inflateReset2(strm, windowBits) {
  3279. var wrap;
  3280. var state;
  3281. /* get the state */
  3282. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  3283. state = strm.state;
  3284. /* extract wrap request from windowBits parameter */
  3285. if (windowBits < 0) {
  3286. wrap = 0;
  3287. windowBits = -windowBits;
  3288. }
  3289. else {
  3290. wrap = (windowBits >> 4) + 1;
  3291. if (windowBits < 48) {
  3292. windowBits &= 15;
  3293. }
  3294. }
  3295. /* set number of window bits, free window if different */
  3296. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  3297. return Z_STREAM_ERROR;
  3298. }
  3299. if (state.window !== null && state.wbits !== windowBits) {
  3300. state.window = null;
  3301. }
  3302. /* update state and reset the rest of it */
  3303. state.wrap = wrap;
  3304. state.wbits = windowBits;
  3305. return inflateReset(strm);
  3306. }
  3307. function inflateInit2(strm, windowBits) {
  3308. var ret;
  3309. var state;
  3310. if (!strm) { return Z_STREAM_ERROR; }
  3311. //strm.msg = Z_NULL; /* in case we return an error */
  3312. state = new InflateState();
  3313. //if (state === Z_NULL) return Z_MEM_ERROR;
  3314. //Tracev((stderr, "inflate: allocated\n"));
  3315. strm.state = state;
  3316. state.window = null/*Z_NULL*/;
  3317. ret = inflateReset2(strm, windowBits);
  3318. if (ret !== Z_OK) {
  3319. strm.state = null/*Z_NULL*/;
  3320. }
  3321. return ret;
  3322. }
  3323. function inflateInit(strm) {
  3324. return inflateInit2(strm, DEF_WBITS);
  3325. }
  3326. /*
  3327. Return state with length and distance decoding tables and index sizes set to
  3328. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  3329. If BUILDFIXED is defined, then instead this routine builds the tables the
  3330. first time it's called, and returns those tables the first time and
  3331. thereafter. This reduces the size of the code by about 2K bytes, in
  3332. exchange for a little execution time. However, BUILDFIXED should not be
  3333. used for threaded applications, since the rewriting of the tables and virgin
  3334. may not be thread-safe.
  3335. */
  3336. var virgin = true;
  3337. var lenfix, distfix; // We have no pointers in JS, so keep tables separate
  3338. function fixedtables(state) {
  3339. /* build fixed huffman tables if first call (may not be thread safe) */
  3340. if (virgin) {
  3341. var sym;
  3342. lenfix = new utils.Buf32(512);
  3343. distfix = new utils.Buf32(32);
  3344. /* literal/length table */
  3345. sym = 0;
  3346. while (sym < 144) { state.lens[sym++] = 8; }
  3347. while (sym < 256) { state.lens[sym++] = 9; }
  3348. while (sym < 280) { state.lens[sym++] = 7; }
  3349. while (sym < 288) { state.lens[sym++] = 8; }
  3350. inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
  3351. /* distance table */
  3352. sym = 0;
  3353. while (sym < 32) { state.lens[sym++] = 5; }
  3354. inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
  3355. /* do this just once */
  3356. virgin = false;
  3357. }
  3358. state.lencode = lenfix;
  3359. state.lenbits = 9;
  3360. state.distcode = distfix;
  3361. state.distbits = 5;
  3362. }
  3363. /*
  3364. Update the window with the last wsize (normally 32K) bytes written before
  3365. returning. If window does not exist yet, create it. This is only called
  3366. when a window is already in use, or when output has been written during this
  3367. inflate call, but the end of the deflate stream has not been reached yet.
  3368. It is also called to create a window for dictionary data when a dictionary
  3369. is loaded.
  3370. Providing output buffers larger than 32K to inflate() should provide a speed
  3371. advantage, since only the last 32K of output is copied to the sliding window
  3372. upon return from inflate(), and since all distances after the first 32K of
  3373. output will fall in the output data, making match copies simpler and faster.
  3374. The advantage may be dependent on the size of the processor's data caches.
  3375. */
  3376. function updatewindow(strm, src, end, copy) {
  3377. var dist;
  3378. var state = strm.state;
  3379. /* if it hasn't been done already, allocate space for the window */
  3380. if (state.window === null) {
  3381. state.wsize = 1 << state.wbits;
  3382. state.wnext = 0;
  3383. state.whave = 0;
  3384. state.window = new utils.Buf8(state.wsize);
  3385. }
  3386. /* copy state->wsize or less output bytes into the circular window */
  3387. if (copy >= state.wsize) {
  3388. utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0);
  3389. state.wnext = 0;
  3390. state.whave = state.wsize;
  3391. }
  3392. else {
  3393. dist = state.wsize - state.wnext;
  3394. if (dist > copy) {
  3395. dist = copy;
  3396. }
  3397. //zmemcpy(state->window + state->wnext, end - copy, dist);
  3398. utils.arraySet(state.window, src, end - copy, dist, state.wnext);
  3399. copy -= dist;
  3400. if (copy) {
  3401. //zmemcpy(state->window, end - copy, copy);
  3402. utils.arraySet(state.window, src, end - copy, copy, 0);
  3403. state.wnext = copy;
  3404. state.whave = state.wsize;
  3405. }
  3406. else {
  3407. state.wnext += dist;
  3408. if (state.wnext === state.wsize) { state.wnext = 0; }
  3409. if (state.whave < state.wsize) { state.whave += dist; }
  3410. }
  3411. }
  3412. return 0;
  3413. }
  3414. function inflate(strm, flush) {
  3415. var state;
  3416. var input, output; // input/output buffers
  3417. var next; /* next input INDEX */
  3418. var put; /* next output INDEX */
  3419. var have, left; /* available input and output */
  3420. var hold; /* bit buffer */
  3421. var bits; /* bits in bit buffer */
  3422. var _in, _out; /* save starting available input and output */
  3423. var copy; /* number of stored or match bytes to copy */
  3424. var from; /* where to copy match bytes from */
  3425. var from_source;
  3426. var here = 0; /* current decoding table entry */
  3427. var here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  3428. //var last; /* parent table entry */
  3429. var last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  3430. var len; /* length to copy for repeats, bits to drop */
  3431. var ret; /* return code */
  3432. var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */
  3433. var opts;
  3434. var n; // temporary var for NEED_BITS
  3435. var order = /* permutation of code lengths */
  3436. [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  3437. if (!strm || !strm.state || !strm.output ||
  3438. (!strm.input && strm.avail_in !== 0)) {
  3439. return Z_STREAM_ERROR;
  3440. }
  3441. state = strm.state;
  3442. if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
  3443. //--- LOAD() ---
  3444. put = strm.next_out;
  3445. output = strm.output;
  3446. left = strm.avail_out;
  3447. next = strm.next_in;
  3448. input = strm.input;
  3449. have = strm.avail_in;
  3450. hold = state.hold;
  3451. bits = state.bits;
  3452. //---
  3453. _in = have;
  3454. _out = left;
  3455. ret = Z_OK;
  3456. inf_leave: // goto emulation
  3457. for (;;) {
  3458. switch (state.mode) {
  3459. case HEAD:
  3460. if (state.wrap === 0) {
  3461. state.mode = TYPEDO;
  3462. break;
  3463. }
  3464. //=== NEEDBITS(16);
  3465. while (bits < 16) {
  3466. if (have === 0) { break inf_leave; }
  3467. have--;
  3468. hold += input[next++] << bits;
  3469. bits += 8;
  3470. }
  3471. //===//
  3472. if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */
  3473. state.check = 0/*crc32(0L, Z_NULL, 0)*/;
  3474. //=== CRC2(state.check, hold);
  3475. hbuf[0] = hold & 0xff;
  3476. hbuf[1] = (hold >>> 8) & 0xff;
  3477. state.check = crc32(state.check, hbuf, 2, 0);
  3478. //===//
  3479. //=== INITBITS();
  3480. hold = 0;
  3481. bits = 0;
  3482. //===//
  3483. state.mode = FLAGS;
  3484. break;
  3485. }
  3486. state.flags = 0; /* expect zlib header */
  3487. if (state.head) {
  3488. state.head.done = false;
  3489. }
  3490. if (!(state.wrap & 1) || /* check if zlib header allowed */
  3491. (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
  3492. strm.msg = 'incorrect header check';
  3493. state.mode = BAD;
  3494. break;
  3495. }
  3496. if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
  3497. strm.msg = 'unknown compression method';
  3498. state.mode = BAD;
  3499. break;
  3500. }
  3501. //--- DROPBITS(4) ---//
  3502. hold >>>= 4;
  3503. bits -= 4;
  3504. //---//
  3505. len = (hold & 0x0f)/*BITS(4)*/ + 8;
  3506. if (state.wbits === 0) {
  3507. state.wbits = len;
  3508. }
  3509. else if (len > state.wbits) {
  3510. strm.msg = 'invalid window size';
  3511. state.mode = BAD;
  3512. break;
  3513. }
  3514. state.dmax = 1 << len;
  3515. //Tracev((stderr, "inflate: zlib header ok\n"));
  3516. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  3517. state.mode = hold & 0x200 ? DICTID : TYPE;
  3518. //=== INITBITS();
  3519. hold = 0;
  3520. bits = 0;
  3521. //===//
  3522. break;
  3523. case FLAGS:
  3524. //=== NEEDBITS(16); */
  3525. while (bits < 16) {
  3526. if (have === 0) { break inf_leave; }
  3527. have--;
  3528. hold += input[next++] << bits;
  3529. bits += 8;
  3530. }
  3531. //===//
  3532. state.flags = hold;
  3533. if ((state.flags & 0xff) !== Z_DEFLATED) {
  3534. strm.msg = 'unknown compression method';
  3535. state.mode = BAD;
  3536. break;
  3537. }
  3538. if (state.flags & 0xe000) {
  3539. strm.msg = 'unknown header flags set';
  3540. state.mode = BAD;
  3541. break;
  3542. }
  3543. if (state.head) {
  3544. state.head.text = ((hold >> 8) & 1);
  3545. }
  3546. if (state.flags & 0x0200) {
  3547. //=== CRC2(state.check, hold);
  3548. hbuf[0] = hold & 0xff;
  3549. hbuf[1] = (hold >>> 8) & 0xff;
  3550. state.check = crc32(state.check, hbuf, 2, 0);
  3551. //===//
  3552. }
  3553. //=== INITBITS();
  3554. hold = 0;
  3555. bits = 0;
  3556. //===//
  3557. state.mode = TIME;
  3558. /* falls through */
  3559. case TIME:
  3560. //=== NEEDBITS(32); */
  3561. while (bits < 32) {
  3562. if (have === 0) { break inf_leave; }
  3563. have--;
  3564. hold += input[next++] << bits;
  3565. bits += 8;
  3566. }
  3567. //===//
  3568. if (state.head) {
  3569. state.head.time = hold;
  3570. }
  3571. if (state.flags & 0x0200) {
  3572. //=== CRC4(state.check, hold)
  3573. hbuf[0] = hold & 0xff;
  3574. hbuf[1] = (hold >>> 8) & 0xff;
  3575. hbuf[2] = (hold >>> 16) & 0xff;
  3576. hbuf[3] = (hold >>> 24) & 0xff;
  3577. state.check = crc32(state.check, hbuf, 4, 0);
  3578. //===
  3579. }
  3580. //=== INITBITS();
  3581. hold = 0;
  3582. bits = 0;
  3583. //===//
  3584. state.mode = OS;
  3585. /* falls through */
  3586. case OS:
  3587. //=== NEEDBITS(16); */
  3588. while (bits < 16) {
  3589. if (have === 0) { break inf_leave; }
  3590. have--;
  3591. hold += input[next++] << bits;
  3592. bits += 8;
  3593. }
  3594. //===//
  3595. if (state.head) {
  3596. state.head.xflags = (hold & 0xff);
  3597. state.head.os = (hold >> 8);
  3598. }
  3599. if (state.flags & 0x0200) {
  3600. //=== CRC2(state.check, hold);
  3601. hbuf[0] = hold & 0xff;
  3602. hbuf[1] = (hold >>> 8) & 0xff;
  3603. state.check = crc32(state.check, hbuf, 2, 0);
  3604. //===//
  3605. }
  3606. //=== INITBITS();
  3607. hold = 0;
  3608. bits = 0;
  3609. //===//
  3610. state.mode = EXLEN;
  3611. /* falls through */
  3612. case EXLEN:
  3613. if (state.flags & 0x0400) {
  3614. //=== NEEDBITS(16); */
  3615. while (bits < 16) {
  3616. if (have === 0) { break inf_leave; }
  3617. have--;
  3618. hold += input[next++] << bits;
  3619. bits += 8;
  3620. }
  3621. //===//
  3622. state.length = hold;
  3623. if (state.head) {
  3624. state.head.extra_len = hold;
  3625. }
  3626. if (state.flags & 0x0200) {
  3627. //=== CRC2(state.check, hold);
  3628. hbuf[0] = hold & 0xff;
  3629. hbuf[1] = (hold >>> 8) & 0xff;
  3630. state.check = crc32(state.check, hbuf, 2, 0);
  3631. //===//
  3632. }
  3633. //=== INITBITS();
  3634. hold = 0;
  3635. bits = 0;
  3636. //===//
  3637. }
  3638. else if (state.head) {
  3639. state.head.extra = null/*Z_NULL*/;
  3640. }
  3641. state.mode = EXTRA;
  3642. /* falls through */
  3643. case EXTRA:
  3644. if (state.flags & 0x0400) {
  3645. copy = state.length;
  3646. if (copy > have) { copy = have; }
  3647. if (copy) {
  3648. if (state.head) {
  3649. len = state.head.extra_len - state.length;
  3650. if (!state.head.extra) {
  3651. // Use untyped array for more convenient processing later
  3652. state.head.extra = new Array(state.head.extra_len);
  3653. }
  3654. utils.arraySet(
  3655. state.head.extra,
  3656. input,
  3657. next,
  3658. // extra field is limited to 65536 bytes
  3659. // - no need for additional size check
  3660. copy,
  3661. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  3662. len
  3663. );
  3664. //zmemcpy(state.head.extra + len, next,
  3665. // len + copy > state.head.extra_max ?
  3666. // state.head.extra_max - len : copy);
  3667. }
  3668. if (state.flags & 0x0200) {
  3669. state.check = crc32(state.check, input, copy, next);
  3670. }
  3671. have -= copy;
  3672. next += copy;
  3673. state.length -= copy;
  3674. }
  3675. if (state.length) { break inf_leave; }
  3676. }
  3677. state.length = 0;
  3678. state.mode = NAME;
  3679. /* falls through */
  3680. case NAME:
  3681. if (state.flags & 0x0800) {
  3682. if (have === 0) { break inf_leave; }
  3683. copy = 0;
  3684. do {
  3685. // TODO: 2 or 1 bytes?
  3686. len = input[next + copy++];
  3687. /* use constant limit because in js we should not preallocate memory */
  3688. if (state.head && len &&
  3689. (state.length < 65536 /*state.head.name_max*/)) {
  3690. state.head.name += String.fromCharCode(len);
  3691. }
  3692. } while (len && copy < have);
  3693. if (state.flags & 0x0200) {
  3694. state.check = crc32(state.check, input, copy, next);
  3695. }
  3696. have -= copy;
  3697. next += copy;
  3698. if (len) { break inf_leave; }
  3699. }
  3700. else if (state.head) {
  3701. state.head.name = null;
  3702. }
  3703. state.length = 0;
  3704. state.mode = COMMENT;
  3705. /* falls through */
  3706. case COMMENT:
  3707. if (state.flags & 0x1000) {
  3708. if (have === 0) { break inf_leave; }
  3709. copy = 0;
  3710. do {
  3711. len = input[next + copy++];
  3712. /* use constant limit because in js we should not preallocate memory */
  3713. if (state.head && len &&
  3714. (state.length < 65536 /*state.head.comm_max*/)) {
  3715. state.head.comment += String.fromCharCode(len);
  3716. }
  3717. } while (len && copy < have);
  3718. if (state.flags & 0x0200) {
  3719. state.check = crc32(state.check, input, copy, next);
  3720. }
  3721. have -= copy;
  3722. next += copy;
  3723. if (len) { break inf_leave; }
  3724. }
  3725. else if (state.head) {
  3726. state.head.comment = null;
  3727. }
  3728. state.mode = HCRC;
  3729. /* falls through */
  3730. case HCRC:
  3731. if (state.flags & 0x0200) {
  3732. //=== NEEDBITS(16); */
  3733. while (bits < 16) {
  3734. if (have === 0) { break inf_leave; }
  3735. have--;
  3736. hold += input[next++] << bits;
  3737. bits += 8;
  3738. }
  3739. //===//
  3740. if (hold !== (state.check & 0xffff)) {
  3741. strm.msg = 'header crc mismatch';
  3742. state.mode = BAD;
  3743. break;
  3744. }
  3745. //=== INITBITS();
  3746. hold = 0;
  3747. bits = 0;
  3748. //===//
  3749. }
  3750. if (state.head) {
  3751. state.head.hcrc = ((state.flags >> 9) & 1);
  3752. state.head.done = true;
  3753. }
  3754. strm.adler = state.check = 0;
  3755. state.mode = TYPE;
  3756. break;
  3757. case DICTID:
  3758. //=== NEEDBITS(32); */
  3759. while (bits < 32) {
  3760. if (have === 0) { break inf_leave; }
  3761. have--;
  3762. hold += input[next++] << bits;
  3763. bits += 8;
  3764. }
  3765. //===//
  3766. strm.adler = state.check = zswap32(hold);
  3767. //=== INITBITS();
  3768. hold = 0;
  3769. bits = 0;
  3770. //===//
  3771. state.mode = DICT;
  3772. /* falls through */
  3773. case DICT:
  3774. if (state.havedict === 0) {
  3775. //--- RESTORE() ---
  3776. strm.next_out = put;
  3777. strm.avail_out = left;
  3778. strm.next_in = next;
  3779. strm.avail_in = have;
  3780. state.hold = hold;
  3781. state.bits = bits;
  3782. //---
  3783. return Z_NEED_DICT;
  3784. }
  3785. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  3786. state.mode = TYPE;
  3787. /* falls through */
  3788. case TYPE:
  3789. if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
  3790. /* falls through */
  3791. case TYPEDO:
  3792. if (state.last) {
  3793. //--- BYTEBITS() ---//
  3794. hold >>>= bits & 7;
  3795. bits -= bits & 7;
  3796. //---//
  3797. state.mode = CHECK;
  3798. break;
  3799. }
  3800. //=== NEEDBITS(3); */
  3801. while (bits < 3) {
  3802. if (have === 0) { break inf_leave; }
  3803. have--;
  3804. hold += input[next++] << bits;
  3805. bits += 8;
  3806. }
  3807. //===//
  3808. state.last = (hold & 0x01)/*BITS(1)*/;
  3809. //--- DROPBITS(1) ---//
  3810. hold >>>= 1;
  3811. bits -= 1;
  3812. //---//
  3813. switch ((hold & 0x03)/*BITS(2)*/) {
  3814. case 0: /* stored block */
  3815. //Tracev((stderr, "inflate: stored block%s\n",
  3816. // state.last ? " (last)" : ""));
  3817. state.mode = STORED;
  3818. break;
  3819. case 1: /* fixed block */
  3820. fixedtables(state);
  3821. //Tracev((stderr, "inflate: fixed codes block%s\n",
  3822. // state.last ? " (last)" : ""));
  3823. state.mode = LEN_; /* decode codes */
  3824. if (flush === Z_TREES) {
  3825. //--- DROPBITS(2) ---//
  3826. hold >>>= 2;
  3827. bits -= 2;
  3828. //---//
  3829. break inf_leave;
  3830. }
  3831. break;
  3832. case 2: /* dynamic block */
  3833. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  3834. // state.last ? " (last)" : ""));
  3835. state.mode = TABLE;
  3836. break;
  3837. case 3:
  3838. strm.msg = 'invalid block type';
  3839. state.mode = BAD;
  3840. }
  3841. //--- DROPBITS(2) ---//
  3842. hold >>>= 2;
  3843. bits -= 2;
  3844. //---//
  3845. break;
  3846. case STORED:
  3847. //--- BYTEBITS() ---// /* go to byte boundary */
  3848. hold >>>= bits & 7;
  3849. bits -= bits & 7;
  3850. //---//
  3851. //=== NEEDBITS(32); */
  3852. while (bits < 32) {
  3853. if (have === 0) { break inf_leave; }
  3854. have--;
  3855. hold += input[next++] << bits;
  3856. bits += 8;
  3857. }
  3858. //===//
  3859. if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
  3860. strm.msg = 'invalid stored block lengths';
  3861. state.mode = BAD;
  3862. break;
  3863. }
  3864. state.length = hold & 0xffff;
  3865. //Tracev((stderr, "inflate: stored length %u\n",
  3866. // state.length));
  3867. //=== INITBITS();
  3868. hold = 0;
  3869. bits = 0;
  3870. //===//
  3871. state.mode = COPY_;
  3872. if (flush === Z_TREES) { break inf_leave; }
  3873. /* falls through */
  3874. case COPY_:
  3875. state.mode = COPY;
  3876. /* falls through */
  3877. case COPY:
  3878. copy = state.length;
  3879. if (copy) {
  3880. if (copy > have) { copy = have; }
  3881. if (copy > left) { copy = left; }
  3882. if (copy === 0) { break inf_leave; }
  3883. //--- zmemcpy(put, next, copy); ---
  3884. utils.arraySet(output, input, next, copy, put);
  3885. //---//
  3886. have -= copy;
  3887. next += copy;
  3888. left -= copy;
  3889. put += copy;
  3890. state.length -= copy;
  3891. break;
  3892. }
  3893. //Tracev((stderr, "inflate: stored end\n"));
  3894. state.mode = TYPE;
  3895. break;
  3896. case TABLE:
  3897. //=== NEEDBITS(14); */
  3898. while (bits < 14) {
  3899. if (have === 0) { break inf_leave; }
  3900. have--;
  3901. hold += input[next++] << bits;
  3902. bits += 8;
  3903. }
  3904. //===//
  3905. state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
  3906. //--- DROPBITS(5) ---//
  3907. hold >>>= 5;
  3908. bits -= 5;
  3909. //---//
  3910. state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
  3911. //--- DROPBITS(5) ---//
  3912. hold >>>= 5;
  3913. bits -= 5;
  3914. //---//
  3915. state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
  3916. //--- DROPBITS(4) ---//
  3917. hold >>>= 4;
  3918. bits -= 4;
  3919. //---//
  3920. //#ifndef PKZIP_BUG_WORKAROUND
  3921. if (state.nlen > 286 || state.ndist > 30) {
  3922. strm.msg = 'too many length or distance symbols';
  3923. state.mode = BAD;
  3924. break;
  3925. }
  3926. //#endif
  3927. //Tracev((stderr, "inflate: table sizes ok\n"));
  3928. state.have = 0;
  3929. state.mode = LENLENS;
  3930. /* falls through */
  3931. case LENLENS:
  3932. while (state.have < state.ncode) {
  3933. //=== NEEDBITS(3);
  3934. while (bits < 3) {
  3935. if (have === 0) { break inf_leave; }
  3936. have--;
  3937. hold += input[next++] << bits;
  3938. bits += 8;
  3939. }
  3940. //===//
  3941. state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
  3942. //--- DROPBITS(3) ---//
  3943. hold >>>= 3;
  3944. bits -= 3;
  3945. //---//
  3946. }
  3947. while (state.have < 19) {
  3948. state.lens[order[state.have++]] = 0;
  3949. }
  3950. // We have separate tables & no pointers. 2 commented lines below not needed.
  3951. //state.next = state.codes;
  3952. //state.lencode = state.next;
  3953. // Switch to use dynamic table
  3954. state.lencode = state.lendyn;
  3955. state.lenbits = 7;
  3956. opts = { bits: state.lenbits };
  3957. ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  3958. state.lenbits = opts.bits;
  3959. if (ret) {
  3960. strm.msg = 'invalid code lengths set';
  3961. state.mode = BAD;
  3962. break;
  3963. }
  3964. //Tracev((stderr, "inflate: code lengths ok\n"));
  3965. state.have = 0;
  3966. state.mode = CODELENS;
  3967. /* falls through */
  3968. case CODELENS:
  3969. while (state.have < state.nlen + state.ndist) {
  3970. for (;;) {
  3971. here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/
  3972. here_bits = here >>> 24;
  3973. here_op = (here >>> 16) & 0xff;
  3974. here_val = here & 0xffff;
  3975. if ((here_bits) <= bits) { break; }
  3976. //--- PULLBYTE() ---//
  3977. if (have === 0) { break inf_leave; }
  3978. have--;
  3979. hold += input[next++] << bits;
  3980. bits += 8;
  3981. //---//
  3982. }
  3983. if (here_val < 16) {
  3984. //--- DROPBITS(here.bits) ---//
  3985. hold >>>= here_bits;
  3986. bits -= here_bits;
  3987. //---//
  3988. state.lens[state.have++] = here_val;
  3989. }
  3990. else {
  3991. if (here_val === 16) {
  3992. //=== NEEDBITS(here.bits + 2);
  3993. n = here_bits + 2;
  3994. while (bits < n) {
  3995. if (have === 0) { break inf_leave; }
  3996. have--;
  3997. hold += input[next++] << bits;
  3998. bits += 8;
  3999. }
  4000. //===//
  4001. //--- DROPBITS(here.bits) ---//
  4002. hold >>>= here_bits;
  4003. bits -= here_bits;
  4004. //---//
  4005. if (state.have === 0) {
  4006. strm.msg = 'invalid bit length repeat';
  4007. state.mode = BAD;
  4008. break;
  4009. }
  4010. len = state.lens[state.have - 1];
  4011. copy = 3 + (hold & 0x03);//BITS(2);
  4012. //--- DROPBITS(2) ---//
  4013. hold >>>= 2;
  4014. bits -= 2;
  4015. //---//
  4016. }
  4017. else if (here_val === 17) {
  4018. //=== NEEDBITS(here.bits + 3);
  4019. n = here_bits + 3;
  4020. while (bits < n) {
  4021. if (have === 0) { break inf_leave; }
  4022. have--;
  4023. hold += input[next++] << bits;
  4024. bits += 8;
  4025. }
  4026. //===//
  4027. //--- DROPBITS(here.bits) ---//
  4028. hold >>>= here_bits;
  4029. bits -= here_bits;
  4030. //---//
  4031. len = 0;
  4032. copy = 3 + (hold & 0x07);//BITS(3);
  4033. //--- DROPBITS(3) ---//
  4034. hold >>>= 3;
  4035. bits -= 3;
  4036. //---//
  4037. }
  4038. else {
  4039. //=== NEEDBITS(here.bits + 7);
  4040. n = here_bits + 7;
  4041. while (bits < n) {
  4042. if (have === 0) { break inf_leave; }
  4043. have--;
  4044. hold += input[next++] << bits;
  4045. bits += 8;
  4046. }
  4047. //===//
  4048. //--- DROPBITS(here.bits) ---//
  4049. hold >>>= here_bits;
  4050. bits -= here_bits;
  4051. //---//
  4052. len = 0;
  4053. copy = 11 + (hold & 0x7f);//BITS(7);
  4054. //--- DROPBITS(7) ---//
  4055. hold >>>= 7;
  4056. bits -= 7;
  4057. //---//
  4058. }
  4059. if (state.have + copy > state.nlen + state.ndist) {
  4060. strm.msg = 'invalid bit length repeat';
  4061. state.mode = BAD;
  4062. break;
  4063. }
  4064. while (copy--) {
  4065. state.lens[state.have++] = len;
  4066. }
  4067. }
  4068. }
  4069. /* handle error breaks in while */
  4070. if (state.mode === BAD) { break; }
  4071. /* check for end-of-block code (better have one) */
  4072. if (state.lens[256] === 0) {
  4073. strm.msg = 'invalid code -- missing end-of-block';
  4074. state.mode = BAD;
  4075. break;
  4076. }
  4077. /* build code tables -- note: do not change the lenbits or distbits
  4078. values here (9 and 6) without reading the comments in inftrees.h
  4079. concerning the ENOUGH constants, which depend on those values */
  4080. state.lenbits = 9;
  4081. opts = { bits: state.lenbits };
  4082. ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
  4083. // We have separate tables & no pointers. 2 commented lines below not needed.
  4084. // state.next_index = opts.table_index;
  4085. state.lenbits = opts.bits;
  4086. // state.lencode = state.next;
  4087. if (ret) {
  4088. strm.msg = 'invalid literal/lengths set';
  4089. state.mode = BAD;
  4090. break;
  4091. }
  4092. state.distbits = 6;
  4093. //state.distcode.copy(state.codes);
  4094. // Switch to use dynamic table
  4095. state.distcode = state.distdyn;
  4096. opts = { bits: state.distbits };
  4097. ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
  4098. // We have separate tables & no pointers. 2 commented lines below not needed.
  4099. // state.next_index = opts.table_index;
  4100. state.distbits = opts.bits;
  4101. // state.distcode = state.next;
  4102. if (ret) {
  4103. strm.msg = 'invalid distances set';
  4104. state.mode = BAD;
  4105. break;
  4106. }
  4107. //Tracev((stderr, 'inflate: codes ok\n'));
  4108. state.mode = LEN_;
  4109. if (flush === Z_TREES) { break inf_leave; }
  4110. /* falls through */
  4111. case LEN_:
  4112. state.mode = LEN;
  4113. /* falls through */
  4114. case LEN:
  4115. if (have >= 6 && left >= 258) {
  4116. //--- RESTORE() ---
  4117. strm.next_out = put;
  4118. strm.avail_out = left;
  4119. strm.next_in = next;
  4120. strm.avail_in = have;
  4121. state.hold = hold;
  4122. state.bits = bits;
  4123. //---
  4124. inflate_fast(strm, _out);
  4125. //--- LOAD() ---
  4126. put = strm.next_out;
  4127. output = strm.output;
  4128. left = strm.avail_out;
  4129. next = strm.next_in;
  4130. input = strm.input;
  4131. have = strm.avail_in;
  4132. hold = state.hold;
  4133. bits = state.bits;
  4134. //---
  4135. if (state.mode === TYPE) {
  4136. state.back = -1;
  4137. }
  4138. break;
  4139. }
  4140. state.back = 0;
  4141. for (;;) {
  4142. here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/
  4143. here_bits = here >>> 24;
  4144. here_op = (here >>> 16) & 0xff;
  4145. here_val = here & 0xffff;
  4146. if (here_bits <= bits) { break; }
  4147. //--- PULLBYTE() ---//
  4148. if (have === 0) { break inf_leave; }
  4149. have--;
  4150. hold += input[next++] << bits;
  4151. bits += 8;
  4152. //---//
  4153. }
  4154. if (here_op && (here_op & 0xf0) === 0) {
  4155. last_bits = here_bits;
  4156. last_op = here_op;
  4157. last_val = here_val;
  4158. for (;;) {
  4159. here = state.lencode[last_val +
  4160. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  4161. here_bits = here >>> 24;
  4162. here_op = (here >>> 16) & 0xff;
  4163. here_val = here & 0xffff;
  4164. if ((last_bits + here_bits) <= bits) { break; }
  4165. //--- PULLBYTE() ---//
  4166. if (have === 0) { break inf_leave; }
  4167. have--;
  4168. hold += input[next++] << bits;
  4169. bits += 8;
  4170. //---//
  4171. }
  4172. //--- DROPBITS(last.bits) ---//
  4173. hold >>>= last_bits;
  4174. bits -= last_bits;
  4175. //---//
  4176. state.back += last_bits;
  4177. }
  4178. //--- DROPBITS(here.bits) ---//
  4179. hold >>>= here_bits;
  4180. bits -= here_bits;
  4181. //---//
  4182. state.back += here_bits;
  4183. state.length = here_val;
  4184. if (here_op === 0) {
  4185. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  4186. // "inflate: literal '%c'\n" :
  4187. // "inflate: literal 0x%02x\n", here.val));
  4188. state.mode = LIT;
  4189. break;
  4190. }
  4191. if (here_op & 32) {
  4192. //Tracevv((stderr, "inflate: end of block\n"));
  4193. state.back = -1;
  4194. state.mode = TYPE;
  4195. break;
  4196. }
  4197. if (here_op & 64) {
  4198. strm.msg = 'invalid literal/length code';
  4199. state.mode = BAD;
  4200. break;
  4201. }
  4202. state.extra = here_op & 15;
  4203. state.mode = LENEXT;
  4204. /* falls through */
  4205. case LENEXT:
  4206. if (state.extra) {
  4207. //=== NEEDBITS(state.extra);
  4208. n = state.extra;
  4209. while (bits < n) {
  4210. if (have === 0) { break inf_leave; }
  4211. have--;
  4212. hold += input[next++] << bits;
  4213. bits += 8;
  4214. }
  4215. //===//
  4216. state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  4217. //--- DROPBITS(state.extra) ---//
  4218. hold >>>= state.extra;
  4219. bits -= state.extra;
  4220. //---//
  4221. state.back += state.extra;
  4222. }
  4223. //Tracevv((stderr, "inflate: length %u\n", state.length));
  4224. state.was = state.length;
  4225. state.mode = DIST;
  4226. /* falls through */
  4227. case DIST:
  4228. for (;;) {
  4229. here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/
  4230. here_bits = here >>> 24;
  4231. here_op = (here >>> 16) & 0xff;
  4232. here_val = here & 0xffff;
  4233. if ((here_bits) <= bits) { break; }
  4234. //--- PULLBYTE() ---//
  4235. if (have === 0) { break inf_leave; }
  4236. have--;
  4237. hold += input[next++] << bits;
  4238. bits += 8;
  4239. //---//
  4240. }
  4241. if ((here_op & 0xf0) === 0) {
  4242. last_bits = here_bits;
  4243. last_op = here_op;
  4244. last_val = here_val;
  4245. for (;;) {
  4246. here = state.distcode[last_val +
  4247. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  4248. here_bits = here >>> 24;
  4249. here_op = (here >>> 16) & 0xff;
  4250. here_val = here & 0xffff;
  4251. if ((last_bits + here_bits) <= bits) { break; }
  4252. //--- PULLBYTE() ---//
  4253. if (have === 0) { break inf_leave; }
  4254. have--;
  4255. hold += input[next++] << bits;
  4256. bits += 8;
  4257. //---//
  4258. }
  4259. //--- DROPBITS(last.bits) ---//
  4260. hold >>>= last_bits;
  4261. bits -= last_bits;
  4262. //---//
  4263. state.back += last_bits;
  4264. }
  4265. //--- DROPBITS(here.bits) ---//
  4266. hold >>>= here_bits;
  4267. bits -= here_bits;
  4268. //---//
  4269. state.back += here_bits;
  4270. if (here_op & 64) {
  4271. strm.msg = 'invalid distance code';
  4272. state.mode = BAD;
  4273. break;
  4274. }
  4275. state.offset = here_val;
  4276. state.extra = (here_op) & 15;
  4277. state.mode = DISTEXT;
  4278. /* falls through */
  4279. case DISTEXT:
  4280. if (state.extra) {
  4281. //=== NEEDBITS(state.extra);
  4282. n = state.extra;
  4283. while (bits < n) {
  4284. if (have === 0) { break inf_leave; }
  4285. have--;
  4286. hold += input[next++] << bits;
  4287. bits += 8;
  4288. }
  4289. //===//
  4290. state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  4291. //--- DROPBITS(state.extra) ---//
  4292. hold >>>= state.extra;
  4293. bits -= state.extra;
  4294. //---//
  4295. state.back += state.extra;
  4296. }
  4297. //#ifdef INFLATE_STRICT
  4298. if (state.offset > state.dmax) {
  4299. strm.msg = 'invalid distance too far back';
  4300. state.mode = BAD;
  4301. break;
  4302. }
  4303. //#endif
  4304. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  4305. state.mode = MATCH;
  4306. /* falls through */
  4307. case MATCH:
  4308. if (left === 0) { break inf_leave; }
  4309. copy = _out - left;
  4310. if (state.offset > copy) { /* copy from window */
  4311. copy = state.offset - copy;
  4312. if (copy > state.whave) {
  4313. if (state.sane) {
  4314. strm.msg = 'invalid distance too far back';
  4315. state.mode = BAD;
  4316. break;
  4317. }
  4318. // (!) This block is disabled in zlib defaults,
  4319. // don't enable it for binary compatibility
  4320. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  4321. // Trace((stderr, "inflate.c too far\n"));
  4322. // copy -= state.whave;
  4323. // if (copy > state.length) { copy = state.length; }
  4324. // if (copy > left) { copy = left; }
  4325. // left -= copy;
  4326. // state.length -= copy;
  4327. // do {
  4328. // output[put++] = 0;
  4329. // } while (--copy);
  4330. // if (state.length === 0) { state.mode = LEN; }
  4331. // break;
  4332. //#endif
  4333. }
  4334. if (copy > state.wnext) {
  4335. copy -= state.wnext;
  4336. from = state.wsize - copy;
  4337. }
  4338. else {
  4339. from = state.wnext - copy;
  4340. }
  4341. if (copy > state.length) { copy = state.length; }
  4342. from_source = state.window;
  4343. }
  4344. else { /* copy from output */
  4345. from_source = output;
  4346. from = put - state.offset;
  4347. copy = state.length;
  4348. }
  4349. if (copy > left) { copy = left; }
  4350. left -= copy;
  4351. state.length -= copy;
  4352. do {
  4353. output[put++] = from_source[from++];
  4354. } while (--copy);
  4355. if (state.length === 0) { state.mode = LEN; }
  4356. break;
  4357. case LIT:
  4358. if (left === 0) { break inf_leave; }
  4359. output[put++] = state.length;
  4360. left--;
  4361. state.mode = LEN;
  4362. break;
  4363. case CHECK:
  4364. if (state.wrap) {
  4365. //=== NEEDBITS(32);
  4366. while (bits < 32) {
  4367. if (have === 0) { break inf_leave; }
  4368. have--;
  4369. // Use '|' instead of '+' to make sure that result is signed
  4370. hold |= input[next++] << bits;
  4371. bits += 8;
  4372. }
  4373. //===//
  4374. _out -= left;
  4375. strm.total_out += _out;
  4376. state.total += _out;
  4377. if (_out) {
  4378. strm.adler = state.check =
  4379. /*UPDATE(state.check, put - _out, _out);*/
  4380. (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out));
  4381. }
  4382. _out = left;
  4383. // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
  4384. if ((state.flags ? hold : zswap32(hold)) !== state.check) {
  4385. strm.msg = 'incorrect data check';
  4386. state.mode = BAD;
  4387. break;
  4388. }
  4389. //=== INITBITS();
  4390. hold = 0;
  4391. bits = 0;
  4392. //===//
  4393. //Tracev((stderr, "inflate: check matches trailer\n"));
  4394. }
  4395. state.mode = LENGTH;
  4396. /* falls through */
  4397. case LENGTH:
  4398. if (state.wrap && state.flags) {
  4399. //=== NEEDBITS(32);
  4400. while (bits < 32) {
  4401. if (have === 0) { break inf_leave; }
  4402. have--;
  4403. hold += input[next++] << bits;
  4404. bits += 8;
  4405. }
  4406. //===//
  4407. if (hold !== (state.total & 0xffffffff)) {
  4408. strm.msg = 'incorrect length check';
  4409. state.mode = BAD;
  4410. break;
  4411. }
  4412. //=== INITBITS();
  4413. hold = 0;
  4414. bits = 0;
  4415. //===//
  4416. //Tracev((stderr, "inflate: length matches trailer\n"));
  4417. }
  4418. state.mode = DONE;
  4419. /* falls through */
  4420. case DONE:
  4421. ret = Z_STREAM_END;
  4422. break inf_leave;
  4423. case BAD:
  4424. ret = Z_DATA_ERROR;
  4425. break inf_leave;
  4426. case MEM:
  4427. return Z_MEM_ERROR;
  4428. case SYNC:
  4429. /* falls through */
  4430. default:
  4431. return Z_STREAM_ERROR;
  4432. }
  4433. }
  4434. // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  4435. /*
  4436. Return from inflate(), updating the total counts and the check value.
  4437. If there was no progress during the inflate() call, return a buffer
  4438. error. Call updatewindow() to create and/or update the window state.
  4439. Note: a memory error from inflate() is non-recoverable.
  4440. */
  4441. //--- RESTORE() ---
  4442. strm.next_out = put;
  4443. strm.avail_out = left;
  4444. strm.next_in = next;
  4445. strm.avail_in = have;
  4446. state.hold = hold;
  4447. state.bits = bits;
  4448. //---
  4449. if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
  4450. (state.mode < CHECK || flush !== Z_FINISH))) {
  4451. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) {
  4452. state.mode = MEM;
  4453. return Z_MEM_ERROR;
  4454. }
  4455. }
  4456. _in -= strm.avail_in;
  4457. _out -= strm.avail_out;
  4458. strm.total_in += _in;
  4459. strm.total_out += _out;
  4460. state.total += _out;
  4461. if (state.wrap && _out) {
  4462. strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/
  4463. (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out));
  4464. }
  4465. strm.data_type = state.bits + (state.last ? 64 : 0) +
  4466. (state.mode === TYPE ? 128 : 0) +
  4467. (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  4468. if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) {
  4469. ret = Z_BUF_ERROR;
  4470. }
  4471. return ret;
  4472. }
  4473. function inflateEnd(strm) {
  4474. if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
  4475. return Z_STREAM_ERROR;
  4476. }
  4477. var state = strm.state;
  4478. if (state.window) {
  4479. state.window = null;
  4480. }
  4481. strm.state = null;
  4482. return Z_OK;
  4483. }
  4484. function inflateGetHeader(strm, head) {
  4485. var state;
  4486. /* check state */
  4487. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  4488. state = strm.state;
  4489. if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
  4490. /* save header structure */
  4491. state.head = head;
  4492. head.done = false;
  4493. return Z_OK;
  4494. }
  4495. function inflateSetDictionary(strm, dictionary) {
  4496. var dictLength = dictionary.length;
  4497. var state;
  4498. var dictid;
  4499. var ret;
  4500. /* check state */
  4501. if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR; }
  4502. state = strm.state;
  4503. if (state.wrap !== 0 && state.mode !== DICT) {
  4504. return Z_STREAM_ERROR;
  4505. }
  4506. /* check for correct dictionary identifier */
  4507. if (state.mode === DICT) {
  4508. dictid = 1; /* adler32(0, null, 0)*/
  4509. /* dictid = adler32(dictid, dictionary, dictLength); */
  4510. dictid = adler32(dictid, dictionary, dictLength, 0);
  4511. if (dictid !== state.check) {
  4512. return Z_DATA_ERROR;
  4513. }
  4514. }
  4515. /* copy dictionary to window using updatewindow(), which will amend the
  4516. existing dictionary if appropriate */
  4517. ret = updatewindow(strm, dictionary, dictLength, dictLength);
  4518. if (ret) {
  4519. state.mode = MEM;
  4520. return Z_MEM_ERROR;
  4521. }
  4522. state.havedict = 1;
  4523. // Tracev((stderr, "inflate: dictionary set\n"));
  4524. return Z_OK;
  4525. }
  4526. exports.inflateReset = inflateReset;
  4527. exports.inflateReset2 = inflateReset2;
  4528. exports.inflateResetKeep = inflateResetKeep;
  4529. exports.inflateInit = inflateInit;
  4530. exports.inflateInit2 = inflateInit2;
  4531. exports.inflate = inflate;
  4532. exports.inflateEnd = inflateEnd;
  4533. exports.inflateGetHeader = inflateGetHeader;
  4534. exports.inflateSetDictionary = inflateSetDictionary;
  4535. exports.inflateInfo = 'pako inflate (from Nodeca project)';
  4536. /* Not implemented
  4537. exports.inflateCopy = inflateCopy;
  4538. exports.inflateGetDictionary = inflateGetDictionary;
  4539. exports.inflateMark = inflateMark;
  4540. exports.inflatePrime = inflatePrime;
  4541. exports.inflateSync = inflateSync;
  4542. exports.inflateSyncPoint = inflateSyncPoint;
  4543. exports.inflateUndermine = inflateUndermine;
  4544. */
  4545. },{"../utils/common":3,"./adler32":5,"./crc32":7,"./inffast":10,"./inftrees":12}],12:[function(require,module,exports){
  4546. 'use strict';
  4547. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  4548. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  4549. //
  4550. // This software is provided 'as-is', without any express or implied
  4551. // warranty. In no event will the authors be held liable for any damages
  4552. // arising from the use of this software.
  4553. //
  4554. // Permission is granted to anyone to use this software for any purpose,
  4555. // including commercial applications, and to alter it and redistribute it
  4556. // freely, subject to the following restrictions:
  4557. //
  4558. // 1. The origin of this software must not be misrepresented; you must not
  4559. // claim that you wrote the original software. If you use this software
  4560. // in a product, an acknowledgment in the product documentation would be
  4561. // appreciated but is not required.
  4562. // 2. Altered source versions must be plainly marked as such, and must not be
  4563. // misrepresented as being the original software.
  4564. // 3. This notice may not be removed or altered from any source distribution.
  4565. var utils = require('../utils/common');
  4566. var MAXBITS = 15;
  4567. var ENOUGH_LENS = 852;
  4568. var ENOUGH_DISTS = 592;
  4569. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  4570. var CODES = 0;
  4571. var LENS = 1;
  4572. var DISTS = 2;
  4573. var lbase = [ /* Length codes 257..285 base */
  4574. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  4575. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  4576. ];
  4577. var lext = [ /* Length codes 257..285 extra */
  4578. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  4579. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
  4580. ];
  4581. var dbase = [ /* Distance codes 0..29 base */
  4582. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  4583. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  4584. 8193, 12289, 16385, 24577, 0, 0
  4585. ];
  4586. var dext = [ /* Distance codes 0..29 extra */
  4587. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  4588. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  4589. 28, 28, 29, 29, 64, 64
  4590. ];
  4591. module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)
  4592. {
  4593. var bits = opts.bits;
  4594. //here = opts.here; /* table entry for duplication */
  4595. var len = 0; /* a code's length in bits */
  4596. var sym = 0; /* index of code symbols */
  4597. var min = 0, max = 0; /* minimum and maximum code lengths */
  4598. var root = 0; /* number of index bits for root table */
  4599. var curr = 0; /* number of index bits for current table */
  4600. var drop = 0; /* code bits to drop for sub-table */
  4601. var left = 0; /* number of prefix codes available */
  4602. var used = 0; /* code entries in table used */
  4603. var huff = 0; /* Huffman code */
  4604. var incr; /* for incrementing code, index */
  4605. var fill; /* index for replicating entries */
  4606. var low; /* low bits for current root entry */
  4607. var mask; /* mask for low root bits */
  4608. var next; /* next available space in table */
  4609. var base = null; /* base value table to use */
  4610. var base_index = 0;
  4611. // var shoextra; /* extra bits table to use */
  4612. var end; /* use base and extra for symbol > end */
  4613. var count = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */
  4614. var offs = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */
  4615. var extra = null;
  4616. var extra_index = 0;
  4617. var here_bits, here_op, here_val;
  4618. /*
  4619. Process a set of code lengths to create a canonical Huffman code. The
  4620. code lengths are lens[0..codes-1]. Each length corresponds to the
  4621. symbols 0..codes-1. The Huffman code is generated by first sorting the
  4622. symbols by length from short to long, and retaining the symbol order
  4623. for codes with equal lengths. Then the code starts with all zero bits
  4624. for the first code of the shortest length, and the codes are integer
  4625. increments for the same length, and zeros are appended as the length
  4626. increases. For the deflate format, these bits are stored backwards
  4627. from their more natural integer increment ordering, and so when the
  4628. decoding tables are built in the large loop below, the integer codes
  4629. are incremented backwards.
  4630. This routine assumes, but does not check, that all of the entries in
  4631. lens[] are in the range 0..MAXBITS. The caller must assure this.
  4632. 1..MAXBITS is interpreted as that code length. zero means that that
  4633. symbol does not occur in this code.
  4634. The codes are sorted by computing a count of codes for each length,
  4635. creating from that a table of starting indices for each length in the
  4636. sorted table, and then entering the symbols in order in the sorted
  4637. table. The sorted table is work[], with that space being provided by
  4638. the caller.
  4639. The length counts are used for other purposes as well, i.e. finding
  4640. the minimum and maximum length codes, determining if there are any
  4641. codes at all, checking for a valid set of lengths, and looking ahead
  4642. at length counts to determine sub-table sizes when building the
  4643. decoding tables.
  4644. */
  4645. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  4646. for (len = 0; len <= MAXBITS; len++) {
  4647. count[len] = 0;
  4648. }
  4649. for (sym = 0; sym < codes; sym++) {
  4650. count[lens[lens_index + sym]]++;
  4651. }
  4652. /* bound code lengths, force root to be within code lengths */
  4653. root = bits;
  4654. for (max = MAXBITS; max >= 1; max--) {
  4655. if (count[max] !== 0) { break; }
  4656. }
  4657. if (root > max) {
  4658. root = max;
  4659. }
  4660. if (max === 0) { /* no symbols to code at all */
  4661. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  4662. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  4663. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  4664. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  4665. //table.op[opts.table_index] = 64;
  4666. //table.bits[opts.table_index] = 1;
  4667. //table.val[opts.table_index++] = 0;
  4668. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  4669. opts.bits = 1;
  4670. return 0; /* no symbols, but wait for decoding to report error */
  4671. }
  4672. for (min = 1; min < max; min++) {
  4673. if (count[min] !== 0) { break; }
  4674. }
  4675. if (root < min) {
  4676. root = min;
  4677. }
  4678. /* check for an over-subscribed or incomplete set of lengths */
  4679. left = 1;
  4680. for (len = 1; len <= MAXBITS; len++) {
  4681. left <<= 1;
  4682. left -= count[len];
  4683. if (left < 0) {
  4684. return -1;
  4685. } /* over-subscribed */
  4686. }
  4687. if (left > 0 && (type === CODES || max !== 1)) {
  4688. return -1; /* incomplete set */
  4689. }
  4690. /* generate offsets into symbol table for each length for sorting */
  4691. offs[1] = 0;
  4692. for (len = 1; len < MAXBITS; len++) {
  4693. offs[len + 1] = offs[len] + count[len];
  4694. }
  4695. /* sort symbols by length, by symbol order within each length */
  4696. for (sym = 0; sym < codes; sym++) {
  4697. if (lens[lens_index + sym] !== 0) {
  4698. work[offs[lens[lens_index + sym]]++] = sym;
  4699. }
  4700. }
  4701. /*
  4702. Create and fill in decoding tables. In this loop, the table being
  4703. filled is at next and has curr index bits. The code being used is huff
  4704. with length len. That code is converted to an index by dropping drop
  4705. bits off of the bottom. For codes where len is less than drop + curr,
  4706. those top drop + curr - len bits are incremented through all values to
  4707. fill the table with replicated entries.
  4708. root is the number of index bits for the root table. When len exceeds
  4709. root, sub-tables are created pointed to by the root entry with an index
  4710. of the low root bits of huff. This is saved in low to check for when a
  4711. new sub-table should be started. drop is zero when the root table is
  4712. being filled, and drop is root when sub-tables are being filled.
  4713. When a new sub-table is needed, it is necessary to look ahead in the
  4714. code lengths to determine what size sub-table is needed. The length
  4715. counts are used for this, and so count[] is decremented as codes are
  4716. entered in the tables.
  4717. used keeps track of how many table entries have been allocated from the
  4718. provided *table space. It is checked for LENS and DIST tables against
  4719. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  4720. the initial root table size constants. See the comments in inftrees.h
  4721. for more information.
  4722. sym increments through all symbols, and the loop terminates when
  4723. all codes of length max, i.e. all codes, have been processed. This
  4724. routine permits incomplete codes, so another loop after this one fills
  4725. in the rest of the decoding tables with invalid code markers.
  4726. */
  4727. /* set up for code type */
  4728. // poor man optimization - use if-else instead of switch,
  4729. // to avoid deopts in old v8
  4730. if (type === CODES) {
  4731. base = extra = work; /* dummy value--not used */
  4732. end = 19;
  4733. } else if (type === LENS) {
  4734. base = lbase;
  4735. base_index -= 257;
  4736. extra = lext;
  4737. extra_index -= 257;
  4738. end = 256;
  4739. } else { /* DISTS */
  4740. base = dbase;
  4741. extra = dext;
  4742. end = -1;
  4743. }
  4744. /* initialize opts for loop */
  4745. huff = 0; /* starting code */
  4746. sym = 0; /* starting code symbol */
  4747. len = min; /* starting code length */
  4748. next = table_index; /* current table to fill in */
  4749. curr = root; /* current table index bits */
  4750. drop = 0; /* current bits to drop from code for index */
  4751. low = -1; /* trigger new sub-table when len > root */
  4752. used = 1 << root; /* use root table entries */
  4753. mask = used - 1; /* mask for comparing low */
  4754. /* check available table space */
  4755. if ((type === LENS && used > ENOUGH_LENS) ||
  4756. (type === DISTS && used > ENOUGH_DISTS)) {
  4757. return 1;
  4758. }
  4759. /* process all codes and make table entries */
  4760. for (;;) {
  4761. /* create table entry */
  4762. here_bits = len - drop;
  4763. if (work[sym] < end) {
  4764. here_op = 0;
  4765. here_val = work[sym];
  4766. }
  4767. else if (work[sym] > end) {
  4768. here_op = extra[extra_index + work[sym]];
  4769. here_val = base[base_index + work[sym]];
  4770. }
  4771. else {
  4772. here_op = 32 + 64; /* end of block */
  4773. here_val = 0;
  4774. }
  4775. /* replicate for those indices with low len bits equal to huff */
  4776. incr = 1 << (len - drop);
  4777. fill = 1 << curr;
  4778. min = fill; /* save offset to next table */
  4779. do {
  4780. fill -= incr;
  4781. table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
  4782. } while (fill !== 0);
  4783. /* backwards increment the len-bit code huff */
  4784. incr = 1 << (len - 1);
  4785. while (huff & incr) {
  4786. incr >>= 1;
  4787. }
  4788. if (incr !== 0) {
  4789. huff &= incr - 1;
  4790. huff += incr;
  4791. } else {
  4792. huff = 0;
  4793. }
  4794. /* go to next symbol, update count, len */
  4795. sym++;
  4796. if (--count[len] === 0) {
  4797. if (len === max) { break; }
  4798. len = lens[lens_index + work[sym]];
  4799. }
  4800. /* create new sub-table if needed */
  4801. if (len > root && (huff & mask) !== low) {
  4802. /* if first time, transition to sub-tables */
  4803. if (drop === 0) {
  4804. drop = root;
  4805. }
  4806. /* increment past last table */
  4807. next += min; /* here min is 1 << curr */
  4808. /* determine length of next table */
  4809. curr = len - drop;
  4810. left = 1 << curr;
  4811. while (curr + drop < max) {
  4812. left -= count[curr + drop];
  4813. if (left <= 0) { break; }
  4814. curr++;
  4815. left <<= 1;
  4816. }
  4817. /* check for enough space */
  4818. used += 1 << curr;
  4819. if ((type === LENS && used > ENOUGH_LENS) ||
  4820. (type === DISTS && used > ENOUGH_DISTS)) {
  4821. return 1;
  4822. }
  4823. /* point entry in root table to sub-table */
  4824. low = huff & mask;
  4825. /*table.op[low] = curr;
  4826. table.bits[low] = root;
  4827. table.val[low] = next - opts.table_index;*/
  4828. table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
  4829. }
  4830. }
  4831. /* fill in remaining table entry if code is incomplete (guaranteed to have
  4832. at most one remaining entry, since if the code is incomplete, the
  4833. maximum code length that was allowed to get this far is one bit) */
  4834. if (huff !== 0) {
  4835. //table.op[next + huff] = 64; /* invalid code marker */
  4836. //table.bits[next + huff] = len - drop;
  4837. //table.val[next + huff] = 0;
  4838. table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
  4839. }
  4840. /* set return parameters */
  4841. //opts.table_index += used;
  4842. opts.bits = root;
  4843. return 0;
  4844. };
  4845. },{"../utils/common":3}],13:[function(require,module,exports){
  4846. 'use strict';
  4847. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  4848. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  4849. //
  4850. // This software is provided 'as-is', without any express or implied
  4851. // warranty. In no event will the authors be held liable for any damages
  4852. // arising from the use of this software.
  4853. //
  4854. // Permission is granted to anyone to use this software for any purpose,
  4855. // including commercial applications, and to alter it and redistribute it
  4856. // freely, subject to the following restrictions:
  4857. //
  4858. // 1. The origin of this software must not be misrepresented; you must not
  4859. // claim that you wrote the original software. If you use this software
  4860. // in a product, an acknowledgment in the product documentation would be
  4861. // appreciated but is not required.
  4862. // 2. Altered source versions must be plainly marked as such, and must not be
  4863. // misrepresented as being the original software.
  4864. // 3. This notice may not be removed or altered from any source distribution.
  4865. module.exports = {
  4866. 2: 'need dictionary', /* Z_NEED_DICT 2 */
  4867. 1: 'stream end', /* Z_STREAM_END 1 */
  4868. 0: '', /* Z_OK 0 */
  4869. '-1': 'file error', /* Z_ERRNO (-1) */
  4870. '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
  4871. '-3': 'data error', /* Z_DATA_ERROR (-3) */
  4872. '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
  4873. '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
  4874. '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
  4875. };
  4876. },{}],14:[function(require,module,exports){
  4877. 'use strict';
  4878. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  4879. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  4880. //
  4881. // This software is provided 'as-is', without any express or implied
  4882. // warranty. In no event will the authors be held liable for any damages
  4883. // arising from the use of this software.
  4884. //
  4885. // Permission is granted to anyone to use this software for any purpose,
  4886. // including commercial applications, and to alter it and redistribute it
  4887. // freely, subject to the following restrictions:
  4888. //
  4889. // 1. The origin of this software must not be misrepresented; you must not
  4890. // claim that you wrote the original software. If you use this software
  4891. // in a product, an acknowledgment in the product documentation would be
  4892. // appreciated but is not required.
  4893. // 2. Altered source versions must be plainly marked as such, and must not be
  4894. // misrepresented as being the original software.
  4895. // 3. This notice may not be removed or altered from any source distribution.
  4896. /* eslint-disable space-unary-ops */
  4897. var utils = require('../utils/common');
  4898. /* Public constants ==========================================================*/
  4899. /* ===========================================================================*/
  4900. //var Z_FILTERED = 1;
  4901. //var Z_HUFFMAN_ONLY = 2;
  4902. //var Z_RLE = 3;
  4903. var Z_FIXED = 4;
  4904. //var Z_DEFAULT_STRATEGY = 0;
  4905. /* Possible values of the data_type field (though see inflate()) */
  4906. var Z_BINARY = 0;
  4907. var Z_TEXT = 1;
  4908. //var Z_ASCII = 1; // = Z_TEXT
  4909. var Z_UNKNOWN = 2;
  4910. /*============================================================================*/
  4911. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  4912. // From zutil.h
  4913. var STORED_BLOCK = 0;
  4914. var STATIC_TREES = 1;
  4915. var DYN_TREES = 2;
  4916. /* The three kinds of block type */
  4917. var MIN_MATCH = 3;
  4918. var MAX_MATCH = 258;
  4919. /* The minimum and maximum match lengths */
  4920. // From deflate.h
  4921. /* ===========================================================================
  4922. * Internal compression state.
  4923. */
  4924. var LENGTH_CODES = 29;
  4925. /* number of length codes, not counting the special END_BLOCK code */
  4926. var LITERALS = 256;
  4927. /* number of literal bytes 0..255 */
  4928. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  4929. /* number of Literal or Length codes, including the END_BLOCK code */
  4930. var D_CODES = 30;
  4931. /* number of distance codes */
  4932. var BL_CODES = 19;
  4933. /* number of codes used to transfer the bit lengths */
  4934. var HEAP_SIZE = 2 * L_CODES + 1;
  4935. /* maximum heap size */
  4936. var MAX_BITS = 15;
  4937. /* All codes must not exceed MAX_BITS bits */
  4938. var Buf_size = 16;
  4939. /* size of bit buffer in bi_buf */
  4940. /* ===========================================================================
  4941. * Constants
  4942. */
  4943. var MAX_BL_BITS = 7;
  4944. /* Bit length codes must not exceed MAX_BL_BITS bits */
  4945. var END_BLOCK = 256;
  4946. /* end of block literal code */
  4947. var REP_3_6 = 16;
  4948. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  4949. var REPZ_3_10 = 17;
  4950. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  4951. var REPZ_11_138 = 18;
  4952. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  4953. /* eslint-disable comma-spacing,array-bracket-spacing */
  4954. var extra_lbits = /* extra bits for each length code */
  4955. [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];
  4956. var extra_dbits = /* extra bits for each distance code */
  4957. [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];
  4958. var extra_blbits = /* extra bits for each bit length code */
  4959. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7];
  4960. var bl_order =
  4961. [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];
  4962. /* eslint-enable comma-spacing,array-bracket-spacing */
  4963. /* The lengths of the bit length codes are sent in order of decreasing
  4964. * probability, to avoid transmitting the lengths for unused bit length codes.
  4965. */
  4966. /* ===========================================================================
  4967. * Local data. These are initialized only once.
  4968. */
  4969. // We pre-fill arrays with 0 to avoid uninitialized gaps
  4970. var DIST_CODE_LEN = 512; /* see definition of array dist_code below */
  4971. // !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1
  4972. var static_ltree = new Array((L_CODES + 2) * 2);
  4973. zero(static_ltree);
  4974. /* The static literal tree. Since the bit lengths are imposed, there is no
  4975. * need for the L_CODES extra codes used during heap construction. However
  4976. * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  4977. * below).
  4978. */
  4979. var static_dtree = new Array(D_CODES * 2);
  4980. zero(static_dtree);
  4981. /* The static distance tree. (Actually a trivial tree since all codes use
  4982. * 5 bits.)
  4983. */
  4984. var _dist_code = new Array(DIST_CODE_LEN);
  4985. zero(_dist_code);
  4986. /* Distance codes. The first 256 values correspond to the distances
  4987. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  4988. * the 15 bit distances.
  4989. */
  4990. var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1);
  4991. zero(_length_code);
  4992. /* length code for each normalized match length (0 == MIN_MATCH) */
  4993. var base_length = new Array(LENGTH_CODES);
  4994. zero(base_length);
  4995. /* First normalized length for each code (0 = MIN_MATCH) */
  4996. var base_dist = new Array(D_CODES);
  4997. zero(base_dist);
  4998. /* First normalized distance for each code (0 = distance of 1) */
  4999. function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
  5000. this.static_tree = static_tree; /* static tree or NULL */
  5001. this.extra_bits = extra_bits; /* extra bits for each code or NULL */
  5002. this.extra_base = extra_base; /* base index for extra_bits */
  5003. this.elems = elems; /* max number of elements in the tree */
  5004. this.max_length = max_length; /* max bit length for the codes */
  5005. // show if `static_tree` has data or dummy - needed for monomorphic objects
  5006. this.has_stree = static_tree && static_tree.length;
  5007. }
  5008. var static_l_desc;
  5009. var static_d_desc;
  5010. var static_bl_desc;
  5011. function TreeDesc(dyn_tree, stat_desc) {
  5012. this.dyn_tree = dyn_tree; /* the dynamic tree */
  5013. this.max_code = 0; /* largest code with non zero frequency */
  5014. this.stat_desc = stat_desc; /* the corresponding static tree */
  5015. }
  5016. function d_code(dist) {
  5017. return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
  5018. }
  5019. /* ===========================================================================
  5020. * Output a short LSB first on the stream.
  5021. * IN assertion: there is enough room in pendingBuf.
  5022. */
  5023. function put_short(s, w) {
  5024. // put_byte(s, (uch)((w) & 0xff));
  5025. // put_byte(s, (uch)((ush)(w) >> 8));
  5026. s.pending_buf[s.pending++] = (w) & 0xff;
  5027. s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
  5028. }
  5029. /* ===========================================================================
  5030. * Send a value on a given number of bits.
  5031. * IN assertion: length <= 16 and value fits in length bits.
  5032. */
  5033. function send_bits(s, value, length) {
  5034. if (s.bi_valid > (Buf_size - length)) {
  5035. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  5036. put_short(s, s.bi_buf);
  5037. s.bi_buf = value >> (Buf_size - s.bi_valid);
  5038. s.bi_valid += length - Buf_size;
  5039. } else {
  5040. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  5041. s.bi_valid += length;
  5042. }
  5043. }
  5044. function send_code(s, c, tree) {
  5045. send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/);
  5046. }
  5047. /* ===========================================================================
  5048. * Reverse the first len bits of a code, using straightforward code (a faster
  5049. * method would use a table)
  5050. * IN assertion: 1 <= len <= 15
  5051. */
  5052. function bi_reverse(code, len) {
  5053. var res = 0;
  5054. do {
  5055. res |= code & 1;
  5056. code >>>= 1;
  5057. res <<= 1;
  5058. } while (--len > 0);
  5059. return res >>> 1;
  5060. }
  5061. /* ===========================================================================
  5062. * Flush the bit buffer, keeping at most 7 bits in it.
  5063. */
  5064. function bi_flush(s) {
  5065. if (s.bi_valid === 16) {
  5066. put_short(s, s.bi_buf);
  5067. s.bi_buf = 0;
  5068. s.bi_valid = 0;
  5069. } else if (s.bi_valid >= 8) {
  5070. s.pending_buf[s.pending++] = s.bi_buf & 0xff;
  5071. s.bi_buf >>= 8;
  5072. s.bi_valid -= 8;
  5073. }
  5074. }
  5075. /* ===========================================================================
  5076. * Compute the optimal bit lengths for a tree and update the total bit length
  5077. * for the current block.
  5078. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  5079. * above are the tree nodes sorted by increasing frequency.
  5080. * OUT assertions: the field len is set to the optimal bit length, the
  5081. * array bl_count contains the frequencies for each bit length.
  5082. * The length opt_len is updated; static_len is also updated if stree is
  5083. * not null.
  5084. */
  5085. function gen_bitlen(s, desc)
  5086. // deflate_state *s;
  5087. // tree_desc *desc; /* the tree descriptor */
  5088. {
  5089. var tree = desc.dyn_tree;
  5090. var max_code = desc.max_code;
  5091. var stree = desc.stat_desc.static_tree;
  5092. var has_stree = desc.stat_desc.has_stree;
  5093. var extra = desc.stat_desc.extra_bits;
  5094. var base = desc.stat_desc.extra_base;
  5095. var max_length = desc.stat_desc.max_length;
  5096. var h; /* heap index */
  5097. var n, m; /* iterate over the tree elements */
  5098. var bits; /* bit length */
  5099. var xbits; /* extra bits */
  5100. var f; /* frequency */
  5101. var overflow = 0; /* number of elements with bit length too large */
  5102. for (bits = 0; bits <= MAX_BITS; bits++) {
  5103. s.bl_count[bits] = 0;
  5104. }
  5105. /* In a first pass, compute the optimal bit lengths (which may
  5106. * overflow in the case of the bit length tree).
  5107. */
  5108. tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */
  5109. for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
  5110. n = s.heap[h];
  5111. bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
  5112. if (bits > max_length) {
  5113. bits = max_length;
  5114. overflow++;
  5115. }
  5116. tree[n * 2 + 1]/*.Len*/ = bits;
  5117. /* We overwrite tree[n].Dad which is no longer needed */
  5118. if (n > max_code) { continue; } /* not a leaf node */
  5119. s.bl_count[bits]++;
  5120. xbits = 0;
  5121. if (n >= base) {
  5122. xbits = extra[n - base];
  5123. }
  5124. f = tree[n * 2]/*.Freq*/;
  5125. s.opt_len += f * (bits + xbits);
  5126. if (has_stree) {
  5127. s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits);
  5128. }
  5129. }
  5130. if (overflow === 0) { return; }
  5131. // Trace((stderr,"\nbit length overflow\n"));
  5132. /* This happens for example on obj2 and pic of the Calgary corpus */
  5133. /* Find the first bit length which could increase: */
  5134. do {
  5135. bits = max_length - 1;
  5136. while (s.bl_count[bits] === 0) { bits--; }
  5137. s.bl_count[bits]--; /* move one leaf down the tree */
  5138. s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
  5139. s.bl_count[max_length]--;
  5140. /* The brother of the overflow item also moves one step up,
  5141. * but this does not affect bl_count[max_length]
  5142. */
  5143. overflow -= 2;
  5144. } while (overflow > 0);
  5145. /* Now recompute all bit lengths, scanning in increasing frequency.
  5146. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  5147. * lengths instead of fixing only the wrong ones. This idea is taken
  5148. * from 'ar' written by Haruhiko Okumura.)
  5149. */
  5150. for (bits = max_length; bits !== 0; bits--) {
  5151. n = s.bl_count[bits];
  5152. while (n !== 0) {
  5153. m = s.heap[--h];
  5154. if (m > max_code) { continue; }
  5155. if (tree[m * 2 + 1]/*.Len*/ !== bits) {
  5156. // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  5157. s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/;
  5158. tree[m * 2 + 1]/*.Len*/ = bits;
  5159. }
  5160. n--;
  5161. }
  5162. }
  5163. }
  5164. /* ===========================================================================
  5165. * Generate the codes for a given tree and bit counts (which need not be
  5166. * optimal).
  5167. * IN assertion: the array bl_count contains the bit length statistics for
  5168. * the given tree and the field len is set for all tree elements.
  5169. * OUT assertion: the field code is set for all tree elements of non
  5170. * zero code length.
  5171. */
  5172. function gen_codes(tree, max_code, bl_count)
  5173. // ct_data *tree; /* the tree to decorate */
  5174. // int max_code; /* largest code with non zero frequency */
  5175. // ushf *bl_count; /* number of codes at each bit length */
  5176. {
  5177. var next_code = new Array(MAX_BITS + 1); /* next code value for each bit length */
  5178. var code = 0; /* running code value */
  5179. var bits; /* bit index */
  5180. var n; /* code index */
  5181. /* The distribution counts are first used to generate the code values
  5182. * without bit reversal.
  5183. */
  5184. for (bits = 1; bits <= MAX_BITS; bits++) {
  5185. next_code[bits] = code = (code + bl_count[bits - 1]) << 1;
  5186. }
  5187. /* Check that the bit counts in bl_count are consistent. The last code
  5188. * must be all ones.
  5189. */
  5190. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  5191. // "inconsistent bit counts");
  5192. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  5193. for (n = 0; n <= max_code; n++) {
  5194. var len = tree[n * 2 + 1]/*.Len*/;
  5195. if (len === 0) { continue; }
  5196. /* Now reverse the bits */
  5197. tree[n * 2]/*.Code*/ = bi_reverse(next_code[len]++, len);
  5198. //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  5199. // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  5200. }
  5201. }
  5202. /* ===========================================================================
  5203. * Initialize the various 'constant' tables.
  5204. */
  5205. function tr_static_init() {
  5206. var n; /* iterates over tree elements */
  5207. var bits; /* bit counter */
  5208. var length; /* length value */
  5209. var code; /* code value */
  5210. var dist; /* distance index */
  5211. var bl_count = new Array(MAX_BITS + 1);
  5212. /* number of codes at each bit length for an optimal tree */
  5213. // do check in _tr_init()
  5214. //if (static_init_done) return;
  5215. /* For some embedded targets, global variables are not initialized: */
  5216. /*#ifdef NO_INIT_GLOBAL_POINTERS
  5217. static_l_desc.static_tree = static_ltree;
  5218. static_l_desc.extra_bits = extra_lbits;
  5219. static_d_desc.static_tree = static_dtree;
  5220. static_d_desc.extra_bits = extra_dbits;
  5221. static_bl_desc.extra_bits = extra_blbits;
  5222. #endif*/
  5223. /* Initialize the mapping length (0..255) -> length code (0..28) */
  5224. length = 0;
  5225. for (code = 0; code < LENGTH_CODES - 1; code++) {
  5226. base_length[code] = length;
  5227. for (n = 0; n < (1 << extra_lbits[code]); n++) {
  5228. _length_code[length++] = code;
  5229. }
  5230. }
  5231. //Assert (length == 256, "tr_static_init: length != 256");
  5232. /* Note that the length 255 (match length 258) can be represented
  5233. * in two different ways: code 284 + 5 bits or code 285, so we
  5234. * overwrite length_code[255] to use the best encoding:
  5235. */
  5236. _length_code[length - 1] = code;
  5237. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  5238. dist = 0;
  5239. for (code = 0; code < 16; code++) {
  5240. base_dist[code] = dist;
  5241. for (n = 0; n < (1 << extra_dbits[code]); n++) {
  5242. _dist_code[dist++] = code;
  5243. }
  5244. }
  5245. //Assert (dist == 256, "tr_static_init: dist != 256");
  5246. dist >>= 7; /* from now on, all distances are divided by 128 */
  5247. for (; code < D_CODES; code++) {
  5248. base_dist[code] = dist << 7;
  5249. for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
  5250. _dist_code[256 + dist++] = code;
  5251. }
  5252. }
  5253. //Assert (dist == 256, "tr_static_init: 256+dist != 512");
  5254. /* Construct the codes of the static literal tree */
  5255. for (bits = 0; bits <= MAX_BITS; bits++) {
  5256. bl_count[bits] = 0;
  5257. }
  5258. n = 0;
  5259. while (n <= 143) {
  5260. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  5261. n++;
  5262. bl_count[8]++;
  5263. }
  5264. while (n <= 255) {
  5265. static_ltree[n * 2 + 1]/*.Len*/ = 9;
  5266. n++;
  5267. bl_count[9]++;
  5268. }
  5269. while (n <= 279) {
  5270. static_ltree[n * 2 + 1]/*.Len*/ = 7;
  5271. n++;
  5272. bl_count[7]++;
  5273. }
  5274. while (n <= 287) {
  5275. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  5276. n++;
  5277. bl_count[8]++;
  5278. }
  5279. /* Codes 286 and 287 do not exist, but we must include them in the
  5280. * tree construction to get a canonical Huffman tree (longest code
  5281. * all ones)
  5282. */
  5283. gen_codes(static_ltree, L_CODES + 1, bl_count);
  5284. /* The static distance tree is trivial: */
  5285. for (n = 0; n < D_CODES; n++) {
  5286. static_dtree[n * 2 + 1]/*.Len*/ = 5;
  5287. static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5);
  5288. }
  5289. // Now data ready and we can init static trees
  5290. static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
  5291. static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
  5292. static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
  5293. //static_init_done = true;
  5294. }
  5295. /* ===========================================================================
  5296. * Initialize a new block.
  5297. */
  5298. function init_block(s) {
  5299. var n; /* iterates over tree elements */
  5300. /* Initialize the trees. */
  5301. for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; }
  5302. for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; }
  5303. for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; }
  5304. s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1;
  5305. s.opt_len = s.static_len = 0;
  5306. s.last_lit = s.matches = 0;
  5307. }
  5308. /* ===========================================================================
  5309. * Flush the bit buffer and align the output on a byte boundary
  5310. */
  5311. function bi_windup(s)
  5312. {
  5313. if (s.bi_valid > 8) {
  5314. put_short(s, s.bi_buf);
  5315. } else if (s.bi_valid > 0) {
  5316. //put_byte(s, (Byte)s->bi_buf);
  5317. s.pending_buf[s.pending++] = s.bi_buf;
  5318. }
  5319. s.bi_buf = 0;
  5320. s.bi_valid = 0;
  5321. }
  5322. /* ===========================================================================
  5323. * Copy a stored block, storing first the length and its
  5324. * one's complement if requested.
  5325. */
  5326. function copy_block(s, buf, len, header)
  5327. //DeflateState *s;
  5328. //charf *buf; /* the input data */
  5329. //unsigned len; /* its length */
  5330. //int header; /* true if block header must be written */
  5331. {
  5332. bi_windup(s); /* align on byte boundary */
  5333. if (header) {
  5334. put_short(s, len);
  5335. put_short(s, ~len);
  5336. }
  5337. // while (len--) {
  5338. // put_byte(s, *buf++);
  5339. // }
  5340. utils.arraySet(s.pending_buf, s.window, buf, len, s.pending);
  5341. s.pending += len;
  5342. }
  5343. /* ===========================================================================
  5344. * Compares to subtrees, using the tree depth as tie breaker when
  5345. * the subtrees have equal frequency. This minimizes the worst case length.
  5346. */
  5347. function smaller(tree, n, m, depth) {
  5348. var _n2 = n * 2;
  5349. var _m2 = m * 2;
  5350. return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
  5351. (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
  5352. }
  5353. /* ===========================================================================
  5354. * Restore the heap property by moving down the tree starting at node k,
  5355. * exchanging a node with the smallest of its two sons if necessary, stopping
  5356. * when the heap property is re-established (each father smaller than its
  5357. * two sons).
  5358. */
  5359. function pqdownheap(s, tree, k)
  5360. // deflate_state *s;
  5361. // ct_data *tree; /* the tree to restore */
  5362. // int k; /* node to move down */
  5363. {
  5364. var v = s.heap[k];
  5365. var j = k << 1; /* left son of k */
  5366. while (j <= s.heap_len) {
  5367. /* Set j to the smallest of the two sons: */
  5368. if (j < s.heap_len &&
  5369. smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
  5370. j++;
  5371. }
  5372. /* Exit if v is smaller than both sons */
  5373. if (smaller(tree, v, s.heap[j], s.depth)) { break; }
  5374. /* Exchange v with the smallest son */
  5375. s.heap[k] = s.heap[j];
  5376. k = j;
  5377. /* And continue down the tree, setting j to the left son of k */
  5378. j <<= 1;
  5379. }
  5380. s.heap[k] = v;
  5381. }
  5382. // inlined manually
  5383. // var SMALLEST = 1;
  5384. /* ===========================================================================
  5385. * Send the block data compressed using the given Huffman trees
  5386. */
  5387. function compress_block(s, ltree, dtree)
  5388. // deflate_state *s;
  5389. // const ct_data *ltree; /* literal tree */
  5390. // const ct_data *dtree; /* distance tree */
  5391. {
  5392. var dist; /* distance of matched string */
  5393. var lc; /* match length or unmatched char (if dist == 0) */
  5394. var lx = 0; /* running index in l_buf */
  5395. var code; /* the code to send */
  5396. var extra; /* number of extra bits to send */
  5397. if (s.last_lit !== 0) {
  5398. do {
  5399. dist = (s.pending_buf[s.d_buf + lx * 2] << 8) | (s.pending_buf[s.d_buf + lx * 2 + 1]);
  5400. lc = s.pending_buf[s.l_buf + lx];
  5401. lx++;
  5402. if (dist === 0) {
  5403. send_code(s, lc, ltree); /* send a literal byte */
  5404. //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  5405. } else {
  5406. /* Here, lc is the match length - MIN_MATCH */
  5407. code = _length_code[lc];
  5408. send_code(s, code + LITERALS + 1, ltree); /* send the length code */
  5409. extra = extra_lbits[code];
  5410. if (extra !== 0) {
  5411. lc -= base_length[code];
  5412. send_bits(s, lc, extra); /* send the extra length bits */
  5413. }
  5414. dist--; /* dist is now the match distance - 1 */
  5415. code = d_code(dist);
  5416. //Assert (code < D_CODES, "bad d_code");
  5417. send_code(s, code, dtree); /* send the distance code */
  5418. extra = extra_dbits[code];
  5419. if (extra !== 0) {
  5420. dist -= base_dist[code];
  5421. send_bits(s, dist, extra); /* send the extra distance bits */
  5422. }
  5423. } /* literal or match pair ? */
  5424. /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  5425. //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
  5426. // "pendingBuf overflow");
  5427. } while (lx < s.last_lit);
  5428. }
  5429. send_code(s, END_BLOCK, ltree);
  5430. }
  5431. /* ===========================================================================
  5432. * Construct one Huffman tree and assigns the code bit strings and lengths.
  5433. * Update the total bit length for the current block.
  5434. * IN assertion: the field freq is set for all tree elements.
  5435. * OUT assertions: the fields len and code are set to the optimal bit length
  5436. * and corresponding code. The length opt_len is updated; static_len is
  5437. * also updated if stree is not null. The field max_code is set.
  5438. */
  5439. function build_tree(s, desc)
  5440. // deflate_state *s;
  5441. // tree_desc *desc; /* the tree descriptor */
  5442. {
  5443. var tree = desc.dyn_tree;
  5444. var stree = desc.stat_desc.static_tree;
  5445. var has_stree = desc.stat_desc.has_stree;
  5446. var elems = desc.stat_desc.elems;
  5447. var n, m; /* iterate over heap elements */
  5448. var max_code = -1; /* largest code with non zero frequency */
  5449. var node; /* new node being created */
  5450. /* Construct the initial heap, with least frequent element in
  5451. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  5452. * heap[0] is not used.
  5453. */
  5454. s.heap_len = 0;
  5455. s.heap_max = HEAP_SIZE;
  5456. for (n = 0; n < elems; n++) {
  5457. if (tree[n * 2]/*.Freq*/ !== 0) {
  5458. s.heap[++s.heap_len] = max_code = n;
  5459. s.depth[n] = 0;
  5460. } else {
  5461. tree[n * 2 + 1]/*.Len*/ = 0;
  5462. }
  5463. }
  5464. /* The pkzip format requires that at least one distance code exists,
  5465. * and that at least one bit should be sent even if there is only one
  5466. * possible code. So to avoid special checks later on we force at least
  5467. * two codes of non zero frequency.
  5468. */
  5469. while (s.heap_len < 2) {
  5470. node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
  5471. tree[node * 2]/*.Freq*/ = 1;
  5472. s.depth[node] = 0;
  5473. s.opt_len--;
  5474. if (has_stree) {
  5475. s.static_len -= stree[node * 2 + 1]/*.Len*/;
  5476. }
  5477. /* node is 0 or 1 so it does not have extra bits */
  5478. }
  5479. desc.max_code = max_code;
  5480. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  5481. * establish sub-heaps of increasing lengths:
  5482. */
  5483. for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
  5484. /* Construct the Huffman tree by repeatedly combining the least two
  5485. * frequent nodes.
  5486. */
  5487. node = elems; /* next internal node of the tree */
  5488. do {
  5489. //pqremove(s, tree, n); /* n = node of least frequency */
  5490. /*** pqremove ***/
  5491. n = s.heap[1/*SMALLEST*/];
  5492. s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
  5493. pqdownheap(s, tree, 1/*SMALLEST*/);
  5494. /***/
  5495. m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
  5496. s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
  5497. s.heap[--s.heap_max] = m;
  5498. /* Create a new node father of n and m */
  5499. tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
  5500. s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
  5501. tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node;
  5502. /* and insert the new node in the heap */
  5503. s.heap[1/*SMALLEST*/] = node++;
  5504. pqdownheap(s, tree, 1/*SMALLEST*/);
  5505. } while (s.heap_len >= 2);
  5506. s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
  5507. /* At this point, the fields freq and dad are set. We can now
  5508. * generate the bit lengths.
  5509. */
  5510. gen_bitlen(s, desc);
  5511. /* The field len is now set, we can generate the bit codes */
  5512. gen_codes(tree, max_code, s.bl_count);
  5513. }
  5514. /* ===========================================================================
  5515. * Scan a literal or distance tree to determine the frequencies of the codes
  5516. * in the bit length tree.
  5517. */
  5518. function scan_tree(s, tree, max_code)
  5519. // deflate_state *s;
  5520. // ct_data *tree; /* the tree to be scanned */
  5521. // int max_code; /* and its largest code of non zero frequency */
  5522. {
  5523. var n; /* iterates over all tree elements */
  5524. var prevlen = -1; /* last emitted length */
  5525. var curlen; /* length of current code */
  5526. var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  5527. var count = 0; /* repeat count of the current code */
  5528. var max_count = 7; /* max repeat count */
  5529. var min_count = 4; /* min repeat count */
  5530. if (nextlen === 0) {
  5531. max_count = 138;
  5532. min_count = 3;
  5533. }
  5534. tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */
  5535. for (n = 0; n <= max_code; n++) {
  5536. curlen = nextlen;
  5537. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  5538. if (++count < max_count && curlen === nextlen) {
  5539. continue;
  5540. } else if (count < min_count) {
  5541. s.bl_tree[curlen * 2]/*.Freq*/ += count;
  5542. } else if (curlen !== 0) {
  5543. if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
  5544. s.bl_tree[REP_3_6 * 2]/*.Freq*/++;
  5545. } else if (count <= 10) {
  5546. s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++;
  5547. } else {
  5548. s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++;
  5549. }
  5550. count = 0;
  5551. prevlen = curlen;
  5552. if (nextlen === 0) {
  5553. max_count = 138;
  5554. min_count = 3;
  5555. } else if (curlen === nextlen) {
  5556. max_count = 6;
  5557. min_count = 3;
  5558. } else {
  5559. max_count = 7;
  5560. min_count = 4;
  5561. }
  5562. }
  5563. }
  5564. /* ===========================================================================
  5565. * Send a literal or distance tree in compressed form, using the codes in
  5566. * bl_tree.
  5567. */
  5568. function send_tree(s, tree, max_code)
  5569. // deflate_state *s;
  5570. // ct_data *tree; /* the tree to be scanned */
  5571. // int max_code; /* and its largest code of non zero frequency */
  5572. {
  5573. var n; /* iterates over all tree elements */
  5574. var prevlen = -1; /* last emitted length */
  5575. var curlen; /* length of current code */
  5576. var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  5577. var count = 0; /* repeat count of the current code */
  5578. var max_count = 7; /* max repeat count */
  5579. var min_count = 4; /* min repeat count */
  5580. /* tree[max_code+1].Len = -1; */ /* guard already set */
  5581. if (nextlen === 0) {
  5582. max_count = 138;
  5583. min_count = 3;
  5584. }
  5585. for (n = 0; n <= max_code; n++) {
  5586. curlen = nextlen;
  5587. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  5588. if (++count < max_count && curlen === nextlen) {
  5589. continue;
  5590. } else if (count < min_count) {
  5591. do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
  5592. } else if (curlen !== 0) {
  5593. if (curlen !== prevlen) {
  5594. send_code(s, curlen, s.bl_tree);
  5595. count--;
  5596. }
  5597. //Assert(count >= 3 && count <= 6, " 3_6?");
  5598. send_code(s, REP_3_6, s.bl_tree);
  5599. send_bits(s, count - 3, 2);
  5600. } else if (count <= 10) {
  5601. send_code(s, REPZ_3_10, s.bl_tree);
  5602. send_bits(s, count - 3, 3);
  5603. } else {
  5604. send_code(s, REPZ_11_138, s.bl_tree);
  5605. send_bits(s, count - 11, 7);
  5606. }
  5607. count = 0;
  5608. prevlen = curlen;
  5609. if (nextlen === 0) {
  5610. max_count = 138;
  5611. min_count = 3;
  5612. } else if (curlen === nextlen) {
  5613. max_count = 6;
  5614. min_count = 3;
  5615. } else {
  5616. max_count = 7;
  5617. min_count = 4;
  5618. }
  5619. }
  5620. }
  5621. /* ===========================================================================
  5622. * Construct the Huffman tree for the bit lengths and return the index in
  5623. * bl_order of the last bit length code to send.
  5624. */
  5625. function build_bl_tree(s) {
  5626. var max_blindex; /* index of last bit length code of non zero freq */
  5627. /* Determine the bit length frequencies for literal and distance trees */
  5628. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  5629. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  5630. /* Build the bit length tree: */
  5631. build_tree(s, s.bl_desc);
  5632. /* opt_len now includes the length of the tree representations, except
  5633. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  5634. */
  5635. /* Determine the number of bit length codes to send. The pkzip format
  5636. * requires that at least 4 bit length codes be sent. (appnote.txt says
  5637. * 3 but the actual value used is 4.)
  5638. */
  5639. for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
  5640. if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) {
  5641. break;
  5642. }
  5643. }
  5644. /* Update opt_len to include the bit length tree and counts */
  5645. s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
  5646. //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  5647. // s->opt_len, s->static_len));
  5648. return max_blindex;
  5649. }
  5650. /* ===========================================================================
  5651. * Send the header for a block using dynamic Huffman trees: the counts, the
  5652. * lengths of the bit length codes, the literal tree and the distance tree.
  5653. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  5654. */
  5655. function send_all_trees(s, lcodes, dcodes, blcodes)
  5656. // deflate_state *s;
  5657. // int lcodes, dcodes, blcodes; /* number of codes for each tree */
  5658. {
  5659. var rank; /* index in bl_order */
  5660. //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  5661. //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  5662. // "too many codes");
  5663. //Tracev((stderr, "\nbl counts: "));
  5664. send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
  5665. send_bits(s, dcodes - 1, 5);
  5666. send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
  5667. for (rank = 0; rank < blcodes; rank++) {
  5668. //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  5669. send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3);
  5670. }
  5671. //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  5672. send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */
  5673. //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  5674. send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */
  5675. //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  5676. }
  5677. /* ===========================================================================
  5678. * Check if the data type is TEXT or BINARY, using the following algorithm:
  5679. * - TEXT if the two conditions below are satisfied:
  5680. * a) There are no non-portable control characters belonging to the
  5681. * "black list" (0..6, 14..25, 28..31).
  5682. * b) There is at least one printable character belonging to the
  5683. * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
  5684. * - BINARY otherwise.
  5685. * - The following partially-portable control characters form a
  5686. * "gray list" that is ignored in this detection algorithm:
  5687. * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  5688. * IN assertion: the fields Freq of dyn_ltree are set.
  5689. */
  5690. function detect_data_type(s) {
  5691. /* black_mask is the bit mask of black-listed bytes
  5692. * set bits 0..6, 14..25, and 28..31
  5693. * 0xf3ffc07f = binary 11110011111111111100000001111111
  5694. */
  5695. var black_mask = 0xf3ffc07f;
  5696. var n;
  5697. /* Check for non-textual ("black-listed") bytes. */
  5698. for (n = 0; n <= 31; n++, black_mask >>>= 1) {
  5699. if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) {
  5700. return Z_BINARY;
  5701. }
  5702. }
  5703. /* Check for textual ("white-listed") bytes. */
  5704. if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
  5705. s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
  5706. return Z_TEXT;
  5707. }
  5708. for (n = 32; n < LITERALS; n++) {
  5709. if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
  5710. return Z_TEXT;
  5711. }
  5712. }
  5713. /* There are no "black-listed" or "white-listed" bytes:
  5714. * this stream either is empty or has tolerated ("gray-listed") bytes only.
  5715. */
  5716. return Z_BINARY;
  5717. }
  5718. var static_init_done = false;
  5719. /* ===========================================================================
  5720. * Initialize the tree data structures for a new zlib stream.
  5721. */
  5722. function _tr_init(s)
  5723. {
  5724. if (!static_init_done) {
  5725. tr_static_init();
  5726. static_init_done = true;
  5727. }
  5728. s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
  5729. s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
  5730. s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
  5731. s.bi_buf = 0;
  5732. s.bi_valid = 0;
  5733. /* Initialize the first block of the first file: */
  5734. init_block(s);
  5735. }
  5736. /* ===========================================================================
  5737. * Send a stored block
  5738. */
  5739. function _tr_stored_block(s, buf, stored_len, last)
  5740. //DeflateState *s;
  5741. //charf *buf; /* input block */
  5742. //ulg stored_len; /* length of input block */
  5743. //int last; /* one if this is the last block for a file */
  5744. {
  5745. send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */
  5746. copy_block(s, buf, stored_len, true); /* with header */
  5747. }
  5748. /* ===========================================================================
  5749. * Send one empty static block to give enough lookahead for inflate.
  5750. * This takes 10 bits, of which 7 may remain in the bit buffer.
  5751. */
  5752. function _tr_align(s) {
  5753. send_bits(s, STATIC_TREES << 1, 3);
  5754. send_code(s, END_BLOCK, static_ltree);
  5755. bi_flush(s);
  5756. }
  5757. /* ===========================================================================
  5758. * Determine the best encoding for the current block: dynamic trees, static
  5759. * trees or store, and output the encoded block to the zip file.
  5760. */
  5761. function _tr_flush_block(s, buf, stored_len, last)
  5762. //DeflateState *s;
  5763. //charf *buf; /* input block, or NULL if too old */
  5764. //ulg stored_len; /* length of input block */
  5765. //int last; /* one if this is the last block for a file */
  5766. {
  5767. var opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  5768. var max_blindex = 0; /* index of last bit length code of non zero freq */
  5769. /* Build the Huffman trees unless a stored block is forced */
  5770. if (s.level > 0) {
  5771. /* Check if the file is binary or text */
  5772. if (s.strm.data_type === Z_UNKNOWN) {
  5773. s.strm.data_type = detect_data_type(s);
  5774. }
  5775. /* Construct the literal and distance trees */
  5776. build_tree(s, s.l_desc);
  5777. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  5778. // s->static_len));
  5779. build_tree(s, s.d_desc);
  5780. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  5781. // s->static_len));
  5782. /* At this point, opt_len and static_len are the total bit lengths of
  5783. * the compressed block data, excluding the tree representations.
  5784. */
  5785. /* Build the bit length tree for the above two trees, and get the index
  5786. * in bl_order of the last bit length code to send.
  5787. */
  5788. max_blindex = build_bl_tree(s);
  5789. /* Determine the best encoding. Compute the block lengths in bytes. */
  5790. opt_lenb = (s.opt_len + 3 + 7) >>> 3;
  5791. static_lenb = (s.static_len + 3 + 7) >>> 3;
  5792. // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  5793. // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  5794. // s->last_lit));
  5795. if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
  5796. } else {
  5797. // Assert(buf != (char*)0, "lost buf");
  5798. opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  5799. }
  5800. if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) {
  5801. /* 4: two words for the lengths */
  5802. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  5803. * Otherwise we can't have processed more than WSIZE input bytes since
  5804. * the last block flush, because compression would have been
  5805. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  5806. * transform a block into a stored block.
  5807. */
  5808. _tr_stored_block(s, buf, stored_len, last);
  5809. } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
  5810. send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
  5811. compress_block(s, static_ltree, static_dtree);
  5812. } else {
  5813. send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
  5814. send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
  5815. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  5816. }
  5817. // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  5818. /* The above check is made mod 2^32, for files larger than 512 MB
  5819. * and uLong implemented on 32 bits.
  5820. */
  5821. init_block(s);
  5822. if (last) {
  5823. bi_windup(s);
  5824. }
  5825. // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  5826. // s->compressed_len-7*last));
  5827. }
  5828. /* ===========================================================================
  5829. * Save the match info and tally the frequency counts. Return true if
  5830. * the current block must be flushed.
  5831. */
  5832. function _tr_tally(s, dist, lc)
  5833. // deflate_state *s;
  5834. // unsigned dist; /* distance of matched string */
  5835. // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  5836. {
  5837. //var out_length, in_length, dcode;
  5838. s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff;
  5839. s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
  5840. s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
  5841. s.last_lit++;
  5842. if (dist === 0) {
  5843. /* lc is the unmatched char */
  5844. s.dyn_ltree[lc * 2]/*.Freq*/++;
  5845. } else {
  5846. s.matches++;
  5847. /* Here, lc is the match length - MIN_MATCH */
  5848. dist--; /* dist = match distance - 1 */
  5849. //Assert((ush)dist < (ush)MAX_DIST(s) &&
  5850. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  5851. // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  5852. s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++;
  5853. s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
  5854. }
  5855. // (!) This block is disabled in zlib defaults,
  5856. // don't enable it for binary compatibility
  5857. //#ifdef TRUNCATE_BLOCK
  5858. // /* Try to guess if it is profitable to stop the current block here */
  5859. // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
  5860. // /* Compute an upper bound for the compressed length */
  5861. // out_length = s.last_lit*8;
  5862. // in_length = s.strstart - s.block_start;
  5863. //
  5864. // for (dcode = 0; dcode < D_CODES; dcode++) {
  5865. // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
  5866. // }
  5867. // out_length >>>= 3;
  5868. // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  5869. // // s->last_lit, in_length, out_length,
  5870. // // 100L - out_length*100L/in_length));
  5871. // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
  5872. // return true;
  5873. // }
  5874. // }
  5875. //#endif
  5876. return (s.last_lit === s.lit_bufsize - 1);
  5877. /* We avoid equality with lit_bufsize because of wraparound at 64K
  5878. * on 16 bit machines and because stored blocks are restricted to
  5879. * 64K-1 bytes.
  5880. */
  5881. }
  5882. exports._tr_init = _tr_init;
  5883. exports._tr_stored_block = _tr_stored_block;
  5884. exports._tr_flush_block = _tr_flush_block;
  5885. exports._tr_tally = _tr_tally;
  5886. exports._tr_align = _tr_align;
  5887. },{"../utils/common":3}],15:[function(require,module,exports){
  5888. 'use strict';
  5889. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  5890. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  5891. //
  5892. // This software is provided 'as-is', without any express or implied
  5893. // warranty. In no event will the authors be held liable for any damages
  5894. // arising from the use of this software.
  5895. //
  5896. // Permission is granted to anyone to use this software for any purpose,
  5897. // including commercial applications, and to alter it and redistribute it
  5898. // freely, subject to the following restrictions:
  5899. //
  5900. // 1. The origin of this software must not be misrepresented; you must not
  5901. // claim that you wrote the original software. If you use this software
  5902. // in a product, an acknowledgment in the product documentation would be
  5903. // appreciated but is not required.
  5904. // 2. Altered source versions must be plainly marked as such, and must not be
  5905. // misrepresented as being the original software.
  5906. // 3. This notice may not be removed or altered from any source distribution.
  5907. function ZStream() {
  5908. /* next input byte */
  5909. this.input = null; // JS specific, because we have no pointers
  5910. this.next_in = 0;
  5911. /* number of bytes available at input */
  5912. this.avail_in = 0;
  5913. /* total number of input bytes read so far */
  5914. this.total_in = 0;
  5915. /* next output byte should be put there */
  5916. this.output = null; // JS specific, because we have no pointers
  5917. this.next_out = 0;
  5918. /* remaining free space at output */
  5919. this.avail_out = 0;
  5920. /* total number of bytes output so far */
  5921. this.total_out = 0;
  5922. /* last error message, NULL if no error */
  5923. this.msg = ''/*Z_NULL*/;
  5924. /* not visible by applications */
  5925. this.state = null;
  5926. /* best guess about the data type: binary or text */
  5927. this.data_type = 2/*Z_UNKNOWN*/;
  5928. /* adler32 value of the uncompressed data */
  5929. this.adler = 0;
  5930. }
  5931. module.exports = ZStream;
  5932. },{}],"/":[function(require,module,exports){
  5933. // Top level file is just a mixin of submodules & constants
  5934. 'use strict';
  5935. var assign = require('./lib/utils/common').assign;
  5936. var deflate = require('./lib/deflate');
  5937. var inflate = require('./lib/inflate');
  5938. var constants = require('./lib/zlib/constants');
  5939. var pako = {};
  5940. assign(pako, deflate, inflate, constants);
  5941. module.exports = pako;
  5942. },{"./lib/deflate":1,"./lib/inflate":2,"./lib/utils/common":3,"./lib/zlib/constants":6}]},{},[])("/")
  5943. });