bzip2.c 200 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998
  1. /*-------------------------------------------------------------*/
  2. /*--- Public header file for the library. ---*/
  3. /*--- bzlib.h ---*/
  4. /*-------------------------------------------------------------*/
  5. /*--
  6. This file is a part of bzip2 and/or libbzip2, a program and
  7. library for lossless, block-sorting data compression.
  8. Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. 1. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. 2. The origin of this software must not be misrepresented; you must
  15. not claim that you wrote the original software. If you use this
  16. software in a product, an acknowledgment in the product
  17. documentation would be appreciated but is not required.
  18. 3. Altered source versions must be plainly marked as such, and must
  19. not be misrepresented as being the original software.
  20. 4. The name of the author may not be used to endorse or promote
  21. products derived from this software without specific prior written
  22. permission.
  23. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  24. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  27. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  29. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. Julian Seward, Cambridge, UK.
  35. jseward@acm.org
  36. bzip2/libbzip2 version 1.0 of 21 March 2000
  37. This program is based on (at least) the work of:
  38. Mike Burrows
  39. David Wheeler
  40. Peter Fenwick
  41. Alistair Moffat
  42. Radford Neal
  43. Ian H. Witten
  44. Robert Sedgewick
  45. Jon L. Bentley
  46. For more information on these sources, see the manual.
  47. --*/
  48. #ifndef _BZLIB_H
  49. #define _BZLIB_H
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #define BZ_RUN 0
  54. #define BZ_FLUSH 1
  55. #define BZ_FINISH 2
  56. #define BZ_OK 0
  57. #define BZ_RUN_OK 1
  58. #define BZ_FLUSH_OK 2
  59. #define BZ_FINISH_OK 3
  60. #define BZ_STREAM_END 4
  61. #define BZ_SEQUENCE_ERROR (-1)
  62. #define BZ_PARAM_ERROR (-2)
  63. #define BZ_MEM_ERROR (-3)
  64. #define BZ_DATA_ERROR (-4)
  65. #define BZ_DATA_ERROR_MAGIC (-5)
  66. #define BZ_IO_ERROR (-6)
  67. #define BZ_UNEXPECTED_EOF (-7)
  68. #define BZ_OUTBUFF_FULL (-8)
  69. #define BZ_CONFIG_ERROR (-9)
  70. typedef
  71. struct {
  72. char *next_in;
  73. unsigned int avail_in;
  74. unsigned int total_in_lo32;
  75. unsigned int total_in_hi32;
  76. char *next_out;
  77. unsigned int avail_out;
  78. unsigned int total_out_lo32;
  79. unsigned int total_out_hi32;
  80. void *state;
  81. void *(*bzalloc)(void *,int,int);
  82. void (*bzfree)(void *,void *);
  83. void *opaque;
  84. }
  85. bz_stream;
  86. #ifndef BZ_IMPORT
  87. #define BZ_EXPORT
  88. #endif
  89. /* Need a definitition for FILE */
  90. #include <stdio.h>
  91. #ifdef _WIN32
  92. # include <windows.h>
  93. # ifdef small
  94. /* windows.h define small to char */
  95. # undef small
  96. # endif
  97. # ifdef BZ_EXPORT
  98. # define BZ_API(func) WINAPI func
  99. # define BZ_EXTERN extern
  100. # else
  101. /* import windows dll dynamically */
  102. # define BZ_API(func) (WINAPI * func)
  103. # define BZ_EXTERN
  104. # endif
  105. #else
  106. # define BZ_API(func) func
  107. # define BZ_EXTERN extern
  108. #endif
  109. /*-- Core (low-level) library functions --*/
  110. BZ_EXTERN int BZ_API(BZ2_bzCompressInit) (
  111. bz_stream* strm,
  112. int blockSize100k,
  113. int verbosity,
  114. int workFactor
  115. );
  116. BZ_EXTERN int BZ_API(BZ2_bzCompress) (
  117. bz_stream* strm,
  118. int action
  119. );
  120. BZ_EXTERN int BZ_API(BZ2_bzCompressEnd) (
  121. bz_stream* strm
  122. );
  123. BZ_EXTERN int BZ_API(BZ2_bzDecompressInit) (
  124. bz_stream *strm,
  125. int verbosity,
  126. int small
  127. );
  128. BZ_EXTERN int BZ_API(BZ2_bzDecompress) (
  129. bz_stream* strm
  130. );
  131. BZ_EXTERN int BZ_API(BZ2_bzDecompressEnd) (
  132. bz_stream *strm
  133. );
  134. /*-- High(er) level library functions --*/
  135. #ifndef BZ_NO_STDIO
  136. #define BZ_MAX_UNUSED 5000
  137. typedef void BZFILE;
  138. BZ_EXTERN BZFILE* BZ_API(BZ2_bzReadOpen) (
  139. int* bzerror,
  140. FILE* f,
  141. int verbosity,
  142. int small,
  143. void* unused,
  144. int nUnused
  145. );
  146. BZ_EXTERN void BZ_API(BZ2_bzReadClose) (
  147. int* bzerror,
  148. BZFILE* b
  149. );
  150. BZ_EXTERN void BZ_API(BZ2_bzReadGetUnused) (
  151. int* bzerror,
  152. BZFILE* b,
  153. void** unused,
  154. int* nUnused
  155. );
  156. BZ_EXTERN int BZ_API(BZ2_bzRead) (
  157. int* bzerror,
  158. BZFILE* b,
  159. void* buf,
  160. int len
  161. );
  162. BZ_EXTERN BZFILE* BZ_API(BZ2_bzWriteOpen) (
  163. int* bzerror,
  164. FILE* f,
  165. int blockSize100k,
  166. int verbosity,
  167. int workFactor
  168. );
  169. BZ_EXTERN void BZ_API(BZ2_bzWrite) (
  170. int* bzerror,
  171. BZFILE* b,
  172. void* buf,
  173. int len
  174. );
  175. BZ_EXTERN void BZ_API(BZ2_bzWriteClose) (
  176. int* bzerror,
  177. BZFILE* b,
  178. int abandon,
  179. unsigned int* nbytes_in,
  180. unsigned int* nbytes_out
  181. );
  182. BZ_EXTERN void BZ_API(BZ2_bzWriteClose64) (
  183. int* bzerror,
  184. BZFILE* b,
  185. int abandon,
  186. unsigned int* nbytes_in_lo32,
  187. unsigned int* nbytes_in_hi32,
  188. unsigned int* nbytes_out_lo32,
  189. unsigned int* nbytes_out_hi32
  190. );
  191. #endif
  192. /*-- Utility functions --*/
  193. BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffCompress) (
  194. char* dest,
  195. unsigned int* destLen,
  196. char* source,
  197. unsigned int sourceLen,
  198. int blockSize100k,
  199. int verbosity,
  200. int workFactor
  201. );
  202. BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffDecompress) (
  203. char* dest,
  204. unsigned int* destLen,
  205. char* source,
  206. unsigned int sourceLen,
  207. int small,
  208. int verbosity
  209. );
  210. /*--
  211. Code contributed by Yoshioka Tsuneo
  212. (QWF00133@niftyserve.or.jp/tsuneo-y@is.aist-nara.ac.jp),
  213. to support better zlib compatibility.
  214. This code is not _officially_ part of libbzip2 (yet);
  215. I haven't tested it, documented it, or considered the
  216. threading-safeness of it.
  217. If this code breaks, please contact both Yoshioka and me.
  218. --*/
  219. BZ_EXTERN const char * BZ_API(BZ2_bzlibVersion) (
  220. void
  221. );
  222. #ifndef BZ_NO_STDIO
  223. BZ_EXTERN BZFILE * BZ_API(BZ2_bzopen) (
  224. const char *path,
  225. const char *mode
  226. );
  227. BZ_EXTERN BZFILE * BZ_API(BZ2_bzdopen) (
  228. int fd,
  229. const char *mode
  230. );
  231. BZ_EXTERN int BZ_API(BZ2_bzread) (
  232. BZFILE* b,
  233. void* buf,
  234. int len
  235. );
  236. BZ_EXTERN int BZ_API(BZ2_bzwrite) (
  237. BZFILE* b,
  238. void* buf,
  239. int len
  240. );
  241. BZ_EXTERN int BZ_API(BZ2_bzflush) (
  242. BZFILE* b
  243. );
  244. BZ_EXTERN void BZ_API(BZ2_bzclose) (
  245. BZFILE* b
  246. );
  247. BZ_EXTERN const char * BZ_API(BZ2_bzerror) (
  248. BZFILE *b,
  249. int *errnum
  250. );
  251. #endif
  252. #ifdef __cplusplus
  253. }
  254. #endif
  255. #endif
  256. /*-------------------------------------------------------------*/
  257. /*--- end bzlib.h ---*/
  258. /*-------------------------------------------------------------*/
  259. /*-------------------------------------------------------------*/
  260. /*--- Private header file for the library. ---*/
  261. /*--- bzlib_private.h ---*/
  262. /*-------------------------------------------------------------*/
  263. #ifndef _BZLIB_PRIVATE_H
  264. #define _BZLIB_PRIVATE_H
  265. #include <stdlib.h>
  266. #ifndef BZ_NO_STDIO
  267. #include <stdio.h>
  268. #include <ctype.h>
  269. #include <string.h>
  270. #endif
  271. /*-- General stuff. --*/
  272. #define BZ_VERSION "1.0.2, 30-Dec-2001"
  273. typedef char Char;
  274. typedef unsigned char Bool;
  275. typedef unsigned char UChar;
  276. typedef int Int32;
  277. typedef unsigned int UInt32;
  278. typedef short Int16;
  279. typedef unsigned short UInt16;
  280. #define True ((Bool)1)
  281. #define False ((Bool)0)
  282. #ifndef __GNUC__
  283. #define __inline__ /* */
  284. #endif
  285. #ifndef BZ_NO_STDIO
  286. extern void BZ2_bz__AssertH__fail ( int errcode );
  287. #define AssertH(cond,errcode) \
  288. { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); }
  289. #if BZ_DEBUG
  290. #define AssertD(cond,msg) \
  291. { if (!(cond)) { \
  292. fprintf ( stderr, \
  293. "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\
  294. exit(1); \
  295. }}
  296. #else
  297. #define AssertD(cond,msg) /* */
  298. #endif
  299. #define VPrintf0(zf) \
  300. fprintf(stderr,zf)
  301. #define VPrintf1(zf,za1) \
  302. fprintf(stderr,zf,za1)
  303. #define VPrintf2(zf,za1,za2) \
  304. fprintf(stderr,zf,za1,za2)
  305. #define VPrintf3(zf,za1,za2,za3) \
  306. fprintf(stderr,zf,za1,za2,za3)
  307. #define VPrintf4(zf,za1,za2,za3,za4) \
  308. fprintf(stderr,zf,za1,za2,za3,za4)
  309. #define VPrintf5(zf,za1,za2,za3,za4,za5) \
  310. fprintf(stderr,zf,za1,za2,za3,za4,za5)
  311. #else
  312. extern void bz_internal_error ( int errcode );
  313. #define AssertH(cond,errcode) \
  314. { if (!(cond)) bz_internal_error ( errcode ); }
  315. #define AssertD(cond,msg) /* */
  316. #define VPrintf0(zf) /* */
  317. #define VPrintf1(zf,za1) /* */
  318. #define VPrintf2(zf,za1,za2) /* */
  319. #define VPrintf3(zf,za1,za2,za3) /* */
  320. #define VPrintf4(zf,za1,za2,za3,za4) /* */
  321. #define VPrintf5(zf,za1,za2,za3,za4,za5) /* */
  322. #endif
  323. #define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
  324. #define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp))
  325. /*-- Header bytes. --*/
  326. #define BZ_HDR_B 0x42 /* 'B' */
  327. #define BZ_HDR_Z 0x5a /* 'Z' */
  328. #define BZ_HDR_h 0x68 /* 'h' */
  329. #define BZ_HDR_0 0x30 /* '0' */
  330. /*-- Constants for the back end. --*/
  331. #define BZ_MAX_ALPHA_SIZE 258
  332. #define BZ_MAX_CODE_LEN 23
  333. #define BZ_RUNA 0
  334. #define BZ_RUNB 1
  335. #define BZ_N_GROUPS 6
  336. #define BZ_G_SIZE 50
  337. #define BZ_N_ITERS 4
  338. #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
  339. /*-- Stuff for randomising repetitive blocks. --*/
  340. extern Int32 BZ2_rNums[512];
  341. #define BZ_RAND_DECLS \
  342. Int32 rNToGo; \
  343. Int32 rTPos \
  344. #define BZ_RAND_INIT_MASK \
  345. s->rNToGo = 0; \
  346. s->rTPos = 0 \
  347. #define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
  348. #define BZ_RAND_UPD_MASK \
  349. if (s->rNToGo == 0) { \
  350. s->rNToGo = BZ2_rNums[s->rTPos]; \
  351. s->rTPos++; \
  352. if (s->rTPos == 512) s->rTPos = 0; \
  353. } \
  354. s->rNToGo--;
  355. /*-- Stuff for doing CRCs. --*/
  356. extern UInt32 BZ2_crc32Table[256];
  357. #define BZ_INITIALISE_CRC(crcVar) \
  358. { \
  359. crcVar = 0xffffffffL; \
  360. }
  361. #define BZ_FINALISE_CRC(crcVar) \
  362. { \
  363. crcVar = ~(crcVar); \
  364. }
  365. #define BZ_UPDATE_CRC(crcVar,cha) \
  366. { \
  367. crcVar = (crcVar << 8) ^ \
  368. BZ2_crc32Table[(crcVar >> 24) ^ \
  369. ((UChar)cha)]; \
  370. }
  371. /*-- States and modes for compression. --*/
  372. #define BZ_M_IDLE 1
  373. #define BZ_M_RUNNING 2
  374. #define BZ_M_FLUSHING 3
  375. #define BZ_M_FINISHING 4
  376. #define BZ_S_OUTPUT 1
  377. #define BZ_S_INPUT 2
  378. #define BZ_N_RADIX 2
  379. #define BZ_N_QSORT 12
  380. #define BZ_N_SHELL 18
  381. #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
  382. /*-- Structure holding all the compression-side stuff. --*/
  383. typedef
  384. struct {
  385. /* pointer back to the struct bz_stream */
  386. bz_stream* strm;
  387. /* mode this stream is in, and whether inputting */
  388. /* or outputting data */
  389. Int32 mode;
  390. Int32 state;
  391. /* remembers avail_in when flush/finish requested */
  392. UInt32 avail_in_expect;
  393. /* for doing the block sorting */
  394. UInt32* arr1;
  395. UInt32* arr2;
  396. UInt32* ftab;
  397. Int32 origPtr;
  398. /* aliases for arr1 and arr2 */
  399. UInt32* ptr;
  400. UChar* block;
  401. UInt16* mtfv;
  402. UChar* zbits;
  403. /* for deciding when to use the fallback sorting algorithm */
  404. Int32 workFactor;
  405. /* run-length-encoding of the input */
  406. UInt32 state_in_ch;
  407. Int32 state_in_len;
  408. BZ_RAND_DECLS;
  409. /* input and output limits and current posns */
  410. Int32 nblock;
  411. Int32 nblockMAX;
  412. Int32 numZ;
  413. Int32 state_out_pos;
  414. /* map of bytes used in block */
  415. Int32 nInUse;
  416. Bool inUse[256];
  417. UChar unseqToSeq[256];
  418. /* the buffer for bit stream creation */
  419. UInt32 bsBuff;
  420. Int32 bsLive;
  421. /* block and combined CRCs */
  422. UInt32 blockCRC;
  423. UInt32 combinedCRC;
  424. /* misc administratium */
  425. Int32 verbosity;
  426. Int32 blockNo;
  427. Int32 blockSize100k;
  428. /* stuff for coding the MTF values */
  429. Int32 nMTF;
  430. Int32 mtfFreq [BZ_MAX_ALPHA_SIZE];
  431. UChar selector [BZ_MAX_SELECTORS];
  432. UChar selectorMtf[BZ_MAX_SELECTORS];
  433. UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  434. Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  435. Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  436. /* second dimension: only 3 needed; 4 makes index calculations faster */
  437. UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4];
  438. }
  439. EState;
  440. /*-- externs for compression. --*/
  441. extern void
  442. BZ2_blockSort ( EState* );
  443. extern void
  444. BZ2_compressBlock ( EState*, Bool );
  445. extern void
  446. BZ2_bsInitWrite ( EState* );
  447. extern void
  448. BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
  449. extern void
  450. BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
  451. /*-- states for decompression. --*/
  452. #define BZ_X_IDLE 1
  453. #define BZ_X_OUTPUT 2
  454. #define BZ_X_MAGIC_1 10
  455. #define BZ_X_MAGIC_2 11
  456. #define BZ_X_MAGIC_3 12
  457. #define BZ_X_MAGIC_4 13
  458. #define BZ_X_BLKHDR_1 14
  459. #define BZ_X_BLKHDR_2 15
  460. #define BZ_X_BLKHDR_3 16
  461. #define BZ_X_BLKHDR_4 17
  462. #define BZ_X_BLKHDR_5 18
  463. #define BZ_X_BLKHDR_6 19
  464. #define BZ_X_BCRC_1 20
  465. #define BZ_X_BCRC_2 21
  466. #define BZ_X_BCRC_3 22
  467. #define BZ_X_BCRC_4 23
  468. #define BZ_X_RANDBIT 24
  469. #define BZ_X_ORIGPTR_1 25
  470. #define BZ_X_ORIGPTR_2 26
  471. #define BZ_X_ORIGPTR_3 27
  472. #define BZ_X_MAPPING_1 28
  473. #define BZ_X_MAPPING_2 29
  474. #define BZ_X_SELECTOR_1 30
  475. #define BZ_X_SELECTOR_2 31
  476. #define BZ_X_SELECTOR_3 32
  477. #define BZ_X_CODING_1 33
  478. #define BZ_X_CODING_2 34
  479. #define BZ_X_CODING_3 35
  480. #define BZ_X_MTF_1 36
  481. #define BZ_X_MTF_2 37
  482. #define BZ_X_MTF_3 38
  483. #define BZ_X_MTF_4 39
  484. #define BZ_X_MTF_5 40
  485. #define BZ_X_MTF_6 41
  486. #define BZ_X_ENDHDR_2 42
  487. #define BZ_X_ENDHDR_3 43
  488. #define BZ_X_ENDHDR_4 44
  489. #define BZ_X_ENDHDR_5 45
  490. #define BZ_X_ENDHDR_6 46
  491. #define BZ_X_CCRC_1 47
  492. #define BZ_X_CCRC_2 48
  493. #define BZ_X_CCRC_3 49
  494. #define BZ_X_CCRC_4 50
  495. /*-- Constants for the fast MTF decoder. --*/
  496. #define MTFA_SIZE 4096
  497. #define MTFL_SIZE 16
  498. /*-- Structure holding all the decompression-side stuff. --*/
  499. typedef
  500. struct {
  501. /* pointer back to the struct bz_stream */
  502. bz_stream* strm;
  503. /* state indicator for this stream */
  504. Int32 state;
  505. /* for doing the final run-length decoding */
  506. UChar state_out_ch;
  507. Int32 state_out_len;
  508. Bool blockRandomised;
  509. BZ_RAND_DECLS;
  510. /* the buffer for bit stream reading */
  511. UInt32 bsBuff;
  512. Int32 bsLive;
  513. /* misc administratium */
  514. Int32 blockSize100k;
  515. Bool smallDecompress;
  516. Int32 currBlockNo;
  517. Int32 verbosity;
  518. /* for undoing the Burrows-Wheeler transform */
  519. Int32 origPtr;
  520. UInt32 tPos;
  521. Int32 k0;
  522. Int32 unzftab[256];
  523. Int32 nblock_used;
  524. Int32 cftab[257];
  525. Int32 cftabCopy[257];
  526. /* for undoing the Burrows-Wheeler transform (FAST) */
  527. UInt32 *tt;
  528. /* for undoing the Burrows-Wheeler transform (SMALL) */
  529. UInt16 *ll16;
  530. UChar *ll4;
  531. /* stored and calculated CRCs */
  532. UInt32 storedBlockCRC;
  533. UInt32 storedCombinedCRC;
  534. UInt32 calculatedBlockCRC;
  535. UInt32 calculatedCombinedCRC;
  536. /* map of bytes used in block */
  537. Int32 nInUse;
  538. Bool inUse[256];
  539. Bool inUse16[16];
  540. UChar seqToUnseq[256];
  541. /* for decoding the MTF values */
  542. UChar mtfa [MTFA_SIZE];
  543. Int32 mtfbase[256 / MTFL_SIZE];
  544. UChar selector [BZ_MAX_SELECTORS];
  545. UChar selectorMtf[BZ_MAX_SELECTORS];
  546. UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  547. Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  548. Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  549. Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  550. Int32 minLens[BZ_N_GROUPS];
  551. /* save area for scalars in the main decompress code */
  552. Int32 save_i;
  553. Int32 save_j;
  554. Int32 save_t;
  555. Int32 save_alphaSize;
  556. Int32 save_nGroups;
  557. Int32 save_nSelectors;
  558. Int32 save_EOB;
  559. Int32 save_groupNo;
  560. Int32 save_groupPos;
  561. Int32 save_nextSym;
  562. Int32 save_nblockMAX;
  563. Int32 save_nblock;
  564. Int32 save_es;
  565. Int32 save_N;
  566. Int32 save_curr;
  567. Int32 save_zt;
  568. Int32 save_zn;
  569. Int32 save_zvec;
  570. Int32 save_zj;
  571. Int32 save_gSel;
  572. Int32 save_gMinlen;
  573. Int32* save_gLimit;
  574. Int32* save_gBase;
  575. Int32* save_gPerm;
  576. }
  577. DState;
  578. /*-- Macros for decompression. --*/
  579. #define BZ_GET_FAST(cccc) \
  580. s->tPos = s->tt[s->tPos]; \
  581. cccc = (UChar)(s->tPos & 0xff); \
  582. s->tPos >>= 8;
  583. #define BZ_GET_FAST_C(cccc) \
  584. c_tPos = c_tt[c_tPos]; \
  585. cccc = (UChar)(c_tPos & 0xff); \
  586. c_tPos >>= 8;
  587. #define SET_LL4(i,n) \
  588. { if (((i) & 0x1) == 0) \
  589. s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \
  590. s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
  591. }
  592. #define GET_LL4(i) \
  593. ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
  594. #define SET_LL(i,n) \
  595. { s->ll16[i] = (UInt16)(n & 0x0000ffff); \
  596. SET_LL4(i, n >> 16); \
  597. }
  598. #define GET_LL(i) \
  599. (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
  600. #define BZ_GET_SMALL(cccc) \
  601. cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \
  602. s->tPos = GET_LL(s->tPos);
  603. /*-- externs for decompression. --*/
  604. extern Int32
  605. BZ2_indexIntoF ( Int32, Int32* );
  606. extern Int32
  607. BZ2_decompress ( DState* );
  608. extern void
  609. BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
  610. Int32, Int32, Int32 );
  611. #endif
  612. /*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
  613. #ifdef BZ_NO_STDIO
  614. #ifndef NULL
  615. #define NULL 0
  616. #endif
  617. #endif
  618. /*-------------------------------------------------------------*/
  619. /*--- end bzlib_private.h ---*/
  620. /*-------------------------------------------------------------*/
  621. /*-------------------------------------------------------------*/
  622. /*--- Block sorting machinery ---*/
  623. /*--- blocksort.c ---*/
  624. /*-------------------------------------------------------------*/
  625. /*---------------------------------------------*/
  626. /*--- Fallback O(N log(N)^2) sorting ---*/
  627. /*--- algorithm, for repetitive blocks ---*/
  628. /*---------------------------------------------*/
  629. /*---------------------------------------------*/
  630. static
  631. __inline__
  632. void fallbackSimpleSort ( UInt32* fmap,
  633. UInt32* eclass,
  634. Int32 lo,
  635. Int32 hi )
  636. {
  637. Int32 i, j, tmp;
  638. UInt32 ec_tmp;
  639. if (lo == hi) return;
  640. if (hi - lo > 3) {
  641. for ( i = hi-4; i >= lo; i-- ) {
  642. tmp = fmap[i];
  643. ec_tmp = eclass[tmp];
  644. for ( j = i+4; j <= hi && ec_tmp > eclass[fmap[j]]; j += 4 )
  645. fmap[j-4] = fmap[j];
  646. fmap[j-4] = tmp;
  647. }
  648. }
  649. for ( i = hi-1; i >= lo; i-- ) {
  650. tmp = fmap[i];
  651. ec_tmp = eclass[tmp];
  652. for ( j = i+1; j <= hi && ec_tmp > eclass[fmap[j]]; j++ )
  653. fmap[j-1] = fmap[j];
  654. fmap[j-1] = tmp;
  655. }
  656. }
  657. /*---------------------------------------------*/
  658. #define fswap(zz1, zz2) \
  659. { Int32 zztmp = zz1; zz1 = zz2; zz2 = zztmp; }
  660. #define fvswap(zzp1, zzp2, zzn) \
  661. { \
  662. Int32 yyp1 = (zzp1); \
  663. Int32 yyp2 = (zzp2); \
  664. Int32 yyn = (zzn); \
  665. while (yyn > 0) { \
  666. fswap(fmap[yyp1], fmap[yyp2]); \
  667. yyp1++; yyp2++; yyn--; \
  668. } \
  669. }
  670. #define fmin(a,b) ((a) < (b)) ? (a) : (b)
  671. #define fpush(lz,hz) { stackLo[sp] = lz; \
  672. stackHi[sp] = hz; \
  673. sp++; }
  674. #define fpop(lz,hz) { sp--; \
  675. lz = stackLo[sp]; \
  676. hz = stackHi[sp]; }
  677. #define FALLBACK_QSORT_SMALL_THRESH 10
  678. #define FALLBACK_QSORT_STACK_SIZE 100
  679. static
  680. void fallbackQSort3 ( UInt32* fmap,
  681. UInt32* eclass,
  682. Int32 loSt,
  683. Int32 hiSt )
  684. {
  685. Int32 unLo, unHi, ltLo, gtHi, n, m;
  686. Int32 sp, lo, hi;
  687. UInt32 med, r, r3;
  688. Int32 stackLo[FALLBACK_QSORT_STACK_SIZE];
  689. Int32 stackHi[FALLBACK_QSORT_STACK_SIZE];
  690. r = 0;
  691. sp = 0;
  692. fpush ( loSt, hiSt );
  693. while (sp > 0) {
  694. AssertH ( sp < FALLBACK_QSORT_STACK_SIZE, 1004 );
  695. fpop ( lo, hi );
  696. if (hi - lo < FALLBACK_QSORT_SMALL_THRESH) {
  697. fallbackSimpleSort ( fmap, eclass, lo, hi );
  698. continue;
  699. }
  700. /* Random partitioning. Median of 3 sometimes fails to
  701. avoid bad cases. Median of 9 seems to help but
  702. looks rather expensive. This too seems to work but
  703. is cheaper. Guidance for the magic constants
  704. 7621 and 32768 is taken from Sedgewick's algorithms
  705. book, chapter 35.
  706. */
  707. r = ((r * 7621) + 1) % 32768;
  708. r3 = r % 3;
  709. if (r3 == 0) med = eclass[fmap[lo]]; else
  710. if (r3 == 1) med = eclass[fmap[(lo+hi)>>1]]; else
  711. med = eclass[fmap[hi]];
  712. unLo = ltLo = lo;
  713. unHi = gtHi = hi;
  714. while (1) {
  715. while (1) {
  716. if (unLo > unHi) break;
  717. n = (Int32)eclass[fmap[unLo]] - (Int32)med;
  718. if (n == 0) {
  719. fswap(fmap[unLo], fmap[ltLo]);
  720. ltLo++; unLo++;
  721. continue;
  722. };
  723. if (n > 0) break;
  724. unLo++;
  725. }
  726. while (1) {
  727. if (unLo > unHi) break;
  728. n = (Int32)eclass[fmap[unHi]] - (Int32)med;
  729. if (n == 0) {
  730. fswap(fmap[unHi], fmap[gtHi]);
  731. gtHi--; unHi--;
  732. continue;
  733. };
  734. if (n < 0) break;
  735. unHi--;
  736. }
  737. if (unLo > unHi) break;
  738. fswap(fmap[unLo], fmap[unHi]); unLo++; unHi--;
  739. }
  740. AssertD ( unHi == unLo-1, "fallbackQSort3(2)" );
  741. if (gtHi < ltLo) continue;
  742. n = fmin(ltLo-lo, unLo-ltLo); fvswap(lo, unLo-n, n);
  743. m = fmin(hi-gtHi, gtHi-unHi); fvswap(unLo, hi-m+1, m);
  744. n = lo + unLo - ltLo - 1;
  745. m = hi - (gtHi - unHi) + 1;
  746. if (n - lo > hi - m) {
  747. fpush ( lo, n );
  748. fpush ( m, hi );
  749. } else {
  750. fpush ( m, hi );
  751. fpush ( lo, n );
  752. }
  753. }
  754. }
  755. #undef fmin
  756. #undef fpush
  757. #undef fpop
  758. #undef fswap
  759. #undef fvswap
  760. #undef FALLBACK_QSORT_SMALL_THRESH
  761. #undef FALLBACK_QSORT_STACK_SIZE
  762. /*---------------------------------------------*/
  763. /* Pre:
  764. nblock > 0
  765. eclass exists for [0 .. nblock-1]
  766. ((UChar*)eclass) [0 .. nblock-1] holds block
  767. ptr exists for [0 .. nblock-1]
  768. Post:
  769. ((UChar*)eclass) [0 .. nblock-1] holds block
  770. All other areas of eclass destroyed
  771. fmap [0 .. nblock-1] holds sorted order
  772. bhtab [ 0 .. 2+(nblock/32) ] destroyed
  773. */
  774. #define SET_BH(zz) bhtab[(zz) >> 5] |= (1 << ((zz) & 31))
  775. #define CLEAR_BH(zz) bhtab[(zz) >> 5] &= ~(1 << ((zz) & 31))
  776. #define ISSET_BH(zz) (bhtab[(zz) >> 5] & (1 << ((zz) & 31)))
  777. #define WORD_BH(zz) bhtab[(zz) >> 5]
  778. #define UNALIGNED_BH(zz) ((zz) & 0x01f)
  779. static
  780. void fallbackSort ( UInt32* fmap,
  781. UInt32* eclass,
  782. UInt32* bhtab,
  783. Int32 nblock,
  784. Int32 verb )
  785. {
  786. Int32 ftab[257];
  787. Int32 ftabCopy[256];
  788. Int32 H, i, j, k, l, r, cc, cc1;
  789. Int32 nNotDone;
  790. Int32 nBhtab;
  791. UChar* eclass8 = (UChar*)eclass;
  792. /*--
  793. Initial 1-char radix sort to generate
  794. initial fmap and initial BH bits.
  795. --*/
  796. if (verb >= 4)
  797. VPrintf0 ( " bucket sorting ...\n" );
  798. for (i = 0; i < 257; i++) ftab[i] = 0;
  799. for (i = 0; i < nblock; i++) ftab[eclass8[i]]++;
  800. for (i = 0; i < 256; i++) ftabCopy[i] = ftab[i];
  801. for (i = 1; i < 257; i++) ftab[i] += ftab[i-1];
  802. for (i = 0; i < nblock; i++) {
  803. j = eclass8[i];
  804. k = ftab[j] - 1;
  805. ftab[j] = k;
  806. fmap[k] = i;
  807. }
  808. nBhtab = 2 + (nblock / 32);
  809. for (i = 0; i < nBhtab; i++) bhtab[i] = 0;
  810. for (i = 0; i < 256; i++) SET_BH(ftab[i]);
  811. /*--
  812. Inductively refine the buckets. Kind-of an
  813. "exponential radix sort" (!), inspired by the
  814. Manber-Myers suffix array construction algorithm.
  815. --*/
  816. /*-- set sentinel bits for block-end detection --*/
  817. for (i = 0; i < 32; i++) {
  818. SET_BH(nblock + 2*i);
  819. CLEAR_BH(nblock + 2*i + 1);
  820. }
  821. /*-- the log(N) loop --*/
  822. H = 1;
  823. while (1) {
  824. if (verb >= 4)
  825. VPrintf1 ( " depth %6d has ", H );
  826. j = 0;
  827. for (i = 0; i < nblock; i++) {
  828. if (ISSET_BH(i)) j = i;
  829. k = fmap[i] - H; if (k < 0) k += nblock;
  830. eclass[k] = j;
  831. }
  832. nNotDone = 0;
  833. r = -1;
  834. while (1) {
  835. /*-- find the next non-singleton bucket --*/
  836. k = r + 1;
  837. while (ISSET_BH(k) && UNALIGNED_BH(k)) k++;
  838. if (ISSET_BH(k)) {
  839. while (WORD_BH(k) == 0xffffffff) k += 32;
  840. while (ISSET_BH(k)) k++;
  841. }
  842. l = k - 1;
  843. if (l >= nblock) break;
  844. while (!ISSET_BH(k) && UNALIGNED_BH(k)) k++;
  845. if (!ISSET_BH(k)) {
  846. while (WORD_BH(k) == 0x00000000) k += 32;
  847. while (!ISSET_BH(k)) k++;
  848. }
  849. r = k - 1;
  850. if (r >= nblock) break;
  851. /*-- now [l, r] bracket current bucket --*/
  852. if (r > l) {
  853. nNotDone += (r - l + 1);
  854. fallbackQSort3 ( fmap, eclass, l, r );
  855. /*-- scan bucket and generate header bits-- */
  856. cc = -1;
  857. for (i = l; i <= r; i++) {
  858. cc1 = eclass[fmap[i]];
  859. if (cc != cc1) { SET_BH(i); cc = cc1; };
  860. }
  861. }
  862. }
  863. if (verb >= 4)
  864. VPrintf1 ( "%6d unresolved strings\n", nNotDone );
  865. H *= 2;
  866. if (H > nblock || nNotDone == 0) break;
  867. }
  868. /*--
  869. Reconstruct the original block in
  870. eclass8 [0 .. nblock-1], since the
  871. previous phase destroyed it.
  872. --*/
  873. if (verb >= 4)
  874. VPrintf0 ( " reconstructing block ...\n" );
  875. j = 0;
  876. for (i = 0; i < nblock; i++) {
  877. while (ftabCopy[j] == 0) j++;
  878. ftabCopy[j]--;
  879. eclass8[fmap[i]] = (UChar)j;
  880. }
  881. AssertH ( j < 256, 1005 );
  882. }
  883. #undef SET_BH
  884. #undef CLEAR_BH
  885. #undef ISSET_BH
  886. #undef WORD_BH
  887. #undef UNALIGNED_BH
  888. /*---------------------------------------------*/
  889. /*--- The main, O(N^2 log(N)) sorting ---*/
  890. /*--- algorithm. Faster for "normal" ---*/
  891. /*--- non-repetitive blocks. ---*/
  892. /*---------------------------------------------*/
  893. /*---------------------------------------------*/
  894. static
  895. __inline__
  896. Bool mainGtU ( UInt32 i1,
  897. UInt32 i2,
  898. UChar* block,
  899. UInt16* quadrant,
  900. UInt32 nblock,
  901. Int32* budget )
  902. {
  903. Int32 k;
  904. UChar c1, c2;
  905. UInt16 s1, s2;
  906. AssertD ( i1 != i2, "mainGtU" );
  907. /* 1 */
  908. c1 = block[i1]; c2 = block[i2];
  909. if (c1 != c2) return (c1 > c2);
  910. i1++; i2++;
  911. /* 2 */
  912. c1 = block[i1]; c2 = block[i2];
  913. if (c1 != c2) return (c1 > c2);
  914. i1++; i2++;
  915. /* 3 */
  916. c1 = block[i1]; c2 = block[i2];
  917. if (c1 != c2) return (c1 > c2);
  918. i1++; i2++;
  919. /* 4 */
  920. c1 = block[i1]; c2 = block[i2];
  921. if (c1 != c2) return (c1 > c2);
  922. i1++; i2++;
  923. /* 5 */
  924. c1 = block[i1]; c2 = block[i2];
  925. if (c1 != c2) return (c1 > c2);
  926. i1++; i2++;
  927. /* 6 */
  928. c1 = block[i1]; c2 = block[i2];
  929. if (c1 != c2) return (c1 > c2);
  930. i1++; i2++;
  931. /* 7 */
  932. c1 = block[i1]; c2 = block[i2];
  933. if (c1 != c2) return (c1 > c2);
  934. i1++; i2++;
  935. /* 8 */
  936. c1 = block[i1]; c2 = block[i2];
  937. if (c1 != c2) return (c1 > c2);
  938. i1++; i2++;
  939. /* 9 */
  940. c1 = block[i1]; c2 = block[i2];
  941. if (c1 != c2) return (c1 > c2);
  942. i1++; i2++;
  943. /* 10 */
  944. c1 = block[i1]; c2 = block[i2];
  945. if (c1 != c2) return (c1 > c2);
  946. i1++; i2++;
  947. /* 11 */
  948. c1 = block[i1]; c2 = block[i2];
  949. if (c1 != c2) return (c1 > c2);
  950. i1++; i2++;
  951. /* 12 */
  952. c1 = block[i1]; c2 = block[i2];
  953. if (c1 != c2) return (c1 > c2);
  954. i1++; i2++;
  955. k = nblock + 8;
  956. do {
  957. /* 1 */
  958. c1 = block[i1]; c2 = block[i2];
  959. if (c1 != c2) return (c1 > c2);
  960. s1 = quadrant[i1]; s2 = quadrant[i2];
  961. if (s1 != s2) return (s1 > s2);
  962. i1++; i2++;
  963. /* 2 */
  964. c1 = block[i1]; c2 = block[i2];
  965. if (c1 != c2) return (c1 > c2);
  966. s1 = quadrant[i1]; s2 = quadrant[i2];
  967. if (s1 != s2) return (s1 > s2);
  968. i1++; i2++;
  969. /* 3 */
  970. c1 = block[i1]; c2 = block[i2];
  971. if (c1 != c2) return (c1 > c2);
  972. s1 = quadrant[i1]; s2 = quadrant[i2];
  973. if (s1 != s2) return (s1 > s2);
  974. i1++; i2++;
  975. /* 4 */
  976. c1 = block[i1]; c2 = block[i2];
  977. if (c1 != c2) return (c1 > c2);
  978. s1 = quadrant[i1]; s2 = quadrant[i2];
  979. if (s1 != s2) return (s1 > s2);
  980. i1++; i2++;
  981. /* 5 */
  982. c1 = block[i1]; c2 = block[i2];
  983. if (c1 != c2) return (c1 > c2);
  984. s1 = quadrant[i1]; s2 = quadrant[i2];
  985. if (s1 != s2) return (s1 > s2);
  986. i1++; i2++;
  987. /* 6 */
  988. c1 = block[i1]; c2 = block[i2];
  989. if (c1 != c2) return (c1 > c2);
  990. s1 = quadrant[i1]; s2 = quadrant[i2];
  991. if (s1 != s2) return (s1 > s2);
  992. i1++; i2++;
  993. /* 7 */
  994. c1 = block[i1]; c2 = block[i2];
  995. if (c1 != c2) return (c1 > c2);
  996. s1 = quadrant[i1]; s2 = quadrant[i2];
  997. if (s1 != s2) return (s1 > s2);
  998. i1++; i2++;
  999. /* 8 */
  1000. c1 = block[i1]; c2 = block[i2];
  1001. if (c1 != c2) return (c1 > c2);
  1002. s1 = quadrant[i1]; s2 = quadrant[i2];
  1003. if (s1 != s2) return (s1 > s2);
  1004. i1++; i2++;
  1005. if (i1 >= nblock) i1 -= nblock;
  1006. if (i2 >= nblock) i2 -= nblock;
  1007. k -= 8;
  1008. (*budget)--;
  1009. }
  1010. while (k >= 0);
  1011. return False;
  1012. }
  1013. /*---------------------------------------------*/
  1014. /*--
  1015. Knuth's increments seem to work better
  1016. than Incerpi-Sedgewick here. Possibly
  1017. because the number of elems to sort is
  1018. usually small, typically <= 20.
  1019. --*/
  1020. static
  1021. Int32 incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280,
  1022. 9841, 29524, 88573, 265720,
  1023. 797161, 2391484 };
  1024. static
  1025. void mainSimpleSort ( UInt32* ptr,
  1026. UChar* block,
  1027. UInt16* quadrant,
  1028. Int32 nblock,
  1029. Int32 lo,
  1030. Int32 hi,
  1031. Int32 d,
  1032. Int32* budget )
  1033. {
  1034. Int32 i, j, h, bigN, hp;
  1035. UInt32 v;
  1036. bigN = hi - lo + 1;
  1037. if (bigN < 2) return;
  1038. hp = 0;
  1039. while (incs[hp] < bigN) hp++;
  1040. hp--;
  1041. for (; hp >= 0; hp--) {
  1042. h = incs[hp];
  1043. i = lo + h;
  1044. while (True) {
  1045. /*-- copy 1 --*/
  1046. if (i > hi) break;
  1047. v = ptr[i];
  1048. j = i;
  1049. while ( mainGtU (
  1050. ptr[j-h]+d, v+d, block, quadrant, nblock, budget
  1051. ) ) {
  1052. ptr[j] = ptr[j-h];
  1053. j = j - h;
  1054. if (j <= (lo + h - 1)) break;
  1055. }
  1056. ptr[j] = v;
  1057. i++;
  1058. /*-- copy 2 --*/
  1059. if (i > hi) break;
  1060. v = ptr[i];
  1061. j = i;
  1062. while ( mainGtU (
  1063. ptr[j-h]+d, v+d, block, quadrant, nblock, budget
  1064. ) ) {
  1065. ptr[j] = ptr[j-h];
  1066. j = j - h;
  1067. if (j <= (lo + h - 1)) break;
  1068. }
  1069. ptr[j] = v;
  1070. i++;
  1071. /*-- copy 3 --*/
  1072. if (i > hi) break;
  1073. v = ptr[i];
  1074. j = i;
  1075. while ( mainGtU (
  1076. ptr[j-h]+d, v+d, block, quadrant, nblock, budget
  1077. ) ) {
  1078. ptr[j] = ptr[j-h];
  1079. j = j - h;
  1080. if (j <= (lo + h - 1)) break;
  1081. }
  1082. ptr[j] = v;
  1083. i++;
  1084. if (*budget < 0) return;
  1085. }
  1086. }
  1087. }
  1088. /*---------------------------------------------*/
  1089. /*--
  1090. The following is an implementation of
  1091. an elegant 3-way quicksort for strings,
  1092. described in a paper "Fast Algorithms for
  1093. Sorting and Searching Strings", by Robert
  1094. Sedgewick and Jon L. Bentley.
  1095. --*/
  1096. #define mswap(zz1, zz2) \
  1097. { Int32 zztmp = zz1; zz1 = zz2; zz2 = zztmp; }
  1098. #define mvswap(zzp1, zzp2, zzn) \
  1099. { \
  1100. Int32 yyp1 = (zzp1); \
  1101. Int32 yyp2 = (zzp2); \
  1102. Int32 yyn = (zzn); \
  1103. while (yyn > 0) { \
  1104. mswap(ptr[yyp1], ptr[yyp2]); \
  1105. yyp1++; yyp2++; yyn--; \
  1106. } \
  1107. }
  1108. static
  1109. __inline__
  1110. UChar mmed3 ( UChar a, UChar b, UChar c )
  1111. {
  1112. UChar t;
  1113. if (a > b) { t = a; a = b; b = t; };
  1114. if (b > c) {
  1115. b = c;
  1116. if (a > b) b = a;
  1117. }
  1118. return b;
  1119. }
  1120. #define mmin(a,b) ((a) < (b)) ? (a) : (b)
  1121. #define mpush(lz,hz,dz) { stackLo[sp] = lz; \
  1122. stackHi[sp] = hz; \
  1123. stackD [sp] = dz; \
  1124. sp++; }
  1125. #define mpop(lz,hz,dz) { sp--; \
  1126. lz = stackLo[sp]; \
  1127. hz = stackHi[sp]; \
  1128. dz = stackD [sp]; }
  1129. #define mnextsize(az) (nextHi[az]-nextLo[az])
  1130. #define mnextswap(az,bz) \
  1131. { Int32 tz; \
  1132. tz = nextLo[az]; nextLo[az] = nextLo[bz]; nextLo[bz] = tz; \
  1133. tz = nextHi[az]; nextHi[az] = nextHi[bz]; nextHi[bz] = tz; \
  1134. tz = nextD [az]; nextD [az] = nextD [bz]; nextD [bz] = tz; }
  1135. #define MAIN_QSORT_SMALL_THRESH 20
  1136. #define MAIN_QSORT_DEPTH_THRESH (BZ_N_RADIX + BZ_N_QSORT)
  1137. #define MAIN_QSORT_STACK_SIZE 100
  1138. static
  1139. void mainQSort3 ( UInt32* ptr,
  1140. UChar* block,
  1141. UInt16* quadrant,
  1142. Int32 nblock,
  1143. Int32 loSt,
  1144. Int32 hiSt,
  1145. Int32 dSt,
  1146. Int32* budget )
  1147. {
  1148. Int32 unLo, unHi, ltLo, gtHi, n, m, med;
  1149. Int32 sp, lo, hi, d;
  1150. Int32 stackLo[MAIN_QSORT_STACK_SIZE];
  1151. Int32 stackHi[MAIN_QSORT_STACK_SIZE];
  1152. Int32 stackD [MAIN_QSORT_STACK_SIZE];
  1153. Int32 nextLo[3];
  1154. Int32 nextHi[3];
  1155. Int32 nextD [3];
  1156. sp = 0;
  1157. mpush ( loSt, hiSt, dSt );
  1158. while (sp > 0) {
  1159. AssertH ( sp < MAIN_QSORT_STACK_SIZE, 1001 );
  1160. mpop ( lo, hi, d );
  1161. if (hi - lo < MAIN_QSORT_SMALL_THRESH ||
  1162. d > MAIN_QSORT_DEPTH_THRESH) {
  1163. mainSimpleSort ( ptr, block, quadrant, nblock, lo, hi, d, budget );
  1164. if (*budget < 0) return;
  1165. continue;
  1166. }
  1167. med = (Int32)
  1168. mmed3 ( block[ptr[ lo ]+d],
  1169. block[ptr[ hi ]+d],
  1170. block[ptr[ (lo+hi)>>1 ]+d] );
  1171. unLo = ltLo = lo;
  1172. unHi = gtHi = hi;
  1173. while (True) {
  1174. while (True) {
  1175. if (unLo > unHi) break;
  1176. n = ((Int32)block[ptr[unLo]+d]) - med;
  1177. if (n == 0) {
  1178. mswap(ptr[unLo], ptr[ltLo]);
  1179. ltLo++; unLo++; continue;
  1180. };
  1181. if (n > 0) break;
  1182. unLo++;
  1183. }
  1184. while (True) {
  1185. if (unLo > unHi) break;
  1186. n = ((Int32)block[ptr[unHi]+d]) - med;
  1187. if (n == 0) {
  1188. mswap(ptr[unHi], ptr[gtHi]);
  1189. gtHi--; unHi--; continue;
  1190. };
  1191. if (n < 0) break;
  1192. unHi--;
  1193. }
  1194. if (unLo > unHi) break;
  1195. mswap(ptr[unLo], ptr[unHi]); unLo++; unHi--;
  1196. }
  1197. AssertD ( unHi == unLo-1, "mainQSort3(2)" );
  1198. if (gtHi < ltLo) {
  1199. mpush(lo, hi, d+1 );
  1200. continue;
  1201. }
  1202. n = mmin(ltLo-lo, unLo-ltLo); mvswap(lo, unLo-n, n);
  1203. m = mmin(hi-gtHi, gtHi-unHi); mvswap(unLo, hi-m+1, m);
  1204. n = lo + unLo - ltLo - 1;
  1205. m = hi - (gtHi - unHi) + 1;
  1206. nextLo[0] = lo; nextHi[0] = n; nextD[0] = d;
  1207. nextLo[1] = m; nextHi[1] = hi; nextD[1] = d;
  1208. nextLo[2] = n+1; nextHi[2] = m-1; nextD[2] = d+1;
  1209. if (mnextsize(0) < mnextsize(1)) mnextswap(0,1);
  1210. if (mnextsize(1) < mnextsize(2)) mnextswap(1,2);
  1211. if (mnextsize(0) < mnextsize(1)) mnextswap(0,1);
  1212. AssertD (mnextsize(0) >= mnextsize(1), "mainQSort3(8)" );
  1213. AssertD (mnextsize(1) >= mnextsize(2), "mainQSort3(9)" );
  1214. mpush (nextLo[0], nextHi[0], nextD[0]);
  1215. mpush (nextLo[1], nextHi[1], nextD[1]);
  1216. mpush (nextLo[2], nextHi[2], nextD[2]);
  1217. }
  1218. }
  1219. #undef mswap
  1220. #undef mvswap
  1221. #undef mpush
  1222. #undef mpop
  1223. #undef mmin
  1224. #undef mnextsize
  1225. #undef mnextswap
  1226. #undef MAIN_QSORT_SMALL_THRESH
  1227. #undef MAIN_QSORT_DEPTH_THRESH
  1228. #undef MAIN_QSORT_STACK_SIZE
  1229. /*---------------------------------------------*/
  1230. /* Pre:
  1231. nblock > N_OVERSHOOT
  1232. block32 exists for [0 .. nblock-1 +N_OVERSHOOT]
  1233. ((UChar*)block32) [0 .. nblock-1] holds block
  1234. ptr exists for [0 .. nblock-1]
  1235. Post:
  1236. ((UChar*)block32) [0 .. nblock-1] holds block
  1237. All other areas of block32 destroyed
  1238. ftab [0 .. 65536 ] destroyed
  1239. ptr [0 .. nblock-1] holds sorted order
  1240. if (*budget < 0), sorting was abandoned
  1241. */
  1242. #define BIGFREQ(b) (ftab[((b)+1) << 8] - ftab[(b) << 8])
  1243. #define SETMASK (1 << 21)
  1244. #define CLEARMASK (~(SETMASK))
  1245. static
  1246. void mainSort ( UInt32* ptr,
  1247. UChar* block,
  1248. UInt16* quadrant,
  1249. UInt32* ftab,
  1250. Int32 nblock,
  1251. Int32 verb,
  1252. Int32* budget )
  1253. {
  1254. Int32 i, j, k, ss, sb;
  1255. Int32 runningOrder[256];
  1256. Bool bigDone[256];
  1257. Int32 copyStart[256];
  1258. Int32 copyEnd [256];
  1259. UChar c1;
  1260. Int32 numQSorted;
  1261. UInt16 s;
  1262. if (verb >= 4) VPrintf0 ( " main sort initialise ...\n" );
  1263. /*-- set up the 2-byte frequency table --*/
  1264. for (i = 65536; i >= 0; i--) ftab[i] = 0;
  1265. j = block[0] << 8;
  1266. i = nblock-1;
  1267. for (; i >= 3; i -= 4) {
  1268. quadrant[i] = 0;
  1269. j = (j >> 8) | ( ((UInt16)block[i]) << 8);
  1270. ftab[j]++;
  1271. quadrant[i-1] = 0;
  1272. j = (j >> 8) | ( ((UInt16)block[i-1]) << 8);
  1273. ftab[j]++;
  1274. quadrant[i-2] = 0;
  1275. j = (j >> 8) | ( ((UInt16)block[i-2]) << 8);
  1276. ftab[j]++;
  1277. quadrant[i-3] = 0;
  1278. j = (j >> 8) | ( ((UInt16)block[i-3]) << 8);
  1279. ftab[j]++;
  1280. }
  1281. for (; i >= 0; i--) {
  1282. quadrant[i] = 0;
  1283. j = (j >> 8) | ( ((UInt16)block[i]) << 8);
  1284. ftab[j]++;
  1285. }
  1286. /*-- (emphasises close relationship of block & quadrant) --*/
  1287. for (i = 0; i < BZ_N_OVERSHOOT; i++) {
  1288. block [nblock+i] = block[i];
  1289. quadrant[nblock+i] = 0;
  1290. }
  1291. if (verb >= 4) VPrintf0 ( " bucket sorting ...\n" );
  1292. /*-- Complete the initial radix sort --*/
  1293. for (i = 1; i <= 65536; i++) ftab[i] += ftab[i-1];
  1294. s = block[0] << 8;
  1295. i = nblock-1;
  1296. for (; i >= 3; i -= 4) {
  1297. s = (s >> 8) | (block[i] << 8);
  1298. j = ftab[s] -1;
  1299. ftab[s] = j;
  1300. ptr[j] = i;
  1301. s = (s >> 8) | (block[i-1] << 8);
  1302. j = ftab[s] -1;
  1303. ftab[s] = j;
  1304. ptr[j] = i-1;
  1305. s = (s >> 8) | (block[i-2] << 8);
  1306. j = ftab[s] -1;
  1307. ftab[s] = j;
  1308. ptr[j] = i-2;
  1309. s = (s >> 8) | (block[i-3] << 8);
  1310. j = ftab[s] -1;
  1311. ftab[s] = j;
  1312. ptr[j] = i-3;
  1313. }
  1314. for (; i >= 0; i--) {
  1315. s = (s >> 8) | (block[i] << 8);
  1316. j = ftab[s] -1;
  1317. ftab[s] = j;
  1318. ptr[j] = i;
  1319. }
  1320. /*--
  1321. Now ftab contains the first loc of every small bucket.
  1322. Calculate the running order, from smallest to largest
  1323. big bucket.
  1324. --*/
  1325. for (i = 0; i <= 255; i++) {
  1326. bigDone [i] = False;
  1327. runningOrder[i] = i;
  1328. }
  1329. {
  1330. Int32 vv;
  1331. Int32 h = 1;
  1332. do h = 3 * h + 1; while (h <= 256);
  1333. do {
  1334. h = h / 3;
  1335. for (i = h; i <= 255; i++) {
  1336. vv = runningOrder[i];
  1337. j = i;
  1338. while ( BIGFREQ(runningOrder[j-h]) > BIGFREQ(vv) ) {
  1339. runningOrder[j] = runningOrder[j-h];
  1340. j = j - h;
  1341. if (j <= (h - 1)) goto zero;
  1342. }
  1343. zero:
  1344. runningOrder[j] = vv;
  1345. }
  1346. } while (h != 1);
  1347. }
  1348. /*--
  1349. The main sorting loop.
  1350. --*/
  1351. numQSorted = 0;
  1352. for (i = 0; i <= 255; i++) {
  1353. /*--
  1354. Process big buckets, starting with the least full.
  1355. Basically this is a 3-step process in which we call
  1356. mainQSort3 to sort the small buckets [ss, j], but
  1357. also make a big effort to avoid the calls if we can.
  1358. --*/
  1359. ss = runningOrder[i];
  1360. /*--
  1361. Step 1:
  1362. Complete the big bucket [ss] by quicksorting
  1363. any unsorted small buckets [ss, j], for j != ss.
  1364. Hopefully previous pointer-scanning phases have already
  1365. completed many of the small buckets [ss, j], so
  1366. we don't have to sort them at all.
  1367. --*/
  1368. for (j = 0; j <= 255; j++) {
  1369. if (j != ss) {
  1370. sb = (ss << 8) + j;
  1371. if ( ! (ftab[sb] & SETMASK) ) {
  1372. Int32 lo = ftab[sb] & CLEARMASK;
  1373. Int32 hi = (ftab[sb+1] & CLEARMASK) - 1;
  1374. if (hi > lo) {
  1375. if (verb >= 4)
  1376. VPrintf4 ( " qsort [0x%x, 0x%x] "
  1377. "done %d this %d\n",
  1378. ss, j, numQSorted, hi - lo + 1 );
  1379. mainQSort3 (
  1380. ptr, block, quadrant, nblock,
  1381. lo, hi, BZ_N_RADIX, budget
  1382. );
  1383. numQSorted += (hi - lo + 1);
  1384. if (*budget < 0) return;
  1385. }
  1386. }
  1387. ftab[sb] |= SETMASK;
  1388. }
  1389. }
  1390. AssertH ( !bigDone[ss], 1006 );
  1391. /*--
  1392. Step 2:
  1393. Now scan this big bucket [ss] so as to synthesise the
  1394. sorted order for small buckets [t, ss] for all t,
  1395. including, magically, the bucket [ss,ss] too.
  1396. This will avoid doing Real Work in subsequent Step 1's.
  1397. --*/
  1398. {
  1399. for (j = 0; j <= 255; j++) {
  1400. copyStart[j] = ftab[(j << 8) + ss] & CLEARMASK;
  1401. copyEnd [j] = (ftab[(j << 8) + ss + 1] & CLEARMASK) - 1;
  1402. }
  1403. for (j = ftab[ss << 8] & CLEARMASK; j < copyStart[ss]; j++) {
  1404. k = ptr[j]-1; if (k < 0) k += nblock;
  1405. c1 = block[k];
  1406. if (!bigDone[c1])
  1407. ptr[ copyStart[c1]++ ] = k;
  1408. }
  1409. for (j = (ftab[(ss+1) << 8] & CLEARMASK) - 1; j > copyEnd[ss]; j--) {
  1410. k = ptr[j]-1; if (k < 0) k += nblock;
  1411. c1 = block[k];
  1412. if (!bigDone[c1])
  1413. ptr[ copyEnd[c1]-- ] = k;
  1414. }
  1415. }
  1416. AssertH ( (copyStart[ss]-1 == copyEnd[ss])
  1417. ||
  1418. /* Extremely rare case missing in bzip2-1.0.0 and 1.0.1.
  1419. Necessity for this case is demonstrated by compressing
  1420. a sequence of approximately 48.5 million of character
  1421. 251; 1.0.0/1.0.1 will then die here. */
  1422. (copyStart[ss] == 0 && copyEnd[ss] == nblock-1),
  1423. 1007 )
  1424. for (j = 0; j <= 255; j++) ftab[(j << 8) + ss] |= SETMASK;
  1425. /*--
  1426. Step 3:
  1427. The [ss] big bucket is now done. Record this fact,
  1428. and update the quadrant descriptors. Remember to
  1429. update quadrants in the overshoot area too, if
  1430. necessary. The "if (i < 255)" test merely skips
  1431. this updating for the last bucket processed, since
  1432. updating for the last bucket is pointless.
  1433. The quadrant array provides a way to incrementally
  1434. cache sort orderings, as they appear, so as to
  1435. make subsequent comparisons in fullGtU() complete
  1436. faster. For repetitive blocks this makes a big
  1437. difference (but not big enough to be able to avoid
  1438. the fallback sorting mechanism, exponential radix sort).
  1439. The precise meaning is: at all times:
  1440. for 0 <= i < nblock and 0 <= j <= nblock
  1441. if block[i] != block[j],
  1442. then the relative values of quadrant[i] and
  1443. quadrant[j] are meaningless.
  1444. else {
  1445. if quadrant[i] < quadrant[j]
  1446. then the string starting at i lexicographically
  1447. precedes the string starting at j
  1448. else if quadrant[i] > quadrant[j]
  1449. then the string starting at j lexicographically
  1450. precedes the string starting at i
  1451. else
  1452. the relative ordering of the strings starting
  1453. at i and j has not yet been determined.
  1454. }
  1455. --*/
  1456. bigDone[ss] = True;
  1457. if (i < 255) {
  1458. Int32 bbStart = ftab[ss << 8] & CLEARMASK;
  1459. Int32 bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
  1460. Int32 shifts = 0;
  1461. while ((bbSize >> shifts) > 65534) shifts++;
  1462. for (j = bbSize-1; j >= 0; j--) {
  1463. Int32 a2update = ptr[bbStart + j];
  1464. UInt16 qVal = (UInt16)(j >> shifts);
  1465. quadrant[a2update] = qVal;
  1466. if (a2update < BZ_N_OVERSHOOT)
  1467. quadrant[a2update + nblock] = qVal;
  1468. }
  1469. AssertH ( ((bbSize-1) >> shifts) <= 65535, 1002 );
  1470. }
  1471. }
  1472. if (verb >= 4)
  1473. VPrintf3 ( " %d pointers, %d sorted, %d scanned\n",
  1474. nblock, numQSorted, nblock - numQSorted );
  1475. }
  1476. #undef BIGFREQ
  1477. #undef SETMASK
  1478. #undef CLEARMASK
  1479. /*---------------------------------------------*/
  1480. /* Pre:
  1481. nblock > 0
  1482. arr2 exists for [0 .. nblock-1 +N_OVERSHOOT]
  1483. ((UChar*)arr2) [0 .. nblock-1] holds block
  1484. arr1 exists for [0 .. nblock-1]
  1485. Post:
  1486. ((UChar*)arr2) [0 .. nblock-1] holds block
  1487. All other areas of block destroyed
  1488. ftab [ 0 .. 65536 ] destroyed
  1489. arr1 [0 .. nblock-1] holds sorted order
  1490. */
  1491. void BZ2_blockSort ( EState* s )
  1492. {
  1493. UInt32* ptr = s->ptr;
  1494. UChar* block = s->block;
  1495. UInt32* ftab = s->ftab;
  1496. Int32 nblock = s->nblock;
  1497. Int32 verb = s->verbosity;
  1498. Int32 wfact = s->workFactor;
  1499. UInt16* quadrant;
  1500. Int32 budget;
  1501. Int32 budgetInit;
  1502. Int32 i;
  1503. if (nblock < 10000) {
  1504. fallbackSort ( s->arr1, s->arr2, ftab, nblock, verb );
  1505. } else {
  1506. /* Calculate the location for quadrant, remembering to get
  1507. the alignment right. Assumes that &(block[0]) is at least
  1508. 2-byte aligned -- this should be ok since block is really
  1509. the first section of arr2.
  1510. */
  1511. i = nblock+BZ_N_OVERSHOOT;
  1512. if (i & 1) i++;
  1513. quadrant = (UInt16*)(&(block[i]));
  1514. /* (wfact-1) / 3 puts the default-factor-30
  1515. transition point at very roughly the same place as
  1516. with v0.1 and v0.9.0.
  1517. Not that it particularly matters any more, since the
  1518. resulting compressed stream is now the same regardless
  1519. of whether or not we use the main sort or fallback sort.
  1520. */
  1521. if (wfact < 1 ) wfact = 1;
  1522. if (wfact > 100) wfact = 100;
  1523. budgetInit = nblock * ((wfact-1) / 3);
  1524. budget = budgetInit;
  1525. mainSort ( ptr, block, quadrant, ftab, nblock, verb, &budget );
  1526. if (verb >= 3)
  1527. VPrintf3 ( " %d work, %d block, ratio %5.2f\n",
  1528. budgetInit - budget,
  1529. nblock,
  1530. (float)(budgetInit - budget) /
  1531. (float)(nblock==0 ? 1 : nblock) );
  1532. if (budget < 0) {
  1533. if (verb >= 2)
  1534. VPrintf0 ( " too repetitive; using fallback"
  1535. " sorting algorithm\n" );
  1536. fallbackSort ( s->arr1, s->arr2, ftab, nblock, verb );
  1537. }
  1538. }
  1539. s->origPtr = -1;
  1540. for (i = 0; i < s->nblock; i++)
  1541. if (ptr[i] == 0)
  1542. { s->origPtr = i; break; };
  1543. AssertH( s->origPtr != -1, 1003 );
  1544. }
  1545. /*-------------------------------------------------------------*/
  1546. /*--- end blocksort.c ---*/
  1547. /*-------------------------------------------------------------*/
  1548. /*-------------------------------------------------------------*/
  1549. /*--- Huffman coding low-level stuff ---*/
  1550. /*--- huffman.c ---*/
  1551. /*-------------------------------------------------------------*/
  1552. /*---------------------------------------------------*/
  1553. #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
  1554. #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
  1555. #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
  1556. #define ADDWEIGHTS(zw1,zw2) \
  1557. (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
  1558. (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
  1559. #define UPHEAP(z) \
  1560. { \
  1561. Int32 zz, tmp; \
  1562. zz = z; tmp = heap[zz]; \
  1563. while (weight[tmp] < weight[heap[zz >> 1]]) { \
  1564. heap[zz] = heap[zz >> 1]; \
  1565. zz >>= 1; \
  1566. } \
  1567. heap[zz] = tmp; \
  1568. }
  1569. #define DOWNHEAP(z) \
  1570. { \
  1571. Int32 zz, yy, tmp; \
  1572. zz = z; tmp = heap[zz]; \
  1573. while (True) { \
  1574. yy = zz << 1; \
  1575. if (yy > nHeap) break; \
  1576. if (yy < nHeap && \
  1577. weight[heap[yy+1]] < weight[heap[yy]]) \
  1578. yy++; \
  1579. if (weight[tmp] < weight[heap[yy]]) break; \
  1580. heap[zz] = heap[yy]; \
  1581. zz = yy; \
  1582. } \
  1583. heap[zz] = tmp; \
  1584. }
  1585. /*---------------------------------------------------*/
  1586. void BZ2_hbMakeCodeLengths ( UChar *len,
  1587. Int32 *freq,
  1588. Int32 alphaSize,
  1589. Int32 maxLen )
  1590. {
  1591. /*--
  1592. Nodes and heap entries run from 1. Entry 0
  1593. for both the heap and nodes is a sentinel.
  1594. --*/
  1595. Int32 nNodes, nHeap, n1, n2, i, j, k;
  1596. Bool tooLong;
  1597. Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
  1598. Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
  1599. Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
  1600. for (i = 0; i < alphaSize; i++)
  1601. weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
  1602. while (True) {
  1603. nNodes = alphaSize;
  1604. nHeap = 0;
  1605. heap[0] = 0;
  1606. weight[0] = 0;
  1607. parent[0] = -2;
  1608. for (i = 1; i <= alphaSize; i++) {
  1609. parent[i] = -1;
  1610. nHeap++;
  1611. heap[nHeap] = i;
  1612. UPHEAP(nHeap);
  1613. }
  1614. AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
  1615. while (nHeap > 1) {
  1616. n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  1617. n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  1618. nNodes++;
  1619. parent[n1] = parent[n2] = nNodes;
  1620. weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
  1621. parent[nNodes] = -1;
  1622. nHeap++;
  1623. heap[nHeap] = nNodes;
  1624. UPHEAP(nHeap);
  1625. }
  1626. AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
  1627. tooLong = False;
  1628. for (i = 1; i <= alphaSize; i++) {
  1629. j = 0;
  1630. k = i;
  1631. while (parent[k] >= 0) { k = parent[k]; j++; }
  1632. len[i-1] = j;
  1633. if (j > maxLen) tooLong = True;
  1634. }
  1635. if (! tooLong) break;
  1636. for (i = 1; i < alphaSize; i++) {
  1637. j = weight[i] >> 8;
  1638. j = 1 + (j / 2);
  1639. weight[i] = j << 8;
  1640. }
  1641. }
  1642. }
  1643. /*---------------------------------------------------*/
  1644. void BZ2_hbAssignCodes ( Int32 *code,
  1645. UChar *length,
  1646. Int32 minLen,
  1647. Int32 maxLen,
  1648. Int32 alphaSize )
  1649. {
  1650. Int32 n, vec, i;
  1651. vec = 0;
  1652. for (n = minLen; n <= maxLen; n++) {
  1653. for (i = 0; i < alphaSize; i++)
  1654. if (length[i] == n) { code[i] = vec; vec++; };
  1655. vec <<= 1;
  1656. }
  1657. }
  1658. /*---------------------------------------------------*/
  1659. void BZ2_hbCreateDecodeTables ( Int32 *limit,
  1660. Int32 *base,
  1661. Int32 *perm,
  1662. UChar *length,
  1663. Int32 minLen,
  1664. Int32 maxLen,
  1665. Int32 alphaSize )
  1666. {
  1667. Int32 pp, i, j, vec;
  1668. pp = 0;
  1669. for (i = minLen; i <= maxLen; i++)
  1670. for (j = 0; j < alphaSize; j++)
  1671. if (length[j] == i) { perm[pp] = j; pp++; };
  1672. for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
  1673. for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
  1674. for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
  1675. for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
  1676. vec = 0;
  1677. for (i = minLen; i <= maxLen; i++) {
  1678. vec += (base[i+1] - base[i]);
  1679. limit[i] = vec-1;
  1680. vec <<= 1;
  1681. }
  1682. for (i = minLen + 1; i <= maxLen; i++)
  1683. base[i] = ((limit[i-1] + 1) << 1) - base[i];
  1684. }
  1685. /*-------------------------------------------------------------*/
  1686. /*--- end huffman.c ---*/
  1687. /*-------------------------------------------------------------*/
  1688. /*-------------------------------------------------------------*/
  1689. /*--- Table for doing CRCs ---*/
  1690. /*--- crctable.c ---*/
  1691. /*-------------------------------------------------------------*/
  1692. /*--
  1693. I think this is an implementation of the AUTODIN-II,
  1694. Ethernet & FDDI 32-bit CRC standard. Vaguely derived
  1695. from code by Rob Warnock, in Section 51 of the
  1696. comp.compression FAQ.
  1697. --*/
  1698. UInt32 BZ2_crc32Table[256] = {
  1699. /*-- Ugly, innit? --*/
  1700. 0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L,
  1701. 0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L,
  1702. 0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L,
  1703. 0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL,
  1704. 0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L,
  1705. 0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L,
  1706. 0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L,
  1707. 0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL,
  1708. 0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L,
  1709. 0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L,
  1710. 0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L,
  1711. 0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL,
  1712. 0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L,
  1713. 0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L,
  1714. 0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L,
  1715. 0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL,
  1716. 0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL,
  1717. 0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L,
  1718. 0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L,
  1719. 0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL,
  1720. 0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL,
  1721. 0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L,
  1722. 0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L,
  1723. 0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL,
  1724. 0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL,
  1725. 0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L,
  1726. 0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L,
  1727. 0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL,
  1728. 0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL,
  1729. 0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L,
  1730. 0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L,
  1731. 0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL,
  1732. 0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L,
  1733. 0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL,
  1734. 0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL,
  1735. 0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L,
  1736. 0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L,
  1737. 0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL,
  1738. 0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL,
  1739. 0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L,
  1740. 0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L,
  1741. 0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL,
  1742. 0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL,
  1743. 0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L,
  1744. 0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L,
  1745. 0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL,
  1746. 0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL,
  1747. 0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L,
  1748. 0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L,
  1749. 0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL,
  1750. 0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L,
  1751. 0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L,
  1752. 0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L,
  1753. 0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL,
  1754. 0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L,
  1755. 0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L,
  1756. 0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L,
  1757. 0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL,
  1758. 0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L,
  1759. 0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L,
  1760. 0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L,
  1761. 0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL,
  1762. 0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L,
  1763. 0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L
  1764. };
  1765. /*-------------------------------------------------------------*/
  1766. /*--- end crctable.c ---*/
  1767. /*-------------------------------------------------------------*/
  1768. /*-------------------------------------------------------------*/
  1769. /*--- Table for randomising repetitive blocks ---*/
  1770. /*--- randtable.c ---*/
  1771. /*-------------------------------------------------------------*/
  1772. /*---------------------------------------------*/
  1773. Int32 BZ2_rNums[512] = {
  1774. 619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
  1775. 985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
  1776. 733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
  1777. 419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
  1778. 878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
  1779. 862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
  1780. 150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
  1781. 170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
  1782. 73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
  1783. 909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
  1784. 641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
  1785. 161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
  1786. 382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
  1787. 98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
  1788. 227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
  1789. 469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
  1790. 184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
  1791. 715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
  1792. 951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
  1793. 652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
  1794. 645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
  1795. 609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
  1796. 653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
  1797. 411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
  1798. 170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
  1799. 857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
  1800. 669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
  1801. 944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
  1802. 344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
  1803. 897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
  1804. 433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
  1805. 686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
  1806. 946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
  1807. 978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
  1808. 680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
  1809. 707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
  1810. 297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
  1811. 134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
  1812. 343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
  1813. 140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
  1814. 170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
  1815. 369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
  1816. 804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
  1817. 896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
  1818. 661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
  1819. 768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
  1820. 61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
  1821. 372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
  1822. 780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
  1823. 920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
  1824. 645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
  1825. 936, 638
  1826. };
  1827. /*-------------------------------------------------------------*/
  1828. /*--- end randtable.c ---*/
  1829. /*-------------------------------------------------------------*/
  1830. /*-------------------------------------------------------------*/
  1831. /*--- Compression machinery (not incl block sorting) ---*/
  1832. /*--- compress.c ---*/
  1833. /*-------------------------------------------------------------*/
  1834. /*---------------------------------------------------*/
  1835. /*--- Bit stream I/O ---*/
  1836. /*---------------------------------------------------*/
  1837. /*---------------------------------------------------*/
  1838. void BZ2_bsInitWrite ( EState* s )
  1839. {
  1840. s->bsLive = 0;
  1841. s->bsBuff = 0;
  1842. }
  1843. /*---------------------------------------------------*/
  1844. static
  1845. void bsFinishWrite ( EState* s )
  1846. {
  1847. while (s->bsLive > 0) {
  1848. s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24);
  1849. s->numZ++;
  1850. s->bsBuff <<= 8;
  1851. s->bsLive -= 8;
  1852. }
  1853. }
  1854. /*---------------------------------------------------*/
  1855. #define bsNEEDW(nz) \
  1856. { \
  1857. while (s->bsLive >= 8) { \
  1858. s->zbits[s->numZ] \
  1859. = (UChar)(s->bsBuff >> 24); \
  1860. s->numZ++; \
  1861. s->bsBuff <<= 8; \
  1862. s->bsLive -= 8; \
  1863. } \
  1864. }
  1865. /*---------------------------------------------------*/
  1866. static
  1867. __inline__
  1868. void bsW ( EState* s, Int32 n, UInt32 v )
  1869. {
  1870. bsNEEDW ( n );
  1871. s->bsBuff |= (v << (32 - s->bsLive - n));
  1872. s->bsLive += n;
  1873. }
  1874. /*---------------------------------------------------*/
  1875. static
  1876. void bsPutUInt32 ( EState* s, UInt32 u )
  1877. {
  1878. bsW ( s, 8, (u >> 24) & 0xffL );
  1879. bsW ( s, 8, (u >> 16) & 0xffL );
  1880. bsW ( s, 8, (u >> 8) & 0xffL );
  1881. bsW ( s, 8, u & 0xffL );
  1882. }
  1883. /*---------------------------------------------------*/
  1884. static
  1885. void bsPutUChar ( EState* s, UChar c )
  1886. {
  1887. bsW( s, 8, (UInt32)c );
  1888. }
  1889. /*---------------------------------------------------*/
  1890. /*--- The back end proper ---*/
  1891. /*---------------------------------------------------*/
  1892. /*---------------------------------------------------*/
  1893. static
  1894. void makeMaps_e ( EState* s )
  1895. {
  1896. Int32 i;
  1897. s->nInUse = 0;
  1898. for (i = 0; i < 256; i++)
  1899. if (s->inUse[i]) {
  1900. s->unseqToSeq[i] = s->nInUse;
  1901. s->nInUse++;
  1902. }
  1903. }
  1904. /*---------------------------------------------------*/
  1905. static
  1906. void generateMTFValues ( EState* s )
  1907. {
  1908. UChar yy[256];
  1909. Int32 i, j;
  1910. Int32 zPend;
  1911. Int32 wr;
  1912. Int32 EOB;
  1913. /*
  1914. After sorting (eg, here),
  1915. s->arr1 [ 0 .. s->nblock-1 ] holds sorted order,
  1916. and
  1917. ((UChar*)s->arr2) [ 0 .. s->nblock-1 ]
  1918. holds the original block data.
  1919. The first thing to do is generate the MTF values,
  1920. and put them in
  1921. ((UInt16*)s->arr1) [ 0 .. s->nblock-1 ].
  1922. Because there are strictly fewer or equal MTF values
  1923. than block values, ptr values in this area are overwritten
  1924. with MTF values only when they are no longer needed.
  1925. The final compressed bitstream is generated into the
  1926. area starting at
  1927. (UChar*) (&((UChar*)s->arr2)[s->nblock])
  1928. These storage aliases are set up in bzCompressInit(),
  1929. except for the last one, which is arranged in
  1930. compressBlock().
  1931. */
  1932. UInt32* ptr = s->ptr;
  1933. UChar* block = s->block;
  1934. UInt16* mtfv = s->mtfv;
  1935. makeMaps_e ( s );
  1936. EOB = s->nInUse+1;
  1937. for (i = 0; i <= EOB; i++) s->mtfFreq[i] = 0;
  1938. wr = 0;
  1939. zPend = 0;
  1940. for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
  1941. for (i = 0; i < s->nblock; i++) {
  1942. UChar ll_i;
  1943. AssertD ( wr <= i, "generateMTFValues(1)" );
  1944. j = ptr[i]-1; if (j < 0) j += s->nblock;
  1945. ll_i = s->unseqToSeq[block[j]];
  1946. AssertD ( ll_i < s->nInUse, "generateMTFValues(2a)" );
  1947. if (yy[0] == ll_i) {
  1948. zPend++;
  1949. } else {
  1950. if (zPend > 0) {
  1951. zPend--;
  1952. while (True) {
  1953. if (zPend & 1) {
  1954. mtfv[wr] = BZ_RUNB; wr++;
  1955. s->mtfFreq[BZ_RUNB]++;
  1956. } else {
  1957. mtfv[wr] = BZ_RUNA; wr++;
  1958. s->mtfFreq[BZ_RUNA]++;
  1959. }
  1960. if (zPend < 2) break;
  1961. zPend = (zPend - 2) / 2;
  1962. };
  1963. zPend = 0;
  1964. }
  1965. {
  1966. register UChar rtmp;
  1967. register UChar* ryy_j;
  1968. register UChar rll_i;
  1969. rtmp = yy[1];
  1970. yy[1] = yy[0];
  1971. ryy_j = &(yy[1]);
  1972. rll_i = ll_i;
  1973. while ( rll_i != rtmp ) {
  1974. register UChar rtmp2;
  1975. ryy_j++;
  1976. rtmp2 = rtmp;
  1977. rtmp = *ryy_j;
  1978. *ryy_j = rtmp2;
  1979. };
  1980. yy[0] = rtmp;
  1981. j = ryy_j - &(yy[0]);
  1982. mtfv[wr] = j+1; wr++; s->mtfFreq[j+1]++;
  1983. }
  1984. }
  1985. }
  1986. if (zPend > 0) {
  1987. zPend--;
  1988. while (True) {
  1989. if (zPend & 1) {
  1990. mtfv[wr] = BZ_RUNB; wr++;
  1991. s->mtfFreq[BZ_RUNB]++;
  1992. } else {
  1993. mtfv[wr] = BZ_RUNA; wr++;
  1994. s->mtfFreq[BZ_RUNA]++;
  1995. }
  1996. if (zPend < 2) break;
  1997. zPend = (zPend - 2) / 2;
  1998. };
  1999. zPend = 0;
  2000. }
  2001. mtfv[wr] = EOB; wr++; s->mtfFreq[EOB]++;
  2002. s->nMTF = wr;
  2003. }
  2004. /*---------------------------------------------------*/
  2005. #define BZ_LESSER_ICOST 0
  2006. #define BZ_GREATER_ICOST 15
  2007. static
  2008. void sendMTFValues ( EState* s )
  2009. {
  2010. Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
  2011. Int32 nSelectors, alphaSize, minLen, maxLen, selCtr;
  2012. Int32 nGroups, nBytes;
  2013. /*--
  2014. UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  2015. is a global since the decoder also needs it.
  2016. Int32 code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  2017. Int32 rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  2018. are also globals only used in this proc.
  2019. Made global to keep stack frame size small.
  2020. --*/
  2021. UInt16 cost[BZ_N_GROUPS];
  2022. Int32 fave[BZ_N_GROUPS];
  2023. UInt16* mtfv = s->mtfv;
  2024. if (s->verbosity >= 3)
  2025. VPrintf3( " %d in block, %d after MTF & 1-2 coding, "
  2026. "%d+2 syms in use\n",
  2027. s->nblock, s->nMTF, s->nInUse );
  2028. alphaSize = s->nInUse+2;
  2029. for (t = 0; t < BZ_N_GROUPS; t++)
  2030. for (v = 0; v < alphaSize; v++)
  2031. s->len[t][v] = BZ_GREATER_ICOST;
  2032. /*--- Decide how many coding tables to use ---*/
  2033. AssertH ( s->nMTF > 0, 3001 );
  2034. if (s->nMTF < 200) nGroups = 2; else
  2035. if (s->nMTF < 600) nGroups = 3; else
  2036. if (s->nMTF < 1200) nGroups = 4; else
  2037. if (s->nMTF < 2400) nGroups = 5; else
  2038. nGroups = 6;
  2039. /*--- Generate an initial set of coding tables ---*/
  2040. {
  2041. Int32 nPart, remF, tFreq, aFreq;
  2042. nPart = nGroups;
  2043. remF = s->nMTF;
  2044. gs = 0;
  2045. while (nPart > 0) {
  2046. tFreq = remF / nPart;
  2047. ge = gs-1;
  2048. aFreq = 0;
  2049. while (aFreq < tFreq && ge < alphaSize-1) {
  2050. ge++;
  2051. aFreq += s->mtfFreq[ge];
  2052. }
  2053. if (ge > gs
  2054. && nPart != nGroups && nPart != 1
  2055. && ((nGroups-nPart) % 2 == 1)) {
  2056. aFreq -= s->mtfFreq[ge];
  2057. ge--;
  2058. }
  2059. if (s->verbosity >= 3)
  2060. VPrintf5( " initial group %d, [%d .. %d], "
  2061. "has %d syms (%4.1f%%)\n",
  2062. nPart, gs, ge, aFreq,
  2063. (100.0 * (float)aFreq) / (float)(s->nMTF) );
  2064. for (v = 0; v < alphaSize; v++)
  2065. if (v >= gs && v <= ge)
  2066. s->len[nPart-1][v] = BZ_LESSER_ICOST; else
  2067. s->len[nPart-1][v] = BZ_GREATER_ICOST;
  2068. nPart--;
  2069. gs = ge+1;
  2070. remF -= aFreq;
  2071. }
  2072. }
  2073. /*---
  2074. Iterate up to BZ_N_ITERS times to improve the tables.
  2075. ---*/
  2076. for (iter = 0; iter < BZ_N_ITERS; iter++) {
  2077. for (t = 0; t < nGroups; t++) fave[t] = 0;
  2078. for (t = 0; t < nGroups; t++)
  2079. for (v = 0; v < alphaSize; v++)
  2080. s->rfreq[t][v] = 0;
  2081. /*---
  2082. Set up an auxiliary length table which is used to fast-track
  2083. the common case (nGroups == 6).
  2084. ---*/
  2085. if (nGroups == 6) {
  2086. for (v = 0; v < alphaSize; v++) {
  2087. s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
  2088. s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
  2089. s->len_pack[v][2] = (s->len[5][v] << 16) | s->len[4][v];
  2090. }
  2091. }
  2092. nSelectors = 0;
  2093. totc = 0;
  2094. gs = 0;
  2095. while (True) {
  2096. /*--- Set group start & end marks. --*/
  2097. if (gs >= s->nMTF) break;
  2098. ge = gs + BZ_G_SIZE - 1;
  2099. if (ge >= s->nMTF) ge = s->nMTF-1;
  2100. /*--
  2101. Calculate the cost of this group as coded
  2102. by each of the coding tables.
  2103. --*/
  2104. for (t = 0; t < nGroups; t++) cost[t] = 0;
  2105. if (nGroups == 6 && 50 == ge-gs+1) {
  2106. /*--- fast track the common case ---*/
  2107. register UInt32 cost01, cost23, cost45;
  2108. register UInt16 icv;
  2109. cost01 = cost23 = cost45 = 0;
  2110. # define BZ_ITER(nn) \
  2111. icv = mtfv[gs+(nn)]; \
  2112. cost01 += s->len_pack[icv][0]; \
  2113. cost23 += s->len_pack[icv][1]; \
  2114. cost45 += s->len_pack[icv][2]; \
  2115. BZ_ITER(0); BZ_ITER(1); BZ_ITER(2); BZ_ITER(3); BZ_ITER(4);
  2116. BZ_ITER(5); BZ_ITER(6); BZ_ITER(7); BZ_ITER(8); BZ_ITER(9);
  2117. BZ_ITER(10); BZ_ITER(11); BZ_ITER(12); BZ_ITER(13); BZ_ITER(14);
  2118. BZ_ITER(15); BZ_ITER(16); BZ_ITER(17); BZ_ITER(18); BZ_ITER(19);
  2119. BZ_ITER(20); BZ_ITER(21); BZ_ITER(22); BZ_ITER(23); BZ_ITER(24);
  2120. BZ_ITER(25); BZ_ITER(26); BZ_ITER(27); BZ_ITER(28); BZ_ITER(29);
  2121. BZ_ITER(30); BZ_ITER(31); BZ_ITER(32); BZ_ITER(33); BZ_ITER(34);
  2122. BZ_ITER(35); BZ_ITER(36); BZ_ITER(37); BZ_ITER(38); BZ_ITER(39);
  2123. BZ_ITER(40); BZ_ITER(41); BZ_ITER(42); BZ_ITER(43); BZ_ITER(44);
  2124. BZ_ITER(45); BZ_ITER(46); BZ_ITER(47); BZ_ITER(48); BZ_ITER(49);
  2125. # undef BZ_ITER
  2126. cost[0] = cost01 & 0xffff; cost[1] = cost01 >> 16;
  2127. cost[2] = cost23 & 0xffff; cost[3] = cost23 >> 16;
  2128. cost[4] = cost45 & 0xffff; cost[5] = cost45 >> 16;
  2129. } else {
  2130. /*--- slow version which correctly handles all situations ---*/
  2131. for (i = gs; i <= ge; i++) {
  2132. UInt16 icv = mtfv[i];
  2133. for (t = 0; t < nGroups; t++) cost[t] += s->len[t][icv];
  2134. }
  2135. }
  2136. /*--
  2137. Find the coding table which is best for this group,
  2138. and record its identity in the selector table.
  2139. --*/
  2140. bc = 999999999; bt = -1;
  2141. for (t = 0; t < nGroups; t++)
  2142. if (cost[t] < bc) { bc = cost[t]; bt = t; };
  2143. totc += bc;
  2144. fave[bt]++;
  2145. s->selector[nSelectors] = bt;
  2146. nSelectors++;
  2147. /*--
  2148. Increment the symbol frequencies for the selected table.
  2149. --*/
  2150. if (nGroups == 6 && 50 == ge-gs+1) {
  2151. /*--- fast track the common case ---*/
  2152. # define BZ_ITUR(nn) s->rfreq[bt][ mtfv[gs+(nn)] ]++
  2153. BZ_ITUR(0); BZ_ITUR(1); BZ_ITUR(2); BZ_ITUR(3); BZ_ITUR(4);
  2154. BZ_ITUR(5); BZ_ITUR(6); BZ_ITUR(7); BZ_ITUR(8); BZ_ITUR(9);
  2155. BZ_ITUR(10); BZ_ITUR(11); BZ_ITUR(12); BZ_ITUR(13); BZ_ITUR(14);
  2156. BZ_ITUR(15); BZ_ITUR(16); BZ_ITUR(17); BZ_ITUR(18); BZ_ITUR(19);
  2157. BZ_ITUR(20); BZ_ITUR(21); BZ_ITUR(22); BZ_ITUR(23); BZ_ITUR(24);
  2158. BZ_ITUR(25); BZ_ITUR(26); BZ_ITUR(27); BZ_ITUR(28); BZ_ITUR(29);
  2159. BZ_ITUR(30); BZ_ITUR(31); BZ_ITUR(32); BZ_ITUR(33); BZ_ITUR(34);
  2160. BZ_ITUR(35); BZ_ITUR(36); BZ_ITUR(37); BZ_ITUR(38); BZ_ITUR(39);
  2161. BZ_ITUR(40); BZ_ITUR(41); BZ_ITUR(42); BZ_ITUR(43); BZ_ITUR(44);
  2162. BZ_ITUR(45); BZ_ITUR(46); BZ_ITUR(47); BZ_ITUR(48); BZ_ITUR(49);
  2163. # undef BZ_ITUR
  2164. } else {
  2165. /*--- slow version which correctly handles all situations ---*/
  2166. for (i = gs; i <= ge; i++)
  2167. s->rfreq[bt][ mtfv[i] ]++;
  2168. }
  2169. gs = ge+1;
  2170. }
  2171. if (s->verbosity >= 3) {
  2172. VPrintf2 ( " pass %d: size is %d, grp uses are ",
  2173. iter+1, totc/8 );
  2174. for (t = 0; t < nGroups; t++)
  2175. VPrintf1 ( "%d ", fave[t] );
  2176. VPrintf0 ( "\n" );
  2177. }
  2178. /*--
  2179. Recompute the tables based on the accumulated frequencies.
  2180. --*/
  2181. for (t = 0; t < nGroups; t++)
  2182. BZ2_hbMakeCodeLengths ( &(s->len[t][0]), &(s->rfreq[t][0]),
  2183. alphaSize, 20 );
  2184. }
  2185. AssertH( nGroups < 8, 3002 );
  2186. AssertH( nSelectors < 32768 &&
  2187. nSelectors <= (2 + (900000 / BZ_G_SIZE)),
  2188. 3003 );
  2189. /*--- Compute MTF values for the selectors. ---*/
  2190. {
  2191. UChar pos[BZ_N_GROUPS], ll_i, tmp2, tmp;
  2192. for (i = 0; i < nGroups; i++) pos[i] = i;
  2193. for (i = 0; i < nSelectors; i++) {
  2194. ll_i = s->selector[i];
  2195. j = 0;
  2196. tmp = pos[j];
  2197. while ( ll_i != tmp ) {
  2198. j++;
  2199. tmp2 = tmp;
  2200. tmp = pos[j];
  2201. pos[j] = tmp2;
  2202. };
  2203. pos[0] = tmp;
  2204. s->selectorMtf[i] = j;
  2205. }
  2206. };
  2207. /*--- Assign actual codes for the tables. --*/
  2208. for (t = 0; t < nGroups; t++) {
  2209. minLen = 32;
  2210. maxLen = 0;
  2211. for (i = 0; i < alphaSize; i++) {
  2212. if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  2213. if (s->len[t][i] < minLen) minLen = s->len[t][i];
  2214. }
  2215. AssertH ( !(maxLen > 20), 3004 );
  2216. AssertH ( !(minLen < 1), 3005 );
  2217. BZ2_hbAssignCodes ( &(s->code[t][0]), &(s->len[t][0]),
  2218. minLen, maxLen, alphaSize );
  2219. }
  2220. /*--- Transmit the mapping table. ---*/
  2221. {
  2222. Bool inUse16[16];
  2223. for (i = 0; i < 16; i++) {
  2224. inUse16[i] = False;
  2225. for (j = 0; j < 16; j++)
  2226. if (s->inUse[i * 16 + j]) inUse16[i] = True;
  2227. }
  2228. nBytes = s->numZ;
  2229. for (i = 0; i < 16; i++)
  2230. if (inUse16[i]) bsW(s,1,1); else bsW(s,1,0);
  2231. for (i = 0; i < 16; i++)
  2232. if (inUse16[i])
  2233. for (j = 0; j < 16; j++) {
  2234. if (s->inUse[i * 16 + j]) bsW(s,1,1); else bsW(s,1,0);
  2235. }
  2236. if (s->verbosity >= 3)
  2237. VPrintf1( " bytes: mapping %d, ", s->numZ-nBytes );
  2238. }
  2239. /*--- Now the selectors. ---*/
  2240. nBytes = s->numZ;
  2241. bsW ( s, 3, nGroups );
  2242. bsW ( s, 15, nSelectors );
  2243. for (i = 0; i < nSelectors; i++) {
  2244. for (j = 0; j < s->selectorMtf[i]; j++) bsW(s,1,1);
  2245. bsW(s,1,0);
  2246. }
  2247. if (s->verbosity >= 3)
  2248. VPrintf1( "selectors %d, ", s->numZ-nBytes );
  2249. /*--- Now the coding tables. ---*/
  2250. nBytes = s->numZ;
  2251. for (t = 0; t < nGroups; t++) {
  2252. Int32 curr = s->len[t][0];
  2253. bsW ( s, 5, curr );
  2254. for (i = 0; i < alphaSize; i++) {
  2255. while (curr < s->len[t][i]) { bsW(s,2,2); curr++; /* 10 */ };
  2256. while (curr > s->len[t][i]) { bsW(s,2,3); curr--; /* 11 */ };
  2257. bsW ( s, 1, 0 );
  2258. }
  2259. }
  2260. if (s->verbosity >= 3)
  2261. VPrintf1 ( "code lengths %d, ", s->numZ-nBytes );
  2262. /*--- And finally, the block data proper ---*/
  2263. nBytes = s->numZ;
  2264. selCtr = 0;
  2265. gs = 0;
  2266. while (True) {
  2267. if (gs >= s->nMTF) break;
  2268. ge = gs + BZ_G_SIZE - 1;
  2269. if (ge >= s->nMTF) ge = s->nMTF-1;
  2270. AssertH ( s->selector[selCtr] < nGroups, 3006 );
  2271. if (nGroups == 6 && 50 == ge-gs+1) {
  2272. /*--- fast track the common case ---*/
  2273. UInt16 mtfv_i;
  2274. UChar* s_len_sel_selCtr
  2275. = &(s->len[s->selector[selCtr]][0]);
  2276. Int32* s_code_sel_selCtr
  2277. = &(s->code[s->selector[selCtr]][0]);
  2278. # define BZ_ITAH(nn) \
  2279. mtfv_i = mtfv[gs+(nn)]; \
  2280. bsW ( s, \
  2281. s_len_sel_selCtr[mtfv_i], \
  2282. s_code_sel_selCtr[mtfv_i] )
  2283. BZ_ITAH(0); BZ_ITAH(1); BZ_ITAH(2); BZ_ITAH(3); BZ_ITAH(4);
  2284. BZ_ITAH(5); BZ_ITAH(6); BZ_ITAH(7); BZ_ITAH(8); BZ_ITAH(9);
  2285. BZ_ITAH(10); BZ_ITAH(11); BZ_ITAH(12); BZ_ITAH(13); BZ_ITAH(14);
  2286. BZ_ITAH(15); BZ_ITAH(16); BZ_ITAH(17); BZ_ITAH(18); BZ_ITAH(19);
  2287. BZ_ITAH(20); BZ_ITAH(21); BZ_ITAH(22); BZ_ITAH(23); BZ_ITAH(24);
  2288. BZ_ITAH(25); BZ_ITAH(26); BZ_ITAH(27); BZ_ITAH(28); BZ_ITAH(29);
  2289. BZ_ITAH(30); BZ_ITAH(31); BZ_ITAH(32); BZ_ITAH(33); BZ_ITAH(34);
  2290. BZ_ITAH(35); BZ_ITAH(36); BZ_ITAH(37); BZ_ITAH(38); BZ_ITAH(39);
  2291. BZ_ITAH(40); BZ_ITAH(41); BZ_ITAH(42); BZ_ITAH(43); BZ_ITAH(44);
  2292. BZ_ITAH(45); BZ_ITAH(46); BZ_ITAH(47); BZ_ITAH(48); BZ_ITAH(49);
  2293. # undef BZ_ITAH
  2294. } else {
  2295. /*--- slow version which correctly handles all situations ---*/
  2296. for (i = gs; i <= ge; i++) {
  2297. bsW ( s,
  2298. s->len [s->selector[selCtr]] [mtfv[i]],
  2299. s->code [s->selector[selCtr]] [mtfv[i]] );
  2300. }
  2301. }
  2302. gs = ge+1;
  2303. selCtr++;
  2304. }
  2305. AssertH( selCtr == nSelectors, 3007 );
  2306. if (s->verbosity >= 3)
  2307. VPrintf1( "codes %d\n", s->numZ-nBytes );
  2308. }
  2309. /*---------------------------------------------------*/
  2310. void BZ2_compressBlock ( EState* s, Bool is_last_block )
  2311. {
  2312. if (s->nblock > 0) {
  2313. BZ_FINALISE_CRC ( s->blockCRC );
  2314. s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
  2315. s->combinedCRC ^= s->blockCRC;
  2316. if (s->blockNo > 1) s->numZ = 0;
  2317. if (s->verbosity >= 2)
  2318. VPrintf4( " block %d: crc = 0x%8x, "
  2319. "combined CRC = 0x%8x, size = %d\n",
  2320. s->blockNo, s->blockCRC, s->combinedCRC, s->nblock );
  2321. BZ2_blockSort ( s );
  2322. }
  2323. s->zbits = (UChar*) (&((UChar*)s->arr2)[s->nblock]);
  2324. /*-- If this is the first block, create the stream header. --*/
  2325. if (s->blockNo == 1) {
  2326. BZ2_bsInitWrite ( s );
  2327. bsPutUChar ( s, BZ_HDR_B );
  2328. bsPutUChar ( s, BZ_HDR_Z );
  2329. bsPutUChar ( s, BZ_HDR_h );
  2330. bsPutUChar ( s, (UChar)(BZ_HDR_0 + s->blockSize100k) );
  2331. }
  2332. if (s->nblock > 0) {
  2333. bsPutUChar ( s, 0x31 ); bsPutUChar ( s, 0x41 );
  2334. bsPutUChar ( s, 0x59 ); bsPutUChar ( s, 0x26 );
  2335. bsPutUChar ( s, 0x53 ); bsPutUChar ( s, 0x59 );
  2336. /*-- Now the block's CRC, so it is in a known place. --*/
  2337. bsPutUInt32 ( s, s->blockCRC );
  2338. /*--
  2339. Now a single bit indicating (non-)randomisation.
  2340. As of version 0.9.5, we use a better sorting algorithm
  2341. which makes randomisation unnecessary. So always set
  2342. the randomised bit to 'no'. Of course, the decoder
  2343. still needs to be able to handle randomised blocks
  2344. so as to maintain backwards compatibility with
  2345. older versions of bzip2.
  2346. --*/
  2347. bsW(s,1,0);
  2348. bsW ( s, 24, s->origPtr );
  2349. generateMTFValues ( s );
  2350. sendMTFValues ( s );
  2351. }
  2352. /*-- If this is the last block, add the stream trailer. --*/
  2353. if (is_last_block) {
  2354. bsPutUChar ( s, 0x17 ); bsPutUChar ( s, 0x72 );
  2355. bsPutUChar ( s, 0x45 ); bsPutUChar ( s, 0x38 );
  2356. bsPutUChar ( s, 0x50 ); bsPutUChar ( s, 0x90 );
  2357. bsPutUInt32 ( s, s->combinedCRC );
  2358. if (s->verbosity >= 2)
  2359. VPrintf1( " final combined CRC = 0x%x\n ", s->combinedCRC );
  2360. bsFinishWrite ( s );
  2361. }
  2362. }
  2363. /*-------------------------------------------------------------*/
  2364. /*--- end compress.c ---*/
  2365. /*-------------------------------------------------------------*/
  2366. /*-------------------------------------------------------------*/
  2367. /*--- Decompression machinery ---*/
  2368. /*--- decompress.c ---*/
  2369. /*-------------------------------------------------------------*/
  2370. /*---------------------------------------------------*/
  2371. static
  2372. void makeMaps_d ( DState* s )
  2373. {
  2374. Int32 i;
  2375. s->nInUse = 0;
  2376. for (i = 0; i < 256; i++)
  2377. if (s->inUse[i]) {
  2378. s->seqToUnseq[s->nInUse] = i;
  2379. s->nInUse++;
  2380. }
  2381. }
  2382. /*---------------------------------------------------*/
  2383. #define RETURN(rrr) \
  2384. { retVal = rrr; goto save_state_and_return; };
  2385. #define GET_BITS(lll,vvv,nnn) \
  2386. case lll: s->state = lll; \
  2387. while (True) { \
  2388. if (s->bsLive >= nnn) { \
  2389. UInt32 v; \
  2390. v = (s->bsBuff >> \
  2391. (s->bsLive-nnn)) & ((1 << nnn)-1); \
  2392. s->bsLive -= nnn; \
  2393. vvv = v; \
  2394. break; \
  2395. } \
  2396. if (s->strm->avail_in == 0) RETURN(BZ_OK); \
  2397. s->bsBuff \
  2398. = (s->bsBuff << 8) | \
  2399. ((UInt32) \
  2400. (*((UChar*)(s->strm->next_in)))); \
  2401. s->bsLive += 8; \
  2402. s->strm->next_in++; \
  2403. s->strm->avail_in--; \
  2404. s->strm->total_in_lo32++; \
  2405. if (s->strm->total_in_lo32 == 0) \
  2406. s->strm->total_in_hi32++; \
  2407. }
  2408. #define GET_UCHAR(lll,uuu) \
  2409. GET_BITS(lll,uuu,8)
  2410. #define GET_BIT(lll,uuu) \
  2411. GET_BITS(lll,uuu,1)
  2412. /*---------------------------------------------------*/
  2413. #define GET_MTF_VAL(label1,label2,lval) \
  2414. { \
  2415. if (groupPos == 0) { \
  2416. groupNo++; \
  2417. if (groupNo >= nSelectors) \
  2418. RETURN(BZ_DATA_ERROR); \
  2419. groupPos = BZ_G_SIZE; \
  2420. gSel = s->selector[groupNo]; \
  2421. gMinlen = s->minLens[gSel]; \
  2422. gLimit = &(s->limit[gSel][0]); \
  2423. gPerm = &(s->perm[gSel][0]); \
  2424. gBase = &(s->base[gSel][0]); \
  2425. } \
  2426. groupPos--; \
  2427. zn = gMinlen; \
  2428. GET_BITS(label1, zvec, zn); \
  2429. while (1) { \
  2430. if (zn > 20 /* the longest code */) \
  2431. RETURN(BZ_DATA_ERROR); \
  2432. if (zvec <= gLimit[zn]) break; \
  2433. zn++; \
  2434. GET_BIT(label2, zj); \
  2435. zvec = (zvec << 1) | zj; \
  2436. }; \
  2437. if (zvec - gBase[zn] < 0 \
  2438. || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \
  2439. RETURN(BZ_DATA_ERROR); \
  2440. lval = gPerm[zvec - gBase[zn]]; \
  2441. }
  2442. /*---------------------------------------------------*/
  2443. Int32 BZ2_decompress ( DState* s )
  2444. {
  2445. UChar uc;
  2446. Int32 retVal;
  2447. Int32 minLen, maxLen;
  2448. bz_stream* strm = s->strm;
  2449. /* stuff that needs to be saved/restored */
  2450. Int32 i;
  2451. Int32 j;
  2452. Int32 t;
  2453. Int32 alphaSize;
  2454. Int32 nGroups;
  2455. Int32 nSelectors;
  2456. Int32 EOB;
  2457. Int32 groupNo;
  2458. Int32 groupPos;
  2459. Int32 nextSym;
  2460. Int32 nblockMAX;
  2461. Int32 nblock;
  2462. Int32 es;
  2463. Int32 N;
  2464. Int32 curr;
  2465. Int32 zt;
  2466. Int32 zn;
  2467. Int32 zvec;
  2468. Int32 zj;
  2469. Int32 gSel;
  2470. Int32 gMinlen;
  2471. Int32* gLimit;
  2472. Int32* gBase;
  2473. Int32* gPerm;
  2474. if (s->state == BZ_X_MAGIC_1) {
  2475. /*initialise the save area*/
  2476. s->save_i = 0;
  2477. s->save_j = 0;
  2478. s->save_t = 0;
  2479. s->save_alphaSize = 0;
  2480. s->save_nGroups = 0;
  2481. s->save_nSelectors = 0;
  2482. s->save_EOB = 0;
  2483. s->save_groupNo = 0;
  2484. s->save_groupPos = 0;
  2485. s->save_nextSym = 0;
  2486. s->save_nblockMAX = 0;
  2487. s->save_nblock = 0;
  2488. s->save_es = 0;
  2489. s->save_N = 0;
  2490. s->save_curr = 0;
  2491. s->save_zt = 0;
  2492. s->save_zn = 0;
  2493. s->save_zvec = 0;
  2494. s->save_zj = 0;
  2495. s->save_gSel = 0;
  2496. s->save_gMinlen = 0;
  2497. s->save_gLimit = NULL;
  2498. s->save_gBase = NULL;
  2499. s->save_gPerm = NULL;
  2500. }
  2501. /*restore from the save area*/
  2502. i = s->save_i;
  2503. j = s->save_j;
  2504. t = s->save_t;
  2505. alphaSize = s->save_alphaSize;
  2506. nGroups = s->save_nGroups;
  2507. nSelectors = s->save_nSelectors;
  2508. EOB = s->save_EOB;
  2509. groupNo = s->save_groupNo;
  2510. groupPos = s->save_groupPos;
  2511. nextSym = s->save_nextSym;
  2512. nblockMAX = s->save_nblockMAX;
  2513. nblock = s->save_nblock;
  2514. es = s->save_es;
  2515. N = s->save_N;
  2516. curr = s->save_curr;
  2517. zt = s->save_zt;
  2518. zn = s->save_zn;
  2519. zvec = s->save_zvec;
  2520. zj = s->save_zj;
  2521. gSel = s->save_gSel;
  2522. gMinlen = s->save_gMinlen;
  2523. gLimit = s->save_gLimit;
  2524. gBase = s->save_gBase;
  2525. gPerm = s->save_gPerm;
  2526. retVal = BZ_OK;
  2527. switch (s->state) {
  2528. GET_UCHAR(BZ_X_MAGIC_1, uc);
  2529. if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC);
  2530. GET_UCHAR(BZ_X_MAGIC_2, uc);
  2531. if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC);
  2532. GET_UCHAR(BZ_X_MAGIC_3, uc)
  2533. if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC);
  2534. GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8)
  2535. if (s->blockSize100k < (BZ_HDR_0 + 1) ||
  2536. s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC);
  2537. s->blockSize100k -= BZ_HDR_0;
  2538. if (s->smallDecompress) {
  2539. s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
  2540. s->ll4 = BZALLOC(
  2541. ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar)
  2542. );
  2543. if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
  2544. } else {
  2545. s->tt = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
  2546. if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
  2547. }
  2548. GET_UCHAR(BZ_X_BLKHDR_1, uc);
  2549. if (uc == 0x17) goto endhdr_2;
  2550. if (uc != 0x31) RETURN(BZ_DATA_ERROR);
  2551. GET_UCHAR(BZ_X_BLKHDR_2, uc);
  2552. if (uc != 0x41) RETURN(BZ_DATA_ERROR);
  2553. GET_UCHAR(BZ_X_BLKHDR_3, uc);
  2554. if (uc != 0x59) RETURN(BZ_DATA_ERROR);
  2555. GET_UCHAR(BZ_X_BLKHDR_4, uc);
  2556. if (uc != 0x26) RETURN(BZ_DATA_ERROR);
  2557. GET_UCHAR(BZ_X_BLKHDR_5, uc);
  2558. if (uc != 0x53) RETURN(BZ_DATA_ERROR);
  2559. GET_UCHAR(BZ_X_BLKHDR_6, uc);
  2560. if (uc != 0x59) RETURN(BZ_DATA_ERROR);
  2561. s->currBlockNo++;
  2562. if (s->verbosity >= 2)
  2563. VPrintf1 ( "\n [%d: huff+mtf ", s->currBlockNo );
  2564. s->storedBlockCRC = 0;
  2565. GET_UCHAR(BZ_X_BCRC_1, uc);
  2566. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  2567. GET_UCHAR(BZ_X_BCRC_2, uc);
  2568. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  2569. GET_UCHAR(BZ_X_BCRC_3, uc);
  2570. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  2571. GET_UCHAR(BZ_X_BCRC_4, uc);
  2572. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  2573. GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);
  2574. s->origPtr = 0;
  2575. GET_UCHAR(BZ_X_ORIGPTR_1, uc);
  2576. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  2577. GET_UCHAR(BZ_X_ORIGPTR_2, uc);
  2578. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  2579. GET_UCHAR(BZ_X_ORIGPTR_3, uc);
  2580. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  2581. if (s->origPtr < 0)
  2582. RETURN(BZ_DATA_ERROR);
  2583. if (s->origPtr > 10 + 100000*s->blockSize100k)
  2584. RETURN(BZ_DATA_ERROR);
  2585. /*--- Receive the mapping table ---*/
  2586. for (i = 0; i < 16; i++) {
  2587. GET_BIT(BZ_X_MAPPING_1, uc);
  2588. if (uc == 1)
  2589. s->inUse16[i] = True; else
  2590. s->inUse16[i] = False;
  2591. }
  2592. for (i = 0; i < 256; i++) s->inUse[i] = False;
  2593. for (i = 0; i < 16; i++)
  2594. if (s->inUse16[i])
  2595. for (j = 0; j < 16; j++) {
  2596. GET_BIT(BZ_X_MAPPING_2, uc);
  2597. if (uc == 1) s->inUse[i * 16 + j] = True;
  2598. }
  2599. makeMaps_d ( s );
  2600. if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
  2601. alphaSize = s->nInUse+2;
  2602. /*--- Now the selectors ---*/
  2603. GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
  2604. if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
  2605. GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
  2606. if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
  2607. for (i = 0; i < nSelectors; i++) {
  2608. j = 0;
  2609. while (True) {
  2610. GET_BIT(BZ_X_SELECTOR_3, uc);
  2611. if (uc == 0) break;
  2612. j++;
  2613. if (j >= nGroups) RETURN(BZ_DATA_ERROR);
  2614. }
  2615. s->selectorMtf[i] = j;
  2616. }
  2617. /*--- Undo the MTF values for the selectors. ---*/
  2618. {
  2619. UChar pos[BZ_N_GROUPS], tmp, v;
  2620. for (v = 0; v < nGroups; v++) pos[v] = v;
  2621. for (i = 0; i < nSelectors; i++) {
  2622. v = s->selectorMtf[i];
  2623. tmp = pos[v];
  2624. while (v > 0) { pos[v] = pos[v-1]; v--; }
  2625. pos[0] = tmp;
  2626. s->selector[i] = tmp;
  2627. }
  2628. }
  2629. /*--- Now the coding tables ---*/
  2630. for (t = 0; t < nGroups; t++) {
  2631. GET_BITS(BZ_X_CODING_1, curr, 5);
  2632. for (i = 0; i < alphaSize; i++) {
  2633. while (True) {
  2634. if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR);
  2635. GET_BIT(BZ_X_CODING_2, uc);
  2636. if (uc == 0) break;
  2637. GET_BIT(BZ_X_CODING_3, uc);
  2638. if (uc == 0) curr++; else curr--;
  2639. }
  2640. s->len[t][i] = curr;
  2641. }
  2642. }
  2643. /*--- Create the Huffman decoding tables ---*/
  2644. for (t = 0; t < nGroups; t++) {
  2645. minLen = 32;
  2646. maxLen = 0;
  2647. for (i = 0; i < alphaSize; i++) {
  2648. if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  2649. if (s->len[t][i] < minLen) minLen = s->len[t][i];
  2650. }
  2651. BZ2_hbCreateDecodeTables (
  2652. &(s->limit[t][0]),
  2653. &(s->base[t][0]),
  2654. &(s->perm[t][0]),
  2655. &(s->len[t][0]),
  2656. minLen, maxLen, alphaSize
  2657. );
  2658. s->minLens[t] = minLen;
  2659. }
  2660. /*--- Now the MTF values ---*/
  2661. EOB = s->nInUse+1;
  2662. nblockMAX = 100000 * s->blockSize100k;
  2663. groupNo = -1;
  2664. groupPos = 0;
  2665. for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
  2666. /*-- MTF init --*/
  2667. {
  2668. Int32 ii, jj, kk;
  2669. kk = MTFA_SIZE-1;
  2670. for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
  2671. for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
  2672. s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj);
  2673. kk--;
  2674. }
  2675. s->mtfbase[ii] = kk + 1;
  2676. }
  2677. }
  2678. /*-- end MTF init --*/
  2679. nblock = 0;
  2680. GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
  2681. while (True) {
  2682. if (nextSym == EOB) break;
  2683. if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
  2684. es = -1;
  2685. N = 1;
  2686. do {
  2687. if (nextSym == BZ_RUNA) es = es + (0+1) * N; else
  2688. if (nextSym == BZ_RUNB) es = es + (1+1) * N;
  2689. N = N * 2;
  2690. GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
  2691. }
  2692. while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
  2693. es++;
  2694. uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
  2695. s->unzftab[uc] += es;
  2696. if (s->smallDecompress)
  2697. while (es > 0) {
  2698. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  2699. s->ll16[nblock] = (UInt16)uc;
  2700. nblock++;
  2701. es--;
  2702. }
  2703. else
  2704. while (es > 0) {
  2705. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  2706. s->tt[nblock] = (UInt32)uc;
  2707. nblock++;
  2708. es--;
  2709. };
  2710. continue;
  2711. } else {
  2712. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  2713. /*-- uc = MTF ( nextSym-1 ) --*/
  2714. {
  2715. Int32 ii, jj, kk, pp, lno, off;
  2716. UInt32 nn;
  2717. nn = (UInt32)(nextSym - 1);
  2718. if (nn < MTFL_SIZE) {
  2719. /* avoid general-case expense */
  2720. pp = s->mtfbase[0];
  2721. uc = s->mtfa[pp+nn];
  2722. while (nn > 3) {
  2723. Int32 z = pp+nn;
  2724. s->mtfa[(z) ] = s->mtfa[(z)-1];
  2725. s->mtfa[(z)-1] = s->mtfa[(z)-2];
  2726. s->mtfa[(z)-2] = s->mtfa[(z)-3];
  2727. s->mtfa[(z)-3] = s->mtfa[(z)-4];
  2728. nn -= 4;
  2729. }
  2730. while (nn > 0) {
  2731. s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
  2732. };
  2733. s->mtfa[pp] = uc;
  2734. } else {
  2735. /* general case */
  2736. lno = nn / MTFL_SIZE;
  2737. off = nn % MTFL_SIZE;
  2738. pp = s->mtfbase[lno] + off;
  2739. uc = s->mtfa[pp];
  2740. while (pp > s->mtfbase[lno]) {
  2741. s->mtfa[pp] = s->mtfa[pp-1]; pp--;
  2742. };
  2743. s->mtfbase[lno]++;
  2744. while (lno > 0) {
  2745. s->mtfbase[lno]--;
  2746. s->mtfa[s->mtfbase[lno]]
  2747. = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
  2748. lno--;
  2749. }
  2750. s->mtfbase[0]--;
  2751. s->mtfa[s->mtfbase[0]] = uc;
  2752. if (s->mtfbase[0] == 0) {
  2753. kk = MTFA_SIZE-1;
  2754. for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
  2755. for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
  2756. s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
  2757. kk--;
  2758. }
  2759. s->mtfbase[ii] = kk + 1;
  2760. }
  2761. }
  2762. }
  2763. }
  2764. /*-- end uc = MTF ( nextSym-1 ) --*/
  2765. s->unzftab[s->seqToUnseq[uc]]++;
  2766. if (s->smallDecompress)
  2767. s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else
  2768. s->tt[nblock] = (UInt32)(s->seqToUnseq[uc]);
  2769. nblock++;
  2770. GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
  2771. continue;
  2772. }
  2773. }
  2774. /* Now we know what nblock is, we can do a better sanity
  2775. check on s->origPtr.
  2776. */
  2777. if (s->origPtr < 0 || s->origPtr >= nblock)
  2778. RETURN(BZ_DATA_ERROR);
  2779. s->state_out_len = 0;
  2780. s->state_out_ch = 0;
  2781. BZ_INITIALISE_CRC ( s->calculatedBlockCRC );
  2782. s->state = BZ_X_OUTPUT;
  2783. if (s->verbosity >= 2) VPrintf0 ( "rt+rld" );
  2784. /*-- Set up cftab to facilitate generation of T^(-1) --*/
  2785. s->cftab[0] = 0;
  2786. for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1];
  2787. for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
  2788. if (s->smallDecompress) {
  2789. /*-- Make a copy of cftab, used in generation of T --*/
  2790. for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
  2791. /*-- compute the T vector --*/
  2792. for (i = 0; i < nblock; i++) {
  2793. uc = (UChar)(s->ll16[i]);
  2794. SET_LL(i, s->cftabCopy[uc]);
  2795. s->cftabCopy[uc]++;
  2796. }
  2797. /*-- Compute T^(-1) by pointer reversal on T --*/
  2798. i = s->origPtr;
  2799. j = GET_LL(i);
  2800. do {
  2801. Int32 tmp = GET_LL(j);
  2802. SET_LL(j, i);
  2803. i = j;
  2804. j = tmp;
  2805. }
  2806. while (i != s->origPtr);
  2807. s->tPos = s->origPtr;
  2808. s->nblock_used = 0;
  2809. if (s->blockRandomised) {
  2810. BZ_RAND_INIT_MASK;
  2811. BZ_GET_SMALL(s->k0); s->nblock_used++;
  2812. BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
  2813. } else {
  2814. BZ_GET_SMALL(s->k0); s->nblock_used++;
  2815. }
  2816. } else {
  2817. /*-- compute the T^(-1) vector --*/
  2818. for (i = 0; i < nblock; i++) {
  2819. uc = (UChar)(s->tt[i] & 0xff);
  2820. s->tt[s->cftab[uc]] |= (i << 8);
  2821. s->cftab[uc]++;
  2822. }
  2823. s->tPos = s->tt[s->origPtr] >> 8;
  2824. s->nblock_used = 0;
  2825. if (s->blockRandomised) {
  2826. BZ_RAND_INIT_MASK;
  2827. BZ_GET_FAST(s->k0); s->nblock_used++;
  2828. BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
  2829. } else {
  2830. BZ_GET_FAST(s->k0); s->nblock_used++;
  2831. }
  2832. }
  2833. RETURN(BZ_OK);
  2834. endhdr_2:
  2835. GET_UCHAR(BZ_X_ENDHDR_2, uc);
  2836. if (uc != 0x72) RETURN(BZ_DATA_ERROR);
  2837. GET_UCHAR(BZ_X_ENDHDR_3, uc);
  2838. if (uc != 0x45) RETURN(BZ_DATA_ERROR);
  2839. GET_UCHAR(BZ_X_ENDHDR_4, uc);
  2840. if (uc != 0x38) RETURN(BZ_DATA_ERROR);
  2841. GET_UCHAR(BZ_X_ENDHDR_5, uc);
  2842. if (uc != 0x50) RETURN(BZ_DATA_ERROR);
  2843. GET_UCHAR(BZ_X_ENDHDR_6, uc);
  2844. if (uc != 0x90) RETURN(BZ_DATA_ERROR);
  2845. s->storedCombinedCRC = 0;
  2846. GET_UCHAR(BZ_X_CCRC_1, uc);
  2847. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  2848. GET_UCHAR(BZ_X_CCRC_2, uc);
  2849. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  2850. GET_UCHAR(BZ_X_CCRC_3, uc);
  2851. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  2852. GET_UCHAR(BZ_X_CCRC_4, uc);
  2853. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  2854. s->state = BZ_X_IDLE;
  2855. RETURN(BZ_STREAM_END);
  2856. default: AssertH ( False, 4001 );
  2857. }
  2858. AssertH ( False, 4002 );
  2859. save_state_and_return:
  2860. s->save_i = i;
  2861. s->save_j = j;
  2862. s->save_t = t;
  2863. s->save_alphaSize = alphaSize;
  2864. s->save_nGroups = nGroups;
  2865. s->save_nSelectors = nSelectors;
  2866. s->save_EOB = EOB;
  2867. s->save_groupNo = groupNo;
  2868. s->save_groupPos = groupPos;
  2869. s->save_nextSym = nextSym;
  2870. s->save_nblockMAX = nblockMAX;
  2871. s->save_nblock = nblock;
  2872. s->save_es = es;
  2873. s->save_N = N;
  2874. s->save_curr = curr;
  2875. s->save_zt = zt;
  2876. s->save_zn = zn;
  2877. s->save_zvec = zvec;
  2878. s->save_zj = zj;
  2879. s->save_gSel = gSel;
  2880. s->save_gMinlen = gMinlen;
  2881. s->save_gLimit = gLimit;
  2882. s->save_gBase = gBase;
  2883. s->save_gPerm = gPerm;
  2884. return retVal;
  2885. }
  2886. /*-------------------------------------------------------------*/
  2887. /*--- end decompress.c ---*/
  2888. /*-------------------------------------------------------------*/
  2889. /*-------------------------------------------------------------*/
  2890. /*--- Library top-level functions. ---*/
  2891. /*--- bzlib.c ---*/
  2892. /*-------------------------------------------------------------*/
  2893. /*---------------------------------------------------*/
  2894. /*--- Compression stuff ---*/
  2895. /*---------------------------------------------------*/
  2896. /*---------------------------------------------------*/
  2897. #ifndef BZ_NO_STDIO
  2898. void BZ2_bz__AssertH__fail ( int errcode )
  2899. {
  2900. fprintf(stderr,
  2901. "\n\nbzip2/libbzip2: internal error number %d.\n"
  2902. "This is a bug in bzip2/libbzip2, %s.\n"
  2903. "Please report it to me at: jseward@acm.org. If this happened\n"
  2904. "when you were using some program which uses libbzip2 as a\n"
  2905. "component, you should also report this bug to the author(s)\n"
  2906. "of that program. Please make an effort to report this bug;\n"
  2907. "timely and accurate bug reports eventually lead to higher\n"
  2908. "quality software. Thanks. Julian Seward, 30 December 2001.\n\n",
  2909. errcode,
  2910. BZ2_bzlibVersion()
  2911. );
  2912. if (errcode == 1007) {
  2913. fprintf(stderr,
  2914. "\n*** A special note about internal error number 1007 ***\n"
  2915. "\n"
  2916. "Experience suggests that a common cause of i.e. 1007\n"
  2917. "is unreliable memory or other hardware. The 1007 assertion\n"
  2918. "just happens to cross-check the results of huge numbers of\n"
  2919. "memory reads/writes, and so acts (unintendedly) as a stress\n"
  2920. "test of your memory system.\n"
  2921. "\n"
  2922. "I suggest the following: try compressing the file again,\n"
  2923. "possibly monitoring progress in detail with the -vv flag.\n"
  2924. "\n"
  2925. "* If the error cannot be reproduced, and/or happens at different\n"
  2926. " points in compression, you may have a flaky memory system.\n"
  2927. " Try a memory-test program. I have used Memtest86\n"
  2928. " (www.memtest86.com). At the time of writing it is free (GPLd).\n"
  2929. " Memtest86 tests memory much more thorougly than your BIOSs\n"
  2930. " power-on test, and may find failures that the BIOS doesn't.\n"
  2931. "\n"
  2932. "* If the error can be repeatably reproduced, this is a bug in\n"
  2933. " bzip2, and I would very much like to hear about it. Please\n"
  2934. " let me know, and, ideally, save a copy of the file causing the\n"
  2935. " problem -- without which I will be unable to investigate it.\n"
  2936. "\n"
  2937. );
  2938. }
  2939. exit(3);
  2940. }
  2941. #endif
  2942. /*---------------------------------------------------*/
  2943. static
  2944. int bz_config_ok ( void )
  2945. {
  2946. if (sizeof(int) != 4) return 0;
  2947. if (sizeof(short) != 2) return 0;
  2948. if (sizeof(char) != 1) return 0;
  2949. return 1;
  2950. }
  2951. /*---------------------------------------------------*/
  2952. static
  2953. void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
  2954. {
  2955. void* v = malloc ( items * size );
  2956. return v;
  2957. }
  2958. static
  2959. void default_bzfree ( void* opaque, void* addr )
  2960. {
  2961. if (addr != NULL) free ( addr );
  2962. }
  2963. /*---------------------------------------------------*/
  2964. static
  2965. void prepare_new_block ( EState* s )
  2966. {
  2967. Int32 i;
  2968. s->nblock = 0;
  2969. s->numZ = 0;
  2970. s->state_out_pos = 0;
  2971. BZ_INITIALISE_CRC ( s->blockCRC );
  2972. for (i = 0; i < 256; i++) s->inUse[i] = False;
  2973. s->blockNo++;
  2974. }
  2975. /*---------------------------------------------------*/
  2976. static
  2977. void init_RL ( EState* s )
  2978. {
  2979. s->state_in_ch = 256;
  2980. s->state_in_len = 0;
  2981. }
  2982. static
  2983. Bool isempty_RL ( EState* s )
  2984. {
  2985. if (s->state_in_ch < 256 && s->state_in_len > 0)
  2986. return False; else
  2987. return True;
  2988. }
  2989. /*---------------------------------------------------*/
  2990. int BZ_API(BZ2_bzCompressInit)
  2991. ( bz_stream* strm,
  2992. int blockSize100k,
  2993. int verbosity,
  2994. int workFactor )
  2995. {
  2996. Int32 n;
  2997. EState* s;
  2998. if (!bz_config_ok()) return BZ_CONFIG_ERROR;
  2999. if (strm == NULL ||
  3000. blockSize100k < 1 || blockSize100k > 9 ||
  3001. workFactor < 0 || workFactor > 250)
  3002. return BZ_PARAM_ERROR;
  3003. if (workFactor == 0) workFactor = 30;
  3004. if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
  3005. if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
  3006. s = BZALLOC( sizeof(EState) );
  3007. if (s == NULL) return BZ_MEM_ERROR;
  3008. s->strm = strm;
  3009. s->arr1 = NULL;
  3010. s->arr2 = NULL;
  3011. s->ftab = NULL;
  3012. n = 100000 * blockSize100k;
  3013. s->arr1 = BZALLOC( n * sizeof(UInt32) );
  3014. s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
  3015. s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
  3016. if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
  3017. if (s->arr1 != NULL) BZFREE(s->arr1);
  3018. if (s->arr2 != NULL) BZFREE(s->arr2);
  3019. if (s->ftab != NULL) BZFREE(s->ftab);
  3020. if (s != NULL) BZFREE(s);
  3021. return BZ_MEM_ERROR;
  3022. }
  3023. s->blockNo = 0;
  3024. s->state = BZ_S_INPUT;
  3025. s->mode = BZ_M_RUNNING;
  3026. s->combinedCRC = 0;
  3027. s->blockSize100k = blockSize100k;
  3028. s->nblockMAX = 100000 * blockSize100k - 19;
  3029. s->verbosity = verbosity;
  3030. s->workFactor = workFactor;
  3031. s->block = (UChar*)s->arr2;
  3032. s->mtfv = (UInt16*)s->arr1;
  3033. s->zbits = NULL;
  3034. s->ptr = (UInt32*)s->arr1;
  3035. strm->state = s;
  3036. strm->total_in_lo32 = 0;
  3037. strm->total_in_hi32 = 0;
  3038. strm->total_out_lo32 = 0;
  3039. strm->total_out_hi32 = 0;
  3040. init_RL ( s );
  3041. prepare_new_block ( s );
  3042. return BZ_OK;
  3043. }
  3044. /*---------------------------------------------------*/
  3045. static
  3046. void add_pair_to_block ( EState* s )
  3047. {
  3048. Int32 i;
  3049. UChar ch = (UChar)(s->state_in_ch);
  3050. for (i = 0; i < s->state_in_len; i++) {
  3051. BZ_UPDATE_CRC( s->blockCRC, ch );
  3052. }
  3053. s->inUse[s->state_in_ch] = True;
  3054. switch (s->state_in_len) {
  3055. case 1:
  3056. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3057. break;
  3058. case 2:
  3059. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3060. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3061. break;
  3062. case 3:
  3063. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3064. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3065. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3066. break;
  3067. default:
  3068. s->inUse[s->state_in_len-4] = True;
  3069. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3070. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3071. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3072. s->block[s->nblock] = (UChar)ch; s->nblock++;
  3073. s->block[s->nblock] = ((UChar)(s->state_in_len-4));
  3074. s->nblock++;
  3075. break;
  3076. }
  3077. }
  3078. /*---------------------------------------------------*/
  3079. static
  3080. void flush_RL ( EState* s )
  3081. {
  3082. if (s->state_in_ch < 256) add_pair_to_block ( s );
  3083. init_RL ( s );
  3084. }
  3085. /*---------------------------------------------------*/
  3086. #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
  3087. { \
  3088. UInt32 zchh = (UInt32)(zchh0); \
  3089. /*-- fast track the common case --*/ \
  3090. if (zchh != zs->state_in_ch && \
  3091. zs->state_in_len == 1) { \
  3092. UChar ch = (UChar)(zs->state_in_ch); \
  3093. BZ_UPDATE_CRC( zs->blockCRC, ch ); \
  3094. zs->inUse[zs->state_in_ch] = True; \
  3095. zs->block[zs->nblock] = (UChar)ch; \
  3096. zs->nblock++; \
  3097. zs->state_in_ch = zchh; \
  3098. } \
  3099. else \
  3100. /*-- general, uncommon cases --*/ \
  3101. if (zchh != zs->state_in_ch || \
  3102. zs->state_in_len == 255) { \
  3103. if (zs->state_in_ch < 256) \
  3104. add_pair_to_block ( zs ); \
  3105. zs->state_in_ch = zchh; \
  3106. zs->state_in_len = 1; \
  3107. } else { \
  3108. zs->state_in_len++; \
  3109. } \
  3110. }
  3111. /*---------------------------------------------------*/
  3112. static
  3113. Bool copy_input_until_stop ( EState* s )
  3114. {
  3115. Bool progress_in = False;
  3116. if (s->mode == BZ_M_RUNNING) {
  3117. /*-- fast track the common case --*/
  3118. while (True) {
  3119. /*-- block full? --*/
  3120. if (s->nblock >= s->nblockMAX) break;
  3121. /*-- no input? --*/
  3122. if (s->strm->avail_in == 0) break;
  3123. progress_in = True;
  3124. ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
  3125. s->strm->next_in++;
  3126. s->strm->avail_in--;
  3127. s->strm->total_in_lo32++;
  3128. if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
  3129. }
  3130. } else {
  3131. /*-- general, uncommon case --*/
  3132. while (True) {
  3133. /*-- block full? --*/
  3134. if (s->nblock >= s->nblockMAX) break;
  3135. /*-- no input? --*/
  3136. if (s->strm->avail_in == 0) break;
  3137. /*-- flush/finish end? --*/
  3138. if (s->avail_in_expect == 0) break;
  3139. progress_in = True;
  3140. ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
  3141. s->strm->next_in++;
  3142. s->strm->avail_in--;
  3143. s->strm->total_in_lo32++;
  3144. if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
  3145. s->avail_in_expect--;
  3146. }
  3147. }
  3148. return progress_in;
  3149. }
  3150. /*---------------------------------------------------*/
  3151. static
  3152. Bool copy_output_until_stop ( EState* s )
  3153. {
  3154. Bool progress_out = False;
  3155. while (True) {
  3156. /*-- no output space? --*/
  3157. if (s->strm->avail_out == 0) break;
  3158. /*-- block done? --*/
  3159. if (s->state_out_pos >= s->numZ) break;
  3160. progress_out = True;
  3161. *(s->strm->next_out) = s->zbits[s->state_out_pos];
  3162. s->state_out_pos++;
  3163. s->strm->avail_out--;
  3164. s->strm->next_out++;
  3165. s->strm->total_out_lo32++;
  3166. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  3167. }
  3168. return progress_out;
  3169. }
  3170. /*---------------------------------------------------*/
  3171. static
  3172. Bool handle_compress ( bz_stream* strm )
  3173. {
  3174. Bool progress_in = False;
  3175. Bool progress_out = False;
  3176. EState* s = strm->state;
  3177. while (True) {
  3178. if (s->state == BZ_S_OUTPUT) {
  3179. progress_out |= copy_output_until_stop ( s );
  3180. if (s->state_out_pos < s->numZ) break;
  3181. if (s->mode == BZ_M_FINISHING &&
  3182. s->avail_in_expect == 0 &&
  3183. isempty_RL(s)) break;
  3184. prepare_new_block ( s );
  3185. s->state = BZ_S_INPUT;
  3186. if (s->mode == BZ_M_FLUSHING &&
  3187. s->avail_in_expect == 0 &&
  3188. isempty_RL(s)) break;
  3189. }
  3190. if (s->state == BZ_S_INPUT) {
  3191. progress_in |= copy_input_until_stop ( s );
  3192. if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
  3193. flush_RL ( s );
  3194. BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
  3195. s->state = BZ_S_OUTPUT;
  3196. }
  3197. else
  3198. if (s->nblock >= s->nblockMAX) {
  3199. BZ2_compressBlock ( s, False );
  3200. s->state = BZ_S_OUTPUT;
  3201. }
  3202. else
  3203. if (s->strm->avail_in == 0) {
  3204. break;
  3205. }
  3206. }
  3207. }
  3208. return progress_in || progress_out;
  3209. }
  3210. /*---------------------------------------------------*/
  3211. int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
  3212. {
  3213. Bool progress;
  3214. EState* s;
  3215. if (strm == NULL) return BZ_PARAM_ERROR;
  3216. s = strm->state;
  3217. if (s == NULL) return BZ_PARAM_ERROR;
  3218. if (s->strm != strm) return BZ_PARAM_ERROR;
  3219. preswitch:
  3220. switch (s->mode) {
  3221. case BZ_M_IDLE:
  3222. return BZ_SEQUENCE_ERROR;
  3223. case BZ_M_RUNNING:
  3224. if (action == BZ_RUN) {
  3225. progress = handle_compress ( strm );
  3226. return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
  3227. }
  3228. else
  3229. if (action == BZ_FLUSH) {
  3230. s->avail_in_expect = strm->avail_in;
  3231. s->mode = BZ_M_FLUSHING;
  3232. goto preswitch;
  3233. }
  3234. else
  3235. if (action == BZ_FINISH) {
  3236. s->avail_in_expect = strm->avail_in;
  3237. s->mode = BZ_M_FINISHING;
  3238. goto preswitch;
  3239. }
  3240. else
  3241. return BZ_PARAM_ERROR;
  3242. case BZ_M_FLUSHING:
  3243. if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
  3244. if (s->avail_in_expect != s->strm->avail_in)
  3245. return BZ_SEQUENCE_ERROR;
  3246. progress = handle_compress ( strm );
  3247. if (s->avail_in_expect > 0 || !isempty_RL(s) ||
  3248. s->state_out_pos < s->numZ) return BZ_FLUSH_OK;
  3249. s->mode = BZ_M_RUNNING;
  3250. return BZ_RUN_OK;
  3251. case BZ_M_FINISHING:
  3252. if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
  3253. if (s->avail_in_expect != s->strm->avail_in)
  3254. return BZ_SEQUENCE_ERROR;
  3255. progress = handle_compress ( strm );
  3256. if (!progress) return BZ_SEQUENCE_ERROR;
  3257. if (s->avail_in_expect > 0 || !isempty_RL(s) ||
  3258. s->state_out_pos < s->numZ) return BZ_FINISH_OK;
  3259. s->mode = BZ_M_IDLE;
  3260. return BZ_STREAM_END;
  3261. }
  3262. return BZ_OK; /*--not reached--*/
  3263. }
  3264. /*---------------------------------------------------*/
  3265. int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
  3266. {
  3267. EState* s;
  3268. if (strm == NULL) return BZ_PARAM_ERROR;
  3269. s = strm->state;
  3270. if (s == NULL) return BZ_PARAM_ERROR;
  3271. if (s->strm != strm) return BZ_PARAM_ERROR;
  3272. if (s->arr1 != NULL) BZFREE(s->arr1);
  3273. if (s->arr2 != NULL) BZFREE(s->arr2);
  3274. if (s->ftab != NULL) BZFREE(s->ftab);
  3275. BZFREE(strm->state);
  3276. strm->state = NULL;
  3277. return BZ_OK;
  3278. }
  3279. /*---------------------------------------------------*/
  3280. /*--- Decompression stuff ---*/
  3281. /*---------------------------------------------------*/
  3282. /*---------------------------------------------------*/
  3283. int BZ_API(BZ2_bzDecompressInit)
  3284. ( bz_stream* strm,
  3285. int verbosity,
  3286. int small )
  3287. {
  3288. DState* s;
  3289. if (!bz_config_ok()) return BZ_CONFIG_ERROR;
  3290. if (strm == NULL) return BZ_PARAM_ERROR;
  3291. if (small != 0 && small != 1) return BZ_PARAM_ERROR;
  3292. if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR;
  3293. if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
  3294. if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
  3295. s = BZALLOC( sizeof(DState) );
  3296. if (s == NULL) return BZ_MEM_ERROR;
  3297. s->strm = strm;
  3298. strm->state = s;
  3299. s->state = BZ_X_MAGIC_1;
  3300. s->bsLive = 0;
  3301. s->bsBuff = 0;
  3302. s->calculatedCombinedCRC = 0;
  3303. strm->total_in_lo32 = 0;
  3304. strm->total_in_hi32 = 0;
  3305. strm->total_out_lo32 = 0;
  3306. strm->total_out_hi32 = 0;
  3307. s->smallDecompress = (Bool)small;
  3308. s->ll4 = NULL;
  3309. s->ll16 = NULL;
  3310. s->tt = NULL;
  3311. s->currBlockNo = 0;
  3312. s->verbosity = verbosity;
  3313. return BZ_OK;
  3314. }
  3315. /*---------------------------------------------------*/
  3316. static
  3317. void unRLE_obuf_to_output_FAST ( DState* s )
  3318. {
  3319. UChar k1;
  3320. if (s->blockRandomised) {
  3321. while (True) {
  3322. /* try to finish existing run */
  3323. while (True) {
  3324. if (s->strm->avail_out == 0) return;
  3325. if (s->state_out_len == 0) break;
  3326. *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
  3327. BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
  3328. s->state_out_len--;
  3329. s->strm->next_out++;
  3330. s->strm->avail_out--;
  3331. s->strm->total_out_lo32++;
  3332. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  3333. }
  3334. /* can a new run be started? */
  3335. if (s->nblock_used == s->save_nblock+1) return;
  3336. s->state_out_len = 1;
  3337. s->state_out_ch = s->k0;
  3338. BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
  3339. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  3340. if (s->nblock_used == s->save_nblock+1) continue;
  3341. if (k1 != s->k0) { s->k0 = k1; continue; };
  3342. s->state_out_len = 2;
  3343. BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
  3344. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  3345. if (s->nblock_used == s->save_nblock+1) continue;
  3346. if (k1 != s->k0) { s->k0 = k1; continue; };
  3347. s->state_out_len = 3;
  3348. BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
  3349. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  3350. if (s->nblock_used == s->save_nblock+1) continue;
  3351. if (k1 != s->k0) { s->k0 = k1; continue; };
  3352. BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
  3353. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  3354. s->state_out_len = ((Int32)k1) + 4;
  3355. BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK;
  3356. s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
  3357. }
  3358. } else {
  3359. /* restore */
  3360. UInt32 c_calculatedBlockCRC = s->calculatedBlockCRC;
  3361. UChar c_state_out_ch = s->state_out_ch;
  3362. Int32 c_state_out_len = s->state_out_len;
  3363. Int32 c_nblock_used = s->nblock_used;
  3364. Int32 c_k0 = s->k0;
  3365. UInt32* c_tt = s->tt;
  3366. UInt32 c_tPos = s->tPos;
  3367. char* cs_next_out = s->strm->next_out;
  3368. unsigned int cs_avail_out = s->strm->avail_out;
  3369. /* end restore */
  3370. UInt32 avail_out_INIT = cs_avail_out;
  3371. Int32 s_save_nblockPP = s->save_nblock+1;
  3372. unsigned int total_out_lo32_old;
  3373. while (True) {
  3374. /* try to finish existing run */
  3375. if (c_state_out_len > 0) {
  3376. while (True) {
  3377. if (cs_avail_out == 0) goto return_notr;
  3378. if (c_state_out_len == 1) break;
  3379. *( (UChar*)(cs_next_out) ) = c_state_out_ch;
  3380. BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
  3381. c_state_out_len--;
  3382. cs_next_out++;
  3383. cs_avail_out--;
  3384. }
  3385. s_state_out_len_eq_one:
  3386. {
  3387. if (cs_avail_out == 0) {
  3388. c_state_out_len = 1; goto return_notr;
  3389. };
  3390. *( (UChar*)(cs_next_out) ) = c_state_out_ch;
  3391. BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
  3392. cs_next_out++;
  3393. cs_avail_out--;
  3394. }
  3395. }
  3396. /* can a new run be started? */
  3397. if (c_nblock_used == s_save_nblockPP) {
  3398. c_state_out_len = 0; goto return_notr;
  3399. };
  3400. c_state_out_ch = c_k0;
  3401. BZ_GET_FAST_C(k1); c_nblock_used++;
  3402. if (k1 != c_k0) {
  3403. c_k0 = k1; goto s_state_out_len_eq_one;
  3404. };
  3405. if (c_nblock_used == s_save_nblockPP)
  3406. goto s_state_out_len_eq_one;
  3407. c_state_out_len = 2;
  3408. BZ_GET_FAST_C(k1); c_nblock_used++;
  3409. if (c_nblock_used == s_save_nblockPP) continue;
  3410. if (k1 != c_k0) { c_k0 = k1; continue; };
  3411. c_state_out_len = 3;
  3412. BZ_GET_FAST_C(k1); c_nblock_used++;
  3413. if (c_nblock_used == s_save_nblockPP) continue;
  3414. if (k1 != c_k0) { c_k0 = k1; continue; };
  3415. BZ_GET_FAST_C(k1); c_nblock_used++;
  3416. c_state_out_len = ((Int32)k1) + 4;
  3417. BZ_GET_FAST_C(c_k0); c_nblock_used++;
  3418. }
  3419. return_notr:
  3420. total_out_lo32_old = s->strm->total_out_lo32;
  3421. s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out);
  3422. if (s->strm->total_out_lo32 < total_out_lo32_old)
  3423. s->strm->total_out_hi32++;
  3424. /* save */
  3425. s->calculatedBlockCRC = c_calculatedBlockCRC;
  3426. s->state_out_ch = c_state_out_ch;
  3427. s->state_out_len = c_state_out_len;
  3428. s->nblock_used = c_nblock_used;
  3429. s->k0 = c_k0;
  3430. s->tt = c_tt;
  3431. s->tPos = c_tPos;
  3432. s->strm->next_out = cs_next_out;
  3433. s->strm->avail_out = cs_avail_out;
  3434. /* end save */
  3435. }
  3436. }
  3437. /*---------------------------------------------------*/
  3438. Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
  3439. {
  3440. Int32 nb, na, mid;
  3441. nb = 0;
  3442. na = 256;
  3443. do {
  3444. mid = (nb + na) >> 1;
  3445. if (indx >= cftab[mid]) nb = mid; else na = mid;
  3446. }
  3447. while (na - nb != 1);
  3448. return nb;
  3449. }
  3450. /*---------------------------------------------------*/
  3451. static
  3452. void unRLE_obuf_to_output_SMALL ( DState* s )
  3453. {
  3454. UChar k1;
  3455. if (s->blockRandomised) {
  3456. while (True) {
  3457. /* try to finish existing run */
  3458. while (True) {
  3459. if (s->strm->avail_out == 0) return;
  3460. if (s->state_out_len == 0) break;
  3461. *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
  3462. BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
  3463. s->state_out_len--;
  3464. s->strm->next_out++;
  3465. s->strm->avail_out--;
  3466. s->strm->total_out_lo32++;
  3467. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  3468. }
  3469. /* can a new run be started? */
  3470. if (s->nblock_used == s->save_nblock+1) return;
  3471. s->state_out_len = 1;
  3472. s->state_out_ch = s->k0;
  3473. BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
  3474. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  3475. if (s->nblock_used == s->save_nblock+1) continue;
  3476. if (k1 != s->k0) { s->k0 = k1; continue; };
  3477. s->state_out_len = 2;
  3478. BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
  3479. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  3480. if (s->nblock_used == s->save_nblock+1) continue;
  3481. if (k1 != s->k0) { s->k0 = k1; continue; };
  3482. s->state_out_len = 3;
  3483. BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
  3484. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  3485. if (s->nblock_used == s->save_nblock+1) continue;
  3486. if (k1 != s->k0) { s->k0 = k1; continue; };
  3487. BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
  3488. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  3489. s->state_out_len = ((Int32)k1) + 4;
  3490. BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK;
  3491. s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
  3492. }
  3493. } else {
  3494. while (True) {
  3495. /* try to finish existing run */
  3496. while (True) {
  3497. if (s->strm->avail_out == 0) return;
  3498. if (s->state_out_len == 0) break;
  3499. *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
  3500. BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
  3501. s->state_out_len--;
  3502. s->strm->next_out++;
  3503. s->strm->avail_out--;
  3504. s->strm->total_out_lo32++;
  3505. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  3506. }
  3507. /* can a new run be started? */
  3508. if (s->nblock_used == s->save_nblock+1) return;
  3509. s->state_out_len = 1;
  3510. s->state_out_ch = s->k0;
  3511. BZ_GET_SMALL(k1); s->nblock_used++;
  3512. if (s->nblock_used == s->save_nblock+1) continue;
  3513. if (k1 != s->k0) { s->k0 = k1; continue; };
  3514. s->state_out_len = 2;
  3515. BZ_GET_SMALL(k1); s->nblock_used++;
  3516. if (s->nblock_used == s->save_nblock+1) continue;
  3517. if (k1 != s->k0) { s->k0 = k1; continue; };
  3518. s->state_out_len = 3;
  3519. BZ_GET_SMALL(k1); s->nblock_used++;
  3520. if (s->nblock_used == s->save_nblock+1) continue;
  3521. if (k1 != s->k0) { s->k0 = k1; continue; };
  3522. BZ_GET_SMALL(k1); s->nblock_used++;
  3523. s->state_out_len = ((Int32)k1) + 4;
  3524. BZ_GET_SMALL(s->k0); s->nblock_used++;
  3525. }
  3526. }
  3527. }
  3528. /*---------------------------------------------------*/
  3529. int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
  3530. {
  3531. DState* s;
  3532. if (strm == NULL) return BZ_PARAM_ERROR;
  3533. s = strm->state;
  3534. if (s == NULL) return BZ_PARAM_ERROR;
  3535. if (s->strm != strm) return BZ_PARAM_ERROR;
  3536. while (True) {
  3537. if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR;
  3538. if (s->state == BZ_X_OUTPUT) {
  3539. if (s->smallDecompress)
  3540. unRLE_obuf_to_output_SMALL ( s ); else
  3541. unRLE_obuf_to_output_FAST ( s );
  3542. if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
  3543. BZ_FINALISE_CRC ( s->calculatedBlockCRC );
  3544. if (s->verbosity >= 3)
  3545. VPrintf2 ( " {0x%x, 0x%x}", s->storedBlockCRC,
  3546. s->calculatedBlockCRC );
  3547. if (s->verbosity >= 2) VPrintf0 ( "]" );
  3548. if (s->calculatedBlockCRC != s->storedBlockCRC)
  3549. return BZ_DATA_ERROR;
  3550. s->calculatedCombinedCRC
  3551. = (s->calculatedCombinedCRC << 1) |
  3552. (s->calculatedCombinedCRC >> 31);
  3553. s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
  3554. s->state = BZ_X_BLKHDR_1;
  3555. } else {
  3556. return BZ_OK;
  3557. }
  3558. }
  3559. if (s->state >= BZ_X_MAGIC_1) {
  3560. Int32 r = BZ2_decompress ( s );
  3561. if (r == BZ_STREAM_END) {
  3562. if (s->verbosity >= 3)
  3563. VPrintf2 ( "\n combined CRCs: stored = 0x%x, computed = 0x%x",
  3564. s->storedCombinedCRC, s->calculatedCombinedCRC );
  3565. if (s->calculatedCombinedCRC != s->storedCombinedCRC)
  3566. return BZ_DATA_ERROR;
  3567. return r;
  3568. }
  3569. if (s->state != BZ_X_OUTPUT) return r;
  3570. }
  3571. }
  3572. AssertH ( 0, 6001 );
  3573. return 0; /*NOTREACHED*/
  3574. }
  3575. /*---------------------------------------------------*/
  3576. int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm )
  3577. {
  3578. DState* s;
  3579. if (strm == NULL) return BZ_PARAM_ERROR;
  3580. s = strm->state;
  3581. if (s == NULL) return BZ_PARAM_ERROR;
  3582. if (s->strm != strm) return BZ_PARAM_ERROR;
  3583. if (s->tt != NULL) BZFREE(s->tt);
  3584. if (s->ll16 != NULL) BZFREE(s->ll16);
  3585. if (s->ll4 != NULL) BZFREE(s->ll4);
  3586. BZFREE(strm->state);
  3587. strm->state = NULL;
  3588. return BZ_OK;
  3589. }
  3590. #ifndef BZ_NO_STDIO
  3591. /*---------------------------------------------------*/
  3592. /*--- File I/O stuff ---*/
  3593. /*---------------------------------------------------*/
  3594. #define BZ_SETERR(eee) \
  3595. { \
  3596. if (bzerror != NULL) *bzerror = eee; \
  3597. if (bzf != NULL) bzf->lastErr = eee; \
  3598. }
  3599. typedef
  3600. struct {
  3601. FILE* handle;
  3602. Char buf[BZ_MAX_UNUSED];
  3603. Int32 bufN;
  3604. Bool writing;
  3605. bz_stream strm;
  3606. Int32 lastErr;
  3607. Bool initialisedOk;
  3608. }
  3609. bzFile;
  3610. /*---------------------------------------------*/
  3611. static Bool myfeof ( FILE* f )
  3612. {
  3613. Int32 c = fgetc ( f );
  3614. if (c == EOF) return True;
  3615. ungetc ( c, f );
  3616. return False;
  3617. }
  3618. /*---------------------------------------------------*/
  3619. BZFILE* BZ_API(BZ2_bzWriteOpen)
  3620. ( int* bzerror,
  3621. FILE* f,
  3622. int blockSize100k,
  3623. int verbosity,
  3624. int workFactor )
  3625. {
  3626. Int32 ret;
  3627. bzFile* bzf = NULL;
  3628. BZ_SETERR(BZ_OK);
  3629. if (f == NULL ||
  3630. (blockSize100k < 1 || blockSize100k > 9) ||
  3631. (workFactor < 0 || workFactor > 250) ||
  3632. (verbosity < 0 || verbosity > 4))
  3633. { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
  3634. if (ferror(f))
  3635. { BZ_SETERR(BZ_IO_ERROR); return NULL; };
  3636. bzf = malloc ( sizeof(bzFile) );
  3637. if (bzf == NULL)
  3638. { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
  3639. BZ_SETERR(BZ_OK);
  3640. bzf->initialisedOk = False;
  3641. bzf->bufN = 0;
  3642. bzf->handle = f;
  3643. bzf->writing = True;
  3644. bzf->strm.bzalloc = NULL;
  3645. bzf->strm.bzfree = NULL;
  3646. bzf->strm.opaque = NULL;
  3647. if (workFactor == 0) workFactor = 30;
  3648. ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k,
  3649. verbosity, workFactor );
  3650. if (ret != BZ_OK)
  3651. { BZ_SETERR(ret); free(bzf); return NULL; };
  3652. bzf->strm.avail_in = 0;
  3653. bzf->initialisedOk = True;
  3654. return bzf;
  3655. }
  3656. /*---------------------------------------------------*/
  3657. void BZ_API(BZ2_bzWrite)
  3658. ( int* bzerror,
  3659. BZFILE* b,
  3660. void* buf,
  3661. int len )
  3662. {
  3663. Int32 n, n2, ret;
  3664. bzFile* bzf = (bzFile*)b;
  3665. BZ_SETERR(BZ_OK);
  3666. if (bzf == NULL || buf == NULL || len < 0)
  3667. { BZ_SETERR(BZ_PARAM_ERROR); return; };
  3668. if (!(bzf->writing))
  3669. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  3670. if (ferror(bzf->handle))
  3671. { BZ_SETERR(BZ_IO_ERROR); return; };
  3672. if (len == 0)
  3673. { BZ_SETERR(BZ_OK); return; };
  3674. bzf->strm.avail_in = len;
  3675. bzf->strm.next_in = buf;
  3676. while (True) {
  3677. bzf->strm.avail_out = BZ_MAX_UNUSED;
  3678. bzf->strm.next_out = bzf->buf;
  3679. ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN );
  3680. if (ret != BZ_RUN_OK)
  3681. { BZ_SETERR(ret); return; };
  3682. if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
  3683. n = BZ_MAX_UNUSED - bzf->strm.avail_out;
  3684. n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
  3685. n, bzf->handle );
  3686. if (n != n2 || ferror(bzf->handle))
  3687. { BZ_SETERR(BZ_IO_ERROR); return; };
  3688. }
  3689. if (bzf->strm.avail_in == 0)
  3690. { BZ_SETERR(BZ_OK); return; };
  3691. }
  3692. }
  3693. /*---------------------------------------------------*/
  3694. void BZ_API(BZ2_bzWriteClose)
  3695. ( int* bzerror,
  3696. BZFILE* b,
  3697. int abandon,
  3698. unsigned int* nbytes_in,
  3699. unsigned int* nbytes_out )
  3700. {
  3701. BZ2_bzWriteClose64 ( bzerror, b, abandon,
  3702. nbytes_in, NULL, nbytes_out, NULL );
  3703. }
  3704. void BZ_API(BZ2_bzWriteClose64)
  3705. ( int* bzerror,
  3706. BZFILE* b,
  3707. int abandon,
  3708. unsigned int* nbytes_in_lo32,
  3709. unsigned int* nbytes_in_hi32,
  3710. unsigned int* nbytes_out_lo32,
  3711. unsigned int* nbytes_out_hi32 )
  3712. {
  3713. Int32 n, n2, ret;
  3714. bzFile* bzf = (bzFile*)b;
  3715. if (bzf == NULL)
  3716. { BZ_SETERR(BZ_OK); return; };
  3717. if (!(bzf->writing))
  3718. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  3719. if (ferror(bzf->handle))
  3720. { BZ_SETERR(BZ_IO_ERROR); return; };
  3721. if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0;
  3722. if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0;
  3723. if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0;
  3724. if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0;
  3725. if ((!abandon) && bzf->lastErr == BZ_OK) {
  3726. while (True) {
  3727. bzf->strm.avail_out = BZ_MAX_UNUSED;
  3728. bzf->strm.next_out = bzf->buf;
  3729. ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH );
  3730. if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
  3731. { BZ_SETERR(ret); return; };
  3732. if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
  3733. n = BZ_MAX_UNUSED - bzf->strm.avail_out;
  3734. n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
  3735. n, bzf->handle );
  3736. if (n != n2 || ferror(bzf->handle))
  3737. { BZ_SETERR(BZ_IO_ERROR); return; };
  3738. }
  3739. if (ret == BZ_STREAM_END) break;
  3740. }
  3741. }
  3742. if ( !abandon && !ferror ( bzf->handle ) ) {
  3743. fflush ( bzf->handle );
  3744. if (ferror(bzf->handle))
  3745. { BZ_SETERR(BZ_IO_ERROR); return; };
  3746. }
  3747. if (nbytes_in_lo32 != NULL)
  3748. *nbytes_in_lo32 = bzf->strm.total_in_lo32;
  3749. if (nbytes_in_hi32 != NULL)
  3750. *nbytes_in_hi32 = bzf->strm.total_in_hi32;
  3751. if (nbytes_out_lo32 != NULL)
  3752. *nbytes_out_lo32 = bzf->strm.total_out_lo32;
  3753. if (nbytes_out_hi32 != NULL)
  3754. *nbytes_out_hi32 = bzf->strm.total_out_hi32;
  3755. BZ_SETERR(BZ_OK);
  3756. BZ2_bzCompressEnd ( &(bzf->strm) );
  3757. free ( bzf );
  3758. }
  3759. /*---------------------------------------------------*/
  3760. BZFILE* BZ_API(BZ2_bzReadOpen)
  3761. ( int* bzerror,
  3762. FILE* f,
  3763. int verbosity,
  3764. int small,
  3765. void* unused,
  3766. int nUnused )
  3767. {
  3768. bzFile* bzf = NULL;
  3769. int ret;
  3770. BZ_SETERR(BZ_OK);
  3771. if (f == NULL ||
  3772. (small != 0 && small != 1) ||
  3773. (verbosity < 0 || verbosity > 4) ||
  3774. (unused == NULL && nUnused != 0) ||
  3775. (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
  3776. { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
  3777. if (ferror(f))
  3778. { BZ_SETERR(BZ_IO_ERROR); return NULL; };
  3779. bzf = malloc ( sizeof(bzFile) );
  3780. if (bzf == NULL)
  3781. { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
  3782. BZ_SETERR(BZ_OK);
  3783. bzf->initialisedOk = False;
  3784. bzf->handle = f;
  3785. bzf->bufN = 0;
  3786. bzf->writing = False;
  3787. bzf->strm.bzalloc = NULL;
  3788. bzf->strm.bzfree = NULL;
  3789. bzf->strm.opaque = NULL;
  3790. while (nUnused > 0) {
  3791. bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
  3792. unused = ((void*)( 1 + ((UChar*)(unused)) ));
  3793. nUnused--;
  3794. }
  3795. ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
  3796. if (ret != BZ_OK)
  3797. { BZ_SETERR(ret); free(bzf); return NULL; };
  3798. bzf->strm.avail_in = bzf->bufN;
  3799. bzf->strm.next_in = bzf->buf;
  3800. bzf->initialisedOk = True;
  3801. return bzf;
  3802. }
  3803. /*---------------------------------------------------*/
  3804. void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
  3805. {
  3806. bzFile* bzf = (bzFile*)b;
  3807. BZ_SETERR(BZ_OK);
  3808. if (bzf == NULL)
  3809. { BZ_SETERR(BZ_OK); return; };
  3810. if (bzf->writing)
  3811. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  3812. if (bzf->initialisedOk)
  3813. (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
  3814. free ( bzf );
  3815. }
  3816. /*---------------------------------------------------*/
  3817. int BZ_API(BZ2_bzRead)
  3818. ( int* bzerror,
  3819. BZFILE* b,
  3820. void* buf,
  3821. int len )
  3822. {
  3823. Int32 n, ret;
  3824. bzFile* bzf = (bzFile*)b;
  3825. BZ_SETERR(BZ_OK);
  3826. if (bzf == NULL || buf == NULL || len < 0)
  3827. { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
  3828. if (bzf->writing)
  3829. { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
  3830. if (len == 0)
  3831. { BZ_SETERR(BZ_OK); return 0; };
  3832. bzf->strm.avail_out = len;
  3833. bzf->strm.next_out = buf;
  3834. while (True) {
  3835. if (ferror(bzf->handle))
  3836. { BZ_SETERR(BZ_IO_ERROR); return 0; };
  3837. if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) {
  3838. n = fread ( bzf->buf, sizeof(UChar),
  3839. BZ_MAX_UNUSED, bzf->handle );
  3840. if (ferror(bzf->handle))
  3841. { BZ_SETERR(BZ_IO_ERROR); return 0; };
  3842. bzf->bufN = n;
  3843. bzf->strm.avail_in = bzf->bufN;
  3844. bzf->strm.next_in = bzf->buf;
  3845. }
  3846. ret = BZ2_bzDecompress ( &(bzf->strm) );
  3847. if (ret != BZ_OK && ret != BZ_STREAM_END)
  3848. { BZ_SETERR(ret); return 0; };
  3849. if (ret == BZ_OK && myfeof(bzf->handle) &&
  3850. bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
  3851. { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
  3852. if (ret == BZ_STREAM_END)
  3853. { BZ_SETERR(BZ_STREAM_END);
  3854. return len - bzf->strm.avail_out; };
  3855. if (bzf->strm.avail_out == 0)
  3856. { BZ_SETERR(BZ_OK); return len; };
  3857. }
  3858. return 0; /*not reached*/
  3859. }
  3860. /*---------------------------------------------------*/
  3861. void BZ_API(BZ2_bzReadGetUnused)
  3862. ( int* bzerror,
  3863. BZFILE* b,
  3864. void** unused,
  3865. int* nUnused )
  3866. {
  3867. bzFile* bzf = (bzFile*)b;
  3868. if (bzf == NULL)
  3869. { BZ_SETERR(BZ_PARAM_ERROR); return; };
  3870. if (bzf->lastErr != BZ_STREAM_END)
  3871. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  3872. if (unused == NULL || nUnused == NULL)
  3873. { BZ_SETERR(BZ_PARAM_ERROR); return; };
  3874. BZ_SETERR(BZ_OK);
  3875. *nUnused = bzf->strm.avail_in;
  3876. *unused = bzf->strm.next_in;
  3877. }
  3878. #endif
  3879. /*---------------------------------------------------*/
  3880. /*--- Misc convenience stuff ---*/
  3881. /*---------------------------------------------------*/
  3882. /*---------------------------------------------------*/
  3883. int BZ_API(BZ2_bzBuffToBuffCompress)
  3884. ( char* dest,
  3885. unsigned int* destLen,
  3886. char* source,
  3887. unsigned int sourceLen,
  3888. int blockSize100k,
  3889. int verbosity,
  3890. int workFactor )
  3891. {
  3892. bz_stream strm;
  3893. int ret;
  3894. if (dest == NULL || destLen == NULL ||
  3895. source == NULL ||
  3896. blockSize100k < 1 || blockSize100k > 9 ||
  3897. verbosity < 0 || verbosity > 4 ||
  3898. workFactor < 0 || workFactor > 250)
  3899. return BZ_PARAM_ERROR;
  3900. if (workFactor == 0) workFactor = 30;
  3901. strm.bzalloc = NULL;
  3902. strm.bzfree = NULL;
  3903. strm.opaque = NULL;
  3904. ret = BZ2_bzCompressInit ( &strm, blockSize100k,
  3905. verbosity, workFactor );
  3906. if (ret != BZ_OK) return ret;
  3907. strm.next_in = source;
  3908. strm.next_out = dest;
  3909. strm.avail_in = sourceLen;
  3910. strm.avail_out = *destLen;
  3911. ret = BZ2_bzCompress ( &strm, BZ_FINISH );
  3912. if (ret == BZ_FINISH_OK) goto output_overflow;
  3913. if (ret != BZ_STREAM_END) goto errhandler;
  3914. /* normal termination */
  3915. *destLen -= strm.avail_out;
  3916. BZ2_bzCompressEnd ( &strm );
  3917. return BZ_OK;
  3918. output_overflow:
  3919. BZ2_bzCompressEnd ( &strm );
  3920. return BZ_OUTBUFF_FULL;
  3921. errhandler:
  3922. BZ2_bzCompressEnd ( &strm );
  3923. return ret;
  3924. }
  3925. /*---------------------------------------------------*/
  3926. int BZ_API(BZ2_bzBuffToBuffDecompress)
  3927. ( char* dest,
  3928. unsigned int* destLen,
  3929. char* source,
  3930. unsigned int sourceLen,
  3931. int small,
  3932. int verbosity )
  3933. {
  3934. bz_stream strm;
  3935. int ret;
  3936. if (dest == NULL || destLen == NULL ||
  3937. source == NULL ||
  3938. (small != 0 && small != 1) ||
  3939. verbosity < 0 || verbosity > 4)
  3940. return BZ_PARAM_ERROR;
  3941. strm.bzalloc = NULL;
  3942. strm.bzfree = NULL;
  3943. strm.opaque = NULL;
  3944. ret = BZ2_bzDecompressInit ( &strm, verbosity, small );
  3945. if (ret != BZ_OK) return ret;
  3946. strm.next_in = source;
  3947. strm.next_out = dest;
  3948. strm.avail_in = sourceLen;
  3949. strm.avail_out = *destLen;
  3950. ret = BZ2_bzDecompress ( &strm );
  3951. if (ret == BZ_OK) goto output_overflow_or_eof;
  3952. if (ret != BZ_STREAM_END) goto errhandler;
  3953. /* normal termination */
  3954. *destLen -= strm.avail_out;
  3955. BZ2_bzDecompressEnd ( &strm );
  3956. return BZ_OK;
  3957. output_overflow_or_eof:
  3958. if (strm.avail_out > 0) {
  3959. BZ2_bzDecompressEnd ( &strm );
  3960. return BZ_UNEXPECTED_EOF;
  3961. } else {
  3962. BZ2_bzDecompressEnd ( &strm );
  3963. return BZ_OUTBUFF_FULL;
  3964. };
  3965. errhandler:
  3966. BZ2_bzDecompressEnd ( &strm );
  3967. return ret;
  3968. }
  3969. /*---------------------------------------------------*/
  3970. /*--
  3971. Code contributed by Yoshioka Tsuneo
  3972. (QWF00133@niftyserve.or.jp/tsuneo-y@is.aist-nara.ac.jp),
  3973. to support better zlib compatibility.
  3974. This code is not _officially_ part of libbzip2 (yet);
  3975. I haven't tested it, documented it, or considered the
  3976. threading-safeness of it.
  3977. If this code breaks, please contact both Yoshioka and me.
  3978. --*/
  3979. /*---------------------------------------------------*/
  3980. /*---------------------------------------------------*/
  3981. /*--
  3982. return version like "0.9.0c".
  3983. --*/
  3984. const char * BZ_API(BZ2_bzlibVersion)(void)
  3985. {
  3986. return BZ_VERSION;
  3987. }
  3988. #ifndef BZ_NO_STDIO
  3989. /*---------------------------------------------------*/
  3990. static
  3991. BZFILE * bzopen_or_bzdopen
  3992. ( const char *path, /* no use when bzdopen */
  3993. int fd, /* no use when bzdopen */
  3994. const char *mode,
  3995. int open_mode) /* bzopen: 0, bzdopen:1 */
  3996. {
  3997. int bzerr;
  3998. char unused[BZ_MAX_UNUSED];
  3999. int blockSize100k = 9;
  4000. int writing = 0;
  4001. char mode2[10] = "";
  4002. FILE *fp = NULL;
  4003. BZFILE *bzfp = NULL;
  4004. int verbosity = 0;
  4005. int workFactor = 30;
  4006. int smallMode = 0;
  4007. int nUnused = 0;
  4008. if (mode == NULL) return NULL;
  4009. while (*mode) {
  4010. switch (*mode) {
  4011. case 'r':
  4012. writing = 0; break;
  4013. case 'w':
  4014. writing = 1; break;
  4015. case 's':
  4016. smallMode = 1; break;
  4017. default:
  4018. if (isdigit((int)(*mode))) {
  4019. blockSize100k = *mode-BZ_HDR_0;
  4020. }
  4021. }
  4022. mode++;
  4023. }
  4024. strcat(mode2, writing ? "w" : "r" );
  4025. strcat(mode2,"b"); /* binary mode */
  4026. if (open_mode==0) {
  4027. if (path==NULL || strcmp(path,"")==0) {
  4028. fp = (writing ? stdout : stdin);
  4029. } else {
  4030. fp = fopen(path,mode2);
  4031. }
  4032. } else {
  4033. #ifdef BZ_STRICT_ANSI
  4034. fp = NULL;
  4035. #else
  4036. fp = fdopen(fd,mode2);
  4037. #endif
  4038. }
  4039. if (fp == NULL) return NULL;
  4040. if (writing) {
  4041. /* Guard against total chaos and anarchy -- JRS */
  4042. if (blockSize100k < 1) blockSize100k = 1;
  4043. if (blockSize100k > 9) blockSize100k = 9;
  4044. bzfp = BZ2_bzWriteOpen(&bzerr,fp,blockSize100k,
  4045. verbosity,workFactor);
  4046. } else {
  4047. bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
  4048. unused,nUnused);
  4049. }
  4050. if (bzfp == NULL) {
  4051. if (fp != stdin && fp != stdout) fclose(fp);
  4052. return NULL;
  4053. }
  4054. return bzfp;
  4055. }
  4056. /*---------------------------------------------------*/
  4057. /*--
  4058. open file for read or write.
  4059. ex) bzopen("file","w9")
  4060. case path="" or NULL => use stdin or stdout.
  4061. --*/
  4062. BZFILE * BZ_API(BZ2_bzopen)
  4063. ( const char *path,
  4064. const char *mode )
  4065. {
  4066. return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
  4067. }
  4068. /*---------------------------------------------------*/
  4069. BZFILE * BZ_API(BZ2_bzdopen)
  4070. ( int fd,
  4071. const char *mode )
  4072. {
  4073. return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
  4074. }
  4075. /*---------------------------------------------------*/
  4076. int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
  4077. {
  4078. int bzerr, nread;
  4079. if (((bzFile*)b)->lastErr == BZ_STREAM_END) return 0;
  4080. nread = BZ2_bzRead(&bzerr,b,buf,len);
  4081. if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) {
  4082. return nread;
  4083. } else {
  4084. return -1;
  4085. }
  4086. }
  4087. /*---------------------------------------------------*/
  4088. int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
  4089. {
  4090. int bzerr;
  4091. BZ2_bzWrite(&bzerr,b,buf,len);
  4092. if(bzerr == BZ_OK){
  4093. return len;
  4094. }else{
  4095. return -1;
  4096. }
  4097. }
  4098. /*---------------------------------------------------*/
  4099. int BZ_API(BZ2_bzflush) (BZFILE *b)
  4100. {
  4101. /* do nothing now... */
  4102. return 0;
  4103. }
  4104. /*---------------------------------------------------*/
  4105. void BZ_API(BZ2_bzclose) (BZFILE* b)
  4106. {
  4107. int bzerr;
  4108. FILE *fp = ((bzFile *)b)->handle;
  4109. if (b==NULL) {return;}
  4110. if(((bzFile*)b)->writing){
  4111. BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
  4112. if(bzerr != BZ_OK){
  4113. BZ2_bzWriteClose(NULL,b,1,NULL,NULL);
  4114. }
  4115. }else{
  4116. BZ2_bzReadClose(&bzerr,b);
  4117. }
  4118. if(fp!=stdin && fp!=stdout){
  4119. fclose(fp);
  4120. }
  4121. }
  4122. /*---------------------------------------------------*/
  4123. /*--
  4124. return last error code
  4125. --*/
  4126. static char *bzerrorstrings[] = {
  4127. "OK"
  4128. ,"SEQUENCE_ERROR"
  4129. ,"PARAM_ERROR"
  4130. ,"MEM_ERROR"
  4131. ,"DATA_ERROR"
  4132. ,"DATA_ERROR_MAGIC"
  4133. ,"IO_ERROR"
  4134. ,"UNEXPECTED_EOF"
  4135. ,"OUTBUFF_FULL"
  4136. ,"CONFIG_ERROR"
  4137. ,"???" /* for future */
  4138. ,"???" /* for future */
  4139. ,"???" /* for future */
  4140. ,"???" /* for future */
  4141. ,"???" /* for future */
  4142. ,"???" /* for future */
  4143. };
  4144. const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
  4145. {
  4146. int err = ((bzFile *)b)->lastErr;
  4147. if(err>0) err = 0;
  4148. *errnum = err;
  4149. return bzerrorstrings[err*-1];
  4150. }
  4151. #endif
  4152. /*-------------------------------------------------------------*/
  4153. /*--- end bzlib.c ---*/
  4154. /*-------------------------------------------------------------*/
  4155. /*-----------------------------------------------------------*/
  4156. /*--- A block-sorting, lossless compressor bzip2.c ---*/
  4157. /*-----------------------------------------------------------*/
  4158. /*----------------------------------------------------*/
  4159. /*--- IMPORTANT ---*/
  4160. /*----------------------------------------------------*/
  4161. /*--
  4162. WARNING:
  4163. This program and library (attempts to) compress data by
  4164. performing several non-trivial transformations on it.
  4165. Unless you are 100% familiar with *all* the algorithms
  4166. contained herein, and with the consequences of modifying them,
  4167. you should NOT meddle with the compression or decompression
  4168. machinery. Incorrect changes can and very likely *will*
  4169. lead to disasterous loss of data.
  4170. DISCLAIMER:
  4171. I TAKE NO RESPONSIBILITY FOR ANY LOSS OF DATA ARISING FROM THE
  4172. USE OF THIS PROGRAM, HOWSOEVER CAUSED.
  4173. Every compression of a file implies an assumption that the
  4174. compressed file can be decompressed to reproduce the original.
  4175. Great efforts in design, coding and testing have been made to
  4176. ensure that this program works correctly. However, the
  4177. complexity of the algorithms, and, in particular, the presence
  4178. of various special cases in the code which occur with very low
  4179. but non-zero probability make it impossible to rule out the
  4180. possibility of bugs remaining in the program. DO NOT COMPRESS
  4181. ANY DATA WITH THIS PROGRAM AND/OR LIBRARY UNLESS YOU ARE PREPARED
  4182. TO ACCEPT THE POSSIBILITY, HOWEVER SMALL, THAT THE DATA WILL
  4183. NOT BE RECOVERABLE.
  4184. That is not to say this program is inherently unreliable.
  4185. Indeed, I very much hope the opposite is true. bzip2/libbzip2
  4186. has been carefully constructed and extensively tested.
  4187. PATENTS:
  4188. To the best of my knowledge, bzip2/libbzip2 does not use any
  4189. patented algorithms. However, I do not have the resources
  4190. available to carry out a full patent search. Therefore I cannot
  4191. give any guarantee of the above statement.
  4192. --*/
  4193. /*----------------------------------------------------*/
  4194. /*--- and now for something much more pleasant :-) ---*/
  4195. /*----------------------------------------------------*/
  4196. /*---------------------------------------------*/
  4197. /*--
  4198. Place a 1 beside your platform, and 0 elsewhere.
  4199. --*/
  4200. /*--
  4201. Generic 32-bit Unix.
  4202. Also works on 64-bit Unix boxes.
  4203. This is the default.
  4204. --*/
  4205. #define BZ_UNIX 1
  4206. /*--
  4207. Win32, as seen by Jacob Navia's excellent
  4208. port of (Chris Fraser & David Hanson)'s excellent
  4209. lcc compiler. Or with MS Visual C.
  4210. This is selected automatically if compiled by a compiler which
  4211. defines _WIN32, not including the Cygwin GCC.
  4212. --*/
  4213. #define BZ_LCCWIN32 0
  4214. #if defined(_WIN32) && !defined(__CYGWIN__)
  4215. #undef BZ_LCCWIN32
  4216. #define BZ_LCCWIN32 1
  4217. #undef BZ_UNIX
  4218. #define BZ_UNIX 0
  4219. #endif
  4220. /*---------------------------------------------*/
  4221. /*--
  4222. Some stuff for all platforms.
  4223. --*/
  4224. #include <stdio.h>
  4225. #include <stdlib.h>
  4226. #include <string.h>
  4227. #include <signal.h>
  4228. #include <math.h>
  4229. #include <errno.h>
  4230. #include <ctype.h>
  4231. #define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }
  4232. #define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); }
  4233. #define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); }
  4234. /*---------------------------------------------*/
  4235. /*--
  4236. Platform-specific stuff.
  4237. --*/
  4238. #if BZ_UNIX
  4239. # include <fcntl.h>
  4240. # include <sys/types.h>
  4241. # include <utime.h>
  4242. # include <unistd.h>
  4243. # include <sys/stat.h>
  4244. # include <sys/times.h>
  4245. # define PATH_SEP '/'
  4246. # define MY_LSTAT lstat
  4247. # define MY_STAT stat
  4248. # define MY_S_ISREG S_ISREG
  4249. # define MY_S_ISDIR S_ISDIR
  4250. # define APPEND_FILESPEC(root, name) \
  4251. root=snocString((root), (name))
  4252. # define APPEND_FLAG(root, name) \
  4253. root=snocString((root), (name))
  4254. # define SET_BINARY_MODE(fd) /**/
  4255. # ifdef __GNUC__
  4256. # define NORETURN __attribute__ ((noreturn))
  4257. # else
  4258. # define NORETURN /**/
  4259. # endif
  4260. # ifdef __DJGPP__
  4261. # include <io.h>
  4262. # include <fcntl.h>
  4263. # undef MY_LSTAT
  4264. # undef MY_STAT
  4265. # define MY_LSTAT stat
  4266. # define MY_STAT stat
  4267. # undef SET_BINARY_MODE
  4268. # define SET_BINARY_MODE(fd) \
  4269. do { \
  4270. int retVal = setmode ( fileno ( fd ), \
  4271. O_BINARY ); \
  4272. ERROR_IF_MINUS_ONE ( retVal ); \
  4273. } while ( 0 )
  4274. # endif
  4275. # ifdef __CYGWIN__
  4276. # include <io.h>
  4277. # include <fcntl.h>
  4278. # undef SET_BINARY_MODE
  4279. # define SET_BINARY_MODE(fd) \
  4280. do { \
  4281. int retVal = setmode ( fileno ( fd ), \
  4282. O_BINARY ); \
  4283. ERROR_IF_MINUS_ONE ( retVal ); \
  4284. } while ( 0 )
  4285. # endif
  4286. #endif /* BZ_UNIX */
  4287. #if BZ_LCCWIN32
  4288. # include <io.h>
  4289. # include <fcntl.h>
  4290. # include <sys\stat.h>
  4291. # define NORETURN /**/
  4292. # define PATH_SEP '\\'
  4293. # define MY_LSTAT _stat
  4294. # define MY_STAT _stat
  4295. # define MY_S_ISREG(x) ((x) & _S_IFREG)
  4296. # define MY_S_ISDIR(x) ((x) & _S_IFDIR)
  4297. # define APPEND_FLAG(root, name) \
  4298. root=snocString((root), (name))
  4299. # define APPEND_FILESPEC(root, name) \
  4300. root = snocString ((root), (name))
  4301. # define SET_BINARY_MODE(fd) \
  4302. do { \
  4303. int retVal = setmode ( fileno ( fd ), \
  4304. O_BINARY ); \
  4305. ERROR_IF_MINUS_ONE ( retVal ); \
  4306. } while ( 0 )
  4307. #endif /* BZ_LCCWIN32 */
  4308. /*---------------------------------------------*/
  4309. /*--
  4310. Some more stuff for all platforms :-)
  4311. --*/
  4312. #define True ((Bool)1)
  4313. #define False ((Bool)0)
  4314. /*--
  4315. IntNative is your platform's `native' int size.
  4316. Only here to avoid probs with 64-bit platforms.
  4317. --*/
  4318. typedef int IntNative;
  4319. /*---------------------------------------------------*/
  4320. /*--- Misc (file handling) data decls ---*/
  4321. /*---------------------------------------------------*/
  4322. Int32 verbosity;
  4323. Bool keepInputFiles, smallMode, deleteOutputOnInterrupt;
  4324. Bool forceOverwrite, testFailsExist, unzFailsExist, noisy;
  4325. Int32 numFileNames, numFilesProcessed, blockSize100k;
  4326. Int32 exitValue;
  4327. /*-- source modes; F==file, I==stdin, O==stdout --*/
  4328. #define SM_I2O 1
  4329. #define SM_F2O 2
  4330. #define SM_F2F 3
  4331. /*-- operation modes --*/
  4332. #define OM_Z 1
  4333. #define OM_UNZ 2
  4334. #define OM_TEST 3
  4335. Int32 opMode;
  4336. Int32 srcMode;
  4337. #define FILE_NAME_LEN 1034
  4338. Int32 longestFileName;
  4339. Char inName [FILE_NAME_LEN];
  4340. Char outName[FILE_NAME_LEN];
  4341. Char tmpName[FILE_NAME_LEN];
  4342. Char *progName;
  4343. Char progNameReally[FILE_NAME_LEN];
  4344. FILE *outputHandleJustInCase;
  4345. Int32 workFactor;
  4346. static void panic ( Char* ) NORETURN;
  4347. static void ioError ( void ) NORETURN;
  4348. static void outOfMemory ( void ) NORETURN;
  4349. static void configError ( void ) NORETURN;
  4350. static void crcError ( void ) NORETURN;
  4351. static void cleanUpAndFail ( Int32 ) NORETURN;
  4352. static void compressedStreamEOF ( void ) NORETURN;
  4353. static void copyFileName ( Char*, Char* );
  4354. static void* myMalloc ( Int32 );
  4355. /*---------------------------------------------------*/
  4356. /*--- An implementation of 64-bit ints. Sigh. ---*/
  4357. /*--- Roll on widespread deployment of ANSI C9X ! ---*/
  4358. /*---------------------------------------------------*/
  4359. typedef
  4360. struct { UChar b[8]; }
  4361. UInt64;
  4362. static
  4363. void uInt64_from_UInt32s ( UInt64* n, UInt32 lo32, UInt32 hi32 )
  4364. {
  4365. n->b[7] = (UChar)((hi32 >> 24) & 0xFF);
  4366. n->b[6] = (UChar)((hi32 >> 16) & 0xFF);
  4367. n->b[5] = (UChar)((hi32 >> 8) & 0xFF);
  4368. n->b[4] = (UChar) (hi32 & 0xFF);
  4369. n->b[3] = (UChar)((lo32 >> 24) & 0xFF);
  4370. n->b[2] = (UChar)((lo32 >> 16) & 0xFF);
  4371. n->b[1] = (UChar)((lo32 >> 8) & 0xFF);
  4372. n->b[0] = (UChar) (lo32 & 0xFF);
  4373. }
  4374. static
  4375. double uInt64_to_double ( UInt64* n )
  4376. {
  4377. Int32 i;
  4378. double base = 1.0;
  4379. double sum = 0.0;
  4380. for (i = 0; i < 8; i++) {
  4381. sum += base * (double)(n->b[i]);
  4382. base *= 256.0;
  4383. }
  4384. return sum;
  4385. }
  4386. static
  4387. Bool uInt64_isZero ( UInt64* n )
  4388. {
  4389. Int32 i;
  4390. for (i = 0; i < 8; i++)
  4391. if (n->b[i] != 0) return 0;
  4392. return 1;
  4393. }
  4394. /* Divide *n by 10, and return the remainder. */
  4395. static
  4396. Int32 uInt64_qrm10 ( UInt64* n )
  4397. {
  4398. UInt32 rem, tmp;
  4399. Int32 i;
  4400. rem = 0;
  4401. for (i = 7; i >= 0; i--) {
  4402. tmp = rem * 256 + n->b[i];
  4403. n->b[i] = tmp / 10;
  4404. rem = tmp % 10;
  4405. }
  4406. return rem;
  4407. }
  4408. /* ... and the Whole Entire Point of all this UInt64 stuff is
  4409. so that we can supply the following function.
  4410. */
  4411. static
  4412. void uInt64_toAscii ( char* outbuf, UInt64* n )
  4413. {
  4414. Int32 i, q;
  4415. UChar buf[32];
  4416. Int32 nBuf = 0;
  4417. UInt64 n_copy = *n;
  4418. do {
  4419. q = uInt64_qrm10 ( &n_copy );
  4420. buf[nBuf] = q + '0';
  4421. nBuf++;
  4422. } while (!uInt64_isZero(&n_copy));
  4423. outbuf[nBuf] = 0;
  4424. for (i = 0; i < nBuf; i++)
  4425. outbuf[i] = buf[nBuf-i-1];
  4426. }
  4427. /*---------------------------------------------------*/
  4428. /*--- Processing of complete files and streams ---*/
  4429. /*---------------------------------------------------*/
  4430. /*---------------------------------------------*/
  4431. /*---------------------------------------------*/
  4432. static
  4433. void compressStream ( FILE *stream, FILE *zStream )
  4434. {
  4435. BZFILE* bzf = NULL;
  4436. UChar ibuf[5000];
  4437. Int32 nIbuf;
  4438. UInt32 nbytes_in_lo32, nbytes_in_hi32;
  4439. UInt32 nbytes_out_lo32, nbytes_out_hi32;
  4440. Int32 bzerr, bzerr_dummy, ret;
  4441. SET_BINARY_MODE(stream);
  4442. SET_BINARY_MODE(zStream);
  4443. if (ferror(stream)) goto errhandler_io;
  4444. if (ferror(zStream)) goto errhandler_io;
  4445. bzf = BZ2_bzWriteOpen ( &bzerr, zStream,
  4446. blockSize100k, verbosity, workFactor );
  4447. if (bzerr != BZ_OK) goto errhandler;
  4448. if (verbosity >= 2) fprintf ( stderr, "\n" );
  4449. while (True) {
  4450. if (myfeof(stream)) break;
  4451. nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream );
  4452. if (ferror(stream)) goto errhandler_io;
  4453. if (nIbuf > 0) BZ2_bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf );
  4454. if (bzerr != BZ_OK) goto errhandler;
  4455. }
  4456. BZ2_bzWriteClose64 ( &bzerr, bzf, 0,
  4457. &nbytes_in_lo32, &nbytes_in_hi32,
  4458. &nbytes_out_lo32, &nbytes_out_hi32 );
  4459. if (bzerr != BZ_OK) goto errhandler;
  4460. if (ferror(zStream)) goto errhandler_io;
  4461. ret = fflush ( zStream );
  4462. if (ret == EOF) goto errhandler_io;
  4463. if (zStream != stdout) {
  4464. ret = fclose ( zStream );
  4465. outputHandleJustInCase = NULL;
  4466. if (ret == EOF) goto errhandler_io;
  4467. }
  4468. outputHandleJustInCase = NULL;
  4469. if (ferror(stream)) goto errhandler_io;
  4470. ret = fclose ( stream );
  4471. if (ret == EOF) goto errhandler_io;
  4472. if (verbosity >= 1) {
  4473. if (nbytes_in_lo32 == 0 && nbytes_in_hi32 == 0) {
  4474. fprintf ( stderr, " no data compressed.\n");
  4475. } else {
  4476. Char buf_nin[32], buf_nout[32];
  4477. UInt64 nbytes_in, nbytes_out;
  4478. double nbytes_in_d, nbytes_out_d;
  4479. uInt64_from_UInt32s ( &nbytes_in,
  4480. nbytes_in_lo32, nbytes_in_hi32 );
  4481. uInt64_from_UInt32s ( &nbytes_out,
  4482. nbytes_out_lo32, nbytes_out_hi32 );
  4483. nbytes_in_d = uInt64_to_double ( &nbytes_in );
  4484. nbytes_out_d = uInt64_to_double ( &nbytes_out );
  4485. uInt64_toAscii ( buf_nin, &nbytes_in );
  4486. uInt64_toAscii ( buf_nout, &nbytes_out );
  4487. fprintf ( stderr, "%6.3f:1, %6.3f bits/byte, "
  4488. "%5.2f%% saved, %s in, %s out.\n",
  4489. nbytes_in_d / nbytes_out_d,
  4490. (8.0 * nbytes_out_d) / nbytes_in_d,
  4491. 100.0 * (1.0 - nbytes_out_d / nbytes_in_d),
  4492. buf_nin,
  4493. buf_nout
  4494. );
  4495. }
  4496. }
  4497. return;
  4498. errhandler:
  4499. BZ2_bzWriteClose64 ( &bzerr_dummy, bzf, 1,
  4500. &nbytes_in_lo32, &nbytes_in_hi32,
  4501. &nbytes_out_lo32, &nbytes_out_hi32 );
  4502. switch (bzerr) {
  4503. case BZ_CONFIG_ERROR:
  4504. configError(); break;
  4505. case BZ_MEM_ERROR:
  4506. outOfMemory (); break;
  4507. case BZ_IO_ERROR:
  4508. errhandler_io:
  4509. ioError(); break;
  4510. default:
  4511. panic ( "compress:unexpected error" );
  4512. }
  4513. panic ( "compress:end" );
  4514. /*notreached*/
  4515. }
  4516. /*---------------------------------------------*/
  4517. static
  4518. Bool uncompressStream ( FILE *zStream, FILE *stream )
  4519. {
  4520. BZFILE* bzf = NULL;
  4521. Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
  4522. UChar obuf[5000];
  4523. UChar unused[BZ_MAX_UNUSED];
  4524. Int32 nUnused;
  4525. UChar* unusedTmp;
  4526. nUnused = 0;
  4527. streamNo = 0;
  4528. SET_BINARY_MODE(stream);
  4529. SET_BINARY_MODE(zStream);
  4530. if (ferror(stream)) goto errhandler_io;
  4531. if (ferror(zStream)) goto errhandler_io;
  4532. while (True) {
  4533. bzf = BZ2_bzReadOpen (
  4534. &bzerr, zStream, verbosity,
  4535. (int)smallMode, unused, nUnused
  4536. );
  4537. if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
  4538. streamNo++;
  4539. while (bzerr == BZ_OK) {
  4540. nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
  4541. if (bzerr == BZ_DATA_ERROR_MAGIC) goto trycat;
  4542. if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0)
  4543. fwrite ( obuf, sizeof(UChar), nread, stream );
  4544. if (ferror(stream)) goto errhandler_io;
  4545. }
  4546. if (bzerr != BZ_STREAM_END) goto errhandler;
  4547. BZ2_bzReadGetUnused ( &bzerr, bzf, (void**)(&unusedTmp), &nUnused );
  4548. if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
  4549. for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
  4550. BZ2_bzReadClose ( &bzerr, bzf );
  4551. if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
  4552. if (nUnused == 0 && myfeof(zStream)) break;
  4553. }
  4554. closeok:
  4555. if (ferror(zStream)) goto errhandler_io;
  4556. ret = fclose ( zStream );
  4557. if (ret == EOF) goto errhandler_io;
  4558. if (ferror(stream)) goto errhandler_io;
  4559. ret = fflush ( stream );
  4560. if (ret != 0) goto errhandler_io;
  4561. if (stream != stdout) {
  4562. ret = fclose ( stream );
  4563. outputHandleJustInCase = NULL;
  4564. if (ret == EOF) goto errhandler_io;
  4565. }
  4566. outputHandleJustInCase = NULL;
  4567. if (verbosity >= 2) fprintf ( stderr, "\n " );
  4568. return True;
  4569. trycat:
  4570. if (forceOverwrite) {
  4571. rewind(zStream);
  4572. while (True) {
  4573. if (myfeof(zStream)) break;
  4574. nread = fread ( obuf, sizeof(UChar), 5000, zStream );
  4575. if (ferror(zStream)) goto errhandler_io;
  4576. if (nread > 0) fwrite ( obuf, sizeof(UChar), nread, stream );
  4577. if (ferror(stream)) goto errhandler_io;
  4578. }
  4579. goto closeok;
  4580. }
  4581. errhandler:
  4582. BZ2_bzReadClose ( &bzerr_dummy, bzf );
  4583. switch (bzerr) {
  4584. case BZ_CONFIG_ERROR:
  4585. configError(); break;
  4586. case BZ_IO_ERROR:
  4587. errhandler_io:
  4588. ioError(); break;
  4589. case BZ_DATA_ERROR:
  4590. crcError();
  4591. case BZ_MEM_ERROR:
  4592. outOfMemory();
  4593. case BZ_UNEXPECTED_EOF:
  4594. compressedStreamEOF();
  4595. case BZ_DATA_ERROR_MAGIC:
  4596. if (zStream != stdin) fclose(zStream);
  4597. if (stream != stdout) fclose(stream);
  4598. if (streamNo == 1) {
  4599. return False;
  4600. } else {
  4601. if (noisy)
  4602. fprintf ( stderr,
  4603. "\n%s: %s: trailing garbage after EOF ignored\n",
  4604. progName, inName );
  4605. return True;
  4606. }
  4607. default:
  4608. panic ( "decompress:unexpected error" );
  4609. }
  4610. panic ( "decompress:end" );
  4611. return True; /*notreached*/
  4612. }
  4613. /*---------------------------------------------*/
  4614. static
  4615. Bool testStream ( FILE *zStream )
  4616. {
  4617. BZFILE* bzf = NULL;
  4618. Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
  4619. UChar obuf[5000];
  4620. UChar unused[BZ_MAX_UNUSED];
  4621. Int32 nUnused;
  4622. UChar* unusedTmp;
  4623. nUnused = 0;
  4624. streamNo = 0;
  4625. SET_BINARY_MODE(zStream);
  4626. if (ferror(zStream)) goto errhandler_io;
  4627. while (True) {
  4628. bzf = BZ2_bzReadOpen (
  4629. &bzerr, zStream, verbosity,
  4630. (int)smallMode, unused, nUnused
  4631. );
  4632. if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
  4633. streamNo++;
  4634. while (bzerr == BZ_OK) {
  4635. nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
  4636. if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler;
  4637. }
  4638. if (bzerr != BZ_STREAM_END) goto errhandler;
  4639. BZ2_bzReadGetUnused ( &bzerr, bzf, (void**)(&unusedTmp), &nUnused );
  4640. if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
  4641. for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
  4642. BZ2_bzReadClose ( &bzerr, bzf );
  4643. if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
  4644. if (nUnused == 0 && myfeof(zStream)) break;
  4645. }
  4646. if (ferror(zStream)) goto errhandler_io;
  4647. ret = fclose ( zStream );
  4648. if (ret == EOF) goto errhandler_io;
  4649. if (verbosity >= 2) fprintf ( stderr, "\n " );
  4650. return True;
  4651. errhandler:
  4652. BZ2_bzReadClose ( &bzerr_dummy, bzf );
  4653. if (verbosity == 0)
  4654. fprintf ( stderr, "%s: %s: ", progName, inName );
  4655. switch (bzerr) {
  4656. case BZ_CONFIG_ERROR:
  4657. configError(); break;
  4658. case BZ_IO_ERROR:
  4659. errhandler_io:
  4660. ioError(); break;
  4661. case BZ_DATA_ERROR:
  4662. fprintf ( stderr,
  4663. "data integrity (CRC) error in data\n" );
  4664. return False;
  4665. case BZ_MEM_ERROR:
  4666. outOfMemory();
  4667. case BZ_UNEXPECTED_EOF:
  4668. fprintf ( stderr,
  4669. "file ends unexpectedly\n" );
  4670. return False;
  4671. case BZ_DATA_ERROR_MAGIC:
  4672. if (zStream != stdin) fclose(zStream);
  4673. if (streamNo == 1) {
  4674. fprintf ( stderr,
  4675. "bad magic number (file not created by bzip2)\n" );
  4676. return False;
  4677. } else {
  4678. if (noisy)
  4679. fprintf ( stderr,
  4680. "trailing garbage after EOF ignored\n" );
  4681. return True;
  4682. }
  4683. default:
  4684. panic ( "test:unexpected error" );
  4685. }
  4686. panic ( "test:end" );
  4687. return True; /*notreached*/
  4688. }
  4689. /*---------------------------------------------------*/
  4690. /*--- Error [non-] handling grunge ---*/
  4691. /*---------------------------------------------------*/
  4692. /*---------------------------------------------*/
  4693. static
  4694. void setExit ( Int32 v )
  4695. {
  4696. if (v > exitValue) exitValue = v;
  4697. }
  4698. /*---------------------------------------------*/
  4699. static
  4700. void cadvise ( void )
  4701. {
  4702. if (noisy)
  4703. fprintf (
  4704. stderr,
  4705. "\nIt is possible that the compressed file(s) have become corrupted.\n"
  4706. "You can use the -tvv option to test integrity of such files.\n\n"
  4707. "You can use the `bzip2recover' program to attempt to recover\n"
  4708. "data from undamaged sections of corrupted files.\n\n"
  4709. );
  4710. }
  4711. /*---------------------------------------------*/
  4712. static
  4713. void showFileNames ( void )
  4714. {
  4715. if (noisy)
  4716. fprintf (
  4717. stderr,
  4718. "\tInput file = %s, output file = %s\n",
  4719. inName, outName
  4720. );
  4721. }
  4722. /*---------------------------------------------*/
  4723. static
  4724. void cleanUpAndFail ( Int32 ec )
  4725. {
  4726. IntNative retVal;
  4727. struct MY_STAT statBuf;
  4728. if ( srcMode == SM_F2F
  4729. && opMode != OM_TEST
  4730. && deleteOutputOnInterrupt ) {
  4731. /* Check whether input file still exists. Delete output file
  4732. only if input exists to avoid loss of data. Joerg Prante, 5
  4733. January 2002. (JRS 06-Jan-2002: other changes in 1.0.2 mean
  4734. this is less likely to happen. But to be ultra-paranoid, we
  4735. do the check anyway.) */
  4736. retVal = MY_STAT ( inName, &statBuf );
  4737. if (retVal == 0) {
  4738. if (noisy)
  4739. fprintf ( stderr,
  4740. "%s: Deleting output file %s, if it exists.\n",
  4741. progName, outName );
  4742. if (outputHandleJustInCase != NULL)
  4743. fclose ( outputHandleJustInCase );
  4744. retVal = remove ( outName );
  4745. if (retVal != 0)
  4746. fprintf ( stderr,
  4747. "%s: WARNING: deletion of output file "
  4748. "(apparently) failed.\n",
  4749. progName );
  4750. } else {
  4751. fprintf ( stderr,
  4752. "%s: WARNING: deletion of output file suppressed\n",
  4753. progName );
  4754. fprintf ( stderr,
  4755. "%s: since input file no longer exists. Output file\n",
  4756. progName );
  4757. fprintf ( stderr,
  4758. "%s: `%s' may be incomplete.\n",
  4759. progName, outName );
  4760. fprintf ( stderr,
  4761. "%s: I suggest doing an integrity test (bzip2 -tv)"
  4762. " of it.\n",
  4763. progName );
  4764. }
  4765. }
  4766. if (noisy && numFileNames > 0 && numFilesProcessed < numFileNames) {
  4767. fprintf ( stderr,
  4768. "%s: WARNING: some files have not been processed:\n"
  4769. "%s: %d specified on command line, %d not processed yet.\n\n",
  4770. progName, progName,
  4771. numFileNames, numFileNames - numFilesProcessed );
  4772. }
  4773. setExit(ec);
  4774. exit(exitValue);
  4775. }
  4776. /*---------------------------------------------*/
  4777. static
  4778. void panic ( Char* s )
  4779. {
  4780. fprintf ( stderr,
  4781. "\n%s: PANIC -- internal consistency error:\n"
  4782. "\t%s\n"
  4783. "\tThis is a BUG. Please report it to me at:\n"
  4784. "\tjseward@acm.org\n",
  4785. progName, s );
  4786. showFileNames();
  4787. cleanUpAndFail( 3 );
  4788. }
  4789. /*---------------------------------------------*/
  4790. static
  4791. void crcError ( void )
  4792. {
  4793. fprintf ( stderr,
  4794. "\n%s: Data integrity error when decompressing.\n",
  4795. progName );
  4796. showFileNames();
  4797. cadvise();
  4798. cleanUpAndFail( 2 );
  4799. }
  4800. /*---------------------------------------------*/
  4801. static
  4802. void compressedStreamEOF ( void )
  4803. {
  4804. if (noisy) {
  4805. fprintf ( stderr,
  4806. "\n%s: Compressed file ends unexpectedly;\n\t"
  4807. "perhaps it is corrupted? *Possible* reason follows.\n",
  4808. progName );
  4809. perror ( progName );
  4810. showFileNames();
  4811. cadvise();
  4812. }
  4813. cleanUpAndFail( 2 );
  4814. }
  4815. /*---------------------------------------------*/
  4816. static
  4817. void ioError ( void )
  4818. {
  4819. fprintf ( stderr,
  4820. "\n%s: I/O or other error, bailing out. "
  4821. "Possible reason follows.\n",
  4822. progName );
  4823. perror ( progName );
  4824. showFileNames();
  4825. cleanUpAndFail( 1 );
  4826. }
  4827. /*---------------------------------------------*/
  4828. static
  4829. void mySignalCatcher ( IntNative n )
  4830. {
  4831. fprintf ( stderr,
  4832. "\n%s: Control-C or similar caught, quitting.\n",
  4833. progName );
  4834. cleanUpAndFail(1);
  4835. }
  4836. /*---------------------------------------------*/
  4837. static
  4838. void mySIGSEGVorSIGBUScatcher ( IntNative n )
  4839. {
  4840. if (opMode == OM_Z)
  4841. fprintf (
  4842. stderr,
  4843. "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n"
  4844. "\n"
  4845. " Possible causes are (most likely first):\n"
  4846. " (1) This computer has unreliable memory or cache hardware\n"
  4847. " (a surprisingly common problem; try a different machine.)\n"
  4848. " (2) A bug in the compiler used to create this executable\n"
  4849. " (unlikely, if you didn't compile bzip2 yourself.)\n"
  4850. " (3) A real bug in bzip2 -- I hope this should never be the case.\n"
  4851. " The user's manual, Section 4.3, has more info on (1) and (2).\n"
  4852. " \n"
  4853. " If you suspect this is a bug in bzip2, or are unsure about (1)\n"
  4854. " or (2), feel free to report it to me at: jseward@acm.org.\n"
  4855. " Section 4.3 of the user's manual describes the info a useful\n"
  4856. " bug report should have. If the manual is available on your\n"
  4857. " system, please try and read it before mailing me. If you don't\n"
  4858. " have the manual or can't be bothered to read it, mail me anyway.\n"
  4859. "\n",
  4860. progName );
  4861. else
  4862. fprintf (
  4863. stderr,
  4864. "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n"
  4865. "\n"
  4866. " Possible causes are (most likely first):\n"
  4867. " (1) The compressed data is corrupted, and bzip2's usual checks\n"
  4868. " failed to detect this. Try bzip2 -tvv my_file.bz2.\n"
  4869. " (2) This computer has unreliable memory or cache hardware\n"
  4870. " (a surprisingly common problem; try a different machine.)\n"
  4871. " (3) A bug in the compiler used to create this executable\n"
  4872. " (unlikely, if you didn't compile bzip2 yourself.)\n"
  4873. " (4) A real bug in bzip2 -- I hope this should never be the case.\n"
  4874. " The user's manual, Section 4.3, has more info on (2) and (3).\n"
  4875. " \n"
  4876. " If you suspect this is a bug in bzip2, or are unsure about (2)\n"
  4877. " or (3), feel free to report it to me at: jseward@acm.org.\n"
  4878. " Section 4.3 of the user's manual describes the info a useful\n"
  4879. " bug report should have. If the manual is available on your\n"
  4880. " system, please try and read it before mailing me. If you don't\n"
  4881. " have the manual or can't be bothered to read it, mail me anyway.\n"
  4882. "\n",
  4883. progName );
  4884. showFileNames();
  4885. if (opMode == OM_Z)
  4886. cleanUpAndFail( 3 ); else
  4887. { cadvise(); cleanUpAndFail( 2 ); }
  4888. }
  4889. /*---------------------------------------------*/
  4890. static
  4891. void outOfMemory ( void )
  4892. {
  4893. fprintf ( stderr,
  4894. "\n%s: couldn't allocate enough memory\n",
  4895. progName );
  4896. showFileNames();
  4897. cleanUpAndFail(1);
  4898. }
  4899. /*---------------------------------------------*/
  4900. static
  4901. void configError ( void )
  4902. {
  4903. fprintf ( stderr,
  4904. "bzip2: I'm not configured correctly for this platform!\n"
  4905. "\tI require Int32, Int16 and Char to have sizes\n"
  4906. "\tof 4, 2 and 1 bytes to run properly, and they don't.\n"
  4907. "\tProbably you can fix this by defining them correctly,\n"
  4908. "\tand recompiling. Bye!\n" );
  4909. setExit(3);
  4910. exit(exitValue);
  4911. }
  4912. /*---------------------------------------------------*/
  4913. /*--- The main driver machinery ---*/
  4914. /*---------------------------------------------------*/
  4915. /* All rather crufty. The main problem is that input files
  4916. are stat()d multiple times before use. This should be
  4917. cleaned up.
  4918. */
  4919. /*---------------------------------------------*/
  4920. static
  4921. void pad ( Char *s )
  4922. {
  4923. Int32 i;
  4924. if ( (Int32)strlen(s) >= longestFileName ) return;
  4925. for (i = 1; i <= longestFileName - (Int32)strlen(s); i++)
  4926. fprintf ( stderr, " " );
  4927. }
  4928. /*---------------------------------------------*/
  4929. static
  4930. void copyFileName ( Char* to, Char* from )
  4931. {
  4932. if ( strlen(from) > FILE_NAME_LEN-10 ) {
  4933. fprintf (
  4934. stderr,
  4935. "bzip2: file name\n`%s'\n"
  4936. "is suspiciously (more than %d chars) long.\n"
  4937. "Try using a reasonable file name instead. Sorry! :-)\n",
  4938. from, FILE_NAME_LEN-10
  4939. );
  4940. setExit(1);
  4941. exit(exitValue);
  4942. }
  4943. strncpy(to,from,FILE_NAME_LEN-10);
  4944. to[FILE_NAME_LEN-10]='\0';
  4945. }
  4946. /*---------------------------------------------*/
  4947. static
  4948. Bool fileExists ( Char* name )
  4949. {
  4950. FILE *tmp = fopen ( name, "rb" );
  4951. Bool exists = (tmp != NULL);
  4952. if (tmp != NULL) fclose ( tmp );
  4953. return exists;
  4954. }
  4955. /*---------------------------------------------*/
  4956. /* Open an output file safely with O_EXCL and good permissions.
  4957. This avoids a race condition in versions < 1.0.2, in which
  4958. the file was first opened and then had its interim permissions
  4959. set safely. We instead use open() to create the file with
  4960. the interim permissions required. (--- --- rw-).
  4961. For non-Unix platforms, if we are not worrying about
  4962. security issues, simple this simply behaves like fopen.
  4963. */
  4964. FILE* fopen_output_safely ( Char* name, const char* mode )
  4965. {
  4966. # if BZ_UNIX
  4967. FILE* fp;
  4968. IntNative fh;
  4969. fh = open(name, O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR);
  4970. if (fh == -1) return NULL;
  4971. fp = fdopen(fh, mode);
  4972. if (fp == NULL) close(fh);
  4973. return fp;
  4974. # else
  4975. return fopen(name, mode);
  4976. # endif
  4977. }
  4978. /*---------------------------------------------*/
  4979. /*--
  4980. if in doubt, return True
  4981. --*/
  4982. static
  4983. Bool notAStandardFile ( Char* name )
  4984. {
  4985. IntNative i;
  4986. struct MY_STAT statBuf;
  4987. i = MY_LSTAT ( name, &statBuf );
  4988. if (i != 0) return True;
  4989. if (MY_S_ISREG(statBuf.st_mode)) return False;
  4990. return True;
  4991. }
  4992. /*---------------------------------------------*/
  4993. /*--
  4994. rac 11/21/98 see if file has hard links to it
  4995. --*/
  4996. static
  4997. Int32 countHardLinks ( Char* name )
  4998. {
  4999. IntNative i;
  5000. struct MY_STAT statBuf;
  5001. i = MY_LSTAT ( name, &statBuf );
  5002. if (i != 0) return 0;
  5003. return (statBuf.st_nlink - 1);
  5004. }
  5005. /*---------------------------------------------*/
  5006. /* Copy modification date, access date, permissions and owner from the
  5007. source to destination file. We have to copy this meta-info off
  5008. into fileMetaInfo before starting to compress / decompress it,
  5009. because doing it afterwards means we get the wrong access time.
  5010. To complicate matters, in compress() and decompress() below, the
  5011. sequence of tests preceding the call to saveInputFileMetaInfo()
  5012. involves calling fileExists(), which in turn establishes its result
  5013. by attempting to fopen() the file, and if successful, immediately
  5014. fclose()ing it again. So we have to assume that the fopen() call
  5015. does not cause the access time field to be updated.
  5016. Reading of the man page for stat() (man 2 stat) on RedHat 7.2 seems
  5017. to imply that merely doing open() will not affect the access time.
  5018. Therefore we merely need to hope that the C library only does
  5019. open() as a result of fopen(), and not any kind of read()-ahead
  5020. cleverness.
  5021. It sounds pretty fragile to me. Whether this carries across
  5022. robustly to arbitrary Unix-like platforms (or even works robustly
  5023. on this one, RedHat 7.2) is unknown to me. Nevertheless ...
  5024. */
  5025. #if BZ_UNIX
  5026. static
  5027. struct MY_STAT fileMetaInfo;
  5028. #endif
  5029. static
  5030. void saveInputFileMetaInfo ( Char *srcName )
  5031. {
  5032. # if BZ_UNIX
  5033. IntNative retVal;
  5034. /* Note use of stat here, not lstat. */
  5035. retVal = MY_STAT( srcName, &fileMetaInfo );
  5036. ERROR_IF_NOT_ZERO ( retVal );
  5037. # endif
  5038. }
  5039. static
  5040. void applySavedMetaInfoToOutputFile ( Char *dstName )
  5041. {
  5042. # if BZ_UNIX
  5043. IntNative retVal;
  5044. struct utimbuf uTimBuf;
  5045. uTimBuf.actime = fileMetaInfo.st_atime;
  5046. uTimBuf.modtime = fileMetaInfo.st_mtime;
  5047. retVal = chmod ( dstName, fileMetaInfo.st_mode );
  5048. ERROR_IF_NOT_ZERO ( retVal );
  5049. retVal = utime ( dstName, &uTimBuf );
  5050. ERROR_IF_NOT_ZERO ( retVal );
  5051. retVal = chown ( dstName, fileMetaInfo.st_uid, fileMetaInfo.st_gid );
  5052. /* chown() will in many cases return with EPERM, which can
  5053. be safely ignored.
  5054. */
  5055. # endif
  5056. }
  5057. /*---------------------------------------------*/
  5058. static
  5059. Bool containsDubiousChars ( Char* name )
  5060. {
  5061. # if BZ_UNIX
  5062. /* On unix, files can contain any characters and the file expansion
  5063. * is performed by the shell.
  5064. */
  5065. return False;
  5066. # else /* ! BZ_UNIX */
  5067. /* On non-unix (Win* platforms), wildcard characters are not allowed in
  5068. * filenames.
  5069. */
  5070. for (; *name != '\0'; name++)
  5071. if (*name == '?' || *name == '*') return True;
  5072. return False;
  5073. # endif /* BZ_UNIX */
  5074. }
  5075. /*---------------------------------------------*/
  5076. #define BZ_N_SUFFIX_PAIRS 4
  5077. Char* zSuffix[BZ_N_SUFFIX_PAIRS]
  5078. = { ".bz2", ".bz", ".tbz2", ".tbz" };
  5079. Char* unzSuffix[BZ_N_SUFFIX_PAIRS]
  5080. = { "", "", ".tar", ".tar" };
  5081. static
  5082. Bool hasSuffix ( Char* s, Char* suffix )
  5083. {
  5084. Int32 ns = strlen(s);
  5085. Int32 nx = strlen(suffix);
  5086. if (ns < nx) return False;
  5087. if (strcmp(s + ns - nx, suffix) == 0) return True;
  5088. return False;
  5089. }
  5090. static
  5091. Bool mapSuffix ( Char* name,
  5092. Char* oldSuffix, Char* newSuffix )
  5093. {
  5094. if (!hasSuffix(name,oldSuffix)) return False;
  5095. name[strlen(name)-strlen(oldSuffix)] = 0;
  5096. strcat ( name, newSuffix );
  5097. return True;
  5098. }
  5099. /*---------------------------------------------*/
  5100. static
  5101. void compress ( Char *name )
  5102. {
  5103. FILE *inStr;
  5104. FILE *outStr;
  5105. Int32 n, i;
  5106. struct MY_STAT statBuf;
  5107. deleteOutputOnInterrupt = False;
  5108. if (name == NULL && srcMode != SM_I2O)
  5109. panic ( "compress: bad modes\n" );
  5110. switch (srcMode) {
  5111. case SM_I2O:
  5112. copyFileName ( inName, "(stdin)" );
  5113. copyFileName ( outName, "(stdout)" );
  5114. break;
  5115. case SM_F2F:
  5116. copyFileName ( inName, name );
  5117. copyFileName ( outName, name );
  5118. strcat ( outName, ".bz2" );
  5119. break;
  5120. case SM_F2O:
  5121. copyFileName ( inName, name );
  5122. copyFileName ( outName, "(stdout)" );
  5123. break;
  5124. }
  5125. if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
  5126. if (noisy)
  5127. fprintf ( stderr, "%s: There are no files matching `%s'.\n",
  5128. progName, inName );
  5129. setExit(1);
  5130. return;
  5131. }
  5132. if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
  5133. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  5134. progName, inName, strerror(errno) );
  5135. setExit(1);
  5136. return;
  5137. }
  5138. for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) {
  5139. if (hasSuffix(inName, zSuffix[i])) {
  5140. if (noisy)
  5141. fprintf ( stderr,
  5142. "%s: Input file %s already has %s suffix.\n",
  5143. progName, inName, zSuffix[i] );
  5144. setExit(1);
  5145. return;
  5146. }
  5147. }
  5148. if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
  5149. MY_STAT(inName, &statBuf);
  5150. if ( MY_S_ISDIR(statBuf.st_mode) ) {
  5151. fprintf( stderr,
  5152. "%s: Input file %s is a directory.\n",
  5153. progName,inName);
  5154. setExit(1);
  5155. return;
  5156. }
  5157. }
  5158. if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
  5159. if (noisy)
  5160. fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
  5161. progName, inName );
  5162. setExit(1);
  5163. return;
  5164. }
  5165. if ( srcMode == SM_F2F && fileExists ( outName ) ) {
  5166. if (forceOverwrite) {
  5167. remove(outName);
  5168. } else {
  5169. fprintf ( stderr, "%s: Output file %s already exists.\n",
  5170. progName, outName );
  5171. setExit(1);
  5172. return;
  5173. }
  5174. }
  5175. if ( srcMode == SM_F2F && !forceOverwrite &&
  5176. (n=countHardLinks ( inName )) > 0) {
  5177. fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
  5178. progName, inName, n, n > 1 ? "s" : "" );
  5179. setExit(1);
  5180. return;
  5181. }
  5182. if ( srcMode == SM_F2F ) {
  5183. /* Save the file's meta-info before we open it. Doing it later
  5184. means we mess up the access times. */
  5185. saveInputFileMetaInfo ( inName );
  5186. }
  5187. switch ( srcMode ) {
  5188. case SM_I2O:
  5189. inStr = stdin;
  5190. outStr = stdout;
  5191. if ( isatty ( fileno ( stdout ) ) ) {
  5192. fprintf ( stderr,
  5193. "%s: I won't write compressed data to a terminal.\n",
  5194. progName );
  5195. fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
  5196. progName, progName );
  5197. setExit(1);
  5198. return;
  5199. };
  5200. break;
  5201. case SM_F2O:
  5202. inStr = fopen ( inName, "rb" );
  5203. outStr = stdout;
  5204. if ( isatty ( fileno ( stdout ) ) ) {
  5205. fprintf ( stderr,
  5206. "%s: I won't write compressed data to a terminal.\n",
  5207. progName );
  5208. fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
  5209. progName, progName );
  5210. if ( inStr != NULL ) fclose ( inStr );
  5211. setExit(1);
  5212. return;
  5213. };
  5214. if ( inStr == NULL ) {
  5215. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  5216. progName, inName, strerror(errno) );
  5217. setExit(1);
  5218. return;
  5219. };
  5220. break;
  5221. case SM_F2F:
  5222. inStr = fopen ( inName, "rb" );
  5223. outStr = fopen_output_safely ( outName, "wb" );
  5224. if ( outStr == NULL) {
  5225. fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
  5226. progName, outName, strerror(errno) );
  5227. if ( inStr != NULL ) fclose ( inStr );
  5228. setExit(1);
  5229. return;
  5230. }
  5231. if ( inStr == NULL ) {
  5232. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  5233. progName, inName, strerror(errno) );
  5234. if ( outStr != NULL ) fclose ( outStr );
  5235. setExit(1);
  5236. return;
  5237. };
  5238. break;
  5239. default:
  5240. panic ( "compress: bad srcMode" );
  5241. break;
  5242. }
  5243. if (verbosity >= 1) {
  5244. fprintf ( stderr, " %s: ", inName );
  5245. pad ( inName );
  5246. fflush ( stderr );
  5247. }
  5248. /*--- Now the input and output handles are sane. Do the Biz. ---*/
  5249. outputHandleJustInCase = outStr;
  5250. deleteOutputOnInterrupt = True;
  5251. compressStream ( inStr, outStr );
  5252. outputHandleJustInCase = NULL;
  5253. /*--- If there was an I/O error, we won't get here. ---*/
  5254. if ( srcMode == SM_F2F ) {
  5255. applySavedMetaInfoToOutputFile ( outName );
  5256. deleteOutputOnInterrupt = False;
  5257. if ( !keepInputFiles ) {
  5258. IntNative retVal = remove ( inName );
  5259. ERROR_IF_NOT_ZERO ( retVal );
  5260. }
  5261. }
  5262. deleteOutputOnInterrupt = False;
  5263. }
  5264. /*---------------------------------------------*/
  5265. static
  5266. void uncompress ( Char *name )
  5267. {
  5268. FILE *inStr;
  5269. FILE *outStr;
  5270. Int32 n, i;
  5271. Bool magicNumberOK;
  5272. Bool cantGuess;
  5273. struct MY_STAT statBuf;
  5274. deleteOutputOnInterrupt = False;
  5275. if (name == NULL && srcMode != SM_I2O)
  5276. panic ( "uncompress: bad modes\n" );
  5277. cantGuess = False;
  5278. switch (srcMode) {
  5279. case SM_I2O:
  5280. copyFileName ( inName, "(stdin)" );
  5281. copyFileName ( outName, "(stdout)" );
  5282. break;
  5283. case SM_F2F:
  5284. copyFileName ( inName, name );
  5285. copyFileName ( outName, name );
  5286. for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++)
  5287. if (mapSuffix(outName,zSuffix[i],unzSuffix[i]))
  5288. goto zzz;
  5289. cantGuess = True;
  5290. strcat ( outName, ".out" );
  5291. break;
  5292. case SM_F2O:
  5293. copyFileName ( inName, name );
  5294. copyFileName ( outName, "(stdout)" );
  5295. break;
  5296. }
  5297. zzz:
  5298. if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
  5299. if (noisy)
  5300. fprintf ( stderr, "%s: There are no files matching `%s'.\n",
  5301. progName, inName );
  5302. setExit(1);
  5303. return;
  5304. }
  5305. if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
  5306. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  5307. progName, inName, strerror(errno) );
  5308. setExit(1);
  5309. return;
  5310. }
  5311. if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
  5312. MY_STAT(inName, &statBuf);
  5313. if ( MY_S_ISDIR(statBuf.st_mode) ) {
  5314. fprintf( stderr,
  5315. "%s: Input file %s is a directory.\n",
  5316. progName,inName);
  5317. setExit(1);
  5318. return;
  5319. }
  5320. }
  5321. if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
  5322. if (noisy)
  5323. fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
  5324. progName, inName );
  5325. setExit(1);
  5326. return;
  5327. }
  5328. if ( /* srcMode == SM_F2F implied && */ cantGuess ) {
  5329. if (noisy)
  5330. fprintf ( stderr,
  5331. "%s: Can't guess original name for %s -- using %s\n",
  5332. progName, inName, outName );
  5333. /* just a warning, no return */
  5334. }
  5335. if ( srcMode == SM_F2F && fileExists ( outName ) ) {
  5336. if (forceOverwrite) {
  5337. remove(outName);
  5338. } else {
  5339. fprintf ( stderr, "%s: Output file %s already exists.\n",
  5340. progName, outName );
  5341. setExit(1);
  5342. return;
  5343. }
  5344. }
  5345. if ( srcMode == SM_F2F && !forceOverwrite &&
  5346. (n=countHardLinks ( inName ) ) > 0) {
  5347. fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
  5348. progName, inName, n, n > 1 ? "s" : "" );
  5349. setExit(1);
  5350. return;
  5351. }
  5352. if ( srcMode == SM_F2F ) {
  5353. /* Save the file's meta-info before we open it. Doing it later
  5354. means we mess up the access times. */
  5355. saveInputFileMetaInfo ( inName );
  5356. }
  5357. switch ( srcMode ) {
  5358. case SM_I2O:
  5359. inStr = stdin;
  5360. outStr = stdout;
  5361. if ( isatty ( fileno ( stdin ) ) ) {
  5362. fprintf ( stderr,
  5363. "%s: I won't read compressed data from a terminal.\n",
  5364. progName );
  5365. fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
  5366. progName, progName );
  5367. setExit(1);
  5368. return;
  5369. };
  5370. break;
  5371. case SM_F2O:
  5372. inStr = fopen ( inName, "rb" );
  5373. outStr = stdout;
  5374. if ( inStr == NULL ) {
  5375. fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
  5376. progName, inName, strerror(errno) );
  5377. if ( inStr != NULL ) fclose ( inStr );
  5378. setExit(1);
  5379. return;
  5380. };
  5381. break;
  5382. case SM_F2F:
  5383. inStr = fopen ( inName, "rb" );
  5384. outStr = fopen_output_safely ( outName, "wb" );
  5385. if ( outStr == NULL) {
  5386. fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
  5387. progName, outName, strerror(errno) );
  5388. if ( inStr != NULL ) fclose ( inStr );
  5389. setExit(1);
  5390. return;
  5391. }
  5392. if ( inStr == NULL ) {
  5393. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  5394. progName, inName, strerror(errno) );
  5395. if ( outStr != NULL ) fclose ( outStr );
  5396. setExit(1);
  5397. return;
  5398. };
  5399. break;
  5400. default:
  5401. panic ( "uncompress: bad srcMode" );
  5402. break;
  5403. }
  5404. if (verbosity >= 1) {
  5405. fprintf ( stderr, " %s: ", inName );
  5406. pad ( inName );
  5407. fflush ( stderr );
  5408. }
  5409. /*--- Now the input and output handles are sane. Do the Biz. ---*/
  5410. outputHandleJustInCase = outStr;
  5411. deleteOutputOnInterrupt = True;
  5412. magicNumberOK = uncompressStream ( inStr, outStr );
  5413. outputHandleJustInCase = NULL;
  5414. /*--- If there was an I/O error, we won't get here. ---*/
  5415. if ( magicNumberOK ) {
  5416. if ( srcMode == SM_F2F ) {
  5417. applySavedMetaInfoToOutputFile ( outName );
  5418. deleteOutputOnInterrupt = False;
  5419. if ( !keepInputFiles ) {
  5420. IntNative retVal = remove ( inName );
  5421. ERROR_IF_NOT_ZERO ( retVal );
  5422. }
  5423. }
  5424. } else {
  5425. unzFailsExist = True;
  5426. deleteOutputOnInterrupt = False;
  5427. if ( srcMode == SM_F2F ) {
  5428. IntNative retVal = remove ( outName );
  5429. ERROR_IF_NOT_ZERO ( retVal );
  5430. }
  5431. }
  5432. deleteOutputOnInterrupt = False;
  5433. if ( magicNumberOK ) {
  5434. if (verbosity >= 1)
  5435. fprintf ( stderr, "done\n" );
  5436. } else {
  5437. setExit(2);
  5438. if (verbosity >= 1)
  5439. fprintf ( stderr, "not a bzip2 file.\n" ); else
  5440. fprintf ( stderr,
  5441. "%s: %s is not a bzip2 file.\n",
  5442. progName, inName );
  5443. }
  5444. }
  5445. /*---------------------------------------------*/
  5446. static
  5447. void testf ( Char *name )
  5448. {
  5449. FILE *inStr;
  5450. Bool allOK;
  5451. struct MY_STAT statBuf;
  5452. deleteOutputOnInterrupt = False;
  5453. if (name == NULL && srcMode != SM_I2O)
  5454. panic ( "testf: bad modes\n" );
  5455. copyFileName ( outName, "(none)" );
  5456. switch (srcMode) {
  5457. case SM_I2O: copyFileName ( inName, "(stdin)" ); break;
  5458. case SM_F2F: copyFileName ( inName, name ); break;
  5459. case SM_F2O: copyFileName ( inName, name ); break;
  5460. }
  5461. if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
  5462. if (noisy)
  5463. fprintf ( stderr, "%s: There are no files matching `%s'.\n",
  5464. progName, inName );
  5465. setExit(1);
  5466. return;
  5467. }
  5468. if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
  5469. fprintf ( stderr, "%s: Can't open input %s: %s.\n",
  5470. progName, inName, strerror(errno) );
  5471. setExit(1);
  5472. return;
  5473. }
  5474. if ( srcMode != SM_I2O ) {
  5475. MY_STAT(inName, &statBuf);
  5476. if ( MY_S_ISDIR(statBuf.st_mode) ) {
  5477. fprintf( stderr,
  5478. "%s: Input file %s is a directory.\n",
  5479. progName,inName);
  5480. setExit(1);
  5481. return;
  5482. }
  5483. }
  5484. switch ( srcMode ) {
  5485. case SM_I2O:
  5486. if ( isatty ( fileno ( stdin ) ) ) {
  5487. fprintf ( stderr,
  5488. "%s: I won't read compressed data from a terminal.\n",
  5489. progName );
  5490. fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
  5491. progName, progName );
  5492. setExit(1);
  5493. return;
  5494. };
  5495. inStr = stdin;
  5496. break;
  5497. case SM_F2O: case SM_F2F:
  5498. inStr = fopen ( inName, "rb" );
  5499. if ( inStr == NULL ) {
  5500. fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
  5501. progName, inName, strerror(errno) );
  5502. setExit(1);
  5503. return;
  5504. };
  5505. break;
  5506. default:
  5507. panic ( "testf: bad srcMode" );
  5508. break;
  5509. }
  5510. if (verbosity >= 1) {
  5511. fprintf ( stderr, " %s: ", inName );
  5512. pad ( inName );
  5513. fflush ( stderr );
  5514. }
  5515. /*--- Now the input handle is sane. Do the Biz. ---*/
  5516. outputHandleJustInCase = NULL;
  5517. allOK = testStream ( inStr );
  5518. if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" );
  5519. if (!allOK) testFailsExist = True;
  5520. }
  5521. /*---------------------------------------------*/
  5522. static
  5523. void license ( void )
  5524. {
  5525. fprintf ( stderr,
  5526. "bzip2, a block-sorting file compressor. "
  5527. "Version %s.\n"
  5528. " \n"
  5529. " Copyright (C) 1996-2002 by Julian Seward.\n"
  5530. " \n"
  5531. " This program is free software; you can redistribute it and/or modify\n"
  5532. " it under the terms set out in the LICENSE file, which is included\n"
  5533. " in the bzip2-1.0 source distribution.\n"
  5534. " \n"
  5535. " This program is distributed in the hope that it will be useful,\n"
  5536. " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  5537. " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  5538. " LICENSE file for more details.\n"
  5539. " \n",
  5540. BZ2_bzlibVersion()
  5541. );
  5542. }
  5543. /*---------------------------------------------*/
  5544. static
  5545. void usage ( Char *fullProgName )
  5546. {
  5547. fprintf (
  5548. stderr,
  5549. "bzip2, a block-sorting file compressor. "
  5550. "Version %s.\n"
  5551. "\n usage: %s [flags and input files in any order]\n"
  5552. "\n"
  5553. " -h --help print this message\n"
  5554. " -d --decompress force decompression\n"
  5555. " -z --compress force compression\n"
  5556. " -k --keep keep (don't delete) input files\n"
  5557. " -f --force overwrite existing output files\n"
  5558. " -t --test test compressed file integrity\n"
  5559. " -c --stdout output to standard out\n"
  5560. " -q --quiet suppress noncritical error messages\n"
  5561. " -v --verbose be verbose (a 2nd -v gives more)\n"
  5562. " -L --license display software version & license\n"
  5563. " -V --version display software version & license\n"
  5564. " -s --small use less memory (at most 2500k)\n"
  5565. " -1 .. -9 set block size to 100k .. 900k\n"
  5566. " --fast alias for -1\n"
  5567. " --best alias for -9\n"
  5568. "\n"
  5569. " If invoked as `bzip2', default action is to compress.\n"
  5570. " as `bunzip2', default action is to decompress.\n"
  5571. " as `bzcat', default action is to decompress to stdout.\n"
  5572. "\n"
  5573. " If no file names are given, bzip2 compresses or decompresses\n"
  5574. " from standard input to standard output. You can combine\n"
  5575. " short flags, so `-v -4' means the same as -v4 or -4v, &c.\n"
  5576. # if BZ_UNIX
  5577. "\n"
  5578. # endif
  5579. ,
  5580. BZ2_bzlibVersion(),
  5581. fullProgName
  5582. );
  5583. }
  5584. /*---------------------------------------------*/
  5585. static
  5586. void redundant ( Char* flag )
  5587. {
  5588. fprintf (
  5589. stderr,
  5590. "%s: %s is redundant in versions 0.9.5 and above\n",
  5591. progName, flag );
  5592. }
  5593. /*---------------------------------------------*/
  5594. /*--
  5595. All the garbage from here to main() is purely to
  5596. implement a linked list of command-line arguments,
  5597. into which main() copies argv[1 .. argc-1].
  5598. The purpose of this exercise is to facilitate
  5599. the expansion of wildcard characters * and ? in
  5600. filenames for OSs which don't know how to do it
  5601. themselves, like MSDOS, Windows 95 and NT.
  5602. The actual Dirty Work is done by the platform-
  5603. specific macro APPEND_FILESPEC.
  5604. --*/
  5605. typedef
  5606. struct zzzz {
  5607. Char *name;
  5608. struct zzzz *link;
  5609. }
  5610. Cell;
  5611. /*---------------------------------------------*/
  5612. static
  5613. void *myMalloc ( Int32 n )
  5614. {
  5615. void* p;
  5616. p = malloc ( (size_t)n );
  5617. if (p == NULL) outOfMemory ();
  5618. return p;
  5619. }
  5620. /*---------------------------------------------*/
  5621. static
  5622. Cell *mkCell ( void )
  5623. {
  5624. Cell *c;
  5625. c = (Cell*) myMalloc ( sizeof ( Cell ) );
  5626. c->name = NULL;
  5627. c->link = NULL;
  5628. return c;
  5629. }
  5630. /*---------------------------------------------*/
  5631. static
  5632. Cell *snocString ( Cell *root, Char *name )
  5633. {
  5634. if (root == NULL) {
  5635. Cell *tmp = mkCell();
  5636. tmp->name = (Char*) myMalloc ( 5 + strlen(name) );
  5637. strcpy ( tmp->name, name );
  5638. return tmp;
  5639. } else {
  5640. Cell *tmp = root;
  5641. while (tmp->link != NULL) tmp = tmp->link;
  5642. tmp->link = snocString ( tmp->link, name );
  5643. return root;
  5644. }
  5645. }
  5646. /*---------------------------------------------*/
  5647. static
  5648. void addFlagsFromEnvVar ( Cell** argList, Char* varName )
  5649. {
  5650. Int32 i, j, k;
  5651. Char *envbase, *p;
  5652. envbase = getenv(varName);
  5653. if (envbase != NULL) {
  5654. p = envbase;
  5655. i = 0;
  5656. while (True) {
  5657. if (p[i] == 0) break;
  5658. p += i;
  5659. i = 0;
  5660. while (isspace((Int32)(p[0]))) p++;
  5661. while (p[i] != 0 && !isspace((Int32)(p[i]))) i++;
  5662. if (i > 0) {
  5663. k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10;
  5664. for (j = 0; j < k; j++) tmpName[j] = p[j];
  5665. tmpName[k] = 0;
  5666. APPEND_FLAG(*argList, tmpName);
  5667. }
  5668. }
  5669. }
  5670. }
  5671. /*---------------------------------------------*/
  5672. #define ISFLAG(s) (strcmp(aa->name, (s))==0)
  5673. IntNative main ( IntNative argc, Char *argv[] )
  5674. {
  5675. Int32 i, j;
  5676. Char *tmp;
  5677. Cell *argList;
  5678. Cell *aa;
  5679. Bool decode;
  5680. /*-- Be really really really paranoid :-) --*/
  5681. if (sizeof(Int32) != 4 || sizeof(UInt32) != 4 ||
  5682. sizeof(Int16) != 2 || sizeof(UInt16) != 2 ||
  5683. sizeof(Char) != 1 || sizeof(UChar) != 1)
  5684. configError();
  5685. /*-- Initialise --*/
  5686. outputHandleJustInCase = NULL;
  5687. smallMode = False;
  5688. keepInputFiles = False;
  5689. forceOverwrite = False;
  5690. noisy = True;
  5691. verbosity = 0;
  5692. blockSize100k = 9;
  5693. testFailsExist = False;
  5694. unzFailsExist = False;
  5695. numFileNames = 0;
  5696. numFilesProcessed = 0;
  5697. workFactor = 30;
  5698. deleteOutputOnInterrupt = False;
  5699. exitValue = 0;
  5700. i = j = 0; /* avoid bogus warning from egcs-1.1.X */
  5701. /*-- Set up signal handlers for mem access errors --*/
  5702. signal (SIGSEGV, mySIGSEGVorSIGBUScatcher);
  5703. # if BZ_UNIX
  5704. # ifndef __DJGPP__
  5705. signal (SIGBUS, mySIGSEGVorSIGBUScatcher);
  5706. # endif
  5707. # endif
  5708. copyFileName ( inName, "(none)" );
  5709. copyFileName ( outName, "(none)" );
  5710. copyFileName ( progNameReally, argv[0] );
  5711. progName = &progNameReally[0];
  5712. for (tmp = &progNameReally[0]; *tmp != '\0'; tmp++)
  5713. if (*tmp == PATH_SEP) progName = tmp + 1;
  5714. /*-- Copy flags from env var BZIP2, and
  5715. expand filename wildcards in arg list.
  5716. --*/
  5717. argList = NULL;
  5718. addFlagsFromEnvVar ( &argList, "BZIP2" );
  5719. addFlagsFromEnvVar ( &argList, "BZIP" );
  5720. for (i = 1; i <= argc-1; i++)
  5721. APPEND_FILESPEC(argList, argv[i]);
  5722. /*-- Find the length of the longest filename --*/
  5723. longestFileName = 7;
  5724. numFileNames = 0;
  5725. decode = True;
  5726. for (aa = argList; aa != NULL; aa = aa->link) {
  5727. if (ISFLAG("--")) { decode = False; continue; }
  5728. if (aa->name[0] == '-' && decode) continue;
  5729. numFileNames++;
  5730. if (longestFileName < (Int32)strlen(aa->name) )
  5731. longestFileName = (Int32)strlen(aa->name);
  5732. }
  5733. /*-- Determine source modes; flag handling may change this too. --*/
  5734. if (numFileNames == 0)
  5735. srcMode = SM_I2O; else srcMode = SM_F2F;
  5736. /*-- Determine what to do (compress/uncompress/test/cat). --*/
  5737. /*-- Note that subsequent flag handling may change this. --*/
  5738. opMode = OM_Z;
  5739. if ( (strstr ( progName, "unzip" ) != 0) ||
  5740. (strstr ( progName, "UNZIP" ) != 0) )
  5741. opMode = OM_UNZ;
  5742. if ( (strstr ( progName, "z2cat" ) != 0) ||
  5743. (strstr ( progName, "Z2CAT" ) != 0) ||
  5744. (strstr ( progName, "zcat" ) != 0) ||
  5745. (strstr ( progName, "ZCAT" ) != 0) ) {
  5746. opMode = OM_UNZ;
  5747. srcMode = (numFileNames == 0) ? SM_I2O : SM_F2O;
  5748. }
  5749. /*-- Look at the flags. --*/
  5750. for (aa = argList; aa != NULL; aa = aa->link) {
  5751. if (ISFLAG("--")) break;
  5752. if (aa->name[0] == '-' && aa->name[1] != '-') {
  5753. for (j = 1; aa->name[j] != '\0'; j++) {
  5754. switch (aa->name[j]) {
  5755. case 'c': srcMode = SM_F2O; break;
  5756. case 'd': opMode = OM_UNZ; break;
  5757. case 'z': opMode = OM_Z; break;
  5758. case 'f': forceOverwrite = True; break;
  5759. case 't': opMode = OM_TEST; break;
  5760. case 'k': keepInputFiles = True; break;
  5761. case 's': smallMode = True; break;
  5762. case 'q': noisy = False; break;
  5763. case '1': blockSize100k = 1; break;
  5764. case '2': blockSize100k = 2; break;
  5765. case '3': blockSize100k = 3; break;
  5766. case '4': blockSize100k = 4; break;
  5767. case '5': blockSize100k = 5; break;
  5768. case '6': blockSize100k = 6; break;
  5769. case '7': blockSize100k = 7; break;
  5770. case '8': blockSize100k = 8; break;
  5771. case '9': blockSize100k = 9; break;
  5772. case 'V':
  5773. case 'L': license(); break;
  5774. case 'v': verbosity++; break;
  5775. case 'h': usage ( progName );
  5776. exit ( 0 );
  5777. break;
  5778. default: fprintf ( stderr, "%s: Bad flag `%s'\n",
  5779. progName, aa->name );
  5780. usage ( progName );
  5781. exit ( 1 );
  5782. break;
  5783. }
  5784. }
  5785. }
  5786. }
  5787. /*-- And again ... --*/
  5788. for (aa = argList; aa != NULL; aa = aa->link) {
  5789. if (ISFLAG("--")) break;
  5790. if (ISFLAG("--stdout")) srcMode = SM_F2O; else
  5791. if (ISFLAG("--decompress")) opMode = OM_UNZ; else
  5792. if (ISFLAG("--compress")) opMode = OM_Z; else
  5793. if (ISFLAG("--force")) forceOverwrite = True; else
  5794. if (ISFLAG("--test")) opMode = OM_TEST; else
  5795. if (ISFLAG("--keep")) keepInputFiles = True; else
  5796. if (ISFLAG("--small")) smallMode = True; else
  5797. if (ISFLAG("--quiet")) noisy = False; else
  5798. if (ISFLAG("--version")) license(); else
  5799. if (ISFLAG("--license")) license(); else
  5800. if (ISFLAG("--exponential")) workFactor = 1; else
  5801. if (ISFLAG("--repetitive-best")) redundant(aa->name); else
  5802. if (ISFLAG("--repetitive-fast")) redundant(aa->name); else
  5803. if (ISFLAG("--fast")) blockSize100k = 1; else
  5804. if (ISFLAG("--best")) blockSize100k = 9; else
  5805. if (ISFLAG("--verbose")) verbosity++; else
  5806. if (ISFLAG("--help")) { usage ( progName ); exit ( 0 ); }
  5807. else
  5808. if (strncmp ( aa->name, "--", 2) == 0) {
  5809. fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name );
  5810. usage ( progName );
  5811. exit ( 1 );
  5812. }
  5813. }
  5814. if (verbosity > 4) verbosity = 4;
  5815. if (opMode == OM_Z && smallMode && blockSize100k > 2)
  5816. blockSize100k = 2;
  5817. if (opMode == OM_TEST && srcMode == SM_F2O) {
  5818. fprintf ( stderr, "%s: -c and -t cannot be used together.\n",
  5819. progName );
  5820. exit ( 1 );
  5821. }
  5822. if (srcMode == SM_F2O && numFileNames == 0)
  5823. srcMode = SM_I2O;
  5824. if (opMode != OM_Z) blockSize100k = 0;
  5825. if (srcMode == SM_F2F) {
  5826. signal (SIGINT, mySignalCatcher);
  5827. signal (SIGTERM, mySignalCatcher);
  5828. # if BZ_UNIX
  5829. signal (SIGHUP, mySignalCatcher);
  5830. # endif
  5831. }
  5832. if (opMode == OM_Z) {
  5833. if (srcMode == SM_I2O) {
  5834. compress ( NULL );
  5835. } else {
  5836. decode = True;
  5837. for (aa = argList; aa != NULL; aa = aa->link) {
  5838. if (ISFLAG("--")) { decode = False; continue; }
  5839. if (aa->name[0] == '-' && decode) continue;
  5840. numFilesProcessed++;
  5841. compress ( aa->name );
  5842. }
  5843. }
  5844. }
  5845. else
  5846. if (opMode == OM_UNZ) {
  5847. unzFailsExist = False;
  5848. if (srcMode == SM_I2O) {
  5849. uncompress ( NULL );
  5850. } else {
  5851. decode = True;
  5852. for (aa = argList; aa != NULL; aa = aa->link) {
  5853. if (ISFLAG("--")) { decode = False; continue; }
  5854. if (aa->name[0] == '-' && decode) continue;
  5855. numFilesProcessed++;
  5856. uncompress ( aa->name );
  5857. }
  5858. }
  5859. if (unzFailsExist) {
  5860. setExit(2);
  5861. exit(exitValue);
  5862. }
  5863. }
  5864. else {
  5865. testFailsExist = False;
  5866. if (srcMode == SM_I2O) {
  5867. testf ( NULL );
  5868. } else {
  5869. decode = True;
  5870. for (aa = argList; aa != NULL; aa = aa->link) {
  5871. if (ISFLAG("--")) { decode = False; continue; }
  5872. if (aa->name[0] == '-' && decode) continue;
  5873. numFilesProcessed++;
  5874. testf ( aa->name );
  5875. }
  5876. }
  5877. if (testFailsExist && noisy) {
  5878. fprintf ( stderr,
  5879. "\n"
  5880. "You can use the `bzip2recover' program to attempt to recover\n"
  5881. "data from undamaged sections of corrupted files.\n\n"
  5882. );
  5883. setExit(2);
  5884. exit(exitValue);
  5885. }
  5886. }
  5887. /* Free the argument list memory to mollify leak detectors
  5888. (eg) Purify, Checker. Serves no other useful purpose.
  5889. */
  5890. aa = argList;
  5891. while (aa != NULL) {
  5892. Cell* aa2 = aa->link;
  5893. if (aa->name != NULL) free(aa->name);
  5894. free(aa);
  5895. aa = aa2;
  5896. }
  5897. return exitValue;
  5898. }
  5899. /*-----------------------------------------------------------*/
  5900. /*--- end bzip2.c ---*/
  5901. /*-----------------------------------------------------------*/