miniz.h 385 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081
  1. #pragma once
  2. /* miniz.c 2.2.0 - public domain deflate/inflate, zlib-subset, ZIP
  3. reading/writing/appending, PNG writing See "unlicense" statement at the end
  4. of this file. Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13,
  5. 2013 Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951:
  6. http://www.ietf.org/rfc/rfc1951.txt
  7. Most API's defined in miniz.c are optional. For example, to disable the
  8. archive related functions just define MINIZ_NO_ARCHIVE_APIS, or to get rid of
  9. all stdio usage define MINIZ_NO_STDIO (see the list below for more macros).
  10. * Low-level Deflate/Inflate implementation notes:
  11. Compression: Use the "tdefl" API's. The compressor supports raw, static,
  12. and dynamic blocks, lazy or greedy parsing, match length filtering, RLE-only,
  13. and Huffman-only streams. It performs and compresses approximately as well as
  14. zlib.
  15. Decompression: Use the "tinfl" API's. The entire decompressor is
  16. implemented as a single function coroutine: see tinfl_decompress(). It
  17. supports decompression into a 32KB (or larger power of 2) wrapping buffer, or
  18. into a memory block large enough to hold the entire file.
  19. The low-level tdefl/tinfl API's do not make any use of dynamic memory
  20. allocation.
  21. * zlib-style API notes:
  22. miniz.c implements a fairly large subset of zlib. There's enough
  23. functionality present for it to be a drop-in zlib replacement in many apps:
  24. The z_stream struct, optional memory allocation callbacks
  25. deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound
  26. inflateInit/inflateInit2/inflate/inflateReset/inflateEnd
  27. compress, compress2, compressBound, uncompress
  28. CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly
  29. routines. Supports raw deflate streams or standard zlib streams with adler-32
  30. checking.
  31. Limitations:
  32. The callback API's are not implemented yet. No support for gzip headers or
  33. zlib static dictionaries. I've tried to closely emulate zlib's various
  34. flavors of stream flushing and return status codes, but there are no
  35. guarantees that miniz.c pulls this off perfectly.
  36. * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function,
  37. originally written by Alex Evans. Supports 1-4 bytes/pixel images.
  38. * ZIP archive API notes:
  39. The ZIP archive API's where designed with simplicity and efficiency in
  40. mind, with just enough abstraction to get the job done with minimal fuss.
  41. There are simple API's to retrieve file information, read files from existing
  42. archives, create new archives, append new files to existing archives, or
  43. clone archive data from one archive to another. It supports archives located
  44. in memory or the heap, on disk (using stdio.h), or you can specify custom
  45. file read/write callbacks.
  46. - Archive reading: Just call this function to read a single file from a
  47. disk archive:
  48. void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const
  49. char *pArchive_name, size_t *pSize, mz_uint zip_flags);
  50. For more complex cases, use the "mz_zip_reader" functions. Upon opening an
  51. archive, the entire central directory is located and read as-is into memory,
  52. and subsequent file access only occurs when reading individual files.
  53. - Archives file scanning: The simple way is to use this function to scan a
  54. loaded archive for a specific file:
  55. int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName,
  56. const char *pComment, mz_uint flags);
  57. The locate operation can optionally check file comments too, which (as one
  58. example) can be used to identify multiple versions of the same file in an
  59. archive. This function uses a simple linear search through the central
  60. directory, so it's not very fast.
  61. Alternately, you can iterate through all the files in an archive (using
  62. mz_zip_reader_get_num_files()) and retrieve detailed info on each file by
  63. calling mz_zip_reader_file_stat().
  64. - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer
  65. immediately writes compressed file data to disk and builds an exact image of
  66. the central directory in memory. The central directory image is written all
  67. at once at the end of the archive file when the archive is finalized.
  68. The archive writer can optionally align each file's local header and file
  69. data to any power of 2 alignment, which can be useful when the archive will
  70. be read from optical media. Also, the writer supports placing arbitrary data
  71. blobs at the very beginning of ZIP archives. Archives written using either
  72. feature are still readable by any ZIP tool.
  73. - Archive appending: The simple way to add a single file to an archive is
  74. to call this function:
  75. mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename,
  76. const char *pArchive_name, const void *pBuf, size_t buf_size, const void
  77. *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
  78. The archive will be created if it doesn't already exist, otherwise it'll be
  79. appended to. Note the appending is done in-place and is not an atomic
  80. operation, so if something goes wrong during the operation it's possible the
  81. archive could be left without a central directory (although the local file
  82. headers and file data will be fine, so the archive will be recoverable).
  83. For more complex archive modification scenarios:
  84. 1. The safest way is to use a mz_zip_reader to read the existing archive,
  85. cloning only those bits you want to preserve into a new archive using using
  86. the mz_zip_writer_add_from_zip_reader() function (which compiles the
  87. compressed file data as-is). When you're done, delete the old archive and
  88. rename the newly written archive, and you're done. This is safe but requires
  89. a bunch of temporary disk space or heap memory.
  90. 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using
  91. mz_zip_writer_init_from_reader(), append new files as needed, then finalize
  92. the archive which will write an updated central directory to the original
  93. archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place()
  94. does.) There's a possibility that the archive's central directory could be
  95. lost with this method if anything goes wrong, though.
  96. - ZIP archive support limitations:
  97. No spanning support. Extraction functions can only handle unencrypted,
  98. stored or deflated files. Requires streams capable of seeking.
  99. * This is a header file library, like stb_image.c. To get only a header file,
  100. either cut and paste the below header, or create miniz.h, #define
  101. MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it.
  102. * Important: For best perf. be sure to customize the below macros for your
  103. target platform: #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 #define
  104. MINIZ_LITTLE_ENDIAN 1 #define MINIZ_HAS_64BIT_REGISTERS 1
  105. * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before
  106. including miniz.c to ensure miniz uses the 64-bit variants: fopen64(),
  107. stat64(), etc. Otherwise you won't be able to process large files (i.e.
  108. 32-bit stat() fails for me on files > 0x7FFFFFFF bytes).
  109. */
  110. /* Defines to completely disable specific portions of miniz.c:
  111. If all macros here are defined the only functionality remaining will be
  112. CRC-32, adler-32, tinfl, and tdefl. */
  113. /* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on
  114. * stdio for file I/O. */
  115. /*#define MINIZ_NO_STDIO */
  116. /* If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able
  117. * to get the current time, or */
  118. /* get/set file times, and the C run-time funcs that get/set times won't be
  119. * called. */
  120. /* The current downside is the times written to your archives will be from 1979.
  121. */
  122. /*#define MINIZ_NO_TIME */
  123. /* Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. */
  124. /*#define MINIZ_NO_ARCHIVE_APIS */
  125. /* Define MINIZ_NO_ARCHIVE_WRITING_APIS to disable all writing related ZIP
  126. * archive API's. */
  127. /*#define MINIZ_NO_ARCHIVE_WRITING_APIS */
  128. /* Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression
  129. * API's. */
  130. /*#define MINIZ_NO_ZLIB_APIS */
  131. /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent
  132. * conflicts against stock zlib. */
  133. /*#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES */
  134. /* Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
  135. Note if MINIZ_NO_MALLOC is defined then the user must always provide custom
  136. user alloc/free/realloc callbacks to the zlib and archive API's, and a few
  137. stand-alone helper API's which don't provide custom user functions (such as
  138. tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work.
  139. */
  140. /*#define MINIZ_NO_MALLOC */
  141. #if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
  142. /* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc
  143. * on Linux */
  144. #define MINIZ_NO_TIME
  145. #endif
  146. #include <stddef.h>
  147. #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
  148. #include <time.h>
  149. #endif
  150. #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \
  151. defined(__i386) || defined(__i486__) || defined(__i486) || \
  152. defined(i386) || defined(__ia64__) || defined(__x86_64__)
  153. /* MINIZ_X86_OR_X64_CPU is only used to help set the below macros. */
  154. #define MINIZ_X86_OR_X64_CPU 1
  155. #else
  156. #define MINIZ_X86_OR_X64_CPU 0
  157. #endif
  158. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
  159. /* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */
  160. #define MINIZ_LITTLE_ENDIAN 1
  161. #else
  162. #define MINIZ_LITTLE_ENDIAN 0
  163. #endif
  164. /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */
  165. #if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES)
  166. #if MINIZ_X86_OR_X64_CPU
  167. /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient
  168. * integer loads and stores from unaligned addresses. */
  169. #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
  170. #define MINIZ_UNALIGNED_USE_MEMCPY
  171. #else
  172. #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
  173. #endif
  174. #endif
  175. #if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || \
  176. defined(_LP64) || defined(__LP64__) || defined(__ia64__) || \
  177. defined(__x86_64__)
  178. /* Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are
  179. * reasonably fast (and don't involve compiler generated calls to helper
  180. * functions). */
  181. #define MINIZ_HAS_64BIT_REGISTERS 1
  182. #else
  183. #define MINIZ_HAS_64BIT_REGISTERS 0
  184. #endif
  185. /* ------------------- zlib-style API Definitions. */
  186. /* For more compatibility with zlib, miniz.c uses unsigned long for some
  187. * parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! */
  188. typedef unsigned long mz_ulong;
  189. /* mz_free() internally uses the MZ_FREE() macro (which by default calls free()
  190. * unless you've modified the MZ_MALLOC macro) to release a block allocated from
  191. * the heap. */
  192. void mz_free(void *p);
  193. #define MZ_ADLER32_INIT (1)
  194. /* mz_adler32() returns the initial adler-32 value to use when called with
  195. * ptr==NULL. */
  196. mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr,
  197. size_t buf_len);
  198. #define MZ_CRC32_INIT (0)
  199. /* mz_crc32() returns the initial CRC-32 value to use when called with
  200. * ptr==NULL. */
  201. mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr,
  202. size_t buf_len);
  203. /* Compression strategies. */
  204. enum {
  205. MZ_DEFAULT_STRATEGY = 0,
  206. MZ_FILTERED = 1,
  207. MZ_HUFFMAN_ONLY = 2,
  208. MZ_RLE = 3,
  209. MZ_FIXED = 4
  210. };
  211. /* Method */
  212. #define MZ_DEFLATED 8
  213. /* Heap allocation callbacks.
  214. Note that mz_alloc_func parameter types purposely differ from zlib's: items/size
  215. is size_t, not unsigned long. */
  216. typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
  217. typedef void (*mz_free_func)(void *opaque, void *address);
  218. typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items,
  219. size_t size);
  220. /* Compression levels: 0-9 are the standard zlib-style levels, 10 is best
  221. * possible compression (not zlib compatible, and may be very slow),
  222. * MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. */
  223. enum {
  224. MZ_NO_COMPRESSION = 0,
  225. MZ_BEST_SPEED = 1,
  226. MZ_BEST_COMPRESSION = 9,
  227. MZ_UBER_COMPRESSION = 10,
  228. MZ_DEFAULT_LEVEL = 6,
  229. MZ_DEFAULT_COMPRESSION = -1
  230. };
  231. #define MZ_VERSION "10.2.0"
  232. #define MZ_VERNUM 0xA100
  233. #define MZ_VER_MAJOR 10
  234. #define MZ_VER_MINOR 2
  235. #define MZ_VER_REVISION 0
  236. #define MZ_VER_SUBREVISION 0
  237. #ifndef MINIZ_NO_ZLIB_APIS
  238. /* Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The
  239. * other values are for advanced use (refer to the zlib docs). */
  240. enum {
  241. MZ_NO_FLUSH = 0,
  242. MZ_PARTIAL_FLUSH = 1,
  243. MZ_SYNC_FLUSH = 2,
  244. MZ_FULL_FLUSH = 3,
  245. MZ_FINISH = 4,
  246. MZ_BLOCK = 5
  247. };
  248. /* Return status codes. MZ_PARAM_ERROR is non-standard. */
  249. enum {
  250. MZ_OK = 0,
  251. MZ_STREAM_END = 1,
  252. MZ_NEED_DICT = 2,
  253. MZ_ERRNO = -1,
  254. MZ_STREAM_ERROR = -2,
  255. MZ_DATA_ERROR = -3,
  256. MZ_MEM_ERROR = -4,
  257. MZ_BUF_ERROR = -5,
  258. MZ_VERSION_ERROR = -6,
  259. MZ_PARAM_ERROR = -10000
  260. };
  261. /* Window bits */
  262. #define MZ_DEFAULT_WINDOW_BITS 15
  263. struct mz_internal_state;
  264. /* Compression/decompression stream struct. */
  265. typedef struct mz_stream_s {
  266. const unsigned char *next_in; /* pointer to next byte to read */
  267. unsigned int avail_in; /* number of bytes available at next_in */
  268. mz_ulong total_in; /* total number of bytes consumed so far */
  269. unsigned char *next_out; /* pointer to next byte to write */
  270. unsigned int avail_out; /* number of bytes that can be written to next_out */
  271. mz_ulong total_out; /* total number of bytes produced so far */
  272. char *msg; /* error msg (unused) */
  273. struct mz_internal_state
  274. *state; /* internal state, allocated by zalloc/zfree */
  275. mz_alloc_func
  276. zalloc; /* optional heap allocation function (defaults to malloc) */
  277. mz_free_func zfree; /* optional heap free function (defaults to free) */
  278. void *opaque; /* heap alloc function user pointer */
  279. int data_type; /* data_type (unused) */
  280. mz_ulong adler; /* adler32 of the source or uncompressed data */
  281. mz_ulong reserved; /* not used */
  282. } mz_stream;
  283. typedef mz_stream *mz_streamp;
  284. /* Returns the version string of miniz.c. */
  285. const char *mz_version(void);
  286. /* mz_deflateInit() initializes a compressor with default options: */
  287. /* Parameters: */
  288. /* pStream must point to an initialized mz_stream struct. */
  289. /* level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. */
  290. /* level 1 enables a specially optimized compression function that's been
  291. * optimized purely for performance, not ratio. */
  292. /* (This special func. is currently only enabled when
  293. * MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) */
  294. /* Return values: */
  295. /* MZ_OK on success. */
  296. /* MZ_STREAM_ERROR if the stream is bogus. */
  297. /* MZ_PARAM_ERROR if the input parameters are bogus. */
  298. /* MZ_MEM_ERROR on out of memory. */
  299. int mz_deflateInit(mz_streamp pStream, int level);
  300. /* mz_deflateInit2() is like mz_deflate(), except with more control: */
  301. /* Additional parameters: */
  302. /* method must be MZ_DEFLATED */
  303. /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with
  304. * zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no
  305. * header or footer) */
  306. /* mem_level must be between [1, 9] (it's checked but ignored by miniz.c) */
  307. int mz_deflateInit2(mz_streamp pStream, int level, int method,
  308. int window_bits, int mem_level, int strategy);
  309. /* Quickly resets a compressor without having to reallocate anything. Same as
  310. * calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). */
  311. int mz_deflateReset(mz_streamp pStream);
  312. /* mz_deflate() compresses the input to output, consuming as much of the input
  313. * and producing as much output as possible. */
  314. /* Parameters: */
  315. /* pStream is the stream to read from and write to. You must initialize/update
  316. * the next_in, avail_in, next_out, and avail_out members. */
  317. /* flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or
  318. * MZ_FINISH. */
  319. /* Return values: */
  320. /* MZ_OK on success (when flushing, or if more input is needed but not
  321. * available, and/or there's more output to be written but the output buffer is
  322. * full). */
  323. /* MZ_STREAM_END if all input has been consumed and all output bytes have been
  324. * written. Don't call mz_deflate() on the stream anymore. */
  325. /* MZ_STREAM_ERROR if the stream is bogus. */
  326. /* MZ_PARAM_ERROR if one of the parameters is invalid. */
  327. /* MZ_BUF_ERROR if no forward progress is possible because the input and/or
  328. * output buffers are empty. (Fill up the input buffer or free up some output
  329. * space and try again.) */
  330. int mz_deflate(mz_streamp pStream, int flush);
  331. /* mz_deflateEnd() deinitializes a compressor: */
  332. /* Return values: */
  333. /* MZ_OK on success. */
  334. /* MZ_STREAM_ERROR if the stream is bogus. */
  335. int mz_deflateEnd(mz_streamp pStream);
  336. /* mz_deflateBound() returns a (very) conservative upper bound on the amount of
  337. * data that could be generated by deflate(), assuming flush is set to only
  338. * MZ_NO_FLUSH or MZ_FINISH. */
  339. mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len);
  340. /* Single-call compression functions mz_compress() and mz_compress2(): */
  341. /* Returns MZ_OK on success, or one of the error codes from mz_deflate() on
  342. * failure. */
  343. int mz_compress(unsigned char *pDest, mz_ulong *pDest_len,
  344. const unsigned char *pSource, mz_ulong source_len);
  345. int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len,
  346. const unsigned char *pSource, mz_ulong source_len,
  347. int level);
  348. /* mz_compressBound() returns a (very) conservative upper bound on the amount of
  349. * data that could be generated by calling mz_compress(). */
  350. mz_ulong mz_compressBound(mz_ulong source_len);
  351. /* Initializes a decompressor. */
  352. int mz_inflateInit(mz_streamp pStream);
  353. /* mz_inflateInit2() is like mz_inflateInit() with an additional option that
  354. * controls the window size and whether or not the stream has been wrapped with
  355. * a zlib header/footer: */
  356. /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or
  357. * -MZ_DEFAULT_WINDOW_BITS (raw deflate). */
  358. int mz_inflateInit2(mz_streamp pStream, int window_bits);
  359. /* Quickly resets a compressor without having to reallocate anything. Same as
  360. * calling mz_inflateEnd() followed by mz_inflateInit()/mz_inflateInit2(). */
  361. int mz_inflateReset(mz_streamp pStream);
  362. /* Decompresses the input stream to the output, consuming only as much of the
  363. * input as needed, and writing as much to the output as possible. */
  364. /* Parameters: */
  365. /* pStream is the stream to read from and write to. You must initialize/update
  366. * the next_in, avail_in, next_out, and avail_out members. */
  367. /* flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. */
  368. /* On the first call, if flush is MZ_FINISH it's assumed the input and output
  369. * buffers are both sized large enough to decompress the entire stream in a
  370. * single call (this is slightly faster). */
  371. /* MZ_FINISH implies that there are no more source bytes available beside
  372. * what's already in the input buffer, and that the output buffer is large
  373. * enough to hold the rest of the decompressed data. */
  374. /* Return values: */
  375. /* MZ_OK on success. Either more input is needed but not available, and/or
  376. * there's more output to be written but the output buffer is full. */
  377. /* MZ_STREAM_END if all needed input has been consumed and all output bytes
  378. * have been written. For zlib streams, the adler-32 of the decompressed data
  379. * has also been verified. */
  380. /* MZ_STREAM_ERROR if the stream is bogus. */
  381. /* MZ_DATA_ERROR if the deflate stream is invalid. */
  382. /* MZ_PARAM_ERROR if one of the parameters is invalid. */
  383. /* MZ_BUF_ERROR if no forward progress is possible because the input buffer is
  384. * empty but the inflater needs more input to continue, or if the output buffer
  385. * is not large enough. Call mz_inflate() again */
  386. /* with more input data, or with more room in the output buffer (except when
  387. * using single call decompression, described above). */
  388. int mz_inflate(mz_streamp pStream, int flush);
  389. /* Deinitializes a decompressor. */
  390. int mz_inflateEnd(mz_streamp pStream);
  391. /* Single-call decompression. */
  392. /* Returns MZ_OK on success, or one of the error codes from mz_inflate() on
  393. * failure. */
  394. int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len,
  395. const unsigned char *pSource,
  396. mz_ulong source_len);
  397. int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len,
  398. const unsigned char *pSource,
  399. mz_ulong *pSource_len);
  400. /* Returns a string description of the specified error code, or NULL if the
  401. * error code is invalid. */
  402. const char *mz_error(int err);
  403. /* Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used
  404. * as a drop-in replacement for the subset of zlib that miniz.c supports. */
  405. /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you
  406. * use zlib in the same project. */
  407. #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
  408. typedef unsigned char Byte;
  409. typedef unsigned int uInt;
  410. typedef mz_ulong uLong;
  411. typedef Byte Bytef;
  412. typedef uInt uIntf;
  413. typedef char charf;
  414. typedef int intf;
  415. typedef void *voidpf;
  416. typedef uLong uLongf;
  417. typedef void *voidp;
  418. typedef void *const voidpc;
  419. #define Z_NULL 0
  420. #define Z_NO_FLUSH MZ_NO_FLUSH
  421. #define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
  422. #define Z_SYNC_FLUSH MZ_SYNC_FLUSH
  423. #define Z_FULL_FLUSH MZ_FULL_FLUSH
  424. #define Z_FINISH MZ_FINISH
  425. #define Z_BLOCK MZ_BLOCK
  426. #define Z_OK MZ_OK
  427. #define Z_STREAM_END MZ_STREAM_END
  428. #define Z_NEED_DICT MZ_NEED_DICT
  429. #define Z_ERRNO MZ_ERRNO
  430. #define Z_STREAM_ERROR MZ_STREAM_ERROR
  431. #define Z_DATA_ERROR MZ_DATA_ERROR
  432. #define Z_MEM_ERROR MZ_MEM_ERROR
  433. #define Z_BUF_ERROR MZ_BUF_ERROR
  434. #define Z_VERSION_ERROR MZ_VERSION_ERROR
  435. #define Z_PARAM_ERROR MZ_PARAM_ERROR
  436. #define Z_NO_COMPRESSION MZ_NO_COMPRESSION
  437. #define Z_BEST_SPEED MZ_BEST_SPEED
  438. #define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
  439. #define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
  440. #define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
  441. #define Z_FILTERED MZ_FILTERED
  442. #define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
  443. #define Z_RLE MZ_RLE
  444. #define Z_FIXED MZ_FIXED
  445. #define Z_DEFLATED MZ_DEFLATED
  446. #define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
  447. #define alloc_func mz_alloc_func
  448. #define free_func mz_free_func
  449. #define internal_state mz_internal_state
  450. #define z_stream mz_stream
  451. #define deflateInit mz_deflateInit
  452. #define deflateInit2 mz_deflateInit2
  453. #define deflateReset mz_deflateReset
  454. #define deflate mz_deflate
  455. #define deflateEnd mz_deflateEnd
  456. #define deflateBound mz_deflateBound
  457. #define compress mz_compress
  458. #define compress2 mz_compress2
  459. #define compressBound mz_compressBound
  460. #define inflateInit mz_inflateInit
  461. #define inflateInit2 mz_inflateInit2
  462. #define inflateReset mz_inflateReset
  463. #define inflate mz_inflate
  464. #define inflateEnd mz_inflateEnd
  465. #define uncompress mz_uncompress
  466. #define uncompress2 mz_uncompress2
  467. #define crc32 mz_crc32
  468. #define adler32 mz_adler32
  469. #define MAX_WBITS 15
  470. #define MAX_MEM_LEVEL 9
  471. #define zError mz_error
  472. #define ZLIB_VERSION MZ_VERSION
  473. #define ZLIB_VERNUM MZ_VERNUM
  474. #define ZLIB_VER_MAJOR MZ_VER_MAJOR
  475. #define ZLIB_VER_MINOR MZ_VER_MINOR
  476. #define ZLIB_VER_REVISION MZ_VER_REVISION
  477. #define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
  478. #define zlibVersion mz_version
  479. #define zlib_version mz_version()
  480. #endif /* #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES */
  481. #endif /* MINIZ_NO_ZLIB_APIS */
  482. #pragma once
  483. #include <assert.h>
  484. #include <stdint.h>
  485. #include <stdlib.h>
  486. #include <string.h>
  487. /* ------------------- Types and macros */
  488. typedef unsigned char mz_uint8;
  489. typedef signed short mz_int16;
  490. typedef unsigned short mz_uint16;
  491. typedef unsigned int mz_uint32;
  492. typedef unsigned int mz_uint;
  493. typedef int64_t mz_int64;
  494. typedef uint64_t mz_uint64;
  495. typedef int mz_bool;
  496. #define MZ_FALSE 0
  497. #define MZ_TRUE 1
  498. /* Works around MSVC's spammy "warning C4127: conditional expression is
  499. * constant" message. */
  500. #ifdef _MSC_VER
  501. #define MZ_MACRO_END while (0, 0)
  502. #else
  503. #define MZ_MACRO_END while (0)
  504. #endif
  505. #ifdef MINIZ_NO_STDIO
  506. #define MZ_FILE void *
  507. #else
  508. #include <stdio.h>
  509. #define MZ_FILE FILE
  510. #endif /* #ifdef MINIZ_NO_STDIO */
  511. #ifdef MINIZ_NO_TIME
  512. typedef struct mz_dummy_time_t_tag {
  513. int m_dummy;
  514. } mz_dummy_time_t;
  515. #define MZ_TIME_T mz_dummy_time_t
  516. #else
  517. #define MZ_TIME_T time_t
  518. #endif
  519. #define MZ_ASSERT(x) assert(x)
  520. #ifdef MINIZ_NO_MALLOC
  521. #define MZ_MALLOC(x) NULL
  522. #define MZ_FREE(x) (void)x, ((void)0)
  523. #define MZ_REALLOC(p, x) NULL
  524. #else
  525. #define MZ_MALLOC(x) malloc(x)
  526. #define MZ_FREE(x) free(x)
  527. #define MZ_REALLOC(p, x) realloc(p, x)
  528. #endif
  529. #define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b))
  530. #define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b))
  531. #define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
  532. #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
  533. #define MZ_READ_LE16(p) *((const mz_uint16 *)(p))
  534. #define MZ_READ_LE32(p) *((const mz_uint32 *)(p))
  535. #else
  536. #define MZ_READ_LE16(p) \
  537. ((mz_uint32)(((const mz_uint8 *)(p))[0]) | \
  538. ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U))
  539. #define MZ_READ_LE32(p) \
  540. ((mz_uint32)(((const mz_uint8 *)(p))[0]) | \
  541. ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | \
  542. ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | \
  543. ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U))
  544. #endif
  545. #define MZ_READ_LE64(p) \
  546. (((mz_uint64)MZ_READ_LE32(p)) | \
  547. (((mz_uint64)MZ_READ_LE32((const mz_uint8 *)(p) + sizeof(mz_uint32))) \
  548. << 32U))
  549. #ifdef _MSC_VER
  550. #define MZ_FORCEINLINE __forceinline
  551. #elif defined(__GNUC__)
  552. #define MZ_FORCEINLINE __inline__ __attribute__((__always_inline__))
  553. #else
  554. #define MZ_FORCEINLINE inline
  555. #endif
  556. extern void *miniz_def_alloc_func(void *opaque, size_t items,
  557. size_t size);
  558. extern void miniz_def_free_func(void *opaque, void *address);
  559. extern void *miniz_def_realloc_func(void *opaque, void *address,
  560. size_t items, size_t size);
  561. #define MZ_UINT16_MAX (0xFFFFU)
  562. #define MZ_UINT32_MAX (0xFFFFFFFFU)
  563. #pragma once
  564. /* ------------------- Low-level Compression API Definitions */
  565. /* Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly
  566. * slower, and raw/dynamic blocks will be output more frequently). */
  567. #define TDEFL_LESS_MEMORY 0
  568. /* tdefl_init() compression flags logically OR'd together (low 12 bits contain
  569. * the max. number of probes per dictionary search): */
  570. /* TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes
  571. * per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap
  572. * compression), 4095=Huffman+LZ (slowest/best compression). */
  573. enum {
  574. TDEFL_HUFFMAN_ONLY = 0,
  575. TDEFL_DEFAULT_MAX_PROBES = 128,
  576. TDEFL_MAX_PROBES_MASK = 0xFFF
  577. };
  578. /* TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before
  579. * the deflate data, and the Adler-32 of the source data at the end. Otherwise,
  580. * you'll get raw deflate data. */
  581. /* TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even
  582. * when not writing zlib headers). */
  583. /* TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more
  584. * efficient lazy parsing. */
  585. /* TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's
  586. * initialization time to the minimum, but the output may vary from run to run
  587. * given the same input (depending on the contents of memory). */
  588. /* TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
  589. */
  590. /* TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. */
  591. /* TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. */
  592. /* TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. */
  593. /* The low 12 bits are reserved to control the max # of hash probes per
  594. * dictionary lookup (see TDEFL_MAX_PROBES_MASK). */
  595. enum {
  596. TDEFL_WRITE_ZLIB_HEADER = 0x01000,
  597. TDEFL_COMPUTE_ADLER32 = 0x02000,
  598. TDEFL_GREEDY_PARSING_FLAG = 0x04000,
  599. TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
  600. TDEFL_RLE_MATCHES = 0x10000,
  601. TDEFL_FILTER_MATCHES = 0x20000,
  602. TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
  603. TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
  604. };
  605. /* High level compression functions: */
  606. /* tdefl_compress_mem_to_heap() compresses a block in memory to a heap block
  607. * allocated via malloc(). */
  608. /* On entry: */
  609. /* pSrc_buf, src_buf_len: Pointer and size of source block to compress. */
  610. /* flags: The max match finder probes (default is 128) logically OR'd against
  611. * the above flags. Higher probes are slower but improve compression. */
  612. /* On return: */
  613. /* Function returns a pointer to the compressed data, or NULL on failure. */
  614. /* *pOut_len will be set to the compressed data's size, which could be larger
  615. * than src_buf_len on uncompressible data. */
  616. /* The caller must free() the returned block when it's no longer needed. */
  617. void *tdefl_compress_mem_to_heap(const void *pSrc_buf,
  618. size_t src_buf_len,
  619. size_t *pOut_len, int flags);
  620. /* tdefl_compress_mem_to_mem() compresses a block in memory to another block in
  621. * memory. */
  622. /* Returns 0 on failure. */
  623. size_t tdefl_compress_mem_to_mem(void *pOut_buf,
  624. size_t out_buf_len,
  625. const void *pSrc_buf,
  626. size_t src_buf_len, int flags);
  627. /* Compresses an image to a compressed PNG file in memory. */
  628. /* On entry: */
  629. /* pImage, w, h, and num_chans describe the image to compress. num_chans may be
  630. * 1, 2, 3, or 4. */
  631. /* The image pitch in bytes per scanline will be w*num_chans. The leftmost
  632. * pixel on the top scanline is stored first in memory. */
  633. /* level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED,
  634. * MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL */
  635. /* If flip is true, the image will be flipped on the Y axis (useful for OpenGL
  636. * apps). */
  637. /* On return: */
  638. /* Function returns a pointer to the compressed data, or NULL on failure. */
  639. /* *pLen_out will be set to the size of the PNG image file. */
  640. /* The caller must mz_free() the returned heap block (which will typically be
  641. * larger than *pLen_out) when it's no longer needed. */
  642. void *
  643. tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h,
  644. int num_chans, size_t *pLen_out,
  645. mz_uint level, mz_bool flip);
  646. void *tdefl_write_image_to_png_file_in_memory(const void *pImage,
  647. int w, int h,
  648. int num_chans,
  649. size_t *pLen_out);
  650. /* Output stream interface. The compressor uses this interface to write
  651. * compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. */
  652. typedef mz_bool (*tdefl_put_buf_func_ptr)(const void *pBuf, int len,
  653. void *pUser);
  654. /* tdefl_compress_mem_to_output() compresses a block to an output stream. The
  655. * above helpers use this function internally. */
  656. mz_bool tdefl_compress_mem_to_output(
  657. const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func,
  658. void *pPut_buf_user, int flags);
  659. enum {
  660. TDEFL_MAX_HUFF_TABLES = 3,
  661. TDEFL_MAX_HUFF_SYMBOLS_0 = 288,
  662. TDEFL_MAX_HUFF_SYMBOLS_1 = 32,
  663. TDEFL_MAX_HUFF_SYMBOLS_2 = 19,
  664. TDEFL_LZ_DICT_SIZE = 32768,
  665. TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1,
  666. TDEFL_MIN_MATCH_LEN = 3,
  667. TDEFL_MAX_MATCH_LEN = 258
  668. };
  669. /* TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed
  670. * output block (using static/fixed Huffman codes). */
  671. #if TDEFL_LESS_MEMORY
  672. enum {
  673. TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024,
  674. TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10,
  675. TDEFL_MAX_HUFF_SYMBOLS = 288,
  676. TDEFL_LZ_HASH_BITS = 12,
  677. TDEFL_LEVEL1_HASH_SIZE_MASK = 4095,
  678. TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3,
  679. TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
  680. };
  681. #else
  682. enum {
  683. TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024,
  684. TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10,
  685. TDEFL_MAX_HUFF_SYMBOLS = 288,
  686. TDEFL_LZ_HASH_BITS = 15,
  687. TDEFL_LEVEL1_HASH_SIZE_MASK = 4095,
  688. TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3,
  689. TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
  690. };
  691. #endif
  692. /* The low-level tdefl functions below may be used directly if the above helper
  693. * functions aren't flexible enough. The low-level functions don't make any heap
  694. * allocations, unlike the above helper functions. */
  695. typedef enum {
  696. TDEFL_STATUS_BAD_PARAM = -2,
  697. TDEFL_STATUS_PUT_BUF_FAILED = -1,
  698. TDEFL_STATUS_OKAY = 0,
  699. TDEFL_STATUS_DONE = 1
  700. } tdefl_status;
  701. /* Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums */
  702. typedef enum {
  703. TDEFL_NO_FLUSH = 0,
  704. TDEFL_SYNC_FLUSH = 2,
  705. TDEFL_FULL_FLUSH = 3,
  706. TDEFL_FINISH = 4
  707. } tdefl_flush;
  708. /* tdefl's compression state structure. */
  709. typedef struct {
  710. tdefl_put_buf_func_ptr m_pPut_buf_func;
  711. void *m_pPut_buf_user;
  712. mz_uint m_flags, m_max_probes[2];
  713. int m_greedy_parsing;
  714. mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
  715. mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
  716. mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in,
  717. m_bit_buffer;
  718. mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit,
  719. m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index,
  720. m_wants_to_finish;
  721. tdefl_status m_prev_return_status;
  722. const void *m_pIn_buf;
  723. void *m_pOut_buf;
  724. size_t *m_pIn_buf_size, *m_pOut_buf_size;
  725. tdefl_flush m_flush;
  726. const mz_uint8 *m_pSrc;
  727. size_t m_src_buf_left, m_out_buf_ofs;
  728. mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
  729. mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
  730. mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
  731. mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
  732. mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
  733. mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
  734. mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
  735. mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
  736. } tdefl_compressor;
  737. /* Initializes the compressor. */
  738. /* There is no corresponding deinit() function because the tdefl API's do not
  739. * dynamically allocate memory. */
  740. /* pBut_buf_func: If NULL, output data will be supplied to the specified
  741. * callback. In this case, the user should call the tdefl_compress_buffer() API
  742. * for compression. */
  743. /* If pBut_buf_func is NULL the user should always call the tdefl_compress()
  744. * API. */
  745. /* flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER,
  746. * etc.) */
  747. tdefl_status tdefl_init(tdefl_compressor *d,
  748. tdefl_put_buf_func_ptr pPut_buf_func,
  749. void *pPut_buf_user, int flags);
  750. /* Compresses a block of data, consuming as much of the specified input buffer
  751. * as possible, and writing as much compressed data to the specified output
  752. * buffer as possible. */
  753. tdefl_status tdefl_compress(tdefl_compressor *d,
  754. const void *pIn_buf,
  755. size_t *pIn_buf_size, void *pOut_buf,
  756. size_t *pOut_buf_size,
  757. tdefl_flush flush);
  758. /* tdefl_compress_buffer() is only usable when the tdefl_init() is called with a
  759. * non-NULL tdefl_put_buf_func_ptr. */
  760. /* tdefl_compress_buffer() always consumes the entire input buffer. */
  761. tdefl_status tdefl_compress_buffer(tdefl_compressor *d,
  762. const void *pIn_buf,
  763. size_t in_buf_size,
  764. tdefl_flush flush);
  765. tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
  766. mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
  767. /* Create tdefl_compress() flags given zlib-style compression parameters. */
  768. /* level may range from [0,10] (where 10 is absolute max compression, but may be
  769. * much slower on some files) */
  770. /* window_bits may be -15 (raw deflate) or 15 (zlib) */
  771. /* strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY,
  772. * MZ_RLE, or MZ_FIXED */
  773. mz_uint tdefl_create_comp_flags_from_zip_params(int level,
  774. int window_bits,
  775. int strategy);
  776. #ifndef MINIZ_NO_MALLOC
  777. /* Allocate the tdefl_compressor structure in C so that */
  778. /* non-C language bindings to tdefl_ API don't need to worry about */
  779. /* structure size and allocation mechanism. */
  780. tdefl_compressor *tdefl_compressor_alloc(void);
  781. void tdefl_compressor_free(tdefl_compressor *pComp);
  782. #endif
  783. #pragma once
  784. /* ------------------- Low-level Decompression API Definitions */
  785. /* Decompression flags used by tinfl_decompress(). */
  786. /* TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and
  787. * ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the
  788. * input is a raw deflate stream. */
  789. /* TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available
  790. * beyond the end of the supplied input buffer. If clear, the input buffer
  791. * contains all remaining input. */
  792. /* TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large
  793. * enough to hold the entire decompressed stream. If clear, the output buffer is
  794. * at least the size of the dictionary (typically 32KB). */
  795. /* TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the
  796. * decompressed bytes. */
  797. enum {
  798. TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
  799. TINFL_FLAG_HAS_MORE_INPUT = 2,
  800. TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
  801. TINFL_FLAG_COMPUTE_ADLER32 = 8
  802. };
  803. /* High level decompression functions: */
  804. /* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block
  805. * allocated via malloc(). */
  806. /* On entry: */
  807. /* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data
  808. * to decompress. */
  809. /* On return: */
  810. /* Function returns a pointer to the decompressed data, or NULL on failure. */
  811. /* *pOut_len will be set to the decompressed data's size, which could be larger
  812. * than src_buf_len on uncompressible data. */
  813. /* The caller must call mz_free() on the returned block when it's no longer
  814. * needed. */
  815. void *tinfl_decompress_mem_to_heap(const void *pSrc_buf,
  816. size_t src_buf_len,
  817. size_t *pOut_len, int flags);
  818. /* tinfl_decompress_mem_to_mem() decompresses a block in memory to another block
  819. * in memory. */
  820. /* Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes
  821. * written on success. */
  822. #define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
  823. size_t tinfl_decompress_mem_to_mem(void *pOut_buf,
  824. size_t out_buf_len,
  825. const void *pSrc_buf,
  826. size_t src_buf_len, int flags);
  827. /* tinfl_decompress_mem_to_callback() decompresses a block in memory to an
  828. * internal 32KB buffer, and a user provided callback function will be called to
  829. * flush the buffer. */
  830. /* Returns 1 on success or 0 on failure. */
  831. typedef int (*tinfl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser);
  832. int
  833. tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size,
  834. tinfl_put_buf_func_ptr pPut_buf_func,
  835. void *pPut_buf_user, int flags);
  836. struct tinfl_decompressor_tag;
  837. typedef struct tinfl_decompressor_tag tinfl_decompressor;
  838. #ifndef MINIZ_NO_MALLOC
  839. /* Allocate the tinfl_decompressor structure in C so that */
  840. /* non-C language bindings to tinfl_ API don't need to worry about */
  841. /* structure size and allocation mechanism. */
  842. tinfl_decompressor *tinfl_decompressor_alloc(void);
  843. void tinfl_decompressor_free(tinfl_decompressor *pDecomp);
  844. #endif
  845. /* Max size of LZ dictionary. */
  846. #define TINFL_LZ_DICT_SIZE 32768
  847. /* Return status. */
  848. typedef enum {
  849. /* This flags indicates the inflator needs 1 or more input bytes to make
  850. forward progress, but the caller is indicating that no more are available.
  851. The compressed data */
  852. /* is probably corrupted. If you call the inflator again with more bytes it'll
  853. try to continue processing the input but this is a BAD sign (either the
  854. data is corrupted or you called it incorrectly). */
  855. /* If you call it again with no input you'll just get
  856. TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */
  857. TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4,
  858. /* This flag indicates that one or more of the input parameters was obviously
  859. bogus. (You can try calling it again, but if you get this error the calling
  860. code is wrong.) */
  861. TINFL_STATUS_BAD_PARAM = -3,
  862. /* This flags indicate the inflator is finished but the adler32 check of the
  863. uncompressed data didn't match. If you call it again it'll return
  864. TINFL_STATUS_DONE. */
  865. TINFL_STATUS_ADLER32_MISMATCH = -2,
  866. /* This flags indicate the inflator has somehow failed (bad code, corrupted
  867. input, etc.). If you call it again without resetting via tinfl_init() it
  868. it'll just keep on returning the same status failure code. */
  869. TINFL_STATUS_FAILED = -1,
  870. /* Any status code less than TINFL_STATUS_DONE must indicate a failure. */
  871. /* This flag indicates the inflator has returned every byte of uncompressed
  872. data that it can, has consumed every byte that it needed, has successfully
  873. reached the end of the deflate stream, and */
  874. /* if zlib headers and adler32 checking enabled that it has successfully
  875. checked the uncompressed data's adler32. If you call it again you'll just
  876. get TINFL_STATUS_DONE over and over again. */
  877. TINFL_STATUS_DONE = 0,
  878. /* This flag indicates the inflator MUST have more input data (even 1 byte)
  879. before it can make any more forward progress, or you need to clear the
  880. TINFL_FLAG_HAS_MORE_INPUT */
  881. /* flag on the next call if you don't have any more source data. If the source
  882. data was somehow corrupted it's also possible (but unlikely) for the
  883. inflator to keep on demanding input to */
  884. /* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */
  885. TINFL_STATUS_NEEDS_MORE_INPUT = 1,
  886. /* This flag indicates the inflator definitely has 1 or more bytes of
  887. uncompressed data available, but it cannot write this data into the output
  888. buffer. */
  889. /* Note if the source compressed data was corrupted it's possible for the
  890. inflator to return a lot of uncompressed data to the caller. I've been
  891. assuming you know how much uncompressed data to expect */
  892. /* (either exact or worst case) and will stop calling the inflator and fail
  893. after receiving too much. In pure streaming scenarios where you have no
  894. idea how many bytes to expect this may not be possible */
  895. /* so I may need to add some code to address this. */
  896. TINFL_STATUS_HAS_MORE_OUTPUT = 2
  897. } tinfl_status;
  898. /* Initializes the decompressor to its initial state. */
  899. #define tinfl_init(r) \
  900. do { \
  901. (r)->m_state = 0; \
  902. } \
  903. MZ_MACRO_END
  904. #define tinfl_get_adler32(r) (r)->m_check_adler32
  905. /* Main low-level decompressor coroutine function. This is the only function
  906. * actually needed for decompression. All the other functions are just
  907. * high-level helpers for improved usability. */
  908. /* This is a universal API, i.e. it can be used as a building block to build any
  909. * desired higher level decompression API. In the limit case, it can be called
  910. * once per every byte input or output. */
  911. tinfl_status tinfl_decompress(
  912. tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size,
  913. mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size,
  914. const mz_uint32 decomp_flags);
  915. /* Internal/private bits follow. */
  916. enum {
  917. TINFL_MAX_HUFF_TABLES = 3,
  918. TINFL_MAX_HUFF_SYMBOLS_0 = 288,
  919. TINFL_MAX_HUFF_SYMBOLS_1 = 32,
  920. TINFL_MAX_HUFF_SYMBOLS_2 = 19,
  921. TINFL_FAST_LOOKUP_BITS = 10,
  922. TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
  923. };
  924. typedef struct {
  925. mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
  926. mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE],
  927. m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
  928. } tinfl_huff_table;
  929. #if MINIZ_HAS_64BIT_REGISTERS
  930. #define TINFL_USE_64BIT_BITBUF 1
  931. #else
  932. #define TINFL_USE_64BIT_BITBUF 0
  933. #endif
  934. #if TINFL_USE_64BIT_BITBUF
  935. typedef mz_uint64 tinfl_bit_buf_t;
  936. #define TINFL_BITBUF_SIZE (64)
  937. #else
  938. typedef mz_uint32 tinfl_bit_buf_t;
  939. #define TINFL_BITBUF_SIZE (32)
  940. #endif
  941. struct tinfl_decompressor_tag {
  942. mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type,
  943. m_check_adler32, m_dist, m_counter, m_num_extra,
  944. m_table_sizes[TINFL_MAX_HUFF_TABLES];
  945. tinfl_bit_buf_t m_bit_buf;
  946. size_t m_dist_from_out_buf_start;
  947. tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
  948. mz_uint8 m_raw_header[4],
  949. m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
  950. };
  951. #pragma once
  952. /* ------------------- ZIP archive reading/writing */
  953. #ifndef MINIZ_NO_ARCHIVE_APIS
  954. enum {
  955. /* Note: These enums can be reduced as needed to save memory or stack space -
  956. they are pretty conservative. */
  957. MZ_ZIP_MAX_IO_BUF_SIZE = 8 * 1024,
  958. MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 512,
  959. MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512
  960. };
  961. typedef struct {
  962. /* Central directory file index. */
  963. mz_uint32 m_file_index;
  964. /* Byte offset of this entry in the archive's central directory. Note we
  965. * currently only support up to UINT_MAX or less bytes in the central dir. */
  966. mz_uint64 m_central_dir_ofs;
  967. /* These fields are copied directly from the zip's central dir. */
  968. mz_uint16 m_version_made_by;
  969. mz_uint16 m_version_needed;
  970. mz_uint16 m_bit_flag;
  971. mz_uint16 m_method;
  972. #ifndef MINIZ_NO_TIME
  973. MZ_TIME_T m_time;
  974. #endif
  975. /* CRC-32 of uncompressed data. */
  976. mz_uint32 m_crc32;
  977. /* File's compressed size. */
  978. mz_uint64 m_comp_size;
  979. /* File's uncompressed size. Note, I've seen some old archives where directory
  980. * entries had 512 bytes for their uncompressed sizes, but when you try to
  981. * unpack them you actually get 0 bytes. */
  982. mz_uint64 m_uncomp_size;
  983. /* Zip internal and external file attributes. */
  984. mz_uint16 m_internal_attr;
  985. mz_uint32 m_external_attr;
  986. /* Entry's local header file offset in bytes. */
  987. mz_uint64 m_local_header_ofs;
  988. /* Size of comment in bytes. */
  989. mz_uint32 m_comment_size;
  990. /* MZ_TRUE if the entry appears to be a directory. */
  991. mz_bool m_is_directory;
  992. /* MZ_TRUE if the entry uses encryption/strong encryption (which miniz_zip
  993. * doesn't support) */
  994. mz_bool m_is_encrypted;
  995. /* MZ_TRUE if the file is not encrypted, a patch file, and if it uses a
  996. * compression method we support. */
  997. mz_bool m_is_supported;
  998. /* Filename. If string ends in '/' it's a subdirectory entry. */
  999. /* Guaranteed to be zero terminated, may be truncated to fit. */
  1000. char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
  1001. /* Comment field. */
  1002. /* Guaranteed to be zero terminated, may be truncated to fit. */
  1003. char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
  1004. } mz_zip_archive_file_stat;
  1005. typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs,
  1006. void *pBuf, size_t n);
  1007. typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs,
  1008. const void *pBuf, size_t n);
  1009. typedef mz_bool (*mz_file_needs_keepalive)(void *pOpaque);
  1010. struct mz_zip_internal_state_tag;
  1011. typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
  1012. typedef enum {
  1013. MZ_ZIP_MODE_INVALID = 0,
  1014. MZ_ZIP_MODE_READING = 1,
  1015. MZ_ZIP_MODE_WRITING = 2,
  1016. MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
  1017. } mz_zip_mode;
  1018. typedef enum {
  1019. MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
  1020. MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
  1021. MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
  1022. MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800,
  1023. MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG =
  1024. 0x1000, /* if enabled, mz_zip_reader_locate_file() will be called on each
  1025. file as its validated to ensure the func finds the file in the
  1026. central dir (intended for testing) */
  1027. MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY =
  1028. 0x2000, /* validate the local headers, but don't decompress the entire
  1029. file and check the crc32 */
  1030. MZ_ZIP_FLAG_WRITE_ZIP64 =
  1031. 0x4000, /* always use the zip64 file format, instead of the original zip
  1032. file format with automatic switch to zip64. Use as flags
  1033. parameter with mz_zip_writer_init*_v2 */
  1034. MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000,
  1035. MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000,
  1036. /*After adding a compressed file, seek back
  1037. to local file header and set the correct sizes*/
  1038. MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000
  1039. } mz_zip_flags;
  1040. typedef enum {
  1041. MZ_ZIP_TYPE_INVALID = 0,
  1042. MZ_ZIP_TYPE_USER,
  1043. MZ_ZIP_TYPE_MEMORY,
  1044. MZ_ZIP_TYPE_HEAP,
  1045. MZ_ZIP_TYPE_FILE,
  1046. MZ_ZIP_TYPE_CFILE,
  1047. MZ_ZIP_TOTAL_TYPES
  1048. } mz_zip_type;
  1049. /* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or
  1050. * modify this enum. */
  1051. typedef enum {
  1052. MZ_ZIP_NO_ERROR = 0,
  1053. MZ_ZIP_UNDEFINED_ERROR,
  1054. MZ_ZIP_TOO_MANY_FILES,
  1055. MZ_ZIP_FILE_TOO_LARGE,
  1056. MZ_ZIP_UNSUPPORTED_METHOD,
  1057. MZ_ZIP_UNSUPPORTED_ENCRYPTION,
  1058. MZ_ZIP_UNSUPPORTED_FEATURE,
  1059. MZ_ZIP_FAILED_FINDING_CENTRAL_DIR,
  1060. MZ_ZIP_NOT_AN_ARCHIVE,
  1061. MZ_ZIP_INVALID_HEADER_OR_CORRUPTED,
  1062. MZ_ZIP_UNSUPPORTED_MULTIDISK,
  1063. MZ_ZIP_DECOMPRESSION_FAILED,
  1064. MZ_ZIP_COMPRESSION_FAILED,
  1065. MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE,
  1066. MZ_ZIP_CRC_CHECK_FAILED,
  1067. MZ_ZIP_UNSUPPORTED_CDIR_SIZE,
  1068. MZ_ZIP_ALLOC_FAILED,
  1069. MZ_ZIP_FILE_OPEN_FAILED,
  1070. MZ_ZIP_FILE_CREATE_FAILED,
  1071. MZ_ZIP_FILE_WRITE_FAILED,
  1072. MZ_ZIP_FILE_READ_FAILED,
  1073. MZ_ZIP_FILE_CLOSE_FAILED,
  1074. MZ_ZIP_FILE_SEEK_FAILED,
  1075. MZ_ZIP_FILE_STAT_FAILED,
  1076. MZ_ZIP_INVALID_PARAMETER,
  1077. MZ_ZIP_INVALID_FILENAME,
  1078. MZ_ZIP_BUF_TOO_SMALL,
  1079. MZ_ZIP_INTERNAL_ERROR,
  1080. MZ_ZIP_FILE_NOT_FOUND,
  1081. MZ_ZIP_ARCHIVE_TOO_LARGE,
  1082. MZ_ZIP_VALIDATION_FAILED,
  1083. MZ_ZIP_WRITE_CALLBACK_FAILED,
  1084. MZ_ZIP_TOTAL_ERRORS
  1085. } mz_zip_error;
  1086. typedef struct {
  1087. mz_uint64 m_archive_size;
  1088. mz_uint64 m_central_directory_file_ofs;
  1089. /* We only support up to UINT32_MAX files in zip64 mode. */
  1090. mz_uint32 m_total_files;
  1091. mz_zip_mode m_zip_mode;
  1092. mz_zip_type m_zip_type;
  1093. mz_zip_error m_last_error;
  1094. mz_uint64 m_file_offset_alignment;
  1095. mz_alloc_func m_pAlloc;
  1096. mz_free_func m_pFree;
  1097. mz_realloc_func m_pRealloc;
  1098. void *m_pAlloc_opaque;
  1099. mz_file_read_func m_pRead;
  1100. mz_file_write_func m_pWrite;
  1101. mz_file_needs_keepalive m_pNeeds_keepalive;
  1102. void *m_pIO_opaque;
  1103. mz_zip_internal_state *m_pState;
  1104. } mz_zip_archive;
  1105. typedef struct {
  1106. mz_zip_archive *pZip;
  1107. mz_uint flags;
  1108. int status;
  1109. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  1110. mz_uint file_crc32;
  1111. #endif
  1112. mz_uint64 read_buf_size, read_buf_ofs, read_buf_avail, comp_remaining,
  1113. out_buf_ofs, cur_file_ofs;
  1114. mz_zip_archive_file_stat file_stat;
  1115. void *pRead_buf;
  1116. void *pWrite_buf;
  1117. size_t out_blk_remain;
  1118. tinfl_decompressor inflator;
  1119. } mz_zip_reader_extract_iter_state;
  1120. /* -------- ZIP reading */
  1121. /* Inits a ZIP archive reader. */
  1122. /* These functions read and validate the archive's central directory. */
  1123. mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size,
  1124. mz_uint flags);
  1125. mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip,
  1126. const void *pMem, size_t size,
  1127. mz_uint flags);
  1128. #ifndef MINIZ_NO_STDIO
  1129. /* Read a archive from a disk file. */
  1130. /* file_start_ofs is the file offset where the archive actually begins, or 0. */
  1131. /* actual_archive_size is the true total size of the archive, which may be
  1132. * smaller than the file's actual size on disk. If zero the entire file is
  1133. * treated as the archive. */
  1134. mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip,
  1135. const char *pFilename,
  1136. mz_uint32 flags);
  1137. mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip,
  1138. const char *pFilename,
  1139. mz_uint flags,
  1140. mz_uint64 file_start_ofs,
  1141. mz_uint64 archive_size);
  1142. mz_bool mz_zip_reader_init_file_v2_rpb(mz_zip_archive *pZip,
  1143. const char *pFilename,
  1144. mz_uint flags,
  1145. mz_uint64 file_start_ofs,
  1146. mz_uint64 archive_size);
  1147. /* Read an archive from an already opened FILE, beginning at the current file
  1148. * position. */
  1149. /* The archive is assumed to be archive_size bytes long. If archive_size is 0,
  1150. * then the entire rest of the file is assumed to contain the archive. */
  1151. /* The FILE will NOT be closed when mz_zip_reader_end() is called. */
  1152. mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip,
  1153. MZ_FILE *pFile,
  1154. mz_uint64 archive_size,
  1155. mz_uint flags);
  1156. #endif
  1157. /* Ends archive reading, freeing all allocations, and closing the input archive
  1158. * file if mz_zip_reader_init_file() was used. */
  1159. mz_bool mz_zip_reader_end(mz_zip_archive *pZip);
  1160. /* -------- ZIP reading or writing */
  1161. /* Clears a mz_zip_archive struct to all zeros. */
  1162. /* Important: This must be done before passing the struct to any mz_zip
  1163. * functions. */
  1164. void mz_zip_zero_struct(mz_zip_archive *pZip);
  1165. mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip);
  1166. mz_zip_type mz_zip_get_type(mz_zip_archive *pZip);
  1167. /* Returns the total number of files in the archive. */
  1168. mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip);
  1169. mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip);
  1170. mz_uint64
  1171. mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip);
  1172. MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip);
  1173. /* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf.
  1174. */
  1175. size_t mz_zip_read_archive_data(mz_zip_archive *pZip,
  1176. mz_uint64 file_ofs, void *pBuf,
  1177. size_t n);
  1178. /* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct.
  1179. * These functions retrieve/manipulate this field. */
  1180. /* Note that the m_last_error functionality is not thread safe. */
  1181. mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip,
  1182. mz_zip_error err_num);
  1183. mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip);
  1184. mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip);
  1185. mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip);
  1186. const char *mz_zip_get_error_string(mz_zip_error mz_err);
  1187. /* MZ_TRUE if the archive file entry is a directory entry. */
  1188. mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip,
  1189. mz_uint file_index);
  1190. /* MZ_TRUE if the file is encrypted/strong encrypted. */
  1191. mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip,
  1192. mz_uint file_index);
  1193. /* MZ_TRUE if the compression method is supported, and the file is not
  1194. * encrypted, and the file is not a compressed patch file. */
  1195. mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip,
  1196. mz_uint file_index);
  1197. /* Retrieves the filename of an archive file entry. */
  1198. /* Returns the number of bytes written to pFilename, or if filename_buf_size is
  1199. * 0 this function returns the number of bytes needed to fully store the
  1200. * filename. */
  1201. mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip,
  1202. mz_uint file_index,
  1203. char *pFilename,
  1204. mz_uint filename_buf_size);
  1205. /* Attempts to locates a file in the archive's central directory. */
  1206. /* Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH */
  1207. /* Returns -1 if the file cannot be found. */
  1208. int mz_zip_reader_locate_file(mz_zip_archive *pZip,
  1209. const char *pName,
  1210. const char *pComment, mz_uint flags);
  1211. mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip,
  1212. const char *pName,
  1213. const char *pComment,
  1214. mz_uint flags,
  1215. mz_uint32 *file_index);
  1216. /* Returns detailed information about an archive file entry. */
  1217. mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip,
  1218. mz_uint file_index,
  1219. mz_zip_archive_file_stat *pStat);
  1220. /* MZ_TRUE if the file is in zip64 format. */
  1221. /* A file is considered zip64 if it contained a zip64 end of central directory
  1222. * marker, or if it contained any zip64 extended file information fields in the
  1223. * central directory. */
  1224. mz_bool mz_zip_is_zip64(mz_zip_archive *pZip);
  1225. /* Returns the total central directory size in bytes. */
  1226. /* The current max supported size is <= MZ_UINT32_MAX. */
  1227. size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip);
  1228. /* Extracts a archive file to a memory buffer using no memory allocation. */
  1229. /* There must be at least enough room on the stack to store the inflator's state
  1230. * (~34KB or so). */
  1231. mz_bool mz_zip_reader_extract_to_mem_no_alloc(
  1232. mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size,
  1233. mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size);
  1234. mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(
  1235. mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size,
  1236. mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size);
  1237. /* Extracts a archive file to a memory buffer. */
  1238. mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip,
  1239. mz_uint file_index,
  1240. void *pBuf, size_t buf_size,
  1241. mz_uint flags);
  1242. mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip,
  1243. const char *pFilename,
  1244. void *pBuf,
  1245. size_t buf_size,
  1246. mz_uint flags);
  1247. /* Extracts a archive file to a dynamically allocated heap buffer. */
  1248. /* The memory will be allocated via the mz_zip_archive's alloc/realloc
  1249. * functions. */
  1250. /* Returns NULL and sets the last error on failure. */
  1251. void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip,
  1252. mz_uint file_index,
  1253. size_t *pSize, mz_uint flags);
  1254. void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip,
  1255. const char *pFilename,
  1256. size_t *pSize,
  1257. mz_uint flags);
  1258. /* Extracts a archive file using a callback function to output the file's data.
  1259. */
  1260. mz_bool mz_zip_reader_extract_to_callback(
  1261. mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback,
  1262. void *pOpaque, mz_uint flags);
  1263. mz_bool mz_zip_reader_extract_file_to_callback(
  1264. mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback,
  1265. void *pOpaque, mz_uint flags);
  1266. /* Extract a file iteratively */
  1267. mz_zip_reader_extract_iter_state *
  1268. mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index,
  1269. mz_uint flags);
  1270. mz_zip_reader_extract_iter_state *
  1271. mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename,
  1272. mz_uint flags);
  1273. size_t mz_zip_reader_extract_iter_read(
  1274. mz_zip_reader_extract_iter_state *pState, void *pvBuf, size_t buf_size);
  1275. mz_bool
  1276. mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state *pState);
  1277. #ifndef MINIZ_NO_STDIO
  1278. /* Extracts a archive file to a disk file and sets its last accessed and
  1279. * modified times. */
  1280. /* This function only extracts files, not archive directory records. */
  1281. mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip,
  1282. mz_uint file_index,
  1283. const char *pDst_filename,
  1284. mz_uint flags);
  1285. mz_bool mz_zip_reader_extract_file_to_file(
  1286. mz_zip_archive *pZip, const char *pArchive_filename,
  1287. const char *pDst_filename, mz_uint flags);
  1288. /* Extracts a archive file starting at the current position in the destination
  1289. * FILE stream. */
  1290. mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip,
  1291. mz_uint file_index,
  1292. MZ_FILE *File,
  1293. mz_uint flags);
  1294. mz_bool mz_zip_reader_extract_file_to_cfile(
  1295. mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile,
  1296. mz_uint flags);
  1297. #endif
  1298. #if 0
  1299. /* TODO */
  1300. typedef void *mz_zip_streaming_extract_state_ptr;
  1301. mz_zip_streaming_extract_state_ptr mz_zip_streaming_extract_begin(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags);
  1302. uint64_t mz_zip_streaming_extract_get_size(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
  1303. uint64_t mz_zip_streaming_extract_get_cur_ofs(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
  1304. mz_bool mz_zip_streaming_extract_seek(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, uint64_t new_ofs);
  1305. size_t mz_zip_streaming_extract_read(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, void *pBuf, size_t buf_size);
  1306. mz_bool mz_zip_streaming_extract_end(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
  1307. #endif
  1308. /* This function compares the archive's local headers, the optional local zip64
  1309. * extended information block, and the optional descriptor following the
  1310. * compressed data vs. the data in the central directory. */
  1311. /* It also validates that each file can be successfully uncompressed unless the
  1312. * MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY is specified. */
  1313. mz_bool mz_zip_validate_file(mz_zip_archive *pZip,
  1314. mz_uint file_index, mz_uint flags);
  1315. /* Validates an entire archive by calling mz_zip_validate_file() on each file.
  1316. */
  1317. mz_bool mz_zip_validate_archive(mz_zip_archive *pZip,
  1318. mz_uint flags);
  1319. /* Misc utils/helpers, valid for ZIP reading or writing */
  1320. mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size,
  1321. mz_uint flags,
  1322. mz_zip_error *pErr);
  1323. mz_bool mz_zip_validate_file_archive(const char *pFilename,
  1324. mz_uint flags,
  1325. mz_zip_error *pErr);
  1326. /* Universal end function - calls either mz_zip_reader_end() or
  1327. * mz_zip_writer_end(). */
  1328. mz_bool mz_zip_end(mz_zip_archive *pZip);
  1329. /* -------- ZIP writing */
  1330. #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
  1331. /* Inits a ZIP archive writer. */
  1332. /*Set pZip->m_pWrite (and pZip->m_pIO_opaque) before calling mz_zip_writer_init
  1333. * or mz_zip_writer_init_v2*/
  1334. /*The output is streamable, i.e. file_ofs in mz_file_write_func always increases
  1335. * only by n*/
  1336. mz_bool mz_zip_writer_init(mz_zip_archive *pZip,
  1337. mz_uint64 existing_size);
  1338. mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip,
  1339. mz_uint64 existing_size,
  1340. mz_uint flags);
  1341. mz_bool mz_zip_writer_init_heap(
  1342. mz_zip_archive *pZip, size_t size_to_reserve_at_beginning,
  1343. size_t initial_allocation_size);
  1344. mz_bool mz_zip_writer_init_heap_v2(
  1345. mz_zip_archive *pZip, size_t size_to_reserve_at_beginning,
  1346. size_t initial_allocation_size, mz_uint flags);
  1347. #ifndef MINIZ_NO_STDIO
  1348. mz_bool
  1349. mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename,
  1350. mz_uint64 size_to_reserve_at_beginning);
  1351. mz_bool mz_zip_writer_init_file_v2(
  1352. mz_zip_archive *pZip, const char *pFilename,
  1353. mz_uint64 size_to_reserve_at_beginning, mz_uint flags);
  1354. mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip,
  1355. MZ_FILE *pFile, mz_uint flags);
  1356. #endif
  1357. /* Converts a ZIP archive reader object into a writer object, to allow efficient
  1358. * in-place file appends to occur on an existing archive. */
  1359. /* For archives opened using mz_zip_reader_init_file, pFilename must be the
  1360. * archive's filename so it can be reopened for writing. If the file can't be
  1361. * reopened, mz_zip_reader_end() will be called. */
  1362. /* For archives opened using mz_zip_reader_init_mem, the memory block must be
  1363. * growable using the realloc callback (which defaults to realloc unless you've
  1364. * overridden it). */
  1365. /* Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's
  1366. * user provided m_pWrite function cannot be NULL. */
  1367. /* Note: In-place archive modification is not recommended unless you know what
  1368. * you're doing, because if execution stops or something goes wrong before */
  1369. /* the archive is finalized the file's central directory will be hosed. */
  1370. mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip,
  1371. const char *pFilename);
  1372. mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip,
  1373. const char *pFilename,
  1374. mz_uint flags);
  1375. mz_bool mz_zip_writer_init_from_reader_v2_noreopen(
  1376. mz_zip_archive *pZip, const char *pFilename, mz_uint flags);
  1377. /* Adds the contents of a memory buffer to an archive. These functions record
  1378. * the current local time into the archive. */
  1379. /* To add a directory entry, call this method with an archive name ending in a
  1380. * forwardslash with an empty buffer. */
  1381. /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED,
  1382. * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or
  1383. * just set to MZ_DEFAULT_COMPRESSION. */
  1384. mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip,
  1385. const char *pArchive_name,
  1386. const void *pBuf, size_t buf_size,
  1387. mz_uint level_and_flags);
  1388. /* Like mz_zip_writer_add_mem(), except you can specify a file comment field,
  1389. * and optionally supply the function with already compressed data. */
  1390. /* uncomp_size/uncomp_crc32 are only used if the MZ_ZIP_FLAG_COMPRESSED_DATA
  1391. * flag is specified. */
  1392. mz_bool mz_zip_writer_add_mem_ex(
  1393. mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf,
  1394. size_t buf_size, const void *pComment, mz_uint16 comment_size,
  1395. mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32);
  1396. mz_bool mz_zip_writer_add_mem_ex_v2(
  1397. mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf,
  1398. size_t buf_size, const void *pComment, mz_uint16 comment_size,
  1399. mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32,
  1400. MZ_TIME_T *last_modified, const char *user_extra_data_local,
  1401. mz_uint user_extra_data_local_len, const char *user_extra_data_central,
  1402. mz_uint user_extra_data_central_len);
  1403. /* Adds the contents of a file to an archive. This function also records the
  1404. * disk file's modified time into the archive. */
  1405. /* File data is supplied via a read callback function. User
  1406. * mz_zip_writer_add_(c)file to add a file directly.*/
  1407. mz_bool mz_zip_writer_add_read_buf_callback(
  1408. mz_zip_archive *pZip, const char *pArchive_name,
  1409. mz_file_read_func read_callback, void *callback_opaque, mz_uint64 max_size,
  1410. const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size,
  1411. mz_uint level_and_flags, mz_uint32 ext_attributes,
  1412. const char *user_extra_data_local, mz_uint user_extra_data_local_len,
  1413. const char *user_extra_data_central, mz_uint user_extra_data_central_len);
  1414. #ifndef MINIZ_NO_STDIO
  1415. /* Adds the contents of a disk file to an archive. This function also records
  1416. * the disk file's modified time into the archive. */
  1417. /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED,
  1418. * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or
  1419. * just set to MZ_DEFAULT_COMPRESSION. */
  1420. mz_bool mz_zip_writer_add_file(
  1421. mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename,
  1422. const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags,
  1423. mz_uint32 ext_attributes);
  1424. /* Like mz_zip_writer_add_file(), except the file data is read from the
  1425. * specified FILE stream. */
  1426. mz_bool mz_zip_writer_add_cfile(
  1427. mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file,
  1428. mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment,
  1429. mz_uint16 comment_size, mz_uint level_and_flags, mz_uint32 ext_attributes,
  1430. const char *user_extra_data_local, mz_uint user_extra_data_local_len,
  1431. const char *user_extra_data_central, mz_uint user_extra_data_central_len);
  1432. #endif
  1433. /* Adds a file to an archive by fully cloning the data from another archive. */
  1434. /* This function fully clones the source file's compressed data (no
  1435. * recompression), along with its full filename, extra data (it may add or
  1436. * modify the zip64 local header extra data field), and the optional descriptor
  1437. * following the compressed data. */
  1438. mz_bool mz_zip_writer_add_from_zip_reader(
  1439. mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index);
  1440. /* Finalizes the archive by writing the central directory records followed by
  1441. * the end of central directory record. */
  1442. /* After an archive is finalized, the only valid call on the mz_zip_archive
  1443. * struct is mz_zip_writer_end(). */
  1444. /* An archive must be manually finalized by calling this function for it to be
  1445. * valid. */
  1446. mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip);
  1447. /* Finalizes a heap archive, returning a poiner to the heap block and its size.
  1448. */
  1449. /* The heap block will be allocated using the mz_zip_archive's alloc/realloc
  1450. * callbacks. */
  1451. mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip,
  1452. void **ppBuf,
  1453. size_t *pSize);
  1454. /* Ends archive writing, freeing all allocations, and closing the output file if
  1455. * mz_zip_writer_init_file() was used. */
  1456. /* Note for the archive to be valid, it *must* have been finalized before ending
  1457. * (this function will not do it for you). */
  1458. mz_bool mz_zip_writer_end(mz_zip_archive *pZip);
  1459. /* -------- Misc. high-level helper functions: */
  1460. /* mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically)
  1461. * appends a memory blob to a ZIP archive. */
  1462. /* Note this is NOT a fully safe operation. If it crashes or dies in some way
  1463. * your archive can be left in a screwed up state (without a central directory).
  1464. */
  1465. /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED,
  1466. * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or
  1467. * just set to MZ_DEFAULT_COMPRESSION. */
  1468. /* TODO: Perhaps add an option to leave the existing central dir in place in
  1469. * case the add dies? We could then truncate the file (so the old central dir
  1470. * would be at the end) if something goes wrong. */
  1471. mz_bool mz_zip_add_mem_to_archive_file_in_place(
  1472. const char *pZip_filename, const char *pArchive_name, const void *pBuf,
  1473. size_t buf_size, const void *pComment, mz_uint16 comment_size,
  1474. mz_uint level_and_flags);
  1475. mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(
  1476. const char *pZip_filename, const char *pArchive_name, const void *pBuf,
  1477. size_t buf_size, const void *pComment, mz_uint16 comment_size,
  1478. mz_uint level_and_flags, mz_zip_error *pErr);
  1479. /* Reads a single file from an archive into a heap block. */
  1480. /* If pComment is not NULL, only the file with the specified comment will be
  1481. * extracted. */
  1482. /* Returns NULL on failure. */
  1483. void *
  1484. mz_zip_extract_archive_file_to_heap(const char *pZip_filename,
  1485. const char *pArchive_name, size_t *pSize,
  1486. mz_uint flags);
  1487. void *mz_zip_extract_archive_file_to_heap_v2(
  1488. const char *pZip_filename, const char *pArchive_name, const char *pComment,
  1489. size_t *pSize, mz_uint flags, mz_zip_error *pErr);
  1490. #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */
  1491. #endif /* MINIZ_NO_ARCHIVE_APIS */
  1492. /**************************************************************************
  1493. *
  1494. * Copyright 2013-2014 RAD Game Tools and Valve Software
  1495. * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
  1496. * All Rights Reserved.
  1497. *
  1498. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1499. * of this software and associated documentation files (the "Software"), to deal
  1500. * in the Software without restriction, including without limitation the rights
  1501. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1502. * copies of the Software, and to permit persons to whom the Software is
  1503. * furnished to do so, subject to the following conditions:
  1504. *
  1505. * The above copyright notice and this permission notice shall be included in
  1506. * all copies or substantial portions of the Software.
  1507. *
  1508. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1509. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1510. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1511. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1512. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1513. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  1514. * THE SOFTWARE.
  1515. *
  1516. **************************************************************************/
  1517. typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1];
  1518. typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1];
  1519. typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1];
  1520. /* ------------------- zlib-style API's */
  1521. mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len) {
  1522. mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16);
  1523. size_t block_len = buf_len % 5552;
  1524. if (!ptr)
  1525. return MZ_ADLER32_INIT;
  1526. while (buf_len) {
  1527. for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
  1528. s1 += ptr[0], s2 += s1;
  1529. s1 += ptr[1], s2 += s1;
  1530. s1 += ptr[2], s2 += s1;
  1531. s1 += ptr[3], s2 += s1;
  1532. s1 += ptr[4], s2 += s1;
  1533. s1 += ptr[5], s2 += s1;
  1534. s1 += ptr[6], s2 += s1;
  1535. s1 += ptr[7], s2 += s1;
  1536. }
  1537. for (; i < block_len; ++i)
  1538. s1 += *ptr++, s2 += s1;
  1539. s1 %= 65521U, s2 %= 65521U;
  1540. buf_len -= block_len;
  1541. block_len = 5552;
  1542. }
  1543. return (s2 << 16) + s1;
  1544. }
  1545. /* Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C
  1546. * implementation that balances processor cache usage against speed":
  1547. * http://www.geocities.com/malbrain/ */
  1548. #if 0
  1549. mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
  1550. {
  1551. static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
  1552. 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
  1553. mz_uint32 crcu32 = (mz_uint32)crc;
  1554. if (!ptr)
  1555. return MZ_CRC32_INIT;
  1556. crcu32 = ~crcu32;
  1557. while (buf_len--)
  1558. {
  1559. mz_uint8 b = *ptr++;
  1560. crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
  1561. crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
  1562. }
  1563. return ~crcu32;
  1564. }
  1565. #elif defined(USE_EXTERNAL_MZCRC)
  1566. /* If USE_EXTERNAL_CRC is defined, an external module will export the
  1567. * mz_crc32() symbol for us to use, e.g. an SSE-accelerated version.
  1568. * Depending on the impl, it may be necessary to ~ the input/output crc values.
  1569. */
  1570. mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len);
  1571. #else
  1572. /* Faster, but larger CPU cache footprint.
  1573. */
  1574. mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) {
  1575. static const mz_uint32 s_crc_table[256] = {
  1576. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
  1577. 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  1578. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
  1579. 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  1580. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  1581. 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  1582. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
  1583. 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  1584. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
  1585. 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  1586. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
  1587. 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  1588. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
  1589. 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  1590. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
  1591. 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  1592. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
  1593. 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  1594. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
  1595. 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  1596. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
  1597. 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  1598. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
  1599. 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  1600. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  1601. 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  1602. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
  1603. 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  1604. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
  1605. 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  1606. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
  1607. 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  1608. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
  1609. 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  1610. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
  1611. 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  1612. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
  1613. 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  1614. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
  1615. 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  1616. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
  1617. 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  1618. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D};
  1619. mz_uint32 crc32 = (mz_uint32)crc ^ 0xFFFFFFFF;
  1620. const mz_uint8 *pByte_buf = (const mz_uint8 *)ptr;
  1621. while (buf_len >= 4) {
  1622. crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];
  1623. crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF];
  1624. crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF];
  1625. crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF];
  1626. pByte_buf += 4;
  1627. buf_len -= 4;
  1628. }
  1629. while (buf_len) {
  1630. crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];
  1631. ++pByte_buf;
  1632. --buf_len;
  1633. }
  1634. return ~crc32;
  1635. }
  1636. #endif
  1637. void mz_free(void *p) { MZ_FREE(p); }
  1638. void *miniz_def_alloc_func(void *opaque, size_t items,
  1639. size_t size) {
  1640. (void)opaque, (void)items, (void)size;
  1641. return MZ_MALLOC(items * size);
  1642. }
  1643. void miniz_def_free_func(void *opaque, void *address) {
  1644. (void)opaque, (void)address;
  1645. MZ_FREE(address);
  1646. }
  1647. void *miniz_def_realloc_func(void *opaque, void *address,
  1648. size_t items, size_t size) {
  1649. (void)opaque, (void)address, (void)items, (void)size;
  1650. return MZ_REALLOC(address, items * size);
  1651. }
  1652. const char *mz_version(void) { return MZ_VERSION; }
  1653. #ifndef MINIZ_NO_ZLIB_APIS
  1654. int mz_deflateInit(mz_streamp pStream, int level) {
  1655. return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9,
  1656. MZ_DEFAULT_STRATEGY);
  1657. }
  1658. int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits,
  1659. int mem_level, int strategy) {
  1660. tdefl_compressor *pComp;
  1661. mz_uint comp_flags =
  1662. TDEFL_COMPUTE_ADLER32 |
  1663. tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy);
  1664. if (!pStream)
  1665. return MZ_STREAM_ERROR;
  1666. if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) ||
  1667. ((window_bits != MZ_DEFAULT_WINDOW_BITS) &&
  1668. (-window_bits != MZ_DEFAULT_WINDOW_BITS)))
  1669. return MZ_PARAM_ERROR;
  1670. pStream->data_type = 0;
  1671. pStream->adler = MZ_ADLER32_INIT;
  1672. pStream->msg = NULL;
  1673. pStream->reserved = 0;
  1674. pStream->total_in = 0;
  1675. pStream->total_out = 0;
  1676. if (!pStream->zalloc)
  1677. pStream->zalloc = miniz_def_alloc_func;
  1678. if (!pStream->zfree)
  1679. pStream->zfree = miniz_def_free_func;
  1680. pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1,
  1681. sizeof(tdefl_compressor));
  1682. if (!pComp)
  1683. return MZ_MEM_ERROR;
  1684. pStream->state = (struct mz_internal_state *)pComp;
  1685. if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) {
  1686. mz_deflateEnd(pStream);
  1687. return MZ_PARAM_ERROR;
  1688. }
  1689. return MZ_OK;
  1690. }
  1691. int mz_deflateReset(mz_streamp pStream) {
  1692. if ((!pStream) || (!pStream->state) || (!pStream->zalloc) ||
  1693. (!pStream->zfree))
  1694. return MZ_STREAM_ERROR;
  1695. pStream->total_in = pStream->total_out = 0;
  1696. tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL,
  1697. ((tdefl_compressor *)pStream->state)->m_flags);
  1698. return MZ_OK;
  1699. }
  1700. int mz_deflate(mz_streamp pStream, int flush) {
  1701. size_t in_bytes, out_bytes;
  1702. mz_ulong orig_total_in, orig_total_out;
  1703. int mz_status = MZ_OK;
  1704. if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) ||
  1705. (!pStream->next_out))
  1706. return MZ_STREAM_ERROR;
  1707. if (!pStream->avail_out)
  1708. return MZ_BUF_ERROR;
  1709. if (flush == MZ_PARTIAL_FLUSH)
  1710. flush = MZ_SYNC_FLUSH;
  1711. if (((tdefl_compressor *)pStream->state)->m_prev_return_status ==
  1712. TDEFL_STATUS_DONE)
  1713. return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR;
  1714. orig_total_in = pStream->total_in;
  1715. orig_total_out = pStream->total_out;
  1716. for (;;) {
  1717. tdefl_status defl_status;
  1718. in_bytes = pStream->avail_in;
  1719. out_bytes = pStream->avail_out;
  1720. defl_status = tdefl_compress((tdefl_compressor *)pStream->state,
  1721. pStream->next_in, &in_bytes, pStream->next_out,
  1722. &out_bytes, (tdefl_flush)flush);
  1723. pStream->next_in += (mz_uint)in_bytes;
  1724. pStream->avail_in -= (mz_uint)in_bytes;
  1725. pStream->total_in += (mz_uint)in_bytes;
  1726. pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state);
  1727. pStream->next_out += (mz_uint)out_bytes;
  1728. pStream->avail_out -= (mz_uint)out_bytes;
  1729. pStream->total_out += (mz_uint)out_bytes;
  1730. if (defl_status < 0) {
  1731. mz_status = MZ_STREAM_ERROR;
  1732. break;
  1733. } else if (defl_status == TDEFL_STATUS_DONE) {
  1734. mz_status = MZ_STREAM_END;
  1735. break;
  1736. } else if (!pStream->avail_out)
  1737. break;
  1738. else if ((!pStream->avail_in) && (flush != MZ_FINISH)) {
  1739. if ((flush) || (pStream->total_in != orig_total_in) ||
  1740. (pStream->total_out != orig_total_out))
  1741. break;
  1742. return MZ_BUF_ERROR; /* Can't make forward progress without some input.
  1743. */
  1744. }
  1745. }
  1746. return mz_status;
  1747. }
  1748. int mz_deflateEnd(mz_streamp pStream) {
  1749. if (!pStream)
  1750. return MZ_STREAM_ERROR;
  1751. if (pStream->state) {
  1752. pStream->zfree(pStream->opaque, pStream->state);
  1753. pStream->state = NULL;
  1754. }
  1755. return MZ_OK;
  1756. }
  1757. mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len) {
  1758. (void)pStream;
  1759. /* This is really over conservative. (And lame, but it's actually pretty
  1760. * tricky to compute a true upper bound given the way tdefl's blocking works.)
  1761. */
  1762. return MZ_MAX(128 + (source_len * 110) / 100,
  1763. 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
  1764. }
  1765. int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len,
  1766. const unsigned char *pSource, mz_ulong source_len, int level) {
  1767. int status;
  1768. mz_stream stream;
  1769. memset(&stream, 0, sizeof(stream));
  1770. /* In case mz_ulong is 64-bits (argh I hate longs). */
  1771. if ((source_len | *pDest_len) > 0xFFFFFFFFU)
  1772. return MZ_PARAM_ERROR;
  1773. stream.next_in = pSource;
  1774. stream.avail_in = (mz_uint32)source_len;
  1775. stream.next_out = pDest;
  1776. stream.avail_out = (mz_uint32)*pDest_len;
  1777. status = mz_deflateInit(&stream, level);
  1778. if (status != MZ_OK)
  1779. return status;
  1780. status = mz_deflate(&stream, MZ_FINISH);
  1781. if (status != MZ_STREAM_END) {
  1782. mz_deflateEnd(&stream);
  1783. return (status == MZ_OK) ? MZ_BUF_ERROR : status;
  1784. }
  1785. *pDest_len = stream.total_out;
  1786. return mz_deflateEnd(&stream);
  1787. }
  1788. int mz_compress(unsigned char *pDest, mz_ulong *pDest_len,
  1789. const unsigned char *pSource, mz_ulong source_len) {
  1790. return mz_compress2(pDest, pDest_len, pSource, source_len,
  1791. MZ_DEFAULT_COMPRESSION);
  1792. }
  1793. mz_ulong mz_compressBound(mz_ulong source_len) {
  1794. return mz_deflateBound(NULL, source_len);
  1795. }
  1796. typedef struct {
  1797. tinfl_decompressor m_decomp;
  1798. mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed;
  1799. int m_window_bits;
  1800. mz_uint8 m_dict[TINFL_LZ_DICT_SIZE];
  1801. tinfl_status m_last_status;
  1802. } inflate_state;
  1803. int mz_inflateInit2(mz_streamp pStream, int window_bits) {
  1804. inflate_state *pDecomp;
  1805. if (!pStream)
  1806. return MZ_STREAM_ERROR;
  1807. if ((window_bits != MZ_DEFAULT_WINDOW_BITS) &&
  1808. (-window_bits != MZ_DEFAULT_WINDOW_BITS))
  1809. return MZ_PARAM_ERROR;
  1810. pStream->data_type = 0;
  1811. pStream->adler = 0;
  1812. pStream->msg = NULL;
  1813. pStream->total_in = 0;
  1814. pStream->total_out = 0;
  1815. pStream->reserved = 0;
  1816. if (!pStream->zalloc)
  1817. pStream->zalloc = miniz_def_alloc_func;
  1818. if (!pStream->zfree)
  1819. pStream->zfree = miniz_def_free_func;
  1820. pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1,
  1821. sizeof(inflate_state));
  1822. if (!pDecomp)
  1823. return MZ_MEM_ERROR;
  1824. pStream->state = (struct mz_internal_state *)pDecomp;
  1825. tinfl_init(&pDecomp->m_decomp);
  1826. pDecomp->m_dict_ofs = 0;
  1827. pDecomp->m_dict_avail = 0;
  1828. pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
  1829. pDecomp->m_first_call = 1;
  1830. pDecomp->m_has_flushed = 0;
  1831. pDecomp->m_window_bits = window_bits;
  1832. return MZ_OK;
  1833. }
  1834. int mz_inflateInit(mz_streamp pStream) {
  1835. return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS);
  1836. }
  1837. int mz_inflateReset(mz_streamp pStream) {
  1838. inflate_state *pDecomp;
  1839. if (!pStream)
  1840. return MZ_STREAM_ERROR;
  1841. pStream->data_type = 0;
  1842. pStream->adler = 0;
  1843. pStream->msg = NULL;
  1844. pStream->total_in = 0;
  1845. pStream->total_out = 0;
  1846. pStream->reserved = 0;
  1847. pDecomp = (inflate_state *)pStream->state;
  1848. tinfl_init(&pDecomp->m_decomp);
  1849. pDecomp->m_dict_ofs = 0;
  1850. pDecomp->m_dict_avail = 0;
  1851. pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
  1852. pDecomp->m_first_call = 1;
  1853. pDecomp->m_has_flushed = 0;
  1854. /* pDecomp->m_window_bits = window_bits */;
  1855. return MZ_OK;
  1856. }
  1857. int mz_inflate(mz_streamp pStream, int flush) {
  1858. inflate_state *pState;
  1859. mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
  1860. size_t in_bytes, out_bytes, orig_avail_in;
  1861. tinfl_status status;
  1862. if ((!pStream) || (!pStream->state))
  1863. return MZ_STREAM_ERROR;
  1864. if (flush == MZ_PARTIAL_FLUSH)
  1865. flush = MZ_SYNC_FLUSH;
  1866. if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH))
  1867. return MZ_STREAM_ERROR;
  1868. pState = (inflate_state *)pStream->state;
  1869. if (pState->m_window_bits > 0)
  1870. decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
  1871. orig_avail_in = pStream->avail_in;
  1872. first_call = pState->m_first_call;
  1873. pState->m_first_call = 0;
  1874. if (pState->m_last_status < 0)
  1875. return MZ_DATA_ERROR;
  1876. if (pState->m_has_flushed && (flush != MZ_FINISH))
  1877. return MZ_STREAM_ERROR;
  1878. pState->m_has_flushed |= (flush == MZ_FINISH);
  1879. if ((flush == MZ_FINISH) && (first_call)) {
  1880. /* MZ_FINISH on the first call implies that the input and output buffers are
  1881. * large enough to hold the entire compressed/decompressed file. */
  1882. decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
  1883. in_bytes = pStream->avail_in;
  1884. out_bytes = pStream->avail_out;
  1885. status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes,
  1886. pStream->next_out, pStream->next_out, &out_bytes,
  1887. decomp_flags);
  1888. pState->m_last_status = status;
  1889. pStream->next_in += (mz_uint)in_bytes;
  1890. pStream->avail_in -= (mz_uint)in_bytes;
  1891. pStream->total_in += (mz_uint)in_bytes;
  1892. pStream->adler = tinfl_get_adler32(&pState->m_decomp);
  1893. pStream->next_out += (mz_uint)out_bytes;
  1894. pStream->avail_out -= (mz_uint)out_bytes;
  1895. pStream->total_out += (mz_uint)out_bytes;
  1896. if (status < 0)
  1897. return MZ_DATA_ERROR;
  1898. else if (status != TINFL_STATUS_DONE) {
  1899. pState->m_last_status = TINFL_STATUS_FAILED;
  1900. return MZ_BUF_ERROR;
  1901. }
  1902. return MZ_STREAM_END;
  1903. }
  1904. /* flush != MZ_FINISH then we must assume there's more input. */
  1905. if (flush != MZ_FINISH)
  1906. decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
  1907. if (pState->m_dict_avail) {
  1908. n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
  1909. memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
  1910. pStream->next_out += n;
  1911. pStream->avail_out -= n;
  1912. pStream->total_out += n;
  1913. pState->m_dict_avail -= n;
  1914. pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
  1915. return ((pState->m_last_status == TINFL_STATUS_DONE) &&
  1916. (!pState->m_dict_avail))
  1917. ? MZ_STREAM_END
  1918. : MZ_OK;
  1919. }
  1920. for (;;) {
  1921. in_bytes = pStream->avail_in;
  1922. out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;
  1923. status = tinfl_decompress(
  1924. &pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict,
  1925. pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags);
  1926. pState->m_last_status = status;
  1927. pStream->next_in += (mz_uint)in_bytes;
  1928. pStream->avail_in -= (mz_uint)in_bytes;
  1929. pStream->total_in += (mz_uint)in_bytes;
  1930. pStream->adler = tinfl_get_adler32(&pState->m_decomp);
  1931. pState->m_dict_avail = (mz_uint)out_bytes;
  1932. n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
  1933. memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
  1934. pStream->next_out += n;
  1935. pStream->avail_out -= n;
  1936. pStream->total_out += n;
  1937. pState->m_dict_avail -= n;
  1938. pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
  1939. if (status < 0)
  1940. return MZ_DATA_ERROR; /* Stream is corrupted (there could be some
  1941. uncompressed data left in the output dictionary -
  1942. oh well). */
  1943. else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
  1944. return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress
  1945. without supplying more input or by setting flush
  1946. to MZ_FINISH. */
  1947. else if (flush == MZ_FINISH) {
  1948. /* The output buffer MUST be large to hold the remaining uncompressed data
  1949. * when flush==MZ_FINISH. */
  1950. if (status == TINFL_STATUS_DONE)
  1951. return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;
  1952. /* status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's
  1953. * at least 1 more byte on the way. If there's no more room left in the
  1954. * output buffer then something is wrong. */
  1955. else if (!pStream->avail_out)
  1956. return MZ_BUF_ERROR;
  1957. } else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) ||
  1958. (!pStream->avail_out) || (pState->m_dict_avail))
  1959. break;
  1960. }
  1961. return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail))
  1962. ? MZ_STREAM_END
  1963. : MZ_OK;
  1964. }
  1965. int mz_inflateEnd(mz_streamp pStream) {
  1966. if (!pStream)
  1967. return MZ_STREAM_ERROR;
  1968. if (pStream->state) {
  1969. pStream->zfree(pStream->opaque, pStream->state);
  1970. pStream->state = NULL;
  1971. }
  1972. return MZ_OK;
  1973. }
  1974. int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len,
  1975. const unsigned char *pSource, mz_ulong *pSource_len) {
  1976. mz_stream stream;
  1977. int status;
  1978. memset(&stream, 0, sizeof(stream));
  1979. /* In case mz_ulong is 64-bits (argh I hate longs). */
  1980. if ((*pSource_len | *pDest_len) > 0xFFFFFFFFU)
  1981. return MZ_PARAM_ERROR;
  1982. stream.next_in = pSource;
  1983. stream.avail_in = (mz_uint32)*pSource_len;
  1984. stream.next_out = pDest;
  1985. stream.avail_out = (mz_uint32)*pDest_len;
  1986. status = mz_inflateInit(&stream);
  1987. if (status != MZ_OK)
  1988. return status;
  1989. status = mz_inflate(&stream, MZ_FINISH);
  1990. *pSource_len = *pSource_len - stream.avail_in;
  1991. if (status != MZ_STREAM_END) {
  1992. mz_inflateEnd(&stream);
  1993. return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR
  1994. : status;
  1995. }
  1996. *pDest_len = stream.total_out;
  1997. return mz_inflateEnd(&stream);
  1998. }
  1999. int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len,
  2000. const unsigned char *pSource, mz_ulong source_len) {
  2001. return mz_uncompress2(pDest, pDest_len, pSource, &source_len);
  2002. }
  2003. const char *mz_error(int err) {
  2004. static struct {
  2005. int m_err;
  2006. const char *m_pDesc;
  2007. } s_error_descs[] = {{MZ_OK, ""},
  2008. {MZ_STREAM_END, "stream end"},
  2009. {MZ_NEED_DICT, "need dictionary"},
  2010. {MZ_ERRNO, "file error"},
  2011. {MZ_STREAM_ERROR, "stream error"},
  2012. {MZ_DATA_ERROR, "data error"},
  2013. {MZ_MEM_ERROR, "out of memory"},
  2014. {MZ_BUF_ERROR, "buf error"},
  2015. {MZ_VERSION_ERROR, "version error"},
  2016. {MZ_PARAM_ERROR, "parameter error"}};
  2017. mz_uint i;
  2018. for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i)
  2019. if (s_error_descs[i].m_err == err)
  2020. return s_error_descs[i].m_pDesc;
  2021. return NULL;
  2022. }
  2023. #endif /*MINIZ_NO_ZLIB_APIS */
  2024. /*
  2025. This is free and unencumbered software released into the public domain.
  2026. Anyone is free to copy, modify, publish, use, compile, sell, or
  2027. distribute this software, either in source code form or as a compiled
  2028. binary, for any purpose, commercial or non-commercial, and by any
  2029. means.
  2030. In jurisdictions that recognize copyright laws, the author or authors
  2031. of this software dedicate any and all copyright interest in the
  2032. software to the public domain. We make this dedication for the benefit
  2033. of the public at large and to the detriment of our heirs and
  2034. successors. We intend this dedication to be an overt act of
  2035. relinquishment in perpetuity of all present and future rights to this
  2036. software under copyright law.
  2037. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2038. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2039. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  2040. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  2041. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  2042. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  2043. OTHER DEALINGS IN THE SOFTWARE.
  2044. For more information, please refer to <http://unlicense.org/>
  2045. */
  2046. /**************************************************************************
  2047. *
  2048. * Copyright 2013-2014 RAD Game Tools and Valve Software
  2049. * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
  2050. * All Rights Reserved.
  2051. *
  2052. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2053. * of this software and associated documentation files (the "Software"), to deal
  2054. * in the Software without restriction, including without limitation the rights
  2055. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  2056. * copies of the Software, and to permit persons to whom the Software is
  2057. * furnished to do so, subject to the following conditions:
  2058. *
  2059. * The above copyright notice and this permission notice shall be included in
  2060. * all copies or substantial portions of the Software.
  2061. *
  2062. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2063. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2064. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2065. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2066. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  2067. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  2068. * THE SOFTWARE.
  2069. *
  2070. **************************************************************************/
  2071. /* ------------------- Low-level Compression (independent from all decompression
  2072. * API's) */
  2073. /* Purposely making these tables static for faster init and thread safety. */
  2074. static const mz_uint16 s_tdefl_len_sym[256] = {
  2075. 257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268,
  2076. 268, 269, 269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272, 272,
  2077. 272, 272, 273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 274,
  2078. 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276, 276, 276,
  2079. 276, 276, 276, 276, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
  2080. 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278,
  2081. 278, 278, 278, 278, 278, 278, 279, 279, 279, 279, 279, 279, 279, 279, 279,
  2082. 279, 279, 279, 279, 279, 279, 279, 280, 280, 280, 280, 280, 280, 280, 280,
  2083. 280, 280, 280, 280, 280, 280, 280, 280, 281, 281, 281, 281, 281, 281, 281,
  2084. 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281,
  2085. 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 282, 282, 282, 282, 282,
  2086. 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282,
  2087. 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 283, 283, 283,
  2088. 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283,
  2089. 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 284,
  2090. 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284,
  2091. 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284,
  2092. 285};
  2093. static const mz_uint8 s_tdefl_len_extra[256] = {
  2094. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
  2095. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  2096. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  2097. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  2098. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  2099. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  2100. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  2101. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  2102. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  2103. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  2104. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0};
  2105. static const mz_uint8 s_tdefl_small_dist_sym[512] = {
  2106. 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
  2107. 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10,
  2108. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  2109. 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  2110. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  2111. 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  2112. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14,
  2113. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  2114. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  2115. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  2116. 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  2117. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  2118. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  2119. 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  2120. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  2121. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  2122. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  2123. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  2124. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  2125. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  2126. 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
  2127. 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
  2128. 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
  2129. 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
  2130. 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
  2131. 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
  2132. 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17};
  2133. static const mz_uint8 s_tdefl_small_dist_extra[512] = {
  2134. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  2135. 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  2136. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  2137. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  2138. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  2139. 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  2140. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  2141. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  2142. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  2143. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  2144. 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2145. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2146. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2147. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2148. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2149. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2150. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2151. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2152. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2153. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2154. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
  2155. static const mz_uint8 s_tdefl_large_dist_sym[128] = {
  2156. 0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24,
  2157. 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26,
  2158. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  2159. 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  2160. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  2161. 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  2162. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29};
  2163. static const mz_uint8 s_tdefl_large_dist_extra[128] = {
  2164. 0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
  2165. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
  2166. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  2167. 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  2168. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  2169. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  2170. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13};
  2171. /* Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted
  2172. * values. */
  2173. typedef struct {
  2174. mz_uint16 m_key, m_sym_index;
  2175. } tdefl_sym_freq;
  2176. static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms,
  2177. tdefl_sym_freq *pSyms0,
  2178. tdefl_sym_freq *pSyms1) {
  2179. mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
  2180. tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1;
  2181. MZ_CLEAR_OBJ(hist);
  2182. for (i = 0; i < num_syms; i++) {
  2183. mz_uint freq = pSyms0[i].m_key;
  2184. hist[freq & 0xFF]++;
  2185. hist[256 + ((freq >> 8) & 0xFF)]++;
  2186. }
  2187. while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
  2188. total_passes--;
  2189. for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) {
  2190. const mz_uint32 *pHist = &hist[pass << 8];
  2191. mz_uint offsets[256], cur_ofs = 0;
  2192. for (i = 0; i < 256; i++) {
  2193. offsets[i] = cur_ofs;
  2194. cur_ofs += pHist[i];
  2195. }
  2196. for (i = 0; i < num_syms; i++)
  2197. pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] =
  2198. pCur_syms[i];
  2199. {
  2200. tdefl_sym_freq *t = pCur_syms;
  2201. pCur_syms = pNew_syms;
  2202. pNew_syms = t;
  2203. }
  2204. }
  2205. return pCur_syms;
  2206. }
  2207. /* tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat,
  2208. * alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996. */
  2209. static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n) {
  2210. int root, leaf, next, avbl, used, dpth;
  2211. if (n == 0)
  2212. return;
  2213. else if (n == 1) {
  2214. A[0].m_key = 1;
  2215. return;
  2216. }
  2217. A[0].m_key += A[1].m_key;
  2218. root = 0;
  2219. leaf = 2;
  2220. for (next = 1; next < n - 1; next++) {
  2221. if (leaf >= n || A[root].m_key < A[leaf].m_key) {
  2222. A[next].m_key = A[root].m_key;
  2223. A[root++].m_key = (mz_uint16)next;
  2224. } else
  2225. A[next].m_key = A[leaf++].m_key;
  2226. if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) {
  2227. A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key);
  2228. A[root++].m_key = (mz_uint16)next;
  2229. } else
  2230. A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key);
  2231. }
  2232. A[n - 2].m_key = 0;
  2233. for (next = n - 3; next >= 0; next--)
  2234. A[next].m_key = A[A[next].m_key].m_key + 1;
  2235. avbl = 1;
  2236. used = dpth = 0;
  2237. root = n - 2;
  2238. next = n - 1;
  2239. while (avbl > 0) {
  2240. while (root >= 0 && (int)A[root].m_key == dpth) {
  2241. used++;
  2242. root--;
  2243. }
  2244. while (avbl > used) {
  2245. A[next--].m_key = (mz_uint16)(dpth);
  2246. avbl--;
  2247. }
  2248. avbl = 2 * used;
  2249. dpth++;
  2250. used = 0;
  2251. }
  2252. }
  2253. /* Limits canonical Huffman code table's max code size. */
  2254. enum { TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32 };
  2255. static void tdefl_huffman_enforce_max_code_size(int *pNum_codes,
  2256. int code_list_len,
  2257. int max_code_size) {
  2258. int i;
  2259. mz_uint32 total = 0;
  2260. if (code_list_len <= 1)
  2261. return;
  2262. for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++)
  2263. pNum_codes[max_code_size] += pNum_codes[i];
  2264. for (i = max_code_size; i > 0; i--)
  2265. total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i));
  2266. while (total != (1UL << max_code_size)) {
  2267. pNum_codes[max_code_size]--;
  2268. for (i = max_code_size - 1; i > 0; i--)
  2269. if (pNum_codes[i]) {
  2270. pNum_codes[i]--;
  2271. pNum_codes[i + 1] += 2;
  2272. break;
  2273. }
  2274. total--;
  2275. }
  2276. }
  2277. static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num,
  2278. int table_len, int code_size_limit,
  2279. int static_table) {
  2280. int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE];
  2281. mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1];
  2282. MZ_CLEAR_OBJ(num_codes);
  2283. if (static_table) {
  2284. for (i = 0; i < table_len; i++)
  2285. num_codes[d->m_huff_code_sizes[table_num][i]]++;
  2286. } else {
  2287. tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS],
  2288. *pSyms;
  2289. int num_used_syms = 0;
  2290. const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0];
  2291. for (i = 0; i < table_len; i++)
  2292. if (pSym_count[i]) {
  2293. syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i];
  2294. syms0[num_used_syms++].m_sym_index = (mz_uint16)i;
  2295. }
  2296. pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1);
  2297. tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);
  2298. for (i = 0; i < num_used_syms; i++)
  2299. num_codes[pSyms[i].m_key]++;
  2300. tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms,
  2301. code_size_limit);
  2302. MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]);
  2303. MZ_CLEAR_OBJ(d->m_huff_codes[table_num]);
  2304. for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
  2305. for (l = num_codes[i]; l > 0; l--)
  2306. d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i);
  2307. }
  2308. next_code[1] = 0;
  2309. for (j = 0, i = 2; i <= code_size_limit; i++)
  2310. next_code[i] = j = ((j + num_codes[i - 1]) << 1);
  2311. for (i = 0; i < table_len; i++) {
  2312. mz_uint rev_code = 0, code, code_size;
  2313. if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0)
  2314. continue;
  2315. code = next_code[code_size]++;
  2316. for (l = code_size; l > 0; l--, code >>= 1)
  2317. rev_code = (rev_code << 1) | (code & 1);
  2318. d->m_huff_codes[table_num][i] = (mz_uint16)rev_code;
  2319. }
  2320. }
  2321. #define TDEFL_PUT_BITS(b, l) \
  2322. do { \
  2323. mz_uint bits = b; \
  2324. mz_uint len = l; \
  2325. MZ_ASSERT(bits <= ((1U << len) - 1U)); \
  2326. d->m_bit_buffer |= (bits << d->m_bits_in); \
  2327. d->m_bits_in += len; \
  2328. while (d->m_bits_in >= 8) { \
  2329. if (d->m_pOutput_buf < d->m_pOutput_buf_end) \
  2330. *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
  2331. d->m_bit_buffer >>= 8; \
  2332. d->m_bits_in -= 8; \
  2333. } \
  2334. } \
  2335. MZ_MACRO_END
  2336. #define TDEFL_RLE_PREV_CODE_SIZE() \
  2337. { \
  2338. if (rle_repeat_count) { \
  2339. if (rle_repeat_count < 3) { \
  2340. d->m_huff_count[2][prev_code_size] = \
  2341. (mz_uint16)(d->m_huff_count[2][prev_code_size] + \
  2342. rle_repeat_count); \
  2343. while (rle_repeat_count--) \
  2344. packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \
  2345. } else { \
  2346. d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \
  2347. packed_code_sizes[num_packed_code_sizes++] = 16; \
  2348. packed_code_sizes[num_packed_code_sizes++] = \
  2349. (mz_uint8)(rle_repeat_count - 3); \
  2350. } \
  2351. rle_repeat_count = 0; \
  2352. } \
  2353. }
  2354. #define TDEFL_RLE_ZERO_CODE_SIZE() \
  2355. { \
  2356. if (rle_z_count) { \
  2357. if (rle_z_count < 3) { \
  2358. d->m_huff_count[2][0] = \
  2359. (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \
  2360. while (rle_z_count--) \
  2361. packed_code_sizes[num_packed_code_sizes++] = 0; \
  2362. } else if (rle_z_count <= 10) { \
  2363. d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \
  2364. packed_code_sizes[num_packed_code_sizes++] = 17; \
  2365. packed_code_sizes[num_packed_code_sizes++] = \
  2366. (mz_uint8)(rle_z_count - 3); \
  2367. } else { \
  2368. d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \
  2369. packed_code_sizes[num_packed_code_sizes++] = 18; \
  2370. packed_code_sizes[num_packed_code_sizes++] = \
  2371. (mz_uint8)(rle_z_count - 11); \
  2372. } \
  2373. rle_z_count = 0; \
  2374. } \
  2375. }
  2376. static mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = {
  2377. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  2378. static void tdefl_start_dynamic_block(tdefl_compressor *d) {
  2379. int num_lit_codes, num_dist_codes, num_bit_lengths;
  2380. mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count,
  2381. rle_repeat_count, packed_code_sizes_index;
  2382. mz_uint8
  2383. code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1],
  2384. packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1],
  2385. prev_code_size = 0xFF;
  2386. d->m_huff_count[0][256] = 1;
  2387. tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE);
  2388. tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE);
  2389. for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
  2390. if (d->m_huff_code_sizes[0][num_lit_codes - 1])
  2391. break;
  2392. for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
  2393. if (d->m_huff_code_sizes[1][num_dist_codes - 1])
  2394. break;
  2395. memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);
  2396. memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0],
  2397. num_dist_codes);
  2398. total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
  2399. num_packed_code_sizes = 0;
  2400. rle_z_count = 0;
  2401. rle_repeat_count = 0;
  2402. memset(&d->m_huff_count[2][0], 0,
  2403. sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2);
  2404. for (i = 0; i < total_code_sizes_to_pack; i++) {
  2405. mz_uint8 code_size = code_sizes_to_pack[i];
  2406. if (!code_size) {
  2407. TDEFL_RLE_PREV_CODE_SIZE();
  2408. if (++rle_z_count == 138) {
  2409. TDEFL_RLE_ZERO_CODE_SIZE();
  2410. }
  2411. } else {
  2412. TDEFL_RLE_ZERO_CODE_SIZE();
  2413. if (code_size != prev_code_size) {
  2414. TDEFL_RLE_PREV_CODE_SIZE();
  2415. d->m_huff_count[2][code_size] =
  2416. (mz_uint16)(d->m_huff_count[2][code_size] + 1);
  2417. packed_code_sizes[num_packed_code_sizes++] = code_size;
  2418. } else if (++rle_repeat_count == 6) {
  2419. TDEFL_RLE_PREV_CODE_SIZE();
  2420. }
  2421. }
  2422. prev_code_size = code_size;
  2423. }
  2424. if (rle_repeat_count) {
  2425. TDEFL_RLE_PREV_CODE_SIZE();
  2426. } else {
  2427. TDEFL_RLE_ZERO_CODE_SIZE();
  2428. }
  2429. tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE);
  2430. TDEFL_PUT_BITS(2, 2);
  2431. TDEFL_PUT_BITS(num_lit_codes - 257, 5);
  2432. TDEFL_PUT_BITS(num_dist_codes - 1, 5);
  2433. for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
  2434. if (d->m_huff_code_sizes
  2435. [2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])
  2436. break;
  2437. num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1));
  2438. TDEFL_PUT_BITS(num_bit_lengths - 4, 4);
  2439. for (i = 0; (int)i < num_bit_lengths; i++)
  2440. TDEFL_PUT_BITS(
  2441. d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3);
  2442. for (packed_code_sizes_index = 0;
  2443. packed_code_sizes_index < num_packed_code_sizes;) {
  2444. mz_uint code = packed_code_sizes[packed_code_sizes_index++];
  2445. MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2);
  2446. TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]);
  2447. if (code >= 16)
  2448. TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++],
  2449. "\02\03\07"[code - 16]);
  2450. }
  2451. }
  2452. static void tdefl_start_static_block(tdefl_compressor *d) {
  2453. mz_uint i;
  2454. mz_uint8 *p = &d->m_huff_code_sizes[0][0];
  2455. for (i = 0; i <= 143; ++i)
  2456. *p++ = 8;
  2457. for (; i <= 255; ++i)
  2458. *p++ = 9;
  2459. for (; i <= 279; ++i)
  2460. *p++ = 7;
  2461. for (; i <= 287; ++i)
  2462. *p++ = 8;
  2463. memset(d->m_huff_code_sizes[1], 5, 32);
  2464. tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE);
  2465. tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE);
  2466. TDEFL_PUT_BITS(1, 2);
  2467. }
  2468. static const mz_uint mz_bitmasks[17] = {
  2469. 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF,
  2470. 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF};
  2471. #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && \
  2472. MINIZ_HAS_64BIT_REGISTERS
  2473. static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) {
  2474. mz_uint flags;
  2475. mz_uint8 *pLZ_codes;
  2476. mz_uint8 *pOutput_buf = d->m_pOutput_buf;
  2477. mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf;
  2478. mz_uint64 bit_buffer = d->m_bit_buffer;
  2479. mz_uint bits_in = d->m_bits_in;
  2480. #define TDEFL_PUT_BITS_FAST(b, l) \
  2481. { \
  2482. bit_buffer |= (((mz_uint64)(b)) << bits_in); \
  2483. bits_in += (l); \
  2484. }
  2485. flags = 1;
  2486. for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end;
  2487. flags >>= 1) {
  2488. if (flags == 1)
  2489. flags = *pLZ_codes++ | 0x100;
  2490. if (flags & 1) {
  2491. mz_uint s0, s1, n0, n1, sym, num_extra_bits;
  2492. mz_uint match_len = pLZ_codes[0],
  2493. match_dist = *(const mz_uint16 *)(pLZ_codes + 1);
  2494. pLZ_codes += 3;
  2495. MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
  2496. TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]],
  2497. d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
  2498. TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]],
  2499. s_tdefl_len_extra[match_len]);
  2500. /* This sequence coaxes MSVC into using cmov's vs. jmp's. */
  2501. s0 = s_tdefl_small_dist_sym[match_dist & 511];
  2502. n0 = s_tdefl_small_dist_extra[match_dist & 511];
  2503. s1 = s_tdefl_large_dist_sym[match_dist >> 8];
  2504. n1 = s_tdefl_large_dist_extra[match_dist >> 8];
  2505. sym = (match_dist < 512) ? s0 : s1;
  2506. num_extra_bits = (match_dist < 512) ? n0 : n1;
  2507. MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
  2508. TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym],
  2509. d->m_huff_code_sizes[1][sym]);
  2510. TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits],
  2511. num_extra_bits);
  2512. } else {
  2513. mz_uint lit = *pLZ_codes++;
  2514. MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
  2515. TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit],
  2516. d->m_huff_code_sizes[0][lit]);
  2517. if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) {
  2518. flags >>= 1;
  2519. lit = *pLZ_codes++;
  2520. MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
  2521. TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit],
  2522. d->m_huff_code_sizes[0][lit]);
  2523. if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) {
  2524. flags >>= 1;
  2525. lit = *pLZ_codes++;
  2526. MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
  2527. TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit],
  2528. d->m_huff_code_sizes[0][lit]);
  2529. }
  2530. }
  2531. }
  2532. if (pOutput_buf >= d->m_pOutput_buf_end)
  2533. return MZ_FALSE;
  2534. *(mz_uint64 *)pOutput_buf = bit_buffer;
  2535. pOutput_buf += (bits_in >> 3);
  2536. bit_buffer >>= (bits_in & ~7);
  2537. bits_in &= 7;
  2538. }
  2539. #undef TDEFL_PUT_BITS_FAST
  2540. d->m_pOutput_buf = pOutput_buf;
  2541. d->m_bits_in = 0;
  2542. d->m_bit_buffer = 0;
  2543. while (bits_in) {
  2544. mz_uint32 n = MZ_MIN(bits_in, 16);
  2545. TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n);
  2546. bit_buffer >>= n;
  2547. bits_in -= n;
  2548. }
  2549. TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
  2550. return (d->m_pOutput_buf < d->m_pOutput_buf_end);
  2551. }
  2552. #else
  2553. static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) {
  2554. mz_uint flags;
  2555. mz_uint8 *pLZ_codes;
  2556. flags = 1;
  2557. for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf;
  2558. flags >>= 1) {
  2559. if (flags == 1)
  2560. flags = *pLZ_codes++ | 0x100;
  2561. if (flags & 1) {
  2562. mz_uint sym, num_extra_bits;
  2563. mz_uint match_len = pLZ_codes[0],
  2564. match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
  2565. pLZ_codes += 3;
  2566. MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
  2567. TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]],
  2568. d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
  2569. TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]],
  2570. s_tdefl_len_extra[match_len]);
  2571. if (match_dist < 512) {
  2572. sym = s_tdefl_small_dist_sym[match_dist];
  2573. num_extra_bits = s_tdefl_small_dist_extra[match_dist];
  2574. } else {
  2575. sym = s_tdefl_large_dist_sym[match_dist >> 8];
  2576. num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];
  2577. }
  2578. MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
  2579. TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
  2580. TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
  2581. } else {
  2582. mz_uint lit = *pLZ_codes++;
  2583. MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
  2584. TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
  2585. }
  2586. }
  2587. TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
  2588. return (d->m_pOutput_buf < d->m_pOutput_buf_end);
  2589. }
  2590. #endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && \
  2591. MINIZ_HAS_64BIT_REGISTERS */
  2592. static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block) {
  2593. if (static_block)
  2594. tdefl_start_static_block(d);
  2595. else
  2596. tdefl_start_dynamic_block(d);
  2597. return tdefl_compress_lz_codes(d);
  2598. }
  2599. static int tdefl_flush_block(tdefl_compressor *d, int flush) {
  2600. mz_uint saved_bit_buf, saved_bits_in;
  2601. mz_uint8 *pSaved_output_buf;
  2602. mz_bool comp_block_succeeded = MZ_FALSE;
  2603. int n, use_raw_block =
  2604. ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) &&
  2605. (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size;
  2606. mz_uint8 *pOutput_buf_start =
  2607. ((d->m_pPut_buf_func == NULL) &&
  2608. ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE))
  2609. ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs)
  2610. : d->m_output_buf;
  2611. d->m_pOutput_buf = pOutput_buf_start;
  2612. d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16;
  2613. MZ_ASSERT(!d->m_output_flush_remaining);
  2614. d->m_output_flush_ofs = 0;
  2615. d->m_output_flush_remaining = 0;
  2616. *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left);
  2617. d->m_pLZ_code_buf -= (d->m_num_flags_left == 8);
  2618. if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) {
  2619. TDEFL_PUT_BITS(0x78, 8);
  2620. TDEFL_PUT_BITS(0x01, 8);
  2621. }
  2622. TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1);
  2623. pSaved_output_buf = d->m_pOutput_buf;
  2624. saved_bit_buf = d->m_bit_buffer;
  2625. saved_bits_in = d->m_bits_in;
  2626. if (!use_raw_block)
  2627. comp_block_succeeded =
  2628. tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) ||
  2629. (d->m_total_lz_bytes < 48));
  2630. /* If the block gets expanded, forget the current contents of the output
  2631. * buffer and send a raw block instead. */
  2632. if (((use_raw_block) ||
  2633. ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >=
  2634. d->m_total_lz_bytes))) &&
  2635. ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size)) {
  2636. mz_uint i;
  2637. d->m_pOutput_buf = pSaved_output_buf;
  2638. d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
  2639. TDEFL_PUT_BITS(0, 2);
  2640. if (d->m_bits_in) {
  2641. TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
  2642. }
  2643. for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF) {
  2644. TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16);
  2645. }
  2646. for (i = 0; i < d->m_total_lz_bytes; ++i) {
  2647. TDEFL_PUT_BITS(
  2648. d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK],
  2649. 8);
  2650. }
  2651. }
  2652. /* Check for the extremely unlikely (if not impossible) case of the compressed
  2653. block not fitting into the output buffer when using dynamic codes. */
  2654. else if (!comp_block_succeeded) {
  2655. d->m_pOutput_buf = pSaved_output_buf;
  2656. d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
  2657. tdefl_compress_block(d, MZ_TRUE);
  2658. }
  2659. if (flush) {
  2660. if (flush == TDEFL_FINISH) {
  2661. if (d->m_bits_in) {
  2662. TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
  2663. }
  2664. if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER) {
  2665. mz_uint i, a = d->m_adler32;
  2666. for (i = 0; i < 4; i++) {
  2667. TDEFL_PUT_BITS((a >> 24) & 0xFF, 8);
  2668. a <<= 8;
  2669. }
  2670. }
  2671. } else {
  2672. mz_uint i, z = 0;
  2673. TDEFL_PUT_BITS(0, 3);
  2674. if (d->m_bits_in) {
  2675. TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
  2676. }
  2677. for (i = 2; i; --i, z ^= 0xFFFF) {
  2678. TDEFL_PUT_BITS(z & 0xFFFF, 16);
  2679. }
  2680. }
  2681. }
  2682. MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end);
  2683. memset(&d->m_huff_count[0][0], 0,
  2684. sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
  2685. memset(&d->m_huff_count[1][0], 0,
  2686. sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
  2687. d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
  2688. d->m_pLZ_flags = d->m_lz_code_buf;
  2689. d->m_num_flags_left = 8;
  2690. d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes;
  2691. d->m_total_lz_bytes = 0;
  2692. d->m_block_index++;
  2693. if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0) {
  2694. if (d->m_pPut_buf_func) {
  2695. *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
  2696. if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user))
  2697. return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED);
  2698. } else if (pOutput_buf_start == d->m_output_buf) {
  2699. int bytes_to_copy = (int)MZ_MIN(
  2700. (size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs));
  2701. memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf,
  2702. bytes_to_copy);
  2703. d->m_out_buf_ofs += bytes_to_copy;
  2704. if ((n -= bytes_to_copy) != 0) {
  2705. d->m_output_flush_ofs = bytes_to_copy;
  2706. d->m_output_flush_remaining = n;
  2707. }
  2708. } else {
  2709. d->m_out_buf_ofs += n;
  2710. }
  2711. }
  2712. return d->m_output_flush_remaining;
  2713. }
  2714. #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
  2715. #ifdef MINIZ_UNALIGNED_USE_MEMCPY
  2716. static mz_uint16 TDEFL_READ_UNALIGNED_WORD(const mz_uint8 *p) {
  2717. mz_uint16 ret;
  2718. memcpy(&ret, p, sizeof(mz_uint16));
  2719. return ret;
  2720. }
  2721. static mz_uint16 TDEFL_READ_UNALIGNED_WORD2(const mz_uint16 *p) {
  2722. mz_uint16 ret;
  2723. memcpy(&ret, p, sizeof(mz_uint16));
  2724. return ret;
  2725. }
  2726. #else
  2727. #define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p)
  2728. #define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16 *)(p)
  2729. #endif
  2730. static MZ_FORCEINLINE void
  2731. tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist,
  2732. mz_uint max_match_len, mz_uint *pMatch_dist,
  2733. mz_uint *pMatch_len) {
  2734. mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK,
  2735. match_len = *pMatch_len, probe_pos = pos, next_probe_pos,
  2736. probe_len;
  2737. mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
  2738. const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q;
  2739. mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]),
  2740. s01 = TDEFL_READ_UNALIGNED_WORD2(s);
  2741. MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
  2742. if (max_match_len <= match_len)
  2743. return;
  2744. for (;;) {
  2745. for (;;) {
  2746. if (--num_probes_left == 0)
  2747. return;
  2748. #define TDEFL_PROBE \
  2749. next_probe_pos = d->m_next[probe_pos]; \
  2750. if ((!next_probe_pos) || \
  2751. ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
  2752. return; \
  2753. probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
  2754. if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \
  2755. break;
  2756. TDEFL_PROBE;
  2757. TDEFL_PROBE;
  2758. TDEFL_PROBE;
  2759. }
  2760. if (!dist)
  2761. break;
  2762. q = (const mz_uint16 *)(d->m_dict + probe_pos);
  2763. if (TDEFL_READ_UNALIGNED_WORD2(q) != s01)
  2764. continue;
  2765. p = s;
  2766. probe_len = 32;
  2767. do {
  2768. } while (
  2769. (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
  2770. (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
  2771. (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
  2772. (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
  2773. (--probe_len > 0));
  2774. if (!probe_len) {
  2775. *pMatch_dist = dist;
  2776. *pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN);
  2777. break;
  2778. } else if ((probe_len = ((mz_uint)(p - s) * 2) +
  2779. (mz_uint)(*(const mz_uint8 *)p ==
  2780. *(const mz_uint8 *)q)) > match_len) {
  2781. *pMatch_dist = dist;
  2782. if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) ==
  2783. max_match_len)
  2784. break;
  2785. c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);
  2786. }
  2787. }
  2788. }
  2789. #else
  2790. static MZ_FORCEINLINE void
  2791. tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist,
  2792. mz_uint max_match_len, mz_uint *pMatch_dist,
  2793. mz_uint *pMatch_len) {
  2794. mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK,
  2795. match_len = *pMatch_len, probe_pos = pos, next_probe_pos,
  2796. probe_len;
  2797. mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
  2798. const mz_uint8 *s = d->m_dict + pos, *p, *q;
  2799. mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1];
  2800. MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
  2801. if (max_match_len <= match_len)
  2802. return;
  2803. for (;;) {
  2804. for (;;) {
  2805. if (--num_probes_left == 0)
  2806. return;
  2807. #define TDEFL_PROBE \
  2808. next_probe_pos = d->m_next[probe_pos]; \
  2809. if ((!next_probe_pos) || \
  2810. ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
  2811. return; \
  2812. probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
  2813. if ((d->m_dict[probe_pos + match_len] == c0) && \
  2814. (d->m_dict[probe_pos + match_len - 1] == c1)) \
  2815. break;
  2816. TDEFL_PROBE;
  2817. TDEFL_PROBE;
  2818. TDEFL_PROBE;
  2819. }
  2820. if (!dist)
  2821. break;
  2822. p = s;
  2823. q = d->m_dict + probe_pos;
  2824. for (probe_len = 0; probe_len < max_match_len; probe_len++)
  2825. if (*p++ != *q++)
  2826. break;
  2827. if (probe_len > match_len) {
  2828. *pMatch_dist = dist;
  2829. if ((*pMatch_len = match_len = probe_len) == max_match_len)
  2830. return;
  2831. c0 = d->m_dict[pos + match_len];
  2832. c1 = d->m_dict[pos + match_len - 1];
  2833. }
  2834. }
  2835. }
  2836. #endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES */
  2837. #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
  2838. #ifdef MINIZ_UNALIGNED_USE_MEMCPY
  2839. static mz_uint32 TDEFL_READ_UNALIGNED_WORD32(const mz_uint8 *p) {
  2840. mz_uint32 ret;
  2841. memcpy(&ret, p, sizeof(mz_uint32));
  2842. return ret;
  2843. }
  2844. #else
  2845. #define TDEFL_READ_UNALIGNED_WORD32(p) *(const mz_uint32 *)(p)
  2846. #endif
  2847. static mz_bool tdefl_compress_fast(tdefl_compressor *d) {
  2848. /* Faster, minimally featured LZRW1-style match+parse loop with better
  2849. * register utilization. Intended for applications where raw throughput is
  2850. * valued more highly than ratio. */
  2851. mz_uint lookahead_pos = d->m_lookahead_pos,
  2852. lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size,
  2853. total_lz_bytes = d->m_total_lz_bytes,
  2854. num_flags_left = d->m_num_flags_left;
  2855. mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags;
  2856. mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
  2857. while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size))) {
  2858. const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
  2859. mz_uint dst_pos =
  2860. (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
  2861. mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(
  2862. d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size);
  2863. d->m_src_buf_left -= num_bytes_to_process;
  2864. lookahead_size += num_bytes_to_process;
  2865. while (num_bytes_to_process) {
  2866. mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process);
  2867. memcpy(d->m_dict + dst_pos, d->m_pSrc, n);
  2868. if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
  2869. memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc,
  2870. MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));
  2871. d->m_pSrc += n;
  2872. dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
  2873. num_bytes_to_process -= n;
  2874. }
  2875. dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size);
  2876. if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
  2877. break;
  2878. while (lookahead_size >= 4) {
  2879. mz_uint cur_match_dist, cur_match_len = 1;
  2880. mz_uint8 *pCur_dict = d->m_dict + cur_pos;
  2881. mz_uint first_trigram = TDEFL_READ_UNALIGNED_WORD32(pCur_dict) & 0xFFFFFF;
  2882. mz_uint hash =
  2883. (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) &
  2884. TDEFL_LEVEL1_HASH_SIZE_MASK;
  2885. mz_uint probe_pos = d->m_hash[hash];
  2886. d->m_hash[hash] = (mz_uint16)lookahead_pos;
  2887. if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <=
  2888. dict_size) &&
  2889. ((TDEFL_READ_UNALIGNED_WORD32(
  2890. d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) &
  2891. 0xFFFFFF) == first_trigram)) {
  2892. const mz_uint16 *p = (const mz_uint16 *)pCur_dict;
  2893. const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos);
  2894. mz_uint32 probe_len = 32;
  2895. do {
  2896. } while ((TDEFL_READ_UNALIGNED_WORD2(++p) ==
  2897. TDEFL_READ_UNALIGNED_WORD2(++q)) &&
  2898. (TDEFL_READ_UNALIGNED_WORD2(++p) ==
  2899. TDEFL_READ_UNALIGNED_WORD2(++q)) &&
  2900. (TDEFL_READ_UNALIGNED_WORD2(++p) ==
  2901. TDEFL_READ_UNALIGNED_WORD2(++q)) &&
  2902. (TDEFL_READ_UNALIGNED_WORD2(++p) ==
  2903. TDEFL_READ_UNALIGNED_WORD2(++q)) &&
  2904. (--probe_len > 0));
  2905. cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) +
  2906. (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q);
  2907. if (!probe_len)
  2908. cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;
  2909. if ((cur_match_len < TDEFL_MIN_MATCH_LEN) ||
  2910. ((cur_match_len == TDEFL_MIN_MATCH_LEN) &&
  2911. (cur_match_dist >= 8U * 1024U))) {
  2912. cur_match_len = 1;
  2913. *pLZ_code_buf++ = (mz_uint8)first_trigram;
  2914. *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
  2915. d->m_huff_count[0][(mz_uint8)first_trigram]++;
  2916. } else {
  2917. mz_uint32 s0, s1;
  2918. cur_match_len = MZ_MIN(cur_match_len, lookahead_size);
  2919. MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) &&
  2920. (cur_match_dist >= 1) &&
  2921. (cur_match_dist <= TDEFL_LZ_DICT_SIZE));
  2922. cur_match_dist--;
  2923. pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN);
  2924. #ifdef MINIZ_UNALIGNED_USE_MEMCPY
  2925. memcpy(&pLZ_code_buf[1], &cur_match_dist, sizeof(cur_match_dist));
  2926. #else
  2927. *(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist;
  2928. #endif
  2929. pLZ_code_buf += 3;
  2930. *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80);
  2931. s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];
  2932. s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];
  2933. d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
  2934. d->m_huff_count[0][s_tdefl_len_sym[cur_match_len -
  2935. TDEFL_MIN_MATCH_LEN]]++;
  2936. }
  2937. } else {
  2938. *pLZ_code_buf++ = (mz_uint8)first_trigram;
  2939. *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
  2940. d->m_huff_count[0][(mz_uint8)first_trigram]++;
  2941. }
  2942. if (--num_flags_left == 0) {
  2943. num_flags_left = 8;
  2944. pLZ_flags = pLZ_code_buf++;
  2945. }
  2946. total_lz_bytes += cur_match_len;
  2947. lookahead_pos += cur_match_len;
  2948. dict_size =
  2949. MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE);
  2950. cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK;
  2951. MZ_ASSERT(lookahead_size >= cur_match_len);
  2952. lookahead_size -= cur_match_len;
  2953. if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) {
  2954. int n;
  2955. d->m_lookahead_pos = lookahead_pos;
  2956. d->m_lookahead_size = lookahead_size;
  2957. d->m_dict_size = dict_size;
  2958. d->m_total_lz_bytes = total_lz_bytes;
  2959. d->m_pLZ_code_buf = pLZ_code_buf;
  2960. d->m_pLZ_flags = pLZ_flags;
  2961. d->m_num_flags_left = num_flags_left;
  2962. if ((n = tdefl_flush_block(d, 0)) != 0)
  2963. return (n < 0) ? MZ_FALSE : MZ_TRUE;
  2964. total_lz_bytes = d->m_total_lz_bytes;
  2965. pLZ_code_buf = d->m_pLZ_code_buf;
  2966. pLZ_flags = d->m_pLZ_flags;
  2967. num_flags_left = d->m_num_flags_left;
  2968. }
  2969. }
  2970. while (lookahead_size) {
  2971. mz_uint8 lit = d->m_dict[cur_pos];
  2972. total_lz_bytes++;
  2973. *pLZ_code_buf++ = lit;
  2974. *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
  2975. if (--num_flags_left == 0) {
  2976. num_flags_left = 8;
  2977. pLZ_flags = pLZ_code_buf++;
  2978. }
  2979. d->m_huff_count[0][lit]++;
  2980. lookahead_pos++;
  2981. dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE);
  2982. cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
  2983. lookahead_size--;
  2984. if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) {
  2985. int n;
  2986. d->m_lookahead_pos = lookahead_pos;
  2987. d->m_lookahead_size = lookahead_size;
  2988. d->m_dict_size = dict_size;
  2989. d->m_total_lz_bytes = total_lz_bytes;
  2990. d->m_pLZ_code_buf = pLZ_code_buf;
  2991. d->m_pLZ_flags = pLZ_flags;
  2992. d->m_num_flags_left = num_flags_left;
  2993. if ((n = tdefl_flush_block(d, 0)) != 0)
  2994. return (n < 0) ? MZ_FALSE : MZ_TRUE;
  2995. total_lz_bytes = d->m_total_lz_bytes;
  2996. pLZ_code_buf = d->m_pLZ_code_buf;
  2997. pLZ_flags = d->m_pLZ_flags;
  2998. num_flags_left = d->m_num_flags_left;
  2999. }
  3000. }
  3001. }
  3002. d->m_lookahead_pos = lookahead_pos;
  3003. d->m_lookahead_size = lookahead_size;
  3004. d->m_dict_size = dict_size;
  3005. d->m_total_lz_bytes = total_lz_bytes;
  3006. d->m_pLZ_code_buf = pLZ_code_buf;
  3007. d->m_pLZ_flags = pLZ_flags;
  3008. d->m_num_flags_left = num_flags_left;
  3009. return MZ_TRUE;
  3010. }
  3011. #endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */
  3012. static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d,
  3013. mz_uint8 lit) {
  3014. d->m_total_lz_bytes++;
  3015. *d->m_pLZ_code_buf++ = lit;
  3016. *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1);
  3017. if (--d->m_num_flags_left == 0) {
  3018. d->m_num_flags_left = 8;
  3019. d->m_pLZ_flags = d->m_pLZ_code_buf++;
  3020. }
  3021. d->m_huff_count[0][lit]++;
  3022. }
  3023. static MZ_FORCEINLINE void
  3024. tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist) {
  3025. mz_uint32 s0, s1;
  3026. MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) &&
  3027. (match_dist <= TDEFL_LZ_DICT_SIZE));
  3028. d->m_total_lz_bytes += match_len;
  3029. d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN);
  3030. match_dist -= 1;
  3031. d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF);
  3032. d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8);
  3033. d->m_pLZ_code_buf += 3;
  3034. *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80);
  3035. if (--d->m_num_flags_left == 0) {
  3036. d->m_num_flags_left = 8;
  3037. d->m_pLZ_flags = d->m_pLZ_code_buf++;
  3038. }
  3039. s0 = s_tdefl_small_dist_sym[match_dist & 511];
  3040. s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];
  3041. d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++;
  3042. d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++;
  3043. }
  3044. static mz_bool tdefl_compress_normal(tdefl_compressor *d) {
  3045. const mz_uint8 *pSrc = d->m_pSrc;
  3046. size_t src_buf_left = d->m_src_buf_left;
  3047. tdefl_flush flush = d->m_flush;
  3048. while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) {
  3049. mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
  3050. /* Update dictionary and hash chains. Keeps the lookahead size equal to
  3051. * TDEFL_MAX_MATCH_LEN. */
  3052. if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1)) {
  3053. mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) &
  3054. TDEFL_LZ_DICT_SIZE_MASK,
  3055. ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2;
  3056. mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK]
  3057. << TDEFL_LZ_HASH_SHIFT) ^
  3058. d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK];
  3059. mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(
  3060. src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size);
  3061. const mz_uint8 *pSrc_end = pSrc + num_bytes_to_process;
  3062. src_buf_left -= num_bytes_to_process;
  3063. d->m_lookahead_size += num_bytes_to_process;
  3064. while (pSrc != pSrc_end) {
  3065. mz_uint8 c = *pSrc++;
  3066. d->m_dict[dst_pos] = c;
  3067. if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
  3068. d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
  3069. hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
  3070. d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
  3071. d->m_hash[hash] = (mz_uint16)(ins_pos);
  3072. dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
  3073. ins_pos++;
  3074. }
  3075. } else {
  3076. while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) {
  3077. mz_uint8 c = *pSrc++;
  3078. mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) &
  3079. TDEFL_LZ_DICT_SIZE_MASK;
  3080. src_buf_left--;
  3081. d->m_dict[dst_pos] = c;
  3082. if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
  3083. d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
  3084. if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN) {
  3085. mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2;
  3086. mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK]
  3087. << (TDEFL_LZ_HASH_SHIFT * 2)) ^
  3088. (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK]
  3089. << TDEFL_LZ_HASH_SHIFT) ^
  3090. c) &
  3091. (TDEFL_LZ_HASH_SIZE - 1);
  3092. d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
  3093. d->m_hash[hash] = (mz_uint16)(ins_pos);
  3094. }
  3095. }
  3096. }
  3097. d->m_dict_size =
  3098. MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size);
  3099. if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
  3100. break;
  3101. /* Simple lazy/greedy parsing state machine. */
  3102. len_to_move = 1;
  3103. cur_match_dist = 0;
  3104. cur_match_len =
  3105. d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1);
  3106. cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
  3107. if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS)) {
  3108. if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) {
  3109. mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK];
  3110. cur_match_len = 0;
  3111. while (cur_match_len < d->m_lookahead_size) {
  3112. if (d->m_dict[cur_pos + cur_match_len] != c)
  3113. break;
  3114. cur_match_len++;
  3115. }
  3116. if (cur_match_len < TDEFL_MIN_MATCH_LEN)
  3117. cur_match_len = 0;
  3118. else
  3119. cur_match_dist = 1;
  3120. }
  3121. } else {
  3122. tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size,
  3123. d->m_lookahead_size, &cur_match_dist, &cur_match_len);
  3124. }
  3125. if (((cur_match_len == TDEFL_MIN_MATCH_LEN) &&
  3126. (cur_match_dist >= 8U * 1024U)) ||
  3127. (cur_pos == cur_match_dist) ||
  3128. ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) {
  3129. cur_match_dist = cur_match_len = 0;
  3130. }
  3131. if (d->m_saved_match_len) {
  3132. if (cur_match_len > d->m_saved_match_len) {
  3133. tdefl_record_literal(d, (mz_uint8)d->m_saved_lit);
  3134. if (cur_match_len >= 128) {
  3135. tdefl_record_match(d, cur_match_len, cur_match_dist);
  3136. d->m_saved_match_len = 0;
  3137. len_to_move = cur_match_len;
  3138. } else {
  3139. d->m_saved_lit = d->m_dict[cur_pos];
  3140. d->m_saved_match_dist = cur_match_dist;
  3141. d->m_saved_match_len = cur_match_len;
  3142. }
  3143. } else {
  3144. tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist);
  3145. len_to_move = d->m_saved_match_len - 1;
  3146. d->m_saved_match_len = 0;
  3147. }
  3148. } else if (!cur_match_dist)
  3149. tdefl_record_literal(d,
  3150. d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]);
  3151. else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) ||
  3152. (cur_match_len >= 128)) {
  3153. tdefl_record_match(d, cur_match_len, cur_match_dist);
  3154. len_to_move = cur_match_len;
  3155. } else {
  3156. d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)];
  3157. d->m_saved_match_dist = cur_match_dist;
  3158. d->m_saved_match_len = cur_match_len;
  3159. }
  3160. /* Move the lookahead forward by len_to_move bytes. */
  3161. d->m_lookahead_pos += len_to_move;
  3162. MZ_ASSERT(d->m_lookahead_size >= len_to_move);
  3163. d->m_lookahead_size -= len_to_move;
  3164. d->m_dict_size =
  3165. MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE);
  3166. /* Check if it's time to flush the current LZ codes to the internal output
  3167. * buffer. */
  3168. if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) ||
  3169. ((d->m_total_lz_bytes > 31 * 1024) &&
  3170. (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >=
  3171. d->m_total_lz_bytes) ||
  3172. (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))) {
  3173. int n;
  3174. d->m_pSrc = pSrc;
  3175. d->m_src_buf_left = src_buf_left;
  3176. if ((n = tdefl_flush_block(d, 0)) != 0)
  3177. return (n < 0) ? MZ_FALSE : MZ_TRUE;
  3178. }
  3179. }
  3180. d->m_pSrc = pSrc;
  3181. d->m_src_buf_left = src_buf_left;
  3182. return MZ_TRUE;
  3183. }
  3184. static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d) {
  3185. if (d->m_pIn_buf_size) {
  3186. *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
  3187. }
  3188. if (d->m_pOut_buf_size) {
  3189. size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs,
  3190. d->m_output_flush_remaining);
  3191. memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs,
  3192. d->m_output_buf + d->m_output_flush_ofs, n);
  3193. d->m_output_flush_ofs += (mz_uint)n;
  3194. d->m_output_flush_remaining -= (mz_uint)n;
  3195. d->m_out_buf_ofs += n;
  3196. *d->m_pOut_buf_size = d->m_out_buf_ofs;
  3197. }
  3198. return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE
  3199. : TDEFL_STATUS_OKAY;
  3200. }
  3201. tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf,
  3202. size_t *pIn_buf_size, void *pOut_buf,
  3203. size_t *pOut_buf_size, tdefl_flush flush) {
  3204. if (!d) {
  3205. if (pIn_buf_size)
  3206. *pIn_buf_size = 0;
  3207. if (pOut_buf_size)
  3208. *pOut_buf_size = 0;
  3209. return TDEFL_STATUS_BAD_PARAM;
  3210. }
  3211. d->m_pIn_buf = pIn_buf;
  3212. d->m_pIn_buf_size = pIn_buf_size;
  3213. d->m_pOut_buf = pOut_buf;
  3214. d->m_pOut_buf_size = pOut_buf_size;
  3215. d->m_pSrc = (const mz_uint8 *)(pIn_buf);
  3216. d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0;
  3217. d->m_out_buf_ofs = 0;
  3218. d->m_flush = flush;
  3219. if (((d->m_pPut_buf_func != NULL) ==
  3220. ((pOut_buf != NULL) || (pOut_buf_size != NULL))) ||
  3221. (d->m_prev_return_status != TDEFL_STATUS_OKAY) ||
  3222. (d->m_wants_to_finish && (flush != TDEFL_FINISH)) ||
  3223. (pIn_buf_size && *pIn_buf_size && !pIn_buf) ||
  3224. (pOut_buf_size && *pOut_buf_size && !pOut_buf)) {
  3225. if (pIn_buf_size)
  3226. *pIn_buf_size = 0;
  3227. if (pOut_buf_size)
  3228. *pOut_buf_size = 0;
  3229. return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM);
  3230. }
  3231. d->m_wants_to_finish |= (flush == TDEFL_FINISH);
  3232. if ((d->m_output_flush_remaining) || (d->m_finished))
  3233. return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
  3234. #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
  3235. if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) &&
  3236. ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) &&
  3237. ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS |
  3238. TDEFL_RLE_MATCHES)) == 0)) {
  3239. if (!tdefl_compress_fast(d))
  3240. return d->m_prev_return_status;
  3241. } else
  3242. #endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */
  3243. {
  3244. if (!tdefl_compress_normal(d))
  3245. return d->m_prev_return_status;
  3246. }
  3247. if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) &&
  3248. (pIn_buf))
  3249. d->m_adler32 =
  3250. (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf,
  3251. d->m_pSrc - (const mz_uint8 *)pIn_buf);
  3252. if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) &&
  3253. (!d->m_output_flush_remaining)) {
  3254. if (tdefl_flush_block(d, flush) < 0)
  3255. return d->m_prev_return_status;
  3256. d->m_finished = (flush == TDEFL_FINISH);
  3257. if (flush == TDEFL_FULL_FLUSH) {
  3258. MZ_CLEAR_OBJ(d->m_hash);
  3259. MZ_CLEAR_OBJ(d->m_next);
  3260. d->m_dict_size = 0;
  3261. }
  3262. }
  3263. return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
  3264. }
  3265. tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf,
  3266. size_t in_buf_size, tdefl_flush flush) {
  3267. MZ_ASSERT(d->m_pPut_buf_func);
  3268. return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);
  3269. }
  3270. tdefl_status tdefl_init(tdefl_compressor *d,
  3271. tdefl_put_buf_func_ptr pPut_buf_func,
  3272. void *pPut_buf_user, int flags) {
  3273. d->m_pPut_buf_func = pPut_buf_func;
  3274. d->m_pPut_buf_user = pPut_buf_user;
  3275. d->m_flags = (mz_uint)(flags);
  3276. d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3;
  3277. d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0;
  3278. d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
  3279. if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
  3280. MZ_CLEAR_OBJ(d->m_hash);
  3281. d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size =
  3282. d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0;
  3283. d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished =
  3284. d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0;
  3285. d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
  3286. d->m_pLZ_flags = d->m_lz_code_buf;
  3287. *d->m_pLZ_flags = 0;
  3288. d->m_num_flags_left = 8;
  3289. d->m_pOutput_buf = d->m_output_buf;
  3290. d->m_pOutput_buf_end = d->m_output_buf;
  3291. d->m_prev_return_status = TDEFL_STATUS_OKAY;
  3292. d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0;
  3293. d->m_adler32 = 1;
  3294. d->m_pIn_buf = NULL;
  3295. d->m_pOut_buf = NULL;
  3296. d->m_pIn_buf_size = NULL;
  3297. d->m_pOut_buf_size = NULL;
  3298. d->m_flush = TDEFL_NO_FLUSH;
  3299. d->m_pSrc = NULL;
  3300. d->m_src_buf_left = 0;
  3301. d->m_out_buf_ofs = 0;
  3302. if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
  3303. MZ_CLEAR_OBJ(d->m_dict);
  3304. memset(&d->m_huff_count[0][0], 0,
  3305. sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
  3306. memset(&d->m_huff_count[1][0], 0,
  3307. sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
  3308. return TDEFL_STATUS_OKAY;
  3309. }
  3310. tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d) {
  3311. return d->m_prev_return_status;
  3312. }
  3313. mz_uint32 tdefl_get_adler32(tdefl_compressor *d) { return d->m_adler32; }
  3314. mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len,
  3315. tdefl_put_buf_func_ptr pPut_buf_func,
  3316. void *pPut_buf_user, int flags) {
  3317. tdefl_compressor *pComp;
  3318. mz_bool succeeded;
  3319. if (((buf_len) && (!pBuf)) || (!pPut_buf_func))
  3320. return MZ_FALSE;
  3321. pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
  3322. if (!pComp)
  3323. return MZ_FALSE;
  3324. succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) ==
  3325. TDEFL_STATUS_OKAY);
  3326. succeeded =
  3327. succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) ==
  3328. TDEFL_STATUS_DONE);
  3329. MZ_FREE(pComp);
  3330. return succeeded;
  3331. }
  3332. typedef struct {
  3333. size_t m_size, m_capacity;
  3334. mz_uint8 *m_pBuf;
  3335. mz_bool m_expandable;
  3336. } tdefl_output_buffer;
  3337. static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len,
  3338. void *pUser) {
  3339. tdefl_output_buffer *p = (tdefl_output_buffer *)pUser;
  3340. size_t new_size = p->m_size + len;
  3341. if (new_size > p->m_capacity) {
  3342. size_t new_capacity = p->m_capacity;
  3343. mz_uint8 *pNew_buf;
  3344. if (!p->m_expandable)
  3345. return MZ_FALSE;
  3346. do {
  3347. new_capacity = MZ_MAX(128U, new_capacity << 1U);
  3348. } while (new_size > new_capacity);
  3349. pNew_buf = (mz_uint8 *)MZ_REALLOC(p->m_pBuf, new_capacity);
  3350. if (!pNew_buf)
  3351. return MZ_FALSE;
  3352. p->m_pBuf = pNew_buf;
  3353. p->m_capacity = new_capacity;
  3354. }
  3355. memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len);
  3356. p->m_size = new_size;
  3357. return MZ_TRUE;
  3358. }
  3359. void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len,
  3360. size_t *pOut_len, int flags) {
  3361. tdefl_output_buffer out_buf;
  3362. MZ_CLEAR_OBJ(out_buf);
  3363. if (!pOut_len)
  3364. return MZ_FALSE;
  3365. else
  3366. *pOut_len = 0;
  3367. out_buf.m_expandable = MZ_TRUE;
  3368. if (!tdefl_compress_mem_to_output(
  3369. pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
  3370. return NULL;
  3371. *pOut_len = out_buf.m_size;
  3372. return out_buf.m_pBuf;
  3373. }
  3374. size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len,
  3375. const void *pSrc_buf, size_t src_buf_len,
  3376. int flags) {
  3377. tdefl_output_buffer out_buf;
  3378. MZ_CLEAR_OBJ(out_buf);
  3379. if (!pOut_buf)
  3380. return 0;
  3381. out_buf.m_pBuf = (mz_uint8 *)pOut_buf;
  3382. out_buf.m_capacity = out_buf_len;
  3383. if (!tdefl_compress_mem_to_output(
  3384. pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
  3385. return 0;
  3386. return out_buf.m_size;
  3387. }
  3388. static const mz_uint s_tdefl_num_probes[11] = {0, 1, 6, 32, 16, 32,
  3389. 128, 256, 512, 768, 1500};
  3390. /* level may actually range from [0,10] (10 is a "hidden" max level, where we
  3391. * want a bit more compression and it's fine if throughput to fall off a cliff
  3392. * on some files). */
  3393. mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits,
  3394. int strategy) {
  3395. mz_uint comp_flags =
  3396. s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] |
  3397. ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);
  3398. if (window_bits > 0)
  3399. comp_flags |= TDEFL_WRITE_ZLIB_HEADER;
  3400. if (!level)
  3401. comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
  3402. else if (strategy == MZ_FILTERED)
  3403. comp_flags |= TDEFL_FILTER_MATCHES;
  3404. else if (strategy == MZ_HUFFMAN_ONLY)
  3405. comp_flags &= ~TDEFL_MAX_PROBES_MASK;
  3406. else if (strategy == MZ_FIXED)
  3407. comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS;
  3408. else if (strategy == MZ_RLE)
  3409. comp_flags |= TDEFL_RLE_MATCHES;
  3410. return comp_flags;
  3411. }
  3412. #ifdef _MSC_VER
  3413. #pragma warning(push)
  3414. #pragma warning(disable : 4204) /* nonstandard extension used : non-constant \
  3415. aggregate initializer (also supported by \
  3416. GNU C and C99, so no big deal) */
  3417. #endif
  3418. /* Simple PNG writer function by Alex Evans, 2011. Released into the public
  3419. domain: https://gist.github.com/908299, more context at
  3420. http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.
  3421. This is actually a modification of Alex's original code so PNG files generated
  3422. by this function pass pngcheck. */
  3423. void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w,
  3424. int h, int num_chans,
  3425. size_t *pLen_out,
  3426. mz_uint level, mz_bool flip) {
  3427. /* Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was
  3428. * defined. */
  3429. static const mz_uint s_tdefl_png_num_probes[11] = {
  3430. 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500};
  3431. tdefl_compressor *pComp =
  3432. (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
  3433. tdefl_output_buffer out_buf;
  3434. int i, bpl = w * num_chans, y, z;
  3435. mz_uint32 c;
  3436. *pLen_out = 0;
  3437. if (!pComp)
  3438. return NULL;
  3439. MZ_CLEAR_OBJ(out_buf);
  3440. out_buf.m_expandable = MZ_TRUE;
  3441. out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h);
  3442. if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity))) {
  3443. MZ_FREE(pComp);
  3444. return NULL;
  3445. }
  3446. /* write dummy header */
  3447. for (z = 41; z; --z)
  3448. tdefl_output_buffer_putter(&z, 1, &out_buf);
  3449. /* compress image data */
  3450. tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf,
  3451. s_tdefl_png_num_probes[MZ_MIN(10, level)] |
  3452. TDEFL_WRITE_ZLIB_HEADER);
  3453. for (y = 0; y < h; ++y) {
  3454. tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH);
  3455. tdefl_compress_buffer(pComp,
  3456. (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl,
  3457. bpl, TDEFL_NO_FLUSH);
  3458. }
  3459. if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) !=
  3460. TDEFL_STATUS_DONE) {
  3461. MZ_FREE(pComp);
  3462. MZ_FREE(out_buf.m_pBuf);
  3463. return NULL;
  3464. }
  3465. /* write real header */
  3466. *pLen_out = out_buf.m_size - 41;
  3467. {
  3468. static const mz_uint8 chans[] = {0x00, 0x00, 0x04, 0x02, 0x06};
  3469. mz_uint8 pnghdr[41] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00,
  3470. 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00,
  3471. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  3472. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3473. 0x00, 0x49, 0x44, 0x41, 0x54};
  3474. pnghdr[18] = (mz_uint8)(w >> 8);
  3475. pnghdr[19] = (mz_uint8)w;
  3476. pnghdr[22] = (mz_uint8)(h >> 8);
  3477. pnghdr[23] = (mz_uint8)h;
  3478. pnghdr[25] = chans[num_chans];
  3479. pnghdr[33] = (mz_uint8)(*pLen_out >> 24);
  3480. pnghdr[34] = (mz_uint8)(*pLen_out >> 16);
  3481. pnghdr[35] = (mz_uint8)(*pLen_out >> 8);
  3482. pnghdr[36] = (mz_uint8)*pLen_out;
  3483. c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17);
  3484. for (i = 0; i < 4; ++i, c <<= 8)
  3485. ((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24);
  3486. memcpy(out_buf.m_pBuf, pnghdr, 41);
  3487. }
  3488. /* write footer (IDAT CRC-32, followed by IEND chunk) */
  3489. if (!tdefl_output_buffer_putter(
  3490. "\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf)) {
  3491. *pLen_out = 0;
  3492. MZ_FREE(pComp);
  3493. MZ_FREE(out_buf.m_pBuf);
  3494. return NULL;
  3495. }
  3496. c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4,
  3497. *pLen_out + 4);
  3498. for (i = 0; i < 4; ++i, c <<= 8)
  3499. (out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24);
  3500. /* compute final size of file, grab compressed data buffer and return */
  3501. *pLen_out += 57;
  3502. MZ_FREE(pComp);
  3503. return out_buf.m_pBuf;
  3504. }
  3505. void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h,
  3506. int num_chans, size_t *pLen_out) {
  3507. /* Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we
  3508. * can't depend on MZ_DEFAULT_LEVEL being available in case the zlib API's
  3509. * where #defined out) */
  3510. return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans,
  3511. pLen_out, 6, MZ_FALSE);
  3512. }
  3513. #ifndef MINIZ_NO_MALLOC
  3514. /* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that
  3515. */
  3516. /* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */
  3517. /* structure size and allocation mechanism. */
  3518. tdefl_compressor *tdefl_compressor_alloc() {
  3519. return (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
  3520. }
  3521. void tdefl_compressor_free(tdefl_compressor *pComp) { MZ_FREE(pComp); }
  3522. #endif
  3523. #ifdef _MSC_VER
  3524. #pragma warning(pop)
  3525. #endif
  3526. /**************************************************************************
  3527. *
  3528. * Copyright 2013-2014 RAD Game Tools and Valve Software
  3529. * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
  3530. * All Rights Reserved.
  3531. *
  3532. * Permission is hereby granted, free of charge, to any person obtaining a copy
  3533. * of this software and associated documentation files (the "Software"), to deal
  3534. * in the Software without restriction, including without limitation the rights
  3535. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  3536. * copies of the Software, and to permit persons to whom the Software is
  3537. * furnished to do so, subject to the following conditions:
  3538. *
  3539. * The above copyright notice and this permission notice shall be included in
  3540. * all copies or substantial portions of the Software.
  3541. *
  3542. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3543. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3544. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3545. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3546. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  3547. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  3548. * THE SOFTWARE.
  3549. *
  3550. **************************************************************************/
  3551. /* ------------------- Low-level Decompression (completely independent from all
  3552. * compression API's) */
  3553. #define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
  3554. #define TINFL_MEMSET(p, c, l) memset(p, c, l)
  3555. #define TINFL_CR_BEGIN \
  3556. switch (r->m_state) { \
  3557. case 0:
  3558. #define TINFL_CR_RETURN(state_index, result) \
  3559. do { \
  3560. status = result; \
  3561. r->m_state = state_index; \
  3562. goto common_exit; \
  3563. case state_index:; \
  3564. } \
  3565. MZ_MACRO_END
  3566. #define TINFL_CR_RETURN_FOREVER(state_index, result) \
  3567. do { \
  3568. for (;;) { \
  3569. TINFL_CR_RETURN(state_index, result); \
  3570. } \
  3571. } \
  3572. MZ_MACRO_END
  3573. #define TINFL_CR_FINISH }
  3574. #define TINFL_GET_BYTE(state_index, c) \
  3575. do { \
  3576. while (pIn_buf_cur >= pIn_buf_end) { \
  3577. TINFL_CR_RETURN(state_index, \
  3578. (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) \
  3579. ? TINFL_STATUS_NEEDS_MORE_INPUT \
  3580. : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
  3581. } \
  3582. c = *pIn_buf_cur++; \
  3583. } \
  3584. MZ_MACRO_END
  3585. #define TINFL_NEED_BITS(state_index, n) \
  3586. do { \
  3587. mz_uint c; \
  3588. TINFL_GET_BYTE(state_index, c); \
  3589. bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
  3590. num_bits += 8; \
  3591. } while (num_bits < (mz_uint)(n))
  3592. #define TINFL_SKIP_BITS(state_index, n) \
  3593. do { \
  3594. if (num_bits < (mz_uint)(n)) { \
  3595. TINFL_NEED_BITS(state_index, n); \
  3596. } \
  3597. bit_buf >>= (n); \
  3598. num_bits -= (n); \
  3599. } \
  3600. MZ_MACRO_END
  3601. #define TINFL_GET_BITS(state_index, b, n) \
  3602. do { \
  3603. if (num_bits < (mz_uint)(n)) { \
  3604. TINFL_NEED_BITS(state_index, n); \
  3605. } \
  3606. b = bit_buf & ((1 << (n)) - 1); \
  3607. bit_buf >>= (n); \
  3608. num_bits -= (n); \
  3609. } \
  3610. MZ_MACRO_END
  3611. /* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes
  3612. * remaining in the input buffer falls below 2. */
  3613. /* It reads just enough bytes from the input stream that are needed to decode
  3614. * the next Huffman code (and absolutely no more). It works by trying to fully
  3615. * decode a */
  3616. /* Huffman code by using whatever bits are currently present in the bit buffer.
  3617. * If this fails, it reads another byte, and tries again until it succeeds or
  3618. * until the */
  3619. /* bit buffer contains >=15 bits (deflate's max. Huffman code size). */
  3620. #define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
  3621. do { \
  3622. temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
  3623. if (temp >= 0) { \
  3624. code_len = temp >> 9; \
  3625. if ((code_len) && (num_bits >= code_len)) \
  3626. break; \
  3627. } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
  3628. code_len = TINFL_FAST_LOOKUP_BITS; \
  3629. do { \
  3630. temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
  3631. } while ((temp < 0) && (num_bits >= (code_len + 1))); \
  3632. if (temp >= 0) \
  3633. break; \
  3634. } \
  3635. TINFL_GET_BYTE(state_index, c); \
  3636. bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
  3637. num_bits += 8; \
  3638. } while (num_bits < 15);
  3639. /* TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex
  3640. * than you would initially expect because the zlib API expects the decompressor
  3641. * to never read */
  3642. /* beyond the final byte of the deflate stream. (In other words, when this macro
  3643. * wants to read another byte from the input, it REALLY needs another byte in
  3644. * order to fully */
  3645. /* decode the next Huffman code.) Handling this properly is particularly
  3646. * important on raw deflate (non-zlib) streams, which aren't followed by a byte
  3647. * aligned adler-32. */
  3648. /* The slow path is only executed at the very end of the input buffer. */
  3649. /* v1.16: The original macro handled the case at the very end of the passed-in
  3650. * input buffer, but we also need to handle the case where the user passes in
  3651. * 1+zillion bytes */
  3652. /* following the deflate data and our non-conservative read-ahead path won't
  3653. * kick in here on this code. This is much trickier. */
  3654. #define TINFL_HUFF_DECODE(state_index, sym, pHuff) \
  3655. do { \
  3656. int temp; \
  3657. mz_uint code_len, c; \
  3658. if (num_bits < 15) { \
  3659. if ((pIn_buf_end - pIn_buf_cur) < 2) { \
  3660. TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
  3661. } else { \
  3662. bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | \
  3663. (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
  3664. pIn_buf_cur += 2; \
  3665. num_bits += 16; \
  3666. } \
  3667. } \
  3668. if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= \
  3669. 0) \
  3670. code_len = temp >> 9, temp &= 511; \
  3671. else { \
  3672. code_len = TINFL_FAST_LOOKUP_BITS; \
  3673. do { \
  3674. temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
  3675. } while (temp < 0); \
  3676. } \
  3677. sym = temp; \
  3678. bit_buf >>= code_len; \
  3679. num_bits -= code_len; \
  3680. } \
  3681. MZ_MACRO_END
  3682. tinfl_status tinfl_decompress(tinfl_decompressor *r,
  3683. const mz_uint8 *pIn_buf_next,
  3684. size_t *pIn_buf_size, mz_uint8 *pOut_buf_start,
  3685. mz_uint8 *pOut_buf_next, size_t *pOut_buf_size,
  3686. const mz_uint32 decomp_flags) {
  3687. static const int s_length_base[31] = {
  3688. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  3689. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  3690. static const int s_length_extra[31] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
  3691. 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4,
  3692. 4, 4, 5, 5, 5, 5, 0, 0, 0};
  3693. static const int s_dist_base[32] = {
  3694. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33,
  3695. 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537,
  3696. 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0};
  3697. static const int s_dist_extra[32] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
  3698. 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
  3699. 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
  3700. static const mz_uint8 s_length_dezigzag[19] = {
  3701. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  3702. static const int s_min_table_sizes[3] = {257, 1, 4};
  3703. tinfl_status status = TINFL_STATUS_FAILED;
  3704. mz_uint32 num_bits, dist, counter, num_extra;
  3705. tinfl_bit_buf_t bit_buf;
  3706. const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end =
  3707. pIn_buf_next + *pIn_buf_size;
  3708. mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end =
  3709. pOut_buf_next + *pOut_buf_size;
  3710. size_t out_buf_size_mask =
  3711. (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)
  3712. ? (size_t)-1
  3713. : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1,
  3714. dist_from_out_buf_start;
  3715. /* Ensure the output buffer's size is a power of 2, unless the output buffer
  3716. * is large enough to hold the entire output file (in which case it doesn't
  3717. * matter). */
  3718. if (((out_buf_size_mask + 1) & out_buf_size_mask) ||
  3719. (pOut_buf_next < pOut_buf_start)) {
  3720. *pIn_buf_size = *pOut_buf_size = 0;
  3721. return TINFL_STATUS_BAD_PARAM;
  3722. }
  3723. num_bits = r->m_num_bits;
  3724. bit_buf = r->m_bit_buf;
  3725. dist = r->m_dist;
  3726. counter = r->m_counter;
  3727. num_extra = r->m_num_extra;
  3728. dist_from_out_buf_start = r->m_dist_from_out_buf_start;
  3729. TINFL_CR_BEGIN
  3730. bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
  3731. r->m_z_adler32 = r->m_check_adler32 = 1;
  3732. if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) {
  3733. TINFL_GET_BYTE(1, r->m_zhdr0);
  3734. TINFL_GET_BYTE(2, r->m_zhdr1);
  3735. counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) ||
  3736. (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
  3737. if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
  3738. counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) ||
  3739. ((out_buf_size_mask + 1) <
  3740. (size_t)(1U << (8U + (r->m_zhdr0 >> 4)))));
  3741. if (counter) {
  3742. TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED);
  3743. }
  3744. }
  3745. do {
  3746. TINFL_GET_BITS(3, r->m_final, 3);
  3747. r->m_type = r->m_final >> 1;
  3748. if (r->m_type == 0) {
  3749. TINFL_SKIP_BITS(5, num_bits & 7);
  3750. for (counter = 0; counter < 4; ++counter) {
  3751. if (num_bits)
  3752. TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
  3753. else
  3754. TINFL_GET_BYTE(7, r->m_raw_header[counter]);
  3755. }
  3756. if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) !=
  3757. (mz_uint)(0xFFFF ^
  3758. (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) {
  3759. TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
  3760. }
  3761. while ((counter) && (num_bits)) {
  3762. TINFL_GET_BITS(51, dist, 8);
  3763. while (pOut_buf_cur >= pOut_buf_end) {
  3764. TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
  3765. }
  3766. *pOut_buf_cur++ = (mz_uint8)dist;
  3767. counter--;
  3768. }
  3769. while (counter) {
  3770. size_t n;
  3771. while (pOut_buf_cur >= pOut_buf_end) {
  3772. TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
  3773. }
  3774. while (pIn_buf_cur >= pIn_buf_end) {
  3775. TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT)
  3776. ? TINFL_STATUS_NEEDS_MORE_INPUT
  3777. : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);
  3778. }
  3779. n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur),
  3780. (size_t)(pIn_buf_end - pIn_buf_cur)),
  3781. counter);
  3782. TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
  3783. pIn_buf_cur += n;
  3784. pOut_buf_cur += n;
  3785. counter -= (mz_uint)n;
  3786. }
  3787. } else if (r->m_type == 3) {
  3788. TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
  3789. } else {
  3790. if (r->m_type == 1) {
  3791. mz_uint8 *p = r->m_tables[0].m_code_size;
  3792. mz_uint i;
  3793. r->m_table_sizes[0] = 288;
  3794. r->m_table_sizes[1] = 32;
  3795. TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32);
  3796. for (i = 0; i <= 143; ++i)
  3797. *p++ = 8;
  3798. for (; i <= 255; ++i)
  3799. *p++ = 9;
  3800. for (; i <= 279; ++i)
  3801. *p++ = 7;
  3802. for (; i <= 287; ++i)
  3803. *p++ = 8;
  3804. } else {
  3805. for (counter = 0; counter < 3; counter++) {
  3806. TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
  3807. r->m_table_sizes[counter] += s_min_table_sizes[counter];
  3808. }
  3809. MZ_CLEAR_OBJ(r->m_tables[2].m_code_size);
  3810. for (counter = 0; counter < r->m_table_sizes[2]; counter++) {
  3811. mz_uint s;
  3812. TINFL_GET_BITS(14, s, 3);
  3813. r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s;
  3814. }
  3815. r->m_table_sizes[2] = 19;
  3816. }
  3817. for (; (int)r->m_type >= 0; r->m_type--) {
  3818. int tree_next, tree_cur;
  3819. tinfl_huff_table *pTable;
  3820. mz_uint i, j, used_syms, total, sym_index, next_code[17],
  3821. total_syms[16];
  3822. pTable = &r->m_tables[r->m_type];
  3823. MZ_CLEAR_OBJ(total_syms);
  3824. MZ_CLEAR_OBJ(pTable->m_look_up);
  3825. MZ_CLEAR_OBJ(pTable->m_tree);
  3826. for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
  3827. total_syms[pTable->m_code_size[i]]++;
  3828. used_syms = 0, total = 0;
  3829. next_code[0] = next_code[1] = 0;
  3830. for (i = 1; i <= 15; ++i) {
  3831. used_syms += total_syms[i];
  3832. next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
  3833. }
  3834. if ((65536 != total) && (used_syms > 1)) {
  3835. TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
  3836. }
  3837. for (tree_next = -1, sym_index = 0;
  3838. sym_index < r->m_table_sizes[r->m_type]; ++sym_index) {
  3839. mz_uint rev_code = 0, l, cur_code,
  3840. code_size = pTable->m_code_size[sym_index];
  3841. if (!code_size)
  3842. continue;
  3843. cur_code = next_code[code_size]++;
  3844. for (l = code_size; l > 0; l--, cur_code >>= 1)
  3845. rev_code = (rev_code << 1) | (cur_code & 1);
  3846. if (code_size <= TINFL_FAST_LOOKUP_BITS) {
  3847. mz_int16 k = (mz_int16)((code_size << 9) | sym_index);
  3848. while (rev_code < TINFL_FAST_LOOKUP_SIZE) {
  3849. pTable->m_look_up[rev_code] = k;
  3850. rev_code += (1 << code_size);
  3851. }
  3852. continue;
  3853. }
  3854. if (0 ==
  3855. (tree_cur = pTable->m_look_up[rev_code &
  3856. (TINFL_FAST_LOOKUP_SIZE - 1)])) {
  3857. pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] =
  3858. (mz_int16)tree_next;
  3859. tree_cur = tree_next;
  3860. tree_next -= 2;
  3861. }
  3862. rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
  3863. for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) {
  3864. tree_cur -= ((rev_code >>= 1) & 1);
  3865. if (!pTable->m_tree[-tree_cur - 1]) {
  3866. pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next;
  3867. tree_cur = tree_next;
  3868. tree_next -= 2;
  3869. } else
  3870. tree_cur = pTable->m_tree[-tree_cur - 1];
  3871. }
  3872. tree_cur -= ((rev_code >>= 1) & 1);
  3873. (void)rev_code; // unused
  3874. pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index;
  3875. }
  3876. if (r->m_type == 2) {
  3877. for (counter = 0;
  3878. counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) {
  3879. mz_uint s;
  3880. TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]);
  3881. if (dist < 16) {
  3882. r->m_len_codes[counter++] = (mz_uint8)dist;
  3883. continue;
  3884. }
  3885. if ((dist == 16) && (!counter)) {
  3886. TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
  3887. }
  3888. num_extra = "\02\03\07"[dist - 16];
  3889. TINFL_GET_BITS(18, s, num_extra);
  3890. s += "\03\03\013"[dist - 16];
  3891. TINFL_MEMSET(r->m_len_codes + counter,
  3892. (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
  3893. counter += s;
  3894. }
  3895. if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) {
  3896. TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
  3897. }
  3898. TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes,
  3899. r->m_table_sizes[0]);
  3900. TINFL_MEMCPY(r->m_tables[1].m_code_size,
  3901. r->m_len_codes + r->m_table_sizes[0],
  3902. r->m_table_sizes[1]);
  3903. }
  3904. }
  3905. for (;;) {
  3906. mz_uint8 *pSrc;
  3907. for (;;) {
  3908. if (((pIn_buf_end - pIn_buf_cur) < 4) ||
  3909. ((pOut_buf_end - pOut_buf_cur) < 2)) {
  3910. TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]);
  3911. if (counter >= 256)
  3912. break;
  3913. while (pOut_buf_cur >= pOut_buf_end) {
  3914. TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
  3915. }
  3916. *pOut_buf_cur++ = (mz_uint8)counter;
  3917. } else {
  3918. int sym2;
  3919. mz_uint code_len;
  3920. #if TINFL_USE_64BIT_BITBUF
  3921. if (num_bits < 30) {
  3922. bit_buf |=
  3923. (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
  3924. pIn_buf_cur += 4;
  3925. num_bits += 32;
  3926. }
  3927. #else
  3928. if (num_bits < 15) {
  3929. bit_buf |=
  3930. (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
  3931. pIn_buf_cur += 2;
  3932. num_bits += 16;
  3933. }
  3934. #endif
  3935. if ((sym2 =
  3936. r->m_tables[0]
  3937. .m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >=
  3938. 0)
  3939. code_len = sym2 >> 9;
  3940. else {
  3941. code_len = TINFL_FAST_LOOKUP_BITS;
  3942. do {
  3943. sym2 = r->m_tables[0]
  3944. .m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];
  3945. } while (sym2 < 0);
  3946. }
  3947. counter = sym2;
  3948. bit_buf >>= code_len;
  3949. num_bits -= code_len;
  3950. if (counter & 256)
  3951. break;
  3952. #if !TINFL_USE_64BIT_BITBUF
  3953. if (num_bits < 15) {
  3954. bit_buf |=
  3955. (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
  3956. pIn_buf_cur += 2;
  3957. num_bits += 16;
  3958. }
  3959. #endif
  3960. if ((sym2 =
  3961. r->m_tables[0]
  3962. .m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >=
  3963. 0)
  3964. code_len = sym2 >> 9;
  3965. else {
  3966. code_len = TINFL_FAST_LOOKUP_BITS;
  3967. do {
  3968. sym2 = r->m_tables[0]
  3969. .m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];
  3970. } while (sym2 < 0);
  3971. }
  3972. bit_buf >>= code_len;
  3973. num_bits -= code_len;
  3974. pOut_buf_cur[0] = (mz_uint8)counter;
  3975. if (sym2 & 256) {
  3976. pOut_buf_cur++;
  3977. counter = sym2;
  3978. break;
  3979. }
  3980. pOut_buf_cur[1] = (mz_uint8)sym2;
  3981. pOut_buf_cur += 2;
  3982. }
  3983. }
  3984. if ((counter &= 511) == 256)
  3985. break;
  3986. num_extra = s_length_extra[counter - 257];
  3987. counter = s_length_base[counter - 257];
  3988. if (num_extra) {
  3989. mz_uint extra_bits;
  3990. TINFL_GET_BITS(25, extra_bits, num_extra);
  3991. counter += extra_bits;
  3992. }
  3993. TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]);
  3994. num_extra = s_dist_extra[dist];
  3995. dist = s_dist_base[dist];
  3996. if (num_extra) {
  3997. mz_uint extra_bits;
  3998. TINFL_GET_BITS(27, extra_bits, num_extra);
  3999. dist += extra_bits;
  4000. }
  4001. dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
  4002. if ((dist == 0 || dist > dist_from_out_buf_start ||
  4003. dist_from_out_buf_start == 0) &&
  4004. (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) {
  4005. TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
  4006. }
  4007. pSrc = pOut_buf_start +
  4008. ((dist_from_out_buf_start - dist) & out_buf_size_mask);
  4009. if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) {
  4010. while (counter--) {
  4011. while (pOut_buf_cur >= pOut_buf_end) {
  4012. TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
  4013. }
  4014. *pOut_buf_cur++ =
  4015. pOut_buf_start[(dist_from_out_buf_start++ - dist) &
  4016. out_buf_size_mask];
  4017. }
  4018. continue;
  4019. }
  4020. #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
  4021. else if ((counter >= 9) && (counter <= dist)) {
  4022. const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
  4023. do {
  4024. #ifdef MINIZ_UNALIGNED_USE_MEMCPY
  4025. memcpy(pOut_buf_cur, pSrc, sizeof(mz_uint32) * 2);
  4026. #else
  4027. ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];
  4028. ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];
  4029. #endif
  4030. pOut_buf_cur += 8;
  4031. } while ((pSrc += 8) < pSrc_end);
  4032. if ((counter &= 7) < 3) {
  4033. if (counter) {
  4034. pOut_buf_cur[0] = pSrc[0];
  4035. if (counter > 1)
  4036. pOut_buf_cur[1] = pSrc[1];
  4037. pOut_buf_cur += counter;
  4038. }
  4039. continue;
  4040. }
  4041. }
  4042. #endif
  4043. while (counter > 2) {
  4044. pOut_buf_cur[0] = pSrc[0];
  4045. pOut_buf_cur[1] = pSrc[1];
  4046. pOut_buf_cur[2] = pSrc[2];
  4047. pOut_buf_cur += 3;
  4048. pSrc += 3;
  4049. counter -= 3;
  4050. }
  4051. if (counter > 0) {
  4052. pOut_buf_cur[0] = pSrc[0];
  4053. if (counter > 1)
  4054. pOut_buf_cur[1] = pSrc[1];
  4055. pOut_buf_cur += counter;
  4056. }
  4057. }
  4058. }
  4059. } while (!(r->m_final & 1));
  4060. /* Ensure byte alignment and put back any bytes from the bitbuf if we've
  4061. * looked ahead too far on gzip, or other Deflate streams followed by
  4062. * arbitrary data. */
  4063. /* I'm being super conservative here. A number of simplifications can be made
  4064. * to the byte alignment part, and the Adler32 check shouldn't ever need to
  4065. * worry about reading from the bitbuf now. */
  4066. TINFL_SKIP_BITS(32, num_bits & 7);
  4067. while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) {
  4068. --pIn_buf_cur;
  4069. num_bits -= 8;
  4070. }
  4071. bit_buf &= (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1);
  4072. MZ_ASSERT(!num_bits); /* if this assert fires then we've read beyond the end
  4073. of non-deflate/zlib streams with following data (such
  4074. as gzip streams). */
  4075. if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) {
  4076. for (counter = 0; counter < 4; ++counter) {
  4077. mz_uint s;
  4078. if (num_bits)
  4079. TINFL_GET_BITS(41, s, 8);
  4080. else
  4081. TINFL_GET_BYTE(42, s);
  4082. r->m_z_adler32 = (r->m_z_adler32 << 8) | s;
  4083. }
  4084. }
  4085. TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
  4086. TINFL_CR_FINISH
  4087. common_exit:
  4088. /* As long as we aren't telling the caller that we NEED more input to make
  4089. * forward progress: */
  4090. /* Put back any bytes from the bitbuf in case we've looked ahead too far on
  4091. * gzip, or other Deflate streams followed by arbitrary data. */
  4092. /* We need to be very careful here to NOT push back any bytes we definitely
  4093. * know we need to make forward progress, though, or we'll lock the caller up
  4094. * into an inf loop. */
  4095. if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) &&
  4096. (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS)) {
  4097. while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) {
  4098. --pIn_buf_cur;
  4099. num_bits -= 8;
  4100. }
  4101. }
  4102. r->m_num_bits = num_bits;
  4103. r->m_bit_buf =
  4104. bit_buf & (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1);
  4105. r->m_dist = dist;
  4106. r->m_counter = counter;
  4107. r->m_num_extra = num_extra;
  4108. r->m_dist_from_out_buf_start = dist_from_out_buf_start;
  4109. *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
  4110. *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
  4111. if ((decomp_flags &
  4112. (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) &&
  4113. (status >= 0)) {
  4114. const mz_uint8 *ptr = pOut_buf_next;
  4115. size_t buf_len = *pOut_buf_size;
  4116. mz_uint32 i, s1 = r->m_check_adler32 & 0xffff,
  4117. s2 = r->m_check_adler32 >> 16;
  4118. size_t block_len = buf_len % 5552;
  4119. while (buf_len) {
  4120. for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
  4121. s1 += ptr[0], s2 += s1;
  4122. s1 += ptr[1], s2 += s1;
  4123. s1 += ptr[2], s2 += s1;
  4124. s1 += ptr[3], s2 += s1;
  4125. s1 += ptr[4], s2 += s1;
  4126. s1 += ptr[5], s2 += s1;
  4127. s1 += ptr[6], s2 += s1;
  4128. s1 += ptr[7], s2 += s1;
  4129. }
  4130. for (; i < block_len; ++i)
  4131. s1 += *ptr++, s2 += s1;
  4132. s1 %= 65521U, s2 %= 65521U;
  4133. buf_len -= block_len;
  4134. block_len = 5552;
  4135. }
  4136. r->m_check_adler32 = (s2 << 16) + s1;
  4137. if ((status == TINFL_STATUS_DONE) &&
  4138. (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) &&
  4139. (r->m_check_adler32 != r->m_z_adler32))
  4140. status = TINFL_STATUS_ADLER32_MISMATCH;
  4141. }
  4142. return status;
  4143. }
  4144. /* Higher level helper functions. */
  4145. void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len,
  4146. size_t *pOut_len, int flags) {
  4147. tinfl_decompressor decomp;
  4148. void *pBuf = NULL, *pNew_buf;
  4149. size_t src_buf_ofs = 0, out_buf_capacity = 0;
  4150. *pOut_len = 0;
  4151. tinfl_init(&decomp);
  4152. for (;;) {
  4153. size_t src_buf_size = src_buf_len - src_buf_ofs,
  4154. dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;
  4155. tinfl_status status = tinfl_decompress(
  4156. &decomp, (const mz_uint8 *)pSrc_buf + src_buf_ofs, &src_buf_size,
  4157. (mz_uint8 *)pBuf, pBuf ? (mz_uint8 *)pBuf + *pOut_len : NULL,
  4158. &dst_buf_size,
  4159. (flags & ~TINFL_FLAG_HAS_MORE_INPUT) |
  4160. TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
  4161. if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) {
  4162. MZ_FREE(pBuf);
  4163. *pOut_len = 0;
  4164. return NULL;
  4165. }
  4166. src_buf_ofs += src_buf_size;
  4167. *pOut_len += dst_buf_size;
  4168. if (status == TINFL_STATUS_DONE)
  4169. break;
  4170. new_out_buf_capacity = out_buf_capacity * 2;
  4171. if (new_out_buf_capacity < 128)
  4172. new_out_buf_capacity = 128;
  4173. pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity);
  4174. if (!pNew_buf) {
  4175. MZ_FREE(pBuf);
  4176. *pOut_len = 0;
  4177. return NULL;
  4178. }
  4179. pBuf = pNew_buf;
  4180. out_buf_capacity = new_out_buf_capacity;
  4181. }
  4182. return pBuf;
  4183. }
  4184. size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len,
  4185. const void *pSrc_buf, size_t src_buf_len,
  4186. int flags) {
  4187. tinfl_decompressor decomp;
  4188. tinfl_status status;
  4189. tinfl_init(&decomp);
  4190. status =
  4191. tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf, &src_buf_len,
  4192. (mz_uint8 *)pOut_buf, (mz_uint8 *)pOut_buf, &out_buf_len,
  4193. (flags & ~TINFL_FLAG_HAS_MORE_INPUT) |
  4194. TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
  4195. return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED
  4196. : out_buf_len;
  4197. }
  4198. int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size,
  4199. tinfl_put_buf_func_ptr pPut_buf_func,
  4200. void *pPut_buf_user, int flags) {
  4201. int result = 0;
  4202. tinfl_decompressor decomp;
  4203. mz_uint8 *pDict = (mz_uint8 *)MZ_MALLOC(TINFL_LZ_DICT_SIZE);
  4204. size_t in_buf_ofs = 0, dict_ofs = 0;
  4205. if (!pDict)
  4206. return TINFL_STATUS_FAILED;
  4207. tinfl_init(&decomp);
  4208. for (;;) {
  4209. size_t in_buf_size = *pIn_buf_size - in_buf_ofs,
  4210. dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs;
  4211. tinfl_status status =
  4212. tinfl_decompress(&decomp, (const mz_uint8 *)pIn_buf + in_buf_ofs,
  4213. &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size,
  4214. (flags & ~(TINFL_FLAG_HAS_MORE_INPUT |
  4215. TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)));
  4216. in_buf_ofs += in_buf_size;
  4217. if ((dst_buf_size) &&
  4218. (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user)))
  4219. break;
  4220. if (status != TINFL_STATUS_HAS_MORE_OUTPUT) {
  4221. result = (status == TINFL_STATUS_DONE);
  4222. break;
  4223. }
  4224. dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1);
  4225. }
  4226. MZ_FREE(pDict);
  4227. *pIn_buf_size = in_buf_ofs;
  4228. return result;
  4229. }
  4230. #ifndef MINIZ_NO_MALLOC
  4231. tinfl_decompressor *tinfl_decompressor_alloc() {
  4232. tinfl_decompressor *pDecomp =
  4233. (tinfl_decompressor *)MZ_MALLOC(sizeof(tinfl_decompressor));
  4234. if (pDecomp)
  4235. tinfl_init(pDecomp);
  4236. return pDecomp;
  4237. }
  4238. void tinfl_decompressor_free(tinfl_decompressor *pDecomp) { MZ_FREE(pDecomp); }
  4239. #endif
  4240. /**************************************************************************
  4241. *
  4242. * Copyright 2013-2014 RAD Game Tools and Valve Software
  4243. * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
  4244. * Copyright 2016 Martin Raiber
  4245. * All Rights Reserved.
  4246. *
  4247. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4248. * of this software and associated documentation files (the "Software"), to deal
  4249. * in the Software without restriction, including without limitation the rights
  4250. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  4251. * copies of the Software, and to permit persons to whom the Software is
  4252. * furnished to do so, subject to the following conditions:
  4253. *
  4254. * The above copyright notice and this permission notice shall be included in
  4255. * all copies or substantial portions of the Software.
  4256. *
  4257. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  4258. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  4259. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  4260. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  4261. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  4262. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  4263. * THE SOFTWARE.
  4264. *
  4265. **************************************************************************/
  4266. #ifndef MINIZ_NO_ARCHIVE_APIS
  4267. /* ------------------- .ZIP archive reading */
  4268. #ifdef MINIZ_NO_STDIO
  4269. #define MZ_FILE void *
  4270. #else
  4271. #include <sys/stat.h>
  4272. #if defined(_MSC_VER)
  4273. #include <windows.h>
  4274. #ifndef MINIZ_NO_TIME
  4275. #include <sys/utime.h>
  4276. #endif
  4277. static wchar_t *str2wstr(const char *str) {
  4278. size_t len = strlen(str) + 1;
  4279. wchar_t *wstr = (wchar_t *)malloc(len * sizeof(wchar_t));
  4280. MultiByteToWideChar(CP_UTF8, 0, str, (int)(len * sizeof(char)), wstr,
  4281. (int)len);
  4282. return wstr;
  4283. }
  4284. static FILE *mz_fopen(const char *pFilename, const char *pMode) {
  4285. FILE *pFile = NULL;
  4286. wchar_t *wFilename = str2wstr(pFilename);
  4287. wchar_t *wMode = str2wstr(pMode);
  4288. #ifdef ZIP_ENABLE_SHARABLE_FILE_OPEN
  4289. pFile = _wfopen(wFilename, wMode);
  4290. #else
  4291. _wfopen_s(&pFile, wFilename, wMode);
  4292. #endif
  4293. free(wFilename);
  4294. free(wMode);
  4295. return pFile;
  4296. }
  4297. static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) {
  4298. FILE *pFile = NULL;
  4299. int res = 0;
  4300. wchar_t *wPath = str2wstr(pPath);
  4301. wchar_t *wMode = str2wstr(pMode);
  4302. #ifdef ZIP_ENABLE_SHARABLE_FILE_OPEN
  4303. pFile = _wfreopen(wPath, wMode, pStream);
  4304. #else
  4305. res = _wfreopen_s(&pFile, wPath, wMode, pStream);
  4306. #endif
  4307. free(wPath);
  4308. free(wMode);
  4309. #ifndef ZIP_ENABLE_SHARABLE_FILE_OPEN
  4310. if (res) {
  4311. return NULL;
  4312. }
  4313. #endif
  4314. return pFile;
  4315. }
  4316. static int mz_stat(const char *pPath, struct _stat64 *buffer) {
  4317. wchar_t *wPath = str2wstr(pPath);
  4318. int res = _wstat64(wPath, buffer);
  4319. free(wPath);
  4320. return res;
  4321. }
  4322. static int mz_mkdir(const char *pDirname) {
  4323. wchar_t *wDirname = str2wstr(pDirname);
  4324. int res = _wmkdir(wDirname);
  4325. free(wDirname);
  4326. return res;
  4327. }
  4328. #define MZ_FOPEN(f, m) fopen(f, m)
  4329. #define MZ_FCLOSE fclose
  4330. #define MZ_FREAD fread
  4331. #define MZ_FWRITE fwrite
  4332. #define MZ_FTELL64 ftell
  4333. #define MZ_FSEEK64 fseek
  4334. #define MZ_FILE_STAT_STRUCT stat
  4335. #define MZ_FILE_STAT stat
  4336. #define MZ_FFLUSH fflush
  4337. #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
  4338. #define MZ_DELETE_FILE remove
  4339. #define MZ_MKDIR(d) _mkdir(d)
  4340. #elif defined(__MINGW32__) || defined(__MINGW64__)
  4341. #include <windows.h>
  4342. #ifndef MINIZ_NO_TIME
  4343. #include <sys/utime.h>
  4344. #endif
  4345. #define MZ_FOPEN(f, m) fopen(f, m)
  4346. #define MZ_FCLOSE fclose
  4347. #define MZ_FREAD fread
  4348. #define MZ_FWRITE fwrite
  4349. #define MZ_FTELL64 ftell
  4350. #define MZ_FSEEK64 fseek
  4351. #define MZ_FILE_STAT_STRUCT stat
  4352. #define MZ_FILE_STAT stat
  4353. #define MZ_FFLUSH fflush
  4354. #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
  4355. #define MZ_DELETE_FILE remove
  4356. #define MZ_MKDIR(d) _mkdir(d)
  4357. #elif defined(__TINYC__)
  4358. #ifndef MINIZ_NO_TIME
  4359. #include <sys/utime.h>
  4360. #endif
  4361. #define MZ_FOPEN(f, m) fopen(f, m)
  4362. #define MZ_FCLOSE fclose
  4363. #define MZ_FREAD fread
  4364. #define MZ_FWRITE fwrite
  4365. #define MZ_FTELL64 ftell
  4366. #define MZ_FSEEK64 fseek
  4367. #define MZ_FILE_STAT_STRUCT stat
  4368. #define MZ_FILE_STAT stat
  4369. #define MZ_FFLUSH fflush
  4370. #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
  4371. #define MZ_DELETE_FILE remove
  4372. #if defined(_WIN32) || defined(_WIN64)
  4373. #define MZ_MKDIR(d) _mkdir(d)
  4374. #else
  4375. #define MZ_MKDIR(d) mkdir(d, 0755)
  4376. #endif
  4377. #elif defined(__USE_LARGEFILE64) /* gcc, clang */
  4378. #ifndef MINIZ_NO_TIME
  4379. #include <utime.h>
  4380. #endif
  4381. #define MZ_FOPEN(f, m) fopen64(f, m)
  4382. #define MZ_FCLOSE fclose
  4383. #define MZ_FREAD fread
  4384. #define MZ_FWRITE fwrite
  4385. #define MZ_FTELL64 ftello64
  4386. #define MZ_FSEEK64 fseeko64
  4387. #define MZ_FILE_STAT_STRUCT stat64
  4388. #define MZ_FILE_STAT stat64
  4389. #define MZ_FFLUSH fflush
  4390. #define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
  4391. #define MZ_DELETE_FILE remove
  4392. #define MZ_MKDIR(d) mkdir(d, 0755)
  4393. #elif defined(__APPLE__)
  4394. #ifndef MINIZ_NO_TIME
  4395. #include <utime.h>
  4396. #endif
  4397. #define MZ_FOPEN(f, m) fopen(f, m)
  4398. #define MZ_FCLOSE fclose
  4399. #define MZ_FREAD fread
  4400. #define MZ_FWRITE fwrite
  4401. #define MZ_FTELL64 ftello
  4402. #define MZ_FSEEK64 fseeko
  4403. #define MZ_FILE_STAT_STRUCT stat
  4404. #define MZ_FILE_STAT stat
  4405. #define MZ_FFLUSH fflush
  4406. #define MZ_FREOPEN(p, m, s) freopen(p, m, s)
  4407. #define MZ_DELETE_FILE remove
  4408. #define MZ_MKDIR(d) mkdir(d, 0755)
  4409. #else
  4410. #pragma message( \
  4411. "Using fopen, ftello, fseeko, stat() etc. path for file I/O - this path may not support large files.")
  4412. #ifndef MINIZ_NO_TIME
  4413. #include <utime.h>
  4414. #endif
  4415. #define MZ_FOPEN(f, m) fopen(f, m)
  4416. #define MZ_FCLOSE fclose
  4417. #define MZ_FREAD fread
  4418. #define MZ_FWRITE fwrite
  4419. #ifdef __STRICT_ANSI__
  4420. #define MZ_FTELL64 ftell
  4421. #define MZ_FSEEK64 fseek
  4422. #else
  4423. #define MZ_FTELL64 ftello
  4424. #define MZ_FSEEK64 fseeko
  4425. #endif
  4426. #define MZ_FILE_STAT_STRUCT stat
  4427. #define MZ_FILE_STAT stat
  4428. #define MZ_FFLUSH fflush
  4429. #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
  4430. #define MZ_DELETE_FILE remove
  4431. #define MZ_MKDIR(d) mkdir(d, 0755)
  4432. #endif /* #ifdef _MSC_VER */
  4433. #endif /* #ifdef MINIZ_NO_STDIO */
  4434. #ifndef CHMOD
  4435. // Upon successful completion, a value of 0 is returned.
  4436. // Otherwise, a value of -1 is returned and errno is set to indicate the error.
  4437. // int chmod(const char *path, mode_t mode);
  4438. #define CHMOD(f, m) chmod(f, m)
  4439. #endif
  4440. #define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
  4441. /* Various ZIP archive enums. To completely avoid cross platform compiler
  4442. * alignment and platform endian issues, miniz.c doesn't use structs for any of
  4443. * this stuff. */
  4444. enum {
  4445. /* ZIP archive identifiers and record sizes */
  4446. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50,
  4447. MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50,
  4448. MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50,
  4449. MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30,
  4450. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46,
  4451. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22,
  4452. /* ZIP64 archive identifier and record sizes */
  4453. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06064b50,
  4454. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG = 0x07064b50,
  4455. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE = 56,
  4456. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE = 20,
  4457. MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID = 0x0001,
  4458. MZ_ZIP_DATA_DESCRIPTOR_ID = 0x08074b50,
  4459. MZ_ZIP_DATA_DESCRIPTER_SIZE64 = 24,
  4460. MZ_ZIP_DATA_DESCRIPTER_SIZE32 = 16,
  4461. /* Central directory header record offsets */
  4462. MZ_ZIP_CDH_SIG_OFS = 0,
  4463. MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4,
  4464. MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6,
  4465. MZ_ZIP_CDH_BIT_FLAG_OFS = 8,
  4466. MZ_ZIP_CDH_METHOD_OFS = 10,
  4467. MZ_ZIP_CDH_FILE_TIME_OFS = 12,
  4468. MZ_ZIP_CDH_FILE_DATE_OFS = 14,
  4469. MZ_ZIP_CDH_CRC32_OFS = 16,
  4470. MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20,
  4471. MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24,
  4472. MZ_ZIP_CDH_FILENAME_LEN_OFS = 28,
  4473. MZ_ZIP_CDH_EXTRA_LEN_OFS = 30,
  4474. MZ_ZIP_CDH_COMMENT_LEN_OFS = 32,
  4475. MZ_ZIP_CDH_DISK_START_OFS = 34,
  4476. MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36,
  4477. MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38,
  4478. MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42,
  4479. /* Local directory header offsets */
  4480. MZ_ZIP_LDH_SIG_OFS = 0,
  4481. MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4,
  4482. MZ_ZIP_LDH_BIT_FLAG_OFS = 6,
  4483. MZ_ZIP_LDH_METHOD_OFS = 8,
  4484. MZ_ZIP_LDH_FILE_TIME_OFS = 10,
  4485. MZ_ZIP_LDH_FILE_DATE_OFS = 12,
  4486. MZ_ZIP_LDH_CRC32_OFS = 14,
  4487. MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18,
  4488. MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22,
  4489. MZ_ZIP_LDH_FILENAME_LEN_OFS = 26,
  4490. MZ_ZIP_LDH_EXTRA_LEN_OFS = 28,
  4491. MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR = 1 << 3,
  4492. /* End of central directory offsets */
  4493. MZ_ZIP_ECDH_SIG_OFS = 0,
  4494. MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4,
  4495. MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6,
  4496. MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8,
  4497. MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10,
  4498. MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12,
  4499. MZ_ZIP_ECDH_CDIR_OFS_OFS = 16,
  4500. MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20,
  4501. /* ZIP64 End of central directory locator offsets */
  4502. MZ_ZIP64_ECDL_SIG_OFS = 0, /* 4 bytes */
  4503. MZ_ZIP64_ECDL_NUM_DISK_CDIR_OFS = 4, /* 4 bytes */
  4504. MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS = 8, /* 8 bytes */
  4505. MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS = 16, /* 4 bytes */
  4506. /* ZIP64 End of central directory header offsets */
  4507. MZ_ZIP64_ECDH_SIG_OFS = 0, /* 4 bytes */
  4508. MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS = 4, /* 8 bytes */
  4509. MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS = 12, /* 2 bytes */
  4510. MZ_ZIP64_ECDH_VERSION_NEEDED_OFS = 14, /* 2 bytes */
  4511. MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS = 16, /* 4 bytes */
  4512. MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS = 20, /* 4 bytes */
  4513. MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 24, /* 8 bytes */
  4514. MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS = 32, /* 8 bytes */
  4515. MZ_ZIP64_ECDH_CDIR_SIZE_OFS = 40, /* 8 bytes */
  4516. MZ_ZIP64_ECDH_CDIR_OFS_OFS = 48, /* 8 bytes */
  4517. MZ_ZIP_VERSION_MADE_BY_DOS_FILESYSTEM_ID = 0,
  4518. MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG = 0x10,
  4519. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED = 1,
  4520. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG = 32,
  4521. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION = 64,
  4522. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED = 8192,
  4523. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 = 1 << 11
  4524. };
  4525. typedef struct {
  4526. void *m_p;
  4527. size_t m_size, m_capacity;
  4528. mz_uint m_element_size;
  4529. } mz_zip_array;
  4530. struct mz_zip_internal_state_tag {
  4531. mz_zip_array m_central_dir;
  4532. mz_zip_array m_central_dir_offsets;
  4533. mz_zip_array m_sorted_central_dir_offsets;
  4534. /* The flags passed in when the archive is initially opened. */
  4535. uint32_t m_init_flags;
  4536. /* MZ_TRUE if the archive has a zip64 end of central directory headers, etc.
  4537. */
  4538. mz_bool m_zip64;
  4539. /* MZ_TRUE if we found zip64 extended info in the central directory (m_zip64
  4540. * will also be slammed to true too, even if we didn't find a zip64 end of
  4541. * central dir header, etc.) */
  4542. mz_bool m_zip64_has_extended_info_fields;
  4543. /* These fields are used by the file, FILE, memory, and memory/heap read/write
  4544. * helpers. */
  4545. MZ_FILE *m_pFile;
  4546. mz_uint64 m_file_archive_start_ofs;
  4547. void *m_pMem;
  4548. size_t m_mem_size;
  4549. size_t m_mem_capacity;
  4550. };
  4551. #define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) \
  4552. (array_ptr)->m_element_size = element_size
  4553. #if defined(DEBUG) || defined(_DEBUG)
  4554. static MZ_FORCEINLINE mz_uint
  4555. mz_zip_array_range_check(const mz_zip_array *pArray, mz_uint index) {
  4556. MZ_ASSERT(index < pArray->m_size);
  4557. return index;
  4558. }
  4559. #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) \
  4560. ((element_type *)((array_ptr) \
  4561. ->m_p))[mz_zip_array_range_check(array_ptr, index)]
  4562. #else
  4563. #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) \
  4564. ((element_type *)((array_ptr)->m_p))[index]
  4565. #endif
  4566. static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array *pArray,
  4567. mz_uint32 element_size) {
  4568. memset(pArray, 0, sizeof(mz_zip_array));
  4569. pArray->m_element_size = element_size;
  4570. }
  4571. static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip,
  4572. mz_zip_array *pArray) {
  4573. pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p);
  4574. memset(pArray, 0, sizeof(mz_zip_array));
  4575. }
  4576. static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip,
  4577. mz_zip_array *pArray,
  4578. size_t min_new_capacity,
  4579. mz_uint growing) {
  4580. void *pNew_p;
  4581. size_t new_capacity = min_new_capacity;
  4582. MZ_ASSERT(pArray->m_element_size);
  4583. if (pArray->m_capacity >= min_new_capacity)
  4584. return MZ_TRUE;
  4585. if (growing) {
  4586. new_capacity = MZ_MAX(1, pArray->m_capacity);
  4587. while (new_capacity < min_new_capacity)
  4588. new_capacity *= 2;
  4589. }
  4590. if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p,
  4591. pArray->m_element_size, new_capacity)))
  4592. return MZ_FALSE;
  4593. pArray->m_p = pNew_p;
  4594. pArray->m_capacity = new_capacity;
  4595. return MZ_TRUE;
  4596. }
  4597. static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip,
  4598. mz_zip_array *pArray,
  4599. size_t new_capacity,
  4600. mz_uint growing) {
  4601. if (new_capacity > pArray->m_capacity) {
  4602. if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing))
  4603. return MZ_FALSE;
  4604. }
  4605. return MZ_TRUE;
  4606. }
  4607. static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip,
  4608. mz_zip_array *pArray,
  4609. size_t new_size,
  4610. mz_uint growing) {
  4611. if (new_size > pArray->m_capacity) {
  4612. if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing))
  4613. return MZ_FALSE;
  4614. }
  4615. pArray->m_size = new_size;
  4616. return MZ_TRUE;
  4617. }
  4618. static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip,
  4619. mz_zip_array *pArray,
  4620. size_t n) {
  4621. return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE);
  4622. }
  4623. static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip,
  4624. mz_zip_array *pArray,
  4625. const void *pElements,
  4626. size_t n) {
  4627. size_t orig_size = pArray->m_size;
  4628. if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE))
  4629. return MZ_FALSE;
  4630. if (n > 0)
  4631. memcpy((mz_uint8 *)pArray->m_p + orig_size * pArray->m_element_size,
  4632. pElements, n * pArray->m_element_size);
  4633. return MZ_TRUE;
  4634. }
  4635. #ifndef MINIZ_NO_TIME
  4636. static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date) {
  4637. struct tm tm;
  4638. memset(&tm, 0, sizeof(tm));
  4639. tm.tm_isdst = -1;
  4640. tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900;
  4641. tm.tm_mon = ((dos_date >> 5) & 15) - 1;
  4642. tm.tm_mday = dos_date & 31;
  4643. tm.tm_hour = (dos_time >> 11) & 31;
  4644. tm.tm_min = (dos_time >> 5) & 63;
  4645. tm.tm_sec = (dos_time << 1) & 62;
  4646. return mktime(&tm);
  4647. }
  4648. #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
  4649. static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16 *pDOS_time,
  4650. mz_uint16 *pDOS_date) {
  4651. #ifdef _MSC_VER
  4652. struct tm tm_struct;
  4653. struct tm *tm = &tm_struct;
  4654. errno_t err = localtime_s(tm, &time);
  4655. if (err) {
  4656. *pDOS_date = 0;
  4657. *pDOS_time = 0;
  4658. return;
  4659. }
  4660. #else
  4661. struct tm *tm = localtime(&time);
  4662. #endif /* #ifdef _MSC_VER */
  4663. *pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) +
  4664. ((tm->tm_sec) >> 1));
  4665. *pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) +
  4666. ((tm->tm_mon + 1) << 5) + tm->tm_mday);
  4667. }
  4668. #endif /* MINIZ_NO_ARCHIVE_WRITING_APIS */
  4669. #ifndef MINIZ_NO_STDIO
  4670. #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
  4671. static mz_bool mz_zip_get_file_modified_time(const char *pFilename,
  4672. MZ_TIME_T *pTime) {
  4673. struct MZ_FILE_STAT_STRUCT file_stat;
  4674. /* On Linux with x86 glibc, this call will fail on large files (I think >=
  4675. * 0x80000000 bytes) unless you compiled with _LARGEFILE64_SOURCE. Argh. */
  4676. if (MZ_FILE_STAT(pFilename, &file_stat) != 0)
  4677. return MZ_FALSE;
  4678. *pTime = file_stat.st_mtime;
  4679. return MZ_TRUE;
  4680. }
  4681. #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS*/
  4682. static mz_bool mz_zip_set_file_times(const char *pFilename,
  4683. MZ_TIME_T access_time,
  4684. MZ_TIME_T modified_time) {
  4685. struct utimbuf t;
  4686. memset(&t, 0, sizeof(t));
  4687. t.actime = access_time;
  4688. t.modtime = modified_time;
  4689. return !utime(pFilename, &t);
  4690. }
  4691. #endif /* #ifndef MINIZ_NO_STDIO */
  4692. #endif /* #ifndef MINIZ_NO_TIME */
  4693. static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive *pZip,
  4694. mz_zip_error err_num) {
  4695. if (pZip)
  4696. pZip->m_last_error = err_num;
  4697. return MZ_FALSE;
  4698. }
  4699. static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip,
  4700. mz_uint flags) {
  4701. (void)flags;
  4702. if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
  4703. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  4704. if (!pZip->m_pAlloc)
  4705. pZip->m_pAlloc = miniz_def_alloc_func;
  4706. if (!pZip->m_pFree)
  4707. pZip->m_pFree = miniz_def_free_func;
  4708. if (!pZip->m_pRealloc)
  4709. pZip->m_pRealloc = miniz_def_realloc_func;
  4710. pZip->m_archive_size = 0;
  4711. pZip->m_central_directory_file_ofs = 0;
  4712. pZip->m_total_files = 0;
  4713. pZip->m_last_error = MZ_ZIP_NO_ERROR;
  4714. if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(
  4715. pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
  4716. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  4717. memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
  4718. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir,
  4719. sizeof(mz_uint8));
  4720. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets,
  4721. sizeof(mz_uint32));
  4722. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets,
  4723. sizeof(mz_uint32));
  4724. pZip->m_pState->m_init_flags = flags;
  4725. pZip->m_pState->m_zip64 = MZ_FALSE;
  4726. pZip->m_pState->m_zip64_has_extended_info_fields = MZ_FALSE;
  4727. pZip->m_zip_mode = MZ_ZIP_MODE_READING;
  4728. return MZ_TRUE;
  4729. }
  4730. static MZ_FORCEINLINE mz_bool
  4731. mz_zip_reader_filename_less(const mz_zip_array *pCentral_dir_array,
  4732. const mz_zip_array *pCentral_dir_offsets,
  4733. mz_uint l_index, mz_uint r_index) {
  4734. const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(
  4735. pCentral_dir_array, mz_uint8,
  4736. MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32,
  4737. l_index)),
  4738. *pE;
  4739. const mz_uint8 *pR = &MZ_ZIP_ARRAY_ELEMENT(
  4740. pCentral_dir_array, mz_uint8,
  4741. MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index));
  4742. mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS),
  4743. r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  4744. mz_uint8 l = 0, r = 0;
  4745. pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  4746. pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  4747. pE = pL + MZ_MIN(l_len, r_len);
  4748. while (pL < pE) {
  4749. if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
  4750. break;
  4751. pL++;
  4752. pR++;
  4753. }
  4754. return (pL == pE) ? (l_len < r_len) : (l < r);
  4755. }
  4756. #define MZ_SWAP_UINT32(a, b) \
  4757. do { \
  4758. mz_uint32 t = a; \
  4759. a = b; \
  4760. b = t; \
  4761. } \
  4762. MZ_MACRO_END
  4763. /* Heap sort of lowercased filenames, used to help accelerate plain central
  4764. * directory searches by mz_zip_reader_locate_file(). (Could also use qsort(),
  4765. * but it could allocate memory.) */
  4766. static void
  4767. mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip) {
  4768. mz_zip_internal_state *pState = pZip->m_pState;
  4769. const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;
  4770. const mz_zip_array *pCentral_dir = &pState->m_central_dir;
  4771. mz_uint32 *pIndices;
  4772. mz_uint32 start, end;
  4773. const mz_uint32 size = pZip->m_total_files;
  4774. if (size <= 1U)
  4775. return;
  4776. pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets,
  4777. mz_uint32, 0);
  4778. start = (size - 2U) >> 1U;
  4779. for (;;) {
  4780. mz_uint64 child, root = start;
  4781. for (;;) {
  4782. if ((child = (root << 1U) + 1U) >= size)
  4783. break;
  4784. child += (((child + 1U) < size) &&
  4785. (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets,
  4786. pIndices[child],
  4787. pIndices[child + 1U])));
  4788. if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets,
  4789. pIndices[root], pIndices[child]))
  4790. break;
  4791. MZ_SWAP_UINT32(pIndices[root], pIndices[child]);
  4792. root = child;
  4793. }
  4794. if (!start)
  4795. break;
  4796. start--;
  4797. }
  4798. end = size - 1;
  4799. while (end > 0) {
  4800. mz_uint64 child, root = 0;
  4801. MZ_SWAP_UINT32(pIndices[end], pIndices[0]);
  4802. for (;;) {
  4803. if ((child = (root << 1U) + 1U) >= end)
  4804. break;
  4805. child +=
  4806. (((child + 1U) < end) &&
  4807. mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets,
  4808. pIndices[child], pIndices[child + 1U]));
  4809. if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets,
  4810. pIndices[root], pIndices[child]))
  4811. break;
  4812. MZ_SWAP_UINT32(pIndices[root], pIndices[child]);
  4813. root = child;
  4814. }
  4815. end--;
  4816. }
  4817. }
  4818. static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive *pZip,
  4819. mz_uint32 record_sig,
  4820. mz_uint32 record_size,
  4821. mz_int64 *pOfs) {
  4822. mz_int64 cur_file_ofs;
  4823. mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];
  4824. mz_uint8 *pBuf = (mz_uint8 *)buf_u32;
  4825. /* Basic sanity checks - reject files which are too small */
  4826. if (pZip->m_archive_size < record_size)
  4827. return MZ_FALSE;
  4828. /* Find the record by scanning the file from the end towards the beginning. */
  4829. cur_file_ofs =
  4830. MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0);
  4831. for (;;) {
  4832. int i,
  4833. n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs);
  4834. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n)
  4835. return MZ_FALSE;
  4836. for (i = n - 4; i >= 0; --i) {
  4837. mz_uint s = MZ_READ_LE32(pBuf + i);
  4838. if (s == record_sig) {
  4839. if ((pZip->m_archive_size - (cur_file_ofs + i)) >= record_size)
  4840. break;
  4841. }
  4842. }
  4843. if (i >= 0) {
  4844. cur_file_ofs += i;
  4845. break;
  4846. }
  4847. /* Give up if we've searched the entire file, or we've gone back "too far"
  4848. * (~64kb) */
  4849. if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >=
  4850. (MZ_UINT16_MAX + record_size)))
  4851. return MZ_FALSE;
  4852. cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0);
  4853. }
  4854. *pOfs = cur_file_ofs;
  4855. return MZ_TRUE;
  4856. }
  4857. static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip,
  4858. mz_uint flags) {
  4859. mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0,
  4860. cdir_disk_index = 0;
  4861. mz_uint64 cdir_ofs = 0;
  4862. mz_int64 cur_file_ofs = 0;
  4863. const mz_uint8 *p;
  4864. mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];
  4865. mz_uint8 *pBuf = (mz_uint8 *)buf_u32;
  4866. mz_bool sort_central_dir =
  4867. ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0);
  4868. mz_uint32 zip64_end_of_central_dir_locator_u32
  4869. [(MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + sizeof(mz_uint32) - 1) /
  4870. sizeof(mz_uint32)];
  4871. mz_uint8 *pZip64_locator = (mz_uint8 *)zip64_end_of_central_dir_locator_u32;
  4872. mz_uint32 zip64_end_of_central_dir_header_u32
  4873. [(MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) /
  4874. sizeof(mz_uint32)];
  4875. mz_uint8 *pZip64_end_of_central_dir =
  4876. (mz_uint8 *)zip64_end_of_central_dir_header_u32;
  4877. mz_uint64 zip64_end_of_central_dir_ofs = 0;
  4878. /* Basic sanity checks - reject files which are too small, and check the first
  4879. * 4 bytes of the file to make sure a local header is there. */
  4880. if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  4881. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  4882. if (!mz_zip_reader_locate_header_sig(
  4883. pZip, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG,
  4884. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE, &cur_file_ofs))
  4885. return mz_zip_set_error(pZip, MZ_ZIP_FAILED_FINDING_CENTRAL_DIR);
  4886. /* Read and verify the end of central directory record. */
  4887. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf,
  4888. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) !=
  4889. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  4890. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  4891. if (MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) !=
  4892. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG)
  4893. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  4894. if (cur_file_ofs >= (MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE +
  4895. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)) {
  4896. if (pZip->m_pRead(pZip->m_pIO_opaque,
  4897. cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE,
  4898. pZip64_locator,
  4899. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) ==
  4900. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) {
  4901. if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) ==
  4902. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG) {
  4903. zip64_end_of_central_dir_ofs = MZ_READ_LE64(
  4904. pZip64_locator + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS);
  4905. if (zip64_end_of_central_dir_ofs >
  4906. (pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE))
  4907. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  4908. if (pZip->m_pRead(pZip->m_pIO_opaque, zip64_end_of_central_dir_ofs,
  4909. pZip64_end_of_central_dir,
  4910. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) ==
  4911. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) {
  4912. if (MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIG_OFS) ==
  4913. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG) {
  4914. pZip->m_pState->m_zip64 = MZ_TRUE;
  4915. }
  4916. }
  4917. }
  4918. }
  4919. }
  4920. pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS);
  4921. cdir_entries_on_this_disk =
  4922. MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);
  4923. num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS);
  4924. cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS);
  4925. cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS);
  4926. cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS);
  4927. if (pZip->m_pState->m_zip64) {
  4928. mz_uint32 zip64_total_num_of_disks =
  4929. MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS);
  4930. mz_uint64 zip64_cdir_total_entries = MZ_READ_LE64(
  4931. pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS);
  4932. mz_uint64 zip64_cdir_total_entries_on_this_disk = MZ_READ_LE64(
  4933. pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);
  4934. mz_uint64 zip64_size_of_end_of_central_dir_record = MZ_READ_LE64(
  4935. pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS);
  4936. mz_uint64 zip64_size_of_central_directory =
  4937. MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_SIZE_OFS);
  4938. if (zip64_size_of_end_of_central_dir_record <
  4939. (MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - 12))
  4940. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  4941. if (zip64_total_num_of_disks != 1U)
  4942. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
  4943. /* Check for miniz's practical limits */
  4944. if (zip64_cdir_total_entries > MZ_UINT32_MAX)
  4945. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  4946. pZip->m_total_files = (mz_uint32)zip64_cdir_total_entries;
  4947. if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX)
  4948. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  4949. cdir_entries_on_this_disk =
  4950. (mz_uint32)zip64_cdir_total_entries_on_this_disk;
  4951. /* Check for miniz's current practical limits (sorry, this should be enough
  4952. * for millions of files) */
  4953. if (zip64_size_of_central_directory > MZ_UINT32_MAX)
  4954. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  4955. cdir_size = (mz_uint32)zip64_size_of_central_directory;
  4956. num_this_disk = MZ_READ_LE32(pZip64_end_of_central_dir +
  4957. MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS);
  4958. cdir_disk_index = MZ_READ_LE32(pZip64_end_of_central_dir +
  4959. MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS);
  4960. cdir_ofs =
  4961. MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_OFS_OFS);
  4962. }
  4963. if (pZip->m_total_files != cdir_entries_on_this_disk)
  4964. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
  4965. if (((num_this_disk | cdir_disk_index) != 0) &&
  4966. ((num_this_disk != 1) || (cdir_disk_index != 1)))
  4967. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
  4968. if (cdir_size < pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)
  4969. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  4970. if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size)
  4971. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  4972. pZip->m_central_directory_file_ofs = cdir_ofs;
  4973. if (pZip->m_total_files) {
  4974. mz_uint i, n;
  4975. /* Read the entire central directory into a heap block, and allocate another
  4976. * heap block to hold the unsorted central dir file record offsets, and
  4977. * possibly another to hold the sorted indices. */
  4978. if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size,
  4979. MZ_FALSE)) ||
  4980. (!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets,
  4981. pZip->m_total_files, MZ_FALSE)))
  4982. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  4983. if (sort_central_dir) {
  4984. if (!mz_zip_array_resize(pZip,
  4985. &pZip->m_pState->m_sorted_central_dir_offsets,
  4986. pZip->m_total_files, MZ_FALSE))
  4987. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  4988. }
  4989. if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs,
  4990. pZip->m_pState->m_central_dir.m_p,
  4991. cdir_size) != cdir_size)
  4992. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  4993. /* Now create an index into the central directory file records, do some
  4994. * basic sanity checking on each record */
  4995. p = (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p;
  4996. for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i) {
  4997. mz_uint total_header_size, disk_index, bit_flags, filename_size,
  4998. ext_data_size;
  4999. mz_uint64 comp_size, decomp_size, local_header_ofs;
  5000. if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) ||
  5001. (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG))
  5002. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5003. MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32,
  5004. i) =
  5005. (mz_uint32)(p - (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p);
  5006. if (sort_central_dir)
  5007. MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets,
  5008. mz_uint32, i) = i;
  5009. comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
  5010. decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
  5011. local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
  5012. filename_size = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  5013. ext_data_size = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);
  5014. if ((!pZip->m_pState->m_zip64_has_extended_info_fields) &&
  5015. (ext_data_size) &&
  5016. (MZ_MAX(MZ_MAX(comp_size, decomp_size), local_header_ofs) ==
  5017. MZ_UINT32_MAX)) {
  5018. /* Attempt to find zip64 extended information field in the entry's extra
  5019. * data */
  5020. mz_uint32 extra_size_remaining = ext_data_size;
  5021. if (extra_size_remaining) {
  5022. const mz_uint8 *pExtra_data;
  5023. void *buf = NULL;
  5024. if (MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + ext_data_size >
  5025. n) {
  5026. buf = MZ_MALLOC(ext_data_size);
  5027. if (buf == NULL)
  5028. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  5029. if (pZip->m_pRead(pZip->m_pIO_opaque,
  5030. cdir_ofs + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
  5031. filename_size,
  5032. buf, ext_data_size) != ext_data_size) {
  5033. MZ_FREE(buf);
  5034. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  5035. }
  5036. pExtra_data = (mz_uint8 *)buf;
  5037. } else {
  5038. pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size;
  5039. }
  5040. do {
  5041. mz_uint32 field_id;
  5042. mz_uint32 field_data_size;
  5043. if (extra_size_remaining < (sizeof(mz_uint16) * 2)) {
  5044. MZ_FREE(buf);
  5045. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5046. }
  5047. field_id = MZ_READ_LE16(pExtra_data);
  5048. field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  5049. if ((field_data_size + sizeof(mz_uint16) * 2) >
  5050. extra_size_remaining) {
  5051. MZ_FREE(buf);
  5052. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5053. }
  5054. if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) {
  5055. /* Ok, the archive didn't have any zip64 headers but it uses a
  5056. * zip64 extended information field so mark it as zip64 anyway
  5057. * (this can occur with infozip's zip util when it reads
  5058. * compresses files from stdin). */
  5059. pZip->m_pState->m_zip64 = MZ_TRUE;
  5060. pZip->m_pState->m_zip64_has_extended_info_fields = MZ_TRUE;
  5061. break;
  5062. }
  5063. pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;
  5064. extra_size_remaining =
  5065. extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;
  5066. } while (extra_size_remaining);
  5067. MZ_FREE(buf);
  5068. }
  5069. }
  5070. /* I've seen archives that aren't marked as zip64 that uses zip64 ext
  5071. * data, argh */
  5072. if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX)) {
  5073. if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) &&
  5074. (decomp_size != comp_size)) ||
  5075. (decomp_size && !comp_size))
  5076. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5077. }
  5078. disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS);
  5079. if ((disk_index == MZ_UINT16_MAX) ||
  5080. ((disk_index != num_this_disk) && (disk_index != 1)))
  5081. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
  5082. if (comp_size != MZ_UINT32_MAX) {
  5083. if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) +
  5084. MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size)
  5085. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5086. }
  5087. bit_flags = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
  5088. if (bit_flags & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED)
  5089. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  5090. if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
  5091. MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) +
  5092. MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) +
  5093. MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) >
  5094. n)
  5095. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5096. n -= total_header_size;
  5097. p += total_header_size;
  5098. }
  5099. }
  5100. if (sort_central_dir)
  5101. mz_zip_reader_sort_central_dir_offsets_by_filename(pZip);
  5102. return MZ_TRUE;
  5103. }
  5104. void mz_zip_zero_struct(mz_zip_archive *pZip) {
  5105. if (pZip)
  5106. MZ_CLEAR_OBJ(*pZip);
  5107. }
  5108. static mz_bool mz_zip_reader_end_internal(mz_zip_archive *pZip,
  5109. mz_bool set_last_error) {
  5110. mz_bool status = MZ_TRUE;
  5111. if (!pZip)
  5112. return MZ_FALSE;
  5113. if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) ||
  5114. (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) {
  5115. if (set_last_error)
  5116. pZip->m_last_error = MZ_ZIP_INVALID_PARAMETER;
  5117. return MZ_FALSE;
  5118. }
  5119. if (pZip->m_pState) {
  5120. mz_zip_internal_state *pState = pZip->m_pState;
  5121. pZip->m_pState = NULL;
  5122. mz_zip_array_clear(pZip, &pState->m_central_dir);
  5123. mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
  5124. mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
  5125. #ifndef MINIZ_NO_STDIO
  5126. if (pState->m_pFile) {
  5127. if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
  5128. if (MZ_FCLOSE(pState->m_pFile) == EOF) {
  5129. if (set_last_error)
  5130. pZip->m_last_error = MZ_ZIP_FILE_CLOSE_FAILED;
  5131. status = MZ_FALSE;
  5132. }
  5133. }
  5134. pState->m_pFile = NULL;
  5135. }
  5136. #endif /* #ifndef MINIZ_NO_STDIO */
  5137. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  5138. }
  5139. pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
  5140. return status;
  5141. }
  5142. mz_bool mz_zip_reader_end(mz_zip_archive *pZip) {
  5143. return mz_zip_reader_end_internal(pZip, MZ_TRUE);
  5144. }
  5145. mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size,
  5146. mz_uint flags) {
  5147. if ((!pZip) || (!pZip->m_pRead))
  5148. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5149. if (!mz_zip_reader_init_internal(pZip, flags))
  5150. return MZ_FALSE;
  5151. pZip->m_zip_type = MZ_ZIP_TYPE_USER;
  5152. pZip->m_archive_size = size;
  5153. if (!mz_zip_reader_read_central_dir(pZip, flags)) {
  5154. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  5155. return MZ_FALSE;
  5156. }
  5157. return MZ_TRUE;
  5158. }
  5159. static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs,
  5160. void *pBuf, size_t n) {
  5161. mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
  5162. size_t s = (file_ofs >= pZip->m_archive_size)
  5163. ? 0
  5164. : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n);
  5165. memcpy(pBuf, (const mz_uint8 *)pZip->m_pState->m_pMem + file_ofs, s);
  5166. return s;
  5167. }
  5168. mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem,
  5169. size_t size, mz_uint flags) {
  5170. if (!pMem)
  5171. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5172. if (size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  5173. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  5174. if (!mz_zip_reader_init_internal(pZip, flags))
  5175. return MZ_FALSE;
  5176. pZip->m_zip_type = MZ_ZIP_TYPE_MEMORY;
  5177. pZip->m_archive_size = size;
  5178. pZip->m_pRead = mz_zip_mem_read_func;
  5179. pZip->m_pIO_opaque = pZip;
  5180. pZip->m_pNeeds_keepalive = NULL;
  5181. #ifdef __cplusplus
  5182. pZip->m_pState->m_pMem = const_cast<void *>(pMem);
  5183. #else
  5184. pZip->m_pState->m_pMem = (void *)pMem;
  5185. #endif
  5186. pZip->m_pState->m_mem_size = size;
  5187. if (!mz_zip_reader_read_central_dir(pZip, flags)) {
  5188. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  5189. return MZ_FALSE;
  5190. }
  5191. return MZ_TRUE;
  5192. }
  5193. #ifndef MINIZ_NO_STDIO
  5194. static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs,
  5195. void *pBuf, size_t n) {
  5196. mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
  5197. mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
  5198. file_ofs += pZip->m_pState->m_file_archive_start_ofs;
  5199. if (((mz_int64)file_ofs < 0) ||
  5200. (((cur_ofs != (mz_int64)file_ofs)) &&
  5201. (MZ_FSEEK64(pZip->m_pState->m_pFile, (long)file_ofs, SEEK_SET))))
  5202. return 0;
  5203. return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile);
  5204. }
  5205. mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename,
  5206. mz_uint32 flags) {
  5207. return mz_zip_reader_init_file_v2(pZip, pFilename, flags, 0, 0);
  5208. }
  5209. mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip, const char *pFilename,
  5210. mz_uint flags, mz_uint64 file_start_ofs,
  5211. mz_uint64 archive_size) {
  5212. mz_uint64 file_size;
  5213. MZ_FILE *pFile;
  5214. if ((!pZip) || (!pFilename) ||
  5215. ((archive_size) &&
  5216. (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)))
  5217. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5218. pFile = MZ_FOPEN(pFilename, "rb");
  5219. if (!pFile)
  5220. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  5221. file_size = archive_size;
  5222. if (!file_size) {
  5223. if (MZ_FSEEK64(pFile, 0, SEEK_END)) {
  5224. MZ_FCLOSE(pFile);
  5225. return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
  5226. }
  5227. file_size = MZ_FTELL64(pFile);
  5228. }
  5229. /* TODO: Better sanity check archive_size and the # of actual remaining bytes
  5230. */
  5231. if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) {
  5232. MZ_FCLOSE(pFile);
  5233. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  5234. }
  5235. if (!mz_zip_reader_init_internal(pZip, flags)) {
  5236. MZ_FCLOSE(pFile);
  5237. return MZ_FALSE;
  5238. }
  5239. pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
  5240. pZip->m_pRead = mz_zip_file_read_func;
  5241. pZip->m_pIO_opaque = pZip;
  5242. pZip->m_pState->m_pFile = pFile;
  5243. pZip->m_archive_size = file_size;
  5244. pZip->m_pState->m_file_archive_start_ofs = file_start_ofs;
  5245. if (!mz_zip_reader_read_central_dir(pZip, flags)) {
  5246. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  5247. return MZ_FALSE;
  5248. }
  5249. return MZ_TRUE;
  5250. }
  5251. mz_bool mz_zip_reader_init_file_v2_rpb(mz_zip_archive *pZip,
  5252. const char *pFilename, mz_uint flags,
  5253. mz_uint64 file_start_ofs,
  5254. mz_uint64 archive_size) {
  5255. mz_uint64 file_size;
  5256. MZ_FILE *pFile;
  5257. if ((!pZip) || (!pFilename) ||
  5258. ((archive_size) &&
  5259. (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)))
  5260. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5261. pFile = MZ_FOPEN(pFilename, "r+b");
  5262. if (!pFile)
  5263. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  5264. file_size = archive_size;
  5265. if (!file_size) {
  5266. if (MZ_FSEEK64(pFile, 0, SEEK_END)) {
  5267. MZ_FCLOSE(pFile);
  5268. return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
  5269. }
  5270. file_size = MZ_FTELL64(pFile);
  5271. }
  5272. /* TODO: Better sanity check archive_size and the # of actual remaining bytes
  5273. */
  5274. if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) {
  5275. MZ_FCLOSE(pFile);
  5276. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  5277. }
  5278. if (!mz_zip_reader_init_internal(pZip, flags)) {
  5279. MZ_FCLOSE(pFile);
  5280. return MZ_FALSE;
  5281. }
  5282. pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
  5283. pZip->m_pRead = mz_zip_file_read_func;
  5284. pZip->m_pIO_opaque = pZip;
  5285. pZip->m_pState->m_pFile = pFile;
  5286. pZip->m_archive_size = file_size;
  5287. pZip->m_pState->m_file_archive_start_ofs = file_start_ofs;
  5288. if (!mz_zip_reader_read_central_dir(pZip, flags)) {
  5289. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  5290. return MZ_FALSE;
  5291. }
  5292. return MZ_TRUE;
  5293. }
  5294. mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile,
  5295. mz_uint64 archive_size, mz_uint flags) {
  5296. mz_uint64 cur_file_ofs;
  5297. if ((!pZip) || (!pFile))
  5298. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  5299. cur_file_ofs = MZ_FTELL64(pFile);
  5300. if (!archive_size) {
  5301. if (MZ_FSEEK64(pFile, 0, SEEK_END))
  5302. return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
  5303. archive_size = MZ_FTELL64(pFile) - cur_file_ofs;
  5304. if (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  5305. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  5306. }
  5307. if (!mz_zip_reader_init_internal(pZip, flags))
  5308. return MZ_FALSE;
  5309. pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
  5310. pZip->m_pRead = mz_zip_file_read_func;
  5311. pZip->m_pIO_opaque = pZip;
  5312. pZip->m_pState->m_pFile = pFile;
  5313. pZip->m_archive_size = archive_size;
  5314. pZip->m_pState->m_file_archive_start_ofs = cur_file_ofs;
  5315. if (!mz_zip_reader_read_central_dir(pZip, flags)) {
  5316. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  5317. return MZ_FALSE;
  5318. }
  5319. return MZ_TRUE;
  5320. }
  5321. #endif /* #ifndef MINIZ_NO_STDIO */
  5322. static MZ_FORCEINLINE const mz_uint8 *mz_zip_get_cdh(mz_zip_archive *pZip,
  5323. mz_uint file_index) {
  5324. if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files))
  5325. return NULL;
  5326. return &MZ_ZIP_ARRAY_ELEMENT(
  5327. &pZip->m_pState->m_central_dir, mz_uint8,
  5328. MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32,
  5329. file_index));
  5330. }
  5331. mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip,
  5332. mz_uint file_index) {
  5333. mz_uint m_bit_flag;
  5334. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  5335. if (!p) {
  5336. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5337. return MZ_FALSE;
  5338. }
  5339. m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
  5340. return (m_bit_flag &
  5341. (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED |
  5342. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) != 0;
  5343. }
  5344. mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip,
  5345. mz_uint file_index) {
  5346. mz_uint bit_flag;
  5347. mz_uint method;
  5348. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  5349. if (!p) {
  5350. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5351. return MZ_FALSE;
  5352. }
  5353. method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);
  5354. bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
  5355. if ((method != 0) && (method != MZ_DEFLATED)) {
  5356. mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  5357. return MZ_FALSE;
  5358. }
  5359. if (bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED |
  5360. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) {
  5361. mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  5362. return MZ_FALSE;
  5363. }
  5364. if (bit_flag & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG) {
  5365. mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
  5366. return MZ_FALSE;
  5367. }
  5368. return MZ_TRUE;
  5369. }
  5370. mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip,
  5371. mz_uint file_index) {
  5372. mz_uint filename_len, attribute_mapping_id, external_attr;
  5373. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  5374. if (!p) {
  5375. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5376. return MZ_FALSE;
  5377. }
  5378. filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  5379. if (filename_len) {
  5380. if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/')
  5381. return MZ_TRUE;
  5382. }
  5383. /* Bugfix: This code was also checking if the internal attribute was non-zero,
  5384. * which wasn't correct. */
  5385. /* Most/all zip writers (hopefully) set DOS file/directory attributes in the
  5386. * low 16-bits, so check for the DOS directory flag and ignore the source OS
  5387. * ID in the created by field. */
  5388. /* FIXME: Remove this check? Is it necessary - we already check the filename.
  5389. */
  5390. attribute_mapping_id = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS) >> 8;
  5391. (void)attribute_mapping_id;
  5392. external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);
  5393. if ((external_attr & MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG) != 0) {
  5394. return MZ_TRUE;
  5395. }
  5396. return MZ_FALSE;
  5397. }
  5398. static mz_bool mz_zip_file_stat_internal(mz_zip_archive *pZip,
  5399. mz_uint file_index,
  5400. const mz_uint8 *pCentral_dir_header,
  5401. mz_zip_archive_file_stat *pStat,
  5402. mz_bool *pFound_zip64_extra_data) {
  5403. mz_uint n;
  5404. const mz_uint8 *p = pCentral_dir_header;
  5405. if (pFound_zip64_extra_data)
  5406. *pFound_zip64_extra_data = MZ_FALSE;
  5407. if ((!p) || (!pStat))
  5408. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5409. /* Extract fields from the central directory record. */
  5410. pStat->m_file_index = file_index;
  5411. pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT(
  5412. &pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index);
  5413. pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS);
  5414. pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS);
  5415. pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
  5416. pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);
  5417. #ifndef MINIZ_NO_TIME
  5418. pStat->m_time =
  5419. mz_zip_dos_to_time_t(MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_TIME_OFS),
  5420. MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_DATE_OFS));
  5421. #endif
  5422. pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS);
  5423. pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
  5424. pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
  5425. pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS);
  5426. pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);
  5427. pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
  5428. /* Copy as much of the filename and comment as possible. */
  5429. n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  5430. n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1);
  5431. memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);
  5432. pStat->m_filename[n] = '\0';
  5433. n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS);
  5434. n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1);
  5435. pStat->m_comment_size = n;
  5436. memcpy(pStat->m_comment,
  5437. p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
  5438. MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) +
  5439. MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS),
  5440. n);
  5441. pStat->m_comment[n] = '\0';
  5442. /* Set some flags for convienance */
  5443. pStat->m_is_directory = mz_zip_reader_is_file_a_directory(pZip, file_index);
  5444. pStat->m_is_encrypted = mz_zip_reader_is_file_encrypted(pZip, file_index);
  5445. pStat->m_is_supported = mz_zip_reader_is_file_supported(pZip, file_index);
  5446. /* See if we need to read any zip64 extended information fields. */
  5447. /* Confusingly, these zip64 fields can be present even on non-zip64 archives
  5448. * (Debian zip on a huge files from stdin piped to stdout creates them). */
  5449. if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size),
  5450. pStat->m_local_header_ofs) == MZ_UINT32_MAX) {
  5451. /* Attempt to find zip64 extended information field in the entry's extra
  5452. * data */
  5453. mz_uint32 extra_size_remaining = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);
  5454. if (extra_size_remaining) {
  5455. const mz_uint8 *pExtra_data =
  5456. p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
  5457. MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  5458. do {
  5459. mz_uint32 field_id;
  5460. mz_uint32 field_data_size;
  5461. if (extra_size_remaining < (sizeof(mz_uint16) * 2))
  5462. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5463. field_id = MZ_READ_LE16(pExtra_data);
  5464. field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  5465. if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining)
  5466. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5467. if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) {
  5468. const mz_uint8 *pField_data = pExtra_data + sizeof(mz_uint16) * 2;
  5469. mz_uint32 field_data_remaining = field_data_size;
  5470. if (pFound_zip64_extra_data)
  5471. *pFound_zip64_extra_data = MZ_TRUE;
  5472. if (pStat->m_uncomp_size == MZ_UINT32_MAX) {
  5473. if (field_data_remaining < sizeof(mz_uint64))
  5474. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5475. pStat->m_uncomp_size = MZ_READ_LE64(pField_data);
  5476. pField_data += sizeof(mz_uint64);
  5477. field_data_remaining -= sizeof(mz_uint64);
  5478. }
  5479. if (pStat->m_comp_size == MZ_UINT32_MAX) {
  5480. if (field_data_remaining < sizeof(mz_uint64))
  5481. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5482. pStat->m_comp_size = MZ_READ_LE64(pField_data);
  5483. pField_data += sizeof(mz_uint64);
  5484. field_data_remaining -= sizeof(mz_uint64);
  5485. }
  5486. if (pStat->m_local_header_ofs == MZ_UINT32_MAX) {
  5487. if (field_data_remaining < sizeof(mz_uint64))
  5488. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5489. pStat->m_local_header_ofs = MZ_READ_LE64(pField_data);
  5490. pField_data += sizeof(mz_uint64);
  5491. (void)pField_data; // unused
  5492. field_data_remaining -= sizeof(mz_uint64);
  5493. (void)field_data_remaining; // unused
  5494. }
  5495. break;
  5496. }
  5497. pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;
  5498. extra_size_remaining =
  5499. extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;
  5500. } while (extra_size_remaining);
  5501. }
  5502. }
  5503. return MZ_TRUE;
  5504. }
  5505. static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char *pA,
  5506. const char *pB, mz_uint len,
  5507. mz_uint flags) {
  5508. mz_uint i;
  5509. if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE)
  5510. return 0 == memcmp(pA, pB, len);
  5511. for (i = 0; i < len; ++i)
  5512. if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i]))
  5513. return MZ_FALSE;
  5514. return MZ_TRUE;
  5515. }
  5516. static MZ_FORCEINLINE int
  5517. mz_zip_filename_compare(const mz_zip_array *pCentral_dir_array,
  5518. const mz_zip_array *pCentral_dir_offsets,
  5519. mz_uint l_index, const char *pR, mz_uint r_len) {
  5520. const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(
  5521. pCentral_dir_array, mz_uint8,
  5522. MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32,
  5523. l_index)),
  5524. *pE;
  5525. mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  5526. mz_uint8 l = 0, r = 0;
  5527. pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  5528. pE = pL + MZ_MIN(l_len, r_len);
  5529. while (pL < pE) {
  5530. if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
  5531. break;
  5532. pL++;
  5533. pR++;
  5534. }
  5535. return (pL == pE) ? (int)(l_len - r_len) : (l - r);
  5536. }
  5537. static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive *pZip,
  5538. const char *pFilename,
  5539. mz_uint32 *pIndex) {
  5540. mz_zip_internal_state *pState = pZip->m_pState;
  5541. const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;
  5542. const mz_zip_array *pCentral_dir = &pState->m_central_dir;
  5543. mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT(
  5544. &pState->m_sorted_central_dir_offsets, mz_uint32, 0);
  5545. const uint32_t size = pZip->m_total_files;
  5546. const mz_uint filename_len = (mz_uint)strlen(pFilename);
  5547. if (pIndex)
  5548. *pIndex = 0;
  5549. if (size) {
  5550. /* yes I could use uint32_t's, but then we would have to add some special
  5551. * case checks in the loop, argh, and */
  5552. /* honestly the major expense here on 32-bit CPU's will still be the
  5553. * filename compare */
  5554. mz_int64 l = 0, h = (mz_int64)size - 1;
  5555. while (l <= h) {
  5556. mz_int64 m = l + ((h - l) >> 1);
  5557. uint32_t file_index = pIndices[(uint32_t)m];
  5558. int comp = mz_zip_filename_compare(pCentral_dir, pCentral_dir_offsets,
  5559. file_index, pFilename, filename_len);
  5560. if (!comp) {
  5561. if (pIndex)
  5562. *pIndex = file_index;
  5563. return MZ_TRUE;
  5564. } else if (comp < 0)
  5565. l = m + 1;
  5566. else
  5567. h = m - 1;
  5568. }
  5569. }
  5570. return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);
  5571. }
  5572. int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName,
  5573. const char *pComment, mz_uint flags) {
  5574. mz_uint32 index;
  5575. if (!mz_zip_reader_locate_file_v2(pZip, pName, pComment, flags, &index))
  5576. return -1;
  5577. else
  5578. return (int)index;
  5579. }
  5580. mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName,
  5581. const char *pComment, mz_uint flags,
  5582. mz_uint32 *pIndex) {
  5583. mz_uint file_index;
  5584. size_t name_len, comment_len;
  5585. if (pIndex)
  5586. *pIndex = 0;
  5587. if ((!pZip) || (!pZip->m_pState) || (!pName))
  5588. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5589. /* See if we can use a binary search */
  5590. if (((pZip->m_pState->m_init_flags &
  5591. MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0) &&
  5592. (pZip->m_zip_mode == MZ_ZIP_MODE_READING) &&
  5593. ((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) &&
  5594. (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) {
  5595. return mz_zip_locate_file_binary_search(pZip, pName, pIndex);
  5596. }
  5597. /* Locate the entry by scanning the entire central directory */
  5598. name_len = strlen(pName);
  5599. if (name_len > MZ_UINT16_MAX)
  5600. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5601. comment_len = pComment ? strlen(pComment) : 0;
  5602. if (comment_len > MZ_UINT16_MAX)
  5603. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5604. for (file_index = 0; file_index < pZip->m_total_files; file_index++) {
  5605. const mz_uint8 *pHeader = &MZ_ZIP_ARRAY_ELEMENT(
  5606. &pZip->m_pState->m_central_dir, mz_uint8,
  5607. MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32,
  5608. file_index));
  5609. mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  5610. const char *pFilename =
  5611. (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  5612. if (filename_len < name_len)
  5613. continue;
  5614. if (comment_len) {
  5615. mz_uint file_extra_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_EXTRA_LEN_OFS),
  5616. file_comment_len =
  5617. MZ_READ_LE16(pHeader + MZ_ZIP_CDH_COMMENT_LEN_OFS);
  5618. const char *pFile_comment = pFilename + filename_len + file_extra_len;
  5619. if ((file_comment_len != comment_len) ||
  5620. (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len,
  5621. flags)))
  5622. continue;
  5623. }
  5624. if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) {
  5625. int ofs = filename_len - 1;
  5626. do {
  5627. if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') ||
  5628. (pFilename[ofs] == ':'))
  5629. break;
  5630. } while (--ofs >= 0);
  5631. ofs++;
  5632. pFilename += ofs;
  5633. filename_len -= ofs;
  5634. }
  5635. if ((filename_len == name_len) &&
  5636. (mz_zip_string_equal(pName, pFilename, filename_len, flags))) {
  5637. if (pIndex)
  5638. *pIndex = file_index;
  5639. return MZ_TRUE;
  5640. }
  5641. }
  5642. return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);
  5643. }
  5644. static mz_bool mz_zip_reader_extract_to_mem_no_alloc1(
  5645. mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size,
  5646. mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size,
  5647. const mz_zip_archive_file_stat *st) {
  5648. int status = TINFL_STATUS_DONE;
  5649. mz_uint64 needed_size, cur_file_ofs, comp_remaining,
  5650. out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail;
  5651. mz_zip_archive_file_stat file_stat;
  5652. void *pRead_buf;
  5653. mz_uint32
  5654. local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) /
  5655. sizeof(mz_uint32)];
  5656. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  5657. tinfl_decompressor inflator;
  5658. if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) ||
  5659. ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead))
  5660. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5661. if (st) {
  5662. file_stat = *st;
  5663. } else if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
  5664. return MZ_FALSE;
  5665. /* A directory or zero length file */
  5666. if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
  5667. return MZ_TRUE;
  5668. /* Encryption and patch files are not supported. */
  5669. if (file_stat.m_bit_flag &
  5670. (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED |
  5671. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION |
  5672. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))
  5673. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  5674. /* This function only supports decompressing stored and deflate. */
  5675. if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) &&
  5676. (file_stat.m_method != MZ_DEFLATED))
  5677. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  5678. /* Ensure supplied output buffer is large enough. */
  5679. needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size
  5680. : file_stat.m_uncomp_size;
  5681. if (buf_size < needed_size)
  5682. return mz_zip_set_error(pZip, MZ_ZIP_BUF_TOO_SMALL);
  5683. /* Read and parse the local directory entry. */
  5684. cur_file_ofs = file_stat.m_local_header_ofs;
  5685. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header,
  5686. MZ_ZIP_LOCAL_DIR_HEADER_SIZE) !=
  5687. MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  5688. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  5689. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
  5690. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5691. cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE +
  5692. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) +
  5693. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  5694. if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
  5695. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5696. if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) {
  5697. /* The file is stored or the caller has requested the compressed data. */
  5698. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf,
  5699. (size_t)needed_size) != needed_size)
  5700. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  5701. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  5702. if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) == 0) {
  5703. if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf,
  5704. (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)
  5705. return mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);
  5706. }
  5707. #endif
  5708. return MZ_TRUE;
  5709. }
  5710. /* Decompress the file either directly from memory or from a file input
  5711. * buffer. */
  5712. tinfl_init(&inflator);
  5713. if (pZip->m_pState->m_pMem) {
  5714. /* Read directly from the archive in memory. */
  5715. pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs;
  5716. read_buf_size = read_buf_avail = file_stat.m_comp_size;
  5717. comp_remaining = 0;
  5718. } else if (pUser_read_buf) {
  5719. /* Use a user provided read buffer. */
  5720. if (!user_read_buf_size)
  5721. return MZ_FALSE;
  5722. pRead_buf = (mz_uint8 *)pUser_read_buf;
  5723. read_buf_size = user_read_buf_size;
  5724. read_buf_avail = 0;
  5725. comp_remaining = file_stat.m_comp_size;
  5726. } else {
  5727. /* Temporarily allocate a read buffer. */
  5728. read_buf_size =
  5729. MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
  5730. if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
  5731. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  5732. if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
  5733. (size_t)read_buf_size)))
  5734. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  5735. read_buf_avail = 0;
  5736. comp_remaining = file_stat.m_comp_size;
  5737. }
  5738. do {
  5739. /* The size_t cast here should be OK because we've verified that the output
  5740. * buffer is >= file_stat.m_uncomp_size above */
  5741. size_t in_buf_size,
  5742. out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs);
  5743. if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) {
  5744. read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
  5745. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf,
  5746. (size_t)read_buf_avail) != read_buf_avail) {
  5747. status = TINFL_STATUS_FAILED;
  5748. mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
  5749. break;
  5750. }
  5751. cur_file_ofs += read_buf_avail;
  5752. comp_remaining -= read_buf_avail;
  5753. read_buf_ofs = 0;
  5754. }
  5755. in_buf_size = (size_t)read_buf_avail;
  5756. status = tinfl_decompress(
  5757. &inflator, (mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size,
  5758. (mz_uint8 *)pBuf, (mz_uint8 *)pBuf + out_buf_ofs, &out_buf_size,
  5759. TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF |
  5760. (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0));
  5761. read_buf_avail -= in_buf_size;
  5762. read_buf_ofs += in_buf_size;
  5763. out_buf_ofs += out_buf_size;
  5764. } while (status == TINFL_STATUS_NEEDS_MORE_INPUT);
  5765. if (status == TINFL_STATUS_DONE) {
  5766. /* Make sure the entire file was decompressed, and check its CRC. */
  5767. if (out_buf_ofs != file_stat.m_uncomp_size) {
  5768. mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
  5769. status = TINFL_STATUS_FAILED;
  5770. }
  5771. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  5772. else if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf,
  5773. (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32) {
  5774. mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);
  5775. status = TINFL_STATUS_FAILED;
  5776. }
  5777. #endif
  5778. }
  5779. if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf))
  5780. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  5781. return status == TINFL_STATUS_DONE;
  5782. }
  5783. mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip,
  5784. mz_uint file_index, void *pBuf,
  5785. size_t buf_size, mz_uint flags,
  5786. void *pUser_read_buf,
  5787. size_t user_read_buf_size) {
  5788. return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf,
  5789. buf_size, flags, pUser_read_buf,
  5790. user_read_buf_size, NULL);
  5791. }
  5792. mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(
  5793. mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size,
  5794. mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) {
  5795. mz_uint32 file_index;
  5796. if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
  5797. return MZ_FALSE;
  5798. return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size,
  5799. flags, pUser_read_buf,
  5800. user_read_buf_size);
  5801. }
  5802. mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index,
  5803. void *pBuf, size_t buf_size,
  5804. mz_uint flags) {
  5805. return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size,
  5806. flags, NULL, 0);
  5807. }
  5808. mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip,
  5809. const char *pFilename, void *pBuf,
  5810. size_t buf_size, mz_uint flags) {
  5811. return mz_zip_reader_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf,
  5812. buf_size, flags, NULL, 0);
  5813. }
  5814. void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index,
  5815. size_t *pSize, mz_uint flags) {
  5816. mz_zip_archive_file_stat file_stat;
  5817. mz_uint64 alloc_size;
  5818. void *pBuf;
  5819. if (pSize)
  5820. *pSize = 0;
  5821. if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
  5822. return NULL;
  5823. alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size
  5824. : file_stat.m_uncomp_size;
  5825. if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) {
  5826. mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  5827. return NULL;
  5828. }
  5829. if (NULL ==
  5830. (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size))) {
  5831. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  5832. return NULL;
  5833. }
  5834. if (!mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf,
  5835. (size_t)alloc_size, flags, NULL,
  5836. 0, &file_stat)) {
  5837. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  5838. return NULL;
  5839. }
  5840. if (pSize)
  5841. *pSize = (size_t)alloc_size;
  5842. return pBuf;
  5843. }
  5844. void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip,
  5845. const char *pFilename, size_t *pSize,
  5846. mz_uint flags) {
  5847. mz_uint32 file_index;
  5848. if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags,
  5849. &file_index)) {
  5850. if (pSize)
  5851. *pSize = 0;
  5852. return MZ_FALSE;
  5853. }
  5854. return mz_zip_reader_extract_to_heap(pZip, file_index, pSize, flags);
  5855. }
  5856. mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip,
  5857. mz_uint file_index,
  5858. mz_file_write_func pCallback,
  5859. void *pOpaque, mz_uint flags) {
  5860. int status = TINFL_STATUS_DONE;
  5861. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  5862. mz_uint file_crc32 = MZ_CRC32_INIT;
  5863. #endif
  5864. mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining,
  5865. out_buf_ofs = 0, cur_file_ofs;
  5866. mz_zip_archive_file_stat file_stat;
  5867. void *pRead_buf = NULL;
  5868. void *pWrite_buf = NULL;
  5869. mz_uint32
  5870. local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) /
  5871. sizeof(mz_uint32)];
  5872. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  5873. if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead))
  5874. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  5875. if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
  5876. return MZ_FALSE;
  5877. /* A directory or zero length file */
  5878. if (file_stat.m_is_directory || (!file_stat.m_comp_size))
  5879. return MZ_TRUE;
  5880. /* Encryption and patch files are not supported. */
  5881. if (file_stat.m_bit_flag &
  5882. (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED |
  5883. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION |
  5884. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))
  5885. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  5886. /* This function only supports decompressing stored and deflate. */
  5887. if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) &&
  5888. (file_stat.m_method != MZ_DEFLATED))
  5889. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  5890. /* Read and do some minimal validation of the local directory entry (this
  5891. * doesn't crack the zip64 stuff, which we already have from the central dir)
  5892. */
  5893. cur_file_ofs = file_stat.m_local_header_ofs;
  5894. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header,
  5895. MZ_ZIP_LOCAL_DIR_HEADER_SIZE) !=
  5896. MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  5897. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  5898. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
  5899. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5900. cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE +
  5901. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) +
  5902. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  5903. if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
  5904. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  5905. /* Decompress the file either directly from memory or from a file input
  5906. * buffer. */
  5907. if (pZip->m_pState->m_pMem) {
  5908. pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs;
  5909. read_buf_size = read_buf_avail = file_stat.m_comp_size;
  5910. comp_remaining = 0;
  5911. } else {
  5912. read_buf_size =
  5913. MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
  5914. if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
  5915. (size_t)read_buf_size)))
  5916. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  5917. read_buf_avail = 0;
  5918. comp_remaining = file_stat.m_comp_size;
  5919. }
  5920. if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) {
  5921. /* The file is stored or the caller has requested the compressed data. */
  5922. if (pZip->m_pState->m_pMem) {
  5923. if (((sizeof(size_t) == sizeof(mz_uint32))) &&
  5924. (file_stat.m_comp_size > MZ_UINT32_MAX))
  5925. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  5926. if (pCallback(pOpaque, out_buf_ofs, pRead_buf,
  5927. (size_t)file_stat.m_comp_size) != file_stat.m_comp_size) {
  5928. mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
  5929. status = TINFL_STATUS_FAILED;
  5930. } else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
  5931. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  5932. file_crc32 =
  5933. (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf,
  5934. (size_t)file_stat.m_comp_size);
  5935. #endif
  5936. }
  5937. cur_file_ofs += file_stat.m_comp_size;
  5938. out_buf_ofs += file_stat.m_comp_size;
  5939. comp_remaining = 0;
  5940. } else {
  5941. while (comp_remaining) {
  5942. read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
  5943. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf,
  5944. (size_t)read_buf_avail) != read_buf_avail) {
  5945. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  5946. status = TINFL_STATUS_FAILED;
  5947. break;
  5948. }
  5949. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  5950. if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
  5951. file_crc32 = (mz_uint32)mz_crc32(
  5952. file_crc32, (const mz_uint8 *)pRead_buf, (size_t)read_buf_avail);
  5953. }
  5954. #endif
  5955. if (pCallback(pOpaque, out_buf_ofs, pRead_buf,
  5956. (size_t)read_buf_avail) != read_buf_avail) {
  5957. mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
  5958. status = TINFL_STATUS_FAILED;
  5959. break;
  5960. }
  5961. cur_file_ofs += read_buf_avail;
  5962. out_buf_ofs += read_buf_avail;
  5963. comp_remaining -= read_buf_avail;
  5964. }
  5965. }
  5966. } else {
  5967. tinfl_decompressor inflator;
  5968. tinfl_init(&inflator);
  5969. if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
  5970. TINFL_LZ_DICT_SIZE))) {
  5971. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  5972. status = TINFL_STATUS_FAILED;
  5973. } else {
  5974. do {
  5975. mz_uint8 *pWrite_buf_cur =
  5976. (mz_uint8 *)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
  5977. size_t in_buf_size,
  5978. out_buf_size =
  5979. TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
  5980. if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) {
  5981. read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
  5982. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf,
  5983. (size_t)read_buf_avail) != read_buf_avail) {
  5984. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  5985. status = TINFL_STATUS_FAILED;
  5986. break;
  5987. }
  5988. cur_file_ofs += read_buf_avail;
  5989. comp_remaining -= read_buf_avail;
  5990. read_buf_ofs = 0;
  5991. }
  5992. in_buf_size = (size_t)read_buf_avail;
  5993. status = tinfl_decompress(
  5994. &inflator, (const mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size,
  5995. (mz_uint8 *)pWrite_buf, pWrite_buf_cur, &out_buf_size,
  5996. comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0);
  5997. read_buf_avail -= in_buf_size;
  5998. read_buf_ofs += in_buf_size;
  5999. if (out_buf_size) {
  6000. if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) !=
  6001. out_buf_size) {
  6002. mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
  6003. status = TINFL_STATUS_FAILED;
  6004. break;
  6005. }
  6006. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  6007. file_crc32 =
  6008. (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size);
  6009. #endif
  6010. if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size) {
  6011. mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
  6012. status = TINFL_STATUS_FAILED;
  6013. break;
  6014. }
  6015. }
  6016. } while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) ||
  6017. (status == TINFL_STATUS_HAS_MORE_OUTPUT));
  6018. }
  6019. }
  6020. if ((status == TINFL_STATUS_DONE) &&
  6021. (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) {
  6022. /* Make sure the entire file was decompressed, and check its CRC. */
  6023. if (out_buf_ofs != file_stat.m_uncomp_size) {
  6024. mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
  6025. status = TINFL_STATUS_FAILED;
  6026. }
  6027. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  6028. else if (file_crc32 != file_stat.m_crc32) {
  6029. mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
  6030. status = TINFL_STATUS_FAILED;
  6031. }
  6032. #endif
  6033. }
  6034. if (!pZip->m_pState->m_pMem)
  6035. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  6036. if (pWrite_buf)
  6037. pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf);
  6038. return status == TINFL_STATUS_DONE;
  6039. }
  6040. mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip,
  6041. const char *pFilename,
  6042. mz_file_write_func pCallback,
  6043. void *pOpaque, mz_uint flags) {
  6044. mz_uint32 file_index;
  6045. if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
  6046. return MZ_FALSE;
  6047. return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque,
  6048. flags);
  6049. }
  6050. mz_zip_reader_extract_iter_state *
  6051. mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index,
  6052. mz_uint flags) {
  6053. mz_zip_reader_extract_iter_state *pState;
  6054. mz_uint32
  6055. local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) /
  6056. sizeof(mz_uint32)];
  6057. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  6058. /* Argument sanity check */
  6059. if ((!pZip) || (!pZip->m_pState))
  6060. return NULL;
  6061. /* Allocate an iterator status structure */
  6062. pState = (mz_zip_reader_extract_iter_state *)pZip->m_pAlloc(
  6063. pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_reader_extract_iter_state));
  6064. if (!pState) {
  6065. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  6066. return NULL;
  6067. }
  6068. /* Fetch file details */
  6069. if (!mz_zip_reader_file_stat(pZip, file_index, &pState->file_stat)) {
  6070. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6071. return NULL;
  6072. }
  6073. /* Encryption and patch files are not supported. */
  6074. if (pState->file_stat.m_bit_flag &
  6075. (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED |
  6076. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION |
  6077. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) {
  6078. mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  6079. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6080. return NULL;
  6081. }
  6082. /* This function only supports decompressing stored and deflate. */
  6083. if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) &&
  6084. (pState->file_stat.m_method != 0) &&
  6085. (pState->file_stat.m_method != MZ_DEFLATED)) {
  6086. mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  6087. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6088. return NULL;
  6089. }
  6090. /* Init state - save args */
  6091. pState->pZip = pZip;
  6092. pState->flags = flags;
  6093. /* Init state - reset variables to defaults */
  6094. pState->status = TINFL_STATUS_DONE;
  6095. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  6096. pState->file_crc32 = MZ_CRC32_INIT;
  6097. #endif
  6098. pState->read_buf_ofs = 0;
  6099. pState->out_buf_ofs = 0;
  6100. pState->pRead_buf = NULL;
  6101. pState->pWrite_buf = NULL;
  6102. pState->out_blk_remain = 0;
  6103. /* Read and parse the local directory entry. */
  6104. pState->cur_file_ofs = pState->file_stat.m_local_header_ofs;
  6105. if (pZip->m_pRead(pZip->m_pIO_opaque, pState->cur_file_ofs, pLocal_header,
  6106. MZ_ZIP_LOCAL_DIR_HEADER_SIZE) !=
  6107. MZ_ZIP_LOCAL_DIR_HEADER_SIZE) {
  6108. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  6109. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6110. return NULL;
  6111. }
  6112. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) {
  6113. mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  6114. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6115. return NULL;
  6116. }
  6117. pState->cur_file_ofs +=
  6118. MZ_ZIP_LOCAL_DIR_HEADER_SIZE +
  6119. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) +
  6120. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  6121. if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) >
  6122. pZip->m_archive_size) {
  6123. mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  6124. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6125. return NULL;
  6126. }
  6127. /* Decompress the file either directly from memory or from a file input
  6128. * buffer. */
  6129. if (pZip->m_pState->m_pMem) {
  6130. pState->pRead_buf =
  6131. (mz_uint8 *)pZip->m_pState->m_pMem + pState->cur_file_ofs;
  6132. pState->read_buf_size = pState->read_buf_avail =
  6133. pState->file_stat.m_comp_size;
  6134. pState->comp_remaining = pState->file_stat.m_comp_size;
  6135. } else {
  6136. if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ||
  6137. (!pState->file_stat.m_method))) {
  6138. /* Decompression required, therefore intermediate read buffer required */
  6139. pState->read_buf_size = MZ_MIN(pState->file_stat.m_comp_size,
  6140. (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
  6141. if (NULL ==
  6142. (pState->pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
  6143. (size_t)pState->read_buf_size))) {
  6144. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  6145. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6146. return NULL;
  6147. }
  6148. } else {
  6149. /* Decompression not required - we will be reading directly into user
  6150. * buffer, no temp buf required */
  6151. pState->read_buf_size = 0;
  6152. }
  6153. pState->read_buf_avail = 0;
  6154. pState->comp_remaining = pState->file_stat.m_comp_size;
  6155. }
  6156. if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ||
  6157. (!pState->file_stat.m_method))) {
  6158. /* Decompression required, init decompressor */
  6159. tinfl_init(&pState->inflator);
  6160. /* Allocate write buffer */
  6161. if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
  6162. TINFL_LZ_DICT_SIZE))) {
  6163. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  6164. if (pState->pRead_buf)
  6165. pZip->m_pFree(pZip->m_pAlloc_opaque, pState->pRead_buf);
  6166. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6167. return NULL;
  6168. }
  6169. }
  6170. return pState;
  6171. }
  6172. mz_zip_reader_extract_iter_state *
  6173. mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename,
  6174. mz_uint flags) {
  6175. mz_uint32 file_index;
  6176. /* Locate file index by name */
  6177. if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
  6178. return NULL;
  6179. /* Construct iterator */
  6180. return mz_zip_reader_extract_iter_new(pZip, file_index, flags);
  6181. }
  6182. size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state *pState,
  6183. void *pvBuf, size_t buf_size) {
  6184. size_t copied_to_caller = 0;
  6185. /* Argument sanity check */
  6186. if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf))
  6187. return 0;
  6188. if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ||
  6189. (!pState->file_stat.m_method)) {
  6190. /* The file is stored or the caller has requested the compressed data, calc
  6191. * amount to return. */
  6192. copied_to_caller = (size_t)MZ_MIN(buf_size, pState->comp_remaining);
  6193. /* Zip is in memory....or requires reading from a file? */
  6194. if (pState->pZip->m_pState->m_pMem) {
  6195. /* Copy data to caller's buffer */
  6196. memcpy(pvBuf, pState->pRead_buf, copied_to_caller);
  6197. pState->pRead_buf = ((mz_uint8 *)pState->pRead_buf) + copied_to_caller;
  6198. } else {
  6199. /* Read directly into caller's buffer */
  6200. if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque,
  6201. pState->cur_file_ofs, pvBuf,
  6202. copied_to_caller) != copied_to_caller) {
  6203. /* Failed to read all that was asked for, flag failure and alert user */
  6204. mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);
  6205. pState->status = TINFL_STATUS_FAILED;
  6206. copied_to_caller = 0;
  6207. }
  6208. }
  6209. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  6210. /* Compute CRC if not returning compressed data only */
  6211. if (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
  6212. pState->file_crc32 = (mz_uint32)mz_crc32(
  6213. pState->file_crc32, (const mz_uint8 *)pvBuf, copied_to_caller);
  6214. #endif
  6215. /* Advance offsets, dec counters */
  6216. pState->cur_file_ofs += copied_to_caller;
  6217. pState->out_buf_ofs += copied_to_caller;
  6218. pState->comp_remaining -= copied_to_caller;
  6219. } else {
  6220. do {
  6221. /* Calc ptr to write buffer - given current output pos and block size */
  6222. mz_uint8 *pWrite_buf_cur =
  6223. (mz_uint8 *)pState->pWrite_buf +
  6224. (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
  6225. /* Calc max output size - given current output pos and block size */
  6226. size_t in_buf_size,
  6227. out_buf_size = TINFL_LZ_DICT_SIZE -
  6228. (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
  6229. if (!pState->out_blk_remain) {
  6230. /* Read more data from file if none available (and reading from file) */
  6231. if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem)) {
  6232. /* Calc read size */
  6233. pState->read_buf_avail =
  6234. MZ_MIN(pState->read_buf_size, pState->comp_remaining);
  6235. if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque,
  6236. pState->cur_file_ofs, pState->pRead_buf,
  6237. (size_t)pState->read_buf_avail) !=
  6238. pState->read_buf_avail) {
  6239. mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);
  6240. pState->status = TINFL_STATUS_FAILED;
  6241. break;
  6242. }
  6243. /* Advance offsets, dec counters */
  6244. pState->cur_file_ofs += pState->read_buf_avail;
  6245. pState->comp_remaining -= pState->read_buf_avail;
  6246. pState->read_buf_ofs = 0;
  6247. }
  6248. /* Perform decompression */
  6249. in_buf_size = (size_t)pState->read_buf_avail;
  6250. pState->status = tinfl_decompress(
  6251. &pState->inflator,
  6252. (const mz_uint8 *)pState->pRead_buf + pState->read_buf_ofs,
  6253. &in_buf_size, (mz_uint8 *)pState->pWrite_buf, pWrite_buf_cur,
  6254. &out_buf_size,
  6255. pState->comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0);
  6256. pState->read_buf_avail -= in_buf_size;
  6257. pState->read_buf_ofs += in_buf_size;
  6258. /* Update current output block size remaining */
  6259. pState->out_blk_remain = out_buf_size;
  6260. }
  6261. if (pState->out_blk_remain) {
  6262. /* Calc amount to return. */
  6263. size_t to_copy =
  6264. MZ_MIN((buf_size - copied_to_caller), pState->out_blk_remain);
  6265. /* Copy data to caller's buffer */
  6266. memcpy((uint8_t *)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy);
  6267. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  6268. /* Perform CRC */
  6269. pState->file_crc32 =
  6270. (mz_uint32)mz_crc32(pState->file_crc32, pWrite_buf_cur, to_copy);
  6271. #endif
  6272. /* Decrement data consumed from block */
  6273. pState->out_blk_remain -= to_copy;
  6274. /* Inc output offset, while performing sanity check */
  6275. if ((pState->out_buf_ofs += to_copy) >
  6276. pState->file_stat.m_uncomp_size) {
  6277. mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);
  6278. pState->status = TINFL_STATUS_FAILED;
  6279. break;
  6280. }
  6281. /* Increment counter of data copied to caller */
  6282. copied_to_caller += to_copy;
  6283. }
  6284. } while ((copied_to_caller < buf_size) &&
  6285. ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) ||
  6286. (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT)));
  6287. }
  6288. /* Return how many bytes were copied into user buffer */
  6289. return copied_to_caller;
  6290. }
  6291. mz_bool
  6292. mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state *pState) {
  6293. int status;
  6294. /* Argument sanity check */
  6295. if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState))
  6296. return MZ_FALSE;
  6297. /* Was decompression completed and requested? */
  6298. if ((pState->status == TINFL_STATUS_DONE) &&
  6299. (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) {
  6300. /* Make sure the entire file was decompressed, and check its CRC. */
  6301. if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size) {
  6302. mz_zip_set_error(pState->pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
  6303. pState->status = TINFL_STATUS_FAILED;
  6304. }
  6305. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  6306. else if (pState->file_crc32 != pState->file_stat.m_crc32) {
  6307. mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);
  6308. pState->status = TINFL_STATUS_FAILED;
  6309. }
  6310. #endif
  6311. }
  6312. /* Free buffers */
  6313. if (!pState->pZip->m_pState->m_pMem)
  6314. pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pRead_buf);
  6315. if (pState->pWrite_buf)
  6316. pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pWrite_buf);
  6317. /* Save status */
  6318. status = pState->status;
  6319. /* Free context */
  6320. pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState);
  6321. return status == TINFL_STATUS_DONE;
  6322. }
  6323. #ifndef MINIZ_NO_STDIO
  6324. static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs,
  6325. const void *pBuf, size_t n) {
  6326. (void)ofs;
  6327. return MZ_FWRITE(pBuf, 1, n, (MZ_FILE *)pOpaque);
  6328. }
  6329. mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index,
  6330. const char *pDst_filename,
  6331. mz_uint flags) {
  6332. mz_bool status;
  6333. mz_zip_archive_file_stat file_stat;
  6334. MZ_FILE *pFile;
  6335. if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
  6336. return MZ_FALSE;
  6337. if (file_stat.m_is_directory || (!file_stat.m_is_supported))
  6338. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
  6339. pFile = MZ_FOPEN(pDst_filename, "wb");
  6340. if (!pFile)
  6341. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  6342. status = mz_zip_reader_extract_to_callback(
  6343. pZip, file_index, mz_zip_file_write_callback, pFile, flags);
  6344. if (MZ_FCLOSE(pFile) == EOF) {
  6345. if (status)
  6346. mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
  6347. status = MZ_FALSE;
  6348. }
  6349. #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
  6350. if (status)
  6351. mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time);
  6352. #endif
  6353. return status;
  6354. }
  6355. mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip,
  6356. const char *pArchive_filename,
  6357. const char *pDst_filename,
  6358. mz_uint flags) {
  6359. mz_uint32 file_index;
  6360. if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags,
  6361. &file_index))
  6362. return MZ_FALSE;
  6363. return mz_zip_reader_extract_to_file(pZip, file_index, pDst_filename, flags);
  6364. }
  6365. mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index,
  6366. MZ_FILE *pFile, mz_uint flags) {
  6367. mz_zip_archive_file_stat file_stat;
  6368. if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
  6369. return MZ_FALSE;
  6370. if (file_stat.m_is_directory || (!file_stat.m_is_supported))
  6371. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
  6372. return mz_zip_reader_extract_to_callback(
  6373. pZip, file_index, mz_zip_file_write_callback, pFile, flags);
  6374. }
  6375. mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive *pZip,
  6376. const char *pArchive_filename,
  6377. MZ_FILE *pFile, mz_uint flags) {
  6378. mz_uint32 file_index;
  6379. if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags,
  6380. &file_index))
  6381. return MZ_FALSE;
  6382. return mz_zip_reader_extract_to_cfile(pZip, file_index, pFile, flags);
  6383. }
  6384. #endif /* #ifndef MINIZ_NO_STDIO */
  6385. static size_t mz_zip_compute_crc32_callback(void *pOpaque, mz_uint64 file_ofs,
  6386. const void *pBuf, size_t n) {
  6387. mz_uint32 *p = (mz_uint32 *)pOpaque;
  6388. (void)file_ofs;
  6389. *p = (mz_uint32)mz_crc32(*p, (const mz_uint8 *)pBuf, n);
  6390. return n;
  6391. }
  6392. mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index,
  6393. mz_uint flags) {
  6394. mz_zip_archive_file_stat file_stat;
  6395. mz_zip_internal_state *pState;
  6396. const mz_uint8 *pCentral_dir_header;
  6397. mz_bool found_zip64_ext_data_in_cdir = MZ_FALSE;
  6398. mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;
  6399. mz_uint32
  6400. local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) /
  6401. sizeof(mz_uint32)];
  6402. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  6403. mz_uint64 local_header_ofs = 0;
  6404. mz_uint32 local_header_filename_len, local_header_extra_len,
  6405. local_header_crc32;
  6406. mz_uint64 local_header_comp_size, local_header_uncomp_size;
  6407. mz_uint32 uncomp_crc32 = MZ_CRC32_INIT;
  6408. mz_bool has_data_descriptor;
  6409. mz_uint32 local_header_bit_flags;
  6410. mz_zip_array file_data_array;
  6411. mz_zip_array_init(&file_data_array, 1);
  6412. if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) ||
  6413. (!pZip->m_pRead))
  6414. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6415. if (file_index > pZip->m_total_files)
  6416. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6417. pState = pZip->m_pState;
  6418. pCentral_dir_header = mz_zip_get_cdh(pZip, file_index);
  6419. if (!mz_zip_file_stat_internal(pZip, file_index, pCentral_dir_header,
  6420. &file_stat, &found_zip64_ext_data_in_cdir))
  6421. return MZ_FALSE;
  6422. /* A directory or zero length file */
  6423. if (file_stat.m_is_directory || (!file_stat.m_uncomp_size))
  6424. return MZ_TRUE;
  6425. /* Encryption and patch files are not supported. */
  6426. if (file_stat.m_is_encrypted)
  6427. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  6428. /* This function only supports stored and deflate. */
  6429. if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
  6430. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  6431. if (!file_stat.m_is_supported)
  6432. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
  6433. /* Read and parse the local directory entry. */
  6434. local_header_ofs = file_stat.m_local_header_ofs;
  6435. if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs, pLocal_header,
  6436. MZ_ZIP_LOCAL_DIR_HEADER_SIZE) !=
  6437. MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  6438. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  6439. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
  6440. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  6441. local_header_filename_len =
  6442. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);
  6443. local_header_extra_len =
  6444. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  6445. local_header_comp_size =
  6446. MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);
  6447. local_header_uncomp_size =
  6448. MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);
  6449. local_header_crc32 = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_CRC32_OFS);
  6450. local_header_bit_flags =
  6451. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);
  6452. has_data_descriptor = (local_header_bit_flags & 8) != 0;
  6453. if (local_header_filename_len != strlen(file_stat.m_filename))
  6454. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  6455. if ((local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE +
  6456. local_header_filename_len + local_header_extra_len +
  6457. file_stat.m_comp_size) > pZip->m_archive_size)
  6458. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  6459. if (!mz_zip_array_resize(
  6460. pZip, &file_data_array,
  6461. MZ_MAX(local_header_filename_len, local_header_extra_len),
  6462. MZ_FALSE)) {
  6463. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  6464. goto handle_failure;
  6465. }
  6466. if (local_header_filename_len) {
  6467. if (pZip->m_pRead(pZip->m_pIO_opaque,
  6468. local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE,
  6469. file_data_array.m_p,
  6470. local_header_filename_len) != local_header_filename_len) {
  6471. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  6472. goto handle_failure;
  6473. }
  6474. /* I've seen 1 archive that had the same pathname, but used backslashes in
  6475. * the local dir and forward slashes in the central dir. Do we care about
  6476. * this? For now, this case will fail validation. */
  6477. if (memcmp(file_stat.m_filename, file_data_array.m_p,
  6478. local_header_filename_len) != 0) {
  6479. mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  6480. goto handle_failure;
  6481. }
  6482. }
  6483. if ((local_header_extra_len) &&
  6484. ((local_header_comp_size == MZ_UINT32_MAX) ||
  6485. (local_header_uncomp_size == MZ_UINT32_MAX))) {
  6486. mz_uint32 extra_size_remaining = local_header_extra_len;
  6487. const mz_uint8 *pExtra_data = (const mz_uint8 *)file_data_array.m_p;
  6488. if (pZip->m_pRead(pZip->m_pIO_opaque,
  6489. local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE +
  6490. local_header_filename_len,
  6491. file_data_array.m_p,
  6492. local_header_extra_len) != local_header_extra_len) {
  6493. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  6494. goto handle_failure;
  6495. }
  6496. do {
  6497. mz_uint32 field_id, field_data_size, field_total_size;
  6498. if (extra_size_remaining < (sizeof(mz_uint16) * 2)) {
  6499. mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  6500. goto handle_failure;
  6501. }
  6502. field_id = MZ_READ_LE16(pExtra_data);
  6503. field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  6504. field_total_size = field_data_size + sizeof(mz_uint16) * 2;
  6505. if (field_total_size > extra_size_remaining) {
  6506. mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  6507. goto handle_failure;
  6508. }
  6509. if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) {
  6510. const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32);
  6511. if (field_data_size < sizeof(mz_uint64) * 2) {
  6512. mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  6513. goto handle_failure;
  6514. }
  6515. local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);
  6516. local_header_comp_size =
  6517. MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64));
  6518. found_zip64_ext_data_in_ldir = MZ_TRUE;
  6519. break;
  6520. }
  6521. pExtra_data += field_total_size;
  6522. extra_size_remaining -= field_total_size;
  6523. } while (extra_size_remaining);
  6524. }
  6525. /* TODO: parse local header extra data when local_header_comp_size is
  6526. * 0xFFFFFFFF! (big_descriptor.zip) */
  6527. /* I've seen zips in the wild with the data descriptor bit set, but proper
  6528. * local header values and bogus data descriptors */
  6529. if ((has_data_descriptor) && (!local_header_comp_size) &&
  6530. (!local_header_crc32)) {
  6531. mz_uint8 descriptor_buf[32];
  6532. mz_bool has_id;
  6533. const mz_uint8 *pSrc;
  6534. mz_uint32 file_crc32;
  6535. mz_uint64 comp_size = 0, uncomp_size = 0;
  6536. mz_uint32 num_descriptor_uint32s =
  6537. ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4;
  6538. if (pZip->m_pRead(pZip->m_pIO_opaque,
  6539. local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE +
  6540. local_header_filename_len + local_header_extra_len +
  6541. file_stat.m_comp_size,
  6542. descriptor_buf,
  6543. sizeof(mz_uint32) * num_descriptor_uint32s) !=
  6544. (sizeof(mz_uint32) * num_descriptor_uint32s)) {
  6545. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  6546. goto handle_failure;
  6547. }
  6548. has_id = (MZ_READ_LE32(descriptor_buf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
  6549. pSrc = has_id ? (descriptor_buf + sizeof(mz_uint32)) : descriptor_buf;
  6550. file_crc32 = MZ_READ_LE32(pSrc);
  6551. if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) {
  6552. comp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32));
  6553. uncomp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32) + sizeof(mz_uint64));
  6554. } else {
  6555. comp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32));
  6556. uncomp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32) + sizeof(mz_uint32));
  6557. }
  6558. if ((file_crc32 != file_stat.m_crc32) ||
  6559. (comp_size != file_stat.m_comp_size) ||
  6560. (uncomp_size != file_stat.m_uncomp_size)) {
  6561. mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  6562. goto handle_failure;
  6563. }
  6564. } else {
  6565. if ((local_header_crc32 != file_stat.m_crc32) ||
  6566. (local_header_comp_size != file_stat.m_comp_size) ||
  6567. (local_header_uncomp_size != file_stat.m_uncomp_size)) {
  6568. mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  6569. goto handle_failure;
  6570. }
  6571. }
  6572. mz_zip_array_clear(pZip, &file_data_array);
  6573. if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0) {
  6574. if (!mz_zip_reader_extract_to_callback(
  6575. pZip, file_index, mz_zip_compute_crc32_callback, &uncomp_crc32, 0))
  6576. return MZ_FALSE;
  6577. /* 1 more check to be sure, although the extract checks too. */
  6578. if (uncomp_crc32 != file_stat.m_crc32) {
  6579. mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  6580. return MZ_FALSE;
  6581. }
  6582. }
  6583. return MZ_TRUE;
  6584. handle_failure:
  6585. mz_zip_array_clear(pZip, &file_data_array);
  6586. return MZ_FALSE;
  6587. }
  6588. mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags) {
  6589. mz_zip_internal_state *pState;
  6590. uint32_t i;
  6591. if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) ||
  6592. (!pZip->m_pRead))
  6593. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6594. pState = pZip->m_pState;
  6595. /* Basic sanity checks */
  6596. if (!pState->m_zip64) {
  6597. if (pZip->m_total_files > MZ_UINT16_MAX)
  6598. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  6599. if (pZip->m_archive_size > MZ_UINT32_MAX)
  6600. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  6601. } else {
  6602. if (pZip->m_total_files >= MZ_UINT32_MAX)
  6603. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  6604. if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)
  6605. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  6606. }
  6607. for (i = 0; i < pZip->m_total_files; i++) {
  6608. if (MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG & flags) {
  6609. mz_uint32 found_index;
  6610. mz_zip_archive_file_stat stat;
  6611. if (!mz_zip_reader_file_stat(pZip, i, &stat))
  6612. return MZ_FALSE;
  6613. if (!mz_zip_reader_locate_file_v2(pZip, stat.m_filename, NULL, 0,
  6614. &found_index))
  6615. return MZ_FALSE;
  6616. /* This check can fail if there are duplicate filenames in the archive
  6617. * (which we don't check for when writing - that's up to the user) */
  6618. if (found_index != i)
  6619. return mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  6620. }
  6621. if (!mz_zip_validate_file(pZip, i, flags))
  6622. return MZ_FALSE;
  6623. }
  6624. return MZ_TRUE;
  6625. }
  6626. mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size,
  6627. mz_uint flags, mz_zip_error *pErr) {
  6628. mz_bool success = MZ_TRUE;
  6629. mz_zip_archive zip;
  6630. mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
  6631. if ((!pMem) || (!size)) {
  6632. if (pErr)
  6633. *pErr = MZ_ZIP_INVALID_PARAMETER;
  6634. return MZ_FALSE;
  6635. }
  6636. mz_zip_zero_struct(&zip);
  6637. if (!mz_zip_reader_init_mem(&zip, pMem, size, flags)) {
  6638. if (pErr)
  6639. *pErr = zip.m_last_error;
  6640. return MZ_FALSE;
  6641. }
  6642. if (!mz_zip_validate_archive(&zip, flags)) {
  6643. actual_err = zip.m_last_error;
  6644. success = MZ_FALSE;
  6645. }
  6646. if (!mz_zip_reader_end_internal(&zip, success)) {
  6647. if (!actual_err)
  6648. actual_err = zip.m_last_error;
  6649. success = MZ_FALSE;
  6650. }
  6651. if (pErr)
  6652. *pErr = actual_err;
  6653. return success;
  6654. }
  6655. #ifndef MINIZ_NO_STDIO
  6656. mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags,
  6657. mz_zip_error *pErr) {
  6658. mz_bool success = MZ_TRUE;
  6659. mz_zip_archive zip;
  6660. mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
  6661. if (!pFilename) {
  6662. if (pErr)
  6663. *pErr = MZ_ZIP_INVALID_PARAMETER;
  6664. return MZ_FALSE;
  6665. }
  6666. mz_zip_zero_struct(&zip);
  6667. if (!mz_zip_reader_init_file_v2(&zip, pFilename, flags, 0, 0)) {
  6668. if (pErr)
  6669. *pErr = zip.m_last_error;
  6670. return MZ_FALSE;
  6671. }
  6672. if (!mz_zip_validate_archive(&zip, flags)) {
  6673. actual_err = zip.m_last_error;
  6674. success = MZ_FALSE;
  6675. }
  6676. if (!mz_zip_reader_end_internal(&zip, success)) {
  6677. if (!actual_err)
  6678. actual_err = zip.m_last_error;
  6679. success = MZ_FALSE;
  6680. }
  6681. if (pErr)
  6682. *pErr = actual_err;
  6683. return success;
  6684. }
  6685. #endif /* #ifndef MINIZ_NO_STDIO */
  6686. /* ------------------- .ZIP archive writing */
  6687. #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
  6688. static MZ_FORCEINLINE void mz_write_le16(mz_uint8 *p, mz_uint16 v) {
  6689. p[0] = (mz_uint8)v;
  6690. p[1] = (mz_uint8)(v >> 8);
  6691. }
  6692. static MZ_FORCEINLINE void mz_write_le32(mz_uint8 *p, mz_uint32 v) {
  6693. p[0] = (mz_uint8)v;
  6694. p[1] = (mz_uint8)(v >> 8);
  6695. p[2] = (mz_uint8)(v >> 16);
  6696. p[3] = (mz_uint8)(v >> 24);
  6697. }
  6698. static MZ_FORCEINLINE void mz_write_le64(mz_uint8 *p, mz_uint64 v) {
  6699. mz_write_le32(p, (mz_uint32)v);
  6700. mz_write_le32(p + sizeof(mz_uint32), (mz_uint32)(v >> 32));
  6701. }
  6702. #define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v))
  6703. #define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v))
  6704. #define MZ_WRITE_LE64(p, v) mz_write_le64((mz_uint8 *)(p), (mz_uint64)(v))
  6705. static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs,
  6706. const void *pBuf, size_t n) {
  6707. mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
  6708. mz_zip_internal_state *pState = pZip->m_pState;
  6709. mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size);
  6710. if (!n)
  6711. return 0;
  6712. /* An allocation this big is likely to just fail on 32-bit systems, so don't
  6713. * even go there. */
  6714. if ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF)) {
  6715. mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
  6716. return 0;
  6717. }
  6718. if (new_size > pState->m_mem_capacity) {
  6719. void *pNew_block;
  6720. size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity);
  6721. while (new_capacity < new_size)
  6722. new_capacity *= 2;
  6723. if (NULL == (pNew_block = pZip->m_pRealloc(
  6724. pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity))) {
  6725. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  6726. return 0;
  6727. }
  6728. pState->m_pMem = pNew_block;
  6729. pState->m_mem_capacity = new_capacity;
  6730. }
  6731. memcpy((mz_uint8 *)pState->m_pMem + file_ofs, pBuf, n);
  6732. pState->m_mem_size = (size_t)new_size;
  6733. return n;
  6734. }
  6735. static mz_bool mz_zip_writer_end_internal(mz_zip_archive *pZip,
  6736. mz_bool set_last_error) {
  6737. mz_zip_internal_state *pState;
  6738. mz_bool status = MZ_TRUE;
  6739. if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) ||
  6740. ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) &&
  6741. (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) {
  6742. if (set_last_error)
  6743. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6744. return MZ_FALSE;
  6745. }
  6746. pState = pZip->m_pState;
  6747. pZip->m_pState = NULL;
  6748. mz_zip_array_clear(pZip, &pState->m_central_dir);
  6749. mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
  6750. mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
  6751. #ifndef MINIZ_NO_STDIO
  6752. if (pState->m_pFile) {
  6753. if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
  6754. if (MZ_FCLOSE(pState->m_pFile) == EOF) {
  6755. if (set_last_error)
  6756. mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
  6757. status = MZ_FALSE;
  6758. }
  6759. }
  6760. pState->m_pFile = NULL;
  6761. }
  6762. #endif /* #ifndef MINIZ_NO_STDIO */
  6763. if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) {
  6764. pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem);
  6765. pState->m_pMem = NULL;
  6766. }
  6767. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  6768. pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
  6769. return status;
  6770. }
  6771. mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size,
  6772. mz_uint flags) {
  6773. mz_bool zip64 = (flags & MZ_ZIP_FLAG_WRITE_ZIP64) != 0;
  6774. if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) ||
  6775. (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
  6776. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6777. if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) {
  6778. if (!pZip->m_pRead)
  6779. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6780. }
  6781. if (pZip->m_file_offset_alignment) {
  6782. /* Ensure user specified file offset alignment is a power of 2. */
  6783. if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1))
  6784. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6785. }
  6786. if (!pZip->m_pAlloc)
  6787. pZip->m_pAlloc = miniz_def_alloc_func;
  6788. if (!pZip->m_pFree)
  6789. pZip->m_pFree = miniz_def_free_func;
  6790. if (!pZip->m_pRealloc)
  6791. pZip->m_pRealloc = miniz_def_realloc_func;
  6792. pZip->m_archive_size = existing_size;
  6793. pZip->m_central_directory_file_ofs = 0;
  6794. pZip->m_total_files = 0;
  6795. if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(
  6796. pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
  6797. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  6798. memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
  6799. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir,
  6800. sizeof(mz_uint8));
  6801. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets,
  6802. sizeof(mz_uint32));
  6803. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets,
  6804. sizeof(mz_uint32));
  6805. pZip->m_pState->m_zip64 = zip64;
  6806. pZip->m_pState->m_zip64_has_extended_info_fields = zip64;
  6807. pZip->m_zip_type = MZ_ZIP_TYPE_USER;
  6808. pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
  6809. return MZ_TRUE;
  6810. }
  6811. mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size) {
  6812. return mz_zip_writer_init_v2(pZip, existing_size, 0);
  6813. }
  6814. mz_bool mz_zip_writer_init_heap_v2(mz_zip_archive *pZip,
  6815. size_t size_to_reserve_at_beginning,
  6816. size_t initial_allocation_size,
  6817. mz_uint flags) {
  6818. pZip->m_pWrite = mz_zip_heap_write_func;
  6819. pZip->m_pNeeds_keepalive = NULL;
  6820. if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
  6821. pZip->m_pRead = mz_zip_mem_read_func;
  6822. pZip->m_pIO_opaque = pZip;
  6823. if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags))
  6824. return MZ_FALSE;
  6825. pZip->m_zip_type = MZ_ZIP_TYPE_HEAP;
  6826. if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size,
  6827. size_to_reserve_at_beginning))) {
  6828. if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(
  6829. pZip->m_pAlloc_opaque, 1, initial_allocation_size))) {
  6830. mz_zip_writer_end_internal(pZip, MZ_FALSE);
  6831. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  6832. }
  6833. pZip->m_pState->m_mem_capacity = initial_allocation_size;
  6834. }
  6835. return MZ_TRUE;
  6836. }
  6837. mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip,
  6838. size_t size_to_reserve_at_beginning,
  6839. size_t initial_allocation_size) {
  6840. return mz_zip_writer_init_heap_v2(pZip, size_to_reserve_at_beginning,
  6841. initial_allocation_size, 0);
  6842. }
  6843. #ifndef MINIZ_NO_STDIO
  6844. static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs,
  6845. const void *pBuf, size_t n) {
  6846. mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
  6847. mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
  6848. file_ofs += pZip->m_pState->m_file_archive_start_ofs;
  6849. if (((mz_int64)file_ofs < 0) ||
  6850. (((cur_ofs != (mz_int64)file_ofs)) &&
  6851. (MZ_FSEEK64(pZip->m_pState->m_pFile, (long)file_ofs, SEEK_SET)))) {
  6852. mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
  6853. return 0;
  6854. }
  6855. return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile);
  6856. }
  6857. mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename,
  6858. mz_uint64 size_to_reserve_at_beginning) {
  6859. return mz_zip_writer_init_file_v2(pZip, pFilename,
  6860. size_to_reserve_at_beginning, 0);
  6861. }
  6862. mz_bool mz_zip_writer_init_file_v2(mz_zip_archive *pZip, const char *pFilename,
  6863. mz_uint64 size_to_reserve_at_beginning,
  6864. mz_uint flags) {
  6865. MZ_FILE *pFile;
  6866. pZip->m_pWrite = mz_zip_file_write_func;
  6867. pZip->m_pNeeds_keepalive = NULL;
  6868. if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
  6869. pZip->m_pRead = mz_zip_file_read_func;
  6870. pZip->m_pIO_opaque = pZip;
  6871. if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags))
  6872. return MZ_FALSE;
  6873. if (NULL == (pFile = MZ_FOPEN(
  6874. pFilename,
  6875. (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) ? "w+b" : "wb"))) {
  6876. mz_zip_writer_end(pZip);
  6877. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  6878. }
  6879. pZip->m_pState->m_pFile = pFile;
  6880. pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
  6881. if (size_to_reserve_at_beginning) {
  6882. mz_uint64 cur_ofs = 0;
  6883. char buf[4096];
  6884. MZ_CLEAR_OBJ(buf);
  6885. do {
  6886. size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning);
  6887. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n) {
  6888. mz_zip_writer_end(pZip);
  6889. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  6890. }
  6891. cur_ofs += n;
  6892. size_to_reserve_at_beginning -= n;
  6893. } while (size_to_reserve_at_beginning);
  6894. }
  6895. return MZ_TRUE;
  6896. }
  6897. mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile,
  6898. mz_uint flags) {
  6899. pZip->m_pWrite = mz_zip_file_write_func;
  6900. pZip->m_pNeeds_keepalive = NULL;
  6901. if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
  6902. pZip->m_pRead = mz_zip_file_read_func;
  6903. pZip->m_pIO_opaque = pZip;
  6904. if (!mz_zip_writer_init_v2(pZip, 0, flags))
  6905. return MZ_FALSE;
  6906. pZip->m_pState->m_pFile = pFile;
  6907. pZip->m_pState->m_file_archive_start_ofs =
  6908. MZ_FTELL64(pZip->m_pState->m_pFile);
  6909. pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
  6910. return MZ_TRUE;
  6911. }
  6912. #endif /* #ifndef MINIZ_NO_STDIO */
  6913. mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip,
  6914. const char *pFilename,
  6915. mz_uint flags) {
  6916. mz_zip_internal_state *pState;
  6917. if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
  6918. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6919. if (flags & MZ_ZIP_FLAG_WRITE_ZIP64) {
  6920. /* We don't support converting a non-zip64 file to zip64 - this seems like
  6921. * more trouble than it's worth. (What about the existing 32-bit data
  6922. * descriptors that could follow the compressed data?) */
  6923. if (!pZip->m_pState->m_zip64)
  6924. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6925. }
  6926. /* No sense in trying to write to an archive that's already at the support max
  6927. * size */
  6928. if (pZip->m_pState->m_zip64) {
  6929. if (pZip->m_total_files == MZ_UINT32_MAX)
  6930. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  6931. } else {
  6932. if (pZip->m_total_files == MZ_UINT16_MAX)
  6933. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  6934. if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
  6935. MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)
  6936. return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
  6937. }
  6938. pState = pZip->m_pState;
  6939. if (pState->m_pFile) {
  6940. #ifdef MINIZ_NO_STDIO
  6941. (void)pFilename;
  6942. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6943. #else
  6944. if (pZip->m_pIO_opaque != pZip)
  6945. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6946. if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
  6947. if (!pFilename)
  6948. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6949. /* Archive is being read from stdio and was originally opened only for
  6950. * reading. Try to reopen as writable. */
  6951. if (NULL ==
  6952. (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile))) {
  6953. /* The mz_zip_archive is now in a bogus state because pState->m_pFile is
  6954. * NULL, so just close it. */
  6955. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  6956. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  6957. }
  6958. }
  6959. pZip->m_pWrite = mz_zip_file_write_func;
  6960. pZip->m_pNeeds_keepalive = NULL;
  6961. #endif /* #ifdef MINIZ_NO_STDIO */
  6962. } else if (pState->m_pMem) {
  6963. /* Archive lives in a memory block. Assume it's from the heap that we can
  6964. * resize using the realloc callback. */
  6965. if (pZip->m_pIO_opaque != pZip)
  6966. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6967. pState->m_mem_capacity = pState->m_mem_size;
  6968. pZip->m_pWrite = mz_zip_heap_write_func;
  6969. pZip->m_pNeeds_keepalive = NULL;
  6970. }
  6971. /* Archive is being read via a user provided read function - make sure the
  6972. user has specified a write function too. */
  6973. else if (!pZip->m_pWrite)
  6974. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6975. /* Start writing new files at the archive's current central directory
  6976. * location. */
  6977. /* TODO: We could add a flag that lets the user start writing immediately
  6978. * AFTER the existing central dir - this would be safer. */
  6979. pZip->m_archive_size = pZip->m_central_directory_file_ofs;
  6980. pZip->m_central_directory_file_ofs = 0;
  6981. /* Clear the sorted central dir offsets, they aren't useful or maintained now.
  6982. */
  6983. /* Even though we're now in write mode, files can still be extracted and
  6984. * verified, but file locates will be slow. */
  6985. /* TODO: We could easily maintain the sorted central directory offsets. */
  6986. mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets);
  6987. pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
  6988. return MZ_TRUE;
  6989. }
  6990. mz_bool mz_zip_writer_init_from_reader_v2_noreopen(mz_zip_archive *pZip,
  6991. const char *pFilename,
  6992. mz_uint flags) {
  6993. mz_zip_internal_state *pState;
  6994. if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
  6995. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  6996. if (flags & MZ_ZIP_FLAG_WRITE_ZIP64) {
  6997. /* We don't support converting a non-zip64 file to zip64 - this seems like
  6998. * more trouble than it's worth. (What about the existing 32-bit data
  6999. * descriptors that could follow the compressed data?) */
  7000. if (!pZip->m_pState->m_zip64)
  7001. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7002. }
  7003. /* No sense in trying to write to an archive that's already at the support max
  7004. * size */
  7005. if (pZip->m_pState->m_zip64) {
  7006. if (pZip->m_total_files == MZ_UINT32_MAX)
  7007. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  7008. } else {
  7009. if (pZip->m_total_files == MZ_UINT16_MAX)
  7010. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  7011. if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
  7012. MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)
  7013. return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
  7014. }
  7015. pState = pZip->m_pState;
  7016. if (pState->m_pFile) {
  7017. #ifdef MINIZ_NO_STDIO
  7018. (void)pFilename;
  7019. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7020. #else
  7021. if (pZip->m_pIO_opaque != pZip)
  7022. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7023. if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
  7024. if (!pFilename)
  7025. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7026. }
  7027. pZip->m_pWrite = mz_zip_file_write_func;
  7028. pZip->m_pNeeds_keepalive = NULL;
  7029. #endif /* #ifdef MINIZ_NO_STDIO */
  7030. } else if (pState->m_pMem) {
  7031. /* Archive lives in a memory block. Assume it's from the heap that we can
  7032. * resize using the realloc callback. */
  7033. if (pZip->m_pIO_opaque != pZip)
  7034. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7035. pState->m_mem_capacity = pState->m_mem_size;
  7036. pZip->m_pWrite = mz_zip_heap_write_func;
  7037. pZip->m_pNeeds_keepalive = NULL;
  7038. }
  7039. /* Archive is being read via a user provided read function - make sure the
  7040. user has specified a write function too. */
  7041. else if (!pZip->m_pWrite)
  7042. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7043. /* Start writing new files at the archive's current central directory
  7044. * location. */
  7045. /* TODO: We could add a flag that lets the user start writing immediately
  7046. * AFTER the existing central dir - this would be safer. */
  7047. pZip->m_archive_size = pZip->m_central_directory_file_ofs;
  7048. pZip->m_central_directory_file_ofs = 0;
  7049. /* Clear the sorted central dir offsets, they aren't useful or maintained now.
  7050. */
  7051. /* Even though we're now in write mode, files can still be extracted and
  7052. * verified, but file locates will be slow. */
  7053. /* TODO: We could easily maintain the sorted central directory offsets. */
  7054. mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets);
  7055. pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
  7056. return MZ_TRUE;
  7057. }
  7058. mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip,
  7059. const char *pFilename) {
  7060. return mz_zip_writer_init_from_reader_v2(pZip, pFilename, 0);
  7061. }
  7062. /* TODO: pArchive_name is a terrible name here! */
  7063. mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name,
  7064. const void *pBuf, size_t buf_size,
  7065. mz_uint level_and_flags) {
  7066. return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0,
  7067. level_and_flags, 0, 0);
  7068. }
  7069. typedef struct {
  7070. mz_zip_archive *m_pZip;
  7071. mz_uint64 m_cur_archive_file_ofs;
  7072. mz_uint64 m_comp_size;
  7073. } mz_zip_writer_add_state;
  7074. static mz_bool mz_zip_writer_add_put_buf_callback(const void *pBuf, int len,
  7075. void *pUser) {
  7076. mz_zip_writer_add_state *pState = (mz_zip_writer_add_state *)pUser;
  7077. if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque,
  7078. pState->m_cur_archive_file_ofs, pBuf,
  7079. len) != len)
  7080. return MZ_FALSE;
  7081. pState->m_cur_archive_file_ofs += len;
  7082. pState->m_comp_size += len;
  7083. return MZ_TRUE;
  7084. }
  7085. #define MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE \
  7086. (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 2)
  7087. #define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE \
  7088. (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 3)
  7089. static mz_uint32
  7090. mz_zip_writer_create_zip64_extra_data(mz_uint8 *pBuf, mz_uint64 *pUncomp_size,
  7091. mz_uint64 *pComp_size,
  7092. mz_uint64 *pLocal_header_ofs) {
  7093. mz_uint8 *pDst = pBuf;
  7094. mz_uint32 field_size = 0;
  7095. MZ_WRITE_LE16(pDst + 0, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID);
  7096. MZ_WRITE_LE16(pDst + 2, 0);
  7097. pDst += sizeof(mz_uint16) * 2;
  7098. if (pUncomp_size) {
  7099. MZ_WRITE_LE64(pDst, *pUncomp_size);
  7100. pDst += sizeof(mz_uint64);
  7101. field_size += sizeof(mz_uint64);
  7102. }
  7103. if (pComp_size) {
  7104. MZ_WRITE_LE64(pDst, *pComp_size);
  7105. pDst += sizeof(mz_uint64);
  7106. field_size += sizeof(mz_uint64);
  7107. }
  7108. if (pLocal_header_ofs) {
  7109. MZ_WRITE_LE64(pDst, *pLocal_header_ofs);
  7110. pDst += sizeof(mz_uint64);
  7111. field_size += sizeof(mz_uint64);
  7112. }
  7113. MZ_WRITE_LE16(pBuf + 2, field_size);
  7114. return (mz_uint32)(pDst - pBuf);
  7115. }
  7116. static mz_bool mz_zip_writer_create_local_dir_header(
  7117. mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size,
  7118. mz_uint16 extra_size, mz_uint64 uncomp_size, mz_uint64 comp_size,
  7119. mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags,
  7120. mz_uint16 dos_time, mz_uint16 dos_date) {
  7121. (void)pZip;
  7122. memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE);
  7123. MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_SIG_OFS, MZ_ZIP_LOCAL_DIR_HEADER_SIG);
  7124. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0);
  7125. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags);
  7126. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method);
  7127. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time);
  7128. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date);
  7129. MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32);
  7130. MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS,
  7131. MZ_MIN(comp_size, MZ_UINT32_MAX));
  7132. MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS,
  7133. MZ_MIN(uncomp_size, MZ_UINT32_MAX));
  7134. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size);
  7135. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size);
  7136. return MZ_TRUE;
  7137. }
  7138. static mz_bool mz_zip_writer_create_central_dir_header(
  7139. mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size,
  7140. mz_uint16 extra_size, mz_uint16 comment_size, mz_uint64 uncomp_size,
  7141. mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method,
  7142. mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,
  7143. mz_uint64 local_header_ofs, mz_uint32 ext_attributes) {
  7144. (void)pZip;
  7145. memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);
  7146. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_SIG_OFS, MZ_ZIP_CENTRAL_DIR_HEADER_SIG);
  7147. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0);
  7148. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags);
  7149. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method);
  7150. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time);
  7151. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date);
  7152. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32);
  7153. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS,
  7154. MZ_MIN(comp_size, MZ_UINT32_MAX));
  7155. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS,
  7156. MZ_MIN(uncomp_size, MZ_UINT32_MAX));
  7157. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size);
  7158. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size);
  7159. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size);
  7160. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes);
  7161. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS,
  7162. MZ_MIN(local_header_ofs, MZ_UINT32_MAX));
  7163. return MZ_TRUE;
  7164. }
  7165. static mz_bool mz_zip_writer_add_to_central_dir(
  7166. mz_zip_archive *pZip, const char *pFilename, mz_uint16 filename_size,
  7167. const void *pExtra, mz_uint16 extra_size, const void *pComment,
  7168. mz_uint16 comment_size, mz_uint64 uncomp_size, mz_uint64 comp_size,
  7169. mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags,
  7170. mz_uint16 dos_time, mz_uint16 dos_date, mz_uint64 local_header_ofs,
  7171. mz_uint32 ext_attributes, const char *user_extra_data,
  7172. mz_uint user_extra_data_len) {
  7173. mz_zip_internal_state *pState = pZip->m_pState;
  7174. mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size;
  7175. size_t orig_central_dir_size = pState->m_central_dir.m_size;
  7176. mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];
  7177. if (!pZip->m_pState->m_zip64) {
  7178. if (local_header_ofs > 0xFFFFFFFF)
  7179. return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
  7180. }
  7181. /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
  7182. if (((mz_uint64)pState->m_central_dir.m_size +
  7183. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size +
  7184. user_extra_data_len + comment_size) >= MZ_UINT32_MAX)
  7185. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  7186. if (!mz_zip_writer_create_central_dir_header(
  7187. pZip, central_dir_header, filename_size,
  7188. (mz_uint16)(extra_size + user_extra_data_len), comment_size,
  7189. uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time,
  7190. dos_date, local_header_ofs, ext_attributes))
  7191. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  7192. if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header,
  7193. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) ||
  7194. (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename,
  7195. filename_size)) ||
  7196. (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra,
  7197. extra_size)) ||
  7198. (!mz_zip_array_push_back(pZip, &pState->m_central_dir, user_extra_data,
  7199. user_extra_data_len)) ||
  7200. (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment,
  7201. comment_size)) ||
  7202. (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets,
  7203. &central_dir_ofs, 1))) {
  7204. /* Try to resize the central directory array back into its original state.
  7205. */
  7206. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size,
  7207. MZ_FALSE);
  7208. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  7209. }
  7210. return MZ_TRUE;
  7211. }
  7212. static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name) {
  7213. /* Basic ZIP archive filename validity checks: Valid filenames cannot start
  7214. * with a forward slash, cannot contain a drive letter, and cannot use
  7215. * DOS-style backward slashes. */
  7216. if (*pArchive_name == '/')
  7217. return MZ_FALSE;
  7218. /* Making sure the name does not contain drive letters or DOS style backward
  7219. * slashes is the responsibility of the program using miniz*/
  7220. return MZ_TRUE;
  7221. }
  7222. static mz_uint
  7223. mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive *pZip) {
  7224. mz_uint32 n;
  7225. if (!pZip->m_file_offset_alignment)
  7226. return 0;
  7227. n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1));
  7228. return (mz_uint)((pZip->m_file_offset_alignment - n) &
  7229. (pZip->m_file_offset_alignment - 1));
  7230. }
  7231. static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip,
  7232. mz_uint64 cur_file_ofs, mz_uint32 n) {
  7233. char buf[4096];
  7234. memset(buf, 0, MZ_MIN(sizeof(buf), n));
  7235. while (n) {
  7236. mz_uint32 s = MZ_MIN(sizeof(buf), n);
  7237. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s)
  7238. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7239. cur_file_ofs += s;
  7240. n -= s;
  7241. }
  7242. return MZ_TRUE;
  7243. }
  7244. mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip,
  7245. const char *pArchive_name, const void *pBuf,
  7246. size_t buf_size, const void *pComment,
  7247. mz_uint16 comment_size,
  7248. mz_uint level_and_flags, mz_uint64 uncomp_size,
  7249. mz_uint32 uncomp_crc32) {
  7250. return mz_zip_writer_add_mem_ex_v2(
  7251. pZip, pArchive_name, pBuf, buf_size, pComment, comment_size,
  7252. level_and_flags, uncomp_size, uncomp_crc32, NULL, NULL, 0, NULL, 0);
  7253. }
  7254. mz_bool mz_zip_writer_add_mem_ex_v2(
  7255. mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf,
  7256. size_t buf_size, const void *pComment, mz_uint16 comment_size,
  7257. mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32,
  7258. MZ_TIME_T *last_modified, const char *user_extra_data,
  7259. mz_uint user_extra_data_len, const char *user_extra_data_central,
  7260. mz_uint user_extra_data_central_len) {
  7261. mz_uint16 method = 0, dos_time = 0, dos_date = 0;
  7262. mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;
  7263. mz_uint64 local_dir_header_ofs = 0, cur_archive_file_ofs = 0, comp_size = 0;
  7264. size_t archive_name_size;
  7265. mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
  7266. tdefl_compressor *pComp = NULL;
  7267. mz_bool store_data_uncompressed;
  7268. mz_zip_internal_state *pState;
  7269. mz_uint8 *pExtra_data = NULL;
  7270. mz_uint32 extra_size = 0;
  7271. mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
  7272. mz_uint16 bit_flags = 0;
  7273. if ((int)level_and_flags < 0)
  7274. level_and_flags = MZ_DEFAULT_LEVEL;
  7275. if (uncomp_size ||
  7276. (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
  7277. bit_flags |= MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;
  7278. if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
  7279. bit_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8;
  7280. level = level_and_flags & 0xF;
  7281. store_data_uncompressed =
  7282. ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA));
  7283. if ((!pZip) || (!pZip->m_pState) ||
  7284. (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) ||
  7285. (!pArchive_name) || ((comment_size) && (!pComment)) ||
  7286. (level > MZ_UBER_COMPRESSION))
  7287. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7288. pState = pZip->m_pState;
  7289. local_dir_header_ofs = pZip->m_archive_size;
  7290. cur_archive_file_ofs = pZip->m_archive_size;
  7291. if (pState->m_zip64) {
  7292. if (pZip->m_total_files == MZ_UINT32_MAX)
  7293. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  7294. } else {
  7295. if (pZip->m_total_files == MZ_UINT16_MAX) {
  7296. pState->m_zip64 = MZ_TRUE;
  7297. /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */
  7298. }
  7299. if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) {
  7300. pState->m_zip64 = MZ_TRUE;
  7301. /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
  7302. }
  7303. }
  7304. if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size))
  7305. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7306. if (!mz_zip_writer_validate_archive_name(pArchive_name))
  7307. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
  7308. #ifndef MINIZ_NO_TIME
  7309. if (last_modified != NULL) {
  7310. mz_zip_time_t_to_dos_time(*last_modified, &dos_time, &dos_date);
  7311. } else {
  7312. MZ_TIME_T cur_time;
  7313. time(&cur_time);
  7314. mz_zip_time_t_to_dos_time(cur_time, &dos_time, &dos_date);
  7315. }
  7316. #endif /* #ifndef MINIZ_NO_TIME */
  7317. if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
  7318. uncomp_crc32 =
  7319. (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, buf_size);
  7320. uncomp_size = buf_size;
  7321. if (uncomp_size <= 3) {
  7322. level = 0;
  7323. store_data_uncompressed = MZ_TRUE;
  7324. }
  7325. }
  7326. archive_name_size = strlen(pArchive_name);
  7327. if (archive_name_size > MZ_UINT16_MAX)
  7328. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
  7329. num_alignment_padding_bytes =
  7330. mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
  7331. /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
  7332. if (((mz_uint64)pState->m_central_dir.m_size +
  7333. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size +
  7334. MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX)
  7335. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  7336. if (!pState->m_zip64) {
  7337. /* Bail early if the archive would obviously become too large */
  7338. if ((pZip->m_archive_size + num_alignment_padding_bytes +
  7339. MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size +
  7340. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size +
  7341. user_extra_data_len + pState->m_central_dir.m_size +
  7342. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + user_extra_data_central_len +
  7343. MZ_ZIP_DATA_DESCRIPTER_SIZE32) > 0xFFFFFFFF) {
  7344. pState->m_zip64 = MZ_TRUE;
  7345. /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
  7346. }
  7347. }
  7348. if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/')) {
  7349. /* Set DOS Subdirectory attribute bit. */
  7350. ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
  7351. /* Subdirectories cannot contain data. */
  7352. if ((buf_size) || (uncomp_size))
  7353. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7354. }
  7355. /* Try to do any allocations before writing to the archive, so if an
  7356. * allocation fails the file remains unmodified. (A good idea if we're doing
  7357. * an in-place modification.) */
  7358. if ((!mz_zip_array_ensure_room(
  7359. pZip, &pState->m_central_dir,
  7360. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size +
  7361. (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) ||
  7362. (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1)))
  7363. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  7364. if ((!store_data_uncompressed) && (buf_size)) {
  7365. if (NULL == (pComp = (tdefl_compressor *)pZip->m_pAlloc(
  7366. pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor))))
  7367. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  7368. }
  7369. if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs,
  7370. num_alignment_padding_bytes)) {
  7371. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  7372. return MZ_FALSE;
  7373. }
  7374. local_dir_header_ofs += num_alignment_padding_bytes;
  7375. if (pZip->m_file_offset_alignment) {
  7376. MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) ==
  7377. 0);
  7378. }
  7379. cur_archive_file_ofs += num_alignment_padding_bytes;
  7380. MZ_CLEAR_OBJ(local_dir_header);
  7381. if (!store_data_uncompressed ||
  7382. (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
  7383. method = MZ_DEFLATED;
  7384. }
  7385. if (pState->m_zip64) {
  7386. if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) {
  7387. pExtra_data = extra_data;
  7388. extra_size = mz_zip_writer_create_zip64_extra_data(
  7389. extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
  7390. (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL,
  7391. (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs
  7392. : NULL);
  7393. }
  7394. if (!mz_zip_writer_create_local_dir_header(
  7395. pZip, local_dir_header, (mz_uint16)archive_name_size,
  7396. (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method,
  7397. bit_flags, dos_time, dos_date))
  7398. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  7399. if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs,
  7400. local_dir_header,
  7401. sizeof(local_dir_header)) != sizeof(local_dir_header))
  7402. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7403. cur_archive_file_ofs += sizeof(local_dir_header);
  7404. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name,
  7405. archive_name_size) != archive_name_size) {
  7406. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  7407. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7408. }
  7409. cur_archive_file_ofs += archive_name_size;
  7410. if (pExtra_data != NULL) {
  7411. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data,
  7412. extra_size) != extra_size)
  7413. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7414. cur_archive_file_ofs += extra_size;
  7415. }
  7416. } else {
  7417. if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
  7418. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  7419. if (!mz_zip_writer_create_local_dir_header(
  7420. pZip, local_dir_header, (mz_uint16)archive_name_size,
  7421. (mz_uint16)user_extra_data_len, 0, 0, 0, method, bit_flags,
  7422. dos_time, dos_date))
  7423. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  7424. if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs,
  7425. local_dir_header,
  7426. sizeof(local_dir_header)) != sizeof(local_dir_header))
  7427. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7428. cur_archive_file_ofs += sizeof(local_dir_header);
  7429. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name,
  7430. archive_name_size) != archive_name_size) {
  7431. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  7432. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7433. }
  7434. cur_archive_file_ofs += archive_name_size;
  7435. }
  7436. if (user_extra_data_len > 0) {
  7437. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs,
  7438. user_extra_data,
  7439. user_extra_data_len) != user_extra_data_len)
  7440. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7441. cur_archive_file_ofs += user_extra_data_len;
  7442. }
  7443. if (store_data_uncompressed) {
  7444. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf,
  7445. buf_size) != buf_size) {
  7446. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  7447. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7448. }
  7449. cur_archive_file_ofs += buf_size;
  7450. comp_size = buf_size;
  7451. } else if (buf_size) {
  7452. mz_zip_writer_add_state state;
  7453. state.m_pZip = pZip;
  7454. state.m_cur_archive_file_ofs = cur_archive_file_ofs;
  7455. state.m_comp_size = 0;
  7456. if ((tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state,
  7457. tdefl_create_comp_flags_from_zip_params(
  7458. level, -15, MZ_DEFAULT_STRATEGY)) !=
  7459. TDEFL_STATUS_OKAY) ||
  7460. (tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) !=
  7461. TDEFL_STATUS_DONE)) {
  7462. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  7463. return mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);
  7464. }
  7465. comp_size = state.m_comp_size;
  7466. cur_archive_file_ofs = state.m_cur_archive_file_ofs;
  7467. }
  7468. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  7469. pComp = NULL;
  7470. if (uncomp_size) {
  7471. mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];
  7472. mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32;
  7473. MZ_ASSERT(bit_flags & MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR);
  7474. MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);
  7475. MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);
  7476. if (pExtra_data == NULL) {
  7477. if (comp_size > MZ_UINT32_MAX)
  7478. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  7479. MZ_WRITE_LE32(local_dir_footer + 8, comp_size);
  7480. MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size);
  7481. } else {
  7482. MZ_WRITE_LE64(local_dir_footer + 8, comp_size);
  7483. MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size);
  7484. local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;
  7485. }
  7486. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs,
  7487. local_dir_footer,
  7488. local_dir_footer_size) != local_dir_footer_size)
  7489. return MZ_FALSE;
  7490. cur_archive_file_ofs += local_dir_footer_size;
  7491. }
  7492. if (pExtra_data != NULL) {
  7493. extra_size = mz_zip_writer_create_zip64_extra_data(
  7494. extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
  7495. (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL,
  7496. (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
  7497. }
  7498. if (!mz_zip_writer_add_to_central_dir(
  7499. pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data,
  7500. (mz_uint16)extra_size, pComment, comment_size, uncomp_size, comp_size,
  7501. uncomp_crc32, method, bit_flags, dos_time, dos_date,
  7502. local_dir_header_ofs, ext_attributes, user_extra_data_central,
  7503. user_extra_data_central_len))
  7504. return MZ_FALSE;
  7505. pZip->m_total_files++;
  7506. pZip->m_archive_size = cur_archive_file_ofs;
  7507. return MZ_TRUE;
  7508. }
  7509. mz_bool mz_zip_writer_add_read_buf_callback(
  7510. mz_zip_archive *pZip, const char *pArchive_name,
  7511. mz_file_read_func read_callback, void *callback_opaque, mz_uint64 max_size,
  7512. const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size,
  7513. mz_uint level_and_flags, mz_uint32 ext_attributes,
  7514. const char *user_extra_data, mz_uint user_extra_data_len,
  7515. const char *user_extra_data_central, mz_uint user_extra_data_central_len) {
  7516. mz_uint16 gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)
  7517. ? 0
  7518. : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;
  7519. mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes;
  7520. mz_uint16 method = 0, dos_time = 0, dos_date = 0;
  7521. mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = 0, uncomp_size = 0,
  7522. comp_size = 0;
  7523. size_t archive_name_size;
  7524. mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
  7525. mz_uint8 *pExtra_data = NULL;
  7526. mz_uint32 extra_size = 0;
  7527. mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
  7528. mz_zip_internal_state *pState;
  7529. mz_uint64 file_ofs = 0, cur_archive_header_file_ofs;
  7530. if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
  7531. gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8;
  7532. if ((int)level_and_flags < 0)
  7533. level_and_flags = MZ_DEFAULT_LEVEL;
  7534. level = level_and_flags & 0xF;
  7535. /* Sanity checks */
  7536. if ((!pZip) || (!pZip->m_pState) ||
  7537. (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) ||
  7538. ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION))
  7539. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7540. pState = pZip->m_pState;
  7541. cur_archive_file_ofs = pZip->m_archive_size;
  7542. if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) {
  7543. /* Source file is too large for non-zip64 */
  7544. /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
  7545. pState->m_zip64 = MZ_TRUE;
  7546. }
  7547. /* We could support this, but why? */
  7548. if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)
  7549. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7550. if (!mz_zip_writer_validate_archive_name(pArchive_name))
  7551. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
  7552. if (pState->m_zip64) {
  7553. if (pZip->m_total_files == MZ_UINT32_MAX)
  7554. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  7555. } else {
  7556. if (pZip->m_total_files == MZ_UINT16_MAX) {
  7557. pState->m_zip64 = MZ_TRUE;
  7558. /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */
  7559. }
  7560. }
  7561. archive_name_size = strlen(pArchive_name);
  7562. if (archive_name_size > MZ_UINT16_MAX)
  7563. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
  7564. num_alignment_padding_bytes =
  7565. mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
  7566. /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
  7567. if (((mz_uint64)pState->m_central_dir.m_size +
  7568. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size +
  7569. MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX)
  7570. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  7571. if (!pState->m_zip64) {
  7572. /* Bail early if the archive would obviously become too large */
  7573. if ((pZip->m_archive_size + num_alignment_padding_bytes +
  7574. MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size +
  7575. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size +
  7576. user_extra_data_len + pState->m_central_dir.m_size +
  7577. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 1024 +
  7578. MZ_ZIP_DATA_DESCRIPTER_SIZE32 + user_extra_data_central_len) >
  7579. 0xFFFFFFFF) {
  7580. pState->m_zip64 = MZ_TRUE;
  7581. /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
  7582. }
  7583. }
  7584. #ifndef MINIZ_NO_TIME
  7585. if (pFile_time) {
  7586. mz_zip_time_t_to_dos_time(*pFile_time, &dos_time, &dos_date);
  7587. }
  7588. #endif
  7589. if (max_size <= 3)
  7590. level = 0;
  7591. if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs,
  7592. num_alignment_padding_bytes)) {
  7593. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7594. }
  7595. cur_archive_file_ofs += num_alignment_padding_bytes;
  7596. local_dir_header_ofs = cur_archive_file_ofs;
  7597. if (pZip->m_file_offset_alignment) {
  7598. MZ_ASSERT((cur_archive_file_ofs & (pZip->m_file_offset_alignment - 1)) ==
  7599. 0);
  7600. }
  7601. if (max_size && level) {
  7602. method = MZ_DEFLATED;
  7603. }
  7604. MZ_CLEAR_OBJ(local_dir_header);
  7605. if (pState->m_zip64) {
  7606. if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) {
  7607. pExtra_data = extra_data;
  7608. if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)
  7609. extra_size = mz_zip_writer_create_zip64_extra_data(
  7610. extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
  7611. (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL,
  7612. (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs
  7613. : NULL);
  7614. else
  7615. extra_size = mz_zip_writer_create_zip64_extra_data(
  7616. extra_data, NULL, NULL,
  7617. (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs
  7618. : NULL);
  7619. }
  7620. if (!mz_zip_writer_create_local_dir_header(
  7621. pZip, local_dir_header, (mz_uint16)archive_name_size,
  7622. (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method,
  7623. gen_flags, dos_time, dos_date))
  7624. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  7625. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs,
  7626. local_dir_header,
  7627. sizeof(local_dir_header)) != sizeof(local_dir_header))
  7628. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7629. cur_archive_file_ofs += sizeof(local_dir_header);
  7630. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name,
  7631. archive_name_size) != archive_name_size) {
  7632. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7633. }
  7634. cur_archive_file_ofs += archive_name_size;
  7635. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data,
  7636. extra_size) != extra_size)
  7637. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7638. cur_archive_file_ofs += extra_size;
  7639. } else {
  7640. if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
  7641. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  7642. if (!mz_zip_writer_create_local_dir_header(
  7643. pZip, local_dir_header, (mz_uint16)archive_name_size,
  7644. (mz_uint16)user_extra_data_len, 0, 0, 0, method, gen_flags,
  7645. dos_time, dos_date))
  7646. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  7647. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs,
  7648. local_dir_header,
  7649. sizeof(local_dir_header)) != sizeof(local_dir_header))
  7650. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7651. cur_archive_file_ofs += sizeof(local_dir_header);
  7652. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name,
  7653. archive_name_size) != archive_name_size) {
  7654. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7655. }
  7656. cur_archive_file_ofs += archive_name_size;
  7657. }
  7658. if (user_extra_data_len > 0) {
  7659. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs,
  7660. user_extra_data,
  7661. user_extra_data_len) != user_extra_data_len)
  7662. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7663. cur_archive_file_ofs += user_extra_data_len;
  7664. }
  7665. if (max_size) {
  7666. void *pRead_buf =
  7667. pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE);
  7668. if (!pRead_buf) {
  7669. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  7670. }
  7671. if (!level) {
  7672. while (1) {
  7673. size_t n = read_callback(callback_opaque, file_ofs, pRead_buf,
  7674. MZ_ZIP_MAX_IO_BUF_SIZE);
  7675. if (n == 0)
  7676. break;
  7677. if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) {
  7678. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  7679. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  7680. }
  7681. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf,
  7682. n) != n) {
  7683. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  7684. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7685. }
  7686. file_ofs += n;
  7687. uncomp_crc32 =
  7688. (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n);
  7689. cur_archive_file_ofs += n;
  7690. }
  7691. uncomp_size = file_ofs;
  7692. comp_size = uncomp_size;
  7693. } else {
  7694. mz_bool result = MZ_FALSE;
  7695. mz_zip_writer_add_state state;
  7696. tdefl_compressor *pComp = (tdefl_compressor *)pZip->m_pAlloc(
  7697. pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor));
  7698. if (!pComp) {
  7699. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  7700. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  7701. }
  7702. state.m_pZip = pZip;
  7703. state.m_cur_archive_file_ofs = cur_archive_file_ofs;
  7704. state.m_comp_size = 0;
  7705. if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state,
  7706. tdefl_create_comp_flags_from_zip_params(
  7707. level, -15, MZ_DEFAULT_STRATEGY)) !=
  7708. TDEFL_STATUS_OKAY) {
  7709. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  7710. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  7711. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  7712. }
  7713. for (;;) {
  7714. tdefl_status status;
  7715. tdefl_flush flush = TDEFL_NO_FLUSH;
  7716. size_t n = read_callback(callback_opaque, file_ofs, pRead_buf,
  7717. MZ_ZIP_MAX_IO_BUF_SIZE);
  7718. if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) {
  7719. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  7720. break;
  7721. }
  7722. file_ofs += n;
  7723. uncomp_crc32 =
  7724. (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n);
  7725. if (pZip->m_pNeeds_keepalive != NULL &&
  7726. pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque))
  7727. flush = TDEFL_FULL_FLUSH;
  7728. if (n == 0)
  7729. flush = TDEFL_FINISH;
  7730. status = tdefl_compress_buffer(pComp, pRead_buf, n, flush);
  7731. if (status == TDEFL_STATUS_DONE) {
  7732. result = MZ_TRUE;
  7733. break;
  7734. } else if (status != TDEFL_STATUS_OKAY) {
  7735. mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);
  7736. break;
  7737. }
  7738. }
  7739. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  7740. if (!result) {
  7741. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  7742. return MZ_FALSE;
  7743. }
  7744. uncomp_size = file_ofs;
  7745. comp_size = state.m_comp_size;
  7746. cur_archive_file_ofs = state.m_cur_archive_file_ofs;
  7747. }
  7748. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  7749. }
  7750. if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)) {
  7751. mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];
  7752. mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32;
  7753. MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);
  7754. MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);
  7755. if (pExtra_data == NULL) {
  7756. if (comp_size > MZ_UINT32_MAX)
  7757. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  7758. MZ_WRITE_LE32(local_dir_footer + 8, comp_size);
  7759. MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size);
  7760. } else {
  7761. MZ_WRITE_LE64(local_dir_footer + 8, comp_size);
  7762. MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size);
  7763. local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;
  7764. }
  7765. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs,
  7766. local_dir_footer,
  7767. local_dir_footer_size) != local_dir_footer_size)
  7768. return MZ_FALSE;
  7769. cur_archive_file_ofs += local_dir_footer_size;
  7770. }
  7771. if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) {
  7772. if (pExtra_data != NULL) {
  7773. extra_size = mz_zip_writer_create_zip64_extra_data(
  7774. extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
  7775. (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL,
  7776. (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs
  7777. : NULL);
  7778. }
  7779. if (!mz_zip_writer_create_local_dir_header(
  7780. pZip, local_dir_header, (mz_uint16)archive_name_size,
  7781. (mz_uint16)(extra_size + user_extra_data_len),
  7782. (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : uncomp_size,
  7783. (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : comp_size,
  7784. uncomp_crc32, method, gen_flags, dos_time, dos_date))
  7785. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  7786. cur_archive_header_file_ofs = local_dir_header_ofs;
  7787. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs,
  7788. local_dir_header,
  7789. sizeof(local_dir_header)) != sizeof(local_dir_header))
  7790. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7791. if (pExtra_data != NULL) {
  7792. cur_archive_header_file_ofs += sizeof(local_dir_header);
  7793. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs,
  7794. pArchive_name,
  7795. archive_name_size) != archive_name_size) {
  7796. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7797. }
  7798. cur_archive_header_file_ofs += archive_name_size;
  7799. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs,
  7800. extra_data, extra_size) != extra_size)
  7801. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  7802. cur_archive_header_file_ofs += extra_size;
  7803. }
  7804. }
  7805. if (pExtra_data != NULL) {
  7806. extra_size = mz_zip_writer_create_zip64_extra_data(
  7807. extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
  7808. (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL,
  7809. (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
  7810. }
  7811. if (!mz_zip_writer_add_to_central_dir(
  7812. pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data,
  7813. (mz_uint16)extra_size, pComment, comment_size, uncomp_size, comp_size,
  7814. uncomp_crc32, method, gen_flags, dos_time, dos_date,
  7815. local_dir_header_ofs, ext_attributes, user_extra_data_central,
  7816. user_extra_data_central_len))
  7817. return MZ_FALSE;
  7818. pZip->m_total_files++;
  7819. pZip->m_archive_size = cur_archive_file_ofs;
  7820. return MZ_TRUE;
  7821. }
  7822. #ifndef MINIZ_NO_STDIO
  7823. static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs,
  7824. void *pBuf, size_t n) {
  7825. MZ_FILE *pSrc_file = (MZ_FILE *)pOpaque;
  7826. mz_int64 cur_ofs = MZ_FTELL64(pSrc_file);
  7827. if (((mz_int64)file_ofs < 0) ||
  7828. (((cur_ofs != (mz_int64)file_ofs)) &&
  7829. (MZ_FSEEK64(pSrc_file, (long)file_ofs, SEEK_SET))))
  7830. return 0;
  7831. return MZ_FREAD(pBuf, 1, n, pSrc_file);
  7832. }
  7833. mz_bool mz_zip_writer_add_cfile(
  7834. mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file,
  7835. mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment,
  7836. mz_uint16 comment_size, mz_uint level_and_flags, mz_uint32 ext_attributes,
  7837. const char *user_extra_data, mz_uint user_extra_data_len,
  7838. const char *user_extra_data_central, mz_uint user_extra_data_central_len) {
  7839. return mz_zip_writer_add_read_buf_callback(
  7840. pZip, pArchive_name, mz_file_read_func_stdio, pSrc_file, max_size,
  7841. pFile_time, pComment, comment_size, level_and_flags, ext_attributes,
  7842. user_extra_data, user_extra_data_len, user_extra_data_central,
  7843. user_extra_data_central_len);
  7844. }
  7845. mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name,
  7846. const char *pSrc_filename, const void *pComment,
  7847. mz_uint16 comment_size, mz_uint level_and_flags,
  7848. mz_uint32 ext_attributes) {
  7849. MZ_FILE *pSrc_file = NULL;
  7850. mz_uint64 uncomp_size = 0;
  7851. MZ_TIME_T file_modified_time;
  7852. MZ_TIME_T *pFile_time = NULL;
  7853. mz_bool status;
  7854. memset(&file_modified_time, 0, sizeof(file_modified_time));
  7855. #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
  7856. pFile_time = &file_modified_time;
  7857. if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time))
  7858. return mz_zip_set_error(pZip, MZ_ZIP_FILE_STAT_FAILED);
  7859. #endif
  7860. pSrc_file = MZ_FOPEN(pSrc_filename, "rb");
  7861. if (!pSrc_file)
  7862. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  7863. MZ_FSEEK64(pSrc_file, 0, SEEK_END);
  7864. uncomp_size = MZ_FTELL64(pSrc_file);
  7865. MZ_FSEEK64(pSrc_file, 0, SEEK_SET);
  7866. status = mz_zip_writer_add_cfile(
  7867. pZip, pArchive_name, pSrc_file, uncomp_size, pFile_time, pComment,
  7868. comment_size, level_and_flags, ext_attributes, NULL, 0, NULL, 0);
  7869. MZ_FCLOSE(pSrc_file);
  7870. return status;
  7871. }
  7872. #endif /* #ifndef MINIZ_NO_STDIO */
  7873. static mz_bool mz_zip_writer_update_zip64_extension_block(
  7874. mz_zip_array *pNew_ext, mz_zip_archive *pZip, const mz_uint8 *pExt,
  7875. uint32_t ext_len, mz_uint64 *pComp_size, mz_uint64 *pUncomp_size,
  7876. mz_uint64 *pLocal_header_ofs, mz_uint32 *pDisk_start) {
  7877. /* + 64 should be enough for any new zip64 data */
  7878. if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE))
  7879. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  7880. mz_zip_array_resize(pZip, pNew_ext, 0, MZ_FALSE);
  7881. if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start)) {
  7882. mz_uint8 new_ext_block[64];
  7883. mz_uint8 *pDst = new_ext_block;
  7884. mz_write_le16(pDst, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID);
  7885. mz_write_le16(pDst + sizeof(mz_uint16), 0);
  7886. pDst += sizeof(mz_uint16) * 2;
  7887. if (pUncomp_size) {
  7888. mz_write_le64(pDst, *pUncomp_size);
  7889. pDst += sizeof(mz_uint64);
  7890. }
  7891. if (pComp_size) {
  7892. mz_write_le64(pDst, *pComp_size);
  7893. pDst += sizeof(mz_uint64);
  7894. }
  7895. if (pLocal_header_ofs) {
  7896. mz_write_le64(pDst, *pLocal_header_ofs);
  7897. pDst += sizeof(mz_uint64);
  7898. }
  7899. if (pDisk_start) {
  7900. mz_write_le32(pDst, *pDisk_start);
  7901. pDst += sizeof(mz_uint32);
  7902. }
  7903. mz_write_le16(new_ext_block + sizeof(mz_uint16),
  7904. (mz_uint16)((pDst - new_ext_block) - sizeof(mz_uint16) * 2));
  7905. if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block,
  7906. pDst - new_ext_block))
  7907. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  7908. }
  7909. if ((pExt) && (ext_len)) {
  7910. mz_uint32 extra_size_remaining = ext_len;
  7911. const mz_uint8 *pExtra_data = pExt;
  7912. do {
  7913. mz_uint32 field_id, field_data_size, field_total_size;
  7914. if (extra_size_remaining < (sizeof(mz_uint16) * 2))
  7915. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  7916. field_id = MZ_READ_LE16(pExtra_data);
  7917. field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  7918. field_total_size = field_data_size + sizeof(mz_uint16) * 2;
  7919. if (field_total_size > extra_size_remaining)
  7920. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  7921. if (field_id != MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) {
  7922. if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data,
  7923. field_total_size))
  7924. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  7925. }
  7926. pExtra_data += field_total_size;
  7927. extra_size_remaining -= field_total_size;
  7928. } while (extra_size_remaining);
  7929. }
  7930. return MZ_TRUE;
  7931. }
  7932. /* TODO: This func is now pretty freakin complex due to zip64, split it up? */
  7933. mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip,
  7934. mz_zip_archive *pSource_zip,
  7935. mz_uint src_file_index) {
  7936. mz_uint n, bit_flags, num_alignment_padding_bytes,
  7937. src_central_dir_following_data_size;
  7938. mz_uint64 src_archive_bytes_remaining, local_dir_header_ofs;
  7939. mz_uint64 cur_src_file_ofs, cur_dst_file_ofs;
  7940. mz_uint32
  7941. local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) /
  7942. sizeof(mz_uint32)];
  7943. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  7944. mz_uint8 new_central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];
  7945. size_t orig_central_dir_size;
  7946. mz_zip_internal_state *pState;
  7947. void *pBuf;
  7948. const mz_uint8 *pSrc_central_header;
  7949. mz_zip_archive_file_stat src_file_stat;
  7950. mz_uint32 src_filename_len, src_comment_len, src_ext_len;
  7951. mz_uint32 local_header_filename_size, local_header_extra_len;
  7952. mz_uint64 local_header_comp_size, local_header_uncomp_size;
  7953. mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;
  7954. /* Sanity checks */
  7955. if ((!pZip) || (!pZip->m_pState) ||
  7956. (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead))
  7957. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7958. pState = pZip->m_pState;
  7959. /* Don't support copying files from zip64 archives to non-zip64, even though
  7960. * in some cases this is possible */
  7961. if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64))
  7962. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7963. /* Get pointer to the source central dir header and crack it */
  7964. if (NULL ==
  7965. (pSrc_central_header = mz_zip_get_cdh(pSource_zip, src_file_index)))
  7966. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  7967. if (MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_SIG_OFS) !=
  7968. MZ_ZIP_CENTRAL_DIR_HEADER_SIG)
  7969. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  7970. src_filename_len =
  7971. MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  7972. src_comment_len =
  7973. MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS);
  7974. src_ext_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS);
  7975. src_central_dir_following_data_size =
  7976. src_filename_len + src_ext_len + src_comment_len;
  7977. /* TODO: We don't support central dir's >= MZ_UINT32_MAX bytes right now (+32
  7978. * fudge factor in case we need to add more extra data) */
  7979. if ((pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
  7980. src_central_dir_following_data_size + 32) >= MZ_UINT32_MAX)
  7981. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  7982. num_alignment_padding_bytes =
  7983. mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
  7984. if (!pState->m_zip64) {
  7985. if (pZip->m_total_files == MZ_UINT16_MAX)
  7986. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  7987. } else {
  7988. /* TODO: Our zip64 support still has some 32-bit limits that may not be
  7989. * worth fixing. */
  7990. if (pZip->m_total_files == MZ_UINT32_MAX)
  7991. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  7992. }
  7993. if (!mz_zip_file_stat_internal(pSource_zip, src_file_index,
  7994. pSrc_central_header, &src_file_stat, NULL))
  7995. return MZ_FALSE;
  7996. cur_src_file_ofs = src_file_stat.m_local_header_ofs;
  7997. cur_dst_file_ofs = pZip->m_archive_size;
  7998. /* Read the source archive's local dir header */
  7999. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs,
  8000. pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) !=
  8001. MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  8002. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  8003. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
  8004. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  8005. cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;
  8006. /* Compute the total size we need to copy (filename+extra data+compressed
  8007. * data) */
  8008. local_header_filename_size =
  8009. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);
  8010. local_header_extra_len =
  8011. MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  8012. local_header_comp_size =
  8013. MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);
  8014. local_header_uncomp_size =
  8015. MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);
  8016. src_archive_bytes_remaining = local_header_filename_size +
  8017. local_header_extra_len +
  8018. src_file_stat.m_comp_size;
  8019. /* Try to find a zip64 extended information field */
  8020. if ((local_header_extra_len) &&
  8021. ((local_header_comp_size == MZ_UINT32_MAX) ||
  8022. (local_header_uncomp_size == MZ_UINT32_MAX))) {
  8023. mz_zip_array file_data_array;
  8024. const mz_uint8 *pExtra_data;
  8025. mz_uint32 extra_size_remaining = local_header_extra_len;
  8026. mz_zip_array_init(&file_data_array, 1);
  8027. if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len,
  8028. MZ_FALSE)) {
  8029. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8030. }
  8031. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque,
  8032. src_file_stat.m_local_header_ofs +
  8033. MZ_ZIP_LOCAL_DIR_HEADER_SIZE +
  8034. local_header_filename_size,
  8035. file_data_array.m_p, local_header_extra_len) !=
  8036. local_header_extra_len) {
  8037. mz_zip_array_clear(pZip, &file_data_array);
  8038. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  8039. }
  8040. pExtra_data = (const mz_uint8 *)file_data_array.m_p;
  8041. do {
  8042. mz_uint32 field_id, field_data_size, field_total_size;
  8043. if (extra_size_remaining < (sizeof(mz_uint16) * 2)) {
  8044. mz_zip_array_clear(pZip, &file_data_array);
  8045. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  8046. }
  8047. field_id = MZ_READ_LE16(pExtra_data);
  8048. field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  8049. field_total_size = field_data_size + sizeof(mz_uint16) * 2;
  8050. if (field_total_size > extra_size_remaining) {
  8051. mz_zip_array_clear(pZip, &file_data_array);
  8052. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  8053. }
  8054. if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) {
  8055. const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32);
  8056. if (field_data_size < sizeof(mz_uint64) * 2) {
  8057. mz_zip_array_clear(pZip, &file_data_array);
  8058. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  8059. }
  8060. local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);
  8061. local_header_comp_size = MZ_READ_LE64(
  8062. pSrc_field_data +
  8063. sizeof(mz_uint64)); /* may be 0 if there's a descriptor */
  8064. found_zip64_ext_data_in_ldir = MZ_TRUE;
  8065. break;
  8066. }
  8067. pExtra_data += field_total_size;
  8068. extra_size_remaining -= field_total_size;
  8069. } while (extra_size_remaining);
  8070. mz_zip_array_clear(pZip, &file_data_array);
  8071. }
  8072. if (!pState->m_zip64) {
  8073. /* Try to detect if the new archive will most likely wind up too big and
  8074. * bail early (+(sizeof(mz_uint32) * 4) is for the optional descriptor which
  8075. * could be present, +64 is a fudge factor). */
  8076. /* We also check when the archive is finalized so this doesn't need to be
  8077. * perfect. */
  8078. mz_uint64 approx_new_archive_size =
  8079. cur_dst_file_ofs + num_alignment_padding_bytes +
  8080. MZ_ZIP_LOCAL_DIR_HEADER_SIZE + src_archive_bytes_remaining +
  8081. (sizeof(mz_uint32) * 4) + pState->m_central_dir.m_size +
  8082. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size +
  8083. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 64;
  8084. if (approx_new_archive_size >= MZ_UINT32_MAX)
  8085. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  8086. }
  8087. /* Write dest archive padding */
  8088. if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs,
  8089. num_alignment_padding_bytes))
  8090. return MZ_FALSE;
  8091. cur_dst_file_ofs += num_alignment_padding_bytes;
  8092. local_dir_header_ofs = cur_dst_file_ofs;
  8093. if (pZip->m_file_offset_alignment) {
  8094. MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) ==
  8095. 0);
  8096. }
  8097. /* The original zip's local header+ext block doesn't change, even with zip64,
  8098. * so we can just copy it over to the dest zip */
  8099. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header,
  8100. MZ_ZIP_LOCAL_DIR_HEADER_SIZE) !=
  8101. MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  8102. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  8103. cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;
  8104. /* Copy over the source archive bytes to the dest archive, also ensure we have
  8105. * enough buf space to handle optional data descriptor */
  8106. if (NULL == (pBuf = pZip->m_pAlloc(
  8107. pZip->m_pAlloc_opaque, 1,
  8108. (size_t)MZ_MAX(32U, MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE,
  8109. src_archive_bytes_remaining)))))
  8110. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8111. while (src_archive_bytes_remaining) {
  8112. n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE,
  8113. src_archive_bytes_remaining);
  8114. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf,
  8115. n) != n) {
  8116. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  8117. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  8118. }
  8119. cur_src_file_ofs += n;
  8120. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) {
  8121. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  8122. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  8123. }
  8124. cur_dst_file_ofs += n;
  8125. src_archive_bytes_remaining -= n;
  8126. }
  8127. /* Now deal with the optional data descriptor */
  8128. bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);
  8129. if (bit_flags & 8) {
  8130. /* Copy data descriptor */
  8131. if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir)) {
  8132. /* src is zip64, dest must be zip64 */
  8133. /* name uint32_t's */
  8134. /* id 1 (optional in zip64?) */
  8135. /* crc 1 */
  8136. /* comp_size 2 */
  8137. /* uncomp_size 2 */
  8138. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs,
  8139. pBuf, (sizeof(mz_uint32) * 6)) !=
  8140. (sizeof(mz_uint32) * 6)) {
  8141. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  8142. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  8143. }
  8144. n = sizeof(mz_uint32) *
  8145. ((MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID) ? 6 : 5);
  8146. } else {
  8147. /* src is NOT zip64 */
  8148. mz_bool has_id;
  8149. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs,
  8150. pBuf, sizeof(mz_uint32) * 4) !=
  8151. sizeof(mz_uint32) * 4) {
  8152. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  8153. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  8154. }
  8155. has_id = (MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
  8156. if (pZip->m_pState->m_zip64) {
  8157. /* dest is zip64, so upgrade the data descriptor */
  8158. const mz_uint32 *pSrc_descriptor =
  8159. (const mz_uint32 *)((const mz_uint8 *)pBuf +
  8160. (has_id ? sizeof(mz_uint32) : 0));
  8161. const mz_uint32 src_crc32 = pSrc_descriptor[0];
  8162. const mz_uint64 src_comp_size = pSrc_descriptor[1];
  8163. const mz_uint64 src_uncomp_size = pSrc_descriptor[2];
  8164. mz_write_le32((mz_uint8 *)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID);
  8165. mz_write_le32((mz_uint8 *)pBuf + sizeof(mz_uint32) * 1, src_crc32);
  8166. mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 2, src_comp_size);
  8167. mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 4,
  8168. src_uncomp_size);
  8169. n = sizeof(mz_uint32) * 6;
  8170. } else {
  8171. /* dest is NOT zip64, just copy it as-is */
  8172. n = sizeof(mz_uint32) * (has_id ? 4 : 3);
  8173. }
  8174. }
  8175. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) {
  8176. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  8177. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  8178. }
  8179. cur_src_file_ofs += n;
  8180. cur_dst_file_ofs += n;
  8181. }
  8182. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  8183. /* Finally, add the new central dir header */
  8184. orig_central_dir_size = pState->m_central_dir.m_size;
  8185. memcpy(new_central_header, pSrc_central_header,
  8186. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);
  8187. if (pState->m_zip64) {
  8188. /* This is the painful part: We need to write a new central dir header + ext
  8189. * block with updated zip64 fields, and ensure the old fields (if any) are
  8190. * not included. */
  8191. const mz_uint8 *pSrc_ext =
  8192. pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len;
  8193. mz_zip_array new_ext_block;
  8194. mz_zip_array_init(&new_ext_block, sizeof(mz_uint8));
  8195. MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS,
  8196. MZ_UINT32_MAX);
  8197. MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS,
  8198. MZ_UINT32_MAX);
  8199. MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS,
  8200. MZ_UINT32_MAX);
  8201. if (!mz_zip_writer_update_zip64_extension_block(
  8202. &new_ext_block, pZip, pSrc_ext, src_ext_len,
  8203. &src_file_stat.m_comp_size, &src_file_stat.m_uncomp_size,
  8204. &local_dir_header_ofs, NULL)) {
  8205. mz_zip_array_clear(pZip, &new_ext_block);
  8206. return MZ_FALSE;
  8207. }
  8208. MZ_WRITE_LE16(new_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS,
  8209. new_ext_block.m_size);
  8210. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir,
  8211. new_central_header,
  8212. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) {
  8213. mz_zip_array_clear(pZip, &new_ext_block);
  8214. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8215. }
  8216. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir,
  8217. pSrc_central_header +
  8218. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE,
  8219. src_filename_len)) {
  8220. mz_zip_array_clear(pZip, &new_ext_block);
  8221. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size,
  8222. MZ_FALSE);
  8223. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8224. }
  8225. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p,
  8226. new_ext_block.m_size)) {
  8227. mz_zip_array_clear(pZip, &new_ext_block);
  8228. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size,
  8229. MZ_FALSE);
  8230. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8231. }
  8232. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir,
  8233. pSrc_central_header +
  8234. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
  8235. src_filename_len + src_ext_len,
  8236. src_comment_len)) {
  8237. mz_zip_array_clear(pZip, &new_ext_block);
  8238. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size,
  8239. MZ_FALSE);
  8240. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8241. }
  8242. mz_zip_array_clear(pZip, &new_ext_block);
  8243. } else {
  8244. /* sanity checks */
  8245. if (cur_dst_file_ofs > MZ_UINT32_MAX)
  8246. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  8247. if (local_dir_header_ofs >= MZ_UINT32_MAX)
  8248. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  8249. MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS,
  8250. local_dir_header_ofs);
  8251. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir,
  8252. new_central_header,
  8253. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE))
  8254. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8255. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir,
  8256. pSrc_central_header +
  8257. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE,
  8258. src_central_dir_following_data_size)) {
  8259. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size,
  8260. MZ_FALSE);
  8261. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8262. }
  8263. }
  8264. /* This shouldn't trigger unless we screwed up during the initial sanity
  8265. * checks */
  8266. if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) {
  8267. /* TODO: Support central dirs >= 32-bits in size */
  8268. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size,
  8269. MZ_FALSE);
  8270. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  8271. }
  8272. n = (mz_uint32)orig_central_dir_size;
  8273. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1)) {
  8274. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size,
  8275. MZ_FALSE);
  8276. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  8277. }
  8278. pZip->m_total_files++;
  8279. pZip->m_archive_size = cur_dst_file_ofs;
  8280. return MZ_TRUE;
  8281. }
  8282. mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) {
  8283. mz_zip_internal_state *pState;
  8284. mz_uint64 central_dir_ofs, central_dir_size;
  8285. mz_uint8 hdr[256];
  8286. if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING))
  8287. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  8288. pState = pZip->m_pState;
  8289. if (pState->m_zip64) {
  8290. if ((pZip->m_total_files > MZ_UINT32_MAX) ||
  8291. (pState->m_central_dir.m_size >= MZ_UINT32_MAX))
  8292. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  8293. } else {
  8294. if ((pZip->m_total_files > MZ_UINT16_MAX) ||
  8295. ((pZip->m_archive_size + pState->m_central_dir.m_size +
  8296. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX))
  8297. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  8298. }
  8299. central_dir_ofs = 0;
  8300. central_dir_size = 0;
  8301. if (pZip->m_total_files) {
  8302. /* Write central directory */
  8303. central_dir_ofs = pZip->m_archive_size;
  8304. central_dir_size = pState->m_central_dir.m_size;
  8305. pZip->m_central_directory_file_ofs = central_dir_ofs;
  8306. if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs,
  8307. pState->m_central_dir.m_p,
  8308. (size_t)central_dir_size) != central_dir_size)
  8309. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  8310. pZip->m_archive_size += central_dir_size;
  8311. }
  8312. if (pState->m_zip64) {
  8313. /* Write zip64 end of central directory header */
  8314. mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size;
  8315. MZ_CLEAR_OBJ(hdr);
  8316. MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDH_SIG_OFS,
  8317. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG);
  8318. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS,
  8319. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - sizeof(mz_uint32) -
  8320. sizeof(mz_uint64));
  8321. MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS,
  8322. 0x031E); /* TODO: always Unix */
  8323. MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_NEEDED_OFS, 0x002D);
  8324. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS,
  8325. pZip->m_total_files);
  8326. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS,
  8327. pZip->m_total_files);
  8328. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_SIZE_OFS, central_dir_size);
  8329. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_OFS_OFS, central_dir_ofs);
  8330. if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr,
  8331. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) !=
  8332. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)
  8333. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  8334. pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE;
  8335. /* Write zip64 end of central directory locator */
  8336. MZ_CLEAR_OBJ(hdr);
  8337. MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_SIG_OFS,
  8338. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG);
  8339. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS,
  8340. rel_ofs_to_zip64_ecdr);
  8341. MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS, 1);
  8342. if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr,
  8343. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) !=
  8344. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE)
  8345. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  8346. pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE;
  8347. }
  8348. /* Write end of central directory record */
  8349. MZ_CLEAR_OBJ(hdr);
  8350. MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS,
  8351. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG);
  8352. MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS,
  8353. MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));
  8354. MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS,
  8355. MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));
  8356. MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS,
  8357. MZ_MIN(MZ_UINT32_MAX, central_dir_size));
  8358. MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS,
  8359. MZ_MIN(MZ_UINT32_MAX, central_dir_ofs));
  8360. if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr,
  8361. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) !=
  8362. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  8363. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  8364. #ifndef MINIZ_NO_STDIO
  8365. if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF))
  8366. return mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
  8367. #endif /* #ifndef MINIZ_NO_STDIO */
  8368. pZip->m_archive_size += MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE;
  8369. pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED;
  8370. return MZ_TRUE;
  8371. }
  8372. mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf,
  8373. size_t *pSize) {
  8374. if ((!ppBuf) || (!pSize))
  8375. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  8376. *ppBuf = NULL;
  8377. *pSize = 0;
  8378. if ((!pZip) || (!pZip->m_pState))
  8379. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  8380. if (pZip->m_pWrite != mz_zip_heap_write_func)
  8381. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  8382. if (!mz_zip_writer_finalize_archive(pZip))
  8383. return MZ_FALSE;
  8384. *ppBuf = pZip->m_pState->m_pMem;
  8385. *pSize = pZip->m_pState->m_mem_size;
  8386. pZip->m_pState->m_pMem = NULL;
  8387. pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0;
  8388. return MZ_TRUE;
  8389. }
  8390. mz_bool mz_zip_writer_end(mz_zip_archive *pZip) {
  8391. return mz_zip_writer_end_internal(pZip, MZ_TRUE);
  8392. }
  8393. #ifndef MINIZ_NO_STDIO
  8394. mz_bool mz_zip_add_mem_to_archive_file_in_place(
  8395. const char *pZip_filename, const char *pArchive_name, const void *pBuf,
  8396. size_t buf_size, const void *pComment, mz_uint16 comment_size,
  8397. mz_uint level_and_flags) {
  8398. return mz_zip_add_mem_to_archive_file_in_place_v2(
  8399. pZip_filename, pArchive_name, pBuf, buf_size, pComment, comment_size,
  8400. level_and_flags, NULL);
  8401. }
  8402. mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(
  8403. const char *pZip_filename, const char *pArchive_name, const void *pBuf,
  8404. size_t buf_size, const void *pComment, mz_uint16 comment_size,
  8405. mz_uint level_and_flags, mz_zip_error *pErr) {
  8406. mz_bool status, created_new_archive = MZ_FALSE;
  8407. mz_zip_archive zip_archive;
  8408. struct MZ_FILE_STAT_STRUCT file_stat;
  8409. mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
  8410. mz_zip_zero_struct(&zip_archive);
  8411. if ((int)level_and_flags < 0)
  8412. level_and_flags = MZ_DEFAULT_LEVEL;
  8413. if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) ||
  8414. ((comment_size) && (!pComment)) ||
  8415. ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION)) {
  8416. if (pErr)
  8417. *pErr = MZ_ZIP_INVALID_PARAMETER;
  8418. return MZ_FALSE;
  8419. }
  8420. if (!mz_zip_writer_validate_archive_name(pArchive_name)) {
  8421. if (pErr)
  8422. *pErr = MZ_ZIP_INVALID_FILENAME;
  8423. return MZ_FALSE;
  8424. }
  8425. /* Important: The regular non-64 bit version of stat() can fail here if the
  8426. * file is very large, which could cause the archive to be overwritten. */
  8427. /* So be sure to compile with _LARGEFILE64_SOURCE 1 */
  8428. if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0) {
  8429. /* Create a new archive. */
  8430. if (!mz_zip_writer_init_file_v2(&zip_archive, pZip_filename, 0,
  8431. level_and_flags)) {
  8432. if (pErr)
  8433. *pErr = zip_archive.m_last_error;
  8434. return MZ_FALSE;
  8435. }
  8436. created_new_archive = MZ_TRUE;
  8437. } else {
  8438. /* Append to an existing archive. */
  8439. if (!mz_zip_reader_init_file_v2(
  8440. &zip_archive, pZip_filename,
  8441. level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0,
  8442. 0)) {
  8443. if (pErr)
  8444. *pErr = zip_archive.m_last_error;
  8445. return MZ_FALSE;
  8446. }
  8447. if (!mz_zip_writer_init_from_reader_v2(&zip_archive, pZip_filename,
  8448. level_and_flags)) {
  8449. if (pErr)
  8450. *pErr = zip_archive.m_last_error;
  8451. mz_zip_reader_end_internal(&zip_archive, MZ_FALSE);
  8452. return MZ_FALSE;
  8453. }
  8454. }
  8455. status =
  8456. mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size,
  8457. pComment, comment_size, level_and_flags, 0, 0);
  8458. actual_err = zip_archive.m_last_error;
  8459. /* Always finalize, even if adding failed for some reason, so we have a valid
  8460. * central directory. (This may not always succeed, but we can try.) */
  8461. if (!mz_zip_writer_finalize_archive(&zip_archive)) {
  8462. if (!actual_err)
  8463. actual_err = zip_archive.m_last_error;
  8464. status = MZ_FALSE;
  8465. }
  8466. if (!mz_zip_writer_end_internal(&zip_archive, status)) {
  8467. if (!actual_err)
  8468. actual_err = zip_archive.m_last_error;
  8469. status = MZ_FALSE;
  8470. }
  8471. if ((!status) && (created_new_archive)) {
  8472. /* It's a new archive and something went wrong, so just delete it. */
  8473. int ignoredStatus = MZ_DELETE_FILE(pZip_filename);
  8474. (void)ignoredStatus;
  8475. }
  8476. if (pErr)
  8477. *pErr = actual_err;
  8478. return status;
  8479. }
  8480. void *mz_zip_extract_archive_file_to_heap_v2(const char *pZip_filename,
  8481. const char *pArchive_name,
  8482. const char *pComment,
  8483. size_t *pSize, mz_uint flags,
  8484. mz_zip_error *pErr) {
  8485. mz_uint32 file_index;
  8486. mz_zip_archive zip_archive;
  8487. void *p = NULL;
  8488. if (pSize)
  8489. *pSize = 0;
  8490. if ((!pZip_filename) || (!pArchive_name)) {
  8491. if (pErr)
  8492. *pErr = MZ_ZIP_INVALID_PARAMETER;
  8493. return NULL;
  8494. }
  8495. mz_zip_zero_struct(&zip_archive);
  8496. if (!mz_zip_reader_init_file_v2(
  8497. &zip_archive, pZip_filename,
  8498. flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) {
  8499. if (pErr)
  8500. *pErr = zip_archive.m_last_error;
  8501. return NULL;
  8502. }
  8503. if (mz_zip_reader_locate_file_v2(&zip_archive, pArchive_name, pComment, flags,
  8504. &file_index)) {
  8505. p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, pSize, flags);
  8506. }
  8507. mz_zip_reader_end_internal(&zip_archive, p != NULL);
  8508. if (pErr)
  8509. *pErr = zip_archive.m_last_error;
  8510. return p;
  8511. }
  8512. void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename,
  8513. const char *pArchive_name,
  8514. size_t *pSize, mz_uint flags) {
  8515. return mz_zip_extract_archive_file_to_heap_v2(pZip_filename, pArchive_name,
  8516. NULL, pSize, flags, NULL);
  8517. }
  8518. #endif /* #ifndef MINIZ_NO_STDIO */
  8519. #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */
  8520. /* ------------------- Misc utils */
  8521. mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip) {
  8522. return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID;
  8523. }
  8524. mz_zip_type mz_zip_get_type(mz_zip_archive *pZip) {
  8525. return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID;
  8526. }
  8527. mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num) {
  8528. mz_zip_error prev_err;
  8529. if (!pZip)
  8530. return MZ_ZIP_INVALID_PARAMETER;
  8531. prev_err = pZip->m_last_error;
  8532. pZip->m_last_error = err_num;
  8533. return prev_err;
  8534. }
  8535. mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip) {
  8536. if (!pZip)
  8537. return MZ_ZIP_INVALID_PARAMETER;
  8538. return pZip->m_last_error;
  8539. }
  8540. mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip) {
  8541. return mz_zip_set_last_error(pZip, MZ_ZIP_NO_ERROR);
  8542. }
  8543. mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip) {
  8544. mz_zip_error prev_err;
  8545. if (!pZip)
  8546. return MZ_ZIP_INVALID_PARAMETER;
  8547. prev_err = pZip->m_last_error;
  8548. pZip->m_last_error = MZ_ZIP_NO_ERROR;
  8549. return prev_err;
  8550. }
  8551. const char *mz_zip_get_error_string(mz_zip_error mz_err) {
  8552. switch (mz_err) {
  8553. case MZ_ZIP_NO_ERROR:
  8554. return "no error";
  8555. case MZ_ZIP_UNDEFINED_ERROR:
  8556. return "undefined error";
  8557. case MZ_ZIP_TOO_MANY_FILES:
  8558. return "too many files";
  8559. case MZ_ZIP_FILE_TOO_LARGE:
  8560. return "file too large";
  8561. case MZ_ZIP_UNSUPPORTED_METHOD:
  8562. return "unsupported method";
  8563. case MZ_ZIP_UNSUPPORTED_ENCRYPTION:
  8564. return "unsupported encryption";
  8565. case MZ_ZIP_UNSUPPORTED_FEATURE:
  8566. return "unsupported feature";
  8567. case MZ_ZIP_FAILED_FINDING_CENTRAL_DIR:
  8568. return "failed finding central directory";
  8569. case MZ_ZIP_NOT_AN_ARCHIVE:
  8570. return "not a ZIP archive";
  8571. case MZ_ZIP_INVALID_HEADER_OR_CORRUPTED:
  8572. return "invalid header or archive is corrupted";
  8573. case MZ_ZIP_UNSUPPORTED_MULTIDISK:
  8574. return "unsupported multidisk archive";
  8575. case MZ_ZIP_DECOMPRESSION_FAILED:
  8576. return "decompression failed or archive is corrupted";
  8577. case MZ_ZIP_COMPRESSION_FAILED:
  8578. return "compression failed";
  8579. case MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE:
  8580. return "unexpected decompressed size";
  8581. case MZ_ZIP_CRC_CHECK_FAILED:
  8582. return "CRC-32 check failed";
  8583. case MZ_ZIP_UNSUPPORTED_CDIR_SIZE:
  8584. return "unsupported central directory size";
  8585. case MZ_ZIP_ALLOC_FAILED:
  8586. return "allocation failed";
  8587. case MZ_ZIP_FILE_OPEN_FAILED:
  8588. return "file open failed";
  8589. case MZ_ZIP_FILE_CREATE_FAILED:
  8590. return "file create failed";
  8591. case MZ_ZIP_FILE_WRITE_FAILED:
  8592. return "file write failed";
  8593. case MZ_ZIP_FILE_READ_FAILED:
  8594. return "file read failed";
  8595. case MZ_ZIP_FILE_CLOSE_FAILED:
  8596. return "file close failed";
  8597. case MZ_ZIP_FILE_SEEK_FAILED:
  8598. return "file seek failed";
  8599. case MZ_ZIP_FILE_STAT_FAILED:
  8600. return "file stat failed";
  8601. case MZ_ZIP_INVALID_PARAMETER:
  8602. return "invalid parameter";
  8603. case MZ_ZIP_INVALID_FILENAME:
  8604. return "invalid filename";
  8605. case MZ_ZIP_BUF_TOO_SMALL:
  8606. return "buffer too small";
  8607. case MZ_ZIP_INTERNAL_ERROR:
  8608. return "internal error";
  8609. case MZ_ZIP_FILE_NOT_FOUND:
  8610. return "file not found";
  8611. case MZ_ZIP_ARCHIVE_TOO_LARGE:
  8612. return "archive is too large";
  8613. case MZ_ZIP_VALIDATION_FAILED:
  8614. return "validation failed";
  8615. case MZ_ZIP_WRITE_CALLBACK_FAILED:
  8616. return "write callback failed";
  8617. case MZ_ZIP_TOTAL_ERRORS:
  8618. return "total errors";
  8619. default:
  8620. break;
  8621. }
  8622. return "unknown error";
  8623. }
  8624. /* Note: Just because the archive is not zip64 doesn't necessarily mean it
  8625. * doesn't have Zip64 extended information extra field, argh. */
  8626. mz_bool mz_zip_is_zip64(mz_zip_archive *pZip) {
  8627. if ((!pZip) || (!pZip->m_pState))
  8628. return MZ_FALSE;
  8629. return pZip->m_pState->m_zip64;
  8630. }
  8631. size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip) {
  8632. if ((!pZip) || (!pZip->m_pState))
  8633. return 0;
  8634. return pZip->m_pState->m_central_dir.m_size;
  8635. }
  8636. mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip) {
  8637. return pZip ? pZip->m_total_files : 0;
  8638. }
  8639. mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip) {
  8640. if (!pZip)
  8641. return 0;
  8642. return pZip->m_archive_size;
  8643. }
  8644. mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip) {
  8645. if ((!pZip) || (!pZip->m_pState))
  8646. return 0;
  8647. return pZip->m_pState->m_file_archive_start_ofs;
  8648. }
  8649. MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip) {
  8650. if ((!pZip) || (!pZip->m_pState))
  8651. return 0;
  8652. return pZip->m_pState->m_pFile;
  8653. }
  8654. size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs,
  8655. void *pBuf, size_t n) {
  8656. if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead))
  8657. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  8658. return pZip->m_pRead(pZip->m_pIO_opaque, file_ofs, pBuf, n);
  8659. }
  8660. mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index,
  8661. char *pFilename, mz_uint filename_buf_size) {
  8662. mz_uint n;
  8663. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  8664. if (!p) {
  8665. if (filename_buf_size)
  8666. pFilename[0] = '\0';
  8667. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  8668. return 0;
  8669. }
  8670. n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  8671. if (filename_buf_size) {
  8672. n = MZ_MIN(n, filename_buf_size - 1);
  8673. memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);
  8674. pFilename[n] = '\0';
  8675. }
  8676. return n + 1;
  8677. }
  8678. mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index,
  8679. mz_zip_archive_file_stat *pStat) {
  8680. return mz_zip_file_stat_internal(
  8681. pZip, file_index, mz_zip_get_cdh(pZip, file_index), pStat, NULL);
  8682. }
  8683. mz_bool mz_zip_end(mz_zip_archive *pZip) {
  8684. if (!pZip)
  8685. return MZ_FALSE;
  8686. if (pZip->m_zip_mode == MZ_ZIP_MODE_READING)
  8687. return mz_zip_reader_end(pZip);
  8688. #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
  8689. else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) ||
  8690. (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))
  8691. return mz_zip_writer_end(pZip);
  8692. #endif
  8693. return MZ_FALSE;
  8694. }
  8695. #endif /*#ifndef MINIZ_NO_ARCHIVE_APIS*/