gzip.c 266 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607
  1. #include <ctype.h>
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <malloc.h>
  7. #include <memory.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/dir.h>
  13. #include <sys/file.h>
  14. #include <sys/stat.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <utime.h>
  20. #define HAVE_CONFIG_H 1
  21. /* config.h. Generated by configure. */
  22. /* config.h.in. Generated from configure.in by autoheader. */
  23. /* Define if an assembler version of longest_match is available. */
  24. /* #undef ASMV */
  25. /* Define to 1 if you have the <dirent.h> header file, and it defines `DIR'.
  26. */
  27. #define HAVE_DIRENT_H 1
  28. /* Define to 1 if you have the <fcntl.h> header file. */
  29. #define HAVE_FCNTL_H 1
  30. /* Define to 1 if you have the <inttypes.h> header file. */
  31. #define HAVE_INTTYPES_H 1
  32. /* Define to 1 if you have the <limits.h> header file. */
  33. #define HAVE_LIMITS_H 1
  34. /* Define to 1 if you have the `lstat' function. */
  35. #define HAVE_LSTAT 1
  36. /* Define to 1 if you have the <memory.h> header file. */
  37. #define HAVE_MEMORY_H 1
  38. /* Define to 1 if you have the <ndir.h> header file, and it defines `DIR'. */
  39. /* #undef HAVE_NDIR_H */
  40. /* Define to 1 if you have the `rpmatch' function. */
  41. /* #undef HAVE_RPMATCH */
  42. /* Define to 1 if you have the <stdint.h> header file. */
  43. #define HAVE_STDINT_H 1
  44. /* Define to 1 if you have the <stdlib.h> header file. */
  45. #define HAVE_STDLIB_H 1
  46. /* Define to 1 if you have the <strings.h> header file. */
  47. #define HAVE_STRINGS_H 1
  48. /* Define to 1 if you have the <string.h> header file. */
  49. #define HAVE_STRING_H 1
  50. /* Define to 1 if you have the <sys/dir.h> header file, and it defines `DIR'.
  51. */
  52. /* #undef HAVE_SYS_DIR_H */
  53. /* Define to 1 if you have the <sys/ndir.h> header file, and it defines `DIR'.
  54. */
  55. /* #undef HAVE_SYS_NDIR_H */
  56. /* Define to 1 if you have the <sys/stat.h> header file. */
  57. #define HAVE_SYS_STAT_H 1
  58. /* Define to 1 if you have the <sys/types.h> header file. */
  59. #define HAVE_SYS_TYPES_H 1
  60. /* Define to 1 if you have the <sys/utime.h> header file. */
  61. /* #undef HAVE_SYS_UTIME_H */
  62. /* Define to 1 if you have the <time.h> header file. */
  63. #define HAVE_TIME_H 1
  64. /* Define to 1 if you have the <unistd.h> header file. */
  65. #define HAVE_UNISTD_H 1
  66. /* Define to 1 if you have the `utime' function. */
  67. #define HAVE_UTIME 1
  68. /* Define to 1 if you have the <utime.h> header file. */
  69. #define HAVE_UTIME_H 1
  70. /* Name of package */
  71. #define PACKAGE "gzip"
  72. /* Define to the address where bug reports for this package should be sent. */
  73. #define PACKAGE_BUGREPORT "bug-gzip@gnu.org"
  74. /* Define to the full name of this package. */
  75. #define PACKAGE_NAME "gzip"
  76. /* Define to the full name and version of this package. */
  77. #define PACKAGE_STRING "gzip 1.3.5"
  78. /* Define to the one symbol short name of this package. */
  79. #define PACKAGE_TARNAME "gzip"
  80. /* Define to the version of this package. */
  81. #define PACKAGE_VERSION "1.3.5"
  82. /* Define as the return type of signal handlers (`int' or `void'). */
  83. #define RETSIGTYPE void
  84. /* Define to 1 if you have the ANSI C header files. */
  85. #define STDC_HEADERS 1
  86. /* Version number of package */
  87. #define VERSION "1.3.5"
  88. /* Define to 1 if on AIX 3.
  89. System headers sometimes define this.
  90. We just want to avoid a redefinition error message. */
  91. #ifndef _ALL_SOURCE
  92. /* # undef _ALL_SOURCE */
  93. #endif
  94. /* Number of bits in a file offset, on hosts where this is settable. */
  95. #define _FILE_OFFSET_BITS 64
  96. /* Enable GNU extensions on systems that have them. */
  97. #ifndef _GNU_SOURCE
  98. # define _GNU_SOURCE 1
  99. #endif
  100. /* Define for large files, on AIX-style hosts. */
  101. /* #undef _LARGE_FILES */
  102. /* Define to 1 if on MINIX. */
  103. /* #undef _MINIX */
  104. /* Define to 2 if the system does not provide POSIX.1 features except with
  105. this defined. */
  106. /* #undef _POSIX_1_SOURCE */
  107. /* Define to 1 if you need to in order for `stat' and other things to work. */
  108. /* #undef _POSIX_SOURCE */
  109. /* Define to empty if `const' does not conform to ANSI C. */
  110. /* #undef const */
  111. /* Define to `long' if <sys/types.h> does not define. */
  112. /* #undef off_t */
  113. /* Define to `unsigned' if <sys/types.h> does not define. */
  114. /* #undef size_t */
  115. /* bits.c -- output variable-length bit strings
  116. * Copyright (C) 1992-1993 Jean-loup Gailly
  117. * This is free software; you can redistribute it and/or modify it under the
  118. * terms of the GNU General Public License, see the file COPYING.
  119. */
  120. /*
  121. * PURPOSE
  122. *
  123. * Output variable-length bit strings. Compression can be done
  124. * to a file or to memory. (The latter is not supported in this version.)
  125. *
  126. * DISCUSSION
  127. *
  128. * The PKZIP "deflate" file format interprets compressed file data
  129. * as a sequence of bits. Multi-bit strings in the file may cross
  130. * byte boundaries without restriction.
  131. *
  132. * The first bit of each byte is the low-order bit.
  133. *
  134. * The routines in this file allow a variable-length bit value to
  135. * be output right-to-left (useful for literal values). For
  136. * left-to-right output (useful for code strings from the tree routines),
  137. * the bits must have been reversed first with bi_reverse().
  138. *
  139. * For in-memory compression, the compressed bit stream goes directly
  140. * into the requested output buffer. The input data is read in blocks
  141. * by the mem_read() function. The buffer is limited to 64K on 16 bit
  142. * machines.
  143. *
  144. * INTERFACE
  145. *
  146. * void bi_init (FILE *zipfile)
  147. * Initialize the bit string routines.
  148. *
  149. * void send_bits (int value, int length)
  150. * Write out a bit string, taking the source bits right to
  151. * left.
  152. *
  153. * int bi_reverse (int value, int length)
  154. * Reverse the bits of a bit string, taking the source bits left to
  155. * right and emitting them right to left.
  156. *
  157. * void bi_windup (void)
  158. * Write out any remaining bits in an incomplete byte.
  159. *
  160. * void copy_block(char *buf, unsigned len, int header)
  161. * Copy a stored block to the zip file, storing first the length and
  162. * its one's complement if requested.
  163. *
  164. */
  165. /* tailor.h -- target dependent definitions
  166. * Copyright (C) 1992-1993 Jean-loup Gailly.
  167. * This is free software; you can redistribute it and/or modify it under the
  168. * terms of the GNU General Public License, see the file COPYING.
  169. */
  170. /* The target dependent definitions should be defined here only.
  171. * The target dependent functions should be defined in tailor.c.
  172. */
  173. /* $Id: tailor.h,v 0.18 1993/06/14 19:32:20 jloup Exp $ */
  174. #if defined(__MSDOS__) && !defined(MSDOS)
  175. # define MSDOS
  176. #endif
  177. #if defined(__OS2__) && !defined(OS2)
  178. # define OS2
  179. #endif
  180. #if defined(OS2) && defined(MSDOS) /* MS C under OS/2 */
  181. # undef MSDOS
  182. #endif
  183. #if defined(ATARI) || defined(atarist)
  184. # ifndef STDC_HEADERS
  185. # define STDC_HEADERS
  186. # define HAVE_UNISTD_H
  187. # define HAVE_DIRENT_H
  188. # endif
  189. # define ASMV
  190. # define OS_CODE 0x05
  191. # ifdef TOSFS
  192. # define PATH_SEP2 '\\'
  193. # define PATH_SEP3 ':'
  194. # define MAX_PATH_LEN 128
  195. # define NO_MULTIPLE_DOTS
  196. # define MAX_EXT_CHARS 3
  197. # define Z_SUFFIX "z"
  198. # define casemap(c) tolow(c) /* Force file names to lower case */
  199. # endif
  200. #endif
  201. #ifdef MACOS
  202. # define PATH_SEP ':'
  203. # define DYN_ALLOC
  204. # define PROTO
  205. # define NO_STDIN_FSTAT
  206. # define chmod(file, mode) (0)
  207. # define OPEN(name, flags, mode) open(name, flags)
  208. # define OS_CODE 0x07
  209. # ifdef MPW
  210. # define isatty(fd) ((fd) <= 2)
  211. # endif
  212. #endif
  213. #ifdef __50SERIES /* Prime/PRIMOS */
  214. # define PATH_SEP '>'
  215. # define STDC_HEADERS
  216. # define NO_STDIN_FSTAT
  217. # define NO_SIZE_CHECK
  218. # define RECORD_IO 1
  219. # define casemap(c) tolow(c) /* Force file names to lower case */
  220. # define put_char(c) put_byte((c) & 0x7F)
  221. # define get_char(c) ascii2pascii(get_byte())
  222. # define OS_CODE 0x0F /* temporary, subject to change */
  223. # ifdef SIGTERM
  224. # undef SIGTERM /* We don't want a signal handler for SIGTERM */
  225. # endif
  226. #endif
  227. #if defined(pyr) && !defined(NOMEMCPY) /* Pyramid */
  228. # define NOMEMCPY /* problem with overlapping copies */
  229. #endif
  230. #ifdef TOPS20
  231. # define OS_CODE 0x0a
  232. #endif
  233. #ifndef unix
  234. # define NO_ST_INO /* don't rely on inode numbers */
  235. #endif
  236. /* Common defaults */
  237. #ifndef OS_CODE
  238. # define OS_CODE 0x03 /* assume Unix */
  239. #endif
  240. #ifndef PATH_SEP
  241. # define PATH_SEP '/'
  242. #endif
  243. #ifndef casemap
  244. # define casemap(c) (c)
  245. #endif
  246. #ifndef OPTIONS_VAR
  247. # define OPTIONS_VAR "GZIP"
  248. #endif
  249. #ifndef Z_SUFFIX
  250. # define Z_SUFFIX ".gz"
  251. #endif
  252. #ifdef MAX_EXT_CHARS
  253. # define MAX_SUFFIX MAX_EXT_CHARS
  254. #else
  255. # define MAX_SUFFIX 30
  256. #endif
  257. #ifndef MAKE_LEGAL_NAME
  258. # ifdef NO_MULTIPLE_DOTS
  259. # define MAKE_LEGAL_NAME(name) make_simple_name(name)
  260. # else
  261. # define MAKE_LEGAL_NAME(name)
  262. # endif
  263. #endif
  264. #ifndef MIN_PART
  265. # define MIN_PART 3
  266. /* keep at least MIN_PART chars between dots in a file name. */
  267. #endif
  268. #ifndef EXPAND
  269. # define EXPAND(argc,argv)
  270. #endif
  271. #ifndef RECORD_IO
  272. # define RECORD_IO 0
  273. #endif
  274. #ifndef SET_BINARY_MODE
  275. # define SET_BINARY_MODE(fd)
  276. #endif
  277. #ifndef OPEN
  278. # define OPEN(name, flags, mode) open(name, flags, mode)
  279. #endif
  280. #ifndef get_char
  281. # define get_char() get_byte()
  282. #endif
  283. #ifndef put_char
  284. # define put_char(c) put_byte(c)
  285. #endif
  286. /* gzip.h -- common declarations for all gzip modules
  287. * Copyright (C) 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
  288. * Copyright (C) 1992-1993 Jean-loup Gailly.
  289. * This is free software; you can redistribute it and/or modify it under the
  290. * terms of the GNU General Public License, see the file COPYING.
  291. */
  292. #if defined(__STDC__) || defined(PROTO)
  293. # define OF(args) args
  294. #else
  295. # define OF(args) ()
  296. #endif
  297. #ifdef __STDC__
  298. typedef void *voidp;
  299. #else
  300. typedef char *voidp;
  301. #endif
  302. # define memzero(s, n) memset ((voidp)(s), 0, (n))
  303. #ifndef RETSIGTYPE
  304. # define RETSIGTYPE void
  305. #endif
  306. #define local static
  307. typedef unsigned char uch;
  308. typedef unsigned short ush;
  309. typedef unsigned long ulg;
  310. /* Return codes from gzip */
  311. #define OK 0
  312. #define ERROR 1
  313. #define WARNING 2
  314. /* Compression methods (see algorithm.doc) */
  315. #define STORED 0
  316. #define COMPRESSED 1
  317. #define PACKED 2
  318. #define LZHED 3
  319. /* methods 4 to 7 reserved */
  320. #define DEFLATED 8
  321. #define MAX_METHODS 9
  322. extern int method; /* compression method */
  323. /* To save memory for 16 bit systems, some arrays are overlaid between
  324. * the various modules:
  325. * deflate: prev+head window d_buf l_buf outbuf
  326. * unlzw: tab_prefix tab_suffix stack inbuf outbuf
  327. * inflate: window inbuf
  328. * unpack: window inbuf prefix_len
  329. * unlzh: left+right window c_table inbuf c_len
  330. * For compression, input is done in window[]. For decompression, output
  331. * is done in window except for unlzw.
  332. */
  333. #ifndef INBUFSIZ
  334. # ifdef SMALL_MEM
  335. # define INBUFSIZ 0x2000 /* input buffer size */
  336. # else
  337. # define INBUFSIZ 0x8000 /* input buffer size */
  338. # endif
  339. #endif
  340. #define INBUF_EXTRA 64 /* required by unlzw() */
  341. #ifndef OUTBUFSIZ
  342. # ifdef SMALL_MEM
  343. # define OUTBUFSIZ 8192 /* output buffer size */
  344. # else
  345. # define OUTBUFSIZ 16384 /* output buffer size */
  346. # endif
  347. #endif
  348. #define OUTBUF_EXTRA 2048 /* required by unlzw() */
  349. #ifndef DIST_BUFSIZE
  350. # ifdef SMALL_MEM
  351. # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
  352. # else
  353. # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
  354. # endif
  355. #endif
  356. #define near
  357. #ifdef DYN_ALLOC
  358. # define EXTERN(type, array) extern type * near array
  359. # define DECLARE(type, array, size) type * near array
  360. # define ALLOC(type, array, size) { \
  361. array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
  362. if (array == NULL) error("insufficient memory"); \
  363. }
  364. # define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
  365. #else
  366. # define EXTERN(type, array) extern type array[]
  367. # define DECLARE(type, array, size) type array[size]
  368. # define ALLOC(type, array, size)
  369. # define FREE(array)
  370. #endif
  371. EXTERN(uch, inbuf); /* input buffer */
  372. EXTERN(uch, outbuf); /* output buffer */
  373. EXTERN(ush, d_buf); /* buffer for distances, see trees.c */
  374. EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */
  375. #define tab_suffix window
  376. #ifndef MAXSEG_64K
  377. # define tab_prefix prev /* hash link (see deflate.c) */
  378. # define head (prev+WSIZE) /* hash head (see deflate.c) */
  379. EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */
  380. #else
  381. # define tab_prefix0 prev
  382. # define head tab_prefix1
  383. EXTERN(ush, tab_prefix0); /* prefix for even codes */
  384. EXTERN(ush, tab_prefix1); /* prefix for odd codes */
  385. #endif
  386. extern unsigned insize; /* valid bytes in inbuf */
  387. extern unsigned inptr; /* index of next byte to be processed in inbuf */
  388. extern unsigned outcnt; /* bytes in output buffer */
  389. extern int rsync; /* deflate into rsyncable chunks */
  390. extern off_t bytes_in; /* number of input bytes */
  391. extern off_t bytes_out; /* number of output bytes */
  392. extern off_t header_bytes;/* number of bytes in gzip header */
  393. extern int ifd; /* input file descriptor */
  394. extern int ofd; /* output file descriptor */
  395. extern char ifname[]; /* input file name or "stdin" */
  396. extern char ofname[]; /* output file name or "stdout" */
  397. extern char *progname; /* program name */
  398. extern time_t time_stamp; /* original time stamp (modification time) */
  399. extern off_t ifile_size; /* input file size, -1 for devices (debug only) */
  400. typedef int file_t; /* Do not use stdio */
  401. #define NO_FILE (-1) /* in memory compression */
  402. #define PACK_MAGIC "\037\036" /* Magic header for packed files */
  403. #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
  404. #define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
  405. #define LZH_MAGIC "\037\240" /* Magic header for SCO LZH Compress files*/
  406. #define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */
  407. /* gzip flag byte */
  408. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  409. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  410. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  411. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  412. #define COMMENT 0x10 /* bit 4 set: file comment present */
  413. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  414. #define RESERVED 0xC0 /* bit 6,7: reserved */
  415. /* internal file attribute */
  416. #define UNKNOWN 0xffff
  417. #define BINARY 0
  418. #define ASCII 1
  419. #ifndef WSIZE
  420. # define WSIZE 0x8000 /* window size--must be a power of two, and */
  421. #endif /* at least 32K for zip's deflate method */
  422. #define MIN_MATCH 3
  423. #define MAX_MATCH 258
  424. /* The minimum and maximum match lengths */
  425. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  426. /* Minimum amount of lookahead, except at the end of the input file.
  427. * See deflate.c for comments about the MIN_MATCH+1.
  428. */
  429. #define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
  430. /* In order to simplify the code, particularly on 16 bit machines, match
  431. * distances are limited to MAX_DIST instead of WSIZE.
  432. */
  433. extern int decrypt; /* flag to turn on decryption */
  434. extern int exit_code; /* program exit code */
  435. extern int verbose; /* be verbose (-v) */
  436. extern int quiet; /* be quiet (-q) */
  437. extern int level; /* compression level */
  438. extern int test; /* check .z file integrity */
  439. extern int to_stdout; /* output to stdout (-c) */
  440. extern int save_orig_name; /* set if original name must be saved */
  441. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
  442. #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
  443. /* put_byte is used for the compressed output, put_ubyte for the
  444. * uncompressed output. However unlzw() uses window for its
  445. * suffix table instead of its output buffer, so it does not use put_ubyte
  446. * (to be cleaned up).
  447. */
  448. #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
  449. flush_outbuf();}
  450. #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
  451. flush_window();}
  452. /* Output a 16 bit value, lsb first */
  453. #define put_short(w) \
  454. { if (outcnt < OUTBUFSIZ-2) { \
  455. outbuf[outcnt++] = (uch) ((w) & 0xff); \
  456. outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
  457. } else { \
  458. put_byte((uch)((w) & 0xff)); \
  459. put_byte((uch)((ush)(w) >> 8)); \
  460. } \
  461. }
  462. /* Output a 32 bit value to the bit stream, lsb first */
  463. #define put_long(n) { \
  464. put_short((n) & 0xffff); \
  465. put_short(((ulg)(n)) >> 16); \
  466. }
  467. #define seekable() 0 /* force sequential output */
  468. #define translate_eol 0 /* no option -a yet */
  469. #define tolow(c) (isupper (c) ? tolower (c) : (c)) /* force to lower case */
  470. /* Macros for getting two-byte and four-byte header values */
  471. #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
  472. #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
  473. /* Diagnostic functions */
  474. #ifdef DEBUG
  475. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  476. # define Trace(x) fprintf x
  477. # define Tracev(x) {if (verbose) fprintf x ;}
  478. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  479. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  480. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  481. #else
  482. # define Assert(cond,msg)
  483. # define Trace(x)
  484. # define Tracev(x)
  485. # define Tracevv(x)
  486. # define Tracec(c,x)
  487. # define Tracecv(c,x)
  488. #endif
  489. #define WARN(msg) {if (!quiet) fprintf msg ; \
  490. if (exit_code == OK) exit_code = WARNING;}
  491. /* in zip.c: */
  492. extern int zip OF((int in, int out));
  493. extern int file_read OF((char *buf, unsigned size));
  494. /* in unzip.c */
  495. extern int unzip OF((int in, int out));
  496. extern int check_zipfile OF((int in));
  497. /* in unpack.c */
  498. extern int unpack OF((int in, int out));
  499. /* in unlzh.c */
  500. extern int unlzh OF((int in, int out));
  501. /* in gzip.c */
  502. RETSIGTYPE abort_gzip_signal OF((void));
  503. /* in deflate.c */
  504. void lm_init OF((int pack_level, ush *flags));
  505. off_t deflate OF((void));
  506. /* in trees.c */
  507. void ct_init OF((ush *attr, int *method));
  508. int ct_tally OF((int dist, int lc));
  509. off_t flush_block OF((char *buf, ulg stored_len, int pad, int eof));
  510. /* in bits.c */
  511. void bi_init OF((file_t zipfile));
  512. void send_bits OF((int value, int length));
  513. unsigned bi_reverse OF((unsigned value, int length));
  514. void bi_windup OF((void));
  515. void copy_block OF((char *buf, unsigned len, int header));
  516. extern int (*read_buf) OF((char *buf, unsigned size));
  517. /* in util.c: */
  518. extern int copy OF((int in, int out));
  519. extern ulg updcrc OF((uch *s, unsigned n));
  520. extern void clear_bufs OF((void));
  521. extern int fill_inbuf OF((int eof_ok));
  522. extern void flush_outbuf OF((void));
  523. extern void flush_window OF((void));
  524. extern void write_buf OF((int fd, voidp buf, unsigned cnt));
  525. extern char *strlwr OF((char *s));
  526. extern char *base_name OF((char *fname));
  527. extern int xunlink OF((char *fname));
  528. extern void make_simple_name OF((char *name));
  529. extern char *add_envopt OF((int *argcp, char ***argvp, char *env));
  530. extern void error OF((char *m));
  531. extern void warning OF((char *m));
  532. extern void read_error OF((void));
  533. extern void write_error OF((void));
  534. extern void display_ratio OF((off_t num, off_t den, FILE *file));
  535. extern void fprint_off OF((FILE *, off_t, int));
  536. extern voidp xmalloc OF((unsigned int size));
  537. /* in inflate.c */
  538. extern int inflate OF((void));
  539. /* in yesno.c */
  540. extern int yesno OF((void));
  541. /* crypt.h (dummy version) -- do not perform encryption
  542. * Hardly worth copyrighting :-)
  543. */
  544. #ifdef CRYPT
  545. # undef CRYPT /* dummy version */
  546. #endif
  547. #define RAND_HEAD_LEN 12 /* length of encryption random header */
  548. #define zencode
  549. #define zdecode
  550. #ifdef RCSID
  551. static char rcsid[] = "$Id: bits.c,v 0.9 1993/06/11 10:16:58 jloup Exp $";
  552. #endif
  553. /* ===========================================================================
  554. * Local data used by the "bit string" routines.
  555. */
  556. local file_t zfile; /* output gzip file */
  557. local unsigned short bi_buf;
  558. /* Output buffer. bits are inserted starting at the bottom (least significant
  559. * bits).
  560. */
  561. #define Buf_size (8 * 2*sizeof(char))
  562. /* Number of bits used within bi_buf. (bi_buf might be implemented on
  563. * more than 16 bits on some systems.)
  564. */
  565. local int bi_valid;
  566. /* Number of valid bits in bi_buf. All bits above the last valid bit
  567. * are always zero.
  568. */
  569. int (*read_buf) OF((char *buf, unsigned size));
  570. /* Current input function. Set to mem_read for in-memory compression */
  571. #ifdef DEBUG
  572. off_t bits_sent; /* bit length of the compressed data */
  573. #endif
  574. /* ===========================================================================
  575. * Initialize the bit string routines.
  576. */
  577. void bi_init (zipfile)
  578. file_t zipfile; /* output zip file, NO_FILE for in-memory compression */
  579. {
  580. zfile = zipfile;
  581. bi_buf = 0;
  582. bi_valid = 0;
  583. #ifdef DEBUG
  584. bits_sent = 0L;
  585. #endif
  586. /* Set the defaults for file compression. They are set by memcompress
  587. * for in-memory compression.
  588. */
  589. if (zfile != NO_FILE) {
  590. read_buf = file_read;
  591. }
  592. }
  593. /* ===========================================================================
  594. * Send a value on a given number of bits.
  595. * IN assertion: length <= 16 and value fits in length bits.
  596. */
  597. void send_bits(value, length)
  598. int value; /* value to send */
  599. int length; /* number of bits */
  600. {
  601. #ifdef DEBUG
  602. Tracev((stderr," l %2d v %4x ", length, value));
  603. Assert(length > 0 && length <= 15, "invalid length");
  604. bits_sent += (off_t)length;
  605. #endif
  606. /* If not enough room in bi_buf, use (valid) bits from bi_buf and
  607. * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  608. * unused bits in value.
  609. */
  610. if (bi_valid > (int)Buf_size - length) {
  611. bi_buf |= (value << bi_valid);
  612. put_short(bi_buf);
  613. bi_buf = (ush)value >> (Buf_size - bi_valid);
  614. bi_valid += length - Buf_size;
  615. } else {
  616. bi_buf |= value << bi_valid;
  617. bi_valid += length;
  618. }
  619. }
  620. /* ===========================================================================
  621. * Reverse the first len bits of a code, using straightforward code (a faster
  622. * method would use a table)
  623. * IN assertion: 1 <= len <= 15
  624. */
  625. unsigned bi_reverse(code, len)
  626. unsigned code; /* the value to invert */
  627. int len; /* its bit length */
  628. {
  629. register unsigned res = 0;
  630. do {
  631. res |= code & 1;
  632. code >>= 1, res <<= 1;
  633. } while (--len > 0);
  634. return res >> 1;
  635. }
  636. /* ===========================================================================
  637. * Write out any remaining bits in an incomplete byte.
  638. */
  639. void bi_windup()
  640. {
  641. if (bi_valid > 8) {
  642. put_short(bi_buf);
  643. } else if (bi_valid > 0) {
  644. put_byte(bi_buf);
  645. }
  646. bi_buf = 0;
  647. bi_valid = 0;
  648. #ifdef DEBUG
  649. bits_sent = (bits_sent+7) & ~7;
  650. #endif
  651. }
  652. /* ===========================================================================
  653. * Copy a stored block to the zip file, storing first the length and its
  654. * one's complement if requested.
  655. */
  656. void copy_block(buf, len, header)
  657. char *buf; /* the input data */
  658. unsigned len; /* its length */
  659. int header; /* true if block header must be written */
  660. {
  661. bi_windup(); /* align on byte boundary */
  662. if (header) {
  663. put_short((ush)len);
  664. put_short((ush)~len);
  665. #ifdef DEBUG
  666. bits_sent += 2*16;
  667. #endif
  668. }
  669. #ifdef DEBUG
  670. bits_sent += (off_t)len<<3;
  671. #endif
  672. while (len--) {
  673. #ifdef CRYPT
  674. int t;
  675. if (key) zencode(*buf, t);
  676. #endif
  677. put_byte(*buf++);
  678. }
  679. }
  680. /* crypt.c (dummy version) -- do not perform encryption
  681. * Hardly worth copyrighting :-)
  682. */
  683. #ifdef RCSID
  684. static char rcsid[] = "$Id: crypt.c,v 0.6 1993/03/22 09:48:47 jloup Exp $";
  685. #endif
  686. /* deflate.c -- compress data using the deflation algorithm
  687. * Copyright (C) 1992-1993 Jean-loup Gailly
  688. * This is free software; you can redistribute it and/or modify it under the
  689. * terms of the GNU General Public License, see the file COPYING.
  690. */
  691. /*
  692. * PURPOSE
  693. *
  694. * Identify new text as repetitions of old text within a fixed-
  695. * length sliding window trailing behind the new text.
  696. *
  697. * DISCUSSION
  698. *
  699. * The "deflation" process depends on being able to identify portions
  700. * of the input text which are identical to earlier input (within a
  701. * sliding window trailing behind the input currently being processed).
  702. *
  703. * The most straightforward technique turns out to be the fastest for
  704. * most input files: try all possible matches and select the longest.
  705. * The key feature of this algorithm is that insertions into the string
  706. * dictionary are very simple and thus fast, and deletions are avoided
  707. * completely. Insertions are performed at each input character, whereas
  708. * string matches are performed only when the previous match ends. So it
  709. * is preferable to spend more time in matches to allow very fast string
  710. * insertions and avoid deletions. The matching algorithm for small
  711. * strings is inspired from that of Rabin & Karp. A brute force approach
  712. * is used to find longer strings when a small match has been found.
  713. * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  714. * (by Leonid Broukhis).
  715. * A previous version of this file used a more sophisticated algorithm
  716. * (by Fiala and Greene) which is guaranteed to run in linear amortized
  717. * time, but has a larger average cost, uses more memory and is patented.
  718. * However the F&G algorithm may be faster for some highly redundant
  719. * files if the parameter max_chain_length (described below) is too large.
  720. *
  721. * ACKNOWLEDGEMENTS
  722. *
  723. * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  724. * I found it in 'freeze' written by Leonid Broukhis.
  725. * Thanks to many info-zippers for bug reports and testing.
  726. *
  727. * REFERENCES
  728. *
  729. * APPNOTE.TXT documentation file in PKZIP 1.93a distribution.
  730. *
  731. * A description of the Rabin and Karp algorithm is given in the book
  732. * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  733. *
  734. * Fiala,E.R., and Greene,D.H.
  735. * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  736. *
  737. * INTERFACE
  738. *
  739. * void lm_init (int pack_level, ush *flags)
  740. * Initialize the "longest match" routines for a new file
  741. *
  742. * off_t deflate (void)
  743. * Processes a new input file and return its compressed length. Sets
  744. * the compressed length, crc, deflate flags and internal file
  745. * attributes.
  746. */
  747. /* lzw.h -- define the lzw functions.
  748. * Copyright (C) 1992-1993 Jean-loup Gailly.
  749. * This is free software; you can redistribute it and/or modify it under the
  750. * terms of the GNU General Public License, see the file COPYING.
  751. */
  752. #ifndef BITS
  753. # define BITS 16
  754. #endif
  755. #define INIT_BITS 9 /* Initial number of bits per code */
  756. #define LZW_MAGIC "\037\235" /* Magic header for lzw files, 1F 9D */
  757. #define BIT_MASK 0x1f /* Mask for 'number of compression bits' */
  758. /* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free.
  759. * It's a pity that old uncompress does not check bit 0x20. That makes
  760. * extension of the format actually undesirable because old compress
  761. * would just crash on the new format instead of giving a meaningful
  762. * error message. It does check the number of bits, but it's more
  763. * helpful to say "unsupported format, get a new version" than
  764. * "can only handle 16 bits".
  765. */
  766. #define BLOCK_MODE 0x80
  767. /* Block compression: if table is full and compression rate is dropping,
  768. * clear the dictionary.
  769. */
  770. #define LZW_RESERVED 0x60 /* reserved bits */
  771. #define CLEAR 256 /* flush the dictionary */
  772. #define FIRST (CLEAR+1) /* first free entry */
  773. extern int maxbits; /* max bits per code for LZW */
  774. extern int block_mode; /* block compress mode -C compatible with 2.0 */
  775. extern int lzw OF((int in, int out));
  776. extern int unlzw OF((int in, int out));
  777. #ifdef RCSID
  778. static char rcsid[] = "$Id: deflate.c,v 0.15 1993/06/24 10:53:53 jloup Exp $";
  779. #endif
  780. /* ===========================================================================
  781. * Configuration parameters
  782. */
  783. /* Compile with MEDIUM_MEM to reduce the memory requirements or
  784. * with SMALL_MEM to use as little memory as possible. Use BIG_MEM if the
  785. * entire input file can be held in memory (not possible on 16 bit systems).
  786. * Warning: defining these symbols affects HASH_BITS (see below) and thus
  787. * affects the compression ratio. The compressed output
  788. * is still correct, and might even be smaller in some cases.
  789. */
  790. #ifdef SMALL_MEM
  791. # define HASH_BITS 13 /* Number of bits used to hash strings */
  792. #endif
  793. #ifdef MEDIUM_MEM
  794. # define HASH_BITS 14
  795. #endif
  796. #ifndef HASH_BITS
  797. # define HASH_BITS 15
  798. /* For portability to 16 bit machines, do not use values above 15. */
  799. #endif
  800. /* To save space (see unlzw.c), we overlay prev+head with tab_prefix and
  801. * window with tab_suffix. Check that we can do this:
  802. */
  803. #if (WSIZE<<1) > (1<<BITS)
  804. error: cannot overlay window with tab_suffix and prev with tab_prefix0
  805. #endif
  806. #if HASH_BITS > BITS-1
  807. error: cannot overlay head with tab_prefix1
  808. #endif
  809. #define HASH_SIZE (unsigned)(1<<HASH_BITS)
  810. #define HASH_MASK (HASH_SIZE-1)
  811. #define WMASK (WSIZE-1)
  812. /* HASH_SIZE and WSIZE must be powers of two */
  813. #define NIL 0
  814. /* Tail of hash chains */
  815. #define FAST 4
  816. #define SLOW 2
  817. /* speed options for the general purpose bit flag */
  818. #ifndef TOO_FAR
  819. # define TOO_FAR 4096
  820. #endif
  821. /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  822. #ifndef RSYNC_WIN
  823. # define RSYNC_WIN 4096
  824. #endif
  825. /* Size of rsync window, must be < MAX_DIST */
  826. #define RSYNC_SUM_MATCH(sum) ((sum) % RSYNC_WIN == 0)
  827. /* Whether window sum matches magic value */
  828. /* ===========================================================================
  829. * Local data used by the "longest match" routines.
  830. */
  831. typedef ush Pos;
  832. typedef unsigned IPos;
  833. /* A Pos is an index in the character window. We use short instead of int to
  834. * save space in the various tables. IPos is used only for parameter passing.
  835. */
  836. /* DECLARE(uch, window, 2L*WSIZE); */
  837. /* Sliding window. Input bytes are read into the second half of the window,
  838. * and move to the first half later to keep a dictionary of at least WSIZE
  839. * bytes. With this organization, matches are limited to a distance of
  840. * WSIZE-MAX_MATCH bytes, but this ensures that IO is always
  841. * performed with a length multiple of the block size. Also, it limits
  842. * the window size to 64K, which is quite useful on MSDOS.
  843. * To do: limit the window size to WSIZE+BSZ if SMALL_MEM (the code would
  844. * be less efficient).
  845. */
  846. /* DECLARE(Pos, prev, WSIZE); */
  847. /* Link to older string with same hash index. To limit the size of this
  848. * array to 64K, this link is maintained only for the last 32K strings.
  849. * An index in this array is thus a window index modulo 32K.
  850. */
  851. /* DECLARE(Pos, head, 1<<HASH_BITS); */
  852. /* Heads of the hash chains or NIL. */
  853. ulg window_size = (ulg)2*WSIZE;
  854. /* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the
  855. * input file length plus MIN_LOOKAHEAD.
  856. */
  857. long block_start;
  858. /* window position at the beginning of the current output block. Gets
  859. * negative when the window is moved backwards.
  860. */
  861. local unsigned ins_h; /* hash index of string to be inserted */
  862. #define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
  863. /* Number of bits by which ins_h and del_h must be shifted at each
  864. * input step. It must be such that after MIN_MATCH steps, the oldest
  865. * byte no longer takes part in the hash key, that is:
  866. * H_SHIFT * MIN_MATCH >= HASH_BITS
  867. */
  868. unsigned int near prev_length;
  869. /* Length of the best match at previous step. Matches not greater than this
  870. * are discarded. This is used in the lazy match evaluation.
  871. */
  872. unsigned near strstart; /* start of string to insert */
  873. unsigned near match_start; /* start of matching string */
  874. local int eofile; /* flag set at end of input file */
  875. local unsigned lookahead; /* number of valid bytes ahead in window */
  876. unsigned near max_chain_length;
  877. /* To speed up deflation, hash chains are never searched beyond this length.
  878. * A higher limit improves compression ratio but degrades the speed.
  879. */
  880. local unsigned int max_lazy_match;
  881. /* Attempt to find a better match only when the current match is strictly
  882. * smaller than this value. This mechanism is used only for compression
  883. * levels >= 4.
  884. */
  885. #define max_insert_length max_lazy_match
  886. /* Insert new strings in the hash table only if the match length
  887. * is not greater than this length. This saves time but degrades compression.
  888. * max_insert_length is used only for compression levels <= 3.
  889. */
  890. local int compr_level;
  891. /* compression level (1..9) */
  892. unsigned near good_match;
  893. /* Use a faster search when the previous match is longer than this */
  894. local ulg rsync_sum; /* rolling sum of rsync window */
  895. local ulg rsync_chunk_end; /* next rsync sequence point */
  896. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  897. * the desired pack level (0..9). The values given below have been tuned to
  898. * exclude worst case performance for pathological files. Better values may be
  899. * found for specific files.
  900. */
  901. typedef struct config {
  902. ush good_length; /* reduce lazy search above this match length */
  903. ush max_lazy; /* do not perform lazy search above this match length */
  904. ush nice_length; /* quit search above this match length */
  905. ush max_chain;
  906. } config;
  907. #ifdef FULL_SEARCH
  908. # define nice_match MAX_MATCH
  909. #else
  910. int near nice_match; /* Stop searching when current match exceeds this */
  911. #endif
  912. local config configuration_table[10] = {
  913. /* good lazy nice chain */
  914. /* 0 */ {0, 0, 0, 0}, /* store only */
  915. /* 1 */ {4, 4, 8, 4}, /* maximum speed, no lazy matches */
  916. /* 2 */ {4, 5, 16, 8},
  917. /* 3 */ {4, 6, 32, 32},
  918. /* 4 */ {4, 4, 16, 16}, /* lazy matches */
  919. /* 5 */ {8, 16, 32, 32},
  920. /* 6 */ {8, 16, 128, 128},
  921. /* 7 */ {8, 32, 128, 256},
  922. /* 8 */ {32, 128, 258, 1024},
  923. /* 9 */ {32, 258, 258, 4096}}; /* maximum compression */
  924. /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  925. * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  926. * meaning.
  927. */
  928. #define EQUAL 0
  929. /* result of memcmp for equal strings */
  930. /* ===========================================================================
  931. * Prototypes for local functions.
  932. */
  933. local void fill_window OF((void));
  934. local off_t deflate_fast OF((void));
  935. int longest_match OF((IPos cur_match));
  936. #ifdef ASMV
  937. void match_init OF((void)); /* asm code initialization */
  938. #endif
  939. #ifdef DEBUG
  940. local void check_match OF((IPos start, IPos match, int length));
  941. #endif
  942. /* ===========================================================================
  943. * Update a hash value with the given input byte
  944. * IN assertion: all calls to to UPDATE_HASH are made with consecutive
  945. * input characters, so that a running hash key can be computed from the
  946. * previous key instead of complete recalculation each time.
  947. */
  948. #define UPDATE_HASH(h,c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK)
  949. /* ===========================================================================
  950. * Insert string s in the dictionary and set match_head to the previous head
  951. * of the hash chain (the most recent string with same hash key). Return
  952. * the previous length of the hash chain.
  953. * IN assertion: all calls to to INSERT_STRING are made with consecutive
  954. * input characters and the first MIN_MATCH bytes of s are valid
  955. * (except for the last MIN_MATCH-1 bytes of the input file).
  956. */
  957. #define INSERT_STRING(s, match_head) \
  958. (UPDATE_HASH(ins_h, window[(s) + MIN_MATCH-1]), \
  959. prev[(s) & WMASK] = match_head = head[ins_h], \
  960. head[ins_h] = (s))
  961. /* ===========================================================================
  962. * Initialize the "longest match" routines for a new file
  963. */
  964. void lm_init (pack_level, flags)
  965. int pack_level; /* 0: store, 1: best speed, 9: best compression */
  966. ush *flags; /* general purpose bit flag */
  967. {
  968. register unsigned j;
  969. if (pack_level < 1 || pack_level > 9) error("bad pack level");
  970. compr_level = pack_level;
  971. /* Initialize the hash table. */
  972. #if defined(MAXSEG_64K) && HASH_BITS == 15
  973. for (j = 0; j < HASH_SIZE; j++) head[j] = NIL;
  974. #else
  975. memzero((char*)head, HASH_SIZE*sizeof(*head));
  976. #endif
  977. /* prev will be initialized on the fly */
  978. /* rsync params */
  979. rsync_chunk_end = 0xFFFFFFFFUL;
  980. rsync_sum = 0;
  981. /* Set the default configuration parameters:
  982. */
  983. max_lazy_match = configuration_table[pack_level].max_lazy;
  984. good_match = configuration_table[pack_level].good_length;
  985. #ifndef FULL_SEARCH
  986. nice_match = configuration_table[pack_level].nice_length;
  987. #endif
  988. max_chain_length = configuration_table[pack_level].max_chain;
  989. if (pack_level == 1) {
  990. *flags |= FAST;
  991. } else if (pack_level == 9) {
  992. *flags |= SLOW;
  993. }
  994. /* ??? reduce max_chain_length for binary files */
  995. strstart = 0;
  996. block_start = 0L;
  997. #ifdef ASMV
  998. match_init(); /* initialize the asm code */
  999. #endif
  1000. lookahead = read_buf((char*)window,
  1001. sizeof(int) <= 2 ? (unsigned)WSIZE : 2*WSIZE);
  1002. if (lookahead == 0 || lookahead == (unsigned)EOF) {
  1003. eofile = 1, lookahead = 0;
  1004. return;
  1005. }
  1006. eofile = 0;
  1007. /* Make sure that we always have enough lookahead. This is important
  1008. * if input comes from a device such as a tty.
  1009. */
  1010. while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
  1011. ins_h = 0;
  1012. for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]);
  1013. /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
  1014. * not important since only literal bytes will be emitted.
  1015. */
  1016. }
  1017. /* ===========================================================================
  1018. * Set match_start to the longest match starting at the given string and
  1019. * return its length. Matches shorter or equal to prev_length are discarded,
  1020. * in which case the result is equal to prev_length and match_start is
  1021. * garbage.
  1022. * IN assertions: cur_match is the head of the hash chain for the current
  1023. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1024. */
  1025. #ifndef ASMV
  1026. /* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or
  1027. * match.s. The code is functionally equivalent, so you can use the C version
  1028. * if desired.
  1029. */
  1030. int longest_match(cur_match)
  1031. IPos cur_match; /* current match */
  1032. {
  1033. unsigned chain_length = max_chain_length; /* max hash chain length */
  1034. register uch *scan = window + strstart; /* current string */
  1035. register uch *match; /* matched string */
  1036. register int len; /* length of current match */
  1037. int best_len = prev_length; /* best match length so far */
  1038. IPos limit = strstart > (IPos)MAX_DIST ? strstart - (IPos)MAX_DIST : NIL;
  1039. /* Stop when cur_match becomes <= limit. To simplify the code,
  1040. * we prevent matches with the string of window index 0.
  1041. */
  1042. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1043. * It is easy to get rid of this optimization if necessary.
  1044. */
  1045. #if HASH_BITS < 8 || MAX_MATCH != 258
  1046. error: Code too clever
  1047. #endif
  1048. #ifdef UNALIGNED_OK
  1049. /* Compare two bytes at a time. Note: this is not always beneficial.
  1050. * Try with and without -DUNALIGNED_OK to check.
  1051. */
  1052. register uch *strend = window + strstart + MAX_MATCH - 1;
  1053. register ush scan_start = *(ush*)scan;
  1054. register ush scan_end = *(ush*)(scan+best_len-1);
  1055. #else
  1056. register uch *strend = window + strstart + MAX_MATCH;
  1057. register uch scan_end1 = scan[best_len-1];
  1058. register uch scan_end = scan[best_len];
  1059. #endif
  1060. /* Do not waste too much time if we already have a good match: */
  1061. if (prev_length >= good_match) {
  1062. chain_length >>= 2;
  1063. }
  1064. Assert(strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");
  1065. do {
  1066. Assert(cur_match < strstart, "no future");
  1067. match = window + cur_match;
  1068. /* Skip to next match if the match length cannot increase
  1069. * or if the match length is less than 2:
  1070. */
  1071. #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  1072. /* This code assumes sizeof(unsigned short) == 2. Do not use
  1073. * UNALIGNED_OK if your compiler uses a different size.
  1074. */
  1075. if (*(ush*)(match+best_len-1) != scan_end ||
  1076. *(ush*)match != scan_start) continue;
  1077. /* It is not necessary to compare scan[2] and match[2] since they are
  1078. * always equal when the other bytes match, given that the hash keys
  1079. * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  1080. * strstart+3, +5, ... up to strstart+257. We check for insufficient
  1081. * lookahead only every 4th comparison; the 128th check will be made
  1082. * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  1083. * necessary to put more guard bytes at the end of the window, or
  1084. * to check more often for insufficient lookahead.
  1085. */
  1086. scan++, match++;
  1087. do {
  1088. } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
  1089. *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  1090. *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  1091. *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  1092. scan < strend);
  1093. /* The funny "do {}" generates better code on most compilers */
  1094. /* Here, scan <= window+strstart+257 */
  1095. Assert(scan <= window+(unsigned)(window_size-1), "wild scan");
  1096. if (*scan == *match) scan++;
  1097. len = (MAX_MATCH - 1) - (int)(strend-scan);
  1098. scan = strend - (MAX_MATCH-1);
  1099. #else /* UNALIGNED_OK */
  1100. if (match[best_len] != scan_end ||
  1101. match[best_len-1] != scan_end1 ||
  1102. *match != *scan ||
  1103. *++match != scan[1]) continue;
  1104. /* The check at best_len-1 can be removed because it will be made
  1105. * again later. (This heuristic is not always a win.)
  1106. * It is not necessary to compare scan[2] and match[2] since they
  1107. * are always equal when the other bytes match, given that
  1108. * the hash keys are equal and that HASH_BITS >= 8.
  1109. */
  1110. scan += 2, match++;
  1111. /* We check for insufficient lookahead only every 8th comparison;
  1112. * the 256th check will be made at strstart+258.
  1113. */
  1114. do {
  1115. } while (*++scan == *++match && *++scan == *++match &&
  1116. *++scan == *++match && *++scan == *++match &&
  1117. *++scan == *++match && *++scan == *++match &&
  1118. *++scan == *++match && *++scan == *++match &&
  1119. scan < strend);
  1120. len = MAX_MATCH - (int)(strend - scan);
  1121. scan = strend - MAX_MATCH;
  1122. #endif /* UNALIGNED_OK */
  1123. if (len > best_len) {
  1124. match_start = cur_match;
  1125. best_len = len;
  1126. if (len >= nice_match) break;
  1127. #ifdef UNALIGNED_OK
  1128. scan_end = *(ush*)(scan+best_len-1);
  1129. #else
  1130. scan_end1 = scan[best_len-1];
  1131. scan_end = scan[best_len];
  1132. #endif
  1133. }
  1134. } while ((cur_match = prev[cur_match & WMASK]) > limit
  1135. && --chain_length != 0);
  1136. return best_len;
  1137. }
  1138. #endif /* ASMV */
  1139. #ifdef DEBUG
  1140. /* ===========================================================================
  1141. * Check that the match at match_start is indeed a match.
  1142. */
  1143. local void check_match(start, match, length)
  1144. IPos start, match;
  1145. int length;
  1146. {
  1147. /* check that the match is indeed a match */
  1148. if (memcmp((char*)window + match,
  1149. (char*)window + start, length) != EQUAL) {
  1150. fprintf(stderr,
  1151. " start %d, match %d, length %d\n",
  1152. start, match, length);
  1153. error("invalid match");
  1154. }
  1155. if (verbose > 1) {
  1156. fprintf(stderr,"\\[%d,%d]", start-match, length);
  1157. do { putc(window[start++], stderr); } while (--length != 0);
  1158. }
  1159. }
  1160. #else
  1161. # define check_match(start, match, length)
  1162. #endif
  1163. /* ===========================================================================
  1164. * Fill the window when the lookahead becomes insufficient.
  1165. * Updates strstart and lookahead, and sets eofile if end of input file.
  1166. * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
  1167. * OUT assertions: at least one byte has been read, or eofile is set;
  1168. * file reads are performed for at least two bytes (required for the
  1169. * translate_eol option).
  1170. */
  1171. local void fill_window()
  1172. {
  1173. register unsigned n, m;
  1174. unsigned more = (unsigned)(window_size - (ulg)lookahead - (ulg)strstart);
  1175. /* Amount of free space at the end of the window. */
  1176. /* If the window is almost full and there is insufficient lookahead,
  1177. * move the upper half to the lower one to make room in the upper half.
  1178. */
  1179. if (more == (unsigned)EOF) {
  1180. /* Very unlikely, but possible on 16 bit machine if strstart == 0
  1181. * and lookahead == 1 (input done one byte at time)
  1182. */
  1183. more--;
  1184. } else if (strstart >= WSIZE+MAX_DIST) {
  1185. /* By the IN assertion, the window is not empty so we can't confuse
  1186. * more == 0 with more == 64K on a 16 bit machine.
  1187. */
  1188. Assert(window_size == (ulg)2*WSIZE, "no sliding with BIG_MEM");
  1189. memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
  1190. match_start -= WSIZE;
  1191. strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
  1192. if (rsync_chunk_end != 0xFFFFFFFFUL)
  1193. rsync_chunk_end -= WSIZE;
  1194. block_start -= (long) WSIZE;
  1195. for (n = 0; n < HASH_SIZE; n++) {
  1196. m = head[n];
  1197. head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
  1198. }
  1199. for (n = 0; n < WSIZE; n++) {
  1200. m = prev[n];
  1201. prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
  1202. /* If n is not on any hash chain, prev[n] is garbage but
  1203. * its value will never be used.
  1204. */
  1205. }
  1206. more += WSIZE;
  1207. }
  1208. /* At this point, more >= 2 */
  1209. if (!eofile) {
  1210. n = read_buf((char*)window+strstart+lookahead, more);
  1211. if (n == 0 || n == (unsigned)EOF) {
  1212. eofile = 1;
  1213. } else {
  1214. lookahead += n;
  1215. }
  1216. }
  1217. }
  1218. local void rsync_roll(start, num)
  1219. unsigned start;
  1220. unsigned num;
  1221. {
  1222. unsigned i;
  1223. if (start < RSYNC_WIN) {
  1224. /* before window fills. */
  1225. for (i = start; i < RSYNC_WIN; i++) {
  1226. if (i == start + num) return;
  1227. rsync_sum += (ulg)window[i];
  1228. }
  1229. num -= (RSYNC_WIN - start);
  1230. start = RSYNC_WIN;
  1231. }
  1232. /* buffer after window full */
  1233. for (i = start; i < start+num; i++) {
  1234. /* New character in */
  1235. rsync_sum += (ulg)window[i];
  1236. /* Old character out */
  1237. rsync_sum -= (ulg)window[i - RSYNC_WIN];
  1238. if (rsync_chunk_end == 0xFFFFFFFFUL && RSYNC_SUM_MATCH(rsync_sum))
  1239. rsync_chunk_end = i;
  1240. }
  1241. }
  1242. /* ===========================================================================
  1243. * Set rsync_chunk_end if window sum matches magic value.
  1244. */
  1245. #define RSYNC_ROLL(s, n) \
  1246. do { if (rsync) rsync_roll((s), (n)); } while(0)
  1247. /* ===========================================================================
  1248. * Flush the current block, with given end-of-file flag.
  1249. * IN assertion: strstart is set to the end of the current match.
  1250. */
  1251. #define FLUSH_BLOCK(eof) \
  1252. flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
  1253. (char*)NULL, (long)strstart - block_start, flush-1, (eof))
  1254. /* ===========================================================================
  1255. * Processes a new input file and return its compressed length. This
  1256. * function does not perform lazy evaluationof matches and inserts
  1257. * new strings in the dictionary only for unmatched strings or for short
  1258. * matches. It is used only for the fast compression options.
  1259. */
  1260. local off_t deflate_fast()
  1261. {
  1262. IPos hash_head; /* head of the hash chain */
  1263. int flush; /* set if current block must be flushed, 2=>and padded */
  1264. unsigned match_length = 0; /* length of best match */
  1265. prev_length = MIN_MATCH-1;
  1266. while (lookahead != 0) {
  1267. /* Insert the string window[strstart .. strstart+2] in the
  1268. * dictionary, and set hash_head to the head of the hash chain:
  1269. */
  1270. INSERT_STRING(strstart, hash_head);
  1271. /* Find the longest match, discarding those <= prev_length.
  1272. * At this point we have always match_length < MIN_MATCH
  1273. */
  1274. if (hash_head != NIL && strstart - hash_head <= MAX_DIST &&
  1275. strstart <= window_size - MIN_LOOKAHEAD) {
  1276. /* To simplify the code, we prevent matches with the string
  1277. * of window index 0 (in particular we have to avoid a match
  1278. * of the string with itself at the start of the input file).
  1279. */
  1280. match_length = longest_match (hash_head);
  1281. /* longest_match() sets match_start */
  1282. if (match_length > lookahead) match_length = lookahead;
  1283. }
  1284. if (match_length >= MIN_MATCH) {
  1285. check_match(strstart, match_start, match_length);
  1286. flush = ct_tally(strstart-match_start, match_length - MIN_MATCH);
  1287. lookahead -= match_length;
  1288. RSYNC_ROLL(strstart, match_length);
  1289. /* Insert new strings in the hash table only if the match length
  1290. * is not too large. This saves time but degrades compression.
  1291. */
  1292. if (match_length <= max_insert_length) {
  1293. match_length--; /* string at strstart already in hash table */
  1294. do {
  1295. strstart++;
  1296. INSERT_STRING(strstart, hash_head);
  1297. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1298. * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  1299. * these bytes are garbage, but it does not matter since
  1300. * the next lookahead bytes will be emitted as literals.
  1301. */
  1302. } while (--match_length != 0);
  1303. strstart++;
  1304. } else {
  1305. strstart += match_length;
  1306. match_length = 0;
  1307. ins_h = window[strstart];
  1308. UPDATE_HASH(ins_h, window[strstart+1]);
  1309. #if MIN_MATCH != 3
  1310. Call UPDATE_HASH() MIN_MATCH-3 more times
  1311. #endif
  1312. }
  1313. } else {
  1314. /* No match, output a literal byte */
  1315. Tracevv((stderr,"%c",window[strstart]));
  1316. flush = ct_tally (0, window[strstart]);
  1317. RSYNC_ROLL(strstart, 1);
  1318. lookahead--;
  1319. strstart++;
  1320. }
  1321. if (rsync && strstart > rsync_chunk_end) {
  1322. rsync_chunk_end = 0xFFFFFFFFUL;
  1323. flush = 2;
  1324. }
  1325. if (flush) FLUSH_BLOCK(0), block_start = strstart;
  1326. /* Make sure that we always have enough lookahead, except
  1327. * at the end of the input file. We need MAX_MATCH bytes
  1328. * for the next match, plus MIN_MATCH bytes to insert the
  1329. * string following the next match.
  1330. */
  1331. while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
  1332. }
  1333. return FLUSH_BLOCK(1); /* eof */
  1334. }
  1335. /* ===========================================================================
  1336. * Same as above, but achieves better compression. We use a lazy
  1337. * evaluation for matches: a match is finally adopted only if there is
  1338. * no better match at the next window position.
  1339. */
  1340. off_t deflate()
  1341. {
  1342. IPos hash_head; /* head of hash chain */
  1343. IPos prev_match; /* previous match */
  1344. int flush; /* set if current block must be flushed */
  1345. int match_available = 0; /* set if previous match exists */
  1346. register unsigned match_length = MIN_MATCH-1; /* length of best match */
  1347. if (compr_level <= 3) return deflate_fast(); /* optimized for speed */
  1348. /* Process the input block. */
  1349. while (lookahead != 0) {
  1350. /* Insert the string window[strstart .. strstart+2] in the
  1351. * dictionary, and set hash_head to the head of the hash chain:
  1352. */
  1353. INSERT_STRING(strstart, hash_head);
  1354. /* Find the longest match, discarding those <= prev_length.
  1355. */
  1356. prev_length = match_length, prev_match = match_start;
  1357. match_length = MIN_MATCH-1;
  1358. if (hash_head != NIL && prev_length < max_lazy_match &&
  1359. strstart - hash_head <= MAX_DIST &&
  1360. strstart <= window_size - MIN_LOOKAHEAD) {
  1361. /* To simplify the code, we prevent matches with the string
  1362. * of window index 0 (in particular we have to avoid a match
  1363. * of the string with itself at the start of the input file).
  1364. */
  1365. match_length = longest_match (hash_head);
  1366. /* longest_match() sets match_start */
  1367. if (match_length > lookahead) match_length = lookahead;
  1368. /* Ignore a length 3 match if it is too distant: */
  1369. if (match_length == MIN_MATCH && strstart-match_start > TOO_FAR){
  1370. /* If prev_match is also MIN_MATCH, match_start is garbage
  1371. * but we will ignore the current match anyway.
  1372. */
  1373. match_length--;
  1374. }
  1375. }
  1376. /* If there was a match at the previous step and the current
  1377. * match is not better, output the previous match:
  1378. */
  1379. if (prev_length >= MIN_MATCH && match_length <= prev_length) {
  1380. check_match(strstart-1, prev_match, prev_length);
  1381. flush = ct_tally(strstart-1-prev_match, prev_length - MIN_MATCH);
  1382. /* Insert in hash table all strings up to the end of the match.
  1383. * strstart-1 and strstart are already inserted.
  1384. */
  1385. lookahead -= prev_length-1;
  1386. prev_length -= 2;
  1387. RSYNC_ROLL(strstart, prev_length+1);
  1388. do {
  1389. strstart++;
  1390. INSERT_STRING(strstart, hash_head);
  1391. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1392. * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  1393. * these bytes are garbage, but it does not matter since the
  1394. * next lookahead bytes will always be emitted as literals.
  1395. */
  1396. } while (--prev_length != 0);
  1397. match_available = 0;
  1398. match_length = MIN_MATCH-1;
  1399. strstart++;
  1400. if (rsync && strstart > rsync_chunk_end) {
  1401. rsync_chunk_end = 0xFFFFFFFFUL;
  1402. flush = 2;
  1403. }
  1404. if (flush) FLUSH_BLOCK(0), block_start = strstart;
  1405. } else if (match_available) {
  1406. /* If there was no match at the previous position, output a
  1407. * single literal. If there was a match but the current match
  1408. * is longer, truncate the previous match to a single literal.
  1409. */
  1410. Tracevv((stderr,"%c",window[strstart-1]));
  1411. flush = ct_tally (0, window[strstart-1]);
  1412. if (rsync && strstart > rsync_chunk_end) {
  1413. rsync_chunk_end = 0xFFFFFFFFUL;
  1414. flush = 2;
  1415. }
  1416. if (flush) FLUSH_BLOCK(0), block_start = strstart;
  1417. RSYNC_ROLL(strstart, 1);
  1418. strstart++;
  1419. lookahead--;
  1420. } else {
  1421. /* There is no previous match to compare with, wait for
  1422. * the next step to decide.
  1423. */
  1424. if (rsync && strstart > rsync_chunk_end) {
  1425. /* Reset huffman tree */
  1426. rsync_chunk_end = 0xFFFFFFFFUL;
  1427. flush = 2;
  1428. FLUSH_BLOCK(0), block_start = strstart;
  1429. }
  1430. match_available = 1;
  1431. RSYNC_ROLL(strstart, 1);
  1432. strstart++;
  1433. lookahead--;
  1434. }
  1435. Assert (strstart <= bytes_in && lookahead <= bytes_in, "a bit too far");
  1436. /* Make sure that we always have enough lookahead, except
  1437. * at the end of the input file. We need MAX_MATCH bytes
  1438. * for the next match, plus MIN_MATCH bytes to insert the
  1439. * string following the next match.
  1440. */
  1441. while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
  1442. }
  1443. if (match_available) ct_tally (0, window[strstart-1]);
  1444. return FLUSH_BLOCK(1); /* eof */
  1445. }
  1446. /* Getopt for GNU.
  1447. NOTE: getopt is now part of the C library, so if you don't know what
  1448. "Keep this file name-space clean" means, talk to drepper@gnu.org
  1449. before changing it!
  1450. Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001
  1451. Free Software Foundation, Inc.
  1452. This file is part of the GNU C Library.
  1453. This program is free software; you can redistribute it and/or modify
  1454. it under the terms of the GNU General Public License as published by
  1455. the Free Software Foundation; either version 2, or (at your option)
  1456. any later version.
  1457. This program is distributed in the hope that it will be useful,
  1458. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1459. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1460. GNU General Public License for more details.
  1461. You should have received a copy of the GNU General Public License
  1462. along with this program; if not, write to the Free Software Foundation,
  1463. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  1464. /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
  1465. Ditto for AIX 3.2 and <stdlib.h>. */
  1466. #ifndef _NO_PROTO
  1467. # define _NO_PROTO
  1468. #endif
  1469. #if !defined __STDC__ || !__STDC__
  1470. /* This is a separate conditional since some stdc systems
  1471. reject `defined (const)'. */
  1472. # ifndef const
  1473. # define const
  1474. # endif
  1475. #endif
  1476. /* This needs to come after some library #include
  1477. to get __GNU_LIBRARY__ defined. */
  1478. # define _(msgid) (msgid)
  1479. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  1480. but it behaves differently for the user, since it allows the user
  1481. to intersperse the options with the other arguments.
  1482. As `getopt' works, it permutes the elements of ARGV so that,
  1483. when it is done, all the options precede everything else. Thus
  1484. all application programs are extended to handle flexible argument order.
  1485. Setting the environment variable POSIXLY_CORRECT disables permutation.
  1486. Then the behavior is completely standard.
  1487. GNU application programs can use a third alternative mode in which
  1488. they can distinguish the relative order of options and other arguments. */
  1489. /* Declarations for getopt.
  1490. Copyright (C) 1989-1994, 1996-1999, 2001 Free Software Foundation, Inc.
  1491. This file is part of the GNU C Library.
  1492. This program is free software; you can redistribute it and/or modify
  1493. it under the terms of the GNU General Public License as published by
  1494. the Free Software Foundation; either version 2, or (at your option)
  1495. any later version.
  1496. This program is distributed in the hope that it will be useful,
  1497. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1498. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1499. GNU General Public License for more details.
  1500. You should have received a copy of the GNU General Public License
  1501. along with this program; if not, write to the Free Software Foundation,
  1502. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  1503. #ifndef _GETOPT_H
  1504. #ifndef __need_getopt
  1505. # define _GETOPT_H 1
  1506. #endif
  1507. #ifdef __cplusplus
  1508. extern "C" {
  1509. #endif
  1510. /* For communication from `getopt' to the caller.
  1511. When `getopt' finds an option that takes an argument,
  1512. the argument value is returned here.
  1513. Also, when `ordering' is RETURN_IN_ORDER,
  1514. each non-option ARGV-element is returned here. */
  1515. extern char *optarg;
  1516. /* Index in ARGV of the next element to be scanned.
  1517. This is used for communication to and from the caller
  1518. and for communication between successive calls to `getopt'.
  1519. On entry to `getopt', zero means this is the first call; initialize.
  1520. When `getopt' returns -1, this is the index of the first of the
  1521. non-option elements that the caller should itself scan.
  1522. Otherwise, `optind' communicates from one call to the next
  1523. how much of ARGV has been scanned so far. */
  1524. extern int optind;
  1525. /* Callers store zero here to inhibit the error message `getopt' prints
  1526. for unrecognized options. */
  1527. extern int opterr;
  1528. /* Set to an option character which was unrecognized. */
  1529. extern int optopt;
  1530. /* Describe the long-named options requested by the application.
  1531. The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
  1532. of `struct option' terminated by an element containing a name which is
  1533. zero.
  1534. The field `has_arg' is:
  1535. no_argument (or 0) if the option does not take an argument,
  1536. required_argument (or 1) if the option requires an argument,
  1537. optional_argument (or 2) if the option takes an optional argument.
  1538. If the field `flag' is not NULL, it points to a variable that is set
  1539. to the value given in the field `val' when the option is found, but
  1540. left unchanged if the option is not found.
  1541. To have a long-named option do something other than set an `int' to
  1542. a compiled-in constant, such as set a value from `optarg', set the
  1543. option's `flag' field to zero and its `val' field to a nonzero
  1544. value (the equivalent single-letter option character, if there is
  1545. one). For long options that have a zero `flag' field, `getopt'
  1546. returns the contents of the `val' field. */
  1547. struct option
  1548. {
  1549. # if (defined __STDC__ && __STDC__) || defined __cplusplus
  1550. const char *name;
  1551. # else
  1552. char *name;
  1553. # endif
  1554. /* has_arg can't be an enum because some compilers complain about
  1555. type mismatches in all the code that assumes it is an int. */
  1556. int has_arg;
  1557. int *flag;
  1558. int val;
  1559. };
  1560. /* Names for the values of the `has_arg' field of `struct option'. */
  1561. # define no_argument 0
  1562. # define required_argument 1
  1563. # define optional_argument 2
  1564. /* Get definitions and prototypes for functions to process the
  1565. arguments in ARGV (ARGC of them, minus the program name) for
  1566. options given in OPTS.
  1567. Return the option character from OPTS just read. Return -1 when
  1568. there are no more options. For unrecognized options, or options
  1569. missing arguments, `optopt' is set to the option letter, and '?' is
  1570. returned.
  1571. The OPTS string is a list of characters which are recognized option
  1572. letters, optionally followed by colons, specifying that that letter
  1573. takes an argument, to be placed in `optarg'.
  1574. If a letter in OPTS is followed by two colons, its argument is
  1575. optional. This behavior is specific to the GNU `getopt'.
  1576. The argument `--' causes premature termination of argument
  1577. scanning, explicitly telling `getopt' that there are no more
  1578. options.
  1579. If OPTS begins with `--', then non-option arguments are treated as
  1580. arguments to the option '\0'. This behavior is specific to the GNU
  1581. `getopt'. */
  1582. #if (defined __STDC__ && __STDC__) || defined __cplusplus
  1583. # ifdef __GNU_LIBRARY__
  1584. /* Many other libraries have conflicting prototypes for getopt, with
  1585. differences in the consts, in stdlib.h. To avoid compilation
  1586. errors, only prototype getopt for the GNU C library. */
  1587. extern int getopt (int __argc, char *const *__argv, const char *__shortopts);
  1588. # else /* not __GNU_LIBRARY__ */
  1589. extern int getopt ();
  1590. # endif /* __GNU_LIBRARY__ */
  1591. # ifndef __need_getopt
  1592. extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts,
  1593. const struct option *__longopts, int *__longind);
  1594. extern int getopt_long_only (int __argc, char *const *__argv,
  1595. const char *__shortopts,
  1596. const struct option *__longopts, int *__longind);
  1597. /* Internal only. Users should not call this directly. */
  1598. extern int _getopt_internal (int __argc, char *const *__argv,
  1599. const char *__shortopts,
  1600. const struct option *__longopts, int *__longind,
  1601. int __long_only);
  1602. # endif
  1603. #else /* not __STDC__ */
  1604. extern int getopt ();
  1605. # ifndef __need_getopt
  1606. extern int getopt_long ();
  1607. extern int getopt_long_only ();
  1608. extern int _getopt_internal ();
  1609. # endif
  1610. #endif /* __STDC__ */
  1611. #ifdef __cplusplus
  1612. }
  1613. #endif
  1614. /* Make sure we later can get all the definitions and declarations. */
  1615. #undef __need_getopt
  1616. #endif /* getopt.h */
  1617. /* For communication from `getopt' to the caller.
  1618. When `getopt' finds an option that takes an argument,
  1619. the argument value is returned here.
  1620. Also, when `ordering' is RETURN_IN_ORDER,
  1621. each non-option ARGV-element is returned here. */
  1622. char *optarg;
  1623. /* Index in ARGV of the next element to be scanned.
  1624. This is used for communication to and from the caller
  1625. and for communication between successive calls to `getopt'.
  1626. On entry to `getopt', zero means this is the first call; initialize.
  1627. When `getopt' returns -1, this is the index of the first of the
  1628. non-option elements that the caller should itself scan.
  1629. Otherwise, `optind' communicates from one call to the next
  1630. how much of ARGV has been scanned so far. */
  1631. /* 1003.2 says this must be 1 before any call. */
  1632. int optind = 1;
  1633. /* Formerly, initialization of getopt depended on optind==0, which
  1634. causes problems with re-calling getopt as programs generally don't
  1635. know that. */
  1636. int __getopt_initialized;
  1637. /* The next char to be scanned in the option-element
  1638. in which the last option character we returned was found.
  1639. This allows us to pick up the scan where we left off.
  1640. If this is zero, or a null string, it means resume the scan
  1641. by advancing to the next ARGV-element. */
  1642. static char *nextchar;
  1643. /* Callers store zero here to inhibit the error message
  1644. for unrecognized options. */
  1645. int opterr = 1;
  1646. /* Set to an option character which was unrecognized.
  1647. This must be initialized on some systems to avoid linking in the
  1648. system's own getopt implementation. */
  1649. int optopt = '?';
  1650. /* Describe how to deal with options that follow non-option ARGV-elements.
  1651. If the caller did not specify anything,
  1652. the default is REQUIRE_ORDER if the environment variable
  1653. POSIXLY_CORRECT is defined, PERMUTE otherwise.
  1654. REQUIRE_ORDER means don't recognize them as options;
  1655. stop option processing when the first non-option is seen.
  1656. This is what Unix does.
  1657. This mode of operation is selected by either setting the environment
  1658. variable POSIXLY_CORRECT, or using `+' as the first character
  1659. of the list of option characters.
  1660. PERMUTE is the default. We permute the contents of ARGV as we scan,
  1661. so that eventually all the non-options are at the end. This allows options
  1662. to be given in any order, even with programs that were not written to
  1663. expect this.
  1664. RETURN_IN_ORDER is an option available to programs that were written
  1665. to expect options and other ARGV-elements in any order and that care about
  1666. the ordering of the two. We describe each non-option ARGV-element
  1667. as if it were the argument of an option with character code 1.
  1668. Using `-' as the first character of the list of option characters
  1669. selects this mode of operation.
  1670. The special argument `--' forces an end of option-scanning regardless
  1671. of the value of `ordering'. In the case of RETURN_IN_ORDER, only
  1672. `--' can cause `getopt' to return -1 with `optind' != ARGC. */
  1673. static enum
  1674. {
  1675. REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
  1676. } ordering;
  1677. /* Value of POSIXLY_CORRECT environment variable. */
  1678. static char *posixly_correct;
  1679. # define my_index strchr
  1680. /* Avoid depending on library functions or files
  1681. whose names are inconsistent. */
  1682. #ifndef getenv
  1683. extern char *getenv ();
  1684. #endif
  1685. char *
  1686. my_index (str, chr)
  1687. const char *str;
  1688. int chr;
  1689. {
  1690. while (*str)
  1691. {
  1692. if (*str == chr)
  1693. return (char *) str;
  1694. str++;
  1695. }
  1696. return 0;
  1697. }
  1698. /* If using GCC, we can safely declare strlen this way.
  1699. If not using GCC, it is ok not to declare it. */
  1700. #ifdef __GNUC__
  1701. /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
  1702. That was relevant to code that was here before. */
  1703. # if (!defined __STDC__ || !__STDC__) && !defined strlen
  1704. /* gcc with -traditional declares the built-in strlen to return int,
  1705. and has done so at least since version 2.4.5. -- rms. */
  1706. extern int strlen (const char *);
  1707. # endif /* not __STDC__ */
  1708. #endif /* __GNUC__ */
  1709. /* Handle permutation of arguments. */
  1710. /* Describe the part of ARGV that contains non-options that have
  1711. been skipped. `first_nonopt' is the index in ARGV of the first of them;
  1712. `last_nonopt' is the index after the last of them. */
  1713. static int first_nonopt;
  1714. static int last_nonopt;
  1715. #ifdef _LIBC
  1716. /* Bash 2.0 gives us an environment variable containing flags
  1717. indicating ARGV elements that should not be considered arguments. */
  1718. #ifdef USE_NONOPTION_FLAGS
  1719. /* Defined in getopt_init.c */
  1720. extern char *__getopt_nonoption_flags;
  1721. static int nonoption_flags_max_len;
  1722. static int nonoption_flags_len;
  1723. #endif
  1724. static int original_argc;
  1725. static char *const *original_argv;
  1726. /* Make sure the environment variable bash 2.0 puts in the environment
  1727. is valid for the getopt call we must make sure that the ARGV passed
  1728. to getopt is that one passed to the process. */
  1729. static void
  1730. __attribute__ ((unused))
  1731. store_args_and_env (int argc, char *const *argv)
  1732. {
  1733. /* XXX This is no good solution. We should rather copy the args so
  1734. that we can compare them later. But we must not use malloc(3). */
  1735. original_argc = argc;
  1736. original_argv = argv;
  1737. }
  1738. # ifdef text_set_element
  1739. text_set_element (__libc_subinit, store_args_and_env);
  1740. # endif /* text_set_element */
  1741. # ifdef USE_NONOPTION_FLAGS
  1742. # define SWAP_FLAGS(ch1, ch2) \
  1743. if (nonoption_flags_len > 0) \
  1744. { \
  1745. char __tmp = __getopt_nonoption_flags[ch1]; \
  1746. __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
  1747. __getopt_nonoption_flags[ch2] = __tmp; \
  1748. }
  1749. # else
  1750. # define SWAP_FLAGS(ch1, ch2)
  1751. # endif
  1752. #else /* !_LIBC */
  1753. # define SWAP_FLAGS(ch1, ch2)
  1754. #endif /* _LIBC */
  1755. /* Exchange two adjacent subsequences of ARGV.
  1756. One subsequence is elements [first_nonopt,last_nonopt)
  1757. which contains all the non-options that have been skipped so far.
  1758. The other is elements [last_nonopt,optind), which contains all
  1759. the options processed since those non-options were skipped.
  1760. `first_nonopt' and `last_nonopt' are relocated so that they describe
  1761. the new indices of the non-options in ARGV after they are moved. */
  1762. #if defined __STDC__ && __STDC__
  1763. static void exchange (char **);
  1764. #endif
  1765. static void
  1766. exchange (argv)
  1767. char **argv;
  1768. {
  1769. int bottom = first_nonopt;
  1770. int middle = last_nonopt;
  1771. int top = optind;
  1772. char *tem;
  1773. /* Exchange the shorter segment with the far end of the longer segment.
  1774. That puts the shorter segment into the right place.
  1775. It leaves the longer segment in the right place overall,
  1776. but it consists of two parts that need to be swapped next. */
  1777. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  1778. /* First make sure the handling of the `__getopt_nonoption_flags'
  1779. string can work normally. Our top argument must be in the range
  1780. of the string. */
  1781. if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
  1782. {
  1783. /* We must extend the array. The user plays games with us and
  1784. presents new arguments. */
  1785. char *new_str = malloc (top + 1);
  1786. if (new_str == NULL)
  1787. nonoption_flags_len = nonoption_flags_max_len = 0;
  1788. else
  1789. {
  1790. memset (__mempcpy (new_str, __getopt_nonoption_flags,
  1791. nonoption_flags_max_len),
  1792. '\0', top + 1 - nonoption_flags_max_len);
  1793. nonoption_flags_max_len = top + 1;
  1794. __getopt_nonoption_flags = new_str;
  1795. }
  1796. }
  1797. #endif
  1798. while (top > middle && middle > bottom)
  1799. {
  1800. if (top - middle > middle - bottom)
  1801. {
  1802. /* Bottom segment is the short one. */
  1803. int len = middle - bottom;
  1804. register int i;
  1805. /* Swap it with the top part of the top segment. */
  1806. for (i = 0; i < len; i++)
  1807. {
  1808. tem = argv[bottom + i];
  1809. argv[bottom + i] = argv[top - (middle - bottom) + i];
  1810. argv[top - (middle - bottom) + i] = tem;
  1811. SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
  1812. }
  1813. /* Exclude the moved bottom segment from further swapping. */
  1814. top -= len;
  1815. }
  1816. else
  1817. {
  1818. /* Top segment is the short one. */
  1819. int len = top - middle;
  1820. register int i;
  1821. /* Swap it with the bottom part of the bottom segment. */
  1822. for (i = 0; i < len; i++)
  1823. {
  1824. tem = argv[bottom + i];
  1825. argv[bottom + i] = argv[middle + i];
  1826. argv[middle + i] = tem;
  1827. SWAP_FLAGS (bottom + i, middle + i);
  1828. }
  1829. /* Exclude the moved top segment from further swapping. */
  1830. bottom += len;
  1831. }
  1832. }
  1833. /* Update records for the slots the non-options now occupy. */
  1834. first_nonopt += (optind - last_nonopt);
  1835. last_nonopt = optind;
  1836. }
  1837. /* Initialize the internal data when the first call is made. */
  1838. #if defined __STDC__ && __STDC__
  1839. static const char *_getopt_initialize (int, char *const *, const char *);
  1840. #endif
  1841. static const char *
  1842. _getopt_initialize (argc, argv, optstring)
  1843. int argc;
  1844. char *const *argv;
  1845. const char *optstring;
  1846. {
  1847. /* Start processing options with ARGV-element 1 (since ARGV-element 0
  1848. is the program name); the sequence of previously skipped
  1849. non-option ARGV-elements is empty. */
  1850. first_nonopt = last_nonopt = optind;
  1851. nextchar = NULL;
  1852. posixly_correct = getenv ("POSIXLY_CORRECT");
  1853. /* Determine how to handle the ordering of options and nonoptions. */
  1854. if (optstring[0] == '-')
  1855. {
  1856. ordering = RETURN_IN_ORDER;
  1857. ++optstring;
  1858. }
  1859. else if (optstring[0] == '+')
  1860. {
  1861. ordering = REQUIRE_ORDER;
  1862. ++optstring;
  1863. }
  1864. else if (posixly_correct != NULL)
  1865. ordering = REQUIRE_ORDER;
  1866. else
  1867. ordering = PERMUTE;
  1868. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  1869. if (posixly_correct == NULL
  1870. && argc == original_argc && argv == original_argv)
  1871. {
  1872. if (nonoption_flags_max_len == 0)
  1873. {
  1874. if (__getopt_nonoption_flags == NULL
  1875. || __getopt_nonoption_flags[0] == '\0')
  1876. nonoption_flags_max_len = -1;
  1877. else
  1878. {
  1879. const char *orig_str = __getopt_nonoption_flags;
  1880. int len = nonoption_flags_max_len = strlen (orig_str);
  1881. if (nonoption_flags_max_len < argc)
  1882. nonoption_flags_max_len = argc;
  1883. __getopt_nonoption_flags =
  1884. (char *) malloc (nonoption_flags_max_len);
  1885. if (__getopt_nonoption_flags == NULL)
  1886. nonoption_flags_max_len = -1;
  1887. else
  1888. memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
  1889. '\0', nonoption_flags_max_len - len);
  1890. }
  1891. }
  1892. nonoption_flags_len = nonoption_flags_max_len;
  1893. }
  1894. else
  1895. nonoption_flags_len = 0;
  1896. #endif
  1897. return optstring;
  1898. }
  1899. /* Scan elements of ARGV (whose length is ARGC) for option characters
  1900. given in OPTSTRING.
  1901. If an element of ARGV starts with '-', and is not exactly "-" or "--",
  1902. then it is an option element. The characters of this element
  1903. (aside from the initial '-') are option characters. If `getopt'
  1904. is called repeatedly, it returns successively each of the option characters
  1905. from each of the option elements.
  1906. If `getopt' finds another option character, it returns that character,
  1907. updating `optind' and `nextchar' so that the next call to `getopt' can
  1908. resume the scan with the following option character or ARGV-element.
  1909. If there are no more option characters, `getopt' returns -1.
  1910. Then `optind' is the index in ARGV of the first ARGV-element
  1911. that is not an option. (The ARGV-elements have been permuted
  1912. so that those that are not options now come last.)
  1913. OPTSTRING is a string containing the legitimate option characters.
  1914. If an option character is seen that is not listed in OPTSTRING,
  1915. return '?' after printing an error message. If you set `opterr' to
  1916. zero, the error message is suppressed but we still return '?'.
  1917. If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  1918. so the following text in the same ARGV-element, or the text of the following
  1919. ARGV-element, is returned in `optarg'. Two colons mean an option that
  1920. wants an optional arg; if there is text in the current ARGV-element,
  1921. it is returned in `optarg', otherwise `optarg' is set to zero.
  1922. If OPTSTRING starts with `-' or `+', it requests different methods of
  1923. handling the non-option ARGV-elements.
  1924. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  1925. Long-named options begin with `--' instead of `-'.
  1926. Their names may be abbreviated as long as the abbreviation is unique
  1927. or is an exact match for some defined option. If they have an
  1928. argument, it follows the option name in the same ARGV-element, separated
  1929. from the option name by a `=', or else the in next ARGV-element.
  1930. When `getopt' finds a long-named option, it returns 0 if that option's
  1931. `flag' field is nonzero, the value of the option's `val' field
  1932. if the `flag' field is zero.
  1933. The elements of ARGV aren't really const, because we permute them.
  1934. But we pretend they're const in the prototype to be compatible
  1935. with other systems.
  1936. LONGOPTS is a vector of `struct option' terminated by an
  1937. element containing a name which is zero.
  1938. LONGIND returns the index in LONGOPT of the long-named option found.
  1939. It is only valid when a long-named option has been found by the most
  1940. recent call.
  1941. If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  1942. long-named options. */
  1943. int
  1944. _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
  1945. int argc;
  1946. char *const *argv;
  1947. const char *optstring;
  1948. const struct option *longopts;
  1949. int *longind;
  1950. int long_only;
  1951. {
  1952. int print_errors = opterr;
  1953. if (optstring[0] == ':')
  1954. print_errors = 0;
  1955. if (argc < 1)
  1956. return -1;
  1957. optarg = NULL;
  1958. if (optind == 0 || !__getopt_initialized)
  1959. {
  1960. if (optind == 0)
  1961. optind = 1; /* Don't scan ARGV[0], the program name. */
  1962. optstring = _getopt_initialize (argc, argv, optstring);
  1963. __getopt_initialized = 1;
  1964. }
  1965. /* Test whether ARGV[optind] points to a non-option argument.
  1966. Either it does not have option syntax, or there is an environment flag
  1967. from the shell indicating it is not an option. The later information
  1968. is only used when the used in the GNU libc. */
  1969. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  1970. # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
  1971. || (optind < nonoption_flags_len \
  1972. && __getopt_nonoption_flags[optind] == '1'))
  1973. #else
  1974. # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
  1975. #endif
  1976. if (nextchar == NULL || *nextchar == '\0')
  1977. {
  1978. /* Advance to the next ARGV-element. */
  1979. /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
  1980. moved back by the user (who may also have changed the arguments). */
  1981. if (last_nonopt > optind)
  1982. last_nonopt = optind;
  1983. if (first_nonopt > optind)
  1984. first_nonopt = optind;
  1985. if (ordering == PERMUTE)
  1986. {
  1987. /* If we have just processed some options following some non-options,
  1988. exchange them so that the options come first. */
  1989. if (first_nonopt != last_nonopt && last_nonopt != optind)
  1990. exchange ((char **) argv);
  1991. else if (last_nonopt != optind)
  1992. first_nonopt = optind;
  1993. /* Skip any additional non-options
  1994. and extend the range of non-options previously skipped. */
  1995. while (optind < argc && NONOPTION_P)
  1996. optind++;
  1997. last_nonopt = optind;
  1998. }
  1999. /* The special ARGV-element `--' means premature end of options.
  2000. Skip it like a null option,
  2001. then exchange with previous non-options as if it were an option,
  2002. then skip everything else like a non-option. */
  2003. if (optind != argc && !strcmp (argv[optind], "--"))
  2004. {
  2005. optind++;
  2006. if (first_nonopt != last_nonopt && last_nonopt != optind)
  2007. exchange ((char **) argv);
  2008. else if (first_nonopt == last_nonopt)
  2009. first_nonopt = optind;
  2010. last_nonopt = argc;
  2011. optind = argc;
  2012. }
  2013. /* If we have done all the ARGV-elements, stop the scan
  2014. and back over any non-options that we skipped and permuted. */
  2015. if (optind == argc)
  2016. {
  2017. /* Set the next-arg-index to point at the non-options
  2018. that we previously skipped, so the caller will digest them. */
  2019. if (first_nonopt != last_nonopt)
  2020. optind = first_nonopt;
  2021. return -1;
  2022. }
  2023. /* If we have come to a non-option and did not permute it,
  2024. either stop the scan or describe it to the caller and pass it by. */
  2025. if (NONOPTION_P)
  2026. {
  2027. if (ordering == REQUIRE_ORDER)
  2028. return -1;
  2029. optarg = argv[optind++];
  2030. return 1;
  2031. }
  2032. /* We have found another option-ARGV-element.
  2033. Skip the initial punctuation. */
  2034. nextchar = (argv[optind] + 1
  2035. + (longopts != NULL && argv[optind][1] == '-'));
  2036. }
  2037. /* Decode the current option-ARGV-element. */
  2038. /* Check whether the ARGV-element is a long option.
  2039. If long_only and the ARGV-element has the form "-f", where f is
  2040. a valid short option, don't consider it an abbreviated form of
  2041. a long option that starts with f. Otherwise there would be no
  2042. way to give the -f short option.
  2043. On the other hand, if there's a long option "fubar" and
  2044. the ARGV-element is "-fu", do consider that an abbreviation of
  2045. the long option, just like "--fu", and not "-f" with arg "u".
  2046. This distinction seems to be the most useful approach. */
  2047. if (longopts != NULL
  2048. && (argv[optind][1] == '-'
  2049. || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
  2050. {
  2051. char *nameend;
  2052. const struct option *p;
  2053. const struct option *pfound = NULL;
  2054. int exact = 0;
  2055. int ambig = 0;
  2056. int indfound = -1;
  2057. int option_index;
  2058. for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
  2059. /* Do nothing. */ ;
  2060. /* Test all long options for either exact match
  2061. or abbreviated matches. */
  2062. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  2063. if (!strncmp (p->name, nextchar, nameend - nextchar))
  2064. {
  2065. if ((unsigned int) (nameend - nextchar)
  2066. == (unsigned int) strlen (p->name))
  2067. {
  2068. /* Exact match found. */
  2069. pfound = p;
  2070. indfound = option_index;
  2071. exact = 1;
  2072. break;
  2073. }
  2074. else if (pfound == NULL)
  2075. {
  2076. /* First nonexact match found. */
  2077. pfound = p;
  2078. indfound = option_index;
  2079. }
  2080. else if (long_only
  2081. || pfound->has_arg != p->has_arg
  2082. || pfound->flag != p->flag
  2083. || pfound->val != p->val)
  2084. /* Second or later nonexact match found. */
  2085. ambig = 1;
  2086. }
  2087. if (ambig && !exact)
  2088. {
  2089. if (print_errors)
  2090. fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
  2091. argv[0], argv[optind]);
  2092. nextchar += strlen (nextchar);
  2093. optind++;
  2094. optopt = 0;
  2095. return '?';
  2096. }
  2097. if (pfound != NULL)
  2098. {
  2099. option_index = indfound;
  2100. optind++;
  2101. if (*nameend)
  2102. {
  2103. /* Don't test has_arg with >, because some C compilers don't
  2104. allow it to be used on enums. */
  2105. if (pfound->has_arg)
  2106. optarg = nameend + 1;
  2107. else
  2108. {
  2109. if (print_errors)
  2110. {
  2111. if (argv[optind - 1][1] == '-')
  2112. /* --option */
  2113. fprintf (stderr,
  2114. _("%s: option `--%s' doesn't allow an argument\n"),
  2115. argv[0], pfound->name);
  2116. else
  2117. /* +option or -option */
  2118. fprintf (stderr,
  2119. _("%s: option `%c%s' doesn't allow an argument\n"),
  2120. argv[0], argv[optind - 1][0], pfound->name);
  2121. }
  2122. nextchar += strlen (nextchar);
  2123. optopt = pfound->val;
  2124. return '?';
  2125. }
  2126. }
  2127. else if (pfound->has_arg == 1)
  2128. {
  2129. if (optind < argc)
  2130. optarg = argv[optind++];
  2131. else
  2132. {
  2133. if (print_errors)
  2134. fprintf (stderr,
  2135. _("%s: option `%s' requires an argument\n"),
  2136. argv[0], argv[optind - 1]);
  2137. nextchar += strlen (nextchar);
  2138. optopt = pfound->val;
  2139. return optstring[0] == ':' ? ':' : '?';
  2140. }
  2141. }
  2142. nextchar += strlen (nextchar);
  2143. if (longind != NULL)
  2144. *longind = option_index;
  2145. if (pfound->flag)
  2146. {
  2147. *(pfound->flag) = pfound->val;
  2148. return 0;
  2149. }
  2150. return pfound->val;
  2151. }
  2152. /* Can't find it as a long option. If this is not getopt_long_only,
  2153. or the option starts with '--' or is not a valid short
  2154. option, then it's an error.
  2155. Otherwise interpret it as a short option. */
  2156. if (!long_only || argv[optind][1] == '-'
  2157. || my_index (optstring, *nextchar) == NULL)
  2158. {
  2159. if (print_errors)
  2160. {
  2161. if (argv[optind][1] == '-')
  2162. /* --option */
  2163. fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
  2164. argv[0], nextchar);
  2165. else
  2166. /* +option or -option */
  2167. fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
  2168. argv[0], argv[optind][0], nextchar);
  2169. }
  2170. nextchar = (char *) "";
  2171. optind++;
  2172. optopt = 0;
  2173. return '?';
  2174. }
  2175. }
  2176. /* Look at and handle the next short option-character. */
  2177. {
  2178. char c = *nextchar++;
  2179. char *temp = my_index (optstring, c);
  2180. /* Increment `optind' when we start to process its last character. */
  2181. if (*nextchar == '\0')
  2182. ++optind;
  2183. if (temp == NULL || c == ':')
  2184. {
  2185. if (print_errors)
  2186. {
  2187. if (posixly_correct)
  2188. /* 1003.2 specifies the format of this message. */
  2189. fprintf (stderr, _("%s: illegal option -- %c\n"),
  2190. argv[0], c);
  2191. else
  2192. fprintf (stderr, _("%s: invalid option -- %c\n"),
  2193. argv[0], c);
  2194. }
  2195. optopt = c;
  2196. return '?';
  2197. }
  2198. /* Convenience. Treat POSIX -W foo same as long option --foo */
  2199. if (temp[0] == 'W' && temp[1] == ';')
  2200. {
  2201. char *nameend;
  2202. const struct option *p;
  2203. const struct option *pfound = NULL;
  2204. int exact = 0;
  2205. int ambig = 0;
  2206. int indfound = 0;
  2207. int option_index;
  2208. /* This is an option that requires an argument. */
  2209. if (*nextchar != '\0')
  2210. {
  2211. optarg = nextchar;
  2212. /* If we end this ARGV-element by taking the rest as an arg,
  2213. we must advance to the next element now. */
  2214. optind++;
  2215. }
  2216. else if (optind == argc)
  2217. {
  2218. if (print_errors)
  2219. {
  2220. /* 1003.2 specifies the format of this message. */
  2221. fprintf (stderr, _("%s: option requires an argument -- %c\n"),
  2222. argv[0], c);
  2223. }
  2224. optopt = c;
  2225. if (optstring[0] == ':')
  2226. c = ':';
  2227. else
  2228. c = '?';
  2229. return c;
  2230. }
  2231. else
  2232. /* We already incremented `optind' once;
  2233. increment it again when taking next ARGV-elt as argument. */
  2234. optarg = argv[optind++];
  2235. /* optarg is now the argument, see if it's in the
  2236. table of longopts. */
  2237. for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
  2238. /* Do nothing. */ ;
  2239. /* Test all long options for either exact match
  2240. or abbreviated matches. */
  2241. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  2242. if (!strncmp (p->name, nextchar, nameend - nextchar))
  2243. {
  2244. if ((unsigned int) (nameend - nextchar) == strlen (p->name))
  2245. {
  2246. /* Exact match found. */
  2247. pfound = p;
  2248. indfound = option_index;
  2249. exact = 1;
  2250. break;
  2251. }
  2252. else if (pfound == NULL)
  2253. {
  2254. /* First nonexact match found. */
  2255. pfound = p;
  2256. indfound = option_index;
  2257. }
  2258. else
  2259. /* Second or later nonexact match found. */
  2260. ambig = 1;
  2261. }
  2262. if (ambig && !exact)
  2263. {
  2264. if (print_errors)
  2265. fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
  2266. argv[0], argv[optind]);
  2267. nextchar += strlen (nextchar);
  2268. optind++;
  2269. return '?';
  2270. }
  2271. if (pfound != NULL)
  2272. {
  2273. option_index = indfound;
  2274. if (*nameend)
  2275. {
  2276. /* Don't test has_arg with >, because some C compilers don't
  2277. allow it to be used on enums. */
  2278. if (pfound->has_arg)
  2279. optarg = nameend + 1;
  2280. else
  2281. {
  2282. if (print_errors)
  2283. fprintf (stderr, _("\
  2284. %s: option `-W %s' doesn't allow an argument\n"),
  2285. argv[0], pfound->name);
  2286. nextchar += strlen (nextchar);
  2287. return '?';
  2288. }
  2289. }
  2290. else if (pfound->has_arg == 1)
  2291. {
  2292. if (optind < argc)
  2293. optarg = argv[optind++];
  2294. else
  2295. {
  2296. if (print_errors)
  2297. fprintf (stderr,
  2298. _("%s: option `%s' requires an argument\n"),
  2299. argv[0], argv[optind - 1]);
  2300. nextchar += strlen (nextchar);
  2301. return optstring[0] == ':' ? ':' : '?';
  2302. }
  2303. }
  2304. nextchar += strlen (nextchar);
  2305. if (longind != NULL)
  2306. *longind = option_index;
  2307. if (pfound->flag)
  2308. {
  2309. *(pfound->flag) = pfound->val;
  2310. return 0;
  2311. }
  2312. return pfound->val;
  2313. }
  2314. nextchar = NULL;
  2315. return 'W'; /* Let the application handle it. */
  2316. }
  2317. if (temp[1] == ':')
  2318. {
  2319. if (temp[2] == ':')
  2320. {
  2321. /* This is an option that accepts an argument optionally. */
  2322. if (*nextchar != '\0')
  2323. {
  2324. optarg = nextchar;
  2325. optind++;
  2326. }
  2327. else
  2328. optarg = NULL;
  2329. nextchar = NULL;
  2330. }
  2331. else
  2332. {
  2333. /* This is an option that requires an argument. */
  2334. if (*nextchar != '\0')
  2335. {
  2336. optarg = nextchar;
  2337. /* If we end this ARGV-element by taking the rest as an arg,
  2338. we must advance to the next element now. */
  2339. optind++;
  2340. }
  2341. else if (optind == argc)
  2342. {
  2343. if (print_errors)
  2344. {
  2345. /* 1003.2 specifies the format of this message. */
  2346. fprintf (stderr,
  2347. _("%s: option requires an argument -- %c\n"),
  2348. argv[0], c);
  2349. }
  2350. optopt = c;
  2351. if (optstring[0] == ':')
  2352. c = ':';
  2353. else
  2354. c = '?';
  2355. }
  2356. else
  2357. /* We already incremented `optind' once;
  2358. increment it again when taking next ARGV-elt as argument. */
  2359. optarg = argv[optind++];
  2360. nextchar = NULL;
  2361. }
  2362. }
  2363. return c;
  2364. }
  2365. }
  2366. int
  2367. getopt (argc, argv, optstring)
  2368. int argc;
  2369. char *const *argv;
  2370. const char *optstring;
  2371. {
  2372. return _getopt_internal (argc, argv, optstring,
  2373. (const struct option *) 0,
  2374. (int *) 0,
  2375. 0);
  2376. }
  2377. /* getopt_long and getopt_long_only entry points for GNU getopt.
  2378. Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
  2379. Free Software Foundation, Inc.
  2380. This file is part of the GNU C Library.
  2381. This program is free software; you can redistribute it and/or modify
  2382. it under the terms of the GNU General Public License as published by
  2383. the Free Software Foundation; either version 2, or (at your option)
  2384. any later version.
  2385. This program is distributed in the hope that it will be useful,
  2386. but WITHOUT ANY WARRANTY; without even the implied warranty of
  2387. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2388. GNU General Public License for more details.
  2389. You should have received a copy of the GNU General Public License
  2390. along with this program; if not, write to the Free Software Foundation,
  2391. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  2392. #if !defined __STDC__ || !__STDC__
  2393. /* This is a separate conditional since some stdc systems
  2394. reject `defined (const)'. */
  2395. #ifndef const
  2396. #define const
  2397. #endif
  2398. #endif
  2399. /* Comment out all this code if we are using the GNU C Library, and are not
  2400. actually compiling the library itself. This code is part of the GNU C
  2401. Library, but also included in many other GNU distributions. Compiling
  2402. and linking in this code is a waste when using the GNU C library
  2403. (especially if it is a shared library). Rather than having every GNU
  2404. program understand `configure --with-gnu-libc' and omit the object files,
  2405. it is simpler to just do this in the source for each such file. */
  2406. /* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  2407. * Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
  2408. * Copyright (C) 1992-1993 Jean-loup Gailly
  2409. * The unzip code was written and put in the public domain by Mark Adler.
  2410. * Portions of the lzw code are derived from the public domain 'compress'
  2411. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  2412. * Ken Turkowski, Dave Mack and Peter Jannesen.
  2413. *
  2414. * See the license_msg below and the file COPYING for the software license.
  2415. * See the file algorithm.doc for the compression algorithms and file formats.
  2416. */
  2417. static char *license_msg[] = {
  2418. "Copyright 2002 Free Software Foundation",
  2419. "Copyright 1992-1993 Jean-loup Gailly",
  2420. "This program comes with ABSOLUTELY NO WARRANTY.",
  2421. "You may redistribute copies of this program",
  2422. "under the terms of the GNU General Public License.",
  2423. "For more information about these matters, see the file named COPYING.",
  2424. 0};
  2425. /* Compress files with zip algorithm and 'compress' interface.
  2426. * See usage() and help() functions below for all options.
  2427. * Outputs:
  2428. * file.gz: compressed file with same mode, owner, and utimes
  2429. * or stdout with -c option or if stdin used as input.
  2430. * If the output file name had to be truncated, the original name is kept
  2431. * in the compressed file.
  2432. * On MSDOS, file.tmp -> file.tmz. On VMS, file.tmp -> file.tmp-gz.
  2433. *
  2434. * Using gz on MSDOS would create too many file name conflicts. For
  2435. * example, foo.txt -> foo.tgz (.tgz must be reserved as shorthand for
  2436. * tar.gz). Similarly, foo.dir and foo.doc would both be mapped to foo.dgz.
  2437. * I also considered 12345678.txt -> 12345txt.gz but this truncates the name
  2438. * too heavily. There is no ideal solution given the MSDOS 8+3 limitation.
  2439. *
  2440. * For the meaning of all compilation flags, see comments in Makefile.in.
  2441. */
  2442. #ifdef RCSID
  2443. static char rcsid[] = "$Id: gzip.c,v 0.24 1993/06/24 10:52:07 jloup Exp $";
  2444. #endif
  2445. /* revision.h -- define the version number
  2446. * Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
  2447. * Copyright (C) 1992-1993 Jean-loup Gailly.
  2448. * This is free software; you can redistribute it and/or modify it under the
  2449. * terms of the GNU General Public License, see the file COPYING.
  2450. */
  2451. #define PATCHLEVEL 0
  2452. #define REVDATE "2002-09-30"
  2453. /* This version does not support compression into old compress format: */
  2454. #ifdef LZW
  2455. # undef LZW
  2456. #endif
  2457. /* $Id: revision.h,v 0.25 1993/06/24 08:29:52 jloup Exp $ */
  2458. /* configuration */
  2459. # define NAMLEN(direct) strlen((direct)->d_name)
  2460. # define DIR_OPT "DIRENT"
  2461. #ifndef NO_DIR
  2462. # define NO_DIR 0
  2463. #endif
  2464. #ifdef CLOSEDIR_VOID
  2465. # define CLOSEDIR(d) (closedir(d), 0)
  2466. #else
  2467. # define CLOSEDIR(d) closedir(d)
  2468. #endif
  2469. #if !defined(HAVE_LSTAT) && !defined(lstat)
  2470. # define lstat(name, buf) stat(name, buf)
  2471. #endif
  2472. # define TIME_OPT "UTIME"
  2473. #if !defined(S_ISDIR) && defined(S_IFDIR)
  2474. # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  2475. #endif
  2476. #if !defined(S_ISREG) && defined(S_IFREG)
  2477. # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  2478. #endif
  2479. typedef RETSIGTYPE (*sig_type) OF((int));
  2480. #ifndef O_BINARY
  2481. # define O_BINARY 0 /* creation mode for open() */
  2482. #endif
  2483. #ifndef S_IRUSR
  2484. # define S_IRUSR 0400
  2485. #endif
  2486. #ifndef S_IWUSR
  2487. # define S_IWUSR 0200
  2488. #endif
  2489. #define RW_USER (S_IRUSR | S_IWUSR) /* creation mode for open() */
  2490. #ifndef MAX_PATH_LEN
  2491. # define MAX_PATH_LEN 1024 /* max pathname length */
  2492. #endif
  2493. #ifndef SEEK_END
  2494. # define SEEK_END 2
  2495. #endif
  2496. #ifndef CHAR_BIT
  2497. # define CHAR_BIT 8
  2498. #endif
  2499. #ifdef off_t
  2500. off_t lseek OF((int fd, off_t offset, int whence));
  2501. #endif
  2502. #ifndef OFF_T_MIN
  2503. #define OFF_T_MIN (~ (off_t) 0 << (sizeof (off_t) * CHAR_BIT - 1))
  2504. #endif
  2505. #ifndef OFF_T_MAX
  2506. #define OFF_T_MAX (~ (off_t) 0 - OFF_T_MIN)
  2507. #endif
  2508. /* Separator for file name parts (see shorten_name()) */
  2509. #ifdef NO_MULTIPLE_DOTS
  2510. # define PART_SEP "-"
  2511. #else
  2512. # define PART_SEP "."
  2513. #endif
  2514. /* global buffers */
  2515. DECLARE(uch, inbuf, INBUFSIZ +INBUF_EXTRA);
  2516. DECLARE(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA);
  2517. DECLARE(ush, d_buf, DIST_BUFSIZE);
  2518. DECLARE(uch, window, 2L*WSIZE);
  2519. #ifndef MAXSEG_64K
  2520. DECLARE(ush, tab_prefix, 1L<<BITS);
  2521. #else
  2522. DECLARE(ush, tab_prefix0, 1L<<(BITS-1));
  2523. DECLARE(ush, tab_prefix1, 1L<<(BITS-1));
  2524. #endif
  2525. /* local variables */
  2526. int ascii = 0; /* convert end-of-lines to local OS conventions */
  2527. int to_stdout = 0; /* output to stdout (-c) */
  2528. int decompress = 0; /* decompress (-d) */
  2529. int force = 0; /* don't ask questions, compress links (-f) */
  2530. int no_name = -1; /* don't save or restore the original file name */
  2531. int no_time = -1; /* don't save or restore the original file time */
  2532. int recursive = 0; /* recurse through directories (-r) */
  2533. int list = 0; /* list the file contents (-l) */
  2534. int verbose = 0; /* be verbose (-v) */
  2535. int quiet = 0; /* be very quiet (-q) */
  2536. int do_lzw = 0; /* generate output compatible with old compress (-Z) */
  2537. int test = 0; /* test .gz file integrity */
  2538. int foreground; /* set if program run in foreground */
  2539. char *progname; /* program name */
  2540. int maxbits = BITS; /* max bits per code for LZW */
  2541. int method = DEFLATED;/* compression method */
  2542. int level = 6; /* compression level */
  2543. int exit_code = OK; /* program exit code */
  2544. int save_orig_name; /* set if original name must be saved */
  2545. int last_member; /* set for .zip and .Z files */
  2546. int part_nb; /* number of parts in .gz file */
  2547. time_t time_stamp; /* original time stamp (modification time) */
  2548. off_t ifile_size; /* input file size, -1 for devices (debug only) */
  2549. char *env; /* contents of GZIP env variable */
  2550. char **args = NULL; /* argv pointer if GZIP env variable defined */
  2551. char *z_suffix; /* default suffix (can be set with --suffix) */
  2552. size_t z_len; /* strlen(z_suffix) */
  2553. off_t bytes_in; /* number of input bytes */
  2554. off_t bytes_out; /* number of output bytes */
  2555. off_t total_in; /* input bytes for all files */
  2556. off_t total_out; /* output bytes for all files */
  2557. char ifname[MAX_PATH_LEN]; /* input file name */
  2558. char ofname[MAX_PATH_LEN]; /* output file name */
  2559. int remove_ofname = 0; /* remove output file on error */
  2560. struct stat istat; /* status for input file */
  2561. int ifd; /* input file descriptor */
  2562. int ofd; /* output file descriptor */
  2563. unsigned insize; /* valid bytes in inbuf */
  2564. unsigned inptr; /* index of next byte to be processed in inbuf */
  2565. unsigned outcnt; /* bytes in output buffer */
  2566. int rsync = 0; /* make ryncable chunks */
  2567. struct option longopts[] =
  2568. {
  2569. /* { name has_arg *flag val } */
  2570. {"ascii", 0, 0, 'a'}, /* ascii text mode */
  2571. {"to-stdout", 0, 0, 'c'}, /* write output on standard output */
  2572. {"stdout", 0, 0, 'c'}, /* write output on standard output */
  2573. {"decompress", 0, 0, 'd'}, /* decompress */
  2574. {"uncompress", 0, 0, 'd'}, /* decompress */
  2575. /* {"encrypt", 0, 0, 'e'}, encrypt */
  2576. {"force", 0, 0, 'f'}, /* force overwrite of output file */
  2577. {"help", 0, 0, 'h'}, /* give help */
  2578. /* {"pkzip", 0, 0, 'k'}, force output in pkzip format */
  2579. {"list", 0, 0, 'l'}, /* list .gz file contents */
  2580. {"license", 0, 0, 'L'}, /* display software license */
  2581. {"no-name", 0, 0, 'n'}, /* don't save or restore original name & time */
  2582. {"name", 0, 0, 'N'}, /* save or restore original name & time */
  2583. {"quiet", 0, 0, 'q'}, /* quiet mode */
  2584. {"silent", 0, 0, 'q'}, /* quiet mode */
  2585. {"recursive", 0, 0, 'r'}, /* recurse through directories */
  2586. {"suffix", 1, 0, 'S'}, /* use given suffix instead of .gz */
  2587. {"test", 0, 0, 't'}, /* test compressed file integrity */
  2588. {"no-time", 0, 0, 'T'}, /* don't save or restore the time stamp */
  2589. {"verbose", 0, 0, 'v'}, /* verbose mode */
  2590. {"version", 0, 0, 'V'}, /* display version number */
  2591. {"fast", 0, 0, '1'}, /* compress faster */
  2592. {"best", 0, 0, '9'}, /* compress better */
  2593. {"lzw", 0, 0, 'Z'}, /* make output compatible with old compress */
  2594. {"bits", 1, 0, 'b'}, /* max number of bits per code (implies -Z) */
  2595. {"rsyncable", 0, 0, 'R'}, /* make rsync-friendly archive */
  2596. { 0, 0, 0, 0 }
  2597. };
  2598. /* local functions */
  2599. local void usage OF((void));
  2600. local void help OF((void));
  2601. local void license OF((void));
  2602. local void version OF((void));
  2603. local int input_eof OF((void));
  2604. local void treat_stdin OF((void));
  2605. local void treat_file OF((char *iname));
  2606. local int create_outfile OF((void));
  2607. local int do_stat OF((char *name, struct stat *sbuf));
  2608. local char *get_suffix OF((char *name));
  2609. local int get_istat OF((char *iname, struct stat *sbuf));
  2610. local int make_ofname OF((void));
  2611. local int same_file OF((struct stat *stat1, struct stat *stat2));
  2612. local int name_too_long OF((char *name, struct stat *statb));
  2613. local void shorten_name OF((char *name));
  2614. local int get_method OF((int in));
  2615. local void do_list OF((int ifd, int method));
  2616. local int check_ofname OF((void));
  2617. local void copy_stat OF((struct stat *ifstat));
  2618. local void do_exit OF((int exitcode));
  2619. int main OF((int argc, char **argv));
  2620. int (*work) OF((int infile, int outfile)) = zip; /* function to call */
  2621. #if ! NO_DIR
  2622. local void treat_dir OF((char *dir));
  2623. #endif
  2624. #ifdef HAVE_UTIME
  2625. local void reset_times OF((char *name, struct stat *statb));
  2626. #endif
  2627. #define strequ(s1, s2) (strcmp((s1),(s2)) == 0)
  2628. /* ======================================================================== */
  2629. local void usage()
  2630. {
  2631. printf ("usage: %s [-%scdfhlLnN%stvV19] [-S suffix] [file ...]\n",
  2632. progname,
  2633. O_BINARY ? "a" : "", NO_DIR ? "" : "r");
  2634. }
  2635. /* ======================================================================== */
  2636. local void help()
  2637. {
  2638. static char *help_msg[] = {
  2639. #if O_BINARY
  2640. " -a --ascii ascii text; convert end-of-lines using local conventions",
  2641. #endif
  2642. " -c --stdout write on standard output, keep original files unchanged",
  2643. " -d --decompress decompress",
  2644. /* -e --encrypt encrypt */
  2645. " -f --force force overwrite of output file and compress links",
  2646. " -h --help give this help",
  2647. /* -k --pkzip force output in pkzip format */
  2648. " -l --list list compressed file contents",
  2649. " -L --license display software license",
  2650. #ifdef UNDOCUMENTED
  2651. " -m --no-time do not save or restore the original modification time",
  2652. " -M --time save or restore the original modification time",
  2653. #endif
  2654. " -n --no-name do not save or restore the original name and time stamp",
  2655. " -N --name save or restore the original name and time stamp",
  2656. " -q --quiet suppress all warnings",
  2657. #if ! NO_DIR
  2658. " -r --recursive operate recursively on directories",
  2659. #endif
  2660. " -S .suf --suffix .suf use suffix .suf on compressed files",
  2661. " -t --test test compressed file integrity",
  2662. " -v --verbose verbose mode",
  2663. " -V --version display version number",
  2664. " -1 --fast compress faster",
  2665. " -9 --best compress better",
  2666. #ifdef LZW
  2667. " -Z --lzw produce output compatible with old compress",
  2668. " -b --bits maxbits max number of bits per code (implies -Z)",
  2669. #endif
  2670. " --rsyncable Make rsync-friendly archive",
  2671. " file... files to (de)compress. If none given, use standard input.",
  2672. "Report bugs to <bug-gzip@gnu.org>.",
  2673. 0};
  2674. char **p = help_msg;
  2675. printf ("%s %s\n(%s)\n", progname, VERSION, REVDATE);
  2676. usage();
  2677. while (*p) printf ("%s\n", *p++);
  2678. }
  2679. /* ======================================================================== */
  2680. local void license()
  2681. {
  2682. char **p = license_msg;
  2683. printf ("%s %s\n(%s)\n", progname, VERSION, REVDATE);
  2684. while (*p) printf ("%s\n", *p++);
  2685. }
  2686. /* ======================================================================== */
  2687. local void version()
  2688. {
  2689. license ();
  2690. printf ("Compilation options:\n%s %s ", DIR_OPT, TIME_OPT);
  2691. #ifdef STDC_HEADERS
  2692. printf ("STDC_HEADERS ");
  2693. #endif
  2694. #ifdef HAVE_UNISTD_H
  2695. printf ("HAVE_UNISTD_H ");
  2696. #endif
  2697. #ifdef HAVE_MEMORY_H
  2698. printf ("HAVE_MEMORY_H ");
  2699. #endif
  2700. #ifdef HAVE_STRING_H
  2701. printf ("HAVE_STRING_H ");
  2702. #endif
  2703. #ifdef HAVE_LSTAT
  2704. printf ("HAVE_LSTAT ");
  2705. #endif
  2706. #ifdef NO_MULTIPLE_DOTS
  2707. printf ("NO_MULTIPLE_DOTS ");
  2708. #endif
  2709. #ifdef HAVE_CHOWN
  2710. printf ("HAVE_CHOWN ");
  2711. #endif
  2712. #ifdef PROTO
  2713. printf ("PROTO ");
  2714. #endif
  2715. #ifdef ASMV
  2716. printf ("ASMV ");
  2717. #endif
  2718. #ifdef DEBUG
  2719. printf ("DEBUG ");
  2720. #endif
  2721. #ifdef DYN_ALLOC
  2722. printf ("DYN_ALLOC ");
  2723. #endif
  2724. #ifdef MAXSEG_64K
  2725. printf ("MAXSEG_64K");
  2726. #endif
  2727. printf ("\n");
  2728. printf ("Written by Jean-loup Gailly.\n");
  2729. }
  2730. local void progerror (string)
  2731. char *string;
  2732. {
  2733. int e = errno;
  2734. fprintf(stderr, "%s: ", progname);
  2735. errno = e;
  2736. perror(string);
  2737. exit_code = ERROR;
  2738. }
  2739. /* ======================================================================== */
  2740. int main (argc, argv)
  2741. int argc;
  2742. char **argv;
  2743. {
  2744. int file_count; /* number of files to precess */
  2745. int proglen; /* length of progname */
  2746. int optc; /* current option */
  2747. EXPAND(argc, argv); /* wild card expansion if necessary */
  2748. progname = base_name (argv[0]);
  2749. proglen = strlen(progname);
  2750. /* Suppress .exe for MSDOS, OS/2 and VMS: */
  2751. if (proglen > 4 && strequ(progname+proglen-4, ".exe")) {
  2752. progname[proglen-4] = '\0';
  2753. }
  2754. /* Add options in GZIP environment variable if there is one */
  2755. env = add_envopt(&argc, &argv, OPTIONS_VAR);
  2756. if (env != NULL) args = argv;
  2757. foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
  2758. if (foreground) {
  2759. (void) signal (SIGINT, (sig_type)abort_gzip_signal);
  2760. }
  2761. #ifdef SIGTERM
  2762. if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
  2763. (void) signal(SIGTERM, (sig_type)abort_gzip_signal);
  2764. }
  2765. #endif
  2766. #ifdef SIGHUP
  2767. if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
  2768. (void) signal(SIGHUP, (sig_type)abort_gzip_signal);
  2769. }
  2770. #endif
  2771. #ifndef GNU_STANDARD
  2772. /* For compatibility with old compress, use program name as an option.
  2773. * If you compile with -DGNU_STANDARD, this program will behave as
  2774. * gzip even if it is invoked under the name gunzip or zcat.
  2775. *
  2776. * Systems which do not support links can still use -d or -dc.
  2777. * Ignore an .exe extension for MSDOS, OS/2 and VMS.
  2778. */
  2779. if ( strncmp(progname, "un", 2) == 0 /* ungzip, uncompress */
  2780. || strncmp(progname, "gun", 3) == 0) { /* gunzip */
  2781. decompress = 1;
  2782. } else if (strequ(progname+1, "cat") /* zcat, pcat, gcat */
  2783. || strequ(progname, "gzcat")) { /* gzcat */
  2784. decompress = to_stdout = 1;
  2785. }
  2786. #endif
  2787. z_suffix = Z_SUFFIX;
  2788. z_len = strlen(z_suffix);
  2789. while ((optc = getopt_long (argc, argv, "ab:cdfhH?lLmMnNqrS:tvVZ123456789",
  2790. longopts, (int *)0)) != -1) {
  2791. switch (optc) {
  2792. case 'a':
  2793. ascii = 1; break;
  2794. case 'b':
  2795. maxbits = atoi(optarg);
  2796. for (; *optarg; optarg++)
  2797. if (! ('0' <= *optarg && *optarg <= '9'))
  2798. {
  2799. fprintf (stderr, "%s: -b operand is not an integer\n",
  2800. progname);
  2801. usage ();
  2802. do_exit (ERROR);
  2803. }
  2804. break;
  2805. case 'c':
  2806. to_stdout = 1; break;
  2807. case 'd':
  2808. decompress = 1; break;
  2809. case 'f':
  2810. force++; break;
  2811. case 'h': case 'H': case '?':
  2812. help(); do_exit(OK); break;
  2813. case 'l':
  2814. list = decompress = to_stdout = 1; break;
  2815. case 'L':
  2816. license(); do_exit(OK); break;
  2817. case 'm': /* undocumented, may change later */
  2818. no_time = 1; break;
  2819. case 'M': /* undocumented, may change later */
  2820. no_time = 0; break;
  2821. case 'n':
  2822. no_name = no_time = 1; break;
  2823. case 'N':
  2824. no_name = no_time = 0; break;
  2825. case 'q':
  2826. quiet = 1; verbose = 0; break;
  2827. case 'r':
  2828. #if NO_DIR
  2829. fprintf(stderr, "%s: -r not supported on this system\n", progname);
  2830. usage();
  2831. do_exit(ERROR); break;
  2832. #else
  2833. recursive = 1; break;
  2834. #endif
  2835. case 'R':
  2836. rsync = 1; break;
  2837. case 'S':
  2838. #ifdef NO_MULTIPLE_DOTS
  2839. if (*optarg == '.') optarg++;
  2840. #endif
  2841. z_len = strlen(optarg);
  2842. z_suffix = optarg;
  2843. break;
  2844. case 't':
  2845. test = decompress = to_stdout = 1;
  2846. break;
  2847. case 'v':
  2848. verbose++; quiet = 0; break;
  2849. case 'V':
  2850. version(); do_exit(OK); break;
  2851. case 'Z':
  2852. #ifdef LZW
  2853. do_lzw = 1; break;
  2854. #else
  2855. fprintf(stderr, "%s: -Z not supported in this version\n",
  2856. progname);
  2857. usage();
  2858. do_exit(ERROR); break;
  2859. #endif
  2860. case '1': case '2': case '3': case '4':
  2861. case '5': case '6': case '7': case '8': case '9':
  2862. level = optc - '0';
  2863. break;
  2864. default:
  2865. /* Error message already emitted by getopt_long. */
  2866. usage();
  2867. do_exit(ERROR);
  2868. }
  2869. } /* loop on all arguments */
  2870. #ifdef SIGPIPE
  2871. /* Ignore "Broken Pipe" message with --quiet */
  2872. if (quiet && signal (SIGPIPE, SIG_IGN) != SIG_IGN)
  2873. signal (SIGPIPE, (sig_type) abort_gzip_signal);
  2874. #endif
  2875. /* By default, save name and timestamp on compression but do not
  2876. * restore them on decompression.
  2877. */
  2878. if (no_time < 0) no_time = decompress;
  2879. if (no_name < 0) no_name = decompress;
  2880. file_count = argc - optind;
  2881. #if O_BINARY
  2882. #else
  2883. if (ascii && !quiet) {
  2884. fprintf(stderr, "%s: option --ascii ignored on this system\n",
  2885. progname);
  2886. }
  2887. #endif
  2888. if ((z_len == 0 && !decompress) || z_len > MAX_SUFFIX) {
  2889. fprintf(stderr, "%s: incorrect suffix '%s'\n",
  2890. progname, z_suffix);
  2891. do_exit(ERROR);
  2892. }
  2893. if (do_lzw && !decompress) work = lzw;
  2894. /* Allocate all global buffers (for DYN_ALLOC option) */
  2895. ALLOC(uch, inbuf, INBUFSIZ +INBUF_EXTRA);
  2896. ALLOC(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA);
  2897. ALLOC(ush, d_buf, DIST_BUFSIZE);
  2898. ALLOC(uch, window, 2L*WSIZE);
  2899. #ifndef MAXSEG_64K
  2900. ALLOC(ush, tab_prefix, 1L<<BITS);
  2901. #else
  2902. ALLOC(ush, tab_prefix0, 1L<<(BITS-1));
  2903. ALLOC(ush, tab_prefix1, 1L<<(BITS-1));
  2904. #endif
  2905. /* And get to work */
  2906. if (file_count != 0) {
  2907. if (to_stdout && !test && !list && (!decompress || !ascii)) {
  2908. SET_BINARY_MODE(fileno(stdout));
  2909. }
  2910. while (optind < argc) {
  2911. treat_file(argv[optind++]);
  2912. }
  2913. } else { /* Standard input */
  2914. treat_stdin();
  2915. }
  2916. if (list && !quiet && file_count > 1) {
  2917. do_list(-1, -1); /* print totals */
  2918. }
  2919. do_exit(exit_code);
  2920. return exit_code; /* just to avoid lint warning */
  2921. }
  2922. /* Return nonzero when at end of file on input. */
  2923. local int
  2924. input_eof ()
  2925. {
  2926. if (!decompress || last_member)
  2927. return 1;
  2928. if (inptr == insize)
  2929. {
  2930. if (insize != INBUFSIZ || fill_inbuf (1) == EOF)
  2931. return 1;
  2932. /* Unget the char that fill_inbuf got. */
  2933. inptr = 0;
  2934. }
  2935. return 0;
  2936. }
  2937. /* ========================================================================
  2938. * Compress or decompress stdin
  2939. */
  2940. local void treat_stdin()
  2941. {
  2942. if (!force && !list &&
  2943. isatty(fileno((FILE *)(decompress ? stdin : stdout)))) {
  2944. /* Do not send compressed data to the terminal or read it from
  2945. * the terminal. We get here when user invoked the program
  2946. * without parameters, so be helpful. According to the GNU standards:
  2947. *
  2948. * If there is one behavior you think is most useful when the output
  2949. * is to a terminal, and another that you think is most useful when
  2950. * the output is a file or a pipe, then it is usually best to make
  2951. * the default behavior the one that is useful with output to a
  2952. * terminal, and have an option for the other behavior.
  2953. *
  2954. * Here we use the --force option to get the other behavior.
  2955. */
  2956. fprintf(stderr,
  2957. "%s: compressed data not %s a terminal. Use -f to force %scompression.\n",
  2958. progname, decompress ? "read from" : "written to",
  2959. decompress ? "de" : "");
  2960. fprintf(stderr,"For help, type: %s -h\n", progname);
  2961. do_exit(ERROR);
  2962. }
  2963. if (decompress || !ascii) {
  2964. SET_BINARY_MODE(fileno(stdin));
  2965. }
  2966. if (!test && !list && (!decompress || !ascii)) {
  2967. SET_BINARY_MODE(fileno(stdout));
  2968. }
  2969. strcpy(ifname, "stdin");
  2970. strcpy(ofname, "stdout");
  2971. /* Get the time stamp on the input file. */
  2972. time_stamp = 0; /* time unknown by default */
  2973. #ifndef NO_STDIN_FSTAT
  2974. if (list || !no_time) {
  2975. if (fstat(fileno(stdin), &istat) != 0) {
  2976. progerror("standard input");
  2977. do_exit(ERROR);
  2978. }
  2979. # ifdef NO_PIPE_TIMESTAMP
  2980. if (S_ISREG(istat.st_mode))
  2981. # endif
  2982. time_stamp = istat.st_mtime;
  2983. #endif /* NO_STDIN_FSTAT */
  2984. }
  2985. ifile_size = -1L; /* convention for unknown size */
  2986. clear_bufs(); /* clear input and output buffers */
  2987. to_stdout = 1;
  2988. part_nb = 0;
  2989. if (decompress) {
  2990. method = get_method(ifd);
  2991. if (method < 0) {
  2992. do_exit(exit_code); /* error message already emitted */
  2993. }
  2994. }
  2995. if (list) {
  2996. do_list(ifd, method);
  2997. return;
  2998. }
  2999. /* Actually do the compression/decompression. Loop over zipped members.
  3000. */
  3001. for (;;) {
  3002. if ((*work)(fileno(stdin), fileno(stdout)) != OK) return;
  3003. if (input_eof ())
  3004. break;
  3005. method = get_method(ifd);
  3006. if (method < 0) return; /* error message already emitted */
  3007. bytes_out = 0; /* required for length check */
  3008. }
  3009. if (verbose) {
  3010. if (test) {
  3011. fprintf(stderr, " OK\n");
  3012. } else if (!decompress) {
  3013. display_ratio(bytes_in-(bytes_out-header_bytes), bytes_in, stderr);
  3014. fprintf(stderr, "\n");
  3015. #ifdef DISPLAY_STDIN_RATIO
  3016. } else {
  3017. display_ratio(bytes_out-(bytes_in-header_bytes), bytes_out,stderr);
  3018. fprintf(stderr, "\n");
  3019. #endif
  3020. }
  3021. }
  3022. }
  3023. /* ========================================================================
  3024. * Compress or decompress the given file
  3025. */
  3026. local void treat_file(iname)
  3027. char *iname;
  3028. {
  3029. /* Accept "-" as synonym for stdin */
  3030. if (strequ(iname, "-")) {
  3031. int cflag = to_stdout;
  3032. treat_stdin();
  3033. to_stdout = cflag;
  3034. return;
  3035. }
  3036. /* Check if the input file is present, set ifname and istat: */
  3037. if (get_istat(iname, &istat) != OK) return;
  3038. /* If the input name is that of a directory, recurse or ignore: */
  3039. if (S_ISDIR(istat.st_mode)) {
  3040. #if ! NO_DIR
  3041. if (recursive) {
  3042. struct stat st;
  3043. st = istat;
  3044. treat_dir(iname);
  3045. /* Warning: ifname is now garbage */
  3046. # ifndef NO_UTIME
  3047. reset_times (iname, &st);
  3048. # endif
  3049. } else
  3050. #endif
  3051. WARN((stderr,"%s: %s is a directory -- ignored\n", progname, ifname));
  3052. return;
  3053. }
  3054. if (!S_ISREG(istat.st_mode)) {
  3055. WARN((stderr,
  3056. "%s: %s is not a directory or a regular file - ignored\n",
  3057. progname, ifname));
  3058. return;
  3059. }
  3060. if (istat.st_nlink > 1 && !to_stdout && !force) {
  3061. WARN((stderr, "%s: %s has %lu other link%c -- unchanged\n",
  3062. progname, ifname, (unsigned long) istat.st_nlink - 1,
  3063. istat.st_nlink > 2 ? 's' : ' '));
  3064. return;
  3065. }
  3066. ifile_size = istat.st_size;
  3067. time_stamp = no_time && !list ? 0 : istat.st_mtime;
  3068. /* Generate output file name. For -r and (-t or -l), skip files
  3069. * without a valid gzip suffix (check done in make_ofname).
  3070. */
  3071. if (to_stdout && !list && !test) {
  3072. strcpy(ofname, "stdout");
  3073. } else if (make_ofname() != OK) {
  3074. return;
  3075. }
  3076. /* Open the input file and determine compression method. The mode
  3077. * parameter is ignored but required by some systems (VMS) and forbidden
  3078. * on other systems (MacOS).
  3079. */
  3080. ifd = OPEN(ifname, ascii && !decompress ? O_RDONLY : O_RDONLY | O_BINARY,
  3081. RW_USER);
  3082. if (ifd == -1) {
  3083. progerror(ifname);
  3084. return;
  3085. }
  3086. clear_bufs(); /* clear input and output buffers */
  3087. part_nb = 0;
  3088. if (decompress) {
  3089. method = get_method(ifd); /* updates ofname if original given */
  3090. if (method < 0) {
  3091. close(ifd);
  3092. return; /* error message already emitted */
  3093. }
  3094. }
  3095. if (list) {
  3096. do_list(ifd, method);
  3097. close(ifd);
  3098. return;
  3099. }
  3100. /* If compressing to a file, check if ofname is not ambiguous
  3101. * because the operating system truncates names. Otherwise, generate
  3102. * a new ofname and save the original name in the compressed file.
  3103. */
  3104. if (to_stdout) {
  3105. ofd = fileno(stdout);
  3106. /* keep remove_ofname as zero */
  3107. } else {
  3108. if (create_outfile() != OK) return;
  3109. if (!decompress && save_orig_name && !verbose && !quiet) {
  3110. fprintf(stderr, "%s: %s compressed to %s\n",
  3111. progname, ifname, ofname);
  3112. }
  3113. }
  3114. /* Keep the name even if not truncated except with --no-name: */
  3115. if (!save_orig_name) save_orig_name = !no_name;
  3116. if (verbose) {
  3117. fprintf(stderr, "%s:\t", ifname);
  3118. }
  3119. /* Actually do the compression/decompression. Loop over zipped members.
  3120. */
  3121. for (;;) {
  3122. if ((*work)(ifd, ofd) != OK) {
  3123. method = -1; /* force cleanup */
  3124. break;
  3125. }
  3126. if (input_eof ())
  3127. break;
  3128. method = get_method(ifd);
  3129. if (method < 0) break; /* error message already emitted */
  3130. bytes_out = 0; /* required for length check */
  3131. }
  3132. close(ifd);
  3133. if (!to_stdout) {
  3134. /* Copy modes, times, ownership, and remove the input file */
  3135. copy_stat(&istat);
  3136. if (close(ofd))
  3137. write_error();
  3138. }
  3139. if (method == -1) {
  3140. if (!to_stdout) xunlink (ofname);
  3141. return;
  3142. }
  3143. /* Display statistics */
  3144. if(verbose) {
  3145. if (test) {
  3146. fprintf(stderr, " OK");
  3147. } else if (decompress) {
  3148. display_ratio(bytes_out-(bytes_in-header_bytes), bytes_out,stderr);
  3149. } else {
  3150. display_ratio(bytes_in-(bytes_out-header_bytes), bytes_in, stderr);
  3151. }
  3152. if (!test && !to_stdout) {
  3153. fprintf(stderr, " -- replaced with %s", ofname);
  3154. }
  3155. fprintf(stderr, "\n");
  3156. }
  3157. }
  3158. /* ========================================================================
  3159. * Create the output file. Return OK or ERROR.
  3160. * Try several times if necessary to avoid truncating the z_suffix. For
  3161. * example, do not create a compressed file of name "1234567890123."
  3162. * Sets save_orig_name to true if the file name has been truncated.
  3163. * IN assertions: the input file has already been open (ifd is set) and
  3164. * ofname has already been updated if there was an original name.
  3165. * OUT assertions: ifd and ofd are closed in case of error.
  3166. */
  3167. local int create_outfile()
  3168. {
  3169. struct stat ostat; /* stat for ofname */
  3170. int flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY;
  3171. if (ascii && decompress) {
  3172. flags &= ~O_BINARY; /* force ascii text mode */
  3173. }
  3174. for (;;) {
  3175. /* Make sure that ofname is not an existing file */
  3176. if (check_ofname() != OK) {
  3177. close(ifd);
  3178. return ERROR;
  3179. }
  3180. /* Create the output file */
  3181. remove_ofname = 1;
  3182. ofd = OPEN(ofname, flags, RW_USER);
  3183. if (ofd == -1) {
  3184. progerror(ofname);
  3185. close(ifd);
  3186. return ERROR;
  3187. }
  3188. /* Check for name truncation on new file (1234567890123.gz) */
  3189. #ifdef NO_FSTAT
  3190. if (stat(ofname, &ostat) != 0) {
  3191. #else
  3192. if (fstat(ofd, &ostat) != 0) {
  3193. #endif
  3194. progerror(ofname);
  3195. close(ifd); close(ofd);
  3196. xunlink (ofname);
  3197. return ERROR;
  3198. }
  3199. if (!name_too_long(ofname, &ostat)) return OK;
  3200. if (decompress) {
  3201. /* name might be too long if an original name was saved */
  3202. WARN((stderr, "%s: %s: warning, name truncated\n",
  3203. progname, ofname));
  3204. return OK;
  3205. }
  3206. close(ofd);
  3207. xunlink (ofname);
  3208. #ifdef NO_MULTIPLE_DOTS
  3209. /* Should never happen, see check_ofname() */
  3210. fprintf(stderr, "%s: %s: name too long\n", progname, ofname);
  3211. do_exit(ERROR);
  3212. #endif
  3213. shorten_name(ofname);
  3214. }
  3215. }
  3216. /* ========================================================================
  3217. * Use lstat if available, except for -c or -f. Use stat otherwise.
  3218. * This allows links when not removing the original file.
  3219. */
  3220. local int do_stat(name, sbuf)
  3221. char *name;
  3222. struct stat *sbuf;
  3223. {
  3224. errno = 0;
  3225. if (!to_stdout && !force) {
  3226. return lstat(name, sbuf);
  3227. }
  3228. return stat(name, sbuf);
  3229. }
  3230. /* ========================================================================
  3231. * Return a pointer to the 'z' suffix of a file name, or NULL. For all
  3232. * systems, ".gz", ".z", ".Z", ".taz", ".tgz", "-gz", "-z" and "_z" are
  3233. * accepted suffixes, in addition to the value of the --suffix option.
  3234. * ".tgz" is a useful convention for tar.z files on systems limited
  3235. * to 3 characters extensions. On such systems, ".?z" and ".??z" are
  3236. * also accepted suffixes. For Unix, we do not want to accept any
  3237. * .??z suffix as indicating a compressed file; some people use .xyz
  3238. * to denote volume data.
  3239. * On systems allowing multiple versions of the same file (such as VMS),
  3240. * this function removes any version suffix in the given name.
  3241. */
  3242. local char *get_suffix(name)
  3243. char *name;
  3244. {
  3245. int nlen, slen;
  3246. char suffix[MAX_SUFFIX+3]; /* last chars of name, forced to lower case */
  3247. static char *known_suffixes[] =
  3248. {NULL, ".gz", ".z", ".taz", ".tgz", "-gz", "-z", "_z",
  3249. #ifdef MAX_EXT_CHARS
  3250. "z",
  3251. #endif
  3252. NULL};
  3253. char **suf = known_suffixes;
  3254. *suf = z_suffix;
  3255. if (strequ(z_suffix, "z")) suf++; /* check long suffixes first */
  3256. #ifdef SUFFIX_SEP
  3257. /* strip a version number from the file name */
  3258. {
  3259. char *v = strrchr(name, SUFFIX_SEP);
  3260. if (v != NULL) *v = '\0';
  3261. }
  3262. #endif
  3263. nlen = strlen(name);
  3264. if (nlen <= MAX_SUFFIX+2) {
  3265. strcpy(suffix, name);
  3266. } else {
  3267. strcpy(suffix, name+nlen-MAX_SUFFIX-2);
  3268. }
  3269. strlwr(suffix);
  3270. slen = strlen(suffix);
  3271. do {
  3272. int s = strlen(*suf);
  3273. if (slen > s && suffix[slen-s-1] != PATH_SEP
  3274. && strequ(suffix + slen - s, *suf)) {
  3275. return name+nlen-s;
  3276. }
  3277. } while (*++suf != NULL);
  3278. return NULL;
  3279. }
  3280. /* ========================================================================
  3281. * Set ifname to the input file name (with a suffix appended if necessary)
  3282. * and istat to its stats. For decompression, if no file exists with the
  3283. * original name, try adding successively z_suffix, .gz, .z, -z and .Z.
  3284. * For MSDOS, we try only z_suffix and z.
  3285. * Return OK or ERROR.
  3286. */
  3287. local int get_istat(iname, sbuf)
  3288. char *iname;
  3289. struct stat *sbuf;
  3290. {
  3291. int ilen; /* strlen(ifname) */
  3292. int z_suffix_errno = 0;
  3293. static char *suffixes[] = {NULL, ".gz", ".z", "-z", ".Z", NULL};
  3294. char **suf = suffixes;
  3295. char *s;
  3296. #ifdef NO_MULTIPLE_DOTS
  3297. char *dot; /* pointer to ifname extension, or NULL */
  3298. #endif
  3299. *suf = z_suffix;
  3300. if (sizeof ifname - 1 <= strlen (iname))
  3301. goto name_too_long;
  3302. strcpy(ifname, iname);
  3303. /* If input file exists, return OK. */
  3304. if (do_stat(ifname, sbuf) == 0) return OK;
  3305. if (!decompress || errno != ENOENT) {
  3306. progerror(ifname);
  3307. return ERROR;
  3308. }
  3309. /* file.ext doesn't exist, try adding a suffix (after removing any
  3310. * version number for VMS).
  3311. */
  3312. s = get_suffix(ifname);
  3313. if (s != NULL) {
  3314. progerror(ifname); /* ifname already has z suffix and does not exist */
  3315. return ERROR;
  3316. }
  3317. #ifdef NO_MULTIPLE_DOTS
  3318. dot = strrchr(ifname, '.');
  3319. if (dot == NULL) {
  3320. strcat(ifname, ".");
  3321. dot = strrchr(ifname, '.');
  3322. }
  3323. #endif
  3324. ilen = strlen(ifname);
  3325. if (strequ(z_suffix, ".gz")) suf++;
  3326. /* Search for all suffixes */
  3327. do {
  3328. char *s0 = s = *suf;
  3329. strcpy (ifname, iname);
  3330. #ifdef NO_MULTIPLE_DOTS
  3331. if (*s == '.') s++;
  3332. if (*dot == '\0') strcpy (dot, ".");
  3333. #endif
  3334. #ifdef MAX_EXT_CHARS
  3335. if (MAX_EXT_CHARS < strlen (s) + strlen (dot + 1))
  3336. dot[MAX_EXT_CHARS + 1 - strlen (s)] = '\0';
  3337. #endif
  3338. if (sizeof ifname <= ilen + strlen (s))
  3339. goto name_too_long;
  3340. strcat(ifname, s);
  3341. if (do_stat(ifname, sbuf) == 0) return OK;
  3342. if (strequ (s0, z_suffix))
  3343. z_suffix_errno = errno;
  3344. } while (*++suf != NULL);
  3345. /* No suffix found, complain using z_suffix: */
  3346. strcpy(ifname, iname);
  3347. #ifdef NO_MULTIPLE_DOTS
  3348. if (*dot == '\0') strcpy(dot, ".");
  3349. #endif
  3350. #ifdef MAX_EXT_CHARS
  3351. if (MAX_EXT_CHARS < z_len + strlen (dot + 1))
  3352. dot[MAX_EXT_CHARS + 1 - z_len] = '\0';
  3353. #endif
  3354. strcat(ifname, z_suffix);
  3355. errno = z_suffix_errno;
  3356. progerror(ifname);
  3357. return ERROR;
  3358. name_too_long:
  3359. fprintf (stderr, "%s: %s: file name too long\n", progname, iname);
  3360. exit_code = ERROR;
  3361. return ERROR;
  3362. }
  3363. /* ========================================================================
  3364. * Generate ofname given ifname. Return OK, or WARNING if file must be skipped.
  3365. * Sets save_orig_name to true if the file name has been truncated.
  3366. */
  3367. local int make_ofname()
  3368. {
  3369. char *suff; /* ofname z suffix */
  3370. strcpy(ofname, ifname);
  3371. /* strip a version number if any and get the gzip suffix if present: */
  3372. suff = get_suffix(ofname);
  3373. if (decompress) {
  3374. if (suff == NULL) {
  3375. /* With -t or -l, try all files (even without .gz suffix)
  3376. * except with -r (behave as with just -dr).
  3377. */
  3378. if (!recursive && (list || test)) return OK;
  3379. /* Avoid annoying messages with -r */
  3380. if (verbose || (!recursive && !quiet)) {
  3381. WARN((stderr,"%s: %s: unknown suffix -- ignored\n",
  3382. progname, ifname));
  3383. }
  3384. return WARNING;
  3385. }
  3386. /* Make a special case for .tgz and .taz: */
  3387. strlwr(suff);
  3388. if (strequ(suff, ".tgz") || strequ(suff, ".taz")) {
  3389. strcpy(suff, ".tar");
  3390. } else {
  3391. *suff = '\0'; /* strip the z suffix */
  3392. }
  3393. /* ofname might be changed later if infile contains an original name */
  3394. } else if (suff != NULL) {
  3395. /* Avoid annoying messages with -r (see treat_dir()) */
  3396. if (verbose || (!recursive && !quiet)) {
  3397. /* don't use WARN -- it will cause an exit_code of 2 */
  3398. fprintf(stderr, "%s: %s already has %s suffix -- unchanged\n",
  3399. progname, ifname, suff);
  3400. }
  3401. return WARNING;
  3402. } else {
  3403. save_orig_name = 0;
  3404. #ifdef NO_MULTIPLE_DOTS
  3405. suff = strrchr(ofname, '.');
  3406. if (suff == NULL) {
  3407. if (sizeof ofname <= strlen (ofname) + 1)
  3408. goto name_too_long;
  3409. strcat(ofname, ".");
  3410. # ifdef MAX_EXT_CHARS
  3411. if (strequ(z_suffix, "z")) {
  3412. if (sizeof ofname <= strlen (ofname) + 2)
  3413. goto name_too_long;
  3414. strcat(ofname, "gz"); /* enough room */
  3415. return OK;
  3416. }
  3417. /* On the Atari and some versions of MSDOS, name_too_long()
  3418. * does not work correctly because of a bug in stat(). So we
  3419. * must truncate here.
  3420. */
  3421. } else if (strlen(suff)-1 + z_len > MAX_SUFFIX) {
  3422. suff[MAX_SUFFIX+1-z_len] = '\0';
  3423. save_orig_name = 1;
  3424. # endif
  3425. }
  3426. #endif /* NO_MULTIPLE_DOTS */
  3427. if (sizeof ofname <= strlen (ofname) + z_len)
  3428. goto name_too_long;
  3429. strcat(ofname, z_suffix);
  3430. } /* decompress ? */
  3431. return OK;
  3432. name_too_long:
  3433. WARN ((stderr, "%s: %s: file name too long\n", progname, ifname));
  3434. return WARNING;
  3435. }
  3436. /* ========================================================================
  3437. * Check the magic number of the input file and update ofname if an
  3438. * original name was given and to_stdout is not set.
  3439. * Return the compression method, -1 for error, -2 for warning.
  3440. * Set inptr to the offset of the next byte to be processed.
  3441. * Updates time_stamp if there is one and --no-time is not used.
  3442. * This function may be called repeatedly for an input file consisting
  3443. * of several contiguous gzip'ed members.
  3444. * IN assertions: there is at least one remaining compressed member.
  3445. * If the member is a zip file, it must be the only one.
  3446. */
  3447. local int get_method(in)
  3448. int in; /* input file descriptor */
  3449. {
  3450. uch flags; /* compression flags */
  3451. char magic[2]; /* magic header */
  3452. int imagic1; /* like magic[1], but can represent EOF */
  3453. ulg stamp; /* time stamp */
  3454. /* If --force and --stdout, zcat == cat, so do not complain about
  3455. * premature end of file: use try_byte instead of get_byte.
  3456. */
  3457. if (force && to_stdout) {
  3458. magic[0] = (char)try_byte();
  3459. imagic1 = try_byte ();
  3460. magic[1] = (char) imagic1;
  3461. /* If try_byte returned EOF, magic[1] == (char) EOF. */
  3462. } else {
  3463. magic[0] = (char)get_byte();
  3464. magic[1] = (char)get_byte();
  3465. imagic1 = 0; /* avoid lint warning */
  3466. }
  3467. method = -1; /* unknown yet */
  3468. part_nb++; /* number of parts in gzip file */
  3469. header_bytes = 0;
  3470. last_member = RECORD_IO;
  3471. /* assume multiple members in gzip file except for record oriented I/O */
  3472. if (memcmp(magic, GZIP_MAGIC, 2) == 0
  3473. || memcmp(magic, OLD_GZIP_MAGIC, 2) == 0) {
  3474. method = (int)get_byte();
  3475. if (method != DEFLATED) {
  3476. fprintf(stderr,
  3477. "%s: %s: unknown method %d -- not supported\n",
  3478. progname, ifname, method);
  3479. exit_code = ERROR;
  3480. return -1;
  3481. }
  3482. work = unzip;
  3483. flags = (uch)get_byte();
  3484. if ((flags & ENCRYPTED) != 0) {
  3485. fprintf(stderr,
  3486. "%s: %s is encrypted -- not supported\n",
  3487. progname, ifname);
  3488. exit_code = ERROR;
  3489. return -1;
  3490. }
  3491. if ((flags & CONTINUATION) != 0) {
  3492. fprintf(stderr,
  3493. "%s: %s is a a multi-part gzip file -- not supported\n",
  3494. progname, ifname);
  3495. exit_code = ERROR;
  3496. if (force <= 1) return -1;
  3497. }
  3498. if ((flags & RESERVED) != 0) {
  3499. fprintf(stderr,
  3500. "%s: %s has flags 0x%x -- not supported\n",
  3501. progname, ifname, flags);
  3502. exit_code = ERROR;
  3503. if (force <= 1) return -1;
  3504. }
  3505. stamp = (ulg)get_byte();
  3506. stamp |= ((ulg)get_byte()) << 8;
  3507. stamp |= ((ulg)get_byte()) << 16;
  3508. stamp |= ((ulg)get_byte()) << 24;
  3509. if (stamp != 0 && !no_time) time_stamp = stamp;
  3510. (void)get_byte(); /* Ignore extra flags for the moment */
  3511. (void)get_byte(); /* Ignore OS type for the moment */
  3512. if ((flags & CONTINUATION) != 0) {
  3513. unsigned part = (unsigned)get_byte();
  3514. part |= ((unsigned)get_byte())<<8;
  3515. if (verbose) {
  3516. fprintf(stderr,"%s: %s: part number %u\n",
  3517. progname, ifname, part);
  3518. }
  3519. }
  3520. if ((flags & EXTRA_FIELD) != 0) {
  3521. unsigned len = (unsigned)get_byte();
  3522. len |= ((unsigned)get_byte())<<8;
  3523. if (verbose) {
  3524. fprintf(stderr,"%s: %s: extra field of %u bytes ignored\n",
  3525. progname, ifname, len);
  3526. }
  3527. while (len--) (void)get_byte();
  3528. }
  3529. /* Get original file name if it was truncated */
  3530. if ((flags & ORIG_NAME) != 0) {
  3531. if (no_name || (to_stdout && !list) || part_nb > 1) {
  3532. /* Discard the old name */
  3533. char c; /* dummy used for NeXTstep 3.0 cc optimizer bug */
  3534. do {c=get_byte();} while (c != 0);
  3535. } else {
  3536. /* Copy the base name. Keep a directory prefix intact. */
  3537. char *p = base_name (ofname);
  3538. char *base = p;
  3539. char *base2;
  3540. for (;;) {
  3541. *p = (char)get_char();
  3542. if (*p++ == '\0') break;
  3543. if (p >= ofname+sizeof(ofname)) {
  3544. error("corrupted input -- file name too large");
  3545. }
  3546. }
  3547. base2 = base_name (base);
  3548. strcpy(base, base2);
  3549. /* If necessary, adapt the name to local OS conventions: */
  3550. if (!list) {
  3551. MAKE_LEGAL_NAME(base);
  3552. if (base) list=0; /* avoid warning about unused variable */
  3553. }
  3554. } /* no_name || to_stdout */
  3555. } /* ORIG_NAME */
  3556. /* Discard file comment if any */
  3557. if ((flags & COMMENT) != 0) {
  3558. while (get_char() != 0) /* null */ ;
  3559. }
  3560. if (part_nb == 1) {
  3561. header_bytes = inptr + 2*sizeof(long); /* include crc and size */
  3562. }
  3563. } else if (memcmp(magic, PKZIP_MAGIC, 2) == 0 && inptr == 2
  3564. && memcmp((char*)inbuf, PKZIP_MAGIC, 4) == 0) {
  3565. /* To simplify the code, we support a zip file when alone only.
  3566. * We are thus guaranteed that the entire local header fits in inbuf.
  3567. */
  3568. inptr = 0;
  3569. work = unzip;
  3570. if (check_zipfile(in) != OK) return -1;
  3571. /* check_zipfile may get ofname from the local header */
  3572. last_member = 1;
  3573. } else if (memcmp(magic, PACK_MAGIC, 2) == 0) {
  3574. work = unpack;
  3575. method = PACKED;
  3576. } else if (memcmp(magic, LZW_MAGIC, 2) == 0) {
  3577. work = unlzw;
  3578. method = COMPRESSED;
  3579. last_member = 1;
  3580. } else if (memcmp(magic, LZH_MAGIC, 2) == 0) {
  3581. work = unlzh;
  3582. method = LZHED;
  3583. last_member = 1;
  3584. } else if (force && to_stdout && !list) { /* pass input unchanged */
  3585. method = STORED;
  3586. work = copy;
  3587. inptr = 0;
  3588. last_member = 1;
  3589. }
  3590. if (method >= 0) return method;
  3591. if (part_nb == 1) {
  3592. fprintf(stderr, "\n%s: %s: not in gzip format\n", progname, ifname);
  3593. exit_code = ERROR;
  3594. return -1;
  3595. } else {
  3596. if (magic[0] == 0)
  3597. {
  3598. int inbyte;
  3599. for (inbyte = imagic1; inbyte == 0; inbyte = try_byte ())
  3600. continue;
  3601. if (inbyte == EOF)
  3602. {
  3603. if (verbose)
  3604. WARN ((stderr, "\n%s: %s: decompression OK, trailing zero bytes ignored\n",
  3605. progname, ifname));
  3606. return -3;
  3607. }
  3608. }
  3609. WARN((stderr, "\n%s: %s: decompression OK, trailing garbage ignored\n",
  3610. progname, ifname));
  3611. return -2;
  3612. }
  3613. }
  3614. /* ========================================================================
  3615. * Display the characteristics of the compressed file.
  3616. * If the given method is < 0, display the accumulated totals.
  3617. * IN assertions: time_stamp, header_bytes and ifile_size are initialized.
  3618. */
  3619. local void do_list(ifd, method)
  3620. int ifd; /* input file descriptor */
  3621. int method; /* compression method */
  3622. {
  3623. ulg crc; /* original crc */
  3624. static int first_time = 1;
  3625. static char* methods[MAX_METHODS] = {
  3626. "store", /* 0 */
  3627. "compr", /* 1 */
  3628. "pack ", /* 2 */
  3629. "lzh ", /* 3 */
  3630. "", "", "", "", /* 4 to 7 reserved */
  3631. "defla"}; /* 8 */
  3632. char *date;
  3633. int positive_off_t_width = 1;
  3634. off_t o;
  3635. for (o = OFF_T_MAX; 9 < o; o /= 10) {
  3636. positive_off_t_width++;
  3637. }
  3638. if (first_time && method >= 0) {
  3639. first_time = 0;
  3640. if (verbose) {
  3641. printf("method crc date time ");
  3642. }
  3643. if (!quiet) {
  3644. printf("%*.*s %*.*s ratio uncompressed_name\n",
  3645. positive_off_t_width, positive_off_t_width, "compressed",
  3646. positive_off_t_width, positive_off_t_width, "uncompressed");
  3647. }
  3648. } else if (method < 0) {
  3649. if (total_in <= 0 || total_out <= 0) return;
  3650. if (verbose) {
  3651. printf(" ");
  3652. }
  3653. if (verbose || !quiet) {
  3654. fprint_off(stdout, total_in, positive_off_t_width);
  3655. printf(" ");
  3656. fprint_off(stdout, total_out, positive_off_t_width);
  3657. printf(" ");
  3658. }
  3659. display_ratio(total_out-(total_in-header_bytes), total_out, stdout);
  3660. /* header_bytes is not meaningful but used to ensure the same
  3661. * ratio if there is a single file.
  3662. */
  3663. printf(" (totals)\n");
  3664. return;
  3665. }
  3666. crc = (ulg)~0; /* unknown */
  3667. bytes_out = -1L;
  3668. bytes_in = ifile_size;
  3669. #if RECORD_IO == 0
  3670. if (method == DEFLATED && !last_member) {
  3671. /* Get the crc and uncompressed size for gzip'ed (not zip'ed) files.
  3672. * If the lseek fails, we could use read() to get to the end, but
  3673. * --list is used to get quick results.
  3674. * Use "gunzip < foo.gz | wc -c" to get the uncompressed size if
  3675. * you are not concerned about speed.
  3676. */
  3677. bytes_in = lseek(ifd, (off_t)(-8), SEEK_END);
  3678. if (bytes_in != -1L) {
  3679. uch buf[8];
  3680. bytes_in += 8L;
  3681. if (read(ifd, (char*)buf, sizeof(buf)) != sizeof(buf)) {
  3682. read_error();
  3683. }
  3684. crc = LG(buf);
  3685. bytes_out = LG(buf+4);
  3686. }
  3687. }
  3688. #endif /* RECORD_IO */
  3689. date = ctime((time_t*)&time_stamp) + 4; /* skip the day of the week */
  3690. date[12] = '\0'; /* suppress the 1/100sec and the year */
  3691. if (verbose) {
  3692. printf("%5s %08lx %11s ", methods[method], crc, date);
  3693. }
  3694. fprint_off(stdout, bytes_in, positive_off_t_width);
  3695. printf(" ");
  3696. fprint_off(stdout, bytes_out, positive_off_t_width);
  3697. printf(" ");
  3698. if (bytes_in == -1L) {
  3699. total_in = -1L;
  3700. bytes_in = bytes_out = header_bytes = 0;
  3701. } else if (total_in >= 0) {
  3702. total_in += bytes_in;
  3703. }
  3704. if (bytes_out == -1L) {
  3705. total_out = -1L;
  3706. bytes_in = bytes_out = header_bytes = 0;
  3707. } else if (total_out >= 0) {
  3708. total_out += bytes_out;
  3709. }
  3710. display_ratio(bytes_out-(bytes_in-header_bytes), bytes_out, stdout);
  3711. printf(" %s\n", ofname);
  3712. }
  3713. /* ========================================================================
  3714. * Return true if the two stat structures correspond to the same file.
  3715. */
  3716. local int same_file(stat1, stat2)
  3717. struct stat *stat1;
  3718. struct stat *stat2;
  3719. {
  3720. return stat1->st_ino == stat2->st_ino
  3721. && stat1->st_dev == stat2->st_dev
  3722. #ifdef NO_ST_INO
  3723. /* Can't rely on st_ino and st_dev, use other fields: */
  3724. && stat1->st_mode == stat2->st_mode
  3725. && stat1->st_uid == stat2->st_uid
  3726. && stat1->st_gid == stat2->st_gid
  3727. && stat1->st_size == stat2->st_size
  3728. && stat1->st_atime == stat2->st_atime
  3729. && stat1->st_mtime == stat2->st_mtime
  3730. && stat1->st_ctime == stat2->st_ctime
  3731. #endif
  3732. ;
  3733. }
  3734. /* ========================================================================
  3735. * Return true if a file name is ambiguous because the operating system
  3736. * truncates file names.
  3737. */
  3738. local int name_too_long(name, statb)
  3739. char *name; /* file name to check */
  3740. struct stat *statb; /* stat buf for this file name */
  3741. {
  3742. int s = strlen(name);
  3743. char c = name[s-1];
  3744. struct stat tstat; /* stat for truncated name */
  3745. int res;
  3746. tstat = *statb; /* Just in case OS does not fill all fields */
  3747. name[s-1] = '\0';
  3748. res = lstat(name, &tstat) == 0 && same_file(statb, &tstat);
  3749. name[s-1] = c;
  3750. Trace((stderr, " too_long(%s) => %d\n", name, res));
  3751. return res;
  3752. }
  3753. /* ========================================================================
  3754. * Shorten the given name by one character, or replace a .tar extension
  3755. * with .tgz. Truncate the last part of the name which is longer than
  3756. * MIN_PART characters: 1234.678.012.gz -> 123.678.012.gz. If the name
  3757. * has only parts shorter than MIN_PART truncate the longest part.
  3758. * For decompression, just remove the last character of the name.
  3759. *
  3760. * IN assertion: for compression, the suffix of the given name is z_suffix.
  3761. */
  3762. local void shorten_name(name)
  3763. char *name;
  3764. {
  3765. int len; /* length of name without z_suffix */
  3766. char *trunc = NULL; /* character to be truncated */
  3767. int plen; /* current part length */
  3768. int min_part = MIN_PART; /* current minimum part length */
  3769. char *p;
  3770. len = strlen(name);
  3771. if (decompress) {
  3772. if (len <= 1) error("name too short");
  3773. name[len-1] = '\0';
  3774. return;
  3775. }
  3776. p = get_suffix(name);
  3777. if (p == NULL) error("can't recover suffix\n");
  3778. *p = '\0';
  3779. save_orig_name = 1;
  3780. /* compress 1234567890.tar to 1234567890.tgz */
  3781. if (len > 4 && strequ(p-4, ".tar")) {
  3782. strcpy(p-4, ".tgz");
  3783. return;
  3784. }
  3785. /* Try keeping short extensions intact:
  3786. * 1234.678.012.gz -> 123.678.012.gz
  3787. */
  3788. do {
  3789. p = strrchr(name, PATH_SEP);
  3790. p = p ? p+1 : name;
  3791. while (*p) {
  3792. plen = strcspn(p, PART_SEP);
  3793. p += plen;
  3794. if (plen > min_part) trunc = p-1;
  3795. if (*p) p++;
  3796. }
  3797. } while (trunc == NULL && --min_part != 0);
  3798. if (trunc != NULL) {
  3799. do {
  3800. trunc[0] = trunc[1];
  3801. } while (*trunc++);
  3802. trunc--;
  3803. } else {
  3804. trunc = strrchr(name, PART_SEP[0]);
  3805. if (trunc == NULL) error("internal error in shorten_name");
  3806. if (trunc[1] == '\0') trunc--; /* force truncation */
  3807. }
  3808. strcpy(trunc, z_suffix);
  3809. }
  3810. /* ========================================================================
  3811. * If compressing to a file, check if ofname is not ambiguous
  3812. * because the operating system truncates names. Otherwise, generate
  3813. * a new ofname and save the original name in the compressed file.
  3814. * If the compressed file already exists, ask for confirmation.
  3815. * The check for name truncation is made dynamically, because different
  3816. * file systems on the same OS might use different truncation rules (on SVR4
  3817. * s5 truncates to 14 chars and ufs does not truncate).
  3818. * This function returns -1 if the file must be skipped, and
  3819. * updates save_orig_name if necessary.
  3820. * IN assertions: save_orig_name is already set if ofname has been
  3821. * already truncated because of NO_MULTIPLE_DOTS. The input file has
  3822. * already been open and istat is set.
  3823. */
  3824. local int check_ofname()
  3825. {
  3826. struct stat ostat; /* stat for ofname */
  3827. #ifdef ENAMETOOLONG
  3828. /* Check for strictly conforming Posix systems (which return ENAMETOOLONG
  3829. * instead of silently truncating filenames).
  3830. */
  3831. errno = 0;
  3832. while (lstat(ofname, &ostat) != 0) {
  3833. if (errno != ENAMETOOLONG) return 0; /* ofname does not exist */
  3834. shorten_name(ofname);
  3835. }
  3836. #else
  3837. if (lstat(ofname, &ostat) != 0) return 0;
  3838. #endif
  3839. /* Check for name truncation on existing file. Do this even on systems
  3840. * defining ENAMETOOLONG, because on most systems the strict Posix
  3841. * behavior is disabled by default (silent name truncation allowed).
  3842. */
  3843. if (!decompress && name_too_long(ofname, &ostat)) {
  3844. shorten_name(ofname);
  3845. if (lstat(ofname, &ostat) != 0) return 0;
  3846. }
  3847. /* Check that the input and output files are different (could be
  3848. * the same by name truncation or links).
  3849. */
  3850. if (same_file(&istat, &ostat)) {
  3851. if (strequ(ifname, ofname)) {
  3852. fprintf(stderr, "%s: %s: cannot %scompress onto itself\n",
  3853. progname, ifname, decompress ? "de" : "");
  3854. } else {
  3855. fprintf(stderr, "%s: %s and %s are the same file\n",
  3856. progname, ifname, ofname);
  3857. }
  3858. exit_code = ERROR;
  3859. return ERROR;
  3860. }
  3861. /* Ask permission to overwrite the existing file */
  3862. if (!force) {
  3863. int ok = 0;
  3864. fprintf(stderr, "%s: %s already exists;", progname, ofname);
  3865. if (foreground && isatty(fileno(stdin))) {
  3866. fprintf(stderr, " do you wish to overwrite (y or n)? ");
  3867. fflush(stderr);
  3868. ok = yesno();
  3869. }
  3870. if (!ok) {
  3871. fprintf(stderr, "\tnot overwritten\n");
  3872. if (exit_code == OK) exit_code = WARNING;
  3873. return ERROR;
  3874. }
  3875. }
  3876. if (xunlink (ofname)) {
  3877. progerror(ofname);
  3878. return ERROR;
  3879. }
  3880. return OK;
  3881. }
  3882. #ifndef NO_UTIME
  3883. /* ========================================================================
  3884. * Set the access and modification times from the given stat buffer.
  3885. */
  3886. local void reset_times (name, statb)
  3887. char *name;
  3888. struct stat *statb;
  3889. {
  3890. struct utimbuf timep;
  3891. /* Copy the time stamp */
  3892. timep.actime = statb->st_atime;
  3893. timep.modtime = statb->st_mtime;
  3894. /* Some systems (at least OS/2) do not support utime on directories */
  3895. if (utime(name, &timep) && !S_ISDIR(statb->st_mode)) {
  3896. int e = errno;
  3897. WARN((stderr, "%s: ", progname));
  3898. if (!quiet) {
  3899. errno = e;
  3900. perror(ofname);
  3901. }
  3902. }
  3903. }
  3904. #endif
  3905. /* ========================================================================
  3906. * Copy modes, times, ownership from input file to output file.
  3907. * IN assertion: to_stdout is false.
  3908. */
  3909. local void copy_stat(ifstat)
  3910. struct stat *ifstat;
  3911. {
  3912. #ifndef NO_UTIME
  3913. if (decompress && time_stamp != 0 && ifstat->st_mtime != time_stamp) {
  3914. ifstat->st_mtime = time_stamp;
  3915. if (verbose > 1) {
  3916. fprintf(stderr, "%s: time stamp restored\n", ofname);
  3917. }
  3918. }
  3919. reset_times(ofname, ifstat);
  3920. #endif
  3921. /* Copy the protection modes */
  3922. if (fchmod(ofd, ifstat->st_mode & 07777)) {
  3923. int e = errno;
  3924. WARN((stderr, "%s: ", progname));
  3925. if (!quiet) {
  3926. errno = e;
  3927. perror(ofname);
  3928. }
  3929. }
  3930. #ifndef NO_CHOWN
  3931. fchown(ofd, ifstat->st_uid, ifstat->st_gid); /* Copy ownership */
  3932. #endif
  3933. remove_ofname = 0;
  3934. /* It's now safe to remove the input file: */
  3935. if (xunlink (ifname)) {
  3936. int e = errno;
  3937. WARN((stderr, "%s: ", progname));
  3938. if (!quiet) {
  3939. errno = e;
  3940. perror(ifname);
  3941. }
  3942. }
  3943. }
  3944. #if ! NO_DIR
  3945. /* ========================================================================
  3946. * Recurse through the given directory. This code is taken from ncompress.
  3947. */
  3948. local void treat_dir(dir)
  3949. char *dir;
  3950. {
  3951. struct dirent *dp;
  3952. DIR *dirp;
  3953. char nbuf[MAX_PATH_LEN];
  3954. int len;
  3955. dirp = opendir(dir);
  3956. if (dirp == NULL) {
  3957. progerror(dir);
  3958. return ;
  3959. }
  3960. /*
  3961. ** WARNING: the following algorithm could occasionally cause
  3962. ** compress to produce error warnings of the form "<filename>.gz
  3963. ** already has .gz suffix - ignored". This occurs when the
  3964. ** .gz output file is inserted into the directory below
  3965. ** readdir's current pointer.
  3966. ** These warnings are harmless but annoying, so they are suppressed
  3967. ** with option -r (except when -v is on). An alternative
  3968. ** to allowing this would be to store the entire directory
  3969. ** list in memory, then compress the entries in the stored
  3970. ** list. Given the depth-first recursive algorithm used here,
  3971. ** this could use up a tremendous amount of memory. I don't
  3972. ** think it's worth it. -- Dave Mack
  3973. ** (An other alternative might be two passes to avoid depth-first.)
  3974. */
  3975. while ((errno = 0, dp = readdir(dirp)) != NULL) {
  3976. if (strequ(dp->d_name,".") || strequ(dp->d_name,"..")) {
  3977. continue;
  3978. }
  3979. len = strlen(dir);
  3980. if (len + NAMLEN(dp) + 1 < MAX_PATH_LEN - 1) {
  3981. strcpy(nbuf,dir);
  3982. if (len != 0 /* dir = "" means current dir on Amiga */
  3983. #ifdef PATH_SEP2
  3984. && dir[len-1] != PATH_SEP2
  3985. #endif
  3986. #ifdef PATH_SEP3
  3987. && dir[len-1] != PATH_SEP3
  3988. #endif
  3989. ) {
  3990. nbuf[len++] = PATH_SEP;
  3991. }
  3992. strcpy(nbuf+len, dp->d_name);
  3993. treat_file(nbuf);
  3994. } else {
  3995. fprintf(stderr,"%s: %s/%s: pathname too long\n",
  3996. progname, dir, dp->d_name);
  3997. exit_code = ERROR;
  3998. }
  3999. }
  4000. if (errno != 0)
  4001. progerror(dir);
  4002. if (CLOSEDIR(dirp) != 0)
  4003. progerror(dir);
  4004. }
  4005. #endif /* ! NO_DIR */
  4006. /* ========================================================================
  4007. * Free all dynamically allocated variables and exit with the given code.
  4008. */
  4009. local void do_exit(exitcode)
  4010. int exitcode;
  4011. {
  4012. static int in_exit = 0;
  4013. if (in_exit) exit(exitcode);
  4014. in_exit = 1;
  4015. if (env != NULL) free(env), env = NULL;
  4016. if (args != NULL) free((char*)args), args = NULL;
  4017. FREE(inbuf);
  4018. FREE(outbuf);
  4019. FREE(d_buf);
  4020. FREE(window);
  4021. #ifndef MAXSEG_64K
  4022. FREE(tab_prefix);
  4023. #else
  4024. FREE(tab_prefix0);
  4025. FREE(tab_prefix1);
  4026. #endif
  4027. exit(exitcode);
  4028. }
  4029. /* ========================================================================
  4030. * Close and unlink the output file if appropriate. This routine must be
  4031. * async-signal-safe.
  4032. */
  4033. local void do_remove() {
  4034. if (remove_ofname) {
  4035. close(ofd);
  4036. xunlink (ofname);
  4037. }
  4038. }
  4039. /* ========================================================================
  4040. * Error handler.
  4041. */
  4042. RETSIGTYPE abort_gzip()
  4043. {
  4044. do_remove();
  4045. do_exit(ERROR);
  4046. }
  4047. /* ========================================================================
  4048. * Signal handler.
  4049. */
  4050. RETSIGTYPE abort_gzip_signal()
  4051. {
  4052. do_remove();
  4053. _exit(ERROR);
  4054. }
  4055. /* Inflate deflated data
  4056. Copyright (C) 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
  4057. This program is free software; you can redistribute it and/or modify
  4058. it under the terms of the GNU General Public License as published by
  4059. the Free Software Foundation; either version 2, or (at your option)
  4060. any later version.
  4061. This program is distributed in the hope that it will be useful,
  4062. but WITHOUT ANY WARRANTY; without even the implied warranty of
  4063. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  4064. See the GNU General Public License for more details.
  4065. You should have received a copy of the GNU General Public License
  4066. along with this program; see the file COPYING.
  4067. If not, write to the Free Software Foundation,
  4068. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  4069. /* Not copyrighted 1992 by Mark Adler
  4070. version c10p1, 10 January 1993 */
  4071. /* You can do whatever you like with this source file, though I would
  4072. prefer that if you modify it and redistribute it that you include
  4073. comments to that effect with your name and the date. Thank you.
  4074. [The history has been moved to the file ChangeLog.]
  4075. */
  4076. /*
  4077. Inflate deflated (PKZIP's method 8 compressed) data. The compression
  4078. method searches for as much of the current string of bytes (up to a
  4079. length of 258) in the previous 32K bytes. If it doesn't find any
  4080. matches (of at least length 3), it codes the next byte. Otherwise, it
  4081. codes the length of the matched string and its distance backwards from
  4082. the current position. There is a single Huffman code that codes both
  4083. single bytes (called "literals") and match lengths. A second Huffman
  4084. code codes the distance information, which follows a length code. Each
  4085. length or distance code actually represents a base value and a number
  4086. of "extra" (sometimes zero) bits to get to add to the base value. At
  4087. the end of each deflated block is a special end-of-block (EOB) literal/
  4088. length code. The decoding process is basically: get a literal/length
  4089. code; if EOB then done; if a literal, emit the decoded byte; if a
  4090. length then get the distance and emit the referred-to bytes from the
  4091. sliding window of previously emitted data.
  4092. There are (currently) three kinds of inflate blocks: stored, fixed, and
  4093. dynamic. The compressor deals with some chunk of data at a time, and
  4094. decides which method to use on a chunk-by-chunk basis. A chunk might
  4095. typically be 32K or 64K. If the chunk is uncompressible, then the
  4096. "stored" method is used. In this case, the bytes are simply stored as
  4097. is, eight bits per byte, with none of the above coding. The bytes are
  4098. preceded by a count, since there is no longer an EOB code.
  4099. If the data is compressible, then either the fixed or dynamic methods
  4100. are used. In the dynamic method, the compressed data is preceded by
  4101. an encoding of the literal/length and distance Huffman codes that are
  4102. to be used to decode this block. The representation is itself Huffman
  4103. coded, and so is preceded by a description of that code. These code
  4104. descriptions take up a little space, and so for small blocks, there is
  4105. a predefined set of codes, called the fixed codes. The fixed method is
  4106. used if the block codes up smaller that way (usually for quite small
  4107. chunks), otherwise the dynamic method is used. In the latter case, the
  4108. codes are customized to the probabilities in the current block, and so
  4109. can code it much better than the pre-determined fixed codes.
  4110. The Huffman codes themselves are decoded using a multi-level table
  4111. lookup, in order to maximize the speed of decoding plus the speed of
  4112. building the decoding tables. See the comments below that precede the
  4113. lbits and dbits tuning parameters.
  4114. */
  4115. /*
  4116. Notes beyond the 1.93a appnote.txt:
  4117. 1. Distance pointers never point before the beginning of the output
  4118. stream.
  4119. 2. Distance pointers can point back across blocks, up to 32k away.
  4120. 3. There is an implied maximum of 7 bits for the bit length table and
  4121. 15 bits for the actual data.
  4122. 4. If only one code exists, then it is encoded using one bit. (Zero
  4123. would be more efficient, but perhaps a little confusing.) If two
  4124. codes exist, they are coded using one bit each (0 and 1).
  4125. 5. There is no way of sending zero distance codes--a dummy must be
  4126. sent if there are none. (History: a pre 2.0 version of PKZIP would
  4127. store blocks with no distance codes, but this was discovered to be
  4128. too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  4129. zero distance codes, which is sent as one code of zero bits in
  4130. length.
  4131. 6. There are up to 286 literal/length codes. Code 256 represents the
  4132. end-of-block. Note however that the static length tree defines
  4133. 288 codes just to fill out the Huffman codes. Codes 286 and 287
  4134. cannot be used though, since there is no length base or extra bits
  4135. defined for them. Similarly, there are up to 30 distance codes.
  4136. However, static trees define 32 codes (all 5 bits) to fill out the
  4137. Huffman codes, but the last two had better not show up in the data.
  4138. 7. Unzip can check dynamic Huffman blocks for complete code sets.
  4139. The exception is that a single code would not be complete (see #4).
  4140. 8. The five bits following the block type is really the number of
  4141. literal codes sent minus 257.
  4142. 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  4143. (1+6+6). Therefore, to output three times the length, you output
  4144. three codes (1+1+1), whereas to output four times the same length,
  4145. you only need two codes (1+3). Hmm.
  4146. 10. In the tree reconstruction algorithm, Code = Code + Increment
  4147. only if BitLength(i) is not zero. (Pretty obvious.)
  4148. 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  4149. 12. Note: length code 284 can represent 227-258, but length code 285
  4150. really is 258. The last length deserves its own, short code
  4151. since it gets used a lot in very redundant files. The length
  4152. 258 is special since 258 - 3 (the min match length) is 255.
  4153. 13. The literal/length and distance code bit lengths are read as a
  4154. single stream of lengths. It is possible (and advantageous) for
  4155. a repeat code (16, 17, or 18) to go across the boundary between
  4156. the two sets of lengths.
  4157. */
  4158. #ifdef RCSID
  4159. static char rcsid[] = "$Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp $";
  4160. #endif
  4161. #define slide window
  4162. /* Huffman code lookup table entry--this entry is four bytes for machines
  4163. that have 16-bit pointers (e.g. PC's in the small or medium model).
  4164. Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
  4165. means that v is a literal, 16 < e < 32 means that v is a pointer to
  4166. the next table, which codes e - 16 bits, and lastly e == 99 indicates
  4167. an unused code. If a code with e == 99 is looked up, this implies an
  4168. error in the data. */
  4169. struct huft {
  4170. uch e; /* number of extra bits or operation */
  4171. uch b; /* number of bits in this code or subcode */
  4172. union {
  4173. ush n; /* literal, length base, or distance base */
  4174. struct huft *t; /* pointer to next level of table */
  4175. } v;
  4176. };
  4177. /* Function prototypes */
  4178. int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *,
  4179. struct huft **, int *));
  4180. int huft_free OF((struct huft *));
  4181. int inflate_codes OF((struct huft *, struct huft *, int, int));
  4182. int inflate_stored OF((void));
  4183. int inflate_fixed OF((void));
  4184. int inflate_dynamic OF((void));
  4185. int inflate_block OF((int *));
  4186. int inflate OF((void));
  4187. /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
  4188. stream to find repeated byte strings. This is implemented here as a
  4189. circular buffer. The index is updated simply by incrementing and then
  4190. and'ing with 0x7fff (32K-1). */
  4191. /* It is left to other modules to supply the 32K area. It is assumed
  4192. to be usable as if it were declared "uch slide[32768];" or as just
  4193. "uch *slide;" and then malloc'ed in the latter case. The definition
  4194. must be in unzip.h, included above. */
  4195. /* unsigned wp; current position in slide */
  4196. #define wp outcnt
  4197. #define flush_output(w) (wp=(w),flush_window())
  4198. /* Tables for deflate from PKZIP's appnote.txt. */
  4199. static unsigned border[] = { /* Order of the bit length code lengths */
  4200. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  4201. static ush cplens[] = { /* Copy lengths for literal codes 257..285 */
  4202. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  4203. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  4204. /* note: see note #13 above about the 258 in this list. */
  4205. static ush cplext[] = { /* Extra bits for literal codes 257..285 */
  4206. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  4207. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
  4208. static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
  4209. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  4210. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  4211. 8193, 12289, 16385, 24577};
  4212. static ush cpdext[] = { /* Extra bits for distance codes */
  4213. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  4214. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  4215. 12, 12, 13, 13};
  4216. /* Macros for inflate() bit peeking and grabbing.
  4217. The usage is:
  4218. NEEDBITS(j)
  4219. x = b & mask_bits[j];
  4220. DUMPBITS(j)
  4221. where NEEDBITS makes sure that b has at least j bits in it, and
  4222. DUMPBITS removes the bits from b. The macros use the variable k
  4223. for the number of bits in b. Normally, b and k are register
  4224. variables for speed, and are initialized at the beginning of a
  4225. routine that uses these macros from a global bit buffer and count.
  4226. The macros also use the variable w, which is a cached copy of wp.
  4227. If we assume that EOB will be the longest code, then we will never
  4228. ask for bits with NEEDBITS that are beyond the end of the stream.
  4229. So, NEEDBITS should not read any more bytes than are needed to
  4230. meet the request. Then no bytes need to be "returned" to the buffer
  4231. at the end of the last block.
  4232. However, this assumption is not true for fixed blocks--the EOB code
  4233. is 7 bits, but the other literal/length codes can be 8 or 9 bits.
  4234. (The EOB code is shorter than other codes because fixed blocks are
  4235. generally short. So, while a block always has an EOB, many other
  4236. literal/length codes have a significantly lower probability of
  4237. showing up at all.) However, by making the first table have a
  4238. lookup of seven bits, the EOB code will be found in that first
  4239. lookup, and so will not require that too many bits be pulled from
  4240. the stream.
  4241. */
  4242. ulg bb; /* bit buffer */
  4243. unsigned bk; /* bits in bit buffer */
  4244. ush mask_bits[] = {
  4245. 0x0000,
  4246. 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  4247. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  4248. };
  4249. #define GETBYTE() (inptr < insize ? inbuf[inptr++] : (wp = w, fill_inbuf(0)))
  4250. #ifdef CRYPT
  4251. uch cc;
  4252. # define NEXTBYTE() \
  4253. (decrypt ? (cc = GETBYTE(), zdecode(cc), cc) : GETBYTE())
  4254. #else
  4255. # define NEXTBYTE() (uch)GETBYTE()
  4256. #endif
  4257. #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
  4258. #define DUMPBITS(n) {b>>=(n);k-=(n);}
  4259. /*
  4260. Huffman code decoding is performed using a multi-level table lookup.
  4261. The fastest way to decode is to simply build a lookup table whose
  4262. size is determined by the longest code. However, the time it takes
  4263. to build this table can also be a factor if the data being decoded
  4264. is not very long. The most common codes are necessarily the
  4265. shortest codes, so those codes dominate the decoding time, and hence
  4266. the speed. The idea is you can have a shorter table that decodes the
  4267. shorter, more probable codes, and then point to subsidiary tables for
  4268. the longer codes. The time it costs to decode the longer codes is
  4269. then traded against the time it takes to make longer tables.
  4270. This results of this trade are in the variables lbits and dbits
  4271. below. lbits is the number of bits the first level table for literal/
  4272. length codes can decode in one step, and dbits is the same thing for
  4273. the distance codes. Subsequent tables are also less than or equal to
  4274. those sizes. These values may be adjusted either when all of the
  4275. codes are shorter than that, in which case the longest code length in
  4276. bits is used, or when the shortest code is *longer* than the requested
  4277. table size, in which case the length of the shortest code in bits is
  4278. used.
  4279. There are two different values for the two tables, since they code a
  4280. different number of possibilities each. The literal/length table
  4281. codes 286 possible values, or in a flat code, a little over eight
  4282. bits. The distance table codes 30 possible values, or a little less
  4283. than five bits, flat. The optimum values for speed end up being
  4284. about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  4285. The optimum values may differ though from machine to machine, and
  4286. possibly even between compilers. Your mileage may vary.
  4287. */
  4288. int lbits = 9; /* bits in base literal/length lookup table */
  4289. int dbits = 6; /* bits in base distance lookup table */
  4290. /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
  4291. #define BMAX 16 /* maximum bit length of any code (16 for explode) */
  4292. #define N_MAX 288 /* maximum number of codes in any set */
  4293. unsigned hufts; /* track memory usage */
  4294. int huft_build(b, n, s, d, e, t, m)
  4295. unsigned *b; /* code lengths in bits (all assumed <= BMAX) */
  4296. unsigned n; /* number of codes (assumed <= N_MAX) */
  4297. unsigned s; /* number of simple-valued codes (0..s-1) */
  4298. ush *d; /* list of base values for non-simple codes */
  4299. ush *e; /* list of extra bits for non-simple codes */
  4300. struct huft **t; /* result: starting table */
  4301. int *m; /* maximum lookup bits, returns actual */
  4302. /* Given a list of code lengths and a maximum table size, make a set of
  4303. tables to decode that set of codes. Return zero on success, one if
  4304. the given code set is incomplete (the tables are still built in this
  4305. case), two if the input is invalid (all zero length codes or an
  4306. oversubscribed set of lengths), and three if not enough memory. */
  4307. {
  4308. unsigned a; /* counter for codes of length k */
  4309. unsigned c[BMAX+1]; /* bit length count table */
  4310. unsigned f; /* i repeats in table every f entries */
  4311. int g; /* maximum code length */
  4312. int h; /* table level */
  4313. register unsigned i; /* counter, current code */
  4314. register unsigned j; /* counter */
  4315. register int k; /* number of bits in current code */
  4316. int l; /* bits per table (returned in m) */
  4317. register unsigned *p; /* pointer into c[], b[], or v[] */
  4318. register struct huft *q; /* points to current table */
  4319. struct huft r; /* table entry for structure assignment */
  4320. struct huft *u[BMAX]; /* table stack */
  4321. unsigned v[N_MAX]; /* values in order of bit length */
  4322. register int w; /* bits before this table == (l * h) */
  4323. unsigned x[BMAX+1]; /* bit offsets, then code stack */
  4324. unsigned *xp; /* pointer into x */
  4325. int y; /* number of dummy codes added */
  4326. unsigned z; /* number of entries in current table */
  4327. /* Generate counts for each bit length */
  4328. memzero(c, sizeof(c));
  4329. p = b; i = n;
  4330. do {
  4331. Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"),
  4332. n-i, *p));
  4333. c[*p]++; /* assume all entries <= BMAX */
  4334. p++; /* Can't combine with above line (Solaris bug) */
  4335. } while (--i);
  4336. if (c[0] == n) /* null input--all zero length codes */
  4337. {
  4338. *t = (struct huft *)NULL;
  4339. *m = 0;
  4340. return 0;
  4341. }
  4342. /* Find minimum and maximum length, bound *m by those */
  4343. l = *m;
  4344. for (j = 1; j <= BMAX; j++)
  4345. if (c[j])
  4346. break;
  4347. k = j; /* minimum code length */
  4348. if ((unsigned)l < j)
  4349. l = j;
  4350. for (i = BMAX; i; i--)
  4351. if (c[i])
  4352. break;
  4353. g = i; /* maximum code length */
  4354. if ((unsigned)l > i)
  4355. l = i;
  4356. *m = l;
  4357. /* Adjust last length count to fill out codes, if needed */
  4358. for (y = 1 << j; j < i; j++, y <<= 1)
  4359. if ((y -= c[j]) < 0)
  4360. return 2; /* bad input: more codes than bits */
  4361. if ((y -= c[i]) < 0)
  4362. return 2;
  4363. c[i] += y;
  4364. /* Generate starting offsets into the value table for each length */
  4365. x[1] = j = 0;
  4366. p = c + 1; xp = x + 2;
  4367. while (--i) { /* note that i == g from above */
  4368. *xp++ = (j += *p++);
  4369. }
  4370. /* Make a table of values in order of bit lengths */
  4371. p = b; i = 0;
  4372. do {
  4373. if ((j = *p++) != 0)
  4374. v[x[j]++] = i;
  4375. } while (++i < n);
  4376. n = x[g]; /* set n to length of v */
  4377. /* Generate the Huffman codes and for each, make the table entries */
  4378. x[0] = i = 0; /* first Huffman code is zero */
  4379. p = v; /* grab values in bit order */
  4380. h = -1; /* no tables yet--level -1 */
  4381. w = -l; /* bits decoded == (l * h) */
  4382. u[0] = (struct huft *)NULL; /* just to keep compilers happy */
  4383. q = (struct huft *)NULL; /* ditto */
  4384. z = 0; /* ditto */
  4385. /* go through the bit lengths (k already is bits in shortest code) */
  4386. for (; k <= g; k++)
  4387. {
  4388. a = c[k];
  4389. while (a--)
  4390. {
  4391. /* here i is the Huffman code of length k bits for value *p */
  4392. /* make tables up to required level */
  4393. while (k > w + l)
  4394. {
  4395. h++;
  4396. w += l; /* previous table always l bits */
  4397. /* compute minimum size table less than or equal to l bits */
  4398. z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */
  4399. if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
  4400. { /* too few codes for k-w bit table */
  4401. f -= a + 1; /* deduct codes from patterns left */
  4402. xp = c + k;
  4403. if (j < z)
  4404. while (++j < z) /* try smaller tables up to z bits */
  4405. {
  4406. if ((f <<= 1) <= *++xp)
  4407. break; /* enough codes to use up j bits */
  4408. f -= *xp; /* else deduct codes from patterns */
  4409. }
  4410. }
  4411. z = 1 << j; /* table entries for j-bit table */
  4412. /* allocate and link in new table */
  4413. if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
  4414. (struct huft *)NULL)
  4415. {
  4416. if (h)
  4417. huft_free(u[0]);
  4418. return 3; /* not enough memory */
  4419. }
  4420. hufts += z + 1; /* track memory usage */
  4421. *t = q + 1; /* link to list for huft_free() */
  4422. *(t = &(q->v.t)) = (struct huft *)NULL;
  4423. u[h] = ++q; /* table starts after link */
  4424. /* connect to last table, if there is one */
  4425. if (h)
  4426. {
  4427. x[h] = i; /* save pattern for backing up */
  4428. r.b = (uch)l; /* bits to dump before this table */
  4429. r.e = (uch)(16 + j); /* bits in this table */
  4430. r.v.t = q; /* pointer to this table */
  4431. j = i >> (w - l); /* (get around Turbo C bug) */
  4432. u[h-1][j] = r; /* connect to last table */
  4433. }
  4434. }
  4435. /* set up table entry in r */
  4436. r.b = (uch)(k - w);
  4437. if (p >= v + n)
  4438. r.e = 99; /* out of values--invalid code */
  4439. else if (*p < s)
  4440. {
  4441. r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
  4442. r.v.n = (ush)(*p); /* simple code is just the value */
  4443. p++; /* one compiler does not like *p++ */
  4444. }
  4445. else
  4446. {
  4447. r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
  4448. r.v.n = d[*p++ - s];
  4449. }
  4450. /* fill code-like entries with r */
  4451. f = 1 << (k - w);
  4452. for (j = i >> w; j < z; j += f)
  4453. q[j] = r;
  4454. /* backwards increment the k-bit code i */
  4455. for (j = 1 << (k - 1); i & j; j >>= 1)
  4456. i ^= j;
  4457. i ^= j;
  4458. /* backup over finished tables */
  4459. while ((i & ((1 << w) - 1)) != x[h])
  4460. {
  4461. h--; /* don't need to update q */
  4462. w -= l;
  4463. }
  4464. }
  4465. }
  4466. /* Return true (1) if we were given an incomplete table */
  4467. return y != 0 && g != 1;
  4468. }
  4469. int huft_free(t)
  4470. struct huft *t; /* table to free */
  4471. /* Free the malloc'ed tables built by huft_build(), which makes a linked
  4472. list of the tables it made, with the links in a dummy first entry of
  4473. each table. */
  4474. {
  4475. register struct huft *p, *q;
  4476. /* Go through linked list, freeing from the malloced (t[-1]) address. */
  4477. p = t;
  4478. while (p != (struct huft *)NULL)
  4479. {
  4480. q = (--p)->v.t;
  4481. free((char*)p);
  4482. p = q;
  4483. }
  4484. return 0;
  4485. }
  4486. int inflate_codes(tl, td, bl, bd)
  4487. struct huft *tl, *td; /* literal/length and distance decoder tables */
  4488. int bl, bd; /* number of bits decoded by tl[] and td[] */
  4489. /* inflate (decompress) the codes in a deflated (compressed) block.
  4490. Return an error code or zero if it all goes ok. */
  4491. {
  4492. register unsigned e; /* table entry flag/number of extra bits */
  4493. unsigned n, d; /* length and index for copy */
  4494. unsigned w; /* current window position */
  4495. struct huft *t; /* pointer to table entry */
  4496. unsigned ml, md; /* masks for bl and bd bits */
  4497. register ulg b; /* bit buffer */
  4498. register unsigned k; /* number of bits in bit buffer */
  4499. /* make local copies of globals */
  4500. b = bb; /* initialize bit buffer */
  4501. k = bk;
  4502. w = wp; /* initialize window position */
  4503. /* inflate the coded data */
  4504. ml = mask_bits[bl]; /* precompute masks for speed */
  4505. md = mask_bits[bd];
  4506. for (;;) /* do until end of block */
  4507. {
  4508. NEEDBITS((unsigned)bl)
  4509. if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
  4510. do {
  4511. if (e == 99)
  4512. return 1;
  4513. DUMPBITS(t->b)
  4514. e -= 16;
  4515. NEEDBITS(e)
  4516. } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
  4517. DUMPBITS(t->b)
  4518. if (e == 16) /* then it's a literal */
  4519. {
  4520. slide[w++] = (uch)t->v.n;
  4521. Tracevv((stderr, "%c", slide[w-1]));
  4522. if (w == WSIZE)
  4523. {
  4524. flush_output(w);
  4525. w = 0;
  4526. }
  4527. }
  4528. else /* it's an EOB or a length */
  4529. {
  4530. /* exit if end of block */
  4531. if (e == 15)
  4532. break;
  4533. /* get length of block to copy */
  4534. NEEDBITS(e)
  4535. n = t->v.n + ((unsigned)b & mask_bits[e]);
  4536. DUMPBITS(e);
  4537. /* decode distance of block to copy */
  4538. NEEDBITS((unsigned)bd)
  4539. if ((e = (t = td + ((unsigned)b & md))->e) > 16)
  4540. do {
  4541. if (e == 99)
  4542. return 1;
  4543. DUMPBITS(t->b)
  4544. e -= 16;
  4545. NEEDBITS(e)
  4546. } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
  4547. DUMPBITS(t->b)
  4548. NEEDBITS(e)
  4549. d = w - t->v.n - ((unsigned)b & mask_bits[e]);
  4550. DUMPBITS(e)
  4551. Tracevv((stderr,"\\[%d,%d]", w-d, n));
  4552. /* do the copy */
  4553. do {
  4554. n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  4555. #if !defined(NOMEMCPY) && !defined(DEBUG)
  4556. if (w - d >= e) /* (this test assumes unsigned comparison) */
  4557. {
  4558. memcpy(slide + w, slide + d, e);
  4559. w += e;
  4560. d += e;
  4561. }
  4562. else /* do it slow to avoid memcpy() overlap */
  4563. #endif /* !NOMEMCPY */
  4564. do {
  4565. slide[w++] = slide[d++];
  4566. Tracevv((stderr, "%c", slide[w-1]));
  4567. } while (--e);
  4568. if (w == WSIZE)
  4569. {
  4570. flush_output(w);
  4571. w = 0;
  4572. }
  4573. } while (n);
  4574. }
  4575. }
  4576. /* restore the globals from the locals */
  4577. wp = w; /* restore global window pointer */
  4578. bb = b; /* restore global bit buffer */
  4579. bk = k;
  4580. /* done */
  4581. return 0;
  4582. }
  4583. int inflate_stored()
  4584. /* "decompress" an inflated type 0 (stored) block. */
  4585. {
  4586. unsigned n; /* number of bytes in block */
  4587. unsigned w; /* current window position */
  4588. register ulg b; /* bit buffer */
  4589. register unsigned k; /* number of bits in bit buffer */
  4590. /* make local copies of globals */
  4591. b = bb; /* initialize bit buffer */
  4592. k = bk;
  4593. w = wp; /* initialize window position */
  4594. /* go to byte boundary */
  4595. n = k & 7;
  4596. DUMPBITS(n);
  4597. /* get the length and its complement */
  4598. NEEDBITS(16)
  4599. n = ((unsigned)b & 0xffff);
  4600. DUMPBITS(16)
  4601. NEEDBITS(16)
  4602. if (n != (unsigned)((~b) & 0xffff))
  4603. return 1; /* error in compressed data */
  4604. DUMPBITS(16)
  4605. /* read and output the compressed data */
  4606. while (n--)
  4607. {
  4608. NEEDBITS(8)
  4609. slide[w++] = (uch)b;
  4610. if (w == WSIZE)
  4611. {
  4612. flush_output(w);
  4613. w = 0;
  4614. }
  4615. DUMPBITS(8)
  4616. }
  4617. /* restore the globals from the locals */
  4618. wp = w; /* restore global window pointer */
  4619. bb = b; /* restore global bit buffer */
  4620. bk = k;
  4621. return 0;
  4622. }
  4623. int inflate_fixed()
  4624. /* decompress an inflated type 1 (fixed Huffman codes) block. We should
  4625. either replace this with a custom decoder, or at least precompute the
  4626. Huffman tables. */
  4627. {
  4628. int i; /* temporary variable */
  4629. struct huft *tl; /* literal/length code table */
  4630. struct huft *td; /* distance code table */
  4631. int bl; /* lookup bits for tl */
  4632. int bd; /* lookup bits for td */
  4633. unsigned l[288]; /* length list for huft_build */
  4634. /* set up literal table */
  4635. for (i = 0; i < 144; i++)
  4636. l[i] = 8;
  4637. for (; i < 256; i++)
  4638. l[i] = 9;
  4639. for (; i < 280; i++)
  4640. l[i] = 7;
  4641. for (; i < 288; i++) /* make a complete, but wrong code set */
  4642. l[i] = 8;
  4643. bl = 7;
  4644. if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
  4645. return i;
  4646. /* set up distance table */
  4647. for (i = 0; i < 30; i++) /* make an incomplete code set */
  4648. l[i] = 5;
  4649. bd = 5;
  4650. if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
  4651. {
  4652. huft_free(tl);
  4653. return i;
  4654. }
  4655. /* decompress until an end-of-block code */
  4656. if (inflate_codes(tl, td, bl, bd))
  4657. return 1;
  4658. /* free the decoding tables, return */
  4659. huft_free(tl);
  4660. huft_free(td);
  4661. return 0;
  4662. }
  4663. int inflate_dynamic()
  4664. /* decompress an inflated type 2 (dynamic Huffman codes) block. */
  4665. {
  4666. int i; /* temporary variables */
  4667. unsigned j;
  4668. unsigned l; /* last length */
  4669. unsigned m; /* mask for bit lengths table */
  4670. unsigned n; /* number of lengths to get */
  4671. unsigned w; /* current window position */
  4672. struct huft *tl; /* literal/length code table */
  4673. struct huft *td; /* distance code table */
  4674. int bl; /* lookup bits for tl */
  4675. int bd; /* lookup bits for td */
  4676. unsigned nb; /* number of bit length codes */
  4677. unsigned nl; /* number of literal/length codes */
  4678. unsigned nd; /* number of distance codes */
  4679. #ifdef PKZIP_BUG_WORKAROUND
  4680. unsigned ll[288+32]; /* literal/length and distance code lengths */
  4681. #else
  4682. unsigned ll[286+30]; /* literal/length and distance code lengths */
  4683. #endif
  4684. register ulg b; /* bit buffer */
  4685. register unsigned k; /* number of bits in bit buffer */
  4686. /* make local bit buffer */
  4687. b = bb;
  4688. k = bk;
  4689. w = wp;
  4690. /* read in table lengths */
  4691. NEEDBITS(5)
  4692. nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
  4693. DUMPBITS(5)
  4694. NEEDBITS(5)
  4695. nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */
  4696. DUMPBITS(5)
  4697. NEEDBITS(4)
  4698. nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */
  4699. DUMPBITS(4)
  4700. #ifdef PKZIP_BUG_WORKAROUND
  4701. if (nl > 288 || nd > 32)
  4702. #else
  4703. if (nl > 286 || nd > 30)
  4704. #endif
  4705. return 1; /* bad lengths */
  4706. /* read in bit-length-code lengths */
  4707. for (j = 0; j < nb; j++)
  4708. {
  4709. NEEDBITS(3)
  4710. ll[border[j]] = (unsigned)b & 7;
  4711. DUMPBITS(3)
  4712. }
  4713. for (; j < 19; j++)
  4714. ll[border[j]] = 0;
  4715. /* build decoding table for trees--single level, 7 bit lookup */
  4716. bl = 7;
  4717. if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
  4718. {
  4719. if (i == 1)
  4720. huft_free(tl);
  4721. return i; /* incomplete code set */
  4722. }
  4723. if (tl == NULL) /* Grrrhhh */
  4724. return 2;
  4725. /* read in literal and distance code lengths */
  4726. n = nl + nd;
  4727. m = mask_bits[bl];
  4728. i = l = 0;
  4729. while ((unsigned)i < n)
  4730. {
  4731. NEEDBITS((unsigned)bl)
  4732. j = (td = tl + ((unsigned)b & m))->b;
  4733. DUMPBITS(j)
  4734. j = td->v.n;
  4735. if (j < 16) /* length of code in bits (0..15) */
  4736. ll[i++] = l = j; /* save last length in l */
  4737. else if (j == 16) /* repeat last length 3 to 6 times */
  4738. {
  4739. NEEDBITS(2)
  4740. j = 3 + ((unsigned)b & 3);
  4741. DUMPBITS(2)
  4742. if ((unsigned)i + j > n)
  4743. return 1;
  4744. while (j--)
  4745. ll[i++] = l;
  4746. }
  4747. else if (j == 17) /* 3 to 10 zero length codes */
  4748. {
  4749. NEEDBITS(3)
  4750. j = 3 + ((unsigned)b & 7);
  4751. DUMPBITS(3)
  4752. if ((unsigned)i + j > n)
  4753. return 1;
  4754. while (j--)
  4755. ll[i++] = 0;
  4756. l = 0;
  4757. }
  4758. else /* j == 18: 11 to 138 zero length codes */
  4759. {
  4760. NEEDBITS(7)
  4761. j = 11 + ((unsigned)b & 0x7f);
  4762. DUMPBITS(7)
  4763. if ((unsigned)i + j > n)
  4764. return 1;
  4765. while (j--)
  4766. ll[i++] = 0;
  4767. l = 0;
  4768. }
  4769. }
  4770. /* free decoding table for trees */
  4771. huft_free(tl);
  4772. /* restore the global bit buffer */
  4773. bb = b;
  4774. bk = k;
  4775. /* build the decoding tables for literal/length and distance codes */
  4776. bl = lbits;
  4777. if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
  4778. {
  4779. if (i == 1) {
  4780. fprintf(stderr, " incomplete literal tree\n");
  4781. huft_free(tl);
  4782. }
  4783. return i; /* incomplete code set */
  4784. }
  4785. bd = dbits;
  4786. if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
  4787. {
  4788. if (i == 1) {
  4789. fprintf(stderr, " incomplete distance tree\n");
  4790. #ifdef PKZIP_BUG_WORKAROUND
  4791. i = 0;
  4792. }
  4793. #else
  4794. huft_free(td);
  4795. }
  4796. huft_free(tl);
  4797. return i; /* incomplete code set */
  4798. #endif
  4799. }
  4800. /* decompress until an end-of-block code */
  4801. if (inflate_codes(tl, td, bl, bd))
  4802. return 1;
  4803. /* free the decoding tables, return */
  4804. huft_free(tl);
  4805. huft_free(td);
  4806. return 0;
  4807. }
  4808. int inflate_block(e)
  4809. int *e; /* last block flag */
  4810. /* decompress an inflated block */
  4811. {
  4812. unsigned t; /* block type */
  4813. unsigned w; /* current window position */
  4814. register ulg b; /* bit buffer */
  4815. register unsigned k; /* number of bits in bit buffer */
  4816. /* make local bit buffer */
  4817. b = bb;
  4818. k = bk;
  4819. w = wp;
  4820. /* read in last block bit */
  4821. NEEDBITS(1)
  4822. *e = (int)b & 1;
  4823. DUMPBITS(1)
  4824. /* read in block type */
  4825. NEEDBITS(2)
  4826. t = (unsigned)b & 3;
  4827. DUMPBITS(2)
  4828. /* restore the global bit buffer */
  4829. bb = b;
  4830. bk = k;
  4831. /* inflate that block type */
  4832. if (t == 2)
  4833. return inflate_dynamic();
  4834. if (t == 0)
  4835. return inflate_stored();
  4836. if (t == 1)
  4837. return inflate_fixed();
  4838. /* bad block type */
  4839. return 2;
  4840. }
  4841. int inflate()
  4842. /* decompress an inflated entry */
  4843. {
  4844. int e; /* last block flag */
  4845. int r; /* result code */
  4846. unsigned h; /* maximum struct huft's malloc'ed */
  4847. /* initialize window, bit buffer */
  4848. wp = 0;
  4849. bk = 0;
  4850. bb = 0;
  4851. /* decompress until the last block */
  4852. h = 0;
  4853. do {
  4854. hufts = 0;
  4855. if ((r = inflate_block(&e)) != 0)
  4856. return r;
  4857. if (hufts > h)
  4858. h = hufts;
  4859. } while (!e);
  4860. /* Undo too much lookahead. The next read will be byte aligned so we
  4861. * can discard unused bits in the last meaningful byte.
  4862. */
  4863. while (bk >= 8) {
  4864. bk -= 8;
  4865. inptr--;
  4866. }
  4867. /* flush out slide */
  4868. flush_output(wp);
  4869. /* return success */
  4870. #ifdef DEBUG
  4871. fprintf(stderr, "<%u> ", h);
  4872. #endif /* DEBUG */
  4873. return 0;
  4874. }
  4875. /* lzw.c -- compress files in LZW format.
  4876. * This is a dummy version avoiding patent problems.
  4877. */
  4878. #ifdef RCSID
  4879. static char rcsid[] = "$Id: lzw.c,v 0.9 1993/06/10 13:27:31 jloup Exp $";
  4880. #endif
  4881. static int msg_done = 0;
  4882. /* Compress in to out with lzw method. */
  4883. int lzw(in, out)
  4884. int in, out;
  4885. {
  4886. if (msg_done) return ERROR;
  4887. msg_done = 1;
  4888. fprintf(stderr,"output in compress .Z format not supported\n");
  4889. if (in != out) { /* avoid warnings on unused variables */
  4890. exit_code = ERROR;
  4891. }
  4892. return ERROR;
  4893. }
  4894. /* trees.c -- output deflated data using Huffman coding
  4895. * Copyright (C) 1992-1993 Jean-loup Gailly
  4896. * This is free software; you can redistribute it and/or modify it under the
  4897. * terms of the GNU General Public License, see the file COPYING.
  4898. */
  4899. /*
  4900. * PURPOSE
  4901. *
  4902. * Encode various sets of source values using variable-length
  4903. * binary code trees.
  4904. *
  4905. * DISCUSSION
  4906. *
  4907. * The PKZIP "deflation" process uses several Huffman trees. The more
  4908. * common source values are represented by shorter bit sequences.
  4909. *
  4910. * Each code tree is stored in the ZIP file in a compressed form
  4911. * which is itself a Huffman encoding of the lengths of
  4912. * all the code strings (in ascending order by source values).
  4913. * The actual code strings are reconstructed from the lengths in
  4914. * the UNZIP process, as described in the "application note"
  4915. * (APPNOTE.TXT) distributed as part of PKWARE's PKZIP program.
  4916. *
  4917. * REFERENCES
  4918. *
  4919. * Lynch, Thomas J.
  4920. * Data Compression: Techniques and Applications, pp. 53-55.
  4921. * Lifetime Learning Publications, 1985. ISBN 0-534-03418-7.
  4922. *
  4923. * Storer, James A.
  4924. * Data Compression: Methods and Theory, pp. 49-50.
  4925. * Computer Science Press, 1988. ISBN 0-7167-8156-5.
  4926. *
  4927. * Sedgewick, R.
  4928. * Algorithms, p290.
  4929. * Addison-Wesley, 1983. ISBN 0-201-06672-6.
  4930. *
  4931. * INTERFACE
  4932. *
  4933. * void ct_init (ush *attr, int *methodp)
  4934. * Allocate the match buffer, initialize the various tables and save
  4935. * the location of the internal file attribute (ascii/binary) and
  4936. * method (DEFLATE/STORE)
  4937. *
  4938. * void ct_tally (int dist, int lc);
  4939. * Save the match info and tally the frequency counts.
  4940. *
  4941. * off_t flush_block (char *buf, ulg stored_len, int pad, int eof)
  4942. * Determine the best encoding for the current block: dynamic trees,
  4943. * static trees or store, and output the encoded block to the zip
  4944. * file. If pad is set, pads the block to the next
  4945. * byte. Returns the total compressed length for the file so
  4946. * far.
  4947. * */
  4948. #ifdef RCSID
  4949. static char rcsid[] = "$Id: trees.c,v 0.12 1993/06/10 13:27:54 jloup Exp $";
  4950. #endif
  4951. /* ===========================================================================
  4952. * Constants
  4953. */
  4954. #define MAX_BITS 15
  4955. /* All codes must not exceed MAX_BITS bits */
  4956. #define MAX_BL_BITS 7
  4957. /* Bit length codes must not exceed MAX_BL_BITS bits */
  4958. #define LENGTH_CODES 29
  4959. /* number of length codes, not counting the special END_BLOCK code */
  4960. #define LITERALS 256
  4961. /* number of literal bytes 0..255 */
  4962. #define END_BLOCK 256
  4963. /* end of block literal code */
  4964. #define L_CODES (LITERALS+1+LENGTH_CODES)
  4965. /* number of Literal or Length codes, including the END_BLOCK code */
  4966. #define D_CODES 30
  4967. /* number of distance codes */
  4968. #define BL_CODES 19
  4969. /* number of codes used to transfer the bit lengths */
  4970. local int near extra_lbits[LENGTH_CODES] /* extra bits for each length code */
  4971. = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
  4972. local int near extra_dbits[D_CODES] /* extra bits for each distance code */
  4973. = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
  4974. local int near extra_blbits[BL_CODES]/* extra bits for each bit length code */
  4975. = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
  4976. #define STORED_BLOCK 0
  4977. #define STATIC_TREES 1
  4978. #define DYN_TREES 2
  4979. /* The three kinds of block type */
  4980. #ifndef LIT_BUFSIZE
  4981. # ifdef SMALL_MEM
  4982. # define LIT_BUFSIZE 0x2000
  4983. # else
  4984. # ifdef MEDIUM_MEM
  4985. # define LIT_BUFSIZE 0x4000
  4986. # else
  4987. # define LIT_BUFSIZE 0x8000
  4988. # endif
  4989. # endif
  4990. #endif
  4991. #ifndef DIST_BUFSIZE
  4992. # define DIST_BUFSIZE LIT_BUFSIZE
  4993. #endif
  4994. /* Sizes of match buffers for literals/lengths and distances. There are
  4995. * 4 reasons for limiting LIT_BUFSIZE to 64K:
  4996. * - frequencies can be kept in 16 bit counters
  4997. * - if compression is not successful for the first block, all input data is
  4998. * still in the window so we can still emit a stored block even when input
  4999. * comes from standard input. (This can also be done for all blocks if
  5000. * LIT_BUFSIZE is not greater than 32K.)
  5001. * - if compression is not successful for a file smaller than 64K, we can
  5002. * even emit a stored file instead of a stored block (saving 5 bytes).
  5003. * - creating new Huffman trees less frequently may not provide fast
  5004. * adaptation to changes in the input data statistics. (Take for
  5005. * example a binary file with poorly compressible code followed by
  5006. * a highly compressible string table.) Smaller buffer sizes give
  5007. * fast adaptation but have of course the overhead of transmitting trees
  5008. * more frequently.
  5009. * - I can't count above 4
  5010. * The current code is general and allows DIST_BUFSIZE < LIT_BUFSIZE (to save
  5011. * memory at the expense of compression). Some optimizations would be possible
  5012. * if we rely on DIST_BUFSIZE == LIT_BUFSIZE.
  5013. */
  5014. #if LIT_BUFSIZE > INBUFSIZ
  5015. error cannot overlay l_buf and inbuf
  5016. #endif
  5017. #define REP_3_6 16
  5018. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  5019. #define REPZ_3_10 17
  5020. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  5021. #define REPZ_11_138 18
  5022. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  5023. /* ===========================================================================
  5024. * Local data
  5025. */
  5026. /* Data structure describing a single value and its code string. */
  5027. typedef struct ct_data {
  5028. union {
  5029. ush freq; /* frequency count */
  5030. ush code; /* bit string */
  5031. } fc;
  5032. union {
  5033. ush dad; /* father node in Huffman tree */
  5034. ush len; /* length of bit string */
  5035. } dl;
  5036. } ct_data;
  5037. #define Freq fc.freq
  5038. #define Code fc.code
  5039. #define Dad dl.dad
  5040. #define Len dl.len
  5041. #define HEAP_SIZE (2*L_CODES+1)
  5042. /* maximum heap size */
  5043. local ct_data near dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  5044. local ct_data near dyn_dtree[2*D_CODES+1]; /* distance tree */
  5045. local ct_data near static_ltree[L_CODES+2];
  5046. /* The static literal tree. Since the bit lengths are imposed, there is no
  5047. * need for the L_CODES extra codes used during heap construction. However
  5048. * The codes 286 and 287 are needed to build a canonical tree (see ct_init
  5049. * below).
  5050. */
  5051. local ct_data near static_dtree[D_CODES];
  5052. /* The static distance tree. (Actually a trivial tree since all codes use
  5053. * 5 bits.)
  5054. */
  5055. local ct_data near bl_tree[2*BL_CODES+1];
  5056. /* Huffman tree for the bit lengths */
  5057. typedef struct tree_desc {
  5058. ct_data near *dyn_tree; /* the dynamic tree */
  5059. ct_data near *static_tree; /* corresponding static tree or NULL */
  5060. int near *extra_bits; /* extra bits for each code or NULL */
  5061. int extra_base; /* base index for extra_bits */
  5062. int elems; /* max number of elements in the tree */
  5063. int max_length; /* max bit length for the codes */
  5064. int max_code; /* largest code with non zero frequency */
  5065. } tree_desc;
  5066. local tree_desc near l_desc =
  5067. {dyn_ltree, static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS, 0};
  5068. local tree_desc near d_desc =
  5069. {dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0};
  5070. local tree_desc near bl_desc =
  5071. {bl_tree, (ct_data near *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS, 0};
  5072. local ush near bl_count[MAX_BITS+1];
  5073. /* number of codes at each bit length for an optimal tree */
  5074. local uch near bl_order[BL_CODES]
  5075. = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
  5076. /* The lengths of the bit length codes are sent in order of decreasing
  5077. * probability, to avoid transmitting the lengths for unused bit length codes.
  5078. */
  5079. local int near heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  5080. local int heap_len; /* number of elements in the heap */
  5081. local int heap_max; /* element of largest frequency */
  5082. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  5083. * The same heap array is used to build all trees.
  5084. */
  5085. local uch near depth[2*L_CODES+1];
  5086. /* Depth of each subtree used as tie breaker for trees of equal frequency */
  5087. local uch length_code[MAX_MATCH-MIN_MATCH+1];
  5088. /* length code for each normalized match length (0 == MIN_MATCH) */
  5089. local uch dist_code[512];
  5090. /* distance codes. The first 256 values correspond to the distances
  5091. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  5092. * the 15 bit distances.
  5093. */
  5094. local int near base_length[LENGTH_CODES];
  5095. /* First normalized length for each code (0 = MIN_MATCH) */
  5096. local int near base_dist[D_CODES];
  5097. /* First normalized distance for each code (0 = distance of 1) */
  5098. #define l_buf inbuf
  5099. /* DECLARE(uch, l_buf, LIT_BUFSIZE); buffer for literals or lengths */
  5100. /* DECLARE(ush, d_buf, DIST_BUFSIZE); buffer for distances */
  5101. local uch near flag_buf[(LIT_BUFSIZE/8)];
  5102. /* flag_buf is a bit array distinguishing literals from lengths in
  5103. * l_buf, thus indicating the presence or absence of a distance.
  5104. */
  5105. local unsigned last_lit; /* running index in l_buf */
  5106. local unsigned last_dist; /* running index in d_buf */
  5107. local unsigned last_flags; /* running index in flag_buf */
  5108. local uch flags; /* current flags not yet saved in flag_buf */
  5109. local uch flag_bit; /* current bit used in flags */
  5110. /* bits are filled in flags starting at bit 0 (least significant).
  5111. * Note: these flags are overkill in the current code since we don't
  5112. * take advantage of DIST_BUFSIZE == LIT_BUFSIZE.
  5113. */
  5114. local ulg opt_len; /* bit length of current block with optimal trees */
  5115. local ulg static_len; /* bit length of current block with static trees */
  5116. local off_t compressed_len; /* total bit length of compressed file */
  5117. local off_t input_len; /* total byte length of input file */
  5118. /* input_len is for debugging only since we can get it by other means. */
  5119. ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */
  5120. int *file_method; /* pointer to DEFLATE or STORE */
  5121. #ifdef DEBUG
  5122. extern off_t bits_sent; /* bit length of the compressed data */
  5123. #endif
  5124. extern long block_start; /* window offset of current block */
  5125. extern unsigned near strstart; /* window offset of current string */
  5126. /* ===========================================================================
  5127. * Local (static) routines in this file.
  5128. */
  5129. local void init_block OF((void));
  5130. local void pqdownheap OF((ct_data near *tree, int k));
  5131. local void gen_bitlen OF((tree_desc near *desc));
  5132. local void gen_codes OF((ct_data near *tree, int max_code));
  5133. local void build_tree_1 OF((tree_desc near *desc));
  5134. local void scan_tree OF((ct_data near *tree, int max_code));
  5135. local void send_tree OF((ct_data near *tree, int max_code));
  5136. local int build_bl_tree OF((void));
  5137. local void send_all_trees OF((int lcodes, int dcodes, int blcodes));
  5138. local void compress_block OF((ct_data near *ltree, ct_data near *dtree));
  5139. local void set_file_type OF((void));
  5140. #ifndef DEBUG
  5141. # define send_code(c, tree) send_bits(tree[c].Code, tree[c].Len)
  5142. /* Send a code of the given tree. c and tree must not have side effects */
  5143. #else /* DEBUG */
  5144. # define send_code(c, tree) \
  5145. { if (verbose>1) fprintf(stderr,"\ncd %3d ",(c)); \
  5146. send_bits(tree[c].Code, tree[c].Len); }
  5147. #endif
  5148. #define d_code(dist) \
  5149. ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
  5150. /* Mapping from a distance to a distance code. dist is the distance - 1 and
  5151. * must not have side effects. dist_code[256] and dist_code[257] are never
  5152. * used.
  5153. */
  5154. #define MAX(a,b) (a >= b ? a : b)
  5155. /* the arguments must not have side effects */
  5156. /* ===========================================================================
  5157. * Allocate the match buffer, initialize the various tables and save the
  5158. * location of the internal file attribute (ascii/binary) and method
  5159. * (DEFLATE/STORE).
  5160. */
  5161. void ct_init(attr, methodp)
  5162. ush *attr; /* pointer to internal file attribute */
  5163. int *methodp; /* pointer to compression method */
  5164. {
  5165. int n; /* iterates over tree elements */
  5166. int bits; /* bit counter */
  5167. int length; /* length value */
  5168. int code; /* code value */
  5169. int dist; /* distance index */
  5170. file_type = attr;
  5171. file_method = methodp;
  5172. compressed_len = input_len = 0L;
  5173. if (static_dtree[0].Len != 0) return; /* ct_init already called */
  5174. /* Initialize the mapping length (0..255) -> length code (0..28) */
  5175. length = 0;
  5176. for (code = 0; code < LENGTH_CODES-1; code++) {
  5177. base_length[code] = length;
  5178. for (n = 0; n < (1<<extra_lbits[code]); n++) {
  5179. length_code[length++] = (uch)code;
  5180. }
  5181. }
  5182. Assert (length == 256, "ct_init: length != 256");
  5183. /* Note that the length 255 (match length 258) can be represented
  5184. * in two different ways: code 284 + 5 bits or code 285, so we
  5185. * overwrite length_code[255] to use the best encoding:
  5186. */
  5187. length_code[length-1] = (uch)code;
  5188. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  5189. dist = 0;
  5190. for (code = 0 ; code < 16; code++) {
  5191. base_dist[code] = dist;
  5192. for (n = 0; n < (1<<extra_dbits[code]); n++) {
  5193. dist_code[dist++] = (uch)code;
  5194. }
  5195. }
  5196. Assert (dist == 256, "ct_init: dist != 256");
  5197. dist >>= 7; /* from now on, all distances are divided by 128 */
  5198. for ( ; code < D_CODES; code++) {
  5199. base_dist[code] = dist << 7;
  5200. for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
  5201. dist_code[256 + dist++] = (uch)code;
  5202. }
  5203. }
  5204. Assert (dist == 256, "ct_init: 256+dist != 512");
  5205. /* Construct the codes of the static literal tree */
  5206. for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
  5207. n = 0;
  5208. while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
  5209. while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
  5210. while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
  5211. while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
  5212. /* Codes 286 and 287 do not exist, but we must include them in the
  5213. * tree construction to get a canonical Huffman tree (longest code
  5214. * all ones)
  5215. */
  5216. gen_codes((ct_data near *)static_ltree, L_CODES+1);
  5217. /* The static distance tree is trivial: */
  5218. for (n = 0; n < D_CODES; n++) {
  5219. static_dtree[n].Len = 5;
  5220. static_dtree[n].Code = bi_reverse(n, 5);
  5221. }
  5222. /* Initialize the first block of the first file: */
  5223. init_block();
  5224. }
  5225. /* ===========================================================================
  5226. * Initialize a new block.
  5227. */
  5228. local void init_block()
  5229. {
  5230. int n; /* iterates over tree elements */
  5231. /* Initialize the trees. */
  5232. for (n = 0; n < L_CODES; n++) dyn_ltree[n].Freq = 0;
  5233. for (n = 0; n < D_CODES; n++) dyn_dtree[n].Freq = 0;
  5234. for (n = 0; n < BL_CODES; n++) bl_tree[n].Freq = 0;
  5235. dyn_ltree[END_BLOCK].Freq = 1;
  5236. opt_len = static_len = 0L;
  5237. last_lit = last_dist = last_flags = 0;
  5238. flags = 0; flag_bit = 1;
  5239. }
  5240. #define SMALLEST 1
  5241. /* Index within the heap array of least frequent node in the Huffman tree */
  5242. /* ===========================================================================
  5243. * Remove the smallest element from the heap and recreate the heap with
  5244. * one less element. Updates heap and heap_len.
  5245. */
  5246. #define pqremove(tree, top) \
  5247. {\
  5248. top = heap[SMALLEST]; \
  5249. heap[SMALLEST] = heap[heap_len--]; \
  5250. pqdownheap(tree, SMALLEST); \
  5251. }
  5252. /* ===========================================================================
  5253. * Compares to subtrees, using the tree depth as tie breaker when
  5254. * the subtrees have equal frequency. This minimizes the worst case length.
  5255. */
  5256. #define smaller(tree, n, m) \
  5257. (tree[n].Freq < tree[m].Freq || \
  5258. (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
  5259. /* ===========================================================================
  5260. * Restore the heap property by moving down the tree starting at node k,
  5261. * exchanging a node with the smallest of its two sons if necessary, stopping
  5262. * when the heap property is re-established (each father smaller than its
  5263. * two sons).
  5264. */
  5265. local void pqdownheap(tree, k)
  5266. ct_data near *tree; /* the tree to restore */
  5267. int k; /* node to move down */
  5268. {
  5269. int v = heap[k];
  5270. int j = k << 1; /* left son of k */
  5271. while (j <= heap_len) {
  5272. /* Set j to the smallest of the two sons: */
  5273. if (j < heap_len && smaller(tree, heap[j+1], heap[j])) j++;
  5274. /* Exit if v is smaller than both sons */
  5275. if (smaller(tree, v, heap[j])) break;
  5276. /* Exchange v with the smallest son */
  5277. heap[k] = heap[j]; k = j;
  5278. /* And continue down the tree, setting j to the left son of k */
  5279. j <<= 1;
  5280. }
  5281. heap[k] = v;
  5282. }
  5283. /* ===========================================================================
  5284. * Compute the optimal bit lengths for a tree and update the total bit length
  5285. * for the current block.
  5286. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  5287. * above are the tree nodes sorted by increasing frequency.
  5288. * OUT assertions: the field len is set to the optimal bit length, the
  5289. * array bl_count contains the frequencies for each bit length.
  5290. * The length opt_len is updated; static_len is also updated if stree is
  5291. * not null.
  5292. */
  5293. local void gen_bitlen(desc)
  5294. tree_desc near *desc; /* the tree descriptor */
  5295. {
  5296. ct_data near *tree = desc->dyn_tree;
  5297. int near *extra = desc->extra_bits;
  5298. int base = desc->extra_base;
  5299. int max_code = desc->max_code;
  5300. int max_length = desc->max_length;
  5301. ct_data near *stree = desc->static_tree;
  5302. int h; /* heap index */
  5303. int n, m; /* iterate over the tree elements */
  5304. int bits; /* bit length */
  5305. int xbits; /* extra bits */
  5306. ush f; /* frequency */
  5307. int overflow = 0; /* number of elements with bit length too large */
  5308. for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
  5309. /* In a first pass, compute the optimal bit lengths (which may
  5310. * overflow in the case of the bit length tree).
  5311. */
  5312. tree[heap[heap_max]].Len = 0; /* root of the heap */
  5313. for (h = heap_max+1; h < HEAP_SIZE; h++) {
  5314. n = heap[h];
  5315. bits = tree[tree[n].Dad].Len + 1;
  5316. if (bits > max_length) bits = max_length, overflow++;
  5317. tree[n].Len = (ush)bits;
  5318. /* We overwrite tree[n].Dad which is no longer needed */
  5319. if (n > max_code) continue; /* not a leaf node */
  5320. bl_count[bits]++;
  5321. xbits = 0;
  5322. if (n >= base) xbits = extra[n-base];
  5323. f = tree[n].Freq;
  5324. opt_len += (ulg)f * (bits + xbits);
  5325. if (stree) static_len += (ulg)f * (stree[n].Len + xbits);
  5326. }
  5327. if (overflow == 0) return;
  5328. Trace((stderr,"\nbit length overflow\n"));
  5329. /* This happens for example on obj2 and pic of the Calgary corpus */
  5330. /* Find the first bit length which could increase: */
  5331. do {
  5332. bits = max_length-1;
  5333. while (bl_count[bits] == 0) bits--;
  5334. bl_count[bits]--; /* move one leaf down the tree */
  5335. bl_count[bits+1] += 2; /* move one overflow item as its brother */
  5336. bl_count[max_length]--;
  5337. /* The brother of the overflow item also moves one step up,
  5338. * but this does not affect bl_count[max_length]
  5339. */
  5340. overflow -= 2;
  5341. } while (overflow > 0);
  5342. /* Now recompute all bit lengths, scanning in increasing frequency.
  5343. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  5344. * lengths instead of fixing only the wrong ones. This idea is taken
  5345. * from 'ar' written by Haruhiko Okumura.)
  5346. */
  5347. for (bits = max_length; bits != 0; bits--) {
  5348. n = bl_count[bits];
  5349. while (n != 0) {
  5350. m = heap[--h];
  5351. if (m > max_code) continue;
  5352. if (tree[m].Len != (unsigned) bits) {
  5353. Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  5354. opt_len += ((long)bits-(long)tree[m].Len)*(long)tree[m].Freq;
  5355. tree[m].Len = (ush)bits;
  5356. }
  5357. n--;
  5358. }
  5359. }
  5360. }
  5361. /* ===========================================================================
  5362. * Generate the codes for a given tree and bit counts (which need not be
  5363. * optimal).
  5364. * IN assertion: the array bl_count contains the bit length statistics for
  5365. * the given tree and the field len is set for all tree elements.
  5366. * OUT assertion: the field code is set for all tree elements of non
  5367. * zero code length.
  5368. */
  5369. local void gen_codes (tree, max_code)
  5370. ct_data near *tree; /* the tree to decorate */
  5371. int max_code; /* largest code with non zero frequency */
  5372. {
  5373. ush next_code[MAX_BITS+1]; /* next code value for each bit length */
  5374. ush code = 0; /* running code value */
  5375. int bits; /* bit index */
  5376. int n; /* code index */
  5377. /* The distribution counts are first used to generate the code values
  5378. * without bit reversal.
  5379. */
  5380. for (bits = 1; bits <= MAX_BITS; bits++) {
  5381. next_code[bits] = code = (code + bl_count[bits-1]) << 1;
  5382. }
  5383. /* Check that the bit counts in bl_count are consistent. The last code
  5384. * must be all ones.
  5385. */
  5386. Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  5387. "inconsistent bit counts");
  5388. Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  5389. for (n = 0; n <= max_code; n++) {
  5390. int len = tree[n].Len;
  5391. if (len == 0) continue;
  5392. /* Now reverse the bits */
  5393. tree[n].Code = bi_reverse(next_code[len]++, len);
  5394. Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  5395. n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  5396. }
  5397. }
  5398. /* ===========================================================================
  5399. * Construct one Huffman tree and assigns the code bit strings and lengths.
  5400. * Update the total bit length for the current block.
  5401. * IN assertion: the field freq is set for all tree elements.
  5402. * OUT assertions: the fields len and code are set to the optimal bit length
  5403. * and corresponding code. The length opt_len is updated; static_len is
  5404. * also updated if stree is not null. The field max_code is set.
  5405. */
  5406. local void build_tree_1(desc)
  5407. tree_desc near *desc; /* the tree descriptor */
  5408. {
  5409. ct_data near *tree = desc->dyn_tree;
  5410. ct_data near *stree = desc->static_tree;
  5411. int elems = desc->elems;
  5412. int n, m; /* iterate over heap elements */
  5413. int max_code = -1; /* largest code with non zero frequency */
  5414. int node = elems; /* next internal node of the tree */
  5415. /* Construct the initial heap, with least frequent element in
  5416. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  5417. * heap[0] is not used.
  5418. */
  5419. heap_len = 0, heap_max = HEAP_SIZE;
  5420. for (n = 0; n < elems; n++) {
  5421. if (tree[n].Freq != 0) {
  5422. heap[++heap_len] = max_code = n;
  5423. depth[n] = 0;
  5424. } else {
  5425. tree[n].Len = 0;
  5426. }
  5427. }
  5428. /* The pkzip format requires that at least one distance code exists,
  5429. * and that at least one bit should be sent even if there is only one
  5430. * possible code. So to avoid special checks later on we force at least
  5431. * two codes of non zero frequency.
  5432. */
  5433. while (heap_len < 2) {
  5434. int new = heap[++heap_len] = (max_code < 2 ? ++max_code : 0);
  5435. tree[new].Freq = 1;
  5436. depth[new] = 0;
  5437. opt_len--; if (stree) static_len -= stree[new].Len;
  5438. /* new is 0 or 1 so it does not have extra bits */
  5439. }
  5440. desc->max_code = max_code;
  5441. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  5442. * establish sub-heaps of increasing lengths:
  5443. */
  5444. for (n = heap_len/2; n >= 1; n--) pqdownheap(tree, n);
  5445. /* Construct the Huffman tree by repeatedly combining the least two
  5446. * frequent nodes.
  5447. */
  5448. do {
  5449. pqremove(tree, n); /* n = node of least frequency */
  5450. m = heap[SMALLEST]; /* m = node of next least frequency */
  5451. heap[--heap_max] = n; /* keep the nodes sorted by frequency */
  5452. heap[--heap_max] = m;
  5453. /* Create a new node father of n and m */
  5454. tree[node].Freq = tree[n].Freq + tree[m].Freq;
  5455. depth[node] = (uch) (MAX(depth[n], depth[m]) + 1);
  5456. tree[n].Dad = tree[m].Dad = (ush)node;
  5457. #ifdef DUMP_BL_TREE
  5458. if (tree == bl_tree) {
  5459. fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
  5460. node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
  5461. }
  5462. #endif
  5463. /* and insert the new node in the heap */
  5464. heap[SMALLEST] = node++;
  5465. pqdownheap(tree, SMALLEST);
  5466. } while (heap_len >= 2);
  5467. heap[--heap_max] = heap[SMALLEST];
  5468. /* At this point, the fields freq and dad are set. We can now
  5469. * generate the bit lengths.
  5470. */
  5471. gen_bitlen((tree_desc near *)desc);
  5472. /* The field len is now set, we can generate the bit codes */
  5473. gen_codes ((ct_data near *)tree, max_code);
  5474. }
  5475. /* ===========================================================================
  5476. * Scan a literal or distance tree to determine the frequencies of the codes
  5477. * in the bit length tree. Updates opt_len to take into account the repeat
  5478. * counts. (The contribution of the bit length codes will be added later
  5479. * during the construction of bl_tree.)
  5480. */
  5481. local void scan_tree (tree, max_code)
  5482. ct_data near *tree; /* the tree to be scanned */
  5483. int max_code; /* and its largest code of non zero frequency */
  5484. {
  5485. int n; /* iterates over all tree elements */
  5486. int prevlen = -1; /* last emitted length */
  5487. int curlen; /* length of current code */
  5488. int nextlen = tree[0].Len; /* length of next code */
  5489. int count = 0; /* repeat count of the current code */
  5490. int max_count = 7; /* max repeat count */
  5491. int min_count = 4; /* min repeat count */
  5492. if (nextlen == 0) max_count = 138, min_count = 3;
  5493. tree[max_code+1].Len = (ush)0xffff; /* guard */
  5494. for (n = 0; n <= max_code; n++) {
  5495. curlen = nextlen; nextlen = tree[n+1].Len;
  5496. if (++count < max_count && curlen == nextlen) {
  5497. continue;
  5498. } else if (count < min_count) {
  5499. bl_tree[curlen].Freq += count;
  5500. } else if (curlen != 0) {
  5501. if (curlen != prevlen) bl_tree[curlen].Freq++;
  5502. bl_tree[REP_3_6].Freq++;
  5503. } else if (count <= 10) {
  5504. bl_tree[REPZ_3_10].Freq++;
  5505. } else {
  5506. bl_tree[REPZ_11_138].Freq++;
  5507. }
  5508. count = 0; prevlen = curlen;
  5509. if (nextlen == 0) {
  5510. max_count = 138, min_count = 3;
  5511. } else if (curlen == nextlen) {
  5512. max_count = 6, min_count = 3;
  5513. } else {
  5514. max_count = 7, min_count = 4;
  5515. }
  5516. }
  5517. }
  5518. /* ===========================================================================
  5519. * Send a literal or distance tree in compressed form, using the codes in
  5520. * bl_tree.
  5521. */
  5522. local void send_tree (tree, max_code)
  5523. ct_data near *tree; /* the tree to be scanned */
  5524. int max_code; /* and its largest code of non zero frequency */
  5525. {
  5526. int n; /* iterates over all tree elements */
  5527. int prevlen = -1; /* last emitted length */
  5528. int curlen; /* length of current code */
  5529. int nextlen = tree[0].Len; /* length of next code */
  5530. int count = 0; /* repeat count of the current code */
  5531. int max_count = 7; /* max repeat count */
  5532. int min_count = 4; /* min repeat count */
  5533. /* tree[max_code+1].Len = -1; */ /* guard already set */
  5534. if (nextlen == 0) max_count = 138, min_count = 3;
  5535. for (n = 0; n <= max_code; n++) {
  5536. curlen = nextlen; nextlen = tree[n+1].Len;
  5537. if (++count < max_count && curlen == nextlen) {
  5538. continue;
  5539. } else if (count < min_count) {
  5540. do { send_code(curlen, bl_tree); } while (--count != 0);
  5541. } else if (curlen != 0) {
  5542. if (curlen != prevlen) {
  5543. send_code(curlen, bl_tree); count--;
  5544. }
  5545. Assert(count >= 3 && count <= 6, " 3_6?");
  5546. send_code(REP_3_6, bl_tree); send_bits(count-3, 2);
  5547. } else if (count <= 10) {
  5548. send_code(REPZ_3_10, bl_tree); send_bits(count-3, 3);
  5549. } else {
  5550. send_code(REPZ_11_138, bl_tree); send_bits(count-11, 7);
  5551. }
  5552. count = 0; prevlen = curlen;
  5553. if (nextlen == 0) {
  5554. max_count = 138, min_count = 3;
  5555. } else if (curlen == nextlen) {
  5556. max_count = 6, min_count = 3;
  5557. } else {
  5558. max_count = 7, min_count = 4;
  5559. }
  5560. }
  5561. }
  5562. /* ===========================================================================
  5563. * Construct the Huffman tree for the bit lengths and return the index in
  5564. * bl_order of the last bit length code to send.
  5565. */
  5566. local int build_bl_tree()
  5567. {
  5568. int max_blindex; /* index of last bit length code of non zero freq */
  5569. /* Determine the bit length frequencies for literal and distance trees */
  5570. scan_tree((ct_data near *)dyn_ltree, l_desc.max_code);
  5571. scan_tree((ct_data near *)dyn_dtree, d_desc.max_code);
  5572. /* Build the bit length tree: */
  5573. build_tree_1((tree_desc near *)(&bl_desc));
  5574. /* opt_len now includes the length of the tree representations, except
  5575. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  5576. */
  5577. /* Determine the number of bit length codes to send. The pkzip format
  5578. * requires that at least 4 bit length codes be sent. (appnote.txt says
  5579. * 3 but the actual value used is 4.)
  5580. */
  5581. for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  5582. if (bl_tree[bl_order[max_blindex]].Len != 0) break;
  5583. }
  5584. /* Update opt_len to include the bit length tree and counts */
  5585. opt_len += 3*(max_blindex+1) + 5+5+4;
  5586. Tracev((stderr, "\ndyn trees: dyn %lu, stat %lu", opt_len, static_len));
  5587. return max_blindex;
  5588. }
  5589. /* ===========================================================================
  5590. * Send the header for a block using dynamic Huffman trees: the counts, the
  5591. * lengths of the bit length codes, the literal tree and the distance tree.
  5592. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  5593. */
  5594. local void send_all_trees(lcodes, dcodes, blcodes)
  5595. int lcodes, dcodes, blcodes; /* number of codes for each tree */
  5596. {
  5597. int rank; /* index in bl_order */
  5598. Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  5599. Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  5600. "too many codes");
  5601. Tracev((stderr, "\nbl counts: "));
  5602. send_bits(lcodes-257, 5); /* not +255 as stated in appnote.txt */
  5603. send_bits(dcodes-1, 5);
  5604. send_bits(blcodes-4, 4); /* not -3 as stated in appnote.txt */
  5605. for (rank = 0; rank < blcodes; rank++) {
  5606. Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  5607. send_bits(bl_tree[bl_order[rank]].Len, 3);
  5608. }
  5609. send_tree((ct_data near *)dyn_ltree, lcodes-1); /* send the literal tree */
  5610. send_tree((ct_data near *)dyn_dtree, dcodes-1); /* send the distance tree */
  5611. }
  5612. /* ===========================================================================
  5613. * Determine the best encoding for the current block: dynamic trees, static
  5614. * trees or store, and output the encoded block to the zip file. This function
  5615. * returns the total compressed length for the file so far.
  5616. */
  5617. off_t flush_block(buf, stored_len, pad, eof)
  5618. char *buf; /* input block, or NULL if too old */
  5619. ulg stored_len; /* length of input block */
  5620. int pad; /* pad output to byte boundary */
  5621. int eof; /* true if this is the last block for a file */
  5622. {
  5623. ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  5624. int max_blindex; /* index of last bit length code of non zero freq */
  5625. flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */
  5626. /* Check if the file is ascii or binary */
  5627. if (*file_type == (ush)UNKNOWN) set_file_type();
  5628. /* Construct the literal and distance trees */
  5629. build_tree_1((tree_desc near *)(&l_desc));
  5630. Tracev((stderr, "\nlit data: dyn %lu, stat %lu", opt_len, static_len));
  5631. build_tree_1((tree_desc near *)(&d_desc));
  5632. Tracev((stderr, "\ndist data: dyn %lu, stat %lu", opt_len, static_len));
  5633. /* At this point, opt_len and static_len are the total bit lengths of
  5634. * the compressed block data, excluding the tree representations.
  5635. */
  5636. /* Build the bit length tree for the above two trees, and get the index
  5637. * in bl_order of the last bit length code to send.
  5638. */
  5639. max_blindex = build_bl_tree();
  5640. /* Determine the best encoding. Compute first the block length in bytes */
  5641. opt_lenb = (opt_len+3+7)>>3;
  5642. static_lenb = (static_len+3+7)>>3;
  5643. input_len += stored_len; /* for debugging only */
  5644. Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
  5645. opt_lenb, opt_len, static_lenb, static_len, stored_len,
  5646. last_lit, last_dist));
  5647. if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
  5648. /* If compression failed and this is the first and last block,
  5649. * and if the zip file can be seeked (to rewrite the local header),
  5650. * the whole file is transformed into a stored file:
  5651. */
  5652. #ifdef FORCE_METHOD
  5653. if (level == 1 && eof && compressed_len == 0L) { /* force stored file */
  5654. #else
  5655. if (stored_len <= opt_lenb && eof && compressed_len == 0L && seekable()) {
  5656. #endif
  5657. /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
  5658. if (buf == (char*)0) error ("block vanished");
  5659. copy_block(buf, (unsigned)stored_len, 0); /* without header */
  5660. compressed_len = stored_len << 3;
  5661. *file_method = STORED;
  5662. #ifdef FORCE_METHOD
  5663. } else if (level == 2 && buf != (char*)0) { /* force stored block */
  5664. #else
  5665. } else if (stored_len+4 <= opt_lenb && buf != (char*)0) {
  5666. /* 4: two words for the lengths */
  5667. #endif
  5668. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  5669. * Otherwise we can't have processed more than WSIZE input bytes since
  5670. * the last block flush, because compression would have been
  5671. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  5672. * transform a block into a stored block.
  5673. */
  5674. send_bits((STORED_BLOCK<<1)+eof, 3); /* send block type */
  5675. compressed_len = (compressed_len + 3 + 7) & ~7L;
  5676. compressed_len += (stored_len + 4) << 3;
  5677. copy_block(buf, (unsigned)stored_len, 1); /* with header */
  5678. #ifdef FORCE_METHOD
  5679. } else if (level == 3) { /* force static trees */
  5680. #else
  5681. } else if (static_lenb == opt_lenb) {
  5682. #endif
  5683. send_bits((STATIC_TREES<<1)+eof, 3);
  5684. compress_block((ct_data near *)static_ltree, (ct_data near *)static_dtree);
  5685. compressed_len += 3 + static_len;
  5686. } else {
  5687. send_bits((DYN_TREES<<1)+eof, 3);
  5688. send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1);
  5689. compress_block((ct_data near *)dyn_ltree, (ct_data near *)dyn_dtree);
  5690. compressed_len += 3 + opt_len;
  5691. }
  5692. Assert (compressed_len == bits_sent, "bad compressed size");
  5693. init_block();
  5694. if (eof) {
  5695. Assert (input_len == bytes_in, "bad input size");
  5696. bi_windup();
  5697. compressed_len += 7; /* align on byte boundary */
  5698. } else if (pad && (compressed_len % 8) != 0) {
  5699. send_bits((STORED_BLOCK<<1)+eof, 3); /* send block type */
  5700. compressed_len = (compressed_len + 3 + 7) & ~7L;
  5701. copy_block(buf, 0, 1); /* with header */
  5702. }
  5703. return compressed_len >> 3;
  5704. }
  5705. /* ===========================================================================
  5706. * Save the match info and tally the frequency counts. Return true if
  5707. * the current block must be flushed.
  5708. */
  5709. int ct_tally (dist, lc)
  5710. int dist; /* distance of matched string */
  5711. int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  5712. {
  5713. l_buf[last_lit++] = (uch)lc;
  5714. if (dist == 0) {
  5715. /* lc is the unmatched char */
  5716. dyn_ltree[lc].Freq++;
  5717. } else {
  5718. /* Here, lc is the match length - MIN_MATCH */
  5719. dist--; /* dist = match distance - 1 */
  5720. Assert((ush)dist < (ush)MAX_DIST &&
  5721. (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  5722. (ush)d_code(dist) < (ush)D_CODES, "ct_tally: bad match");
  5723. dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
  5724. dyn_dtree[d_code(dist)].Freq++;
  5725. d_buf[last_dist++] = (ush)dist;
  5726. flags |= flag_bit;
  5727. }
  5728. flag_bit <<= 1;
  5729. /* Output the flags if they fill a byte: */
  5730. if ((last_lit & 7) == 0) {
  5731. flag_buf[last_flags++] = flags;
  5732. flags = 0, flag_bit = 1;
  5733. }
  5734. /* Try to guess if it is profitable to stop the current block here */
  5735. if (level > 2 && (last_lit & 0xfff) == 0) {
  5736. /* Compute an upper bound for the compressed length */
  5737. ulg out_length = (ulg)last_lit*8L;
  5738. ulg in_length = (ulg)strstart-block_start;
  5739. int dcode;
  5740. for (dcode = 0; dcode < D_CODES; dcode++) {
  5741. out_length += (ulg)dyn_dtree[dcode].Freq*(5L+extra_dbits[dcode]);
  5742. }
  5743. out_length >>= 3;
  5744. Trace((stderr,"\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
  5745. last_lit, last_dist, in_length, out_length,
  5746. 100L - out_length*100L/in_length));
  5747. if (last_dist < last_lit/2 && out_length < in_length/2) return 1;
  5748. }
  5749. return (last_lit == LIT_BUFSIZE-1 || last_dist == DIST_BUFSIZE);
  5750. /* We avoid equality with LIT_BUFSIZE because of wraparound at 64K
  5751. * on 16 bit machines and because stored blocks are restricted to
  5752. * 64K-1 bytes.
  5753. */
  5754. }
  5755. /* ===========================================================================
  5756. * Send the block data compressed using the given Huffman trees
  5757. */
  5758. local void compress_block(ltree, dtree)
  5759. ct_data near *ltree; /* literal tree */
  5760. ct_data near *dtree; /* distance tree */
  5761. {
  5762. unsigned dist; /* distance of matched string */
  5763. int lc; /* match length or unmatched char (if dist == 0) */
  5764. unsigned lx = 0; /* running index in l_buf */
  5765. unsigned dx = 0; /* running index in d_buf */
  5766. unsigned fx = 0; /* running index in flag_buf */
  5767. uch flag = 0; /* current flags */
  5768. unsigned code; /* the code to send */
  5769. int extra; /* number of extra bits to send */
  5770. if (last_lit != 0) do {
  5771. if ((lx & 7) == 0) flag = flag_buf[fx++];
  5772. lc = l_buf[lx++];
  5773. if ((flag & 1) == 0) {
  5774. send_code(lc, ltree); /* send a literal byte */
  5775. Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  5776. } else {
  5777. /* Here, lc is the match length - MIN_MATCH */
  5778. code = length_code[lc];
  5779. send_code(code+LITERALS+1, ltree); /* send the length code */
  5780. extra = extra_lbits[code];
  5781. if (extra != 0) {
  5782. lc -= base_length[code];
  5783. send_bits(lc, extra); /* send the extra length bits */
  5784. }
  5785. dist = d_buf[dx++];
  5786. /* Here, dist is the match distance - 1 */
  5787. code = d_code(dist);
  5788. Assert (code < D_CODES, "bad d_code");
  5789. send_code(code, dtree); /* send the distance code */
  5790. extra = extra_dbits[code];
  5791. if (extra != 0) {
  5792. dist -= base_dist[code];
  5793. send_bits(dist, extra); /* send the extra distance bits */
  5794. }
  5795. } /* literal or match pair ? */
  5796. flag >>= 1;
  5797. } while (lx < last_lit);
  5798. send_code(END_BLOCK, ltree);
  5799. }
  5800. /* ===========================================================================
  5801. * Set the file type to ASCII or BINARY, using a crude approximation:
  5802. * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  5803. * IN assertion: the fields freq of dyn_ltree are set and the total of all
  5804. * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
  5805. */
  5806. local void set_file_type()
  5807. {
  5808. int n = 0;
  5809. unsigned ascii_freq = 0;
  5810. unsigned bin_freq = 0;
  5811. while (n < 7) bin_freq += dyn_ltree[n++].Freq;
  5812. while (n < 128) ascii_freq += dyn_ltree[n++].Freq;
  5813. while (n < LITERALS) bin_freq += dyn_ltree[n++].Freq;
  5814. *file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII;
  5815. if (*file_type == BINARY && translate_eol) {
  5816. warning ("-l used on binary file");
  5817. }
  5818. }
  5819. /* unlzh.c -- decompress files in SCO compress -H (LZH) format.
  5820. * The code in this file is directly derived from the public domain 'ar002'
  5821. * written by Haruhiko Okumura.
  5822. */
  5823. #ifdef RCSID
  5824. static char rcsid[] = "$Id: unlzh.c,v 1.2 1993/06/24 10:59:01 jloup Exp $";
  5825. #endif
  5826. /* decode.c */
  5827. local unsigned decode OF((unsigned count, uch buffer[]));
  5828. local void decode_start OF((void));
  5829. /* huf.c */
  5830. local void huf_decode_start OF((void));
  5831. local unsigned decode_c OF((void));
  5832. local unsigned decode_p OF((void));
  5833. local void read_pt_len OF((int nn, int nbit, int i_special));
  5834. local void read_c_len OF((void));
  5835. /* io.c */
  5836. local void fillbuf OF((int n));
  5837. local unsigned getbits OF((int n));
  5838. local void init_getbits OF((void));
  5839. /* maketbl.c */
  5840. local void make_table OF((int nchar, uch bitlen[],
  5841. int tablebits, ush table[]));
  5842. #define DICBIT 13 /* 12(-lh4-) or 13(-lh5-) */
  5843. #define DICSIZ ((unsigned) 1 << DICBIT)
  5844. #ifndef CHAR_BIT
  5845. # define CHAR_BIT 8
  5846. #endif
  5847. #ifndef UCHAR_MAX
  5848. # define UCHAR_MAX 255
  5849. #endif
  5850. #define BITBUFSIZ (CHAR_BIT * 2 * sizeof(char))
  5851. /* Do not use CHAR_BIT * sizeof(bitbuf), does not work on machines
  5852. * for which short is not on 16 bits (Cray).
  5853. */
  5854. /* encode.c and decode.c */
  5855. #define MAXMATCH 256 /* formerly F (not more than UCHAR_MAX + 1) */
  5856. #define THRESHOLD 3 /* choose optimal value */
  5857. /* huf.c */
  5858. #define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
  5859. /* alphabet = {0, 1, 2, ..., NC - 1} */
  5860. #define CBIT 9 /* $\lfloor \log_2 NC \rfloor + 1$ */
  5861. #define CODE_BIT 16 /* codeword length */
  5862. #define NP (DICBIT + 1)
  5863. #define NT (CODE_BIT + 3)
  5864. #define PBIT 4 /* smallest integer such that (1U << PBIT) > NP */
  5865. #define TBIT 5 /* smallest integer such that (1U << TBIT) > NT */
  5866. #if NT > NP
  5867. # define NPT NT
  5868. #else
  5869. # define NPT NP
  5870. #endif
  5871. /* local ush left[2 * NC - 1]; */
  5872. /* local ush right[2 * NC - 1]; */
  5873. #define left prev
  5874. #define right head
  5875. #if NC > (1<<(BITS-2))
  5876. error cannot overlay left+right and prev
  5877. #endif
  5878. /* local uch c_len[NC]; */
  5879. #define c_len outbuf
  5880. #if NC > OUTBUFSIZ
  5881. error cannot overlay c_len and outbuf
  5882. #endif
  5883. local uch pt_len[NPT];
  5884. local unsigned blocksize;
  5885. local ush pt_table[256];
  5886. /* local ush c_table[4096]; */
  5887. #define c_table d_buf
  5888. #if (DIST_BUFSIZE-1) < 4095
  5889. error cannot overlay c_table and d_buf
  5890. #endif
  5891. /***********************************************************
  5892. io.c -- input/output
  5893. ***********************************************************/
  5894. local ush io_bitbuf;
  5895. local unsigned subbitbuf;
  5896. local int bitcount;
  5897. local void fillbuf(n) /* Shift io_bitbuf n bits left, read n bits */
  5898. int n;
  5899. {
  5900. io_bitbuf <<= n;
  5901. while (n > bitcount) {
  5902. io_bitbuf |= subbitbuf << (n -= bitcount);
  5903. subbitbuf = (unsigned)try_byte();
  5904. if ((int)subbitbuf == EOF) subbitbuf = 0;
  5905. bitcount = CHAR_BIT;
  5906. }
  5907. io_bitbuf |= subbitbuf >> (bitcount -= n);
  5908. }
  5909. local unsigned getbits(n)
  5910. int n;
  5911. {
  5912. unsigned x;
  5913. x = io_bitbuf >> (BITBUFSIZ - n); fillbuf(n);
  5914. return x;
  5915. }
  5916. local void init_getbits()
  5917. {
  5918. io_bitbuf = 0; subbitbuf = 0; bitcount = 0;
  5919. fillbuf(BITBUFSIZ);
  5920. }
  5921. /***********************************************************
  5922. maketbl.c -- make table for decoding
  5923. ***********************************************************/
  5924. local void make_table(nchar, bitlen, tablebits, table)
  5925. int nchar;
  5926. uch bitlen[];
  5927. int tablebits;
  5928. ush table[];
  5929. {
  5930. ush count[17], weight[17], start[18], *p;
  5931. unsigned i, k, len, ch, jutbits, avail, nextcode, mask;
  5932. for (i = 1; i <= 16; i++) count[i] = 0;
  5933. for (i = 0; i < (unsigned)nchar; i++) count[bitlen[i]]++;
  5934. start[1] = 0;
  5935. for (i = 1; i <= 16; i++)
  5936. start[i + 1] = start[i] + (count[i] << (16 - i));
  5937. if ((start[17] & 0xffff) != 0)
  5938. error("Bad table\n");
  5939. jutbits = 16 - tablebits;
  5940. for (i = 1; i <= (unsigned)tablebits; i++) {
  5941. start[i] >>= jutbits;
  5942. weight[i] = (unsigned) 1 << (tablebits - i);
  5943. }
  5944. while (i <= 16) {
  5945. weight[i] = (unsigned) 1 << (16 - i);
  5946. i++;
  5947. }
  5948. i = start[tablebits + 1] >> jutbits;
  5949. if (i != 0) {
  5950. k = 1 << tablebits;
  5951. while (i != k) table[i++] = 0;
  5952. }
  5953. avail = nchar;
  5954. mask = (unsigned) 1 << (15 - tablebits);
  5955. for (ch = 0; ch < (unsigned)nchar; ch++) {
  5956. if ((len = bitlen[ch]) == 0) continue;
  5957. nextcode = start[len] + weight[len];
  5958. if (len <= (unsigned)tablebits) {
  5959. for (i = start[len]; i < nextcode; i++) table[i] = ch;
  5960. } else {
  5961. k = start[len];
  5962. p = &table[k >> jutbits];
  5963. i = len - tablebits;
  5964. while (i != 0) {
  5965. if (*p == 0) {
  5966. right[avail] = left[avail] = 0;
  5967. *p = avail++;
  5968. }
  5969. if (k & mask) p = &right[*p];
  5970. else p = &left[*p];
  5971. k <<= 1; i--;
  5972. }
  5973. *p = ch;
  5974. }
  5975. start[len] = nextcode;
  5976. }
  5977. }
  5978. /***********************************************************
  5979. huf.c -- static Huffman
  5980. ***********************************************************/
  5981. local void read_pt_len(nn, nbit, i_special)
  5982. int nn;
  5983. int nbit;
  5984. int i_special;
  5985. {
  5986. int i, c, n;
  5987. unsigned mask;
  5988. n = getbits(nbit);
  5989. if (n == 0) {
  5990. c = getbits(nbit);
  5991. for (i = 0; i < nn; i++) pt_len[i] = 0;
  5992. for (i = 0; i < 256; i++) pt_table[i] = c;
  5993. } else {
  5994. i = 0;
  5995. while (i < n) {
  5996. c = io_bitbuf >> (BITBUFSIZ - 3);
  5997. if (c == 7) {
  5998. mask = (unsigned) 1 << (BITBUFSIZ - 1 - 3);
  5999. while (mask & io_bitbuf) { mask >>= 1; c++; }
  6000. }
  6001. fillbuf((c < 7) ? 3 : c - 3);
  6002. pt_len[i++] = c;
  6003. if (i == i_special) {
  6004. c = getbits(2);
  6005. while (--c >= 0) pt_len[i++] = 0;
  6006. }
  6007. }
  6008. while (i < nn) pt_len[i++] = 0;
  6009. make_table(nn, pt_len, 8, pt_table);
  6010. }
  6011. }
  6012. local void read_c_len()
  6013. {
  6014. int i, c, n;
  6015. unsigned mask;
  6016. n = getbits(CBIT);
  6017. if (n == 0) {
  6018. c = getbits(CBIT);
  6019. for (i = 0; i < NC; i++) c_len[i] = 0;
  6020. for (i = 0; i < 4096; i++) c_table[i] = c;
  6021. } else {
  6022. i = 0;
  6023. while (i < n) {
  6024. c = pt_table[io_bitbuf >> (BITBUFSIZ - 8)];
  6025. if (c >= NT) {
  6026. mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
  6027. do {
  6028. if (io_bitbuf & mask) c = right[c];
  6029. else c = left [c];
  6030. mask >>= 1;
  6031. } while (c >= NT);
  6032. }
  6033. fillbuf((int) pt_len[c]);
  6034. if (c <= 2) {
  6035. if (c == 0) c = 1;
  6036. else if (c == 1) c = getbits(4) + 3;
  6037. else c = getbits(CBIT) + 20;
  6038. while (--c >= 0) c_len[i++] = 0;
  6039. } else c_len[i++] = c - 2;
  6040. }
  6041. while (i < NC) c_len[i++] = 0;
  6042. make_table(NC, c_len, 12, c_table);
  6043. }
  6044. }
  6045. local unsigned decode_c()
  6046. {
  6047. unsigned j, mask;
  6048. if (blocksize == 0) {
  6049. blocksize = getbits(16);
  6050. if (blocksize == 0) {
  6051. return NC; /* end of file */
  6052. }
  6053. read_pt_len(NT, TBIT, 3);
  6054. read_c_len();
  6055. read_pt_len(NP, PBIT, -1);
  6056. }
  6057. blocksize--;
  6058. j = c_table[io_bitbuf >> (BITBUFSIZ - 12)];
  6059. if (j >= NC) {
  6060. mask = (unsigned) 1 << (BITBUFSIZ - 1 - 12);
  6061. do {
  6062. if (io_bitbuf & mask) j = right[j];
  6063. else j = left [j];
  6064. mask >>= 1;
  6065. } while (j >= NC);
  6066. }
  6067. fillbuf((int) c_len[j]);
  6068. return j;
  6069. }
  6070. local unsigned decode_p()
  6071. {
  6072. unsigned j, mask;
  6073. j = pt_table[io_bitbuf >> (BITBUFSIZ - 8)];
  6074. if (j >= NP) {
  6075. mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
  6076. do {
  6077. if (io_bitbuf & mask) j = right[j];
  6078. else j = left [j];
  6079. mask >>= 1;
  6080. } while (j >= NP);
  6081. }
  6082. fillbuf((int) pt_len[j]);
  6083. if (j != 0) j = ((unsigned) 1 << (j - 1)) + getbits((int) (j - 1));
  6084. return j;
  6085. }
  6086. local void huf_decode_start()
  6087. {
  6088. init_getbits(); blocksize = 0;
  6089. }
  6090. /***********************************************************
  6091. decode.c
  6092. ***********************************************************/
  6093. local int j; /* remaining bytes to copy */
  6094. local int done; /* set at end of input */
  6095. local void decode_start()
  6096. {
  6097. huf_decode_start();
  6098. j = 0;
  6099. done = 0;
  6100. }
  6101. /* Decode the input and return the number of decoded bytes put in buffer
  6102. */
  6103. local unsigned decode(count, buffer)
  6104. unsigned count;
  6105. uch buffer[];
  6106. /* The calling function must keep the number of
  6107. bytes to be processed. This function decodes
  6108. either 'count' bytes or 'DICSIZ' bytes, whichever
  6109. is smaller, into the array 'buffer[]' of size
  6110. 'DICSIZ' or more.
  6111. Call decode_start() once for each new file
  6112. before calling this function.
  6113. */
  6114. {
  6115. local unsigned i;
  6116. unsigned r, c;
  6117. r = 0;
  6118. while (--j >= 0) {
  6119. buffer[r] = buffer[i];
  6120. i = (i + 1) & (DICSIZ - 1);
  6121. if (++r == count) return r;
  6122. }
  6123. for ( ; ; ) {
  6124. c = decode_c();
  6125. if (c == NC) {
  6126. done = 1;
  6127. return r;
  6128. }
  6129. if (c <= UCHAR_MAX) {
  6130. buffer[r] = c;
  6131. if (++r == count) return r;
  6132. } else {
  6133. j = c - (UCHAR_MAX + 1 - THRESHOLD);
  6134. i = (r - decode_p() - 1) & (DICSIZ - 1);
  6135. while (--j >= 0) {
  6136. buffer[r] = buffer[i];
  6137. i = (i + 1) & (DICSIZ - 1);
  6138. if (++r == count) return r;
  6139. }
  6140. }
  6141. }
  6142. }
  6143. /* ===========================================================================
  6144. * Unlzh in to out. Return OK or ERROR.
  6145. */
  6146. int unlzh(in, out)
  6147. int in;
  6148. int out;
  6149. {
  6150. unsigned n;
  6151. ifd = in;
  6152. ofd = out;
  6153. decode_start();
  6154. while (!done) {
  6155. n = decode((unsigned) DICSIZ, window);
  6156. if (!test && n > 0) {
  6157. write_buf(out, (char*)window, n);
  6158. }
  6159. }
  6160. return OK;
  6161. }
  6162. /* unlzw.c -- decompress files in LZW format.
  6163. * The code in this file is directly derived from the public domain 'compress'
  6164. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  6165. * Ken Turkowski, Dave Mack and Peter Jannesen.
  6166. *
  6167. * This is a temporary version which will be rewritten in some future version
  6168. * to accommodate in-memory decompression.
  6169. */
  6170. #ifdef RCSID
  6171. static char rcsid[] = "$Id: unlzw.c,v 0.15 1993/06/10 13:28:35 jloup Exp $";
  6172. #endif
  6173. typedef unsigned char char_type;
  6174. typedef long code_int;
  6175. typedef unsigned long count_int;
  6176. typedef unsigned short count_short;
  6177. typedef unsigned long cmp_code_int;
  6178. #define MAXCODE(n) (1L << (n))
  6179. #ifndef REGISTERS
  6180. # define REGISTERS 2
  6181. #endif
  6182. #define REG1
  6183. #define REG2
  6184. #define REG3
  6185. #define REG4
  6186. #define REG5
  6187. #define REG6
  6188. #define REG7
  6189. #define REG8
  6190. #define REG9
  6191. #define REG10
  6192. #define REG11
  6193. #define REG12
  6194. #define REG13
  6195. #define REG14
  6196. #define REG15
  6197. #define REG16
  6198. #if REGISTERS >= 1
  6199. # undef REG1
  6200. # define REG1 register
  6201. #endif
  6202. #if REGISTERS >= 2
  6203. # undef REG2
  6204. # define REG2 register
  6205. #endif
  6206. #if REGISTERS >= 3
  6207. # undef REG3
  6208. # define REG3 register
  6209. #endif
  6210. #if REGISTERS >= 4
  6211. # undef REG4
  6212. # define REG4 register
  6213. #endif
  6214. #if REGISTERS >= 5
  6215. # undef REG5
  6216. # define REG5 register
  6217. #endif
  6218. #if REGISTERS >= 6
  6219. # undef REG6
  6220. # define REG6 register
  6221. #endif
  6222. #if REGISTERS >= 7
  6223. # undef REG7
  6224. # define REG7 register
  6225. #endif
  6226. #if REGISTERS >= 8
  6227. # undef REG8
  6228. # define REG8 register
  6229. #endif
  6230. #if REGISTERS >= 9
  6231. # undef REG9
  6232. # define REG9 register
  6233. #endif
  6234. #if REGISTERS >= 10
  6235. # undef REG10
  6236. # define REG10 register
  6237. #endif
  6238. #if REGISTERS >= 11
  6239. # undef REG11
  6240. # define REG11 register
  6241. #endif
  6242. #if REGISTERS >= 12
  6243. # undef REG12
  6244. # define REG12 register
  6245. #endif
  6246. #if REGISTERS >= 13
  6247. # undef REG13
  6248. # define REG13 register
  6249. #endif
  6250. #if REGISTERS >= 14
  6251. # undef REG14
  6252. # define REG14 register
  6253. #endif
  6254. #if REGISTERS >= 15
  6255. # undef REG15
  6256. # define REG15 register
  6257. #endif
  6258. #if REGISTERS >= 16
  6259. # undef REG16
  6260. # define REG16 register
  6261. #endif
  6262. #ifndef BYTEORDER
  6263. # define BYTEORDER 0000
  6264. #endif
  6265. #ifndef NOALLIGN
  6266. # define NOALLIGN 0
  6267. #endif
  6268. union bytes {
  6269. long word;
  6270. struct {
  6271. #if BYTEORDER == 4321
  6272. char_type b1;
  6273. char_type b2;
  6274. char_type b3;
  6275. char_type b4;
  6276. #else
  6277. #if BYTEORDER == 1234
  6278. char_type b4;
  6279. char_type b3;
  6280. char_type b2;
  6281. char_type b1;
  6282. #else
  6283. # undef BYTEORDER
  6284. int dummy;
  6285. #endif
  6286. #endif
  6287. } bytes;
  6288. };
  6289. #if BYTEORDER == 4321 && NOALLIGN == 1
  6290. # define input(b,o,c,n,m){ \
  6291. (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
  6292. (o) += (n); \
  6293. }
  6294. #else
  6295. # define input(b,o,c,n,m){ \
  6296. REG1 char_type *p = &(b)[(o)>>3]; \
  6297. (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
  6298. ((long)(p[2])<<16))>>((o)&0x7))&(m); \
  6299. (o) += (n); \
  6300. }
  6301. #endif
  6302. #ifndef MAXSEG_64K
  6303. /* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
  6304. # define tab_prefixof(i) tab_prefix[i]
  6305. # define clear_tab_prefixof() memzero(tab_prefix, 256);
  6306. #else
  6307. /* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
  6308. /* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd codes */
  6309. ush *tab_prefix[2];
  6310. # define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
  6311. # define clear_tab_prefixof() \
  6312. memzero(tab_prefix0, 128), \
  6313. memzero(tab_prefix1, 128);
  6314. #endif
  6315. #define de_stack ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
  6316. #define tab_suffixof(i) tab_suffix[i]
  6317. int block_mode = BLOCK_MODE; /* block compress mode -C compatible with 2.0 */
  6318. /* ============================================================================
  6319. * Decompress in to out. This routine adapts to the codes in the
  6320. * file building the "string" table on-the-fly; requiring no table to
  6321. * be stored in the compressed file.
  6322. * IN assertions: the buffer inbuf contains already the beginning of
  6323. * the compressed data, from offsets iptr to insize-1 included.
  6324. * The magic header has already been checked and skipped.
  6325. * bytes_in and bytes_out have been initialized.
  6326. */
  6327. int unlzw(in, out)
  6328. int in, out; /* input and output file descriptors */
  6329. {
  6330. REG2 char_type *stackp;
  6331. REG3 code_int code;
  6332. REG4 int finchar;
  6333. REG5 code_int oldcode;
  6334. REG6 code_int incode;
  6335. REG7 long inbits;
  6336. REG8 long posbits;
  6337. REG9 int outpos;
  6338. /* REG10 int insize; (global) */
  6339. REG11 unsigned bitmask;
  6340. REG12 code_int free_ent;
  6341. REG13 code_int maxcode;
  6342. REG14 code_int maxmaxcode;
  6343. REG15 int n_bits;
  6344. REG16 int rsize;
  6345. #ifdef MAXSEG_64K
  6346. tab_prefix[0] = tab_prefix0;
  6347. tab_prefix[1] = tab_prefix1;
  6348. #endif
  6349. maxbits = get_byte();
  6350. block_mode = maxbits & BLOCK_MODE;
  6351. if ((maxbits & LZW_RESERVED) != 0) {
  6352. WARN((stderr, "\n%s: %s: warning, unknown flags 0x%x\n",
  6353. progname, ifname, maxbits & LZW_RESERVED));
  6354. }
  6355. maxbits &= BIT_MASK;
  6356. maxmaxcode = MAXCODE(maxbits);
  6357. if (maxbits > BITS) {
  6358. fprintf(stderr,
  6359. "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
  6360. progname, ifname, maxbits, BITS);
  6361. exit_code = ERROR;
  6362. return ERROR;
  6363. }
  6364. rsize = insize;
  6365. maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  6366. bitmask = (1<<n_bits)-1;
  6367. oldcode = -1;
  6368. finchar = 0;
  6369. outpos = 0;
  6370. posbits = inptr<<3;
  6371. free_ent = ((block_mode) ? FIRST : 256);
  6372. clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
  6373. for (code = 255 ; code >= 0 ; --code) {
  6374. tab_suffixof(code) = (char_type)code;
  6375. }
  6376. do {
  6377. REG1 int i;
  6378. int e;
  6379. int o;
  6380. resetbuf:
  6381. e = insize-(o = (posbits>>3));
  6382. for (i = 0 ; i < e ; ++i) {
  6383. inbuf[i] = inbuf[i+o];
  6384. }
  6385. insize = e;
  6386. posbits = 0;
  6387. if (insize < INBUF_EXTRA) {
  6388. if ((rsize = read(in, (char*)inbuf+insize, INBUFSIZ)) == -1) {
  6389. read_error();
  6390. }
  6391. insize += rsize;
  6392. bytes_in += (off_t)rsize;
  6393. }
  6394. inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 :
  6395. ((long)insize<<3)-(n_bits-1));
  6396. while (inbits > posbits) {
  6397. if (free_ent > maxcode) {
  6398. posbits = ((posbits-1) +
  6399. ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  6400. ++n_bits;
  6401. if (n_bits == maxbits) {
  6402. maxcode = maxmaxcode;
  6403. } else {
  6404. maxcode = MAXCODE(n_bits)-1;
  6405. }
  6406. bitmask = (1<<n_bits)-1;
  6407. goto resetbuf;
  6408. }
  6409. input(inbuf,posbits,code,n_bits,bitmask);
  6410. Tracev((stderr, "%d ", code));
  6411. if (oldcode == -1) {
  6412. if (code >= 256) error("corrupt input.");
  6413. outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
  6414. continue;
  6415. }
  6416. if (code == CLEAR && block_mode) {
  6417. clear_tab_prefixof();
  6418. free_ent = FIRST - 1;
  6419. posbits = ((posbits-1) +
  6420. ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  6421. maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  6422. bitmask = (1<<n_bits)-1;
  6423. goto resetbuf;
  6424. }
  6425. incode = code;
  6426. stackp = de_stack;
  6427. if (code >= free_ent) { /* Special case for KwKwK string. */
  6428. if (code > free_ent) {
  6429. #ifdef DEBUG
  6430. char_type *p;
  6431. posbits -= n_bits;
  6432. p = &inbuf[posbits>>3];
  6433. fprintf(stderr,
  6434. "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
  6435. code, free_ent, n_bits, insize);
  6436. fprintf(stderr,
  6437. "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
  6438. posbits, p[-1],p[0],p[1],p[2],p[3]);
  6439. #endif
  6440. if (!test && outpos > 0) {
  6441. write_buf(out, (char*)outbuf, outpos);
  6442. bytes_out += (off_t)outpos;
  6443. }
  6444. error(to_stdout ? "corrupt input." :
  6445. "corrupt input. Use zcat to recover some data.");
  6446. }
  6447. *--stackp = (char_type)finchar;
  6448. code = oldcode;
  6449. }
  6450. while ((cmp_code_int)code >= (cmp_code_int)256) {
  6451. /* Generate output characters in reverse order */
  6452. *--stackp = tab_suffixof(code);
  6453. code = tab_prefixof(code);
  6454. }
  6455. *--stackp = (char_type)(finchar = tab_suffixof(code));
  6456. /* And put them out in forward order */
  6457. {
  6458. REG1 int i;
  6459. if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
  6460. do {
  6461. if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
  6462. if (i > 0) {
  6463. memcpy(outbuf+outpos, stackp, i);
  6464. outpos += i;
  6465. }
  6466. if (outpos >= OUTBUFSIZ) {
  6467. if (!test) {
  6468. write_buf(out, (char*)outbuf, outpos);
  6469. bytes_out += (off_t)outpos;
  6470. }
  6471. outpos = 0;
  6472. }
  6473. stackp+= i;
  6474. } while ((i = (de_stack-stackp)) > 0);
  6475. } else {
  6476. memcpy(outbuf+outpos, stackp, i);
  6477. outpos += i;
  6478. }
  6479. }
  6480. if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
  6481. tab_prefixof(code) = (unsigned short)oldcode;
  6482. tab_suffixof(code) = (char_type)finchar;
  6483. free_ent = code+1;
  6484. }
  6485. oldcode = incode; /* Remember previous code. */
  6486. }
  6487. } while (rsize != 0);
  6488. if (!test && outpos > 0) {
  6489. write_buf(out, (char*)outbuf, outpos);
  6490. bytes_out += (off_t)outpos;
  6491. }
  6492. return OK;
  6493. }
  6494. /* unpack.c -- decompress files in pack format.
  6495. * Copyright (C) 1992-1993 Jean-loup Gailly
  6496. * This is free software; you can redistribute it and/or modify it under the
  6497. * terms of the GNU General Public License, see the file COPYING.
  6498. */
  6499. #ifdef RCSID
  6500. static char rcsid[] = "$Id: unpack.c,v 1.4 1993/06/11 19:25:36 jloup Exp $";
  6501. #endif
  6502. #define MIN(a,b) ((a) <= (b) ? (a) : (b))
  6503. /* The arguments must not have side effects. */
  6504. #define MAX_BITLEN 25
  6505. /* Maximum length of Huffman codes. (Minor modifications to the code
  6506. * would be needed to support 32 bits codes, but pack never generates
  6507. * more than 24 bits anyway.)
  6508. */
  6509. #define LITERALS 256
  6510. /* Number of literals, excluding the End of Block (EOB) code */
  6511. #define MAX_PEEK 12
  6512. /* Maximum number of 'peek' bits used to optimize traversal of the
  6513. * Huffman tree.
  6514. */
  6515. local ulg orig_len; /* original uncompressed length */
  6516. local int max_len; /* maximum bit length of Huffman codes */
  6517. local uch literal[LITERALS];
  6518. /* The literal bytes present in the Huffman tree. The EOB code is not
  6519. * represented.
  6520. */
  6521. local int lit_base[MAX_BITLEN+1];
  6522. /* All literals of a given bit length are contiguous in literal[] and
  6523. * have contiguous codes. literal[code+lit_base[len]] is the literal
  6524. * for a code of len bits.
  6525. */
  6526. local int leaves [MAX_BITLEN+1]; /* Number of leaves for each bit length */
  6527. local int parents[MAX_BITLEN+1]; /* Number of parents for each bit length */
  6528. local int peek_bits; /* Number of peek bits currently used */
  6529. /* local uch prefix_len[1 << MAX_PEEK]; */
  6530. #define prefix_len outbuf
  6531. /* For each bit pattern b of peek_bits bits, prefix_len[b] is the length
  6532. * of the Huffman code starting with a prefix of b (upper bits), or 0
  6533. * if all codes of prefix b have more than peek_bits bits. It is not
  6534. * necessary to have a huge table (large MAX_PEEK) because most of the
  6535. * codes encountered in the input stream are short codes (by construction).
  6536. * So for most codes a single lookup will be necessary.
  6537. */
  6538. #if (1<<MAX_PEEK) > OUTBUFSIZ
  6539. error cannot overlay prefix_len and outbuf
  6540. #endif
  6541. local ulg bitbuf;
  6542. /* Bits are added on the low part of bitbuf and read from the high part. */
  6543. local int valid; /* number of valid bits in bitbuf */
  6544. /* all bits above the last valid bit are always zero */
  6545. /* Set code to the next 'bits' input bits without skipping them. code
  6546. * must be the name of a simple variable and bits must not have side effects.
  6547. * IN assertions: bits <= 25 (so that we still have room for an extra byte
  6548. * when valid is only 24), and mask = (1<<bits)-1.
  6549. */
  6550. #define look_bits(code,bits,mask) \
  6551. { \
  6552. while (valid < (bits)) bitbuf = (bitbuf<<8) | (ulg)get_byte(), valid += 8; \
  6553. code = (bitbuf >> (valid-(bits))) & (mask); \
  6554. }
  6555. /* Skip the given number of bits (after having peeked at them): */
  6556. #define skip_bits(bits) (valid -= (bits))
  6557. #define clear_bitbuf() (valid = 0, bitbuf = 0)
  6558. /* Local functions */
  6559. local void read_tree OF((void));
  6560. local void build_tree OF((void));
  6561. /* ===========================================================================
  6562. * Read the Huffman tree.
  6563. */
  6564. local void read_tree()
  6565. {
  6566. int len; /* bit length */
  6567. int base; /* base offset for a sequence of leaves */
  6568. int n;
  6569. /* Read the original input size, MSB first */
  6570. orig_len = 0;
  6571. for (n = 1; n <= 4; n++) orig_len = (orig_len << 8) | (ulg)get_byte();
  6572. max_len = (int)get_byte(); /* maximum bit length of Huffman codes */
  6573. if (max_len > MAX_BITLEN) {
  6574. error("invalid compressed data -- Huffman code > 32 bits");
  6575. }
  6576. /* Get the number of leaves at each bit length */
  6577. n = 0;
  6578. for (len = 1; len <= max_len; len++) {
  6579. leaves[len] = (int)get_byte();
  6580. n += leaves[len];
  6581. }
  6582. if (n > LITERALS) {
  6583. error("too many leaves in Huffman tree");
  6584. }
  6585. Trace((stderr, "orig_len %lu, max_len %d, leaves %d\n",
  6586. orig_len, max_len, n));
  6587. /* There are at least 2 and at most 256 leaves of length max_len.
  6588. * (Pack arbitrarily rejects empty files and files consisting of
  6589. * a single byte even repeated.) To fit the last leaf count in a
  6590. * byte, it is offset by 2. However, the last literal is the EOB
  6591. * code, and is not transmitted explicitly in the tree, so we must
  6592. * adjust here by one only.
  6593. */
  6594. leaves[max_len]++;
  6595. /* Now read the leaves themselves */
  6596. base = 0;
  6597. for (len = 1; len <= max_len; len++) {
  6598. /* Remember where the literals of this length start in literal[] : */
  6599. lit_base[len] = base;
  6600. /* And read the literals: */
  6601. for (n = leaves[len]; n > 0; n--) {
  6602. literal[base++] = (uch)get_byte();
  6603. }
  6604. }
  6605. leaves[max_len]++; /* Now include the EOB code in the Huffman tree */
  6606. }
  6607. /* ===========================================================================
  6608. * Build the Huffman tree and the prefix table.
  6609. */
  6610. local void build_tree()
  6611. {
  6612. int nodes = 0; /* number of nodes (parents+leaves) at current bit length */
  6613. int len; /* current bit length */
  6614. uch *prefixp; /* pointer in prefix_len */
  6615. for (len = max_len; len >= 1; len--) {
  6616. /* The number of parent nodes at this level is half the total
  6617. * number of nodes at parent level:
  6618. */
  6619. nodes >>= 1;
  6620. parents[len] = nodes;
  6621. /* Update lit_base by the appropriate bias to skip the parent nodes
  6622. * (which are not represented in the literal array):
  6623. */
  6624. lit_base[len] -= nodes;
  6625. /* Restore nodes to be parents+leaves: */
  6626. nodes += leaves[len];
  6627. }
  6628. /* Construct the prefix table, from shortest leaves to longest ones.
  6629. * The shortest code is all ones, so we start at the end of the table.
  6630. */
  6631. peek_bits = MIN(max_len, MAX_PEEK);
  6632. prefixp = &prefix_len[1<<peek_bits];
  6633. for (len = 1; len <= peek_bits; len++) {
  6634. int prefixes = leaves[len] << (peek_bits-len); /* may be 0 */
  6635. while (prefixes--) *--prefixp = (uch)len;
  6636. }
  6637. /* The length of all other codes is unknown: */
  6638. while (prefixp > prefix_len) *--prefixp = 0;
  6639. }
  6640. /* ===========================================================================
  6641. * Unpack in to out. This routine does not support the old pack format
  6642. * with magic header \037\037.
  6643. *
  6644. * IN assertions: the buffer inbuf contains already the beginning of
  6645. * the compressed data, from offsets inptr to insize-1 included.
  6646. * The magic header has already been checked. The output buffer is cleared.
  6647. */
  6648. int unpack(in, out)
  6649. int in, out; /* input and output file descriptors */
  6650. {
  6651. int len; /* Bit length of current code */
  6652. unsigned eob; /* End Of Block code */
  6653. register unsigned peek; /* lookahead bits */
  6654. unsigned peek_mask; /* Mask for peek_bits bits */
  6655. ifd = in;
  6656. ofd = out;
  6657. read_tree(); /* Read the Huffman tree */
  6658. build_tree(); /* Build the prefix table */
  6659. clear_bitbuf(); /* Initialize bit input */
  6660. peek_mask = (1<<peek_bits)-1;
  6661. /* The eob code is the largest code among all leaves of maximal length: */
  6662. eob = leaves[max_len]-1;
  6663. Trace((stderr, "eob %d %x\n", max_len, eob));
  6664. /* Decode the input data: */
  6665. for (;;) {
  6666. /* Since eob is the longest code and not shorter than max_len,
  6667. * we can peek at max_len bits without having the risk of reading
  6668. * beyond the end of file.
  6669. */
  6670. look_bits(peek, peek_bits, peek_mask);
  6671. len = prefix_len[peek];
  6672. if (len > 0) {
  6673. peek >>= peek_bits - len; /* discard the extra bits */
  6674. } else {
  6675. /* Code of more than peek_bits bits, we must traverse the tree */
  6676. ulg mask = peek_mask;
  6677. len = peek_bits;
  6678. do {
  6679. len++, mask = (mask<<1)+1;
  6680. look_bits(peek, len, mask);
  6681. } while (peek < (unsigned)parents[len]);
  6682. /* loop as long as peek is a parent node */
  6683. }
  6684. /* At this point, peek is the next complete code, of len bits */
  6685. if (peek == eob && len == max_len) break; /* end of file? */
  6686. put_ubyte(literal[peek+lit_base[len]]);
  6687. Tracev((stderr,"%02d %04x %c\n", len, peek,
  6688. literal[peek+lit_base[len]]));
  6689. skip_bits(len);
  6690. } /* for (;;) */
  6691. flush_window();
  6692. if (orig_len != (ulg)(bytes_out & 0xffffffff)) {
  6693. error("invalid compressed data--length error");
  6694. }
  6695. return OK;
  6696. }
  6697. /* unzip.c -- decompress files in gzip or pkzip format.
  6698. * Copyright (C) 1992-1993 Jean-loup Gailly
  6699. * This is free software; you can redistribute it and/or modify it under the
  6700. * terms of the GNU General Public License, see the file COPYING.
  6701. *
  6702. * The code in this file is derived from the file funzip.c written
  6703. * and put in the public domain by Mark Adler.
  6704. */
  6705. /*
  6706. This version can extract files in gzip or pkzip format.
  6707. For the latter, only the first entry is extracted, and it has to be
  6708. either deflated or stored.
  6709. */
  6710. #ifdef RCSID
  6711. static char rcsid[] = "$Id: unzip.c,v 0.13 1993/06/10 13:29:00 jloup Exp $";
  6712. #endif
  6713. /* PKZIP header definitions */
  6714. #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
  6715. #define LOCFLG 6 /* offset of bit flag */
  6716. #define CRPFLG 1 /* bit for encrypted entry */
  6717. #define EXTFLG 8 /* bit for extended local header */
  6718. #define LOCHOW 8 /* offset of compression method */
  6719. #define LOCTIM 10 /* file mod time (for decryption) */
  6720. #define LOCCRC 14 /* offset of crc */
  6721. #define LOCSIZ 18 /* offset of compressed size */
  6722. #define LOCLEN 22 /* offset of uncompressed length */
  6723. #define LOCFIL 26 /* offset of file name field length */
  6724. #define LOCEXT 28 /* offset of extra field length */
  6725. #define LOCHDR 30 /* size of local header, including sig */
  6726. #define EXTHDR 16 /* size of extended local header, inc sig */
  6727. #define RAND_HEAD_LEN 12 /* length of encryption random header */
  6728. /* Globals */
  6729. int decrypt; /* flag to turn on decryption */
  6730. char *key; /* not used--needed to link crypt.c */
  6731. int pkzip = 0; /* set for a pkzip file */
  6732. int ext_header = 0; /* set if extended local header */
  6733. /* ===========================================================================
  6734. * Check zip file and advance inptr to the start of the compressed data.
  6735. * Get ofname from the local header if necessary.
  6736. */
  6737. int check_zipfile(in)
  6738. int in; /* input file descriptors */
  6739. {
  6740. uch *h = inbuf + inptr; /* first local header */
  6741. ifd = in;
  6742. /* Check validity of local header, and skip name and extra fields */
  6743. inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
  6744. if (inptr > insize || LG(h) != LOCSIG) {
  6745. fprintf(stderr, "\n%s: %s: not a valid zip file\n",
  6746. progname, ifname);
  6747. exit_code = ERROR;
  6748. return ERROR;
  6749. }
  6750. method = h[LOCHOW];
  6751. if (method != STORED && method != DEFLATED) {
  6752. fprintf(stderr,
  6753. "\n%s: %s: first entry not deflated or stored -- use unzip\n",
  6754. progname, ifname);
  6755. exit_code = ERROR;
  6756. return ERROR;
  6757. }
  6758. /* If entry encrypted, decrypt and validate encryption header */
  6759. if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
  6760. fprintf(stderr, "\n%s: %s: encrypted file -- use unzip\n",
  6761. progname, ifname);
  6762. exit_code = ERROR;
  6763. return ERROR;
  6764. }
  6765. /* Save flags for unzip() */
  6766. ext_header = (h[LOCFLG] & EXTFLG) != 0;
  6767. pkzip = 1;
  6768. /* Get ofname and time stamp from local header (to be done) */
  6769. return OK;
  6770. }
  6771. /* ===========================================================================
  6772. * Unzip in to out. This routine works on both gzip and pkzip files.
  6773. *
  6774. * IN assertions: the buffer inbuf contains already the beginning of
  6775. * the compressed data, from offsets inptr to insize-1 included.
  6776. * The magic header has already been checked. The output buffer is cleared.
  6777. */
  6778. int unzip(in, out)
  6779. int in, out; /* input and output file descriptors */
  6780. {
  6781. ulg orig_crc = 0; /* original crc */
  6782. ulg orig_len = 0; /* original uncompressed length */
  6783. int n;
  6784. uch buf[EXTHDR]; /* extended local header */
  6785. int err = OK;
  6786. ifd = in;
  6787. ofd = out;
  6788. updcrc(NULL, 0); /* initialize crc */
  6789. if (pkzip && !ext_header) { /* crc and length at the end otherwise */
  6790. orig_crc = LG(inbuf + LOCCRC);
  6791. orig_len = LG(inbuf + LOCLEN);
  6792. }
  6793. /* Decompress */
  6794. if (method == DEFLATED) {
  6795. int res = inflate();
  6796. if (res == 3) {
  6797. error("out of memory");
  6798. } else if (res != 0) {
  6799. error("invalid compressed data--format violated");
  6800. }
  6801. } else if (pkzip && method == STORED) {
  6802. register ulg n = LG(inbuf + LOCLEN);
  6803. if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
  6804. fprintf(stderr, "len %ld, siz %ld\n", n, LG(inbuf + LOCSIZ));
  6805. error("invalid compressed data--length mismatch");
  6806. }
  6807. while (n--) {
  6808. uch c = (uch)get_byte();
  6809. put_ubyte(c);
  6810. }
  6811. flush_window();
  6812. } else {
  6813. error("internal error, invalid method");
  6814. }
  6815. /* Get the crc and original length */
  6816. if (!pkzip) {
  6817. /* crc32 (see algorithm.doc)
  6818. * uncompressed input size modulo 2^32
  6819. */
  6820. for (n = 0; n < 8; n++) {
  6821. buf[n] = (uch)get_byte(); /* may cause an error if EOF */
  6822. }
  6823. orig_crc = LG(buf);
  6824. orig_len = LG(buf+4);
  6825. } else if (ext_header) { /* If extended header, check it */
  6826. /* signature - 4bytes: 0x50 0x4b 0x07 0x08
  6827. * CRC-32 value
  6828. * compressed size 4-bytes
  6829. * uncompressed size 4-bytes
  6830. */
  6831. for (n = 0; n < EXTHDR; n++) {
  6832. buf[n] = (uch)get_byte(); /* may cause an error if EOF */
  6833. }
  6834. orig_crc = LG(buf+4);
  6835. orig_len = LG(buf+12);
  6836. }
  6837. /* Validate decompression */
  6838. if (orig_crc != updcrc(outbuf, 0)) {
  6839. fprintf(stderr, "\n%s: %s: invalid compressed data--crc error\n",
  6840. progname, ifname);
  6841. err = ERROR;
  6842. }
  6843. if (orig_len != (ulg)(bytes_out & 0xffffffff)) {
  6844. fprintf(stderr, "\n%s: %s: invalid compressed data--length error\n",
  6845. progname, ifname);
  6846. err = ERROR;
  6847. }
  6848. /* Check if there are more entries in a pkzip file */
  6849. if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
  6850. if (to_stdout) {
  6851. WARN((stderr,
  6852. "%s: %s has more than one entry--rest ignored\n",
  6853. progname, ifname));
  6854. } else {
  6855. /* Don't destroy the input zip file */
  6856. fprintf(stderr,
  6857. "%s: %s has more than one entry -- unchanged\n",
  6858. progname, ifname);
  6859. err = ERROR;
  6860. }
  6861. }
  6862. ext_header = pkzip = 0; /* for next file */
  6863. if (err == OK) return OK;
  6864. exit_code = ERROR;
  6865. if (!test) abort_gzip();
  6866. return err;
  6867. }
  6868. /* util.c -- utility functions for gzip support
  6869. * Copyright (C) 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
  6870. * Copyright (C) 1992-1993 Jean-loup Gailly
  6871. * This is free software; you can redistribute it and/or modify it under the
  6872. * terms of the GNU General Public License, see the file COPYING.
  6873. */
  6874. #ifdef RCSID
  6875. static char rcsid[] = "$Id: util.c,v 0.15 1993/06/15 09:04:13 jloup Exp $";
  6876. #endif
  6877. #ifndef CHAR_BIT
  6878. # define CHAR_BIT 8
  6879. #endif
  6880. extern ulg crc_32_tab[]; /* crc table, defined below */
  6881. /* ===========================================================================
  6882. * Copy input to output unchanged: zcat == cat with --force.
  6883. * IN assertion: insize bytes have already been read in inbuf.
  6884. */
  6885. int copy(in, out)
  6886. int in, out; /* input and output file descriptors */
  6887. {
  6888. errno = 0;
  6889. while (insize != 0 && (int)insize != -1) {
  6890. write_buf(out, (char*)inbuf, insize);
  6891. bytes_out += insize;
  6892. insize = read(in, (char*)inbuf, INBUFSIZ);
  6893. }
  6894. if ((int)insize == -1) {
  6895. read_error();
  6896. }
  6897. bytes_in = bytes_out;
  6898. return OK;
  6899. }
  6900. /* ===========================================================================
  6901. * Run a set of bytes through the crc shift register. If s is a NULL
  6902. * pointer, then initialize the crc shift register contents instead.
  6903. * Return the current crc in either case.
  6904. */
  6905. ulg updcrc(s, n)
  6906. uch *s; /* pointer to bytes to pump through */
  6907. unsigned n; /* number of bytes in s[] */
  6908. {
  6909. register ulg c; /* temporary variable */
  6910. static ulg crc = (ulg)0xffffffffL; /* shift register contents */
  6911. if (s == NULL) {
  6912. c = 0xffffffffL;
  6913. } else {
  6914. c = crc;
  6915. if (n) do {
  6916. c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
  6917. } while (--n);
  6918. }
  6919. crc = c;
  6920. return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */
  6921. }
  6922. /* ===========================================================================
  6923. * Clear input and output buffers
  6924. */
  6925. void clear_bufs()
  6926. {
  6927. outcnt = 0;
  6928. insize = inptr = 0;
  6929. bytes_in = bytes_out = 0L;
  6930. }
  6931. /* ===========================================================================
  6932. * Fill the input buffer. This is called only when the buffer is empty.
  6933. */
  6934. int fill_inbuf(eof_ok)
  6935. int eof_ok; /* set if EOF acceptable as a result */
  6936. {
  6937. int len;
  6938. /* Read as much as possible */
  6939. insize = 0;
  6940. do {
  6941. len = read(ifd, (char*)inbuf+insize, INBUFSIZ-insize);
  6942. if (len == 0) break;
  6943. if (len == -1) {
  6944. read_error();
  6945. break;
  6946. }
  6947. insize += len;
  6948. } while (insize < INBUFSIZ);
  6949. if (insize == 0) {
  6950. if (eof_ok) return EOF;
  6951. flush_window();
  6952. errno = 0;
  6953. read_error();
  6954. }
  6955. bytes_in += (off_t)insize;
  6956. inptr = 1;
  6957. return inbuf[0];
  6958. }
  6959. /* ===========================================================================
  6960. * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
  6961. * (used for the compressed data only)
  6962. */
  6963. void flush_outbuf()
  6964. {
  6965. if (outcnt == 0) return;
  6966. write_buf(ofd, (char *)outbuf, outcnt);
  6967. bytes_out += (off_t)outcnt;
  6968. outcnt = 0;
  6969. }
  6970. /* ===========================================================================
  6971. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  6972. * (Used for the decompressed data only.)
  6973. */
  6974. void flush_window()
  6975. {
  6976. if (outcnt == 0) return;
  6977. updcrc(window, outcnt);
  6978. if (!test) {
  6979. write_buf(ofd, (char *)window, outcnt);
  6980. }
  6981. bytes_out += (off_t)outcnt;
  6982. outcnt = 0;
  6983. }
  6984. /* ===========================================================================
  6985. * Does the same as write(), but also handles partial pipe writes and checks
  6986. * for error return.
  6987. */
  6988. void write_buf(fd, buf, cnt)
  6989. int fd;
  6990. voidp buf;
  6991. unsigned cnt;
  6992. {
  6993. unsigned n;
  6994. while ((n = write(fd, buf, cnt)) != cnt) {
  6995. if (n == (unsigned)(-1)) {
  6996. write_error();
  6997. }
  6998. cnt -= n;
  6999. buf = (voidp)((char*)buf+n);
  7000. }
  7001. }
  7002. /* ========================================================================
  7003. * Put string s in lower case, return s.
  7004. */
  7005. char *strlwr(s)
  7006. char *s;
  7007. {
  7008. char *t;
  7009. for (t = s; *t; t++)
  7010. *t = tolow ((unsigned char) *t);
  7011. return s;
  7012. }
  7013. /* ========================================================================
  7014. * Return the base name of a file (remove any directory prefix and
  7015. * any version suffix). For systems with file names that are not
  7016. * case sensitive, force the base name to lower case.
  7017. */
  7018. char *base_name(fname)
  7019. char *fname;
  7020. {
  7021. char *p;
  7022. if ((p = strrchr(fname, PATH_SEP)) != NULL) fname = p+1;
  7023. #ifdef PATH_SEP2
  7024. if ((p = strrchr(fname, PATH_SEP2)) != NULL) fname = p+1;
  7025. #endif
  7026. #ifdef PATH_SEP3
  7027. if ((p = strrchr(fname, PATH_SEP3)) != NULL) fname = p+1;
  7028. #endif
  7029. #ifdef SUFFIX_SEP
  7030. if ((p = strrchr(fname, SUFFIX_SEP)) != NULL) *p = '\0';
  7031. #endif
  7032. if (casemap('A') == 'a') strlwr(fname);
  7033. return fname;
  7034. }
  7035. /* ========================================================================
  7036. * Unlink a file, working around the unlink readonly bug (if present).
  7037. */
  7038. int xunlink (filename)
  7039. char *filename;
  7040. {
  7041. int r = unlink (filename);
  7042. #ifdef UNLINK_READONLY_BUG
  7043. if (r != 0)
  7044. {
  7045. int e = errno;
  7046. if (chmod (filename, S_IWUSR) != 0)
  7047. {
  7048. errno = e;
  7049. return -1;
  7050. }
  7051. r = unlink (filename);
  7052. }
  7053. #endif
  7054. return r;
  7055. }
  7056. /* ========================================================================
  7057. * Make a file name legal for file systems not allowing file names with
  7058. * multiple dots or starting with a dot (such as MSDOS), by changing
  7059. * all dots except the last one into underlines. A target dependent
  7060. * function can be used instead of this simple function by defining the macro
  7061. * MAKE_LEGAL_NAME in tailor.h and providing the function in a target
  7062. * dependent module.
  7063. */
  7064. void make_simple_name(name)
  7065. char *name;
  7066. {
  7067. char *p = strrchr(name, '.');
  7068. if (p == NULL) return;
  7069. if (p == name) p++;
  7070. do {
  7071. if (*--p == '.') *p = '_';
  7072. } while (p != name);
  7073. }
  7074. #if !defined HAVE_STRING_H && !defined STDC_HEADERS
  7075. /* Provide missing strspn and strcspn functions. */
  7076. # ifndef __STDC__
  7077. # define const
  7078. # endif
  7079. int strspn OF((const char *s, const char *accept));
  7080. int strcspn OF((const char *s, const char *reject));
  7081. /* ========================================================================
  7082. * Return the length of the maximum initial segment
  7083. * of s which contains only characters in accept.
  7084. */
  7085. int strspn(s, accept)
  7086. const char *s;
  7087. const char *accept;
  7088. {
  7089. register const char *p;
  7090. register const char *a;
  7091. register int count = 0;
  7092. for (p = s; *p != '\0'; ++p) {
  7093. for (a = accept; *a != '\0'; ++a) {
  7094. if (*p == *a) break;
  7095. }
  7096. if (*a == '\0') return count;
  7097. ++count;
  7098. }
  7099. return count;
  7100. }
  7101. /* ========================================================================
  7102. * Return the length of the maximum inital segment of s
  7103. * which contains no characters from reject.
  7104. */
  7105. int strcspn(s, reject)
  7106. const char *s;
  7107. const char *reject;
  7108. {
  7109. register int count = 0;
  7110. while (*s != '\0') {
  7111. if (strchr(reject, *s++) != NULL) return count;
  7112. ++count;
  7113. }
  7114. return count;
  7115. }
  7116. #endif
  7117. /* ========================================================================
  7118. * Add an environment variable (if any) before argv, and update argc.
  7119. * Return the expanded environment variable to be freed later, or NULL
  7120. * if no options were added to argv.
  7121. */
  7122. #define SEPARATOR " \t" /* separators in env variable */
  7123. char *add_envopt(argcp, argvp, env)
  7124. int *argcp; /* pointer to argc */
  7125. char ***argvp; /* pointer to argv */
  7126. char *env; /* name of environment variable */
  7127. {
  7128. char *p; /* running pointer through env variable */
  7129. char **oargv; /* runs through old argv array */
  7130. char **nargv; /* runs through new argv array */
  7131. int oargc = *argcp; /* old argc */
  7132. int nargc = 0; /* number of arguments in env variable */
  7133. env = (char*)getenv(env);
  7134. if (env == NULL) return NULL;
  7135. p = (char*)xmalloc(strlen(env)+1);
  7136. env = strcpy(p, env); /* keep env variable intact */
  7137. for (p = env; *p; nargc++ ) { /* move through env */
  7138. p += strspn(p, SEPARATOR); /* skip leading separators */
  7139. if (*p == '\0') break;
  7140. p += strcspn(p, SEPARATOR); /* find end of word */
  7141. if (*p) *p++ = '\0'; /* mark it */
  7142. }
  7143. if (nargc == 0) {
  7144. free(env);
  7145. return NULL;
  7146. }
  7147. *argcp += nargc;
  7148. /* Allocate the new argv array, with an extra element just in case
  7149. * the original arg list did not end with a NULL.
  7150. */
  7151. nargv = (char**)calloc(*argcp+1, sizeof(char *));
  7152. if (nargv == NULL) error("out of memory");
  7153. oargv = *argvp;
  7154. *argvp = nargv;
  7155. /* Copy the program name first */
  7156. if (oargc-- < 0) error("argc<=0");
  7157. *(nargv++) = *(oargv++);
  7158. /* Then copy the environment args */
  7159. for (p = env; nargc > 0; nargc--) {
  7160. p += strspn(p, SEPARATOR); /* skip separators */
  7161. *(nargv++) = p; /* store start */
  7162. while (*p++) ; /* skip over word */
  7163. }
  7164. /* Finally copy the old args and add a NULL (usual convention) */
  7165. while (oargc--) *(nargv++) = *(oargv++);
  7166. *nargv = NULL;
  7167. return env;
  7168. }
  7169. /* ========================================================================
  7170. * Error handlers.
  7171. */
  7172. void error(m)
  7173. char *m;
  7174. {
  7175. fprintf(stderr, "\n%s: %s: %s\n", progname, ifname, m);
  7176. abort_gzip();
  7177. }
  7178. void warning (m)
  7179. char *m;
  7180. {
  7181. WARN ((stderr, "%s: %s: warning: %s\n", progname, ifname, m));
  7182. }
  7183. void read_error()
  7184. {
  7185. int e = errno;
  7186. fprintf(stderr, "\n%s: ", progname);
  7187. if (e != 0) {
  7188. errno = e;
  7189. perror(ifname);
  7190. } else {
  7191. fprintf(stderr, "%s: unexpected end of file\n", ifname);
  7192. }
  7193. abort_gzip();
  7194. }
  7195. void write_error()
  7196. {
  7197. int e = errno;
  7198. fprintf(stderr, "\n%s: ", progname);
  7199. errno = e;
  7200. perror(ofname);
  7201. abort_gzip();
  7202. }
  7203. /* ========================================================================
  7204. * Display compression ratio on the given stream on 6 characters.
  7205. */
  7206. void display_ratio(num, den, file)
  7207. off_t num;
  7208. off_t den;
  7209. FILE *file;
  7210. {
  7211. fprintf(file, "%5.1f%%", den == 0 ? 0 : 100.0 * num / den);
  7212. }
  7213. /* ========================================================================
  7214. * Print an off_t. There's no completely portable way to use printf,
  7215. * so we do it ourselves.
  7216. */
  7217. void fprint_off(file, offset, width)
  7218. FILE *file;
  7219. off_t offset;
  7220. int width;
  7221. {
  7222. char buf[CHAR_BIT * sizeof (off_t)];
  7223. char *p = buf + sizeof buf;
  7224. /* Don't negate offset here; it might overflow. */
  7225. if (offset < 0) {
  7226. do
  7227. *--p = '0' - offset % 10;
  7228. while ((offset /= 10) != 0);
  7229. *--p = '-';
  7230. } else {
  7231. do
  7232. *--p = '0' + offset % 10;
  7233. while ((offset /= 10) != 0);
  7234. }
  7235. width -= buf + sizeof buf - p;
  7236. while (0 < width--) {
  7237. putc (' ', file);
  7238. }
  7239. for (; p < buf + sizeof buf; p++)
  7240. putc (*p, file);
  7241. }
  7242. /* ========================================================================
  7243. * Semi-safe malloc -- never returns NULL.
  7244. */
  7245. voidp xmalloc (size)
  7246. unsigned size;
  7247. {
  7248. voidp cp = (voidp)malloc (size);
  7249. if (cp == NULL) error("out of memory");
  7250. return cp;
  7251. }
  7252. /* ========================================================================
  7253. * Table of CRC-32's of all single-byte values (made by makecrc.c)
  7254. */
  7255. ulg crc_32_tab[] = {
  7256. 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
  7257. 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
  7258. 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
  7259. 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
  7260. 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
  7261. 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
  7262. 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
  7263. 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  7264. 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
  7265. 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
  7266. 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
  7267. 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
  7268. 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
  7269. 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
  7270. 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
  7271. 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  7272. 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
  7273. 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
  7274. 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
  7275. 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
  7276. 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
  7277. 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
  7278. 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
  7279. 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  7280. 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
  7281. 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
  7282. 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
  7283. 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
  7284. 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
  7285. 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
  7286. 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
  7287. 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  7288. 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
  7289. 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
  7290. 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  7291. 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  7292. 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  7293. 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  7294. 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  7295. 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  7296. 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  7297. 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  7298. 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  7299. 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  7300. 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  7301. 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  7302. 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  7303. 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  7304. 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  7305. 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  7306. 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  7307. 0x2d02ef8dL
  7308. };
  7309. /* yesno.c -- read a yes/no response from stdin
  7310. Copyright (C) 1990, 1998 Free Software Foundation, Inc.
  7311. This program is free software; you can redistribute it and/or modify
  7312. it under the terms of the GNU General Public License as published by
  7313. the Free Software Foundation; either version 2, or (at your option)
  7314. any later version.
  7315. This program is distributed in the hope that it will be useful,
  7316. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7317. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7318. GNU General Public License for more details.
  7319. You should have received a copy of the GNU General Public License
  7320. along with this program; if not, write to the Free Software Foundation,
  7321. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  7322. /* Read one line from standard input
  7323. and return nonzero if that line begins with y or Y,
  7324. otherwise return 0. */
  7325. int rpmatch ();
  7326. int
  7327. yesno ()
  7328. {
  7329. /* We make some assumptions here:
  7330. a) leading white space in the response are not vital
  7331. b) the first 128 characters of the answer are enough (the rest can
  7332. be ignored)
  7333. I cannot think for a situation where this is not ok. --drepper@gnu */
  7334. char buf[128];
  7335. int len = 0;
  7336. int c;
  7337. while ((c = getchar ()) != EOF && c != '\n')
  7338. if ((len > 0 && len < 127) || (len == 0 && !isspace (c)))
  7339. buf[len++] = c;
  7340. buf[len] = '\0';
  7341. return rpmatch (buf) == 1;
  7342. }
  7343. /* zip.c -- compress files to the gzip or pkzip format
  7344. * Copyright (C) 1992-1993 Jean-loup Gailly
  7345. * This is free software; you can redistribute it and/or modify it under the
  7346. * terms of the GNU General Public License, see the file COPYING.
  7347. */
  7348. #ifdef RCSID
  7349. static char rcsid[] = "$Id: zip.c,v 0.17 1993/06/10 13:29:25 jloup Exp $";
  7350. #endif
  7351. local ulg crc; /* crc on uncompressed file data */
  7352. off_t header_bytes; /* number of bytes in gzip header */
  7353. /* ===========================================================================
  7354. * Deflate in to out.
  7355. * IN assertions: the input and output buffers are cleared.
  7356. * The variables time_stamp and save_orig_name are initialized.
  7357. */
  7358. int zip(in, out)
  7359. int in, out; /* input and output file descriptors */
  7360. {
  7361. uch flags = 0; /* general purpose bit flags */
  7362. ush attr = 0; /* ascii/binary flag */
  7363. ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
  7364. ifd = in;
  7365. ofd = out;
  7366. outcnt = 0;
  7367. /* Write the header to the gzip file. See algorithm.doc for the format */
  7368. method = DEFLATED;
  7369. put_byte(GZIP_MAGIC[0]); /* magic header */
  7370. put_byte(GZIP_MAGIC[1]);
  7371. put_byte(DEFLATED); /* compression method */
  7372. if (save_orig_name) {
  7373. flags |= ORIG_NAME;
  7374. }
  7375. put_byte(flags); /* general flags */
  7376. put_long(time_stamp == (time_stamp & 0xffffffff)
  7377. ? (ulg)time_stamp : (ulg)0);
  7378. /* Write deflated file to zip file */
  7379. crc = updcrc(0, 0);
  7380. bi_init(out);
  7381. ct_init(&attr, &method);
  7382. lm_init(level, &deflate_flags);
  7383. put_byte((uch)deflate_flags); /* extra flags */
  7384. put_byte(OS_CODE); /* OS identifier */
  7385. if (save_orig_name) {
  7386. char *p = base_name(ifname); /* Don't save the directory part. */
  7387. do {
  7388. put_char(*p);
  7389. } while (*p++);
  7390. }
  7391. header_bytes = (off_t)outcnt;
  7392. (void)deflate();
  7393. #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
  7394. /* Check input size (but not in VMS -- variable record lengths mess it up)
  7395. * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
  7396. */
  7397. if (ifile_size != -1L && bytes_in != ifile_size) {
  7398. fprintf(stderr, "%s: %s: file size changed while zipping\n",
  7399. progname, ifname);
  7400. }
  7401. #endif
  7402. /* Write the crc and uncompressed size */
  7403. put_long(crc);
  7404. put_long((ulg)bytes_in);
  7405. header_bytes += 2*sizeof(long);
  7406. flush_outbuf();
  7407. return OK;
  7408. }
  7409. /* ===========================================================================
  7410. * Read a new buffer from the current input file, perform end-of-line
  7411. * translation, and update the crc and input file size.
  7412. * IN assertion: size >= 2 (for end-of-line translation)
  7413. */
  7414. int file_read(buf, size)
  7415. char *buf;
  7416. unsigned size;
  7417. {
  7418. unsigned len;
  7419. Assert(insize == 0, "inbuf not empty");
  7420. len = read(ifd, buf, size);
  7421. if (len == 0) return (int)len;
  7422. if (len == (unsigned)-1) {
  7423. read_error();
  7424. return EOF;
  7425. }
  7426. crc = updcrc((uch*)buf, len);
  7427. bytes_in += (off_t)len;
  7428. return (int)len;
  7429. }
  7430. /* Determine whether string value is affirmation or negative response
  7431. according to current locale's data.
  7432. Copyright (C) 1996, 1998, 2000 Free Software Foundation, Inc.
  7433. This program is free software; you can redistribute it and/or modify
  7434. it under the terms of the GNU General Public License as published by
  7435. the Free Software Foundation; either version 2, or (at your option)
  7436. any later version.
  7437. This program is distributed in the hope that it will be useful,
  7438. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7439. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7440. GNU General Public License for more details.
  7441. You should have received a copy of the GNU General Public License
  7442. along with this program; if not, write to the Free Software Foundation,
  7443. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  7444. int
  7445. rpmatch (const char *response)
  7446. {
  7447. /* Test against "^[yY]" and "^[nN]", hardcoded to avoid requiring regex */
  7448. return (*response == 'y' || *response == 'Y' ? 1
  7449. : *response == 'n' || *response == 'N' ? 0 : -1);
  7450. }
  7451. int
  7452. getopt_long (argc, argv, options, long_options, opt_index)
  7453. int argc;
  7454. char *const *argv;
  7455. const char *options;
  7456. const struct option *long_options;
  7457. int *opt_index;
  7458. {
  7459. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  7460. }
  7461. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  7462. If an option that starts with '-' (not '--') doesn't match a long option,
  7463. but does match a short option, it is parsed as a short option
  7464. instead. */
  7465. int
  7466. getopt_long_only (argc, argv, options, long_options, opt_index)
  7467. int argc;
  7468. char *const *argv;
  7469. const char *options;
  7470. const struct option *long_options;
  7471. int *opt_index;
  7472. {
  7473. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  7474. }