pako.js 216 KB

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