archive_write_set_format_iso9660.c 207 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162
  1. /*-
  2. * Copyright (c) 2009-2012 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #ifdef HAVE_SYS_TYPES_H
  27. #include <sys/types.h>
  28. #endif
  29. #ifdef HAVE_SYS_UTSNAME_H
  30. #include <sys/utsname.h>
  31. #endif
  32. #ifdef HAVE_ERRNO_H
  33. #include <errno.h>
  34. #endif
  35. #ifdef HAVE_LIMITS_H
  36. #include <limits.h>
  37. #endif
  38. #include <stdio.h>
  39. #include <stdarg.h>
  40. #ifdef HAVE_STDLIB_H
  41. #include <stdlib.h>
  42. #endif
  43. #include <time.h>
  44. #ifdef HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #ifdef HAVE_ZLIB_H
  48. #include <cm_zlib.h>
  49. #endif
  50. #include "archive.h"
  51. #include "archive_endian.h"
  52. #include "archive_entry.h"
  53. #include "archive_entry_locale.h"
  54. #include "archive_private.h"
  55. #include "archive_rb.h"
  56. #include "archive_write_private.h"
  57. #if defined(_WIN32) && !defined(__CYGWIN__)
  58. #define getuid() 0
  59. #define getgid() 0
  60. #endif
  61. /*#define DEBUG 1*/
  62. #ifdef DEBUG
  63. /* To compare to the ISO image file made by mkisofs. */
  64. #define COMPAT_MKISOFS 1
  65. #endif
  66. #define LOGICAL_BLOCK_BITS 11
  67. #define LOGICAL_BLOCK_SIZE 2048
  68. #define PATH_TABLE_BLOCK_SIZE 4096
  69. #define SYSTEM_AREA_BLOCK 16
  70. #define PRIMARY_VOLUME_DESCRIPTOR_BLOCK 1
  71. #define SUPPLEMENTARY_VOLUME_DESCRIPTOR_BLOCK 1
  72. #define BOOT_RECORD_DESCRIPTOR_BLOCK 1
  73. #define VOLUME_DESCRIPTOR_SET_TERMINATOR_BLOCK 1
  74. #define NON_ISO_FILE_SYSTEM_INFORMATION_BLOCK 1
  75. #define RRIP_ER_BLOCK 1
  76. #define PADDING_BLOCK 150
  77. #define FD_1_2M_SIZE (1024 * 1200)
  78. #define FD_1_44M_SIZE (1024 * 1440)
  79. #define FD_2_88M_SIZE (1024 * 2880)
  80. #define MULTI_EXTENT_SIZE (ARCHIVE_LITERAL_LL(1) << 32) /* 4Gi bytes. */
  81. #define MAX_DEPTH 8
  82. #define RR_CE_SIZE 28 /* SUSP "CE" extension size */
  83. #define FILE_FLAG_EXISTENCE 0x01
  84. #define FILE_FLAG_DIRECTORY 0x02
  85. #define FILE_FLAG_ASSOCIATED 0x04
  86. #define FILE_FLAG_RECORD 0x08
  87. #define FILE_FLAG_PROTECTION 0x10
  88. #define FILE_FLAG_MULTI_EXTENT 0x80
  89. static const char rrip_identifier[] =
  90. "RRIP_1991A";
  91. static const char rrip_descriptor[] =
  92. "THE ROCK RIDGE INTERCHANGE PROTOCOL PROVIDES SUPPORT FOR "
  93. "POSIX FILE SYSTEM SEMANTICS";
  94. static const char rrip_source[] =
  95. "PLEASE CONTACT DISC PUBLISHER FOR SPECIFICATION SOURCE. "
  96. "SEE PUBLISHER IDENTIFIER IN PRIMARY VOLUME DESCRIPTOR FOR "
  97. "CONTACT INFORMATION.";
  98. #define RRIP_ER_ID_SIZE (sizeof(rrip_identifier)-1)
  99. #define RRIP_ER_DSC_SIZE (sizeof(rrip_descriptor)-1)
  100. #define RRIP_ER_SRC_SIZE (sizeof(rrip_source)-1)
  101. #define RRIP_ER_SIZE (8 + RRIP_ER_ID_SIZE + \
  102. RRIP_ER_DSC_SIZE + RRIP_ER_SRC_SIZE)
  103. static const unsigned char zisofs_magic[8] = {
  104. 0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
  105. };
  106. #define ZF_HEADER_SIZE 16 /* zisofs header size. */
  107. #define ZF_LOG2_BS 15 /* log2 block size; 32K bytes. */
  108. #define ZF_BLOCK_SIZE (1UL << ZF_LOG2_BS)
  109. /*
  110. * Manage extra records.
  111. */
  112. struct extr_rec {
  113. int location;
  114. int offset;
  115. unsigned char buf[LOGICAL_BLOCK_SIZE];
  116. struct extr_rec *next;
  117. };
  118. struct ctl_extr_rec {
  119. int use_extr;
  120. unsigned char *bp;
  121. struct isoent *isoent;
  122. unsigned char *ce_ptr;
  123. int cur_len;
  124. int dr_len;
  125. int limit;
  126. int extr_off;
  127. int extr_loc;
  128. };
  129. #define DR_SAFETY RR_CE_SIZE
  130. #define DR_LIMIT (254 - DR_SAFETY)
  131. /*
  132. * The relation of struct isofile and isoent and archive_entry.
  133. *
  134. * Primary volume tree --> struct isoent
  135. * |
  136. * v
  137. * struct isofile --> archive_entry
  138. * ^
  139. * |
  140. * Joliet volume tree --> struct isoent
  141. *
  142. * struct isoent has specific information for volume.
  143. */
  144. struct isofile {
  145. /* Used for managing struct isofile list. */
  146. struct isofile *allnext;
  147. struct isofile *datanext;
  148. /* Used for managing a hardlinked struct isofile list. */
  149. struct isofile *hlnext;
  150. struct isofile *hardlink_target;
  151. struct archive_entry *entry;
  152. /*
  153. * Used for making a directory tree.
  154. */
  155. struct archive_string parentdir;
  156. struct archive_string basename;
  157. struct archive_string basename_utf16;
  158. struct archive_string symlink;
  159. int dircnt; /* The number of elements of
  160. * its parent directory */
  161. /*
  162. * Used for a Directory Record.
  163. */
  164. struct content {
  165. int64_t offset_of_temp;
  166. int64_t size;
  167. int blocks;
  168. uint32_t location;
  169. /*
  170. * One extent equals one content.
  171. * If this entry has multi extent, `next' variable points
  172. * next content data.
  173. */
  174. struct content *next; /* next content */
  175. } content, *cur_content;
  176. int write_content;
  177. enum {
  178. NO = 0,
  179. BOOT_CATALOG,
  180. BOOT_IMAGE
  181. } boot;
  182. /*
  183. * Used for a zisofs.
  184. */
  185. struct {
  186. unsigned char header_size;
  187. unsigned char log2_bs;
  188. uint32_t uncompressed_size;
  189. } zisofs;
  190. };
  191. struct isoent {
  192. /* Keep `rbnode' at the first member of struct isoent. */
  193. struct archive_rb_node rbnode;
  194. struct isofile *file;
  195. struct isoent *parent;
  196. /* A list of children.(use chnext) */
  197. struct {
  198. struct isoent *first;
  199. struct isoent **last;
  200. int cnt;
  201. } children;
  202. struct archive_rb_tree rbtree;
  203. /* A list of sub directories.(use drnext) */
  204. struct {
  205. struct isoent *first;
  206. struct isoent **last;
  207. int cnt;
  208. } subdirs;
  209. /* A sorted list of sub directories. */
  210. struct isoent **children_sorted;
  211. /* Used for managing struct isoent list. */
  212. struct isoent *chnext;
  213. struct isoent *drnext;
  214. struct isoent *ptnext;
  215. /*
  216. * Used for making a Directory Record.
  217. */
  218. int dir_number;
  219. struct {
  220. int vd;
  221. int self;
  222. int parent;
  223. int normal;
  224. } dr_len;
  225. uint32_t dir_location;
  226. int dir_block;
  227. /*
  228. * Identifier:
  229. * on primary, ISO9660 file/directory name.
  230. * on joliet, UCS2 file/directory name.
  231. * ext_off : offset of identifier extension.
  232. * ext_len : length of identifier extension.
  233. * id_len : byte size of identifier.
  234. * on primary, this is ext_off + ext_len + version length.
  235. * on joliet, this is ext_off + ext_len.
  236. * mb_len : length of multibyte-character of identifier.
  237. * on primary, mb_len and id_len are always the same.
  238. * on joliet, mb_len and id_len are different.
  239. */
  240. char *identifier;
  241. int ext_off;
  242. int ext_len;
  243. int id_len;
  244. int mb_len;
  245. /*
  246. * Used for making a Rockridge extension.
  247. * This is a part of Directory Records.
  248. */
  249. struct isoent *rr_parent;
  250. struct isoent *rr_child;
  251. /* Extra Record.(which we call in this source file)
  252. * A maximum size of the Directory Record is 254.
  253. * so, if generated RRIP data of a file cannot into a Directory
  254. * Record because of its size, that surplus data relocate this
  255. * Extra Record.
  256. */
  257. struct {
  258. struct extr_rec *first;
  259. struct extr_rec **last;
  260. struct extr_rec *current;
  261. } extr_rec_list;
  262. signed int virtual:1;
  263. /* If set to one, this file type is a directory.
  264. * A convenience flag to be used as
  265. * "archive_entry_filetype(isoent->file->entry) == AE_IFDIR".
  266. */
  267. signed int dir:1;
  268. };
  269. struct hardlink {
  270. struct archive_rb_node rbnode;
  271. int nlink;
  272. struct {
  273. struct isofile *first;
  274. struct isofile **last;
  275. } file_list;
  276. };
  277. /*
  278. * ISO writer options
  279. */
  280. struct iso_option {
  281. /*
  282. * Usage : abstract-file=<value>
  283. * Type : string, max 37 bytes
  284. * Default: Not specified
  285. * COMPAT : mkisofs -abstract <value>
  286. *
  287. * Specifies Abstract Filename.
  288. * This file shall be described in the Root Directory
  289. * and containing a abstract statement.
  290. */
  291. unsigned int abstract_file:1;
  292. #define OPT_ABSTRACT_FILE_DEFAULT 0 /* Not specified */
  293. #define ABSTRACT_FILE_SIZE 37
  294. /*
  295. * Usage : application-id=<value>
  296. * Type : string, max 128 bytes
  297. * Default: Not specified
  298. * COMPAT : mkisofs -A/-appid <value>.
  299. *
  300. * Specifies Application Identifier.
  301. * If the first byte is set to '_'(5F), the remaining
  302. * bytes of this option shall specify an identifier
  303. * for a file containing the identification of the
  304. * application.
  305. * This file shall be described in the Root Directory.
  306. */
  307. unsigned int application_id:1;
  308. #define OPT_APPLICATION_ID_DEFAULT 0 /* Use default identifier */
  309. #define APPLICATION_IDENTIFIER_SIZE 128
  310. /*
  311. * Usage : !allow-vernum
  312. * Type : boolean
  313. * Default: Enabled
  314. * : Violates the ISO9660 standard if disable.
  315. * COMPAT: mkisofs -N
  316. *
  317. * Allow filenames to use version numbers.
  318. */
  319. unsigned int allow_vernum:1;
  320. #define OPT_ALLOW_VERNUM_DEFAULT 1 /* Enabled */
  321. /*
  322. * Usage : biblio-file=<value>
  323. * Type : string, max 37 bytes
  324. * Default: Not specified
  325. * COMPAT : mkisofs -biblio <value>
  326. *
  327. * Specifies Bibliographic Filename.
  328. * This file shall be described in the Root Directory
  329. * and containing bibliographic records.
  330. */
  331. unsigned int biblio_file:1;
  332. #define OPT_BIBLIO_FILE_DEFAULT 0 /* Not specified */
  333. #define BIBLIO_FILE_SIZE 37
  334. /*
  335. * Usage : boot=<value>
  336. * Type : string
  337. * Default: Not specified
  338. * COMPAT : mkisofs -b/-eltorito-boot <value>
  339. *
  340. * Specifies "El Torito" boot image file to make
  341. * a bootable CD.
  342. */
  343. unsigned int boot:1;
  344. #define OPT_BOOT_DEFAULT 0 /* Not specified */
  345. /*
  346. * Usage : boot-catalog=<value>
  347. * Type : string
  348. * Default: "boot.catalog"
  349. * COMPAT : mkisofs -c/-eltorito-catalog <value>
  350. *
  351. * Specifies a fullpath of El Torito boot catalog.
  352. */
  353. unsigned int boot_catalog:1;
  354. #define OPT_BOOT_CATALOG_DEFAULT 0 /* Not specified */
  355. /*
  356. * Usage : boot-info-table
  357. * Type : boolean
  358. * Default: Disabled
  359. * COMPAT : mkisofs -boot-info-table
  360. *
  361. * Modify the boot image file specified by `boot'
  362. * option; ISO writer stores boot file information
  363. * into the boot file in ISO image at offset 8
  364. * through offset 64.
  365. */
  366. unsigned int boot_info_table:1;
  367. #define OPT_BOOT_INFO_TABLE_DEFAULT 0 /* Disabled */
  368. /*
  369. * Usage : boot-load-seg=<value>
  370. * Type : hexadecimal
  371. * Default: Not specified
  372. * COMPAT : mkisofs -boot-load-seg <value>
  373. *
  374. * Specifies a load segment for boot image.
  375. * This is used with no-emulation mode.
  376. */
  377. unsigned int boot_load_seg:1;
  378. #define OPT_BOOT_LOAD_SEG_DEFAULT 0 /* Not specified */
  379. /*
  380. * Usage : boot-load-size=<value>
  381. * Type : decimal
  382. * Default: Not specified
  383. * COMPAT : mkisofs -boot-load-size <value>
  384. *
  385. * Specifies a sector count for boot image.
  386. * This is used with no-emulation mode.
  387. */
  388. unsigned int boot_load_size:1;
  389. #define OPT_BOOT_LOAD_SIZE_DEFAULT 0 /* Not specified */
  390. /*
  391. * Usage : boot-type=<boot-media-type>
  392. * : 'no-emulation' : 'no emulation' image
  393. * : 'fd' : floppy disk image
  394. * : 'hard-disk' : hard disk image
  395. * Type : string
  396. * Default: Auto detect
  397. * : We check a size of boot image;
  398. * : If the size is just 1.22M/1.44M/2.88M,
  399. * : we assume boot_type is 'fd';
  400. * : otherwise boot_type is 'no-emulation'.
  401. * COMPAT :
  402. * boot=no-emulation
  403. * mkisofs -no-emul-boot
  404. * boot=fd
  405. * This is a default on the mkisofs.
  406. * boot=hard-disk
  407. * mkisofs -hard-disk-boot
  408. *
  409. * Specifies a type of "El Torito" boot image.
  410. */
  411. unsigned int boot_type:2;
  412. #define OPT_BOOT_TYPE_AUTO 0 /* auto detect */
  413. #define OPT_BOOT_TYPE_NO_EMU 1 /* ``no emulation'' image */
  414. #define OPT_BOOT_TYPE_FD 2 /* floppy disk image */
  415. #define OPT_BOOT_TYPE_HARD_DISK 3 /* hard disk image */
  416. #define OPT_BOOT_TYPE_DEFAULT OPT_BOOT_TYPE_AUTO
  417. /*
  418. * Usage : compression-level=<value>
  419. * Type : decimal
  420. * Default: Not specified
  421. * COMPAT : NONE
  422. *
  423. * Specifies compression level for option zisofs=direct.
  424. */
  425. unsigned int compression_level:1;
  426. #define OPT_COMPRESSION_LEVEL_DEFAULT 0 /* Not specified */
  427. /*
  428. * Usage : copyright-file=<value>
  429. * Type : string, max 37 bytes
  430. * Default: Not specified
  431. * COMPAT : mkisofs -copyright <value>
  432. *
  433. * Specifies Copyright Filename.
  434. * This file shall be described in the Root Directory
  435. * and containing a copyright statement.
  436. */
  437. unsigned int copyright_file:1;
  438. #define OPT_COPYRIGHT_FILE_DEFAULT 0 /* Not specified */
  439. #define COPYRIGHT_FILE_SIZE 37
  440. /*
  441. * Usage : gid=<value>
  442. * Type : decimal
  443. * Default: Not specified
  444. * COMPAT : mkisofs -gid <value>
  445. *
  446. * Specifies a group id to rewrite the group id of all files.
  447. */
  448. unsigned int gid:1;
  449. #define OPT_GID_DEFAULT 0 /* Not specified */
  450. /*
  451. * Usage : iso-level=[1234]
  452. * Type : decimal
  453. * Default: 1
  454. * COMPAT : mkisofs -iso-level <value>
  455. *
  456. * Specifies ISO9600 Level.
  457. * Level 1: [DEFAULT]
  458. * - limits each file size less than 4Gi bytes;
  459. * - a File Name shall not contain more than eight
  460. * d-characters or eight d1-characters;
  461. * - a File Name Extension shall not contain more than
  462. * three d-characters or three d1-characters;
  463. * - a Directory Identifier shall not contain more
  464. * than eight d-characters or eight d1-characters.
  465. * Level 2:
  466. * - limits each file size less than 4Giga bytes;
  467. * - a File Name shall not contain more than thirty
  468. * d-characters or thirty d1-characters;
  469. * - a File Name Extension shall not contain more than
  470. * thirty d-characters or thirty d1-characters;
  471. * - a Directory Identifier shall not contain more
  472. * than thirty-one d-characters or thirty-one
  473. * d1-characters.
  474. * Level 3:
  475. * - no limit of file size; use multi extent.
  476. * Level 4:
  477. * - this level 4 simulates mkisofs option
  478. * '-iso-level 4';
  479. * - crate a enhanced volume as mkisofs doing;
  480. * - allow a File Name to have leading dot;
  481. * - allow a File Name to have all ASCII letters;
  482. * - allow a File Name to have multiple dots;
  483. * - allow more then 8 depths of directory trees;
  484. * - disable a version number to a File Name;
  485. * - disable a forced period to the tail of a File Name;
  486. * - the maximum length of files and directories is raised to 193.
  487. * if rockridge option is disabled, raised to 207.
  488. */
  489. unsigned int iso_level:3;
  490. #define OPT_ISO_LEVEL_DEFAULT 1 /* ISO Level 1 */
  491. /*
  492. * Usage : joliet[=long]
  493. * : !joliet
  494. * : Do not generate Joliet Volume and Records.
  495. * : joliet [DEFAULT]
  496. * : Generates Joliet Volume and Directory Records.
  497. * : [COMPAT: mkisofs -J/-joliet]
  498. * : joliet=long
  499. * : The joliet filenames are up to 103 Unicode
  500. * : characters.
  501. * : This option breaks the Joliet specification.
  502. * : [COMPAT: mkisofs -J -joliet-long]
  503. * Type : boolean/string
  504. * Default: Enabled
  505. * COMPAT : mkisofs -J / -joliet-long
  506. *
  507. * Generates Joliet Volume and Directory Records.
  508. */
  509. unsigned int joliet:2;
  510. #define OPT_JOLIET_DISABLE 0 /* Not generate Joliet Records. */
  511. #define OPT_JOLIET_ENABLE 1 /* Generate Joliet Records. */
  512. #define OPT_JOLIET_LONGNAME 2 /* Use long joliet filenames.*/
  513. #define OPT_JOLIET_DEFAULT OPT_JOLIET_ENABLE
  514. /*
  515. * Usage : !limit-depth
  516. * Type : boolean
  517. * Default: Enabled
  518. * : Violates the ISO9660 standard if disable.
  519. * COMPAT : mkisofs -D/-disable-deep-relocation
  520. *
  521. * The number of levels in hierarchy cannot exceed eight.
  522. */
  523. unsigned int limit_depth:1;
  524. #define OPT_LIMIT_DEPTH_DEFAULT 1 /* Enabled */
  525. /*
  526. * Usage : !limit-dirs
  527. * Type : boolean
  528. * Default: Enabled
  529. * : Violates the ISO9660 standard if disable.
  530. * COMPAT : mkisofs -no-limit-pathtables
  531. *
  532. * Limits the number of directories less than 65536 due
  533. * to the size of the Parent Directory Number of Path
  534. * Table.
  535. */
  536. unsigned int limit_dirs:1;
  537. #define OPT_LIMIT_DIRS_DEFAULT 1 /* Enabled */
  538. /*
  539. * Usage : !pad
  540. * Type : boolean
  541. * Default: Enabled
  542. * COMPAT : -pad/-no-pad
  543. *
  544. * Pads the end of the ISO image by null of 300Ki bytes.
  545. */
  546. unsigned int pad:1;
  547. #define OPT_PAD_DEFAULT 1 /* Enabled */
  548. /*
  549. * Usage : publisher=<value>
  550. * Type : string, max 128 bytes
  551. * Default: Not specified
  552. * COMPAT : mkisofs -publisher <value>
  553. *
  554. * Specifies Publisher Identifier.
  555. * If the first byte is set to '_'(5F), the remaining
  556. * bytes of this option shall specify an identifier
  557. * for a file containing the identification of the user.
  558. * This file shall be described in the Root Directory.
  559. */
  560. unsigned int publisher:1;
  561. #define OPT_PUBLISHER_DEFAULT 0 /* Not specified */
  562. #define PUBLISHER_IDENTIFIER_SIZE 128
  563. /*
  564. * Usage : rockridge
  565. * : !rockridge
  566. * : disable to generate SUSP and RR records.
  567. * : rockridge
  568. * : the same as 'rockridge=useful'.
  569. * : rockridge=strict
  570. * : generate SUSP and RR records.
  571. * : [COMPAT: mkisofs -R]
  572. * : rockridge=useful [DEFAULT]
  573. * : generate SUSP and RR records.
  574. * : [COMPAT: mkisofs -r]
  575. * : NOTE Our rockridge=useful option does not set a zero
  576. * : to uid and gid, you should use application
  577. * : option such as --gid,--gname,--uid and --uname
  578. * : bsdtar options instead.
  579. * Type : boolean/string
  580. * Default: Enabled as rockridge=useful
  581. * COMPAT : mkisofs -r / -R
  582. *
  583. * Generates SUSP and RR records.
  584. */
  585. unsigned int rr:2;
  586. #define OPT_RR_DISABLED 0
  587. #define OPT_RR_STRICT 1
  588. #define OPT_RR_USEFUL 2
  589. #define OPT_RR_DEFAULT OPT_RR_USEFUL
  590. /*
  591. * Usage : volume-id=<value>
  592. * Type : string, max 32 bytes
  593. * Default: Not specified
  594. * COMPAT : mkisofs -V <value>
  595. *
  596. * Specifies Volume Identifier.
  597. */
  598. unsigned int volume_id:1;
  599. #define OPT_VOLUME_ID_DEFAULT 0 /* Use default identifier */
  600. #define VOLUME_IDENTIFIER_SIZE 32
  601. /*
  602. * Usage : !zisofs [DEFAULT]
  603. * : Disable to generate RRIP 'ZF' extension.
  604. * : zisofs
  605. * : Make files zisofs file and generate RRIP 'ZF'
  606. * : extension. So you do not need mkzftree utility
  607. * : for making zisofs.
  608. * : When the file size is less than one Logical Block
  609. * : size, that file will not zisofs'ed since it does
  610. * : reduce an ISO-image size.
  611. * :
  612. * : When you specify option 'boot=<boot-image>', that
  613. * : 'boot-image' file won't be converted to zisofs file.
  614. * Type : boolean
  615. * Default: Disabled
  616. *
  617. * Generates RRIP 'ZF' System Use Entry.
  618. */
  619. unsigned int zisofs:1;
  620. #define OPT_ZISOFS_DISABLED 0
  621. #define OPT_ZISOFS_DIRECT 1
  622. #define OPT_ZISOFS_DEFAULT OPT_ZISOFS_DISABLED
  623. };
  624. struct iso9660 {
  625. /* The creation time of ISO image. */
  626. time_t birth_time;
  627. /* A file stream of a temporary file, which file contents
  628. * save to until ISO image can be created. */
  629. int temp_fd;
  630. struct isofile *cur_file;
  631. struct isoent *cur_dirent;
  632. struct archive_string cur_dirstr;
  633. uint64_t bytes_remaining;
  634. int need_multi_extent;
  635. /* Temporary string buffer for Joliet extension. */
  636. struct archive_string utf16be;
  637. struct archive_string mbs;
  638. struct archive_string_conv *sconv_to_utf16be;
  639. struct archive_string_conv *sconv_from_utf16be;
  640. /* A list of all of struct isofile entries. */
  641. struct {
  642. struct isofile *first;
  643. struct isofile **last;
  644. } all_file_list;
  645. /* A list of struct isofile entries which have its
  646. * contents and are not a directory, a hardlinked file
  647. * and a symlink file. */
  648. struct {
  649. struct isofile *first;
  650. struct isofile **last;
  651. } data_file_list;
  652. /* Used for managing to find hardlinking files. */
  653. struct archive_rb_tree hardlink_rbtree;
  654. /* Used for making the Path Table Record. */
  655. struct vdd {
  656. /* the root of entry tree. */
  657. struct isoent *rootent;
  658. enum vdd_type {
  659. VDD_PRIMARY,
  660. VDD_JOLIET,
  661. VDD_ENHANCED
  662. } vdd_type;
  663. struct path_table {
  664. struct isoent *first;
  665. struct isoent **last;
  666. struct isoent **sorted;
  667. int cnt;
  668. } *pathtbl;
  669. int max_depth;
  670. int path_table_block;
  671. int path_table_size;
  672. int location_type_L_path_table;
  673. int location_type_M_path_table;
  674. int total_dir_block;
  675. } primary, joliet;
  676. /* Used for making a Volume Descriptor. */
  677. int volume_space_size;
  678. int volume_sequence_number;
  679. int total_file_block;
  680. struct archive_string volume_identifier;
  681. struct archive_string publisher_identifier;
  682. struct archive_string data_preparer_identifier;
  683. struct archive_string application_identifier;
  684. struct archive_string copyright_file_identifier;
  685. struct archive_string abstract_file_identifier;
  686. struct archive_string bibliographic_file_identifier;
  687. /* Used for making rockridge extensions. */
  688. int location_rrip_er;
  689. /* Used for making zisofs. */
  690. struct {
  691. signed int detect_magic:1;
  692. signed int making:1;
  693. signed int allzero:1;
  694. unsigned char magic_buffer[64];
  695. int magic_cnt;
  696. #ifdef HAVE_ZLIB_H
  697. /*
  698. * Copy a compressed file to iso9660.zisofs.temp_fd
  699. * and also copy a uncompressed file(original file) to
  700. * iso9660.temp_fd . If the number of logical block
  701. * of the compressed file is less than the number of
  702. * logical block of the uncompressed file, use it and
  703. * remove the copy of the uncompressed file.
  704. * but if not, we use uncompressed file and remove
  705. * the copy of the compressed file.
  706. */
  707. uint32_t *block_pointers;
  708. size_t block_pointers_allocated;
  709. int block_pointers_cnt;
  710. int block_pointers_idx;
  711. int64_t total_size;
  712. int64_t block_offset;
  713. z_stream stream;
  714. int stream_valid;
  715. int64_t remaining;
  716. int compression_level;
  717. #endif
  718. } zisofs;
  719. struct isoent *directories_too_deep;
  720. int dircnt_max;
  721. /* Write buffer. */
  722. #define wb_buffmax() (LOGICAL_BLOCK_SIZE * 32)
  723. #define wb_remaining(a) (((struct iso9660 *)(a)->format_data)->wbuff_remaining)
  724. #define wb_offset(a) (((struct iso9660 *)(a)->format_data)->wbuff_offset \
  725. + wb_buffmax() - wb_remaining(a))
  726. unsigned char wbuff[LOGICAL_BLOCK_SIZE * 32];
  727. size_t wbuff_remaining;
  728. enum {
  729. WB_TO_STREAM,
  730. WB_TO_TEMP
  731. } wbuff_type;
  732. int64_t wbuff_offset;
  733. int64_t wbuff_written;
  734. int64_t wbuff_tail;
  735. /* 'El Torito' boot data. */
  736. struct {
  737. /* boot catalog file */
  738. struct archive_string catalog_filename;
  739. struct isoent *catalog;
  740. /* boot image file */
  741. struct archive_string boot_filename;
  742. struct isoent *boot;
  743. unsigned char platform_id;
  744. #define BOOT_PLATFORM_X86 0
  745. #define BOOT_PLATFORM_PPC 1
  746. #define BOOT_PLATFORM_MAC 2
  747. struct archive_string id;
  748. unsigned char media_type;
  749. #define BOOT_MEDIA_NO_EMULATION 0
  750. #define BOOT_MEDIA_1_2M_DISKETTE 1
  751. #define BOOT_MEDIA_1_44M_DISKETTE 2
  752. #define BOOT_MEDIA_2_88M_DISKETTE 3
  753. #define BOOT_MEDIA_HARD_DISK 4
  754. unsigned char system_type;
  755. uint16_t boot_load_seg;
  756. uint16_t boot_load_size;
  757. #define BOOT_LOAD_SIZE 4
  758. } el_torito;
  759. struct iso_option opt;
  760. };
  761. /*
  762. * Types of Volume Descriptor
  763. */
  764. enum VD_type {
  765. VDT_BOOT_RECORD=0, /* Boot Record Volume Descriptor */
  766. VDT_PRIMARY=1, /* Primary Volume Descriptor */
  767. VDT_SUPPLEMENTARY=2, /* Supplementary Volume Descriptor */
  768. VDT_TERMINATOR=255 /* Volume Descriptor Set Terminator */
  769. };
  770. /*
  771. * Types of Directory Record
  772. */
  773. enum dir_rec_type {
  774. DIR_REC_VD, /* Stored in Volume Descriptor. */
  775. DIR_REC_SELF, /* Stored as Current Directory. */
  776. DIR_REC_PARENT, /* Stored as Parent Directory. */
  777. DIR_REC_NORMAL /* Stored as Child. */
  778. };
  779. /*
  780. * Kinds of Volume Descriptor Character
  781. */
  782. enum vdc {
  783. VDC_STD,
  784. VDC_LOWERCASE,
  785. VDC_UCS2,
  786. VDC_UCS2_DIRECT
  787. };
  788. /*
  789. * IDentifier Resolver.
  790. * Used for resolving duplicated filenames.
  791. */
  792. struct idr {
  793. struct idrent {
  794. struct archive_rb_node rbnode;
  795. /* Used in wait_list. */
  796. struct idrent *wnext;
  797. struct idrent *avail;
  798. struct isoent *isoent;
  799. int weight;
  800. int noff;
  801. int rename_num;
  802. } *idrent_pool;
  803. struct archive_rb_tree rbtree;
  804. struct {
  805. struct idrent *first;
  806. struct idrent **last;
  807. } wait_list;
  808. int pool_size;
  809. int pool_idx;
  810. int num_size;
  811. int null_size;
  812. char char_map[0x80];
  813. };
  814. enum char_type {
  815. A_CHAR,
  816. D_CHAR
  817. };
  818. static int iso9660_options(struct archive_write *,
  819. const char *, const char *);
  820. static int iso9660_write_header(struct archive_write *,
  821. struct archive_entry *);
  822. static ssize_t iso9660_write_data(struct archive_write *,
  823. const void *, size_t);
  824. static int iso9660_finish_entry(struct archive_write *);
  825. static int iso9660_close(struct archive_write *);
  826. static int iso9660_free(struct archive_write *);
  827. static void get_system_identitier(char *, size_t);
  828. static void set_str(unsigned char *, const char *, size_t, char,
  829. const char *);
  830. static inline int joliet_allowed_char(unsigned char, unsigned char);
  831. static int set_str_utf16be(struct archive_write *, unsigned char *,
  832. const char *, size_t, uint16_t, enum vdc);
  833. static int set_str_a_characters_bp(struct archive_write *,
  834. unsigned char *, int, int, const char *, enum vdc);
  835. static int set_str_d_characters_bp(struct archive_write *,
  836. unsigned char *, int, int, const char *, enum vdc);
  837. static void set_VD_bp(unsigned char *, enum VD_type, unsigned char);
  838. static inline void set_unused_field_bp(unsigned char *, int, int);
  839. static unsigned char *extra_open_record(unsigned char *, int,
  840. struct isoent *, struct ctl_extr_rec *);
  841. static void extra_close_record(struct ctl_extr_rec *, int);
  842. static unsigned char * extra_next_record(struct ctl_extr_rec *, int);
  843. static unsigned char *extra_get_record(struct isoent *, int *, int *, int *);
  844. static void extra_tell_used_size(struct ctl_extr_rec *, int);
  845. static int extra_setup_location(struct isoent *, int);
  846. static int set_directory_record_rr(unsigned char *, int,
  847. struct isoent *, struct iso9660 *, enum dir_rec_type);
  848. static int set_directory_record(unsigned char *, size_t,
  849. struct isoent *, struct iso9660 *, enum dir_rec_type,
  850. enum vdd_type);
  851. static inline int get_dir_rec_size(struct iso9660 *, struct isoent *,
  852. enum dir_rec_type, enum vdd_type);
  853. static inline unsigned char *wb_buffptr(struct archive_write *);
  854. static int wb_write_out(struct archive_write *);
  855. static int wb_consume(struct archive_write *, size_t);
  856. #ifdef HAVE_ZLIB_H
  857. static int wb_set_offset(struct archive_write *, int64_t);
  858. #endif
  859. static int write_null(struct archive_write *, size_t);
  860. static int write_VD_terminator(struct archive_write *);
  861. static int set_file_identifier(unsigned char *, int, int, enum vdc,
  862. struct archive_write *, struct vdd *,
  863. struct archive_string *, const char *, int,
  864. enum char_type);
  865. static int write_VD(struct archive_write *, struct vdd *);
  866. static int write_VD_boot_record(struct archive_write *);
  867. static int write_information_block(struct archive_write *);
  868. static int write_path_table(struct archive_write *, int,
  869. struct vdd *);
  870. static int write_directory_descriptors(struct archive_write *,
  871. struct vdd *);
  872. static int write_file_descriptors(struct archive_write *);
  873. static int write_rr_ER(struct archive_write *);
  874. static void calculate_path_table_size(struct vdd *);
  875. static void isofile_init_entry_list(struct iso9660 *);
  876. static void isofile_add_entry(struct iso9660 *, struct isofile *);
  877. static void isofile_free_all_entries(struct iso9660 *);
  878. static void isofile_init_entry_data_file_list(struct iso9660 *);
  879. static void isofile_add_data_file(struct iso9660 *, struct isofile *);
  880. static struct isofile * isofile_new(struct archive_write *,
  881. struct archive_entry *);
  882. static void isofile_free(struct isofile *);
  883. static int isofile_gen_utility_names(struct archive_write *,
  884. struct isofile *);
  885. static int isofile_register_hardlink(struct archive_write *,
  886. struct isofile *);
  887. static void isofile_connect_hardlink_files(struct iso9660 *);
  888. static void isofile_init_hardlinks(struct iso9660 *);
  889. static void isofile_free_hardlinks(struct iso9660 *);
  890. static struct isoent *isoent_new(struct isofile *);
  891. static int isoent_clone_tree(struct archive_write *,
  892. struct isoent **, struct isoent *);
  893. static void _isoent_free(struct isoent *isoent);
  894. static void isoent_free_all(struct isoent *);
  895. static struct isoent * isoent_create_virtual_dir(struct archive_write *,
  896. struct iso9660 *, const char *);
  897. static int isoent_cmp_node(const struct archive_rb_node *,
  898. const struct archive_rb_node *);
  899. static int isoent_cmp_key(const struct archive_rb_node *,
  900. const void *);
  901. static int isoent_add_child_head(struct isoent *, struct isoent *);
  902. static int isoent_add_child_tail(struct isoent *, struct isoent *);
  903. static void isoent_remove_child(struct isoent *, struct isoent *);
  904. static void isoent_setup_directory_location(struct iso9660 *,
  905. int, struct vdd *);
  906. static void isoent_setup_file_location(struct iso9660 *, int);
  907. static int get_path_component(char *, size_t, const char *);
  908. static int isoent_tree(struct archive_write *, struct isoent **);
  909. static struct isoent *isoent_find_child(struct isoent *, const char *);
  910. static struct isoent *isoent_find_entry(struct isoent *, const char *);
  911. static void idr_relaxed_filenames(char *);
  912. static void idr_init(struct iso9660 *, struct vdd *, struct idr *);
  913. static void idr_cleanup(struct idr *);
  914. static int idr_ensure_poolsize(struct archive_write *, struct idr *,
  915. int);
  916. static int idr_start(struct archive_write *, struct idr *,
  917. int, int, int, int, const struct archive_rb_tree_ops *);
  918. static void idr_register(struct idr *, struct isoent *, int,
  919. int);
  920. static void idr_extend_identifier(struct idrent *, int, int);
  921. static void idr_resolve(struct idr *, void (*)(unsigned char *, int));
  922. static void idr_set_num(unsigned char *, int);
  923. static void idr_set_num_beutf16(unsigned char *, int);
  924. static int isoent_gen_iso9660_identifier(struct archive_write *,
  925. struct isoent *, struct idr *);
  926. static int isoent_gen_joliet_identifier(struct archive_write *,
  927. struct isoent *, struct idr *);
  928. static int isoent_cmp_iso9660_identifier(const struct isoent *,
  929. const struct isoent *);
  930. static int isoent_cmp_node_iso9660(const struct archive_rb_node *,
  931. const struct archive_rb_node *);
  932. static int isoent_cmp_key_iso9660(const struct archive_rb_node *,
  933. const void *);
  934. static int isoent_cmp_joliet_identifier(const struct isoent *,
  935. const struct isoent *);
  936. static int isoent_cmp_node_joliet(const struct archive_rb_node *,
  937. const struct archive_rb_node *);
  938. static int isoent_cmp_key_joliet(const struct archive_rb_node *,
  939. const void *);
  940. static inline void path_table_add_entry(struct path_table *, struct isoent *);
  941. static inline struct isoent * path_table_last_entry(struct path_table *);
  942. static int isoent_make_path_table(struct archive_write *);
  943. static int isoent_find_out_boot_file(struct archive_write *,
  944. struct isoent *);
  945. static int isoent_create_boot_catalog(struct archive_write *,
  946. struct isoent *);
  947. static size_t fd_boot_image_size(int);
  948. static int make_boot_catalog(struct archive_write *);
  949. static int setup_boot_information(struct archive_write *);
  950. static int zisofs_init(struct archive_write *, struct isofile *);
  951. static void zisofs_detect_magic(struct archive_write *,
  952. const void *, size_t);
  953. static int zisofs_write_to_temp(struct archive_write *,
  954. const void *, size_t);
  955. static int zisofs_finish_entry(struct archive_write *);
  956. static int zisofs_rewind_boot_file(struct archive_write *);
  957. static int zisofs_free(struct archive_write *);
  958. int
  959. archive_write_set_format_iso9660(struct archive *_a)
  960. {
  961. struct archive_write *a = (struct archive_write *)_a;
  962. struct iso9660 *iso9660;
  963. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  964. ARCHIVE_STATE_NEW, "archive_write_set_format_iso9660");
  965. /* If another format was already registered, unregister it. */
  966. if (a->format_free != NULL)
  967. (a->format_free)(a);
  968. iso9660 = calloc(1, sizeof(*iso9660));
  969. if (iso9660 == NULL) {
  970. archive_set_error(&a->archive, ENOMEM,
  971. "Can't allocate iso9660 data");
  972. return (ARCHIVE_FATAL);
  973. }
  974. iso9660->birth_time = 0;
  975. iso9660->temp_fd = -1;
  976. iso9660->cur_file = NULL;
  977. iso9660->primary.max_depth = 0;
  978. iso9660->primary.vdd_type = VDD_PRIMARY;
  979. iso9660->primary.pathtbl = NULL;
  980. iso9660->joliet.rootent = NULL;
  981. iso9660->joliet.max_depth = 0;
  982. iso9660->joliet.vdd_type = VDD_JOLIET;
  983. iso9660->joliet.pathtbl = NULL;
  984. isofile_init_entry_list(iso9660);
  985. isofile_init_entry_data_file_list(iso9660);
  986. isofile_init_hardlinks(iso9660);
  987. iso9660->directories_too_deep = NULL;
  988. iso9660->dircnt_max = 1;
  989. iso9660->wbuff_remaining = wb_buffmax();
  990. iso9660->wbuff_type = WB_TO_TEMP;
  991. iso9660->wbuff_offset = 0;
  992. iso9660->wbuff_written = 0;
  993. iso9660->wbuff_tail = 0;
  994. archive_string_init(&(iso9660->utf16be));
  995. archive_string_init(&(iso9660->mbs));
  996. /*
  997. * Init Identifiers used for PVD and SVD.
  998. */
  999. archive_string_init(&(iso9660->volume_identifier));
  1000. archive_strcpy(&(iso9660->volume_identifier), "CDROM");
  1001. archive_string_init(&(iso9660->publisher_identifier));
  1002. archive_string_init(&(iso9660->data_preparer_identifier));
  1003. archive_string_init(&(iso9660->application_identifier));
  1004. archive_strcpy(&(iso9660->application_identifier),
  1005. archive_version_string());
  1006. archive_string_init(&(iso9660->copyright_file_identifier));
  1007. archive_string_init(&(iso9660->abstract_file_identifier));
  1008. archive_string_init(&(iso9660->bibliographic_file_identifier));
  1009. /*
  1010. * Init El Torito bootable CD variables.
  1011. */
  1012. archive_string_init(&(iso9660->el_torito.catalog_filename));
  1013. iso9660->el_torito.catalog = NULL;
  1014. /* Set default file name of boot catalog */
  1015. archive_strcpy(&(iso9660->el_torito.catalog_filename),
  1016. "boot.catalog");
  1017. archive_string_init(&(iso9660->el_torito.boot_filename));
  1018. iso9660->el_torito.boot = NULL;
  1019. iso9660->el_torito.platform_id = BOOT_PLATFORM_X86;
  1020. archive_string_init(&(iso9660->el_torito.id));
  1021. iso9660->el_torito.boot_load_seg = 0;
  1022. iso9660->el_torito.boot_load_size = BOOT_LOAD_SIZE;
  1023. /*
  1024. * Init zisofs variables.
  1025. */
  1026. #ifdef HAVE_ZLIB_H
  1027. iso9660->zisofs.block_pointers = NULL;
  1028. iso9660->zisofs.block_pointers_allocated = 0;
  1029. iso9660->zisofs.stream_valid = 0;
  1030. iso9660->zisofs.compression_level = 9;
  1031. memset(&(iso9660->zisofs.stream), 0,
  1032. sizeof(iso9660->zisofs.stream));
  1033. #endif
  1034. /*
  1035. * Set default value of iso9660 options.
  1036. */
  1037. iso9660->opt.abstract_file = OPT_ABSTRACT_FILE_DEFAULT;
  1038. iso9660->opt.application_id = OPT_APPLICATION_ID_DEFAULT;
  1039. iso9660->opt.allow_vernum = OPT_ALLOW_VERNUM_DEFAULT;
  1040. iso9660->opt.biblio_file = OPT_BIBLIO_FILE_DEFAULT;
  1041. iso9660->opt.boot = OPT_BOOT_DEFAULT;
  1042. iso9660->opt.boot_catalog = OPT_BOOT_CATALOG_DEFAULT;
  1043. iso9660->opt.boot_info_table = OPT_BOOT_INFO_TABLE_DEFAULT;
  1044. iso9660->opt.boot_load_seg = OPT_BOOT_LOAD_SEG_DEFAULT;
  1045. iso9660->opt.boot_load_size = OPT_BOOT_LOAD_SIZE_DEFAULT;
  1046. iso9660->opt.boot_type = OPT_BOOT_TYPE_DEFAULT;
  1047. iso9660->opt.compression_level = OPT_COMPRESSION_LEVEL_DEFAULT;
  1048. iso9660->opt.copyright_file = OPT_COPYRIGHT_FILE_DEFAULT;
  1049. iso9660->opt.iso_level = OPT_ISO_LEVEL_DEFAULT;
  1050. iso9660->opt.joliet = OPT_JOLIET_DEFAULT;
  1051. iso9660->opt.limit_depth = OPT_LIMIT_DEPTH_DEFAULT;
  1052. iso9660->opt.limit_dirs = OPT_LIMIT_DIRS_DEFAULT;
  1053. iso9660->opt.pad = OPT_PAD_DEFAULT;
  1054. iso9660->opt.publisher = OPT_PUBLISHER_DEFAULT;
  1055. iso9660->opt.rr = OPT_RR_DEFAULT;
  1056. iso9660->opt.volume_id = OPT_VOLUME_ID_DEFAULT;
  1057. iso9660->opt.zisofs = OPT_ZISOFS_DEFAULT;
  1058. /* Create the root directory. */
  1059. iso9660->primary.rootent =
  1060. isoent_create_virtual_dir(a, iso9660, "");
  1061. if (iso9660->primary.rootent == NULL) {
  1062. free(iso9660);
  1063. archive_set_error(&a->archive, ENOMEM,
  1064. "Can't allocate memory");
  1065. return (ARCHIVE_FATAL);
  1066. }
  1067. iso9660->primary.rootent->parent = iso9660->primary.rootent;
  1068. iso9660->cur_dirent = iso9660->primary.rootent;
  1069. archive_string_init(&(iso9660->cur_dirstr));
  1070. archive_string_ensure(&(iso9660->cur_dirstr), 1);
  1071. iso9660->cur_dirstr.s[0] = 0;
  1072. iso9660->sconv_to_utf16be = NULL;
  1073. iso9660->sconv_from_utf16be = NULL;
  1074. a->format_data = iso9660;
  1075. a->format_name = "iso9660";
  1076. a->format_options = iso9660_options;
  1077. a->format_write_header = iso9660_write_header;
  1078. a->format_write_data = iso9660_write_data;
  1079. a->format_finish_entry = iso9660_finish_entry;
  1080. a->format_close = iso9660_close;
  1081. a->format_free = iso9660_free;
  1082. a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
  1083. a->archive.archive_format_name = "ISO9660";
  1084. return (ARCHIVE_OK);
  1085. }
  1086. static int
  1087. get_str_opt(struct archive_write *a, struct archive_string *s,
  1088. size_t maxsize, const char *key, const char *value)
  1089. {
  1090. if (strlen(value) > maxsize) {
  1091. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1092. "Value is longer than %zu characters "
  1093. "for option ``%s''", maxsize, key);
  1094. return (ARCHIVE_FATAL);
  1095. }
  1096. archive_strcpy(s, value);
  1097. return (ARCHIVE_OK);
  1098. }
  1099. static int
  1100. get_num_opt(struct archive_write *a, int *num, int high, int low,
  1101. const char *key, const char *value)
  1102. {
  1103. const char *p = value;
  1104. int data = 0;
  1105. int neg = 0;
  1106. if (p == NULL) {
  1107. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1108. "Invalid value(empty) for option ``%s''", key);
  1109. return (ARCHIVE_FATAL);
  1110. }
  1111. if (*p == '-') {
  1112. neg = 1;
  1113. p++;
  1114. }
  1115. while (*p) {
  1116. if (*p >= '0' && *p <= '9')
  1117. data = data * 10 + *p - '0';
  1118. else {
  1119. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1120. "Invalid value for option ``%s''", key);
  1121. return (ARCHIVE_FATAL);
  1122. }
  1123. if (data > high) {
  1124. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1125. "Invalid value(over %d) for "
  1126. "option ``%s''", high, key);
  1127. return (ARCHIVE_FATAL);
  1128. }
  1129. if (data < low) {
  1130. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1131. "Invalid value(under %d) for "
  1132. "option ``%s''", low, key);
  1133. return (ARCHIVE_FATAL);
  1134. }
  1135. p++;
  1136. }
  1137. if (neg)
  1138. data *= -1;
  1139. *num = data;
  1140. return (ARCHIVE_OK);
  1141. }
  1142. static int
  1143. iso9660_options(struct archive_write *a, const char *key, const char *value)
  1144. {
  1145. struct iso9660 *iso9660 = a->format_data;
  1146. const char *p;
  1147. int r;
  1148. switch (key[0]) {
  1149. case 'a':
  1150. if (strcmp(key, "abstract-file") == 0) {
  1151. r = get_str_opt(a,
  1152. &(iso9660->abstract_file_identifier),
  1153. ABSTRACT_FILE_SIZE, key, value);
  1154. iso9660->opt.abstract_file = r == ARCHIVE_OK;
  1155. return (r);
  1156. }
  1157. if (strcmp(key, "application-id") == 0) {
  1158. r = get_str_opt(a,
  1159. &(iso9660->application_identifier),
  1160. APPLICATION_IDENTIFIER_SIZE, key, value);
  1161. iso9660->opt.application_id = r == ARCHIVE_OK;
  1162. return (r);
  1163. }
  1164. if (strcmp(key, "allow-vernum") == 0) {
  1165. iso9660->opt.allow_vernum = value != NULL;
  1166. return (ARCHIVE_OK);
  1167. }
  1168. break;
  1169. case 'b':
  1170. if (strcmp(key, "biblio-file") == 0) {
  1171. r = get_str_opt(a,
  1172. &(iso9660->bibliographic_file_identifier),
  1173. BIBLIO_FILE_SIZE, key, value);
  1174. iso9660->opt.biblio_file = r == ARCHIVE_OK;
  1175. return (r);
  1176. }
  1177. if (strcmp(key, "boot") == 0) {
  1178. if (value == NULL)
  1179. iso9660->opt.boot = 0;
  1180. else {
  1181. iso9660->opt.boot = 1;
  1182. archive_strcpy(
  1183. &(iso9660->el_torito.boot_filename),
  1184. value);
  1185. }
  1186. return (ARCHIVE_OK);
  1187. }
  1188. if (strcmp(key, "boot-catalog") == 0) {
  1189. r = get_str_opt(a,
  1190. &(iso9660->el_torito.catalog_filename),
  1191. 1024, key, value);
  1192. iso9660->opt.boot_catalog = r == ARCHIVE_OK;
  1193. return (r);
  1194. }
  1195. if (strcmp(key, "boot-info-table") == 0) {
  1196. iso9660->opt.boot_info_table = value != NULL;
  1197. return (ARCHIVE_OK);
  1198. }
  1199. if (strcmp(key, "boot-load-seg") == 0) {
  1200. uint32_t seg;
  1201. iso9660->opt.boot_load_seg = 0;
  1202. if (value == NULL)
  1203. goto invalid_value;
  1204. seg = 0;
  1205. p = value;
  1206. if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
  1207. p += 2;
  1208. while (*p) {
  1209. if (seg)
  1210. seg <<= 4;
  1211. if (*p >= 'A' && *p <= 'F')
  1212. seg += *p - 'A' + 0x0a;
  1213. else if (*p >= 'a' && *p <= 'f')
  1214. seg += *p - 'a' + 0x0a;
  1215. else if (*p >= '0' && *p <= '9')
  1216. seg += *p - '0';
  1217. else
  1218. goto invalid_value;
  1219. if (seg > 0xffff) {
  1220. archive_set_error(&a->archive,
  1221. ARCHIVE_ERRNO_MISC,
  1222. "Invalid value(over 0xffff) for "
  1223. "option ``%s''", key);
  1224. return (ARCHIVE_FATAL);
  1225. }
  1226. p++;
  1227. }
  1228. iso9660->el_torito.boot_load_seg = (uint16_t)seg;
  1229. iso9660->opt.boot_load_seg = 1;
  1230. return (ARCHIVE_OK);
  1231. }
  1232. if (strcmp(key, "boot-load-size") == 0) {
  1233. int num = 0;
  1234. r = get_num_opt(a, &num, 0xffff, 1, key, value);
  1235. iso9660->opt.boot_load_size = r == ARCHIVE_OK;
  1236. if (r != ARCHIVE_OK)
  1237. return (ARCHIVE_FATAL);
  1238. iso9660->el_torito.boot_load_size = (uint16_t)num;
  1239. return (ARCHIVE_OK);
  1240. }
  1241. if (strcmp(key, "boot-type") == 0) {
  1242. if (value == NULL)
  1243. goto invalid_value;
  1244. if (strcmp(value, "no-emulation") == 0)
  1245. iso9660->opt.boot_type = OPT_BOOT_TYPE_NO_EMU;
  1246. else if (strcmp(value, "fd") == 0)
  1247. iso9660->opt.boot_type = OPT_BOOT_TYPE_FD;
  1248. else if (strcmp(value, "hard-disk") == 0)
  1249. iso9660->opt.boot_type = OPT_BOOT_TYPE_HARD_DISK;
  1250. else
  1251. goto invalid_value;
  1252. return (ARCHIVE_OK);
  1253. }
  1254. break;
  1255. case 'c':
  1256. if (strcmp(key, "compression-level") == 0) {
  1257. #ifdef HAVE_ZLIB_H
  1258. if (value == NULL ||
  1259. !(value[0] >= '0' && value[0] <= '9') ||
  1260. value[1] != '\0')
  1261. goto invalid_value;
  1262. iso9660->zisofs.compression_level = value[0] - '0';
  1263. iso9660->opt.compression_level = 1;
  1264. return (ARCHIVE_OK);
  1265. #else
  1266. archive_set_error(&a->archive,
  1267. ARCHIVE_ERRNO_MISC,
  1268. "Option ``%s'' "
  1269. "is not supported on this platform.", key);
  1270. return (ARCHIVE_FATAL);
  1271. #endif
  1272. }
  1273. if (strcmp(key, "copyright-file") == 0) {
  1274. r = get_str_opt(a,
  1275. &(iso9660->copyright_file_identifier),
  1276. COPYRIGHT_FILE_SIZE, key, value);
  1277. iso9660->opt.copyright_file = r == ARCHIVE_OK;
  1278. return (r);
  1279. }
  1280. #ifdef DEBUG
  1281. /* Specifies Volume creation date and time;
  1282. * year(4),month(2),day(2),hour(2),minute(2),second(2).
  1283. * e.g. "20090929033757"
  1284. */
  1285. if (strcmp(key, "creation") == 0) {
  1286. struct tm tm;
  1287. char buf[5];
  1288. p = value;
  1289. if (p == NULL || strlen(p) < 14)
  1290. goto invalid_value;
  1291. memset(&tm, 0, sizeof(tm));
  1292. memcpy(buf, p, 4); buf[4] = '\0'; p += 4;
  1293. tm.tm_year = strtol(buf, NULL, 10) - 1900;
  1294. memcpy(buf, p, 2); buf[2] = '\0'; p += 2;
  1295. tm.tm_mon = strtol(buf, NULL, 10) - 1;
  1296. memcpy(buf, p, 2); buf[2] = '\0'; p += 2;
  1297. tm.tm_mday = strtol(buf, NULL, 10);
  1298. memcpy(buf, p, 2); buf[2] = '\0'; p += 2;
  1299. tm.tm_hour = strtol(buf, NULL, 10);
  1300. memcpy(buf, p, 2); buf[2] = '\0'; p += 2;
  1301. tm.tm_min = strtol(buf, NULL, 10);
  1302. memcpy(buf, p, 2); buf[2] = '\0';
  1303. tm.tm_sec = strtol(buf, NULL, 10);
  1304. iso9660->birth_time = mktime(&tm);
  1305. return (ARCHIVE_OK);
  1306. }
  1307. #endif
  1308. break;
  1309. case 'i':
  1310. if (strcmp(key, "iso-level") == 0) {
  1311. if (value != NULL && value[1] == '\0' &&
  1312. (value[0] >= '1' && value[0] <= '4')) {
  1313. iso9660->opt.iso_level = value[0]-'0';
  1314. return (ARCHIVE_OK);
  1315. }
  1316. goto invalid_value;
  1317. }
  1318. break;
  1319. case 'j':
  1320. if (strcmp(key, "joliet") == 0) {
  1321. if (value == NULL)
  1322. iso9660->opt.joliet = OPT_JOLIET_DISABLE;
  1323. else if (strcmp(value, "1") == 0)
  1324. iso9660->opt.joliet = OPT_JOLIET_ENABLE;
  1325. else if (strcmp(value, "long") == 0)
  1326. iso9660->opt.joliet = OPT_JOLIET_LONGNAME;
  1327. else
  1328. goto invalid_value;
  1329. return (ARCHIVE_OK);
  1330. }
  1331. break;
  1332. case 'l':
  1333. if (strcmp(key, "limit-depth") == 0) {
  1334. iso9660->opt.limit_depth = value != NULL;
  1335. return (ARCHIVE_OK);
  1336. }
  1337. if (strcmp(key, "limit-dirs") == 0) {
  1338. iso9660->opt.limit_dirs = value != NULL;
  1339. return (ARCHIVE_OK);
  1340. }
  1341. break;
  1342. case 'p':
  1343. if (strcmp(key, "pad") == 0) {
  1344. iso9660->opt.pad = value != NULL;
  1345. return (ARCHIVE_OK);
  1346. }
  1347. if (strcmp(key, "publisher") == 0) {
  1348. r = get_str_opt(a,
  1349. &(iso9660->publisher_identifier),
  1350. PUBLISHER_IDENTIFIER_SIZE, key, value);
  1351. iso9660->opt.publisher = r == ARCHIVE_OK;
  1352. return (r);
  1353. }
  1354. break;
  1355. case 'r':
  1356. if (strcmp(key, "rockridge") == 0 ||
  1357. strcmp(key, "Rockridge") == 0) {
  1358. if (value == NULL)
  1359. iso9660->opt.rr = OPT_RR_DISABLED;
  1360. else if (strcmp(value, "1") == 0)
  1361. iso9660->opt.rr = OPT_RR_USEFUL;
  1362. else if (strcmp(value, "strict") == 0)
  1363. iso9660->opt.rr = OPT_RR_STRICT;
  1364. else if (strcmp(value, "useful") == 0)
  1365. iso9660->opt.rr = OPT_RR_USEFUL;
  1366. else
  1367. goto invalid_value;
  1368. return (ARCHIVE_OK);
  1369. }
  1370. break;
  1371. case 'v':
  1372. if (strcmp(key, "volume-id") == 0) {
  1373. r = get_str_opt(a, &(iso9660->volume_identifier),
  1374. VOLUME_IDENTIFIER_SIZE, key, value);
  1375. iso9660->opt.volume_id = r == ARCHIVE_OK;
  1376. return (r);
  1377. }
  1378. break;
  1379. case 'z':
  1380. if (strcmp(key, "zisofs") == 0) {
  1381. if (value == NULL)
  1382. iso9660->opt.zisofs = OPT_ZISOFS_DISABLED;
  1383. else {
  1384. #ifdef HAVE_ZLIB_H
  1385. iso9660->opt.zisofs = OPT_ZISOFS_DIRECT;
  1386. #else
  1387. archive_set_error(&a->archive,
  1388. ARCHIVE_ERRNO_MISC,
  1389. "``zisofs'' "
  1390. "is not supported on this platform.");
  1391. return (ARCHIVE_FATAL);
  1392. #endif
  1393. }
  1394. return (ARCHIVE_OK);
  1395. }
  1396. break;
  1397. }
  1398. /* Note: The "warn" return is just to inform the options
  1399. * supervisor that we didn't handle it. It will generate
  1400. * a suitable error if no one used this option. */
  1401. return (ARCHIVE_WARN);
  1402. invalid_value:
  1403. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1404. "Invalid value for option ``%s''", key);
  1405. return (ARCHIVE_FAILED);
  1406. }
  1407. static int
  1408. iso9660_write_header(struct archive_write *a, struct archive_entry *entry)
  1409. {
  1410. struct iso9660 *iso9660;
  1411. struct isofile *file;
  1412. struct isoent *isoent;
  1413. int r, ret = ARCHIVE_OK;
  1414. iso9660 = a->format_data;
  1415. iso9660->cur_file = NULL;
  1416. iso9660->bytes_remaining = 0;
  1417. iso9660->need_multi_extent = 0;
  1418. if (archive_entry_filetype(entry) == AE_IFLNK
  1419. && iso9660->opt.rr == OPT_RR_DISABLED) {
  1420. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1421. "Ignore symlink file.");
  1422. iso9660->cur_file = NULL;
  1423. return (ARCHIVE_WARN);
  1424. }
  1425. if (archive_entry_filetype(entry) == AE_IFREG &&
  1426. archive_entry_size(entry) >= MULTI_EXTENT_SIZE) {
  1427. if (iso9660->opt.iso_level < 3) {
  1428. archive_set_error(&a->archive,
  1429. ARCHIVE_ERRNO_MISC,
  1430. "Ignore over %lld bytes file. "
  1431. "This file too large.",
  1432. MULTI_EXTENT_SIZE);
  1433. iso9660->cur_file = NULL;
  1434. return (ARCHIVE_WARN);
  1435. }
  1436. iso9660->need_multi_extent = 1;
  1437. }
  1438. file = isofile_new(a, entry);
  1439. if (file == NULL) {
  1440. archive_set_error(&a->archive, ENOMEM,
  1441. "Can't allocate data");
  1442. return (ARCHIVE_FATAL);
  1443. }
  1444. r = isofile_gen_utility_names(a, file);
  1445. if (r < ARCHIVE_WARN) {
  1446. isofile_free(file);
  1447. return (r);
  1448. }
  1449. else if (r < ret)
  1450. ret = r;
  1451. /*
  1452. * Ignore a path which looks like the top of directory name
  1453. * since we have already made the root directory of an ISO image.
  1454. */
  1455. if (archive_strlen(&(file->parentdir)) == 0 &&
  1456. archive_strlen(&(file->basename)) == 0) {
  1457. isofile_free(file);
  1458. return (r);
  1459. }
  1460. isofile_add_entry(iso9660, file);
  1461. isoent = isoent_new(file);
  1462. if (isoent == NULL) {
  1463. archive_set_error(&a->archive, ENOMEM,
  1464. "Can't allocate data");
  1465. return (ARCHIVE_FATAL);
  1466. }
  1467. if (isoent->file->dircnt > iso9660->dircnt_max)
  1468. iso9660->dircnt_max = isoent->file->dircnt;
  1469. /* Add the current file into tree */
  1470. r = isoent_tree(a, &isoent);
  1471. if (r != ARCHIVE_OK)
  1472. return (r);
  1473. /* If there is the same file in tree and
  1474. * the current file is older than the file in tree.
  1475. * So we don't need the current file data anymore. */
  1476. if (isoent->file != file)
  1477. return (ARCHIVE_OK);
  1478. /* Non regular files contents are unneeded to be saved to
  1479. * temporary files. */
  1480. if (archive_entry_filetype(file->entry) != AE_IFREG)
  1481. return (ret);
  1482. /*
  1483. * Set the current file to cur_file to read its contents.
  1484. */
  1485. iso9660->cur_file = file;
  1486. if (archive_entry_nlink(file->entry) > 1) {
  1487. r = isofile_register_hardlink(a, file);
  1488. if (r != ARCHIVE_OK)
  1489. return (ARCHIVE_FATAL);
  1490. }
  1491. /*
  1492. * Prepare to save the contents of the file.
  1493. */
  1494. if (iso9660->temp_fd < 0) {
  1495. iso9660->temp_fd = __archive_mktemp(NULL);
  1496. if (iso9660->temp_fd < 0) {
  1497. archive_set_error(&a->archive, errno,
  1498. "Couldn't create temporary file");
  1499. return (ARCHIVE_FATAL);
  1500. }
  1501. }
  1502. /* Save an offset of current file in temporary file. */
  1503. file->content.offset_of_temp = wb_offset(a);
  1504. file->cur_content = &(file->content);
  1505. r = zisofs_init(a, file);
  1506. if (r < ret)
  1507. ret = r;
  1508. iso9660->bytes_remaining = archive_entry_size(file->entry);
  1509. return (ret);
  1510. }
  1511. static int
  1512. write_to_temp(struct archive_write *a, const void *buff, size_t s)
  1513. {
  1514. struct iso9660 *iso9660 = a->format_data;
  1515. ssize_t written;
  1516. const unsigned char *b;
  1517. b = (const unsigned char *)buff;
  1518. while (s) {
  1519. written = write(iso9660->temp_fd, b, s);
  1520. if (written < 0) {
  1521. archive_set_error(&a->archive, errno,
  1522. "Can't write to temporary file");
  1523. return (ARCHIVE_FATAL);
  1524. }
  1525. s -= written;
  1526. b += written;
  1527. }
  1528. return (ARCHIVE_OK);
  1529. }
  1530. static int
  1531. wb_write_to_temp(struct archive_write *a, const void *buff, size_t s)
  1532. {
  1533. const char *xp = buff;
  1534. size_t xs = s;
  1535. /*
  1536. * If a written data size is big enough to use system-call
  1537. * and there is no waiting data, this calls write_to_temp() in
  1538. * order to reduce a extra memory copy.
  1539. */
  1540. if (wb_remaining(a) == wb_buffmax() && s > (1024 * 16)) {
  1541. struct iso9660 *iso9660 = (struct iso9660 *)a->format_data;
  1542. xs = s % LOGICAL_BLOCK_SIZE;
  1543. iso9660->wbuff_offset += s - xs;
  1544. if (write_to_temp(a, buff, s - xs) != ARCHIVE_OK)
  1545. return (ARCHIVE_FATAL);
  1546. if (xs == 0)
  1547. return (ARCHIVE_OK);
  1548. xp += s - xs;
  1549. }
  1550. while (xs) {
  1551. size_t size = xs;
  1552. if (size > wb_remaining(a))
  1553. size = wb_remaining(a);
  1554. memcpy(wb_buffptr(a), xp, size);
  1555. if (wb_consume(a, size) != ARCHIVE_OK)
  1556. return (ARCHIVE_FATAL);
  1557. xs -= size;
  1558. xp += size;
  1559. }
  1560. return (ARCHIVE_OK);
  1561. }
  1562. static int
  1563. wb_write_padding_to_temp(struct archive_write *a, int64_t csize)
  1564. {
  1565. size_t ns;
  1566. int ret;
  1567. ns = (size_t)(csize % LOGICAL_BLOCK_SIZE);
  1568. if (ns != 0)
  1569. ret = write_null(a, LOGICAL_BLOCK_SIZE - ns);
  1570. else
  1571. ret = ARCHIVE_OK;
  1572. return (ret);
  1573. }
  1574. static ssize_t
  1575. write_iso9660_data(struct archive_write *a, const void *buff, size_t s)
  1576. {
  1577. struct iso9660 *iso9660 = a->format_data;
  1578. size_t ws;
  1579. if (iso9660->temp_fd < 0) {
  1580. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1581. "Couldn't create temporary file");
  1582. return (ARCHIVE_FATAL);
  1583. }
  1584. ws = s;
  1585. if (iso9660->need_multi_extent &&
  1586. (iso9660->cur_file->cur_content->size + ws) >=
  1587. (MULTI_EXTENT_SIZE - LOGICAL_BLOCK_SIZE)) {
  1588. struct content *con;
  1589. size_t ts;
  1590. ts = (size_t)(MULTI_EXTENT_SIZE - LOGICAL_BLOCK_SIZE -
  1591. iso9660->cur_file->cur_content->size);
  1592. if (iso9660->zisofs.detect_magic)
  1593. zisofs_detect_magic(a, buff, ts);
  1594. if (iso9660->zisofs.making) {
  1595. if (zisofs_write_to_temp(a, buff, ts) != ARCHIVE_OK)
  1596. return (ARCHIVE_FATAL);
  1597. } else {
  1598. if (wb_write_to_temp(a, buff, ts) != ARCHIVE_OK)
  1599. return (ARCHIVE_FATAL);
  1600. iso9660->cur_file->cur_content->size += ts;
  1601. }
  1602. /* Write padding. */
  1603. if (wb_write_padding_to_temp(a,
  1604. iso9660->cur_file->cur_content->size) != ARCHIVE_OK)
  1605. return (ARCHIVE_FATAL);
  1606. /* Compute the logical block number. */
  1607. iso9660->cur_file->cur_content->blocks = (int)
  1608. ((iso9660->cur_file->cur_content->size
  1609. + LOGICAL_BLOCK_SIZE -1) >> LOGICAL_BLOCK_BITS);
  1610. /*
  1611. * Make next extent.
  1612. */
  1613. ws -= ts;
  1614. buff = (const void *)(((const unsigned char *)buff) + ts);
  1615. /* Make a content for next extent. */
  1616. con = calloc(1, sizeof(*con));
  1617. if (con == NULL) {
  1618. archive_set_error(&a->archive, ENOMEM,
  1619. "Can't allocate content data");
  1620. return (ARCHIVE_FATAL);
  1621. }
  1622. con->offset_of_temp = wb_offset(a);
  1623. iso9660->cur_file->cur_content->next = con;
  1624. iso9660->cur_file->cur_content = con;
  1625. #ifdef HAVE_ZLIB_H
  1626. iso9660->zisofs.block_offset = 0;
  1627. #endif
  1628. }
  1629. if (iso9660->zisofs.detect_magic)
  1630. zisofs_detect_magic(a, buff, ws);
  1631. if (iso9660->zisofs.making) {
  1632. if (zisofs_write_to_temp(a, buff, ws) != ARCHIVE_OK)
  1633. return (ARCHIVE_FATAL);
  1634. } else {
  1635. if (wb_write_to_temp(a, buff, ws) != ARCHIVE_OK)
  1636. return (ARCHIVE_FATAL);
  1637. iso9660->cur_file->cur_content->size += ws;
  1638. }
  1639. return (s);
  1640. }
  1641. static ssize_t
  1642. iso9660_write_data(struct archive_write *a, const void *buff, size_t s)
  1643. {
  1644. struct iso9660 *iso9660 = a->format_data;
  1645. ssize_t r;
  1646. if (iso9660->cur_file == NULL)
  1647. return (0);
  1648. if (archive_entry_filetype(iso9660->cur_file->entry) != AE_IFREG)
  1649. return (0);
  1650. if (s > iso9660->bytes_remaining)
  1651. s = (size_t)iso9660->bytes_remaining;
  1652. if (s == 0)
  1653. return (0);
  1654. r = write_iso9660_data(a, buff, s);
  1655. if (r > 0)
  1656. iso9660->bytes_remaining -= r;
  1657. return (r);
  1658. }
  1659. static int
  1660. iso9660_finish_entry(struct archive_write *a)
  1661. {
  1662. struct iso9660 *iso9660 = a->format_data;
  1663. if (iso9660->cur_file == NULL)
  1664. return (ARCHIVE_OK);
  1665. if (archive_entry_filetype(iso9660->cur_file->entry) != AE_IFREG)
  1666. return (ARCHIVE_OK);
  1667. if (iso9660->cur_file->content.size == 0)
  1668. return (ARCHIVE_OK);
  1669. /* If there are unwritten data, write null data instead. */
  1670. while (iso9660->bytes_remaining > 0) {
  1671. size_t s;
  1672. s = (iso9660->bytes_remaining > a->null_length)?
  1673. a->null_length: (size_t)iso9660->bytes_remaining;
  1674. if (write_iso9660_data(a, a->nulls, s) < 0)
  1675. return (ARCHIVE_FATAL);
  1676. iso9660->bytes_remaining -= s;
  1677. }
  1678. if (iso9660->zisofs.making && zisofs_finish_entry(a) != ARCHIVE_OK)
  1679. return (ARCHIVE_FATAL);
  1680. /* Write padding. */
  1681. if (wb_write_padding_to_temp(a, iso9660->cur_file->cur_content->size)
  1682. != ARCHIVE_OK)
  1683. return (ARCHIVE_FATAL);
  1684. /* Compute the logical block number. */
  1685. iso9660->cur_file->cur_content->blocks = (int)
  1686. ((iso9660->cur_file->cur_content->size
  1687. + LOGICAL_BLOCK_SIZE -1) >> LOGICAL_BLOCK_BITS);
  1688. /* Add the current file to data file list. */
  1689. isofile_add_data_file(iso9660, iso9660->cur_file);
  1690. return (ARCHIVE_OK);
  1691. }
  1692. static int
  1693. iso9660_close(struct archive_write *a)
  1694. {
  1695. struct iso9660 *iso9660;
  1696. int ret, blocks;
  1697. iso9660 = a->format_data;
  1698. /*
  1699. * Write remaining data out to the temporary file.
  1700. */
  1701. if (wb_remaining(a) > 0) {
  1702. ret = wb_write_out(a);
  1703. if (ret < 0)
  1704. return (ret);
  1705. }
  1706. /*
  1707. * Preparations...
  1708. */
  1709. #ifdef DEBUG
  1710. if (iso9660->birth_time == 0)
  1711. #endif
  1712. time(&(iso9660->birth_time));
  1713. /*
  1714. * Prepare a bootable ISO image.
  1715. */
  1716. if (iso9660->opt.boot) {
  1717. /* Find out the boot file entry. */
  1718. ret = isoent_find_out_boot_file(a, iso9660->primary.rootent);
  1719. if (ret < 0)
  1720. return (ret);
  1721. /* Reconvert the boot file from zisofs'ed form to
  1722. * plain form. */
  1723. ret = zisofs_rewind_boot_file(a);
  1724. if (ret < 0)
  1725. return (ret);
  1726. /* Write remaining data out to the temporary file. */
  1727. if (wb_remaining(a) > 0) {
  1728. ret = wb_write_out(a);
  1729. if (ret < 0)
  1730. return (ret);
  1731. }
  1732. /* Create the boot catalog. */
  1733. ret = isoent_create_boot_catalog(a, iso9660->primary.rootent);
  1734. if (ret < 0)
  1735. return (ret);
  1736. }
  1737. /*
  1738. * Prepare joliet extensions.
  1739. */
  1740. if (iso9660->opt.joliet) {
  1741. /* Make a new tree for joliet. */
  1742. ret = isoent_clone_tree(a, &(iso9660->joliet.rootent),
  1743. iso9660->primary.rootent);
  1744. if (ret < 0)
  1745. return (ret);
  1746. /* Make sure we have UTF-16BE converters.
  1747. * if there is no file entry, converters are still
  1748. * uninitialized. */
  1749. if (iso9660->sconv_to_utf16be == NULL) {
  1750. iso9660->sconv_to_utf16be =
  1751. archive_string_conversion_to_charset(
  1752. &(a->archive), "UTF-16BE", 1);
  1753. if (iso9660->sconv_to_utf16be == NULL)
  1754. /* Couldn't allocate memory */
  1755. return (ARCHIVE_FATAL);
  1756. iso9660->sconv_from_utf16be =
  1757. archive_string_conversion_from_charset(
  1758. &(a->archive), "UTF-16BE", 1);
  1759. if (iso9660->sconv_from_utf16be == NULL)
  1760. /* Couldn't allocate memory */
  1761. return (ARCHIVE_FATAL);
  1762. }
  1763. }
  1764. /*
  1765. * Make Path Tables.
  1766. */
  1767. ret = isoent_make_path_table(a);
  1768. if (ret < 0)
  1769. return (ret);
  1770. /*
  1771. * Calculate a total volume size and setup all locations of
  1772. * contents of an iso9660 image.
  1773. */
  1774. blocks = SYSTEM_AREA_BLOCK
  1775. + PRIMARY_VOLUME_DESCRIPTOR_BLOCK
  1776. + VOLUME_DESCRIPTOR_SET_TERMINATOR_BLOCK
  1777. + NON_ISO_FILE_SYSTEM_INFORMATION_BLOCK;
  1778. if (iso9660->opt.boot)
  1779. blocks += BOOT_RECORD_DESCRIPTOR_BLOCK;
  1780. if (iso9660->opt.joliet)
  1781. blocks += SUPPLEMENTARY_VOLUME_DESCRIPTOR_BLOCK;
  1782. if (iso9660->opt.iso_level == 4)
  1783. blocks += SUPPLEMENTARY_VOLUME_DESCRIPTOR_BLOCK;
  1784. /* Setup the locations of Path Table. */
  1785. iso9660->primary.location_type_L_path_table = blocks;
  1786. blocks += iso9660->primary.path_table_block;
  1787. iso9660->primary.location_type_M_path_table = blocks;
  1788. blocks += iso9660->primary.path_table_block;
  1789. if (iso9660->opt.joliet) {
  1790. iso9660->joliet.location_type_L_path_table = blocks;
  1791. blocks += iso9660->joliet.path_table_block;
  1792. iso9660->joliet.location_type_M_path_table = blocks;
  1793. blocks += iso9660->joliet.path_table_block;
  1794. }
  1795. /* Setup the locations of directories. */
  1796. isoent_setup_directory_location(iso9660, blocks,
  1797. &(iso9660->primary));
  1798. blocks += iso9660->primary.total_dir_block;
  1799. if (iso9660->opt.joliet) {
  1800. isoent_setup_directory_location(iso9660, blocks,
  1801. &(iso9660->joliet));
  1802. blocks += iso9660->joliet.total_dir_block;
  1803. }
  1804. if (iso9660->opt.rr) {
  1805. iso9660->location_rrip_er = blocks;
  1806. blocks += RRIP_ER_BLOCK;
  1807. }
  1808. /* Setup the locations of all file contents. */
  1809. isoent_setup_file_location(iso9660, blocks);
  1810. blocks += iso9660->total_file_block;
  1811. if (iso9660->opt.boot && iso9660->opt.boot_info_table) {
  1812. ret = setup_boot_information(a);
  1813. if (ret < 0)
  1814. return (ret);
  1815. }
  1816. /* Now we have a total volume size. */
  1817. iso9660->volume_space_size = blocks;
  1818. if (iso9660->opt.pad)
  1819. iso9660->volume_space_size += PADDING_BLOCK;
  1820. iso9660->volume_sequence_number = 1;
  1821. /*
  1822. * Write an ISO 9660 image.
  1823. */
  1824. /* Switch to start using wbuff as file buffer. */
  1825. iso9660->wbuff_remaining = wb_buffmax();
  1826. iso9660->wbuff_type = WB_TO_STREAM;
  1827. iso9660->wbuff_offset = 0;
  1828. iso9660->wbuff_written = 0;
  1829. iso9660->wbuff_tail = 0;
  1830. /* Write The System Area */
  1831. ret = write_null(a, SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE);
  1832. if (ret != ARCHIVE_OK)
  1833. return (ARCHIVE_FATAL);
  1834. /* Write Primary Volume Descriptor */
  1835. ret = write_VD(a, &(iso9660->primary));
  1836. if (ret != ARCHIVE_OK)
  1837. return (ARCHIVE_FATAL);
  1838. if (iso9660->opt.boot) {
  1839. /* Write Boot Record Volume Descriptor */
  1840. ret = write_VD_boot_record(a);
  1841. if (ret != ARCHIVE_OK)
  1842. return (ARCHIVE_FATAL);
  1843. }
  1844. if (iso9660->opt.iso_level == 4) {
  1845. /* Write Enhanced Volume Descriptor */
  1846. iso9660->primary.vdd_type = VDD_ENHANCED;
  1847. ret = write_VD(a, &(iso9660->primary));
  1848. iso9660->primary.vdd_type = VDD_PRIMARY;
  1849. if (ret != ARCHIVE_OK)
  1850. return (ARCHIVE_FATAL);
  1851. }
  1852. if (iso9660->opt.joliet) {
  1853. ret = write_VD(a, &(iso9660->joliet));
  1854. if (ret != ARCHIVE_OK)
  1855. return (ARCHIVE_FATAL);
  1856. }
  1857. /* Write Volume Descriptor Set Terminator */
  1858. ret = write_VD_terminator(a);
  1859. if (ret != ARCHIVE_OK)
  1860. return (ARCHIVE_FATAL);
  1861. /* Write Non-ISO File System Information */
  1862. ret = write_information_block(a);
  1863. if (ret != ARCHIVE_OK)
  1864. return (ARCHIVE_FATAL);
  1865. /* Write Type L Path Table */
  1866. ret = write_path_table(a, 0, &(iso9660->primary));
  1867. if (ret != ARCHIVE_OK)
  1868. return (ARCHIVE_FATAL);
  1869. /* Write Type M Path Table */
  1870. ret = write_path_table(a, 1, &(iso9660->primary));
  1871. if (ret != ARCHIVE_OK)
  1872. return (ARCHIVE_FATAL);
  1873. if (iso9660->opt.joliet) {
  1874. /* Write Type L Path Table */
  1875. ret = write_path_table(a, 0, &(iso9660->joliet));
  1876. if (ret != ARCHIVE_OK)
  1877. return (ARCHIVE_FATAL);
  1878. /* Write Type M Path Table */
  1879. ret = write_path_table(a, 1, &(iso9660->joliet));
  1880. if (ret != ARCHIVE_OK)
  1881. return (ARCHIVE_FATAL);
  1882. }
  1883. /* Write Directory Descriptors */
  1884. ret = write_directory_descriptors(a, &(iso9660->primary));
  1885. if (ret != ARCHIVE_OK)
  1886. return (ARCHIVE_FATAL);
  1887. if (iso9660->opt.joliet) {
  1888. ret = write_directory_descriptors(a, &(iso9660->joliet));
  1889. if (ret != ARCHIVE_OK)
  1890. return (ARCHIVE_FATAL);
  1891. }
  1892. if (iso9660->opt.rr) {
  1893. /* Write Rockridge ER(Extensions Reference) */
  1894. ret = write_rr_ER(a);
  1895. if (ret != ARCHIVE_OK)
  1896. return (ARCHIVE_FATAL);
  1897. }
  1898. /* Write File Descriptors */
  1899. ret = write_file_descriptors(a);
  1900. if (ret != ARCHIVE_OK)
  1901. return (ARCHIVE_FATAL);
  1902. /* Write Padding */
  1903. if (iso9660->opt.pad) {
  1904. ret = write_null(a, PADDING_BLOCK * LOGICAL_BLOCK_SIZE);
  1905. if (ret != ARCHIVE_OK)
  1906. return (ARCHIVE_FATAL);
  1907. }
  1908. if (iso9660->directories_too_deep != NULL) {
  1909. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1910. "%s: Directories too deep.",
  1911. archive_entry_pathname(
  1912. iso9660->directories_too_deep->file->entry));
  1913. return (ARCHIVE_WARN);
  1914. }
  1915. /* Write remaining data out. */
  1916. ret = wb_write_out(a);
  1917. return (ret);
  1918. }
  1919. static int
  1920. iso9660_free(struct archive_write *a)
  1921. {
  1922. struct iso9660 *iso9660;
  1923. int i, ret;
  1924. iso9660 = a->format_data;
  1925. /* Close the temporary file. */
  1926. if (iso9660->temp_fd >= 0)
  1927. close(iso9660->temp_fd);
  1928. /* Free some stuff for zisofs operations. */
  1929. ret = zisofs_free(a);
  1930. /* Remove directory entries in tree which includes file entries. */
  1931. isoent_free_all(iso9660->primary.rootent);
  1932. for (i = 0; i < iso9660->primary.max_depth; i++)
  1933. free(iso9660->primary.pathtbl[i].sorted);
  1934. free(iso9660->primary.pathtbl);
  1935. if (iso9660->opt.joliet) {
  1936. isoent_free_all(iso9660->joliet.rootent);
  1937. for (i = 0; i < iso9660->joliet.max_depth; i++)
  1938. free(iso9660->joliet.pathtbl[i].sorted);
  1939. free(iso9660->joliet.pathtbl);
  1940. }
  1941. /* Remove isofile entries. */
  1942. isofile_free_all_entries(iso9660);
  1943. isofile_free_hardlinks(iso9660);
  1944. archive_string_free(&(iso9660->cur_dirstr));
  1945. archive_string_free(&(iso9660->volume_identifier));
  1946. archive_string_free(&(iso9660->publisher_identifier));
  1947. archive_string_free(&(iso9660->data_preparer_identifier));
  1948. archive_string_free(&(iso9660->application_identifier));
  1949. archive_string_free(&(iso9660->copyright_file_identifier));
  1950. archive_string_free(&(iso9660->abstract_file_identifier));
  1951. archive_string_free(&(iso9660->bibliographic_file_identifier));
  1952. archive_string_free(&(iso9660->el_torito.catalog_filename));
  1953. archive_string_free(&(iso9660->el_torito.boot_filename));
  1954. archive_string_free(&(iso9660->el_torito.id));
  1955. archive_string_free(&(iso9660->utf16be));
  1956. archive_string_free(&(iso9660->mbs));
  1957. free(iso9660);
  1958. a->format_data = NULL;
  1959. return (ret);
  1960. }
  1961. /*
  1962. * Get the System Identifier
  1963. */
  1964. static void
  1965. get_system_identitier(char *system_id, size_t size)
  1966. {
  1967. #if defined(HAVE_SYS_UTSNAME_H)
  1968. struct utsname u;
  1969. uname(&u);
  1970. strncpy(system_id, u.sysname, size-1);
  1971. system_id[size-1] = '\0';
  1972. #elif defined(_WIN32) && !defined(__CYGWIN__)
  1973. strncpy(system_id, "Windows", size-1);
  1974. system_id[size-1] = '\0';
  1975. #else
  1976. #error no way to get the system identifier on your platform.
  1977. #endif
  1978. }
  1979. static void
  1980. set_str(unsigned char *p, const char *s, size_t l, char f, const char *map)
  1981. {
  1982. unsigned char c;
  1983. if (s == NULL)
  1984. s = "";
  1985. while ((c = *s++) != 0 && l > 0) {
  1986. if (c >= 0x80 || map[c] == 0)
  1987. {
  1988. /* illegal character */
  1989. if (c >= 'a' && c <= 'z') {
  1990. /* convert c from a-z to A-Z */
  1991. c -= 0x20;
  1992. } else
  1993. c = 0x5f;
  1994. }
  1995. *p++ = c;
  1996. l--;
  1997. }
  1998. /* If l isn't zero, fill p buffer by the character
  1999. * which indicated by f. */
  2000. if (l > 0)
  2001. memset(p , f, l);
  2002. }
  2003. static inline int
  2004. joliet_allowed_char(unsigned char high, unsigned char low)
  2005. {
  2006. int utf16 = (high << 8) | low;
  2007. if (utf16 <= 0x001F)
  2008. return (0);
  2009. switch (utf16) {
  2010. case 0x002A: /* '*' */
  2011. case 0x002F: /* '/' */
  2012. case 0x003A: /* ':' */
  2013. case 0x003B: /* ';' */
  2014. case 0x003F: /* '?' */
  2015. case 0x005C: /* '\' */
  2016. return (0);/* Not allowed. */
  2017. }
  2018. return (1);
  2019. }
  2020. static int
  2021. set_str_utf16be(struct archive_write *a, unsigned char *p, const char *s,
  2022. size_t l, uint16_t uf, enum vdc vdc)
  2023. {
  2024. size_t size, i;
  2025. int onepad;
  2026. if (s == NULL)
  2027. s = "";
  2028. if (l & 0x01) {
  2029. onepad = 1;
  2030. l &= ~1;
  2031. } else
  2032. onepad = 0;
  2033. if (vdc == VDC_UCS2) {
  2034. struct iso9660 *iso9660 = a->format_data;
  2035. if (archive_strncpy_l(&iso9660->utf16be, s, strlen(s),
  2036. iso9660->sconv_to_utf16be) != 0 && errno == ENOMEM) {
  2037. archive_set_error(&a->archive, ENOMEM,
  2038. "Can't allocate memory for UTF-16BE");
  2039. return (ARCHIVE_FATAL);
  2040. }
  2041. size = iso9660->utf16be.length;
  2042. if (size > l)
  2043. size = l;
  2044. memcpy(p, iso9660->utf16be.s, size);
  2045. } else {
  2046. const uint16_t *u16 = (const uint16_t *)s;
  2047. size = 0;
  2048. while (*u16++)
  2049. size += 2;
  2050. if (size > l)
  2051. size = l;
  2052. memcpy(p, s, size);
  2053. }
  2054. for (i = 0; i < size; i += 2, p += 2) {
  2055. if (!joliet_allowed_char(p[0], p[1]))
  2056. archive_be16enc(p, 0x005F);/* '_' */
  2057. }
  2058. l -= size;
  2059. while (l > 0) {
  2060. archive_be16enc(p, uf);
  2061. p += 2;
  2062. l -= 2;
  2063. }
  2064. if (onepad)
  2065. *p = 0;
  2066. return (ARCHIVE_OK);
  2067. }
  2068. static const char a_characters_map[0x80] = {
  2069. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  2070. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 00-0F */
  2071. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 10-1F */
  2072. 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20-2F */
  2073. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30-3F */
  2074. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40-4F */
  2075. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,/* 50-5F */
  2076. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 60-6F */
  2077. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 70-7F */
  2078. };
  2079. static const char a1_characters_map[0x80] = {
  2080. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  2081. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 00-0F */
  2082. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 10-1F */
  2083. 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20-2F */
  2084. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30-3F */
  2085. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40-4F */
  2086. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,/* 50-5F */
  2087. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60-6F */
  2088. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,/* 70-7F */
  2089. };
  2090. static const char d_characters_map[0x80] = {
  2091. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  2092. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 00-0F */
  2093. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 10-1F */
  2094. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 20-2F */
  2095. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,/* 30-3F */
  2096. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40-4F */
  2097. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,/* 50-5F */
  2098. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 60-6F */
  2099. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 70-7F */
  2100. };
  2101. static const char d1_characters_map[0x80] = {
  2102. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  2103. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 00-0F */
  2104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 10-1F */
  2105. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 20-2F */
  2106. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,/* 30-3F */
  2107. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40-4F */
  2108. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,/* 50-5F */
  2109. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60-6F */
  2110. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,/* 70-7F */
  2111. };
  2112. static int
  2113. set_str_a_characters_bp(struct archive_write *a, unsigned char *bp,
  2114. int from, int to, const char *s, enum vdc vdc)
  2115. {
  2116. int r;
  2117. switch (vdc) {
  2118. case VDC_STD:
  2119. set_str(bp+from, s, to - from + 1, 0x20,
  2120. a_characters_map);
  2121. r = ARCHIVE_OK;
  2122. break;
  2123. case VDC_LOWERCASE:
  2124. set_str(bp+from, s, to - from + 1, 0x20,
  2125. a1_characters_map);
  2126. r = ARCHIVE_OK;
  2127. break;
  2128. case VDC_UCS2:
  2129. case VDC_UCS2_DIRECT:
  2130. r = set_str_utf16be(a, bp+from, s, to - from + 1,
  2131. 0x0020, vdc);
  2132. break;
  2133. default:
  2134. r = ARCHIVE_FATAL;
  2135. }
  2136. return (r);
  2137. }
  2138. static int
  2139. set_str_d_characters_bp(struct archive_write *a, unsigned char *bp,
  2140. int from, int to, const char *s, enum vdc vdc)
  2141. {
  2142. int r;
  2143. switch (vdc) {
  2144. case VDC_STD:
  2145. set_str(bp+from, s, to - from + 1, 0x20,
  2146. d_characters_map);
  2147. r = ARCHIVE_OK;
  2148. break;
  2149. case VDC_LOWERCASE:
  2150. set_str(bp+from, s, to - from + 1, 0x20,
  2151. d1_characters_map);
  2152. r = ARCHIVE_OK;
  2153. break;
  2154. case VDC_UCS2:
  2155. case VDC_UCS2_DIRECT:
  2156. r = set_str_utf16be(a, bp+from, s, to - from + 1,
  2157. 0x0020, vdc);
  2158. break;
  2159. default:
  2160. r = ARCHIVE_FATAL;
  2161. }
  2162. return (r);
  2163. }
  2164. static void
  2165. set_VD_bp(unsigned char *bp, enum VD_type type, unsigned char ver)
  2166. {
  2167. /* Volume Descriptor Type */
  2168. bp[1] = (unsigned char)type;
  2169. /* Standard Identifier */
  2170. memcpy(bp + 2, "CD001", 5);
  2171. /* Volume Descriptor Version */
  2172. bp[7] = ver;
  2173. }
  2174. static inline void
  2175. set_unused_field_bp(unsigned char *bp, int from, int to)
  2176. {
  2177. memset(bp + from, 0, to - from + 1);
  2178. }
  2179. /*
  2180. * 8-bit unsigned numerical values.
  2181. * ISO9660 Standard 7.1.1
  2182. */
  2183. static inline void
  2184. set_num_711(unsigned char *p, unsigned char value)
  2185. {
  2186. *p = value;
  2187. }
  2188. /*
  2189. * 8-bit signed numerical values.
  2190. * ISO9660 Standard 7.1.2
  2191. */
  2192. static inline void
  2193. set_num_712(unsigned char *p, char value)
  2194. {
  2195. *((char *)p) = value;
  2196. }
  2197. /*
  2198. * Least significant byte first.
  2199. * ISO9660 Standard 7.2.1
  2200. */
  2201. static inline void
  2202. set_num_721(unsigned char *p, uint16_t value)
  2203. {
  2204. archive_le16enc(p, value);
  2205. }
  2206. /*
  2207. * Most significant byte first.
  2208. * ISO9660 Standard 7.2.2
  2209. */
  2210. static inline void
  2211. set_num_722(unsigned char *p, uint16_t value)
  2212. {
  2213. archive_be16enc(p, value);
  2214. }
  2215. /*
  2216. * Both-byte orders.
  2217. * ISO9660 Standard 7.2.3
  2218. */
  2219. static void
  2220. set_num_723(unsigned char *p, uint16_t value)
  2221. {
  2222. archive_le16enc(p, value);
  2223. archive_be16enc(p+2, value);
  2224. }
  2225. /*
  2226. * Least significant byte first.
  2227. * ISO9660 Standard 7.3.1
  2228. */
  2229. static inline void
  2230. set_num_731(unsigned char *p, uint32_t value)
  2231. {
  2232. archive_le32enc(p, value);
  2233. }
  2234. /*
  2235. * Most significant byte first.
  2236. * ISO9660 Standard 7.3.2
  2237. */
  2238. static inline void
  2239. set_num_732(unsigned char *p, uint32_t value)
  2240. {
  2241. archive_be32enc(p, value);
  2242. }
  2243. /*
  2244. * Both-byte orders.
  2245. * ISO9660 Standard 7.3.3
  2246. */
  2247. static inline void
  2248. set_num_733(unsigned char *p, uint32_t value)
  2249. {
  2250. archive_le32enc(p, value);
  2251. archive_be32enc(p+4, value);
  2252. }
  2253. static void
  2254. set_digit(unsigned char *p, size_t s, int value)
  2255. {
  2256. while (s--) {
  2257. p[s] = '0' + (value % 10);
  2258. value /= 10;
  2259. }
  2260. }
  2261. #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
  2262. #define get_gmoffset(tm) ((tm)->tm_gmtoff)
  2263. #elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
  2264. #define get_gmoffset(tm) ((tm)->__tm_gmtoff)
  2265. #else
  2266. static long
  2267. get_gmoffset(struct tm *tm)
  2268. {
  2269. long offset;
  2270. #if defined(HAVE__GET_TIMEZONE)
  2271. _get_timezone(&offset);
  2272. #elif defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  2273. offset = _timezone;
  2274. #else
  2275. offset = timezone;
  2276. #endif
  2277. offset *= -1;
  2278. if (tm->tm_isdst)
  2279. offset += 3600;
  2280. return (offset);
  2281. }
  2282. #endif
  2283. static void
  2284. get_tmfromtime(struct tm *tm, time_t *t)
  2285. {
  2286. #if HAVE_LOCALTIME_R
  2287. tzset();
  2288. localtime_r(t, tm);
  2289. #elif HAVE__LOCALTIME64_S
  2290. __time64_t tmp_t = (__time64_t) *t; //time_t may be shorter than 64 bits
  2291. _localtime64_s(tm, &tmp_t);
  2292. #else
  2293. memcpy(tm, localtime(t), sizeof(*tm));
  2294. #endif
  2295. }
  2296. /*
  2297. * Date and Time Format.
  2298. * ISO9660 Standard 8.4.26.1
  2299. */
  2300. static void
  2301. set_date_time(unsigned char *p, time_t t)
  2302. {
  2303. struct tm tm;
  2304. get_tmfromtime(&tm, &t);
  2305. set_digit(p, 4, tm.tm_year + 1900);
  2306. set_digit(p+4, 2, tm.tm_mon + 1);
  2307. set_digit(p+6, 2, tm.tm_mday);
  2308. set_digit(p+8, 2, tm.tm_hour);
  2309. set_digit(p+10, 2, tm.tm_min);
  2310. set_digit(p+12, 2, tm.tm_sec);
  2311. set_digit(p+14, 2, 0);
  2312. set_num_712(p+16, (char)(get_gmoffset(&tm)/(60*15)));
  2313. }
  2314. static void
  2315. set_date_time_null(unsigned char *p)
  2316. {
  2317. memset(p, (int)'0', 16);
  2318. p[16] = 0;
  2319. }
  2320. static void
  2321. set_time_915(unsigned char *p, time_t t)
  2322. {
  2323. struct tm tm;
  2324. get_tmfromtime(&tm, &t);
  2325. set_num_711(p+0, tm.tm_year);
  2326. set_num_711(p+1, tm.tm_mon+1);
  2327. set_num_711(p+2, tm.tm_mday);
  2328. set_num_711(p+3, tm.tm_hour);
  2329. set_num_711(p+4, tm.tm_min);
  2330. set_num_711(p+5, tm.tm_sec);
  2331. set_num_712(p+6, (char)(get_gmoffset(&tm)/(60*15)));
  2332. }
  2333. /*
  2334. * Write SUSP "CE" System Use Entry.
  2335. */
  2336. static int
  2337. set_SUSP_CE(unsigned char *p, int location, int offset, int size)
  2338. {
  2339. unsigned char *bp = p -1;
  2340. /* Extend the System Use Area
  2341. * "CE" Format:
  2342. * len ver
  2343. * +----+----+----+----+-----------+-----------+
  2344. * | 'C'| 'E'| 1C | 01 | LOCATION1 | LOCATION2 |
  2345. * +----+----+----+----+-----------+-----------+
  2346. * 0 1 2 3 4 12 20
  2347. * +-----------+
  2348. * | LOCATION3 |
  2349. * +-----------+
  2350. * 20 28
  2351. * LOCATION1 : Location of Continuation of System Use Area.
  2352. * LOCATION2 : Offset to Start of Continuation.
  2353. * LOCATION3 : Length of the Continuation.
  2354. */
  2355. bp[1] = 'C';
  2356. bp[2] = 'E';
  2357. bp[3] = RR_CE_SIZE; /* length */
  2358. bp[4] = 1; /* version */
  2359. set_num_733(bp+5, location);
  2360. set_num_733(bp+13, offset);
  2361. set_num_733(bp+21, size);
  2362. return (RR_CE_SIZE);
  2363. }
  2364. /*
  2365. * The functions, which names are beginning with extra_, are used to
  2366. * control extra records.
  2367. * The maximum size of a Directory Record is 254. When a filename is
  2368. * very long, all of RRIP data of a file won't stored to the Directory
  2369. * Record and so remaining RRIP data store to an extra record instead.
  2370. */
  2371. static unsigned char *
  2372. extra_open_record(unsigned char *bp, int dr_len, struct isoent *isoent,
  2373. struct ctl_extr_rec *ctl)
  2374. {
  2375. ctl->bp = bp;
  2376. if (bp != NULL)
  2377. bp += dr_len;
  2378. ctl->use_extr = 0;
  2379. ctl->isoent = isoent;
  2380. ctl->ce_ptr = NULL;
  2381. ctl->cur_len = ctl->dr_len = dr_len;
  2382. ctl->limit = DR_LIMIT;
  2383. return (bp);
  2384. }
  2385. static void
  2386. extra_close_record(struct ctl_extr_rec *ctl, int ce_size)
  2387. {
  2388. int padding = 0;
  2389. if (ce_size > 0)
  2390. extra_tell_used_size(ctl, ce_size);
  2391. /* Padding. */
  2392. if (ctl->cur_len & 0x01) {
  2393. ctl->cur_len++;
  2394. if (ctl->bp != NULL)
  2395. ctl->bp[ctl->cur_len] = 0;
  2396. padding = 1;
  2397. }
  2398. if (ctl->use_extr) {
  2399. if (ctl->ce_ptr != NULL)
  2400. set_SUSP_CE(ctl->ce_ptr, ctl->extr_loc,
  2401. ctl->extr_off, ctl->cur_len - padding);
  2402. } else
  2403. ctl->dr_len = ctl->cur_len;
  2404. }
  2405. #define extra_space(ctl) ((ctl)->limit - (ctl)->cur_len)
  2406. static unsigned char *
  2407. extra_next_record(struct ctl_extr_rec *ctl, int length)
  2408. {
  2409. int cur_len = ctl->cur_len;/* save cur_len */
  2410. /* Close the current extra record or Directory Record. */
  2411. extra_close_record(ctl, RR_CE_SIZE);
  2412. /* Get a next extra record. */
  2413. ctl->use_extr = 1;
  2414. if (ctl->bp != NULL) {
  2415. /* Storing data into an extra record. */
  2416. unsigned char *p;
  2417. /* Save the pointer where a CE extension will be
  2418. * stored to. */
  2419. ctl->ce_ptr = &ctl->bp[cur_len+1];
  2420. p = extra_get_record(ctl->isoent,
  2421. &ctl->limit, &ctl->extr_off, &ctl->extr_loc);
  2422. ctl->bp = p - 1;/* the base of bp offset is 1. */
  2423. } else
  2424. /* Calculating the size of an extra record. */
  2425. (void)extra_get_record(ctl->isoent,
  2426. &ctl->limit, NULL, NULL);
  2427. ctl->cur_len = 0;
  2428. /* Check if an extra record is almost full.
  2429. * If so, get a next one. */
  2430. if (extra_space(ctl) < length)
  2431. (void)extra_next_record(ctl, length);
  2432. return (ctl->bp);
  2433. }
  2434. static inline struct extr_rec *
  2435. extra_last_record(struct isoent *isoent)
  2436. {
  2437. if (isoent->extr_rec_list.first == NULL)
  2438. return (NULL);
  2439. return ((struct extr_rec *)(void *)
  2440. ((char *)(isoent->extr_rec_list.last)
  2441. - offsetof(struct extr_rec, next)));
  2442. }
  2443. static unsigned char *
  2444. extra_get_record(struct isoent *isoent, int *space, int *off, int *loc)
  2445. {
  2446. struct extr_rec *rec;
  2447. isoent = isoent->parent;
  2448. if (off != NULL) {
  2449. /* Storing data into an extra record. */
  2450. rec = isoent->extr_rec_list.current;
  2451. if (DR_SAFETY > LOGICAL_BLOCK_SIZE - rec->offset)
  2452. rec = rec->next;
  2453. } else {
  2454. /* Calculating the size of an extra record. */
  2455. rec = extra_last_record(isoent);
  2456. if (rec == NULL ||
  2457. DR_SAFETY > LOGICAL_BLOCK_SIZE - rec->offset) {
  2458. rec = malloc(sizeof(*rec));
  2459. if (rec == NULL)
  2460. return (NULL);
  2461. rec->location = 0;
  2462. rec->offset = 0;
  2463. /* Insert `rec` into the tail of isoent->extr_rec_list */
  2464. rec->next = NULL;
  2465. /*
  2466. * Note: testing isoent->extr_rec_list.last == NULL
  2467. * here is really unneeded since it has been already
  2468. * initialized at isoent_new function but Clang Static
  2469. * Analyzer claims that it is dereference of null
  2470. * pointer.
  2471. */
  2472. if (isoent->extr_rec_list.last == NULL)
  2473. isoent->extr_rec_list.last =
  2474. &(isoent->extr_rec_list.first);
  2475. *isoent->extr_rec_list.last = rec;
  2476. isoent->extr_rec_list.last = &(rec->next);
  2477. }
  2478. }
  2479. *space = LOGICAL_BLOCK_SIZE - rec->offset - DR_SAFETY;
  2480. if (*space & 0x01)
  2481. *space -= 1;/* Keep padding space. */
  2482. if (off != NULL)
  2483. *off = rec->offset;
  2484. if (loc != NULL)
  2485. *loc = rec->location;
  2486. isoent->extr_rec_list.current = rec;
  2487. return (&rec->buf[rec->offset]);
  2488. }
  2489. static void
  2490. extra_tell_used_size(struct ctl_extr_rec *ctl, int size)
  2491. {
  2492. struct isoent *isoent;
  2493. struct extr_rec *rec;
  2494. if (ctl->use_extr) {
  2495. isoent = ctl->isoent->parent;
  2496. rec = isoent->extr_rec_list.current;
  2497. if (rec != NULL)
  2498. rec->offset += size;
  2499. }
  2500. ctl->cur_len += size;
  2501. }
  2502. static int
  2503. extra_setup_location(struct isoent *isoent, int location)
  2504. {
  2505. struct extr_rec *rec;
  2506. int cnt;
  2507. cnt = 0;
  2508. rec = isoent->extr_rec_list.first;
  2509. isoent->extr_rec_list.current = rec;
  2510. while (rec) {
  2511. cnt++;
  2512. rec->location = location++;
  2513. rec->offset = 0;
  2514. rec = rec->next;
  2515. }
  2516. return (cnt);
  2517. }
  2518. /*
  2519. * Create the RRIP entries.
  2520. */
  2521. static int
  2522. set_directory_record_rr(unsigned char *bp, int dr_len,
  2523. struct isoent *isoent, struct iso9660 *iso9660, enum dir_rec_type t)
  2524. {
  2525. /* Flags(BP 5) of the Rockridge "RR" System Use Field */
  2526. unsigned char rr_flag;
  2527. #define RR_USE_PX 0x01
  2528. #define RR_USE_PN 0x02
  2529. #define RR_USE_SL 0x04
  2530. #define RR_USE_NM 0x08
  2531. #define RR_USE_CL 0x10
  2532. #define RR_USE_PL 0x20
  2533. #define RR_USE_RE 0x40
  2534. #define RR_USE_TF 0x80
  2535. int length;
  2536. struct ctl_extr_rec ctl;
  2537. struct isoent *rr_parent, *pxent;
  2538. struct isofile *file;
  2539. bp = extra_open_record(bp, dr_len, isoent, &ctl);
  2540. if (t == DIR_REC_PARENT) {
  2541. rr_parent = isoent->rr_parent;
  2542. pxent = isoent->parent;
  2543. if (rr_parent != NULL)
  2544. isoent = rr_parent;
  2545. else
  2546. isoent = isoent->parent;
  2547. } else {
  2548. rr_parent = NULL;
  2549. pxent = isoent;
  2550. }
  2551. file = isoent->file;
  2552. if (t != DIR_REC_NORMAL) {
  2553. rr_flag = RR_USE_PX | RR_USE_TF;
  2554. if (rr_parent != NULL)
  2555. rr_flag |= RR_USE_PL;
  2556. } else {
  2557. rr_flag = RR_USE_PX | RR_USE_NM | RR_USE_TF;
  2558. if (archive_entry_filetype(file->entry) == AE_IFLNK)
  2559. rr_flag |= RR_USE_SL;
  2560. if (isoent->rr_parent != NULL)
  2561. rr_flag |= RR_USE_RE;
  2562. if (isoent->rr_child != NULL)
  2563. rr_flag |= RR_USE_CL;
  2564. if (archive_entry_filetype(file->entry) == AE_IFCHR ||
  2565. archive_entry_filetype(file->entry) == AE_IFBLK)
  2566. rr_flag |= RR_USE_PN;
  2567. #ifdef COMPAT_MKISOFS
  2568. /*
  2569. * mkisofs 2.01.01a63 records "RE" extension to
  2570. * the entry of "rr_moved" directory.
  2571. * I don't understand this behavior.
  2572. */
  2573. if (isoent->virtual &&
  2574. isoent->parent == iso9660->primary.rootent &&
  2575. strcmp(isoent->file->basename.s, "rr_moved") == 0)
  2576. rr_flag |= RR_USE_RE;
  2577. #endif
  2578. }
  2579. /* Write "SP" System Use Entry. */
  2580. if (t == DIR_REC_SELF && isoent == isoent->parent) {
  2581. length = 7;
  2582. if (bp != NULL) {
  2583. bp[1] = 'S';
  2584. bp[2] = 'P';
  2585. bp[3] = length;
  2586. bp[4] = 1; /* version */
  2587. bp[5] = 0xBE; /* Check Byte */
  2588. bp[6] = 0xEF; /* Check Byte */
  2589. bp[7] = 0;
  2590. bp += length;
  2591. }
  2592. extra_tell_used_size(&ctl, length);
  2593. }
  2594. /* Write "RR" System Use Entry. */
  2595. length = 5;
  2596. if (extra_space(&ctl) < length)
  2597. bp = extra_next_record(&ctl, length);
  2598. if (bp != NULL) {
  2599. bp[1] = 'R';
  2600. bp[2] = 'R';
  2601. bp[3] = length;
  2602. bp[4] = 1; /* version */
  2603. bp[5] = rr_flag;
  2604. bp += length;
  2605. }
  2606. extra_tell_used_size(&ctl, length);
  2607. /* Write "NM" System Use Entry. */
  2608. if (rr_flag & RR_USE_NM) {
  2609. /*
  2610. * "NM" Format:
  2611. * e.g. a basename is 'foo'
  2612. * len ver flg
  2613. * +----+----+----+----+----+----+----+----+
  2614. * | 'N'| 'M'| 08 | 01 | 00 | 'f'| 'o'| 'o'|
  2615. * +----+----+----+----+----+----+----+----+
  2616. * <----------------- len ----------------->
  2617. */
  2618. size_t nmlen = file->basename.length;
  2619. const char *nm = file->basename.s;
  2620. size_t nmmax;
  2621. if (extra_space(&ctl) < 6)
  2622. bp = extra_next_record(&ctl, 6);
  2623. if (bp != NULL) {
  2624. bp[1] = 'N';
  2625. bp[2] = 'M';
  2626. bp[4] = 1; /* version */
  2627. }
  2628. nmmax = extra_space(&ctl);
  2629. if (nmmax > 0xff)
  2630. nmmax = 0xff;
  2631. while (nmlen + 5 > nmmax) {
  2632. length = (int)nmmax;
  2633. if (bp != NULL) {
  2634. bp[3] = length;
  2635. bp[5] = 0x01;/* Alternate Name continues
  2636. * in next "NM" field */
  2637. memcpy(bp+6, nm, length - 5);
  2638. bp += length;
  2639. }
  2640. nmlen -= length - 5;
  2641. nm += length - 5;
  2642. extra_tell_used_size(&ctl, length);
  2643. if (extra_space(&ctl) < 6) {
  2644. bp = extra_next_record(&ctl, 6);
  2645. nmmax = extra_space(&ctl);
  2646. if (nmmax > 0xff)
  2647. nmmax = 0xff;
  2648. }
  2649. if (bp != NULL) {
  2650. bp[1] = 'N';
  2651. bp[2] = 'M';
  2652. bp[4] = 1; /* version */
  2653. }
  2654. }
  2655. length = 5 + (int)nmlen;
  2656. if (bp != NULL) {
  2657. bp[3] = length;
  2658. bp[5] = 0;
  2659. memcpy(bp+6, nm, nmlen);
  2660. bp += length;
  2661. }
  2662. extra_tell_used_size(&ctl, length);
  2663. }
  2664. /* Write "PX" System Use Entry. */
  2665. if (rr_flag & RR_USE_PX) {
  2666. /*
  2667. * "PX" Format:
  2668. * len ver
  2669. * +----+----+----+----+-----------+-----------+
  2670. * | 'P'| 'X'| 2C | 01 | FILE MODE | LINKS |
  2671. * +----+----+----+----+-----------+-----------+
  2672. * 0 1 2 3 4 12 20
  2673. * +-----------+-----------+------------------+
  2674. * | USER ID | GROUP ID |FILE SERIAL NUMBER|
  2675. * +-----------+-----------+------------------+
  2676. * 20 28 36 44
  2677. */
  2678. length = 44;
  2679. if (extra_space(&ctl) < length)
  2680. bp = extra_next_record(&ctl, length);
  2681. if (bp != NULL) {
  2682. mode_t mode;
  2683. int64_t uid;
  2684. int64_t gid;
  2685. mode = archive_entry_mode(file->entry);
  2686. uid = archive_entry_uid(file->entry);
  2687. gid = archive_entry_gid(file->entry);
  2688. if (iso9660->opt.rr == OPT_RR_USEFUL) {
  2689. /*
  2690. * This action is similar to mkisofs -r option
  2691. * but our rockridge=useful option does not
  2692. * set a zero to uid and gid.
  2693. */
  2694. /* set all read bit ON */
  2695. mode |= 0444;
  2696. #if !defined(_WIN32) && !defined(__CYGWIN__)
  2697. if (mode & 0111)
  2698. #endif
  2699. /* set all exec bit ON */
  2700. mode |= 0111;
  2701. /* clear all write bits. */
  2702. mode &= ~0222;
  2703. /* clear setuid,setgid,sticky bits. */
  2704. mode &= ~07000;
  2705. }
  2706. bp[1] = 'P';
  2707. bp[2] = 'X';
  2708. bp[3] = length;
  2709. bp[4] = 1; /* version */
  2710. /* file mode */
  2711. set_num_733(bp+5, mode);
  2712. /* file links (stat.st_nlink) */
  2713. set_num_733(bp+13,
  2714. archive_entry_nlink(file->entry));
  2715. set_num_733(bp+21, (uint32_t)uid);
  2716. set_num_733(bp+29, (uint32_t)gid);
  2717. /* File Serial Number */
  2718. if (pxent->dir)
  2719. set_num_733(bp+37, pxent->dir_location);
  2720. else if (file->hardlink_target != NULL)
  2721. set_num_733(bp+37,
  2722. file->hardlink_target->cur_content->location);
  2723. else
  2724. set_num_733(bp+37,
  2725. file->cur_content->location);
  2726. bp += length;
  2727. }
  2728. extra_tell_used_size(&ctl, length);
  2729. }
  2730. /* Write "SL" System Use Entry. */
  2731. if (rr_flag & RR_USE_SL) {
  2732. /*
  2733. * "SL" Format:
  2734. * e.g. a symbolic name is 'foo/bar'
  2735. * len ver flg
  2736. * +----+----+----+----+----+------------+
  2737. * | 'S'| 'L'| 0F | 01 | 00 | components |
  2738. * +----+----+----+----+----+-----+------+
  2739. * 0 1 2 3 4 5 ...|... 15
  2740. * <----------------- len --------+------>
  2741. * components : |
  2742. * cflg clen |
  2743. * +----+----+----+----+----+ |
  2744. * | 00 | 03 | 'f'| 'o'| 'o'| <---+
  2745. * +----+----+----+----+----+ |
  2746. * 5 6 7 8 9 10 |
  2747. * cflg clen |
  2748. * +----+----+----+----+----+ |
  2749. * | 00 | 03 | 'b'| 'a'| 'r'| <---+
  2750. * +----+----+----+----+----+
  2751. * 10 11 12 13 14 15
  2752. *
  2753. * - cflg : flag of component
  2754. * - clen : length of component
  2755. */
  2756. const char *sl;
  2757. char sl_last;
  2758. if (extra_space(&ctl) < 7)
  2759. bp = extra_next_record(&ctl, 7);
  2760. sl = file->symlink.s;
  2761. sl_last = '\0';
  2762. if (bp != NULL) {
  2763. bp[1] = 'S';
  2764. bp[2] = 'L';
  2765. bp[4] = 1; /* version */
  2766. }
  2767. for (;;) {
  2768. unsigned char *nc, *cf, *cl, cldmy = 0;
  2769. int sllen, slmax;
  2770. slmax = extra_space(&ctl);
  2771. if (slmax > 0xff)
  2772. slmax = 0xff;
  2773. if (bp != NULL)
  2774. nc = &bp[6];
  2775. else
  2776. nc = NULL;
  2777. cf = cl = NULL;
  2778. sllen = 0;
  2779. while (*sl && sllen + 11 < slmax) {
  2780. if (sl_last == '\0' && sl[0] == '/') {
  2781. /*
  2782. * flg len
  2783. * +----+----+
  2784. * | 08 | 00 | ROOT component.
  2785. * +----+----+ ("/")
  2786. *
  2787. * Root component has to appear
  2788. * at the first component only.
  2789. */
  2790. if (nc != NULL) {
  2791. cf = nc++;
  2792. *cf = 0x08; /* ROOT */
  2793. *nc++ = 0;
  2794. }
  2795. sllen += 2;
  2796. sl++;
  2797. sl_last = '/';
  2798. cl = NULL;
  2799. continue;
  2800. }
  2801. if (((sl_last == '\0' || sl_last == '/') &&
  2802. sl[0] == '.' && sl[1] == '.' &&
  2803. (sl[2] == '/' || sl[2] == '\0')) ||
  2804. (sl[0] == '/' &&
  2805. sl[1] == '.' && sl[2] == '.' &&
  2806. (sl[3] == '/' || sl[3] == '\0'))) {
  2807. /*
  2808. * flg len
  2809. * +----+----+
  2810. * | 04 | 00 | PARENT component.
  2811. * +----+----+ ("..")
  2812. */
  2813. if (nc != NULL) {
  2814. cf = nc++;
  2815. *cf = 0x04; /* PARENT */
  2816. *nc++ = 0;
  2817. }
  2818. sllen += 2;
  2819. if (sl[0] == '/')
  2820. sl += 3;/* skip "/.." */
  2821. else
  2822. sl += 2;/* skip ".." */
  2823. sl_last = '.';
  2824. cl = NULL;
  2825. continue;
  2826. }
  2827. if (((sl_last == '\0' || sl_last == '/') &&
  2828. sl[0] == '.' &&
  2829. (sl[1] == '/' || sl[1] == '\0')) ||
  2830. (sl[0] == '/' && sl[1] == '.' &&
  2831. (sl[2] == '/' || sl[2] == '\0'))) {
  2832. /*
  2833. * flg len
  2834. * +----+----+
  2835. * | 02 | 00 | CURRENT component.
  2836. * +----+----+ (".")
  2837. */
  2838. if (nc != NULL) {
  2839. cf = nc++;
  2840. *cf = 0x02; /* CURRENT */
  2841. *nc++ = 0;
  2842. }
  2843. sllen += 2;
  2844. if (sl[0] == '/')
  2845. sl += 2;/* skip "/." */
  2846. else
  2847. sl ++; /* skip "." */
  2848. sl_last = '.';
  2849. cl = NULL;
  2850. continue;
  2851. }
  2852. if (sl[0] == '/' || cl == NULL) {
  2853. if (nc != NULL) {
  2854. cf = nc++;
  2855. *cf = 0;
  2856. cl = nc++;
  2857. *cl = 0;
  2858. } else
  2859. cl = &cldmy;
  2860. sllen += 2;
  2861. if (sl[0] == '/') {
  2862. sl_last = *sl++;
  2863. continue;
  2864. }
  2865. }
  2866. sl_last = *sl++;
  2867. if (nc != NULL) {
  2868. *nc++ = sl_last;
  2869. (*cl) ++;
  2870. }
  2871. sllen++;
  2872. }
  2873. if (*sl) {
  2874. length = 5 + sllen;
  2875. if (bp != NULL) {
  2876. /*
  2877. * Mark flg as CONTINUE component.
  2878. */
  2879. *cf |= 0x01;
  2880. /*
  2881. * len ver flg
  2882. * +----+----+----+----+----+-
  2883. * | 'S'| 'L'| XX | 01 | 01 |
  2884. * +----+----+----+----+----+-
  2885. * ^
  2886. * continues in next "SL"
  2887. */
  2888. bp[3] = length;
  2889. bp[5] = 0x01;/* This Symbolic Link
  2890. * continues in next
  2891. * "SL" field */
  2892. bp += length;
  2893. }
  2894. extra_tell_used_size(&ctl, length);
  2895. if (extra_space(&ctl) < 11)
  2896. bp = extra_next_record(&ctl, 11);
  2897. if (bp != NULL) {
  2898. /* Next 'SL' */
  2899. bp[1] = 'S';
  2900. bp[2] = 'L';
  2901. bp[4] = 1; /* version */
  2902. }
  2903. } else {
  2904. length = 5 + sllen;
  2905. if (bp != NULL) {
  2906. bp[3] = length;
  2907. bp[5] = 0;
  2908. bp += length;
  2909. }
  2910. extra_tell_used_size(&ctl, length);
  2911. break;
  2912. }
  2913. }
  2914. }
  2915. /* Write "TF" System Use Entry. */
  2916. if (rr_flag & RR_USE_TF) {
  2917. /*
  2918. * "TF" Format:
  2919. * len ver
  2920. * +----+----+----+----+-----+-------------+
  2921. * | 'T'| 'F'| XX | 01 |FLAGS| TIME STAMPS |
  2922. * +----+----+----+----+-----+-------------+
  2923. * 0 1 2 3 4 5 XX
  2924. * TIME STAMPS : ISO 9660 Standard 9.1.5.
  2925. * If TF_LONG_FORM FLAGS is set,
  2926. * use ISO9660 Standard 8.4.26.1.
  2927. */
  2928. #define TF_CREATION 0x01 /* Creation time recorded */
  2929. #define TF_MODIFY 0x02 /* Modification time recorded */
  2930. #define TF_ACCESS 0x04 /* Last Access time recorded */
  2931. #define TF_ATTRIBUTES 0x08 /* Last Attribute Change time recorded */
  2932. #define TF_BACKUP 0x10 /* Last Backup time recorded */
  2933. #define TF_EXPIRATION 0x20 /* Expiration time recorded */
  2934. #define TF_EFFECTIVE 0x40 /* Effective time recorded */
  2935. #define TF_LONG_FORM 0x80 /* ISO 9660 17-byte time format used */
  2936. unsigned char tf_flags;
  2937. length = 5;
  2938. tf_flags = 0;
  2939. #ifndef COMPAT_MKISOFS
  2940. if (archive_entry_birthtime_is_set(file->entry) &&
  2941. archive_entry_birthtime(file->entry) <=
  2942. archive_entry_mtime(file->entry)) {
  2943. length += 7;
  2944. tf_flags |= TF_CREATION;
  2945. }
  2946. #endif
  2947. if (archive_entry_mtime_is_set(file->entry)) {
  2948. length += 7;
  2949. tf_flags |= TF_MODIFY;
  2950. }
  2951. if (archive_entry_atime_is_set(file->entry)) {
  2952. length += 7;
  2953. tf_flags |= TF_ACCESS;
  2954. }
  2955. if (archive_entry_ctime_is_set(file->entry)) {
  2956. length += 7;
  2957. tf_flags |= TF_ATTRIBUTES;
  2958. }
  2959. if (extra_space(&ctl) < length)
  2960. bp = extra_next_record(&ctl, length);
  2961. if (bp != NULL) {
  2962. bp[1] = 'T';
  2963. bp[2] = 'F';
  2964. bp[3] = length;
  2965. bp[4] = 1; /* version */
  2966. bp[5] = tf_flags;
  2967. bp += 5;
  2968. /* Creation time */
  2969. if (tf_flags & TF_CREATION) {
  2970. set_time_915(bp+1,
  2971. archive_entry_birthtime(file->entry));
  2972. bp += 7;
  2973. }
  2974. /* Modification time */
  2975. if (tf_flags & TF_MODIFY) {
  2976. set_time_915(bp+1,
  2977. archive_entry_mtime(file->entry));
  2978. bp += 7;
  2979. }
  2980. /* Last Access time */
  2981. if (tf_flags & TF_ACCESS) {
  2982. set_time_915(bp+1,
  2983. archive_entry_atime(file->entry));
  2984. bp += 7;
  2985. }
  2986. /* Last Attribute Change time */
  2987. if (tf_flags & TF_ATTRIBUTES) {
  2988. set_time_915(bp+1,
  2989. archive_entry_ctime(file->entry));
  2990. bp += 7;
  2991. }
  2992. }
  2993. extra_tell_used_size(&ctl, length);
  2994. }
  2995. /* Write "RE" System Use Entry. */
  2996. if (rr_flag & RR_USE_RE) {
  2997. /*
  2998. * "RE" Format:
  2999. * len ver
  3000. * +----+----+----+----+
  3001. * | 'R'| 'E'| 04 | 01 |
  3002. * +----+----+----+----+
  3003. * 0 1 2 3 4
  3004. */
  3005. length = 4;
  3006. if (extra_space(&ctl) < length)
  3007. bp = extra_next_record(&ctl, length);
  3008. if (bp != NULL) {
  3009. bp[1] = 'R';
  3010. bp[2] = 'E';
  3011. bp[3] = length;
  3012. bp[4] = 1; /* version */
  3013. bp += length;
  3014. }
  3015. extra_tell_used_size(&ctl, length);
  3016. }
  3017. /* Write "PL" System Use Entry. */
  3018. if (rr_flag & RR_USE_PL) {
  3019. /*
  3020. * "PL" Format:
  3021. * len ver
  3022. * +----+----+----+----+------------+
  3023. * | 'P'| 'L'| 0C | 01 | *LOCATION |
  3024. * +----+----+----+----+------------+
  3025. * 0 1 2 3 4 12
  3026. * *LOCATION: location of parent directory
  3027. */
  3028. length = 12;
  3029. if (extra_space(&ctl) < length)
  3030. bp = extra_next_record(&ctl, length);
  3031. if (bp != NULL) {
  3032. bp[1] = 'P';
  3033. bp[2] = 'L';
  3034. bp[3] = length;
  3035. bp[4] = 1; /* version */
  3036. set_num_733(bp + 5,
  3037. rr_parent->dir_location);
  3038. bp += length;
  3039. }
  3040. extra_tell_used_size(&ctl, length);
  3041. }
  3042. /* Write "CL" System Use Entry. */
  3043. if (rr_flag & RR_USE_CL) {
  3044. /*
  3045. * "CL" Format:
  3046. * len ver
  3047. * +----+----+----+----+------------+
  3048. * | 'C'| 'L'| 0C | 01 | *LOCATION |
  3049. * +----+----+----+----+------------+
  3050. * 0 1 2 3 4 12
  3051. * *LOCATION: location of child directory
  3052. */
  3053. length = 12;
  3054. if (extra_space(&ctl) < length)
  3055. bp = extra_next_record(&ctl, length);
  3056. if (bp != NULL) {
  3057. bp[1] = 'C';
  3058. bp[2] = 'L';
  3059. bp[3] = length;
  3060. bp[4] = 1; /* version */
  3061. set_num_733(bp + 5,
  3062. isoent->rr_child->dir_location);
  3063. bp += length;
  3064. }
  3065. extra_tell_used_size(&ctl, length);
  3066. }
  3067. /* Write "PN" System Use Entry. */
  3068. if (rr_flag & RR_USE_PN) {
  3069. /*
  3070. * "PN" Format:
  3071. * len ver
  3072. * +----+----+----+----+------------+------------+
  3073. * | 'P'| 'N'| 14 | 01 | dev_t high | dev_t low |
  3074. * +----+----+----+----+------------+------------+
  3075. * 0 1 2 3 4 12 20
  3076. */
  3077. length = 20;
  3078. if (extra_space(&ctl) < length)
  3079. bp = extra_next_record(&ctl, length);
  3080. if (bp != NULL) {
  3081. uint64_t dev;
  3082. bp[1] = 'P';
  3083. bp[2] = 'N';
  3084. bp[3] = length;
  3085. bp[4] = 1; /* version */
  3086. dev = (uint64_t)archive_entry_rdev(file->entry);
  3087. set_num_733(bp + 5, (uint32_t)(dev >> 32));
  3088. set_num_733(bp + 13, (uint32_t)(dev & 0xFFFFFFFF));
  3089. bp += length;
  3090. }
  3091. extra_tell_used_size(&ctl, length);
  3092. }
  3093. /* Write "ZF" System Use Entry. */
  3094. if (file->zisofs.header_size) {
  3095. /*
  3096. * "ZF" Format:
  3097. * len ver
  3098. * +----+----+----+----+----+----+-------------+
  3099. * | 'Z'| 'F'| 10 | 01 | 'p'| 'z'| Header Size |
  3100. * +----+----+----+----+----+----+-------------+
  3101. * 0 1 2 3 4 5 6 7
  3102. * +--------------------+-------------------+
  3103. * | Log2 of block Size | Uncompressed Size |
  3104. * +--------------------+-------------------+
  3105. * 7 8 16
  3106. */
  3107. length = 16;
  3108. if (extra_space(&ctl) < length)
  3109. bp = extra_next_record(&ctl, length);
  3110. if (bp != NULL) {
  3111. bp[1] = 'Z';
  3112. bp[2] = 'F';
  3113. bp[3] = length;
  3114. bp[4] = 1; /* version */
  3115. bp[5] = 'p';
  3116. bp[6] = 'z';
  3117. bp[7] = file->zisofs.header_size;
  3118. bp[8] = file->zisofs.log2_bs;
  3119. set_num_733(bp + 9, file->zisofs.uncompressed_size);
  3120. bp += length;
  3121. }
  3122. extra_tell_used_size(&ctl, length);
  3123. }
  3124. /* Write "CE" System Use Entry. */
  3125. if (t == DIR_REC_SELF && isoent == isoent->parent) {
  3126. length = RR_CE_SIZE;
  3127. if (bp != NULL)
  3128. set_SUSP_CE(bp+1, iso9660->location_rrip_er,
  3129. 0, RRIP_ER_SIZE);
  3130. extra_tell_used_size(&ctl, length);
  3131. }
  3132. extra_close_record(&ctl, 0);
  3133. return (ctl.dr_len);
  3134. }
  3135. /*
  3136. * Write data of a Directory Record or calculate writing bytes itself.
  3137. * If parameter `p' is NULL, calculates the size of writing data, which
  3138. * a Directory Record needs to write, then it saved and return
  3139. * the calculated size.
  3140. * Parameter `n' is a remaining size of buffer. when parameter `p' is
  3141. * not NULL, check whether that `n' is not less than the saved size.
  3142. * if that `n' is small, return zero.
  3143. *
  3144. * This format of the Directory Record is according to
  3145. * ISO9660 Standard 9.1
  3146. */
  3147. static int
  3148. set_directory_record(unsigned char *p, size_t n, struct isoent *isoent,
  3149. struct iso9660 *iso9660, enum dir_rec_type t,
  3150. enum vdd_type vdd_type)
  3151. {
  3152. unsigned char *bp;
  3153. size_t dr_len;
  3154. size_t fi_len;
  3155. if (p != NULL) {
  3156. /*
  3157. * Check whether a write buffer size is less than the
  3158. * saved size which is needed to write this Directory
  3159. * Record.
  3160. */
  3161. switch (t) {
  3162. case DIR_REC_VD:
  3163. dr_len = isoent->dr_len.vd; break;
  3164. case DIR_REC_SELF:
  3165. dr_len = isoent->dr_len.self; break;
  3166. case DIR_REC_PARENT:
  3167. dr_len = isoent->dr_len.parent; break;
  3168. case DIR_REC_NORMAL:
  3169. default:
  3170. dr_len = isoent->dr_len.normal; break;
  3171. }
  3172. if (dr_len > n)
  3173. return (0);/* Needs more buffer size. */
  3174. }
  3175. if (t == DIR_REC_NORMAL && isoent->identifier != NULL)
  3176. fi_len = isoent->id_len;
  3177. else
  3178. fi_len = 1;
  3179. if (p != NULL) {
  3180. struct isoent *xisoent;
  3181. struct isofile *file;
  3182. unsigned char flag;
  3183. if (t == DIR_REC_PARENT)
  3184. xisoent = isoent->parent;
  3185. else
  3186. xisoent = isoent;
  3187. file = isoent->file;
  3188. if (file->hardlink_target != NULL)
  3189. file = file->hardlink_target;
  3190. /* Make a file flag. */
  3191. if (xisoent->dir)
  3192. flag = FILE_FLAG_DIRECTORY;
  3193. else {
  3194. if (file->cur_content->next != NULL)
  3195. flag = FILE_FLAG_MULTI_EXTENT;
  3196. else
  3197. flag = 0;
  3198. }
  3199. bp = p -1;
  3200. /* Extended Attribute Record Length */
  3201. set_num_711(bp+2, 0);
  3202. /* Location of Extent */
  3203. if (xisoent->dir)
  3204. set_num_733(bp+3, xisoent->dir_location);
  3205. else
  3206. set_num_733(bp+3, file->cur_content->location);
  3207. /* Data Length */
  3208. if (xisoent->dir)
  3209. set_num_733(bp+11,
  3210. xisoent->dir_block * LOGICAL_BLOCK_SIZE);
  3211. else
  3212. set_num_733(bp+11, (uint32_t)file->cur_content->size);
  3213. /* Recording Date and Time */
  3214. /* NOTE:
  3215. * If a file type is symbolic link, you are seeing this
  3216. * field value is different from a value mkisofs makes.
  3217. * libarchive uses lstat to get this one, but it
  3218. * seems mkisofs uses stat to get.
  3219. */
  3220. set_time_915(bp+19,
  3221. archive_entry_mtime(xisoent->file->entry));
  3222. /* File Flags */
  3223. bp[26] = flag;
  3224. /* File Unit Size */
  3225. set_num_711(bp+27, 0);
  3226. /* Interleave Gap Size */
  3227. set_num_711(bp+28, 0);
  3228. /* Volume Sequence Number */
  3229. set_num_723(bp+29, iso9660->volume_sequence_number);
  3230. /* Length of File Identifier */
  3231. set_num_711(bp+33, (unsigned char)fi_len);
  3232. /* File Identifier */
  3233. switch (t) {
  3234. case DIR_REC_VD:
  3235. case DIR_REC_SELF:
  3236. set_num_711(bp+34, 0);
  3237. break;
  3238. case DIR_REC_PARENT:
  3239. set_num_711(bp+34, 1);
  3240. break;
  3241. case DIR_REC_NORMAL:
  3242. if (isoent->identifier != NULL)
  3243. memcpy(bp+34, isoent->identifier, fi_len);
  3244. else
  3245. set_num_711(bp+34, 0);
  3246. break;
  3247. }
  3248. } else
  3249. bp = NULL;
  3250. dr_len = 33 + fi_len;
  3251. /* Padding Field */
  3252. if (dr_len & 0x01) {
  3253. dr_len ++;
  3254. if (p != NULL)
  3255. bp[dr_len] = 0;
  3256. }
  3257. /* Volume Descriptor does not record extension. */
  3258. if (t == DIR_REC_VD) {
  3259. if (p != NULL)
  3260. /* Length of Directory Record */
  3261. set_num_711(p, (unsigned char)dr_len);
  3262. else
  3263. isoent->dr_len.vd = (int)dr_len;
  3264. return ((int)dr_len);
  3265. }
  3266. /* Rockridge */
  3267. if (iso9660->opt.rr && vdd_type != VDD_JOLIET)
  3268. dr_len = set_directory_record_rr(bp, (int)dr_len,
  3269. isoent, iso9660, t);
  3270. if (p != NULL)
  3271. /* Length of Directory Record */
  3272. set_num_711(p, (unsigned char)dr_len);
  3273. else {
  3274. /*
  3275. * Save the size which is needed to write this
  3276. * Directory Record.
  3277. */
  3278. switch (t) {
  3279. case DIR_REC_VD:
  3280. /* This case does not come, but compiler
  3281. * complains that DIR_REC_VD not handled
  3282. * in switch .... */
  3283. break;
  3284. case DIR_REC_SELF:
  3285. isoent->dr_len.self = (int)dr_len; break;
  3286. case DIR_REC_PARENT:
  3287. isoent->dr_len.parent = (int)dr_len; break;
  3288. case DIR_REC_NORMAL:
  3289. isoent->dr_len.normal = (int)dr_len; break;
  3290. }
  3291. }
  3292. return ((int)dr_len);
  3293. }
  3294. /*
  3295. * Calculate the size of a directory record.
  3296. */
  3297. static inline int
  3298. get_dir_rec_size(struct iso9660 *iso9660, struct isoent *isoent,
  3299. enum dir_rec_type t, enum vdd_type vdd_type)
  3300. {
  3301. return (set_directory_record(NULL, SIZE_MAX,
  3302. isoent, iso9660, t, vdd_type));
  3303. }
  3304. /*
  3305. * Manage to write ISO-image data with wbuff to reduce calling
  3306. * __archive_write_output() for performance.
  3307. */
  3308. static inline unsigned char *
  3309. wb_buffptr(struct archive_write *a)
  3310. {
  3311. struct iso9660 *iso9660 = (struct iso9660 *)a->format_data;
  3312. return (&(iso9660->wbuff[sizeof(iso9660->wbuff)
  3313. - iso9660->wbuff_remaining]));
  3314. }
  3315. static int
  3316. wb_write_out(struct archive_write *a)
  3317. {
  3318. struct iso9660 *iso9660 = (struct iso9660 *)a->format_data;
  3319. size_t wsize, nw;
  3320. int r;
  3321. wsize = sizeof(iso9660->wbuff) - iso9660->wbuff_remaining;
  3322. nw = wsize % LOGICAL_BLOCK_SIZE;
  3323. if (iso9660->wbuff_type == WB_TO_STREAM)
  3324. r = __archive_write_output(a, iso9660->wbuff, wsize - nw);
  3325. else
  3326. r = write_to_temp(a, iso9660->wbuff, wsize - nw);
  3327. /* Increase the offset. */
  3328. iso9660->wbuff_offset += wsize - nw;
  3329. if (iso9660->wbuff_offset > iso9660->wbuff_written)
  3330. iso9660->wbuff_written = iso9660->wbuff_offset;
  3331. iso9660->wbuff_remaining = sizeof(iso9660->wbuff);
  3332. if (nw) {
  3333. iso9660->wbuff_remaining -= nw;
  3334. memmove(iso9660->wbuff, iso9660->wbuff + wsize - nw, nw);
  3335. }
  3336. return (r);
  3337. }
  3338. static int
  3339. wb_consume(struct archive_write *a, size_t size)
  3340. {
  3341. struct iso9660 *iso9660 = (struct iso9660 *)a->format_data;
  3342. if (size > iso9660->wbuff_remaining ||
  3343. iso9660->wbuff_remaining == 0) {
  3344. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3345. "Internal Programming error: iso9660:wb_consume()"
  3346. " size=%jd, wbuff_remaining=%jd",
  3347. (intmax_t)size, (intmax_t)iso9660->wbuff_remaining);
  3348. return (ARCHIVE_FATAL);
  3349. }
  3350. iso9660->wbuff_remaining -= size;
  3351. if (iso9660->wbuff_remaining < LOGICAL_BLOCK_SIZE)
  3352. return (wb_write_out(a));
  3353. return (ARCHIVE_OK);
  3354. }
  3355. #ifdef HAVE_ZLIB_H
  3356. static int
  3357. wb_set_offset(struct archive_write *a, int64_t off)
  3358. {
  3359. struct iso9660 *iso9660 = (struct iso9660 *)a->format_data;
  3360. int64_t used, ext_bytes;
  3361. if (iso9660->wbuff_type != WB_TO_TEMP) {
  3362. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3363. "Internal Programming error: iso9660:wb_set_offset()");
  3364. return (ARCHIVE_FATAL);
  3365. }
  3366. used = sizeof(iso9660->wbuff) - iso9660->wbuff_remaining;
  3367. if (iso9660->wbuff_offset + used > iso9660->wbuff_tail)
  3368. iso9660->wbuff_tail = iso9660->wbuff_offset + used;
  3369. if (iso9660->wbuff_offset < iso9660->wbuff_written) {
  3370. if (used > 0 &&
  3371. write_to_temp(a, iso9660->wbuff, (size_t)used) != ARCHIVE_OK)
  3372. return (ARCHIVE_FATAL);
  3373. iso9660->wbuff_offset = iso9660->wbuff_written;
  3374. lseek(iso9660->temp_fd, iso9660->wbuff_offset, SEEK_SET);
  3375. iso9660->wbuff_remaining = sizeof(iso9660->wbuff);
  3376. used = 0;
  3377. }
  3378. if (off < iso9660->wbuff_offset) {
  3379. /*
  3380. * Write out waiting data.
  3381. */
  3382. if (used > 0) {
  3383. if (wb_write_out(a) != ARCHIVE_OK)
  3384. return (ARCHIVE_FATAL);
  3385. }
  3386. lseek(iso9660->temp_fd, off, SEEK_SET);
  3387. iso9660->wbuff_offset = off;
  3388. iso9660->wbuff_remaining = sizeof(iso9660->wbuff);
  3389. } else if (off <= iso9660->wbuff_tail) {
  3390. iso9660->wbuff_remaining = (size_t)
  3391. (sizeof(iso9660->wbuff) - (off - iso9660->wbuff_offset));
  3392. } else {
  3393. ext_bytes = off - iso9660->wbuff_tail;
  3394. iso9660->wbuff_remaining = (size_t)(sizeof(iso9660->wbuff)
  3395. - (iso9660->wbuff_tail - iso9660->wbuff_offset));
  3396. while (ext_bytes >= (int64_t)iso9660->wbuff_remaining) {
  3397. if (write_null(a, (size_t)iso9660->wbuff_remaining)
  3398. != ARCHIVE_OK)
  3399. return (ARCHIVE_FATAL);
  3400. ext_bytes -= iso9660->wbuff_remaining;
  3401. }
  3402. if (ext_bytes > 0) {
  3403. if (write_null(a, (size_t)ext_bytes) != ARCHIVE_OK)
  3404. return (ARCHIVE_FATAL);
  3405. }
  3406. }
  3407. return (ARCHIVE_OK);
  3408. }
  3409. #endif /* HAVE_ZLIB_H */
  3410. static int
  3411. write_null(struct archive_write *a, size_t size)
  3412. {
  3413. size_t remaining;
  3414. unsigned char *p, *old;
  3415. int r;
  3416. remaining = wb_remaining(a);
  3417. p = wb_buffptr(a);
  3418. if (size <= remaining) {
  3419. memset(p, 0, size);
  3420. return (wb_consume(a, size));
  3421. }
  3422. memset(p, 0, remaining);
  3423. r = wb_consume(a, remaining);
  3424. if (r != ARCHIVE_OK)
  3425. return (r);
  3426. size -= remaining;
  3427. old = p;
  3428. p = wb_buffptr(a);
  3429. memset(p, 0, old - p);
  3430. remaining = wb_remaining(a);
  3431. while (size) {
  3432. size_t wsize = size;
  3433. if (wsize > remaining)
  3434. wsize = remaining;
  3435. r = wb_consume(a, wsize);
  3436. if (r != ARCHIVE_OK)
  3437. return (r);
  3438. size -= wsize;
  3439. }
  3440. return (ARCHIVE_OK);
  3441. }
  3442. /*
  3443. * Write Volume Descriptor Set Terminator
  3444. */
  3445. static int
  3446. write_VD_terminator(struct archive_write *a)
  3447. {
  3448. unsigned char *bp;
  3449. bp = wb_buffptr(a) -1;
  3450. set_VD_bp(bp, VDT_TERMINATOR, 1);
  3451. set_unused_field_bp(bp, 8, LOGICAL_BLOCK_SIZE);
  3452. return (wb_consume(a, LOGICAL_BLOCK_SIZE));
  3453. }
  3454. static int
  3455. set_file_identifier(unsigned char *bp, int from, int to, enum vdc vdc,
  3456. struct archive_write *a, struct vdd *vdd, struct archive_string *id,
  3457. const char *label, int leading_under, enum char_type char_type)
  3458. {
  3459. char identifier[256];
  3460. struct isoent *isoent;
  3461. const char *ids;
  3462. size_t len;
  3463. int r;
  3464. if (id->length > 0 && leading_under && id->s[0] != '_') {
  3465. if (char_type == A_CHAR)
  3466. r = set_str_a_characters_bp(a, bp, from, to, id->s, vdc);
  3467. else
  3468. r = set_str_d_characters_bp(a, bp, from, to, id->s, vdc);
  3469. } else if (id->length > 0) {
  3470. ids = id->s;
  3471. if (leading_under)
  3472. ids++;
  3473. isoent = isoent_find_entry(vdd->rootent, ids);
  3474. if (isoent == NULL) {
  3475. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3476. "Not Found %s `%s'.",
  3477. label, ids);
  3478. return (ARCHIVE_FATAL);
  3479. }
  3480. len = isoent->ext_off + isoent->ext_len;
  3481. if (vdd->vdd_type == VDD_JOLIET) {
  3482. if (len > sizeof(identifier)-2)
  3483. len = sizeof(identifier)-2;
  3484. } else {
  3485. if (len > sizeof(identifier)-1)
  3486. len = sizeof(identifier)-1;
  3487. }
  3488. memcpy(identifier, isoent->identifier, len);
  3489. identifier[len] = '\0';
  3490. if (vdd->vdd_type == VDD_JOLIET) {
  3491. identifier[len+1] = 0;
  3492. vdc = VDC_UCS2_DIRECT;
  3493. }
  3494. if (char_type == A_CHAR)
  3495. r = set_str_a_characters_bp(a, bp, from, to,
  3496. identifier, vdc);
  3497. else
  3498. r = set_str_d_characters_bp(a, bp, from, to,
  3499. identifier, vdc);
  3500. } else {
  3501. if (char_type == A_CHAR)
  3502. r = set_str_a_characters_bp(a, bp, from, to, NULL, vdc);
  3503. else
  3504. r = set_str_d_characters_bp(a, bp, from, to, NULL, vdc);
  3505. }
  3506. return (r);
  3507. }
  3508. /*
  3509. * Write Primary/Supplementary Volume Descriptor
  3510. */
  3511. static int
  3512. write_VD(struct archive_write *a, struct vdd *vdd)
  3513. {
  3514. struct iso9660 *iso9660;
  3515. unsigned char *bp;
  3516. uint16_t volume_set_size = 1;
  3517. char identifier[256];
  3518. enum VD_type vdt;
  3519. enum vdc vdc;
  3520. unsigned char vd_ver, fst_ver;
  3521. int r;
  3522. iso9660 = a->format_data;
  3523. switch (vdd->vdd_type) {
  3524. case VDD_JOLIET:
  3525. vdt = VDT_SUPPLEMENTARY;
  3526. vd_ver = fst_ver = 1;
  3527. vdc = VDC_UCS2;
  3528. break;
  3529. case VDD_ENHANCED:
  3530. vdt = VDT_SUPPLEMENTARY;
  3531. vd_ver = fst_ver = 2;
  3532. vdc = VDC_LOWERCASE;
  3533. break;
  3534. case VDD_PRIMARY:
  3535. default:
  3536. vdt = VDT_PRIMARY;
  3537. vd_ver = fst_ver = 1;
  3538. #ifdef COMPAT_MKISOFS
  3539. vdc = VDC_LOWERCASE;
  3540. #else
  3541. vdc = VDC_STD;
  3542. #endif
  3543. break;
  3544. }
  3545. bp = wb_buffptr(a) -1;
  3546. /* Volume Descriptor Type */
  3547. set_VD_bp(bp, vdt, vd_ver);
  3548. /* Unused Field */
  3549. set_unused_field_bp(bp, 8, 8);
  3550. /* System Identifier */
  3551. get_system_identitier(identifier, sizeof(identifier));
  3552. r = set_str_a_characters_bp(a, bp, 9, 40, identifier, vdc);
  3553. if (r != ARCHIVE_OK)
  3554. return (r);
  3555. /* Volume Identifier */
  3556. r = set_str_d_characters_bp(a, bp, 41, 72,
  3557. iso9660->volume_identifier.s, vdc);
  3558. if (r != ARCHIVE_OK)
  3559. return (r);
  3560. /* Unused Field */
  3561. set_unused_field_bp(bp, 73, 80);
  3562. /* Volume Space Size */
  3563. set_num_733(bp+81, iso9660->volume_space_size);
  3564. if (vdd->vdd_type == VDD_JOLIET) {
  3565. /* Escape Sequences */
  3566. bp[89] = 0x25;/* UCS-2 Level 3 */
  3567. bp[90] = 0x2F;
  3568. bp[91] = 0x45;
  3569. memset(bp + 92, 0, 120 - 92 + 1);
  3570. } else {
  3571. /* Unused Field */
  3572. set_unused_field_bp(bp, 89, 120);
  3573. }
  3574. /* Volume Set Size */
  3575. set_num_723(bp+121, volume_set_size);
  3576. /* Volume Sequence Number */
  3577. set_num_723(bp+125, iso9660->volume_sequence_number);
  3578. /* Logical Block Size */
  3579. set_num_723(bp+129, LOGICAL_BLOCK_SIZE);
  3580. /* Path Table Size */
  3581. set_num_733(bp+133, vdd->path_table_size);
  3582. /* Location of Occurrence of Type L Path Table */
  3583. set_num_731(bp+141, vdd->location_type_L_path_table);
  3584. /* Location of Optional Occurrence of Type L Path Table */
  3585. set_num_731(bp+145, 0);
  3586. /* Location of Occurrence of Type M Path Table */
  3587. set_num_732(bp+149, vdd->location_type_M_path_table);
  3588. /* Location of Optional Occurrence of Type M Path Table */
  3589. set_num_732(bp+153, 0);
  3590. /* Directory Record for Root Directory(BP 157 to 190) */
  3591. set_directory_record(bp+157, 190-157+1, vdd->rootent,
  3592. iso9660, DIR_REC_VD, vdd->vdd_type);
  3593. /* Volume Set Identifier */
  3594. r = set_str_d_characters_bp(a, bp, 191, 318, "", vdc);
  3595. if (r != ARCHIVE_OK)
  3596. return (r);
  3597. /* Publisher Identifier */
  3598. r = set_file_identifier(bp, 319, 446, vdc, a, vdd,
  3599. &(iso9660->publisher_identifier),
  3600. "Publisher File", 1, A_CHAR);
  3601. if (r != ARCHIVE_OK)
  3602. return (r);
  3603. /* Data Preparer Identifier */
  3604. r = set_file_identifier(bp, 447, 574, vdc, a, vdd,
  3605. &(iso9660->data_preparer_identifier),
  3606. "Data Preparer File", 1, A_CHAR);
  3607. if (r != ARCHIVE_OK)
  3608. return (r);
  3609. /* Application Identifier */
  3610. r = set_file_identifier(bp, 575, 702, vdc, a, vdd,
  3611. &(iso9660->application_identifier),
  3612. "Application File", 1, A_CHAR);
  3613. if (r != ARCHIVE_OK)
  3614. return (r);
  3615. /* Copyright File Identifier */
  3616. r = set_file_identifier(bp, 703, 739, vdc, a, vdd,
  3617. &(iso9660->copyright_file_identifier),
  3618. "Copyright File", 0, D_CHAR);
  3619. if (r != ARCHIVE_OK)
  3620. return (r);
  3621. /* Abstract File Identifier */
  3622. r = set_file_identifier(bp, 740, 776, vdc, a, vdd,
  3623. &(iso9660->abstract_file_identifier),
  3624. "Abstract File", 0, D_CHAR);
  3625. if (r != ARCHIVE_OK)
  3626. return (r);
  3627. /* Bibliographic File Identifier */
  3628. r = set_file_identifier(bp, 777, 813, vdc, a, vdd,
  3629. &(iso9660->bibliographic_file_identifier),
  3630. "Bibliongraphic File", 0, D_CHAR);
  3631. if (r != ARCHIVE_OK)
  3632. return (r);
  3633. /* Volume Creation Date and Time */
  3634. set_date_time(bp+814, iso9660->birth_time);
  3635. /* Volume Modification Date and Time */
  3636. set_date_time(bp+831, iso9660->birth_time);
  3637. /* Volume Expiration Date and Time(obsolete) */
  3638. set_date_time_null(bp+848);
  3639. /* Volume Effective Date and Time */
  3640. set_date_time(bp+865, iso9660->birth_time);
  3641. /* File Structure Version */
  3642. bp[882] = fst_ver;
  3643. /* Reserved */
  3644. bp[883] = 0;
  3645. /* Application Use */
  3646. memset(bp + 884, 0x20, 1395 - 884 + 1);
  3647. /* Reserved */
  3648. set_unused_field_bp(bp, 1396, LOGICAL_BLOCK_SIZE);
  3649. return (wb_consume(a, LOGICAL_BLOCK_SIZE));
  3650. }
  3651. /*
  3652. * Write Boot Record Volume Descriptor
  3653. */
  3654. static int
  3655. write_VD_boot_record(struct archive_write *a)
  3656. {
  3657. struct iso9660 *iso9660;
  3658. unsigned char *bp;
  3659. iso9660 = a->format_data;
  3660. bp = wb_buffptr(a) -1;
  3661. /* Volume Descriptor Type */
  3662. set_VD_bp(bp, VDT_BOOT_RECORD, 1);
  3663. /* Boot System Identifier */
  3664. memcpy(bp+8, "EL TORITO SPECIFICATION", 23);
  3665. set_unused_field_bp(bp, 8+23, 39);
  3666. /* Unused */
  3667. set_unused_field_bp(bp, 40, 71);
  3668. /* Absolute pointer to first sector of Boot Catalog */
  3669. set_num_731(bp+72,
  3670. iso9660->el_torito.catalog->file->content.location);
  3671. /* Unused */
  3672. set_unused_field_bp(bp, 76, LOGICAL_BLOCK_SIZE);
  3673. return (wb_consume(a, LOGICAL_BLOCK_SIZE));
  3674. }
  3675. enum keytype {
  3676. KEY_FLG,
  3677. KEY_STR,
  3678. KEY_INT,
  3679. KEY_HEX
  3680. };
  3681. static void
  3682. set_option_info(struct archive_string *info, int *opt, const char *key,
  3683. enum keytype type, ...)
  3684. {
  3685. va_list ap;
  3686. char prefix;
  3687. const char *s;
  3688. int d;
  3689. prefix = (*opt==0)? ' ':',';
  3690. va_start(ap, type);
  3691. switch (type) {
  3692. case KEY_FLG:
  3693. d = va_arg(ap, int);
  3694. archive_string_sprintf(info, "%c%s%s",
  3695. prefix, (d == 0)?"!":"", key);
  3696. break;
  3697. case KEY_STR:
  3698. s = va_arg(ap, const char *);
  3699. archive_string_sprintf(info, "%c%s=%s",
  3700. prefix, key, s);
  3701. break;
  3702. case KEY_INT:
  3703. d = va_arg(ap, int);
  3704. archive_string_sprintf(info, "%c%s=%d",
  3705. prefix, key, d);
  3706. break;
  3707. case KEY_HEX:
  3708. d = va_arg(ap, int);
  3709. archive_string_sprintf(info, "%c%s=%x",
  3710. prefix, key, d);
  3711. break;
  3712. }
  3713. va_end(ap);
  3714. *opt = 1;
  3715. }
  3716. /*
  3717. * Make Non-ISO File System Information
  3718. */
  3719. static int
  3720. write_information_block(struct archive_write *a)
  3721. {
  3722. struct iso9660 *iso9660;
  3723. char buf[128];
  3724. const char *v;
  3725. int opt, r;
  3726. struct archive_string info;
  3727. size_t info_size = LOGICAL_BLOCK_SIZE *
  3728. NON_ISO_FILE_SYSTEM_INFORMATION_BLOCK;
  3729. iso9660 = (struct iso9660 *)a->format_data;
  3730. if (info_size > wb_remaining(a)) {
  3731. r = wb_write_out(a);
  3732. if (r != ARCHIVE_OK)
  3733. return (r);
  3734. }
  3735. archive_string_init(&info);
  3736. if (archive_string_ensure(&info, info_size) == NULL) {
  3737. archive_set_error(&a->archive, ENOMEM,
  3738. "Can't allocate memory");
  3739. return (ARCHIVE_FATAL);
  3740. }
  3741. memset(info.s, 0, info_size);
  3742. opt = 0;
  3743. #if defined(HAVE__CTIME64_S)
  3744. {
  3745. __time64_t iso9660_birth_time_tmp = (__time64_t) iso9660->birth_time; //time_t may be shorter than 64 bits
  3746. _ctime64_s(buf, sizeof(buf), &(iso9660_birth_time_tmp));
  3747. }
  3748. #elif defined(HAVE_CTIME_R)
  3749. ctime_r(&(iso9660->birth_time), buf);
  3750. #else
  3751. strncpy(buf, ctime(&(iso9660->birth_time)), sizeof(buf)-1);
  3752. buf[sizeof(buf)-1] = '\0';
  3753. #endif
  3754. archive_string_sprintf(&info,
  3755. "INFO %s%s", buf, archive_version_string());
  3756. if (iso9660->opt.abstract_file != OPT_ABSTRACT_FILE_DEFAULT)
  3757. set_option_info(&info, &opt, "abstract-file",
  3758. KEY_STR, iso9660->abstract_file_identifier.s);
  3759. if (iso9660->opt.application_id != OPT_APPLICATION_ID_DEFAULT)
  3760. set_option_info(&info, &opt, "application-id",
  3761. KEY_STR, iso9660->application_identifier.s);
  3762. if (iso9660->opt.allow_vernum != OPT_ALLOW_VERNUM_DEFAULT)
  3763. set_option_info(&info, &opt, "allow-vernum",
  3764. KEY_FLG, iso9660->opt.allow_vernum);
  3765. if (iso9660->opt.biblio_file != OPT_BIBLIO_FILE_DEFAULT)
  3766. set_option_info(&info, &opt, "biblio-file",
  3767. KEY_STR, iso9660->bibliographic_file_identifier.s);
  3768. if (iso9660->opt.boot != OPT_BOOT_DEFAULT)
  3769. set_option_info(&info, &opt, "boot",
  3770. KEY_STR, iso9660->el_torito.boot_filename.s);
  3771. if (iso9660->opt.boot_catalog != OPT_BOOT_CATALOG_DEFAULT)
  3772. set_option_info(&info, &opt, "boot-catalog",
  3773. KEY_STR, iso9660->el_torito.catalog_filename.s);
  3774. if (iso9660->opt.boot_info_table != OPT_BOOT_INFO_TABLE_DEFAULT)
  3775. set_option_info(&info, &opt, "boot-info-table",
  3776. KEY_FLG, iso9660->opt.boot_info_table);
  3777. if (iso9660->opt.boot_load_seg != OPT_BOOT_LOAD_SEG_DEFAULT)
  3778. set_option_info(&info, &opt, "boot-load-seg",
  3779. KEY_HEX, iso9660->el_torito.boot_load_seg);
  3780. if (iso9660->opt.boot_load_size != OPT_BOOT_LOAD_SIZE_DEFAULT)
  3781. set_option_info(&info, &opt, "boot-load-size",
  3782. KEY_INT, iso9660->el_torito.boot_load_size);
  3783. if (iso9660->opt.boot_type != OPT_BOOT_TYPE_DEFAULT) {
  3784. v = "no-emulation";
  3785. if (iso9660->opt.boot_type == OPT_BOOT_TYPE_FD)
  3786. v = "fd";
  3787. if (iso9660->opt.boot_type == OPT_BOOT_TYPE_HARD_DISK)
  3788. v = "hard-disk";
  3789. set_option_info(&info, &opt, "boot-type",
  3790. KEY_STR, v);
  3791. }
  3792. #ifdef HAVE_ZLIB_H
  3793. if (iso9660->opt.compression_level != OPT_COMPRESSION_LEVEL_DEFAULT)
  3794. set_option_info(&info, &opt, "compression-level",
  3795. KEY_INT, iso9660->zisofs.compression_level);
  3796. #endif
  3797. if (iso9660->opt.copyright_file != OPT_COPYRIGHT_FILE_DEFAULT)
  3798. set_option_info(&info, &opt, "copyright-file",
  3799. KEY_STR, iso9660->copyright_file_identifier.s);
  3800. if (iso9660->opt.iso_level != OPT_ISO_LEVEL_DEFAULT)
  3801. set_option_info(&info, &opt, "iso-level",
  3802. KEY_INT, iso9660->opt.iso_level);
  3803. if (iso9660->opt.joliet != OPT_JOLIET_DEFAULT) {
  3804. if (iso9660->opt.joliet == OPT_JOLIET_LONGNAME)
  3805. set_option_info(&info, &opt, "joliet",
  3806. KEY_STR, "long");
  3807. else
  3808. set_option_info(&info, &opt, "joliet",
  3809. KEY_FLG, iso9660->opt.joliet);
  3810. }
  3811. if (iso9660->opt.limit_depth != OPT_LIMIT_DEPTH_DEFAULT)
  3812. set_option_info(&info, &opt, "limit-depth",
  3813. KEY_FLG, iso9660->opt.limit_depth);
  3814. if (iso9660->opt.limit_dirs != OPT_LIMIT_DIRS_DEFAULT)
  3815. set_option_info(&info, &opt, "limit-dirs",
  3816. KEY_FLG, iso9660->opt.limit_dirs);
  3817. if (iso9660->opt.pad != OPT_PAD_DEFAULT)
  3818. set_option_info(&info, &opt, "pad",
  3819. KEY_FLG, iso9660->opt.pad);
  3820. if (iso9660->opt.publisher != OPT_PUBLISHER_DEFAULT)
  3821. set_option_info(&info, &opt, "publisher",
  3822. KEY_STR, iso9660->publisher_identifier.s);
  3823. if (iso9660->opt.rr != OPT_RR_DEFAULT) {
  3824. if (iso9660->opt.rr == OPT_RR_DISABLED)
  3825. set_option_info(&info, &opt, "rockridge",
  3826. KEY_FLG, iso9660->opt.rr);
  3827. else if (iso9660->opt.rr == OPT_RR_STRICT)
  3828. set_option_info(&info, &opt, "rockridge",
  3829. KEY_STR, "strict");
  3830. else if (iso9660->opt.rr == OPT_RR_USEFUL)
  3831. set_option_info(&info, &opt, "rockridge",
  3832. KEY_STR, "useful");
  3833. }
  3834. if (iso9660->opt.volume_id != OPT_VOLUME_ID_DEFAULT)
  3835. set_option_info(&info, &opt, "volume-id",
  3836. KEY_STR, iso9660->volume_identifier.s);
  3837. if (iso9660->opt.zisofs != OPT_ZISOFS_DEFAULT)
  3838. set_option_info(&info, &opt, "zisofs",
  3839. KEY_FLG, iso9660->opt.zisofs);
  3840. memcpy(wb_buffptr(a), info.s, info_size);
  3841. archive_string_free(&info);
  3842. return (wb_consume(a, info_size));
  3843. }
  3844. static int
  3845. write_rr_ER(struct archive_write *a)
  3846. {
  3847. unsigned char *p;
  3848. p = wb_buffptr(a);
  3849. memset(p, 0, LOGICAL_BLOCK_SIZE);
  3850. p[0] = 'E';
  3851. p[1] = 'R';
  3852. p[3] = 0x01;
  3853. p[2] = RRIP_ER_SIZE;
  3854. p[4] = RRIP_ER_ID_SIZE;
  3855. p[5] = RRIP_ER_DSC_SIZE;
  3856. p[6] = RRIP_ER_SRC_SIZE;
  3857. p[7] = 0x01;
  3858. memcpy(&p[8], rrip_identifier, p[4]);
  3859. memcpy(&p[8+p[4]], rrip_descriptor, p[5]);
  3860. memcpy(&p[8+p[4]+p[5]], rrip_source, p[6]);
  3861. return (wb_consume(a, LOGICAL_BLOCK_SIZE));
  3862. }
  3863. static void
  3864. calculate_path_table_size(struct vdd *vdd)
  3865. {
  3866. int depth, size;
  3867. struct path_table *pt;
  3868. pt = vdd->pathtbl;
  3869. size = 0;
  3870. for (depth = 0; depth < vdd->max_depth; depth++) {
  3871. struct isoent **ptbl;
  3872. int i, cnt;
  3873. if ((cnt = pt[depth].cnt) == 0)
  3874. break;
  3875. ptbl = pt[depth].sorted;
  3876. for (i = 0; i < cnt; i++) {
  3877. int len;
  3878. if (ptbl[i]->identifier == NULL)
  3879. len = 1; /* root directory */
  3880. else
  3881. len = ptbl[i]->id_len;
  3882. if (len & 0x01)
  3883. len++; /* Padding Field */
  3884. size += 8 + len;
  3885. }
  3886. }
  3887. vdd->path_table_size = size;
  3888. vdd->path_table_block =
  3889. ((size + PATH_TABLE_BLOCK_SIZE -1) /
  3890. PATH_TABLE_BLOCK_SIZE) *
  3891. (PATH_TABLE_BLOCK_SIZE / LOGICAL_BLOCK_SIZE);
  3892. }
  3893. static int
  3894. _write_path_table(struct archive_write *a, int type_m, int depth,
  3895. struct vdd *vdd)
  3896. {
  3897. unsigned char *bp, *wb;
  3898. struct isoent **ptbl;
  3899. size_t wbremaining;
  3900. int i, r, wsize;
  3901. if (vdd->pathtbl[depth].cnt == 0)
  3902. return (0);
  3903. wsize = 0;
  3904. wb = wb_buffptr(a);
  3905. wbremaining = wb_remaining(a);
  3906. bp = wb - 1;
  3907. ptbl = vdd->pathtbl[depth].sorted;
  3908. for (i = 0; i < vdd->pathtbl[depth].cnt; i++) {
  3909. struct isoent *np;
  3910. size_t len;
  3911. np = ptbl[i];
  3912. if (np->identifier == NULL)
  3913. len = 1; /* root directory */
  3914. else
  3915. len = np->id_len;
  3916. if (wbremaining - ((bp+1) - wb) < (len + 1 + 8)) {
  3917. r = wb_consume(a, (bp+1) - wb);
  3918. if (r < 0)
  3919. return (r);
  3920. wb = wb_buffptr(a);
  3921. wbremaining = wb_remaining(a);
  3922. bp = wb -1;
  3923. }
  3924. /* Length of Directory Identifier */
  3925. set_num_711(bp+1, (unsigned char)len);
  3926. /* Extended Attribute Record Length */
  3927. set_num_711(bp+2, 0);
  3928. /* Location of Extent */
  3929. if (type_m)
  3930. set_num_732(bp+3, np->dir_location);
  3931. else
  3932. set_num_731(bp+3, np->dir_location);
  3933. /* Parent Directory Number */
  3934. if (type_m)
  3935. set_num_722(bp+7, np->parent->dir_number);
  3936. else
  3937. set_num_721(bp+7, np->parent->dir_number);
  3938. /* Directory Identifier */
  3939. if (np->identifier == NULL)
  3940. bp[9] = 0;
  3941. else
  3942. memcpy(&bp[9], np->identifier, len);
  3943. if (len & 0x01) {
  3944. /* Padding Field */
  3945. bp[9+len] = 0;
  3946. len++;
  3947. }
  3948. wsize += 8 + (int)len;
  3949. bp += 8 + len;
  3950. }
  3951. if ((bp + 1) > wb) {
  3952. r = wb_consume(a, (bp+1)-wb);
  3953. if (r < 0)
  3954. return (r);
  3955. }
  3956. return (wsize);
  3957. }
  3958. static int
  3959. write_path_table(struct archive_write *a, int type_m, struct vdd *vdd)
  3960. {
  3961. int depth, r;
  3962. size_t path_table_size;
  3963. r = ARCHIVE_OK;
  3964. path_table_size = 0;
  3965. for (depth = 0; depth < vdd->max_depth; depth++) {
  3966. r = _write_path_table(a, type_m, depth, vdd);
  3967. if (r < 0)
  3968. return (r);
  3969. path_table_size += r;
  3970. }
  3971. /* Write padding data. */
  3972. path_table_size = path_table_size % PATH_TABLE_BLOCK_SIZE;
  3973. if (path_table_size > 0)
  3974. r = write_null(a, PATH_TABLE_BLOCK_SIZE - path_table_size);
  3975. return (r);
  3976. }
  3977. static int
  3978. calculate_directory_descriptors(struct iso9660 *iso9660, struct vdd *vdd,
  3979. struct isoent *isoent, int depth)
  3980. {
  3981. struct isoent **enttbl;
  3982. int bs, block, i;
  3983. block = 1;
  3984. bs = get_dir_rec_size(iso9660, isoent, DIR_REC_SELF, vdd->vdd_type);
  3985. bs += get_dir_rec_size(iso9660, isoent, DIR_REC_PARENT, vdd->vdd_type);
  3986. if (isoent->children.cnt <= 0 || (vdd->vdd_type != VDD_JOLIET &&
  3987. !iso9660->opt.rr && depth + 1 >= vdd->max_depth))
  3988. return (block);
  3989. enttbl = isoent->children_sorted;
  3990. for (i = 0; i < isoent->children.cnt; i++) {
  3991. struct isoent *np = enttbl[i];
  3992. struct isofile *file;
  3993. file = np->file;
  3994. if (file->hardlink_target != NULL)
  3995. file = file->hardlink_target;
  3996. file->cur_content = &(file->content);
  3997. do {
  3998. int dr_l;
  3999. dr_l = get_dir_rec_size(iso9660, np, DIR_REC_NORMAL,
  4000. vdd->vdd_type);
  4001. if ((bs + dr_l) > LOGICAL_BLOCK_SIZE) {
  4002. block ++;
  4003. bs = dr_l;
  4004. } else
  4005. bs += dr_l;
  4006. file->cur_content = file->cur_content->next;
  4007. } while (file->cur_content != NULL);
  4008. }
  4009. return (block);
  4010. }
  4011. static int
  4012. _write_directory_descriptors(struct archive_write *a, struct vdd *vdd,
  4013. struct isoent *isoent, int depth)
  4014. {
  4015. struct iso9660 *iso9660 = a->format_data;
  4016. struct isoent **enttbl;
  4017. unsigned char *p, *wb;
  4018. int i, r;
  4019. int dr_l;
  4020. p = wb = wb_buffptr(a);
  4021. #define WD_REMAINING (LOGICAL_BLOCK_SIZE - (p - wb))
  4022. p += set_directory_record(p, WD_REMAINING, isoent,
  4023. iso9660, DIR_REC_SELF, vdd->vdd_type);
  4024. p += set_directory_record(p, WD_REMAINING, isoent,
  4025. iso9660, DIR_REC_PARENT, vdd->vdd_type);
  4026. if (isoent->children.cnt <= 0 || (vdd->vdd_type != VDD_JOLIET &&
  4027. !iso9660->opt.rr && depth + 1 >= vdd->max_depth)) {
  4028. memset(p, 0, WD_REMAINING);
  4029. return (wb_consume(a, LOGICAL_BLOCK_SIZE));
  4030. }
  4031. enttbl = isoent->children_sorted;
  4032. for (i = 0; i < isoent->children.cnt; i++) {
  4033. struct isoent *np = enttbl[i];
  4034. struct isofile *file = np->file;
  4035. if (file->hardlink_target != NULL)
  4036. file = file->hardlink_target;
  4037. file->cur_content = &(file->content);
  4038. do {
  4039. dr_l = set_directory_record(p, WD_REMAINING,
  4040. np, iso9660, DIR_REC_NORMAL,
  4041. vdd->vdd_type);
  4042. if (dr_l == 0) {
  4043. memset(p, 0, WD_REMAINING);
  4044. r = wb_consume(a, LOGICAL_BLOCK_SIZE);
  4045. if (r < 0)
  4046. return (r);
  4047. p = wb = wb_buffptr(a);
  4048. dr_l = set_directory_record(p,
  4049. WD_REMAINING, np, iso9660,
  4050. DIR_REC_NORMAL, vdd->vdd_type);
  4051. }
  4052. p += dr_l;
  4053. file->cur_content = file->cur_content->next;
  4054. } while (file->cur_content != NULL);
  4055. }
  4056. memset(p, 0, WD_REMAINING);
  4057. return (wb_consume(a, LOGICAL_BLOCK_SIZE));
  4058. }
  4059. static int
  4060. write_directory_descriptors(struct archive_write *a, struct vdd *vdd)
  4061. {
  4062. struct isoent *np;
  4063. int depth, r;
  4064. depth = 0;
  4065. np = vdd->rootent;
  4066. do {
  4067. struct extr_rec *extr;
  4068. r = _write_directory_descriptors(a, vdd, np, depth);
  4069. if (r < 0)
  4070. return (r);
  4071. if (vdd->vdd_type != VDD_JOLIET) {
  4072. /*
  4073. * This extract record is used by SUSP,RRIP.
  4074. * Not for joliet.
  4075. */
  4076. for (extr = np->extr_rec_list.first;
  4077. extr != NULL;
  4078. extr = extr->next) {
  4079. unsigned char *wb;
  4080. wb = wb_buffptr(a);
  4081. memcpy(wb, extr->buf, extr->offset);
  4082. memset(wb + extr->offset, 0,
  4083. LOGICAL_BLOCK_SIZE - extr->offset);
  4084. r = wb_consume(a, LOGICAL_BLOCK_SIZE);
  4085. if (r < 0)
  4086. return (r);
  4087. }
  4088. }
  4089. if (np->subdirs.first != NULL && depth + 1 < vdd->max_depth) {
  4090. /* Enter to sub directories. */
  4091. np = np->subdirs.first;
  4092. depth++;
  4093. continue;
  4094. }
  4095. while (np != np->parent) {
  4096. if (np->drnext == NULL) {
  4097. /* Return to the parent directory. */
  4098. np = np->parent;
  4099. depth--;
  4100. } else {
  4101. np = np->drnext;
  4102. break;
  4103. }
  4104. }
  4105. } while (np != np->parent);
  4106. return (ARCHIVE_OK);
  4107. }
  4108. /*
  4109. * Read file contents from the temporary file, and write it.
  4110. */
  4111. static int
  4112. write_file_contents(struct archive_write *a, int64_t offset, int64_t size)
  4113. {
  4114. struct iso9660 *iso9660 = a->format_data;
  4115. int r;
  4116. lseek(iso9660->temp_fd, offset, SEEK_SET);
  4117. while (size) {
  4118. size_t rsize;
  4119. ssize_t rs;
  4120. unsigned char *wb;
  4121. wb = wb_buffptr(a);
  4122. rsize = wb_remaining(a);
  4123. if (rsize > (size_t)size)
  4124. rsize = (size_t)size;
  4125. rs = read(iso9660->temp_fd, wb, rsize);
  4126. if (rs <= 0) {
  4127. archive_set_error(&a->archive, errno,
  4128. "Can't read temporary file(%jd)", (intmax_t)rs);
  4129. return (ARCHIVE_FATAL);
  4130. }
  4131. size -= rs;
  4132. r = wb_consume(a, rs);
  4133. if (r < 0)
  4134. return (r);
  4135. }
  4136. return (ARCHIVE_OK);
  4137. }
  4138. static int
  4139. write_file_descriptors(struct archive_write *a)
  4140. {
  4141. struct iso9660 *iso9660 = a->format_data;
  4142. struct isofile *file;
  4143. int64_t blocks, offset;
  4144. int r;
  4145. blocks = 0;
  4146. offset = 0;
  4147. /* Make the boot catalog contents, and write it. */
  4148. if (iso9660->el_torito.catalog != NULL) {
  4149. r = make_boot_catalog(a);
  4150. if (r < 0)
  4151. return (r);
  4152. }
  4153. /* Write the boot file contents. */
  4154. if (iso9660->el_torito.boot != NULL) {
  4155. file = iso9660->el_torito.boot->file;
  4156. blocks = file->content.blocks;
  4157. offset = file->content.offset_of_temp;
  4158. if (offset != 0) {
  4159. r = write_file_contents(a, offset,
  4160. blocks << LOGICAL_BLOCK_BITS);
  4161. if (r < 0)
  4162. return (r);
  4163. blocks = 0;
  4164. offset = 0;
  4165. }
  4166. }
  4167. /* Write out all file contents. */
  4168. for (file = iso9660->data_file_list.first;
  4169. file != NULL; file = file->datanext) {
  4170. if (!file->write_content)
  4171. continue;
  4172. if ((offset + (blocks << LOGICAL_BLOCK_BITS)) <
  4173. file->content.offset_of_temp) {
  4174. if (blocks > 0) {
  4175. r = write_file_contents(a, offset,
  4176. blocks << LOGICAL_BLOCK_BITS);
  4177. if (r < 0)
  4178. return (r);
  4179. }
  4180. blocks = 0;
  4181. offset = file->content.offset_of_temp;
  4182. }
  4183. file->cur_content = &(file->content);
  4184. do {
  4185. blocks += file->cur_content->blocks;
  4186. /* Next fragment */
  4187. file->cur_content = file->cur_content->next;
  4188. } while (file->cur_content != NULL);
  4189. }
  4190. /* Flush out remaining blocks. */
  4191. if (blocks > 0) {
  4192. r = write_file_contents(a, offset,
  4193. blocks << LOGICAL_BLOCK_BITS);
  4194. if (r < 0)
  4195. return (r);
  4196. }
  4197. return (ARCHIVE_OK);
  4198. }
  4199. static void
  4200. isofile_init_entry_list(struct iso9660 *iso9660)
  4201. {
  4202. iso9660->all_file_list.first = NULL;
  4203. iso9660->all_file_list.last = &(iso9660->all_file_list.first);
  4204. }
  4205. static void
  4206. isofile_add_entry(struct iso9660 *iso9660, struct isofile *file)
  4207. {
  4208. file->allnext = NULL;
  4209. *iso9660->all_file_list.last = file;
  4210. iso9660->all_file_list.last = &(file->allnext);
  4211. }
  4212. static void
  4213. isofile_free_all_entries(struct iso9660 *iso9660)
  4214. {
  4215. struct isofile *file, *file_next;
  4216. file = iso9660->all_file_list.first;
  4217. while (file != NULL) {
  4218. file_next = file->allnext;
  4219. isofile_free(file);
  4220. file = file_next;
  4221. }
  4222. }
  4223. static void
  4224. isofile_init_entry_data_file_list(struct iso9660 *iso9660)
  4225. {
  4226. iso9660->data_file_list.first = NULL;
  4227. iso9660->data_file_list.last = &(iso9660->data_file_list.first);
  4228. }
  4229. static void
  4230. isofile_add_data_file(struct iso9660 *iso9660, struct isofile *file)
  4231. {
  4232. file->datanext = NULL;
  4233. *iso9660->data_file_list.last = file;
  4234. iso9660->data_file_list.last = &(file->datanext);
  4235. }
  4236. static struct isofile *
  4237. isofile_new(struct archive_write *a, struct archive_entry *entry)
  4238. {
  4239. struct isofile *file;
  4240. file = calloc(1, sizeof(*file));
  4241. if (file == NULL)
  4242. return (NULL);
  4243. if (entry != NULL)
  4244. file->entry = archive_entry_clone(entry);
  4245. else
  4246. file->entry = archive_entry_new2(&a->archive);
  4247. if (file->entry == NULL) {
  4248. free(file);
  4249. return (NULL);
  4250. }
  4251. archive_string_init(&(file->parentdir));
  4252. archive_string_init(&(file->basename));
  4253. archive_string_init(&(file->basename_utf16));
  4254. archive_string_init(&(file->symlink));
  4255. file->cur_content = &(file->content);
  4256. return (file);
  4257. }
  4258. static void
  4259. isofile_free(struct isofile *file)
  4260. {
  4261. struct content *con, *tmp;
  4262. con = file->content.next;
  4263. while (con != NULL) {
  4264. tmp = con;
  4265. con = con->next;
  4266. free(tmp);
  4267. }
  4268. archive_entry_free(file->entry);
  4269. archive_string_free(&(file->parentdir));
  4270. archive_string_free(&(file->basename));
  4271. archive_string_free(&(file->basename_utf16));
  4272. archive_string_free(&(file->symlink));
  4273. free(file);
  4274. }
  4275. #if defined(_WIN32) || defined(__CYGWIN__)
  4276. static int
  4277. cleanup_backslash_1(char *p)
  4278. {
  4279. int mb, dos;
  4280. mb = dos = 0;
  4281. while (*p) {
  4282. if (*(unsigned char *)p > 127)
  4283. mb = 1;
  4284. if (*p == '\\') {
  4285. /* If we have not met any multi-byte characters,
  4286. * we can replace '\' with '/'. */
  4287. if (!mb)
  4288. *p = '/';
  4289. dos = 1;
  4290. }
  4291. p++;
  4292. }
  4293. if (!mb || !dos)
  4294. return (0);
  4295. return (-1);
  4296. }
  4297. static void
  4298. cleanup_backslash_2(wchar_t *p)
  4299. {
  4300. /* Convert a path-separator from '\' to '/' */
  4301. while (*p != L'\0') {
  4302. if (*p == L'\\')
  4303. *p = L'/';
  4304. p++;
  4305. }
  4306. }
  4307. #endif
  4308. /*
  4309. * Generate a parent directory name and a base name from a pathname.
  4310. */
  4311. static int
  4312. isofile_gen_utility_names(struct archive_write *a, struct isofile *file)
  4313. {
  4314. struct iso9660 *iso9660;
  4315. const char *pathname;
  4316. char *p, *dirname, *slash;
  4317. size_t len;
  4318. int ret = ARCHIVE_OK;
  4319. iso9660 = a->format_data;
  4320. archive_string_empty(&(file->parentdir));
  4321. archive_string_empty(&(file->basename));
  4322. archive_string_empty(&(file->basename_utf16));
  4323. archive_string_empty(&(file->symlink));
  4324. pathname = archive_entry_pathname(file->entry);
  4325. if (pathname == NULL || pathname[0] == '\0') {/* virtual root */
  4326. file->dircnt = 0;
  4327. return (ret);
  4328. }
  4329. /*
  4330. * Make a UTF-16BE basename if Joliet extension enabled.
  4331. */
  4332. if (iso9660->opt.joliet) {
  4333. const char *u16, *ulast;
  4334. size_t u16len, ulen_last;
  4335. if (iso9660->sconv_to_utf16be == NULL) {
  4336. iso9660->sconv_to_utf16be =
  4337. archive_string_conversion_to_charset(
  4338. &(a->archive), "UTF-16BE", 1);
  4339. if (iso9660->sconv_to_utf16be == NULL)
  4340. /* Couldn't allocate memory */
  4341. return (ARCHIVE_FATAL);
  4342. iso9660->sconv_from_utf16be =
  4343. archive_string_conversion_from_charset(
  4344. &(a->archive), "UTF-16BE", 1);
  4345. if (iso9660->sconv_from_utf16be == NULL)
  4346. /* Couldn't allocate memory */
  4347. return (ARCHIVE_FATAL);
  4348. }
  4349. /*
  4350. * Convert a filename to UTF-16BE.
  4351. */
  4352. if (0 > archive_entry_pathname_l(file->entry, &u16, &u16len,
  4353. iso9660->sconv_to_utf16be)) {
  4354. if (errno == ENOMEM) {
  4355. archive_set_error(&a->archive, ENOMEM,
  4356. "Can't allocate memory for UTF-16BE");
  4357. return (ARCHIVE_FATAL);
  4358. }
  4359. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  4360. "A filename cannot be converted to UTF-16BE;"
  4361. "You should disable making Joliet extension");
  4362. ret = ARCHIVE_WARN;
  4363. }
  4364. /*
  4365. * Make sure a path separator is not in the last;
  4366. * Remove trailing '/'.
  4367. */
  4368. while (u16len >= 2) {
  4369. #if defined(_WIN32) || defined(__CYGWIN__)
  4370. if (u16[u16len-2] == 0 &&
  4371. (u16[u16len-1] == '/' || u16[u16len-1] == '\\'))
  4372. #else
  4373. if (u16[u16len-2] == 0 && u16[u16len-1] == '/')
  4374. #endif
  4375. {
  4376. u16len -= 2;
  4377. } else
  4378. break;
  4379. }
  4380. /*
  4381. * Find a basename in UTF-16BE.
  4382. */
  4383. ulast = u16;
  4384. u16len >>= 1;
  4385. ulen_last = u16len;
  4386. while (u16len > 0) {
  4387. #if defined(_WIN32) || defined(__CYGWIN__)
  4388. if (u16[0] == 0 && (u16[1] == '/' || u16[1] == '\\'))
  4389. #else
  4390. if (u16[0] == 0 && u16[1] == '/')
  4391. #endif
  4392. {
  4393. ulast = u16 + 2;
  4394. ulen_last = u16len -1;
  4395. }
  4396. u16 += 2;
  4397. u16len --;
  4398. }
  4399. ulen_last <<= 1;
  4400. if (archive_string_ensure(&(file->basename_utf16),
  4401. ulen_last) == NULL) {
  4402. archive_set_error(&a->archive, ENOMEM,
  4403. "Can't allocate memory for UTF-16BE");
  4404. return (ARCHIVE_FATAL);
  4405. }
  4406. /*
  4407. * Set UTF-16BE basename.
  4408. */
  4409. memcpy(file->basename_utf16.s, ulast, ulen_last);
  4410. file->basename_utf16.length = ulen_last;
  4411. }
  4412. archive_strcpy(&(file->parentdir), pathname);
  4413. #if defined(_WIN32) || defined(__CYGWIN__)
  4414. /*
  4415. * Convert a path-separator from '\' to '/'
  4416. */
  4417. if (cleanup_backslash_1(file->parentdir.s) != 0) {
  4418. const wchar_t *wp = archive_entry_pathname_w(file->entry);
  4419. struct archive_wstring ws;
  4420. if (wp != NULL) {
  4421. int r;
  4422. archive_string_init(&ws);
  4423. archive_wstrcpy(&ws, wp);
  4424. cleanup_backslash_2(ws.s);
  4425. archive_string_empty(&(file->parentdir));
  4426. r = archive_string_append_from_wcs(&(file->parentdir),
  4427. ws.s, ws.length);
  4428. archive_wstring_free(&ws);
  4429. if (r < 0 && errno == ENOMEM) {
  4430. archive_set_error(&a->archive, ENOMEM,
  4431. "Can't allocate memory");
  4432. return (ARCHIVE_FATAL);
  4433. }
  4434. }
  4435. }
  4436. #endif
  4437. len = file->parentdir.length;
  4438. p = dirname = file->parentdir.s;
  4439. /*
  4440. * Remove leading '/', '../' and './' elements
  4441. */
  4442. while (*p) {
  4443. if (p[0] == '/') {
  4444. p++;
  4445. len--;
  4446. } else if (p[0] != '.')
  4447. break;
  4448. else if (p[1] == '.' && p[2] == '/') {
  4449. p += 3;
  4450. len -= 3;
  4451. } else if (p[1] == '/' || (p[1] == '.' && p[2] == '\0')) {
  4452. p += 2;
  4453. len -= 2;
  4454. } else if (p[1] == '\0') {
  4455. p++;
  4456. len--;
  4457. } else
  4458. break;
  4459. }
  4460. if (p != dirname) {
  4461. memmove(dirname, p, len+1);
  4462. p = dirname;
  4463. }
  4464. /*
  4465. * Remove "/","/." and "/.." elements from tail.
  4466. */
  4467. while (len > 0) {
  4468. size_t ll = len;
  4469. if (len > 0 && p[len-1] == '/') {
  4470. p[len-1] = '\0';
  4471. len--;
  4472. }
  4473. if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
  4474. p[len-2] = '\0';
  4475. len -= 2;
  4476. }
  4477. if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
  4478. p[len-1] == '.') {
  4479. p[len-3] = '\0';
  4480. len -= 3;
  4481. }
  4482. if (ll == len)
  4483. break;
  4484. }
  4485. while (*p) {
  4486. if (p[0] == '/') {
  4487. if (p[1] == '/')
  4488. /* Convert '//' --> '/' */
  4489. memmove(p, p+1, strlen(p+1) + 1);
  4490. else if (p[1] == '.' && p[2] == '/')
  4491. /* Convert '/./' --> '/' */
  4492. memmove(p, p+2, strlen(p+2) + 1);
  4493. else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
  4494. /* Convert 'dir/dir1/../dir2/'
  4495. * --> 'dir/dir2/'
  4496. */
  4497. char *rp = p -1;
  4498. while (rp >= dirname) {
  4499. if (*rp == '/')
  4500. break;
  4501. --rp;
  4502. }
  4503. if (rp > dirname) {
  4504. strcpy(rp, p+3);
  4505. p = rp;
  4506. } else {
  4507. strcpy(dirname, p+4);
  4508. p = dirname;
  4509. }
  4510. } else
  4511. p++;
  4512. } else
  4513. p++;
  4514. }
  4515. p = dirname;
  4516. len = strlen(p);
  4517. if (archive_entry_filetype(file->entry) == AE_IFLNK) {
  4518. /* Convert symlink name too. */
  4519. pathname = archive_entry_symlink(file->entry);
  4520. archive_strcpy(&(file->symlink), pathname);
  4521. #if defined(_WIN32) || defined(__CYGWIN__)
  4522. /*
  4523. * Convert a path-separator from '\' to '/'
  4524. */
  4525. if (archive_strlen(&(file->symlink)) > 0 &&
  4526. cleanup_backslash_1(file->symlink.s) != 0) {
  4527. const wchar_t *wp =
  4528. archive_entry_symlink_w(file->entry);
  4529. struct archive_wstring ws;
  4530. if (wp != NULL) {
  4531. int r;
  4532. archive_string_init(&ws);
  4533. archive_wstrcpy(&ws, wp);
  4534. cleanup_backslash_2(ws.s);
  4535. archive_string_empty(&(file->symlink));
  4536. r = archive_string_append_from_wcs(
  4537. &(file->symlink),
  4538. ws.s, ws.length);
  4539. archive_wstring_free(&ws);
  4540. if (r < 0 && errno == ENOMEM) {
  4541. archive_set_error(&a->archive, ENOMEM,
  4542. "Can't allocate memory");
  4543. return (ARCHIVE_FATAL);
  4544. }
  4545. }
  4546. }
  4547. #endif
  4548. }
  4549. /*
  4550. * - Count up directory elements.
  4551. * - Find out the position which points the last position of
  4552. * path separator('/').
  4553. */
  4554. slash = NULL;
  4555. file->dircnt = 0;
  4556. for (; *p != '\0'; p++)
  4557. if (*p == '/') {
  4558. slash = p;
  4559. file->dircnt++;
  4560. }
  4561. if (slash == NULL) {
  4562. /* The pathname doesn't have a parent directory. */
  4563. file->parentdir.length = len;
  4564. archive_string_copy(&(file->basename), &(file->parentdir));
  4565. archive_string_empty(&(file->parentdir));
  4566. *file->parentdir.s = '\0';
  4567. return (ret);
  4568. }
  4569. /* Make a basename from dirname and slash */
  4570. *slash = '\0';
  4571. file->parentdir.length = slash - dirname;
  4572. archive_strcpy(&(file->basename), slash + 1);
  4573. if (archive_entry_filetype(file->entry) == AE_IFDIR)
  4574. file->dircnt ++;
  4575. return (ret);
  4576. }
  4577. /*
  4578. * Register a entry to get a hardlink target.
  4579. */
  4580. static int
  4581. isofile_register_hardlink(struct archive_write *a, struct isofile *file)
  4582. {
  4583. struct iso9660 *iso9660 = a->format_data;
  4584. struct hardlink *hl;
  4585. const char *pathname;
  4586. archive_entry_set_nlink(file->entry, 1);
  4587. pathname = archive_entry_hardlink(file->entry);
  4588. if (pathname == NULL) {
  4589. /* This `file` is a hardlink target. */
  4590. hl = malloc(sizeof(*hl));
  4591. if (hl == NULL) {
  4592. archive_set_error(&a->archive, ENOMEM,
  4593. "Can't allocate memory");
  4594. return (ARCHIVE_FATAL);
  4595. }
  4596. hl->nlink = 1;
  4597. /* A hardlink target must be the first position. */
  4598. file->hlnext = NULL;
  4599. hl->file_list.first = file;
  4600. hl->file_list.last = &(file->hlnext);
  4601. __archive_rb_tree_insert_node(&(iso9660->hardlink_rbtree),
  4602. (struct archive_rb_node *)hl);
  4603. } else {
  4604. hl = (struct hardlink *)__archive_rb_tree_find_node(
  4605. &(iso9660->hardlink_rbtree), pathname);
  4606. if (hl != NULL) {
  4607. /* Insert `file` entry into the tail. */
  4608. file->hlnext = NULL;
  4609. *hl->file_list.last = file;
  4610. hl->file_list.last = &(file->hlnext);
  4611. hl->nlink++;
  4612. }
  4613. archive_entry_unset_size(file->entry);
  4614. }
  4615. return (ARCHIVE_OK);
  4616. }
  4617. /*
  4618. * Hardlinked files have to have the same location of extent.
  4619. * We have to find out hardlink target entries for the entries
  4620. * which have a hardlink target name.
  4621. */
  4622. static void
  4623. isofile_connect_hardlink_files(struct iso9660 *iso9660)
  4624. {
  4625. struct archive_rb_node *n;
  4626. struct hardlink *hl;
  4627. struct isofile *target, *nf;
  4628. ARCHIVE_RB_TREE_FOREACH(n, &(iso9660->hardlink_rbtree)) {
  4629. hl = (struct hardlink *)n;
  4630. /* The first entry must be a hardlink target. */
  4631. target = hl->file_list.first;
  4632. archive_entry_set_nlink(target->entry, hl->nlink);
  4633. /* Set a hardlink target to reference entries. */
  4634. for (nf = target->hlnext;
  4635. nf != NULL; nf = nf->hlnext) {
  4636. nf->hardlink_target = target;
  4637. archive_entry_set_nlink(nf->entry, hl->nlink);
  4638. }
  4639. }
  4640. }
  4641. static int
  4642. isofile_hd_cmp_node(const struct archive_rb_node *n1,
  4643. const struct archive_rb_node *n2)
  4644. {
  4645. const struct hardlink *h1 = (const struct hardlink *)n1;
  4646. const struct hardlink *h2 = (const struct hardlink *)n2;
  4647. return (strcmp(archive_entry_pathname(h1->file_list.first->entry),
  4648. archive_entry_pathname(h2->file_list.first->entry)));
  4649. }
  4650. static int
  4651. isofile_hd_cmp_key(const struct archive_rb_node *n, const void *key)
  4652. {
  4653. const struct hardlink *h = (const struct hardlink *)n;
  4654. return (strcmp(archive_entry_pathname(h->file_list.first->entry),
  4655. (const char *)key));
  4656. }
  4657. static void
  4658. isofile_init_hardlinks(struct iso9660 *iso9660)
  4659. {
  4660. static const struct archive_rb_tree_ops rb_ops = {
  4661. isofile_hd_cmp_node, isofile_hd_cmp_key,
  4662. };
  4663. __archive_rb_tree_init(&(iso9660->hardlink_rbtree), &rb_ops);
  4664. }
  4665. static void
  4666. isofile_free_hardlinks(struct iso9660 *iso9660)
  4667. {
  4668. struct archive_rb_node *n, *tmp;
  4669. ARCHIVE_RB_TREE_FOREACH_SAFE(n, &(iso9660->hardlink_rbtree), tmp) {
  4670. __archive_rb_tree_remove_node(&(iso9660->hardlink_rbtree), n);
  4671. free(n);
  4672. }
  4673. }
  4674. static struct isoent *
  4675. isoent_new(struct isofile *file)
  4676. {
  4677. struct isoent *isoent;
  4678. static const struct archive_rb_tree_ops rb_ops = {
  4679. isoent_cmp_node, isoent_cmp_key,
  4680. };
  4681. isoent = calloc(1, sizeof(*isoent));
  4682. if (isoent == NULL)
  4683. return (NULL);
  4684. isoent->file = file;
  4685. isoent->children.first = NULL;
  4686. isoent->children.last = &(isoent->children.first);
  4687. __archive_rb_tree_init(&(isoent->rbtree), &rb_ops);
  4688. isoent->subdirs.first = NULL;
  4689. isoent->subdirs.last = &(isoent->subdirs.first);
  4690. isoent->extr_rec_list.first = NULL;
  4691. isoent->extr_rec_list.last = &(isoent->extr_rec_list.first);
  4692. isoent->extr_rec_list.current = NULL;
  4693. if (archive_entry_filetype(file->entry) == AE_IFDIR)
  4694. isoent->dir = 1;
  4695. return (isoent);
  4696. }
  4697. static inline struct isoent *
  4698. isoent_clone(struct isoent *src)
  4699. {
  4700. return (isoent_new(src->file));
  4701. }
  4702. static void
  4703. _isoent_free(struct isoent *isoent)
  4704. {
  4705. struct extr_rec *er, *er_next;
  4706. free(isoent->children_sorted);
  4707. free(isoent->identifier);
  4708. er = isoent->extr_rec_list.first;
  4709. while (er != NULL) {
  4710. er_next = er->next;
  4711. free(er);
  4712. er = er_next;
  4713. }
  4714. free(isoent);
  4715. }
  4716. static void
  4717. isoent_free_all(struct isoent *isoent)
  4718. {
  4719. struct isoent *np, *np_temp;
  4720. if (isoent == NULL)
  4721. return;
  4722. np = isoent;
  4723. for (;;) {
  4724. if (np->dir) {
  4725. if (np->children.first != NULL) {
  4726. /* Enter to sub directories. */
  4727. np = np->children.first;
  4728. continue;
  4729. }
  4730. }
  4731. for (;;) {
  4732. np_temp = np;
  4733. if (np->chnext == NULL) {
  4734. /* Return to the parent directory. */
  4735. np = np->parent;
  4736. _isoent_free(np_temp);
  4737. if (np == np_temp)
  4738. return;
  4739. } else {
  4740. np = np->chnext;
  4741. _isoent_free(np_temp);
  4742. break;
  4743. }
  4744. }
  4745. }
  4746. }
  4747. static struct isoent *
  4748. isoent_create_virtual_dir(struct archive_write *a, struct iso9660 *iso9660, const char *pathname)
  4749. {
  4750. struct isofile *file;
  4751. struct isoent *isoent;
  4752. file = isofile_new(a, NULL);
  4753. if (file == NULL)
  4754. return (NULL);
  4755. archive_entry_set_pathname(file->entry, pathname);
  4756. archive_entry_unset_mtime(file->entry);
  4757. archive_entry_unset_atime(file->entry);
  4758. archive_entry_unset_ctime(file->entry);
  4759. archive_entry_set_uid(file->entry, getuid());
  4760. archive_entry_set_gid(file->entry, getgid());
  4761. archive_entry_set_mode(file->entry, 0555 | AE_IFDIR);
  4762. archive_entry_set_nlink(file->entry, 2);
  4763. if (isofile_gen_utility_names(a, file) < ARCHIVE_WARN) {
  4764. isofile_free(file);
  4765. return (NULL);
  4766. }
  4767. isofile_add_entry(iso9660, file);
  4768. isoent = isoent_new(file);
  4769. if (isoent == NULL)
  4770. return (NULL);
  4771. isoent->dir = 1;
  4772. isoent->virtual = 1;
  4773. return (isoent);
  4774. }
  4775. static int
  4776. isoent_cmp_node(const struct archive_rb_node *n1,
  4777. const struct archive_rb_node *n2)
  4778. {
  4779. const struct isoent *e1 = (const struct isoent *)n1;
  4780. const struct isoent *e2 = (const struct isoent *)n2;
  4781. return (strcmp(e1->file->basename.s, e2->file->basename.s));
  4782. }
  4783. static int
  4784. isoent_cmp_key(const struct archive_rb_node *n, const void *key)
  4785. {
  4786. const struct isoent *e = (const struct isoent *)n;
  4787. return (strcmp(e->file->basename.s, (const char *)key));
  4788. }
  4789. static int
  4790. isoent_add_child_head(struct isoent *parent, struct isoent *child)
  4791. {
  4792. if (!__archive_rb_tree_insert_node(
  4793. &(parent->rbtree), (struct archive_rb_node *)child))
  4794. return (0);
  4795. if ((child->chnext = parent->children.first) == NULL)
  4796. parent->children.last = &(child->chnext);
  4797. parent->children.first = child;
  4798. parent->children.cnt++;
  4799. child->parent = parent;
  4800. /* Add a child to a sub-directory chain */
  4801. if (child->dir) {
  4802. if ((child->drnext = parent->subdirs.first) == NULL)
  4803. parent->subdirs.last = &(child->drnext);
  4804. parent->subdirs.first = child;
  4805. parent->subdirs.cnt++;
  4806. child->parent = parent;
  4807. } else
  4808. child->drnext = NULL;
  4809. return (1);
  4810. }
  4811. static int
  4812. isoent_add_child_tail(struct isoent *parent, struct isoent *child)
  4813. {
  4814. if (!__archive_rb_tree_insert_node(
  4815. &(parent->rbtree), (struct archive_rb_node *)child))
  4816. return (0);
  4817. child->chnext = NULL;
  4818. *parent->children.last = child;
  4819. parent->children.last = &(child->chnext);
  4820. parent->children.cnt++;
  4821. child->parent = parent;
  4822. /* Add a child to a sub-directory chain */
  4823. child->drnext = NULL;
  4824. if (child->dir) {
  4825. *parent->subdirs.last = child;
  4826. parent->subdirs.last = &(child->drnext);
  4827. parent->subdirs.cnt++;
  4828. child->parent = parent;
  4829. }
  4830. return (1);
  4831. }
  4832. static void
  4833. isoent_remove_child(struct isoent *parent, struct isoent *child)
  4834. {
  4835. struct isoent *ent;
  4836. /* Remove a child entry from children chain. */
  4837. ent = parent->children.first;
  4838. while (ent->chnext != child)
  4839. ent = ent->chnext;
  4840. if ((ent->chnext = ent->chnext->chnext) == NULL)
  4841. parent->children.last = &(ent->chnext);
  4842. parent->children.cnt--;
  4843. if (child->dir) {
  4844. /* Remove a child entry from sub-directory chain. */
  4845. ent = parent->subdirs.first;
  4846. while (ent->drnext != child)
  4847. ent = ent->drnext;
  4848. if ((ent->drnext = ent->drnext->drnext) == NULL)
  4849. parent->subdirs.last = &(ent->drnext);
  4850. parent->subdirs.cnt--;
  4851. }
  4852. __archive_rb_tree_remove_node(&(parent->rbtree),
  4853. (struct archive_rb_node *)child);
  4854. }
  4855. static int
  4856. isoent_clone_tree(struct archive_write *a, struct isoent **nroot,
  4857. struct isoent *root)
  4858. {
  4859. struct isoent *np, *xroot, *newent;
  4860. np = root;
  4861. xroot = NULL;
  4862. do {
  4863. newent = isoent_clone(np);
  4864. if (newent == NULL) {
  4865. archive_set_error(&a->archive, ENOMEM,
  4866. "Can't allocate memory");
  4867. return (ARCHIVE_FATAL);
  4868. }
  4869. if (xroot == NULL) {
  4870. *nroot = xroot = newent;
  4871. newent->parent = xroot;
  4872. } else
  4873. isoent_add_child_tail(xroot, newent);
  4874. if (np->dir && np->children.first != NULL) {
  4875. /* Enter to sub directories. */
  4876. np = np->children.first;
  4877. xroot = newent;
  4878. continue;
  4879. }
  4880. while (np != np->parent) {
  4881. if (np->chnext == NULL) {
  4882. /* Return to the parent directory. */
  4883. np = np->parent;
  4884. xroot = xroot->parent;
  4885. } else {
  4886. np = np->chnext;
  4887. break;
  4888. }
  4889. }
  4890. } while (np != np->parent);
  4891. return (ARCHIVE_OK);
  4892. }
  4893. /*
  4894. * Setup directory locations.
  4895. */
  4896. static void
  4897. isoent_setup_directory_location(struct iso9660 *iso9660, int location,
  4898. struct vdd *vdd)
  4899. {
  4900. struct isoent *np;
  4901. int depth;
  4902. vdd->total_dir_block = 0;
  4903. depth = 0;
  4904. np = vdd->rootent;
  4905. do {
  4906. int block;
  4907. np->dir_block = calculate_directory_descriptors(
  4908. iso9660, vdd, np, depth);
  4909. vdd->total_dir_block += np->dir_block;
  4910. np->dir_location = location;
  4911. location += np->dir_block;
  4912. block = extra_setup_location(np, location);
  4913. vdd->total_dir_block += block;
  4914. location += block;
  4915. if (np->subdirs.first != NULL && depth + 1 < vdd->max_depth) {
  4916. /* Enter to sub directories. */
  4917. np = np->subdirs.first;
  4918. depth++;
  4919. continue;
  4920. }
  4921. while (np != np->parent) {
  4922. if (np->drnext == NULL) {
  4923. /* Return to the parent directory. */
  4924. np = np->parent;
  4925. depth--;
  4926. } else {
  4927. np = np->drnext;
  4928. break;
  4929. }
  4930. }
  4931. } while (np != np->parent);
  4932. }
  4933. static void
  4934. _isoent_file_location(struct iso9660 *iso9660, struct isoent *isoent,
  4935. int *symlocation)
  4936. {
  4937. struct isoent **children;
  4938. int n;
  4939. if (isoent->children.cnt == 0)
  4940. return;
  4941. children = isoent->children_sorted;
  4942. for (n = 0; n < isoent->children.cnt; n++) {
  4943. struct isoent *np;
  4944. struct isofile *file;
  4945. np = children[n];
  4946. if (np->dir)
  4947. continue;
  4948. if (np == iso9660->el_torito.boot)
  4949. continue;
  4950. file = np->file;
  4951. if (file->boot || file->hardlink_target != NULL)
  4952. continue;
  4953. if (archive_entry_filetype(file->entry) == AE_IFLNK ||
  4954. file->content.size == 0) {
  4955. /*
  4956. * Do not point a valid location.
  4957. * Make sure entry is not hardlink file.
  4958. */
  4959. file->content.location = (*symlocation)--;
  4960. continue;
  4961. }
  4962. file->write_content = 1;
  4963. }
  4964. }
  4965. /*
  4966. * Setup file locations.
  4967. */
  4968. static void
  4969. isoent_setup_file_location(struct iso9660 *iso9660, int location)
  4970. {
  4971. struct isoent *isoent;
  4972. struct isoent *np;
  4973. struct isofile *file;
  4974. size_t size;
  4975. int block;
  4976. int depth;
  4977. int joliet;
  4978. int symlocation;
  4979. int total_block;
  4980. iso9660->total_file_block = 0;
  4981. if ((isoent = iso9660->el_torito.catalog) != NULL) {
  4982. isoent->file->content.location = location;
  4983. block = (int)((archive_entry_size(isoent->file->entry) +
  4984. LOGICAL_BLOCK_SIZE -1) >> LOGICAL_BLOCK_BITS);
  4985. location += block;
  4986. iso9660->total_file_block += block;
  4987. }
  4988. if ((isoent = iso9660->el_torito.boot) != NULL) {
  4989. isoent->file->content.location = location;
  4990. size = fd_boot_image_size(iso9660->el_torito.media_type);
  4991. if (size == 0)
  4992. size = (size_t)archive_entry_size(isoent->file->entry);
  4993. block = ((int)size + LOGICAL_BLOCK_SIZE -1)
  4994. >> LOGICAL_BLOCK_BITS;
  4995. location += block;
  4996. iso9660->total_file_block += block;
  4997. isoent->file->content.blocks = block;
  4998. }
  4999. depth = 0;
  5000. symlocation = -16;
  5001. if (!iso9660->opt.rr && iso9660->opt.joliet) {
  5002. joliet = 1;
  5003. np = iso9660->joliet.rootent;
  5004. } else {
  5005. joliet = 0;
  5006. np = iso9660->primary.rootent;
  5007. }
  5008. do {
  5009. _isoent_file_location(iso9660, np, &symlocation);
  5010. if (np->subdirs.first != NULL &&
  5011. (joliet ||
  5012. ((iso9660->opt.rr == OPT_RR_DISABLED &&
  5013. depth + 2 < iso9660->primary.max_depth) ||
  5014. (iso9660->opt.rr &&
  5015. depth + 1 < iso9660->primary.max_depth)))) {
  5016. /* Enter to sub directories. */
  5017. np = np->subdirs.first;
  5018. depth++;
  5019. continue;
  5020. }
  5021. while (np != np->parent) {
  5022. if (np->drnext == NULL) {
  5023. /* Return to the parent directory. */
  5024. np = np->parent;
  5025. depth--;
  5026. } else {
  5027. np = np->drnext;
  5028. break;
  5029. }
  5030. }
  5031. } while (np != np->parent);
  5032. total_block = 0;
  5033. for (file = iso9660->data_file_list.first;
  5034. file != NULL; file = file->datanext) {
  5035. if (!file->write_content)
  5036. continue;
  5037. file->cur_content = &(file->content);
  5038. do {
  5039. file->cur_content->location = location;
  5040. location += file->cur_content->blocks;
  5041. total_block += file->cur_content->blocks;
  5042. /* Next fragment */
  5043. file->cur_content = file->cur_content->next;
  5044. } while (file->cur_content != NULL);
  5045. }
  5046. iso9660->total_file_block += total_block;
  5047. }
  5048. static int
  5049. get_path_component(char *name, size_t n, const char *fn)
  5050. {
  5051. char *p;
  5052. size_t l;
  5053. p = strchr(fn, '/');
  5054. if (p == NULL) {
  5055. if ((l = strlen(fn)) == 0)
  5056. return (0);
  5057. } else
  5058. l = p - fn;
  5059. if (l > n -1)
  5060. return (-1);
  5061. memcpy(name, fn, l);
  5062. name[l] = '\0';
  5063. return ((int)l);
  5064. }
  5065. /*
  5066. * Add a new entry into the tree.
  5067. */
  5068. static int
  5069. isoent_tree(struct archive_write *a, struct isoent **isoentpp)
  5070. {
  5071. #if defined(_WIN32) && !defined(__CYGWIN__)
  5072. char name[_MAX_FNAME];/* Included null terminator size. */
  5073. #elif defined(NAME_MAX) && NAME_MAX >= 255
  5074. char name[NAME_MAX+1];
  5075. #else
  5076. char name[256];
  5077. #endif
  5078. struct iso9660 *iso9660 = a->format_data;
  5079. struct isoent *dent, *isoent, *np;
  5080. struct isofile *f1, *f2;
  5081. const char *fn, *p;
  5082. int l;
  5083. isoent = *isoentpp;
  5084. dent = iso9660->primary.rootent;
  5085. if (isoent->file->parentdir.length > 0)
  5086. fn = p = isoent->file->parentdir.s;
  5087. else
  5088. fn = p = "";
  5089. /*
  5090. * If the path of the parent directory of `isoent' entry is
  5091. * the same as the path of `cur_dirent', add isoent to
  5092. * `cur_dirent'.
  5093. */
  5094. if (archive_strlen(&(iso9660->cur_dirstr))
  5095. == archive_strlen(&(isoent->file->parentdir)) &&
  5096. strcmp(iso9660->cur_dirstr.s, fn) == 0) {
  5097. if (!isoent_add_child_tail(iso9660->cur_dirent, isoent)) {
  5098. np = (struct isoent *)__archive_rb_tree_find_node(
  5099. &(iso9660->cur_dirent->rbtree),
  5100. isoent->file->basename.s);
  5101. goto same_entry;
  5102. }
  5103. return (ARCHIVE_OK);
  5104. }
  5105. for (;;) {
  5106. l = get_path_component(name, sizeof(name), fn);
  5107. if (l == 0) {
  5108. np = NULL;
  5109. break;
  5110. }
  5111. if (l < 0) {
  5112. archive_set_error(&a->archive,
  5113. ARCHIVE_ERRNO_MISC,
  5114. "A name buffer is too small");
  5115. _isoent_free(isoent);
  5116. return (ARCHIVE_FATAL);
  5117. }
  5118. np = isoent_find_child(dent, name);
  5119. if (np == NULL || fn[0] == '\0')
  5120. break;
  5121. /* Find next subdirectory. */
  5122. if (!np->dir) {
  5123. /* NOT Directory! */
  5124. archive_set_error(&a->archive,
  5125. ARCHIVE_ERRNO_MISC,
  5126. "`%s' is not directory, we cannot insert `%s' ",
  5127. archive_entry_pathname(np->file->entry),
  5128. archive_entry_pathname(isoent->file->entry));
  5129. _isoent_free(isoent);
  5130. *isoentpp = NULL;
  5131. return (ARCHIVE_FAILED);
  5132. }
  5133. fn += l;
  5134. if (fn[0] == '/')
  5135. fn++;
  5136. dent = np;
  5137. }
  5138. if (np == NULL) {
  5139. /*
  5140. * Create virtual parent directories.
  5141. */
  5142. while (fn[0] != '\0') {
  5143. struct isoent *vp;
  5144. struct archive_string as;
  5145. archive_string_init(&as);
  5146. archive_strncat(&as, p, fn - p + l);
  5147. if (as.s[as.length-1] == '/') {
  5148. as.s[as.length-1] = '\0';
  5149. as.length--;
  5150. }
  5151. vp = isoent_create_virtual_dir(a, iso9660, as.s);
  5152. if (vp == NULL) {
  5153. archive_string_free(&as);
  5154. archive_set_error(&a->archive, ENOMEM,
  5155. "Can't allocate memory");
  5156. _isoent_free(isoent);
  5157. *isoentpp = NULL;
  5158. return (ARCHIVE_FATAL);
  5159. }
  5160. archive_string_free(&as);
  5161. if (vp->file->dircnt > iso9660->dircnt_max)
  5162. iso9660->dircnt_max = vp->file->dircnt;
  5163. isoent_add_child_tail(dent, vp);
  5164. np = vp;
  5165. fn += l;
  5166. if (fn[0] == '/')
  5167. fn++;
  5168. l = get_path_component(name, sizeof(name), fn);
  5169. if (l < 0) {
  5170. archive_string_free(&as);
  5171. archive_set_error(&a->archive,
  5172. ARCHIVE_ERRNO_MISC,
  5173. "A name buffer is too small");
  5174. _isoent_free(isoent);
  5175. *isoentpp = NULL;
  5176. return (ARCHIVE_FATAL);
  5177. }
  5178. dent = np;
  5179. }
  5180. /* Found out the parent directory where isoent can be
  5181. * inserted. */
  5182. iso9660->cur_dirent = dent;
  5183. archive_string_empty(&(iso9660->cur_dirstr));
  5184. archive_string_ensure(&(iso9660->cur_dirstr),
  5185. archive_strlen(&(dent->file->parentdir)) +
  5186. archive_strlen(&(dent->file->basename)) + 2);
  5187. if (archive_strlen(&(dent->file->parentdir)) +
  5188. archive_strlen(&(dent->file->basename)) == 0)
  5189. iso9660->cur_dirstr.s[0] = 0;
  5190. else {
  5191. if (archive_strlen(&(dent->file->parentdir)) > 0) {
  5192. archive_string_copy(&(iso9660->cur_dirstr),
  5193. &(dent->file->parentdir));
  5194. archive_strappend_char(&(iso9660->cur_dirstr), '/');
  5195. }
  5196. archive_string_concat(&(iso9660->cur_dirstr),
  5197. &(dent->file->basename));
  5198. }
  5199. if (!isoent_add_child_tail(dent, isoent)) {
  5200. np = (struct isoent *)__archive_rb_tree_find_node(
  5201. &(dent->rbtree), isoent->file->basename.s);
  5202. goto same_entry;
  5203. }
  5204. return (ARCHIVE_OK);
  5205. }
  5206. same_entry:
  5207. /*
  5208. * We have already has the entry the filename of which is
  5209. * the same.
  5210. */
  5211. f1 = np->file;
  5212. f2 = isoent->file;
  5213. /* If the file type of entries is different,
  5214. * we cannot handle it. */
  5215. if (archive_entry_filetype(f1->entry) !=
  5216. archive_entry_filetype(f2->entry)) {
  5217. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  5218. "Found duplicate entries `%s' and its file type is "
  5219. "different",
  5220. archive_entry_pathname(f1->entry));
  5221. _isoent_free(isoent);
  5222. *isoentpp = NULL;
  5223. return (ARCHIVE_FAILED);
  5224. }
  5225. /* Swap file entries. */
  5226. np->file = f2;
  5227. isoent->file = f1;
  5228. np->virtual = 0;
  5229. _isoent_free(isoent);
  5230. *isoentpp = np;
  5231. return (ARCHIVE_OK);
  5232. }
  5233. /*
  5234. * Find a entry from `isoent'
  5235. */
  5236. static struct isoent *
  5237. isoent_find_child(struct isoent *isoent, const char *child_name)
  5238. {
  5239. struct isoent *np;
  5240. np = (struct isoent *)__archive_rb_tree_find_node(
  5241. &(isoent->rbtree), child_name);
  5242. return (np);
  5243. }
  5244. /*
  5245. * Find a entry full-path of which is specified by `fn' parameter,
  5246. * in the tree.
  5247. */
  5248. static struct isoent *
  5249. isoent_find_entry(struct isoent *rootent, const char *fn)
  5250. {
  5251. #if defined(_WIN32) && !defined(__CYGWIN__)
  5252. char name[_MAX_FNAME];/* Included null terminator size. */
  5253. #elif defined(NAME_MAX) && NAME_MAX >= 255
  5254. char name[NAME_MAX+1];
  5255. #else
  5256. char name[256];
  5257. #endif
  5258. struct isoent *isoent, *np;
  5259. int l;
  5260. isoent = rootent;
  5261. np = NULL;
  5262. for (;;) {
  5263. l = get_path_component(name, sizeof(name), fn);
  5264. if (l == 0)
  5265. break;
  5266. fn += l;
  5267. if (fn[0] == '/')
  5268. fn++;
  5269. np = isoent_find_child(isoent, name);
  5270. if (np == NULL)
  5271. break;
  5272. if (fn[0] == '\0')
  5273. break;/* We found out the entry */
  5274. /* Try sub directory. */
  5275. isoent = np;
  5276. np = NULL;
  5277. if (!isoent->dir)
  5278. break;/* Not directory */
  5279. }
  5280. return (np);
  5281. }
  5282. /*
  5283. * Following idr_* functions are used for resolving duplicated filenames
  5284. * and unreceivable filenames to generate ISO9660/Joliet Identifiers.
  5285. */
  5286. static void
  5287. idr_relaxed_filenames(char *map)
  5288. {
  5289. int i;
  5290. for (i = 0x21; i <= 0x2F; i++)
  5291. map[i] = 1;
  5292. for (i = 0x3A; i <= 0x41; i++)
  5293. map[i] = 1;
  5294. for (i = 0x5B; i <= 0x5E; i++)
  5295. map[i] = 1;
  5296. map[0x60] = 1;
  5297. for (i = 0x7B; i <= 0x7E; i++)
  5298. map[i] = 1;
  5299. }
  5300. static void
  5301. idr_init(struct iso9660 *iso9660, struct vdd *vdd, struct idr *idr)
  5302. {
  5303. idr->idrent_pool = NULL;
  5304. idr->pool_size = 0;
  5305. if (vdd->vdd_type != VDD_JOLIET) {
  5306. if (iso9660->opt.iso_level <= 3) {
  5307. memcpy(idr->char_map, d_characters_map,
  5308. sizeof(idr->char_map));
  5309. } else {
  5310. memcpy(idr->char_map, d1_characters_map,
  5311. sizeof(idr->char_map));
  5312. idr_relaxed_filenames(idr->char_map);
  5313. }
  5314. }
  5315. }
  5316. static void
  5317. idr_cleanup(struct idr *idr)
  5318. {
  5319. free(idr->idrent_pool);
  5320. }
  5321. static int
  5322. idr_ensure_poolsize(struct archive_write *a, struct idr *idr,
  5323. int cnt)
  5324. {
  5325. if (idr->pool_size < cnt) {
  5326. void *p;
  5327. const int bk = (1 << 7) - 1;
  5328. int psize;
  5329. psize = (cnt + bk) & ~bk;
  5330. p = realloc(idr->idrent_pool, sizeof(struct idrent) * psize);
  5331. if (p == NULL) {
  5332. archive_set_error(&a->archive, ENOMEM,
  5333. "Can't allocate memory");
  5334. return (ARCHIVE_FATAL);
  5335. }
  5336. idr->idrent_pool = (struct idrent *)p;
  5337. idr->pool_size = psize;
  5338. }
  5339. return (ARCHIVE_OK);
  5340. }
  5341. static int
  5342. idr_start(struct archive_write *a, struct idr *idr, int cnt, int ffmax,
  5343. int num_size, int null_size, const struct archive_rb_tree_ops *rbt_ops)
  5344. {
  5345. int r;
  5346. (void)ffmax; /* UNUSED */
  5347. r = idr_ensure_poolsize(a, idr, cnt);
  5348. if (r != ARCHIVE_OK)
  5349. return (r);
  5350. __archive_rb_tree_init(&(idr->rbtree), rbt_ops);
  5351. idr->wait_list.first = NULL;
  5352. idr->wait_list.last = &(idr->wait_list.first);
  5353. idr->pool_idx = 0;
  5354. idr->num_size = num_size;
  5355. idr->null_size = null_size;
  5356. return (ARCHIVE_OK);
  5357. }
  5358. static void
  5359. idr_register(struct idr *idr, struct isoent *isoent, int weight, int noff)
  5360. {
  5361. struct idrent *idrent, *n;
  5362. idrent = &(idr->idrent_pool[idr->pool_idx++]);
  5363. idrent->wnext = idrent->avail = NULL;
  5364. idrent->isoent = isoent;
  5365. idrent->weight = weight;
  5366. idrent->noff = noff;
  5367. idrent->rename_num = 0;
  5368. if (!__archive_rb_tree_insert_node(&(idr->rbtree), &(idrent->rbnode))) {
  5369. n = (struct idrent *)__archive_rb_tree_find_node(
  5370. &(idr->rbtree), idrent->isoent);
  5371. if (n != NULL) {
  5372. /* this `idrent' needs to rename. */
  5373. idrent->avail = n;
  5374. *idr->wait_list.last = idrent;
  5375. idr->wait_list.last = &(idrent->wnext);
  5376. }
  5377. }
  5378. }
  5379. static void
  5380. idr_extend_identifier(struct idrent *wnp, int numsize, int nullsize)
  5381. {
  5382. unsigned char *p;
  5383. int wnp_ext_off;
  5384. wnp_ext_off = wnp->isoent->ext_off;
  5385. if (wnp->noff + numsize != wnp_ext_off) {
  5386. p = (unsigned char *)wnp->isoent->identifier;
  5387. /* Extend the filename; foo.c --> foo___.c */
  5388. memmove(p + wnp->noff + numsize, p + wnp_ext_off,
  5389. wnp->isoent->ext_len + nullsize);
  5390. wnp->isoent->ext_off = wnp_ext_off = wnp->noff + numsize;
  5391. wnp->isoent->id_len = wnp_ext_off + wnp->isoent->ext_len;
  5392. }
  5393. }
  5394. static void
  5395. idr_resolve(struct idr *idr, void (*fsetnum)(unsigned char *p, int num))
  5396. {
  5397. struct idrent *n;
  5398. unsigned char *p;
  5399. for (n = idr->wait_list.first; n != NULL; n = n->wnext) {
  5400. idr_extend_identifier(n, idr->num_size, idr->null_size);
  5401. p = (unsigned char *)n->isoent->identifier + n->noff;
  5402. do {
  5403. fsetnum(p, n->avail->rename_num++);
  5404. } while (!__archive_rb_tree_insert_node(
  5405. &(idr->rbtree), &(n->rbnode)));
  5406. }
  5407. }
  5408. static void
  5409. idr_set_num(unsigned char *p, int num)
  5410. {
  5411. static const char xdig[] = {
  5412. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  5413. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  5414. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  5415. 'U', 'V', 'W', 'X', 'Y', 'Z'
  5416. };
  5417. num %= sizeof(xdig) * sizeof(xdig) * sizeof(xdig);
  5418. p[0] = xdig[(num / (sizeof(xdig) * sizeof(xdig)))];
  5419. num %= sizeof(xdig) * sizeof(xdig);
  5420. p[1] = xdig[ (num / sizeof(xdig))];
  5421. num %= sizeof(xdig);
  5422. p[2] = xdig[num];
  5423. }
  5424. static void
  5425. idr_set_num_beutf16(unsigned char *p, int num)
  5426. {
  5427. static const uint16_t xdig[] = {
  5428. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
  5429. 0x0036, 0x0037, 0x0038, 0x0039,
  5430. 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046,
  5431. 0x0047, 0x0048, 0x0049, 0x004A, 0x004B, 0x004C,
  5432. 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052,
  5433. 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058,
  5434. 0x0059, 0x005A
  5435. };
  5436. #define XDIG_CNT (sizeof(xdig)/sizeof(xdig[0]))
  5437. num %= XDIG_CNT * XDIG_CNT * XDIG_CNT;
  5438. archive_be16enc(p, xdig[(num / (XDIG_CNT * XDIG_CNT))]);
  5439. num %= XDIG_CNT * XDIG_CNT;
  5440. archive_be16enc(p+2, xdig[ (num / XDIG_CNT)]);
  5441. num %= XDIG_CNT;
  5442. archive_be16enc(p+4, xdig[num]);
  5443. }
  5444. /*
  5445. * Generate ISO9660 Identifier.
  5446. */
  5447. static int
  5448. isoent_gen_iso9660_identifier(struct archive_write *a, struct isoent *isoent,
  5449. struct idr *idr)
  5450. {
  5451. struct iso9660 *iso9660;
  5452. struct isoent *np;
  5453. char *p;
  5454. int l, r;
  5455. const char *char_map;
  5456. char allow_ldots, allow_multidot, allow_period, allow_vernum;
  5457. int fnmax, ffmax, dnmax;
  5458. static const struct archive_rb_tree_ops rb_ops = {
  5459. isoent_cmp_node_iso9660, isoent_cmp_key_iso9660
  5460. };
  5461. if (isoent->children.cnt == 0)
  5462. return (0);
  5463. iso9660 = a->format_data;
  5464. char_map = idr->char_map;
  5465. if (iso9660->opt.iso_level <= 3) {
  5466. allow_ldots = 0;
  5467. allow_multidot = 0;
  5468. allow_period = 1;
  5469. allow_vernum = iso9660->opt.allow_vernum;
  5470. if (iso9660->opt.iso_level == 1) {
  5471. fnmax = 8;
  5472. ffmax = 12;/* fnmax + '.' + 3 */
  5473. dnmax = 8;
  5474. } else {
  5475. fnmax = 30;
  5476. ffmax = 31;
  5477. dnmax = 31;
  5478. }
  5479. } else {
  5480. allow_ldots = allow_multidot = 1;
  5481. allow_period = allow_vernum = 0;
  5482. if (iso9660->opt.rr)
  5483. /*
  5484. * MDR : The maximum size of Directory Record(254).
  5485. * DRL : A Directory Record Length(33).
  5486. * CE : A size of SUSP CE System Use Entry(28).
  5487. * MDR - DRL - CE = 254 - 33 - 28 = 193.
  5488. */
  5489. fnmax = ffmax = dnmax = 193;
  5490. else
  5491. /*
  5492. * XA : CD-ROM XA System Use Extension
  5493. * Information(14).
  5494. * MDR - DRL - XA = 254 - 33 -14 = 207.
  5495. */
  5496. fnmax = ffmax = dnmax = 207;
  5497. }
  5498. r = idr_start(a, idr, isoent->children.cnt, ffmax, 3, 1, &rb_ops);
  5499. if (r < 0)
  5500. return (r);
  5501. for (np = isoent->children.first; np != NULL; np = np->chnext) {
  5502. char *dot, *xdot;
  5503. int ext_off, noff, weight;
  5504. l = (int)np->file->basename.length;
  5505. p = malloc(l+31+2+1);
  5506. if (p == NULL) {
  5507. archive_set_error(&a->archive, ENOMEM,
  5508. "Can't allocate memory");
  5509. return (ARCHIVE_FATAL);
  5510. }
  5511. memcpy(p, np->file->basename.s, l);
  5512. p[l] = '\0';
  5513. np->identifier = p;
  5514. dot = xdot = NULL;
  5515. if (!allow_ldots) {
  5516. /*
  5517. * If there is a '.' character at the first byte,
  5518. * it has to be replaced by '_' character.
  5519. */
  5520. if (*p == '.')
  5521. *p++ = '_';
  5522. }
  5523. for (;*p; p++) {
  5524. if (*p & 0x80) {
  5525. *p = '_';
  5526. continue;
  5527. }
  5528. if (char_map[(unsigned char)*p]) {
  5529. /* if iso-level is '4', a character '.' is
  5530. * allowed by char_map. */
  5531. if (*p == '.') {
  5532. xdot = dot;
  5533. dot = p;
  5534. }
  5535. continue;
  5536. }
  5537. if (*p >= 'a' && *p <= 'z') {
  5538. *p -= 'a' - 'A';
  5539. continue;
  5540. }
  5541. if (*p == '.') {
  5542. xdot = dot;
  5543. dot = p;
  5544. if (allow_multidot)
  5545. continue;
  5546. }
  5547. *p = '_';
  5548. }
  5549. p = np->identifier;
  5550. weight = -1;
  5551. if (dot == NULL) {
  5552. int nammax;
  5553. if (np->dir)
  5554. nammax = dnmax;
  5555. else
  5556. nammax = fnmax;
  5557. if (l > nammax) {
  5558. p[nammax] = '\0';
  5559. weight = nammax;
  5560. ext_off = nammax;
  5561. } else
  5562. ext_off = l;
  5563. } else {
  5564. *dot = '.';
  5565. ext_off = (int)(dot - p);
  5566. if (iso9660->opt.iso_level == 1) {
  5567. if (dot - p <= 8) {
  5568. if (strlen(dot) > 4) {
  5569. /* A length of a file extension
  5570. * must be less than 4 */
  5571. dot[4] = '\0';
  5572. weight = 0;
  5573. }
  5574. } else {
  5575. p[8] = dot[0];
  5576. p[9] = dot[1];
  5577. p[10] = dot[2];
  5578. p[11] = dot[3];
  5579. p[12] = '\0';
  5580. weight = 8;
  5581. ext_off = 8;
  5582. }
  5583. } else if (np->dir) {
  5584. if (l > dnmax) {
  5585. p[dnmax] = '\0';
  5586. weight = dnmax;
  5587. if (ext_off > dnmax)
  5588. ext_off = dnmax;
  5589. }
  5590. } else if (l > ffmax) {
  5591. int extlen = (int)strlen(dot);
  5592. int xdoff;
  5593. if (xdot != NULL)
  5594. xdoff = (int)(xdot - p);
  5595. else
  5596. xdoff = 0;
  5597. if (extlen > 1 && xdoff < fnmax-1) {
  5598. int off;
  5599. if (extlen > ffmax)
  5600. extlen = ffmax;
  5601. off = ffmax - extlen;
  5602. if (off == 0) {
  5603. /* A dot('.') character
  5604. * doesn't place to the first
  5605. * byte of identifier. */
  5606. off ++;
  5607. extlen --;
  5608. }
  5609. memmove(p+off, dot, extlen);
  5610. p[ffmax] = '\0';
  5611. ext_off = off;
  5612. weight = off;
  5613. #ifdef COMPAT_MKISOFS
  5614. } else if (xdoff >= fnmax-1) {
  5615. /* Simulate a bug(?) of mkisofs. */
  5616. p[fnmax-1] = '\0';
  5617. ext_off = fnmax-1;
  5618. weight = fnmax-1;
  5619. #endif
  5620. } else {
  5621. p[fnmax] = '\0';
  5622. ext_off = fnmax;
  5623. weight = fnmax;
  5624. }
  5625. }
  5626. }
  5627. /* Save an offset of a file name extension to sort files. */
  5628. np->ext_off = ext_off;
  5629. np->ext_len = (int)strlen(&p[ext_off]);
  5630. np->id_len = l = ext_off + np->ext_len;
  5631. /* Make an offset of the number which is used to be set
  5632. * hexadecimal number to avoid duplicate identifier. */
  5633. if (iso9660->opt.iso_level == 1) {
  5634. if (ext_off >= 5)
  5635. noff = 5;
  5636. else
  5637. noff = ext_off;
  5638. } else {
  5639. if (l == ffmax)
  5640. noff = ext_off - 3;
  5641. else if (l == ffmax-1)
  5642. noff = ext_off - 2;
  5643. else if (l == ffmax-2)
  5644. noff = ext_off - 1;
  5645. else
  5646. noff = ext_off;
  5647. }
  5648. /* Register entry to the identifier resolver. */
  5649. idr_register(idr, np, weight, noff);
  5650. }
  5651. /* Resolve duplicate identifier. */
  5652. idr_resolve(idr, idr_set_num);
  5653. /* Add a period and a version number to identifiers. */
  5654. for (np = isoent->children.first; np != NULL; np = np->chnext) {
  5655. if (!np->dir && np->rr_child == NULL) {
  5656. p = np->identifier + np->ext_off + np->ext_len;
  5657. if (np->ext_len == 0 && allow_period) {
  5658. *p++ = '.';
  5659. np->ext_len = 1;
  5660. }
  5661. if (np->ext_len == 1 && !allow_period) {
  5662. *--p = '\0';
  5663. np->ext_len = 0;
  5664. }
  5665. np->id_len = np->ext_off + np->ext_len;
  5666. if (allow_vernum) {
  5667. *p++ = ';';
  5668. *p++ = '1';
  5669. np->id_len += 2;
  5670. }
  5671. *p = '\0';
  5672. } else
  5673. np->id_len = np->ext_off + np->ext_len;
  5674. np->mb_len = np->id_len;
  5675. }
  5676. return (ARCHIVE_OK);
  5677. }
  5678. /*
  5679. * Generate Joliet Identifier.
  5680. */
  5681. static int
  5682. isoent_gen_joliet_identifier(struct archive_write *a, struct isoent *isoent,
  5683. struct idr *idr)
  5684. {
  5685. struct iso9660 *iso9660;
  5686. struct isoent *np;
  5687. unsigned char *p;
  5688. size_t l;
  5689. int r;
  5690. size_t ffmax, parent_len;
  5691. static const struct archive_rb_tree_ops rb_ops = {
  5692. isoent_cmp_node_joliet, isoent_cmp_key_joliet
  5693. };
  5694. if (isoent->children.cnt == 0)
  5695. return (0);
  5696. iso9660 = a->format_data;
  5697. if (iso9660->opt.joliet == OPT_JOLIET_LONGNAME)
  5698. ffmax = 206;
  5699. else
  5700. ffmax = 128;
  5701. r = idr_start(a, idr, isoent->children.cnt, (int)ffmax, 6, 2, &rb_ops);
  5702. if (r < 0)
  5703. return (r);
  5704. parent_len = 1;
  5705. for (np = isoent; np->parent != np; np = np->parent)
  5706. parent_len += np->mb_len + 1;
  5707. for (np = isoent->children.first; np != NULL; np = np->chnext) {
  5708. unsigned char *dot;
  5709. int ext_off, noff, weight;
  5710. size_t lt;
  5711. if ((l = np->file->basename_utf16.length) > ffmax)
  5712. l = ffmax;
  5713. p = malloc((l+1)*2);
  5714. if (p == NULL) {
  5715. archive_set_error(&a->archive, ENOMEM,
  5716. "Can't allocate memory");
  5717. return (ARCHIVE_FATAL);
  5718. }
  5719. memcpy(p, np->file->basename_utf16.s, l);
  5720. p[l] = 0;
  5721. p[l+1] = 0;
  5722. np->identifier = (char *)p;
  5723. lt = l;
  5724. dot = p + l;
  5725. weight = 0;
  5726. while (lt > 0) {
  5727. if (!joliet_allowed_char(p[0], p[1]))
  5728. archive_be16enc(p, 0x005F); /* '_' */
  5729. else if (p[0] == 0 && p[1] == 0x2E) /* '.' */
  5730. dot = p;
  5731. p += 2;
  5732. lt -= 2;
  5733. }
  5734. ext_off = (int)(dot - (unsigned char *)np->identifier);
  5735. np->ext_off = ext_off;
  5736. np->ext_len = (int)l - ext_off;
  5737. np->id_len = (int)l;
  5738. /*
  5739. * Get a length of MBS of a full-pathname.
  5740. */
  5741. if (np->file->basename_utf16.length > ffmax) {
  5742. if (archive_strncpy_l(&iso9660->mbs,
  5743. (const char *)np->identifier, l,
  5744. iso9660->sconv_from_utf16be) != 0 &&
  5745. errno == ENOMEM) {
  5746. archive_set_error(&a->archive, errno,
  5747. "No memory");
  5748. return (ARCHIVE_FATAL);
  5749. }
  5750. np->mb_len = (int)iso9660->mbs.length;
  5751. if (np->mb_len != (int)np->file->basename.length)
  5752. weight = np->mb_len;
  5753. } else
  5754. np->mb_len = (int)np->file->basename.length;
  5755. /* If a length of full-pathname is longer than 240 bytes,
  5756. * it violates Joliet extensions regulation. */
  5757. if (parent_len > 240
  5758. || np->mb_len > 240
  5759. || parent_len + np->mb_len > 240) {
  5760. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  5761. "The regulation of Joliet extensions;"
  5762. " A length of a full-pathname of `%s' is "
  5763. "longer than 240 bytes, (p=%d, b=%d)",
  5764. archive_entry_pathname(np->file->entry),
  5765. (int)parent_len, (int)np->mb_len);
  5766. return (ARCHIVE_FATAL);
  5767. }
  5768. /* Make an offset of the number which is used to be set
  5769. * hexadecimal number to avoid duplicate identifier. */
  5770. if (l == ffmax)
  5771. noff = ext_off - 6;
  5772. else if (l == ffmax-2)
  5773. noff = ext_off - 4;
  5774. else if (l == ffmax-4)
  5775. noff = ext_off - 2;
  5776. else
  5777. noff = ext_off;
  5778. /* Register entry to the identifier resolver. */
  5779. idr_register(idr, np, weight, noff);
  5780. }
  5781. /* Resolve duplicate identifier with Joliet Volume. */
  5782. idr_resolve(idr, idr_set_num_beutf16);
  5783. return (ARCHIVE_OK);
  5784. }
  5785. /*
  5786. * This comparing rule is according to ISO9660 Standard 9.3
  5787. */
  5788. static int
  5789. isoent_cmp_iso9660_identifier(const struct isoent *p1, const struct isoent *p2)
  5790. {
  5791. const char *s1, *s2;
  5792. int cmp;
  5793. int l;
  5794. s1 = p1->identifier;
  5795. s2 = p2->identifier;
  5796. /* Compare File Name */
  5797. l = p1->ext_off;
  5798. if (l > p2->ext_off)
  5799. l = p2->ext_off;
  5800. cmp = memcmp(s1, s2, l);
  5801. if (cmp != 0)
  5802. return (cmp);
  5803. if (p1->ext_off < p2->ext_off) {
  5804. s2 += l;
  5805. l = p2->ext_off - p1->ext_off;
  5806. while (l--)
  5807. if (0x20 != *s2++)
  5808. return (0x20
  5809. - *(const unsigned char *)(s2 - 1));
  5810. } else if (p1->ext_off > p2->ext_off) {
  5811. s1 += l;
  5812. l = p1->ext_off - p2->ext_off;
  5813. while (l--)
  5814. if (0x20 != *s1++)
  5815. return (*(const unsigned char *)(s1 - 1)
  5816. - 0x20);
  5817. }
  5818. /* Compare File Name Extension */
  5819. if (p1->ext_len == 0 && p2->ext_len == 0)
  5820. return (0);
  5821. if (p1->ext_len == 1 && p2->ext_len == 1)
  5822. return (0);
  5823. if (p1->ext_len <= 1)
  5824. return (-1);
  5825. if (p2->ext_len <= 1)
  5826. return (1);
  5827. l = p1->ext_len;
  5828. if (l > p2->ext_len)
  5829. l = p2->ext_len;
  5830. s1 = p1->identifier + p1->ext_off;
  5831. s2 = p2->identifier + p2->ext_off;
  5832. if (l > 1) {
  5833. cmp = memcmp(s1, s2, l);
  5834. if (cmp != 0)
  5835. return (cmp);
  5836. }
  5837. if (p1->ext_len < p2->ext_len) {
  5838. s2 += l;
  5839. l = p2->ext_len - p1->ext_len;
  5840. while (l--)
  5841. if (0x20 != *s2++)
  5842. return (0x20
  5843. - *(const unsigned char *)(s2 - 1));
  5844. } else if (p1->ext_len > p2->ext_len) {
  5845. s1 += l;
  5846. l = p1->ext_len - p2->ext_len;
  5847. while (l--)
  5848. if (0x20 != *s1++)
  5849. return (*(const unsigned char *)(s1 - 1)
  5850. - 0x20);
  5851. }
  5852. /* Compare File Version Number */
  5853. /* No operation. The File Version Number is always one. */
  5854. return (cmp);
  5855. }
  5856. static int
  5857. isoent_cmp_node_iso9660(const struct archive_rb_node *n1,
  5858. const struct archive_rb_node *n2)
  5859. {
  5860. const struct idrent *e1 = (const struct idrent *)n1;
  5861. const struct idrent *e2 = (const struct idrent *)n2;
  5862. return (isoent_cmp_iso9660_identifier(e2->isoent, e1->isoent));
  5863. }
  5864. static int
  5865. isoent_cmp_key_iso9660(const struct archive_rb_node *node, const void *key)
  5866. {
  5867. const struct isoent *isoent = (const struct isoent *)key;
  5868. const struct idrent *idrent = (const struct idrent *)node;
  5869. return (isoent_cmp_iso9660_identifier(isoent, idrent->isoent));
  5870. }
  5871. static int
  5872. isoent_cmp_joliet_identifier(const struct isoent *p1, const struct isoent *p2)
  5873. {
  5874. const unsigned char *s1, *s2;
  5875. int cmp;
  5876. int l;
  5877. s1 = (const unsigned char *)p1->identifier;
  5878. s2 = (const unsigned char *)p2->identifier;
  5879. /* Compare File Name */
  5880. l = p1->ext_off;
  5881. if (l > p2->ext_off)
  5882. l = p2->ext_off;
  5883. cmp = memcmp(s1, s2, l);
  5884. if (cmp != 0)
  5885. return (cmp);
  5886. if (p1->ext_off < p2->ext_off) {
  5887. s2 += l;
  5888. l = p2->ext_off - p1->ext_off;
  5889. while (l--)
  5890. if (0 != *s2++)
  5891. return (- *(const unsigned char *)(s2 - 1));
  5892. } else if (p1->ext_off > p2->ext_off) {
  5893. s1 += l;
  5894. l = p1->ext_off - p2->ext_off;
  5895. while (l--)
  5896. if (0 != *s1++)
  5897. return (*(const unsigned char *)(s1 - 1));
  5898. }
  5899. /* Compare File Name Extension */
  5900. if (p1->ext_len == 0 && p2->ext_len == 0)
  5901. return (0);
  5902. if (p1->ext_len == 2 && p2->ext_len == 2)
  5903. return (0);
  5904. if (p1->ext_len <= 2)
  5905. return (-1);
  5906. if (p2->ext_len <= 2)
  5907. return (1);
  5908. l = p1->ext_len;
  5909. if (l > p2->ext_len)
  5910. l = p2->ext_len;
  5911. s1 = (unsigned char *)(p1->identifier + p1->ext_off);
  5912. s2 = (unsigned char *)(p2->identifier + p2->ext_off);
  5913. if (l > 1) {
  5914. cmp = memcmp(s1, s2, l);
  5915. if (cmp != 0)
  5916. return (cmp);
  5917. }
  5918. if (p1->ext_len < p2->ext_len) {
  5919. s2 += l;
  5920. l = p2->ext_len - p1->ext_len;
  5921. while (l--)
  5922. if (0 != *s2++)
  5923. return (- *(const unsigned char *)(s2 - 1));
  5924. } else if (p1->ext_len > p2->ext_len) {
  5925. s1 += l;
  5926. l = p1->ext_len - p2->ext_len;
  5927. while (l--)
  5928. if (0 != *s1++)
  5929. return (*(const unsigned char *)(s1 - 1));
  5930. }
  5931. /* Compare File Version Number */
  5932. /* No operation. The File Version Number is always one. */
  5933. return (cmp);
  5934. }
  5935. static int
  5936. isoent_cmp_node_joliet(const struct archive_rb_node *n1,
  5937. const struct archive_rb_node *n2)
  5938. {
  5939. const struct idrent *e1 = (const struct idrent *)n1;
  5940. const struct idrent *e2 = (const struct idrent *)n2;
  5941. return (isoent_cmp_joliet_identifier(e2->isoent, e1->isoent));
  5942. }
  5943. static int
  5944. isoent_cmp_key_joliet(const struct archive_rb_node *node, const void *key)
  5945. {
  5946. const struct isoent *isoent = (const struct isoent *)key;
  5947. const struct idrent *idrent = (const struct idrent *)node;
  5948. return (isoent_cmp_joliet_identifier(isoent, idrent->isoent));
  5949. }
  5950. static int
  5951. isoent_make_sorted_files(struct archive_write *a, struct isoent *isoent,
  5952. struct idr *idr)
  5953. {
  5954. struct archive_rb_node *rn;
  5955. struct isoent **children;
  5956. children = malloc(isoent->children.cnt * sizeof(struct isoent *));
  5957. if (children == NULL) {
  5958. archive_set_error(&a->archive, ENOMEM,
  5959. "Can't allocate memory");
  5960. return (ARCHIVE_FATAL);
  5961. }
  5962. isoent->children_sorted = children;
  5963. ARCHIVE_RB_TREE_FOREACH(rn, &(idr->rbtree)) {
  5964. struct idrent *idrent = (struct idrent *)rn;
  5965. *children ++ = idrent->isoent;
  5966. }
  5967. return (ARCHIVE_OK);
  5968. }
  5969. /*
  5970. * - Generate ISO9660 and Joliet identifiers from basenames.
  5971. * - Sort files by each directory.
  5972. */
  5973. static int
  5974. isoent_traverse_tree(struct archive_write *a, struct vdd* vdd)
  5975. {
  5976. struct iso9660 *iso9660 = a->format_data;
  5977. struct isoent *np;
  5978. struct idr idr;
  5979. int depth;
  5980. int r;
  5981. int (*genid)(struct archive_write *, struct isoent *, struct idr *);
  5982. idr_init(iso9660, vdd, &idr);
  5983. np = vdd->rootent;
  5984. depth = 0;
  5985. if (vdd->vdd_type == VDD_JOLIET)
  5986. genid = isoent_gen_joliet_identifier;
  5987. else
  5988. genid = isoent_gen_iso9660_identifier;
  5989. do {
  5990. if (np->virtual &&
  5991. !archive_entry_mtime_is_set(np->file->entry)) {
  5992. /* Set properly times to virtual directory */
  5993. archive_entry_set_mtime(np->file->entry,
  5994. iso9660->birth_time, 0);
  5995. archive_entry_set_atime(np->file->entry,
  5996. iso9660->birth_time, 0);
  5997. archive_entry_set_ctime(np->file->entry,
  5998. iso9660->birth_time, 0);
  5999. }
  6000. if (np->children.first != NULL) {
  6001. if (vdd->vdd_type != VDD_JOLIET &&
  6002. !iso9660->opt.rr && depth + 1 >= vdd->max_depth) {
  6003. if (np->children.cnt > 0)
  6004. iso9660->directories_too_deep = np;
  6005. } else {
  6006. /* Generate Identifier */
  6007. r = genid(a, np, &idr);
  6008. if (r < 0)
  6009. goto exit_traverse_tree;
  6010. r = isoent_make_sorted_files(a, np, &idr);
  6011. if (r < 0)
  6012. goto exit_traverse_tree;
  6013. if (np->subdirs.first != NULL &&
  6014. depth + 1 < vdd->max_depth) {
  6015. /* Enter to sub directories. */
  6016. np = np->subdirs.first;
  6017. depth++;
  6018. continue;
  6019. }
  6020. }
  6021. }
  6022. while (np != np->parent) {
  6023. if (np->drnext == NULL) {
  6024. /* Return to the parent directory. */
  6025. np = np->parent;
  6026. depth--;
  6027. } else {
  6028. np = np->drnext;
  6029. break;
  6030. }
  6031. }
  6032. } while (np != np->parent);
  6033. r = ARCHIVE_OK;
  6034. exit_traverse_tree:
  6035. idr_cleanup(&idr);
  6036. return (r);
  6037. }
  6038. /*
  6039. * Collect directory entries into path_table by a directory depth.
  6040. */
  6041. static int
  6042. isoent_collect_dirs(struct vdd *vdd, struct isoent *rootent, int depth)
  6043. {
  6044. struct isoent *np;
  6045. if (rootent == NULL)
  6046. rootent = vdd->rootent;
  6047. np = rootent;
  6048. do {
  6049. /* Register current directory to pathtable. */
  6050. path_table_add_entry(&(vdd->pathtbl[depth]), np);
  6051. if (np->subdirs.first != NULL && depth + 1 < vdd->max_depth) {
  6052. /* Enter to sub directories. */
  6053. np = np->subdirs.first;
  6054. depth++;
  6055. continue;
  6056. }
  6057. while (np != rootent) {
  6058. if (np->drnext == NULL) {
  6059. /* Return to the parent directory. */
  6060. np = np->parent;
  6061. depth--;
  6062. } else {
  6063. np = np->drnext;
  6064. break;
  6065. }
  6066. }
  6067. } while (np != rootent);
  6068. return (ARCHIVE_OK);
  6069. }
  6070. /*
  6071. * The entry whose number of levels in a directory hierarchy is
  6072. * large than eight relocate to rr_move directory.
  6073. */
  6074. static int
  6075. isoent_rr_move_dir(struct archive_write *a, struct isoent **rr_moved,
  6076. struct isoent *curent, struct isoent **newent)
  6077. {
  6078. struct iso9660 *iso9660 = a->format_data;
  6079. struct isoent *rrmoved, *mvent, *np;
  6080. if ((rrmoved = *rr_moved) == NULL) {
  6081. struct isoent *rootent = iso9660->primary.rootent;
  6082. /* There isn't rr_move entry.
  6083. * Create rr_move entry and insert it into the root entry.
  6084. */
  6085. rrmoved = isoent_create_virtual_dir(a, iso9660, "rr_moved");
  6086. if (rrmoved == NULL) {
  6087. archive_set_error(&a->archive, ENOMEM,
  6088. "Can't allocate memory");
  6089. return (ARCHIVE_FATAL);
  6090. }
  6091. /* Add "rr_moved" entry to the root entry. */
  6092. isoent_add_child_head(rootent, rrmoved);
  6093. archive_entry_set_nlink(rootent->file->entry,
  6094. archive_entry_nlink(rootent->file->entry) + 1);
  6095. /* Register "rr_moved" entry to second level pathtable. */
  6096. path_table_add_entry(&(iso9660->primary.pathtbl[1]), rrmoved);
  6097. /* Save rr_moved. */
  6098. *rr_moved = rrmoved;
  6099. }
  6100. /*
  6101. * Make a clone of curent which is going to be relocated
  6102. * to rr_moved.
  6103. */
  6104. mvent = isoent_clone(curent);
  6105. if (mvent == NULL) {
  6106. archive_set_error(&a->archive, ENOMEM,
  6107. "Can't allocate memory");
  6108. return (ARCHIVE_FATAL);
  6109. }
  6110. /* linking.. and use for creating "CL", "PL" and "RE" */
  6111. mvent->rr_parent = curent->parent;
  6112. curent->rr_child = mvent;
  6113. /*
  6114. * Move subdirectories from the curent to mvent
  6115. */
  6116. if (curent->children.first != NULL) {
  6117. *mvent->children.last = curent->children.first;
  6118. mvent->children.last = curent->children.last;
  6119. }
  6120. for (np = mvent->children.first; np != NULL; np = np->chnext)
  6121. np->parent = mvent;
  6122. mvent->children.cnt = curent->children.cnt;
  6123. curent->children.cnt = 0;
  6124. curent->children.first = NULL;
  6125. curent->children.last = &curent->children.first;
  6126. if (curent->subdirs.first != NULL) {
  6127. *mvent->subdirs.last = curent->subdirs.first;
  6128. mvent->subdirs.last = curent->subdirs.last;
  6129. }
  6130. mvent->subdirs.cnt = curent->subdirs.cnt;
  6131. curent->subdirs.cnt = 0;
  6132. curent->subdirs.first = NULL;
  6133. curent->subdirs.last = &curent->subdirs.first;
  6134. /*
  6135. * The mvent becomes a child of the rr_moved entry.
  6136. */
  6137. isoent_add_child_tail(rrmoved, mvent);
  6138. archive_entry_set_nlink(rrmoved->file->entry,
  6139. archive_entry_nlink(rrmoved->file->entry) + 1);
  6140. /*
  6141. * This entry which relocated to the rr_moved directory
  6142. * has to set the flag as a file.
  6143. * See also RRIP 4.1.5.1 Description of the "CL" System Use Entry.
  6144. */
  6145. curent->dir = 0;
  6146. *newent = mvent;
  6147. return (ARCHIVE_OK);
  6148. }
  6149. static int
  6150. isoent_rr_move(struct archive_write *a)
  6151. {
  6152. struct iso9660 *iso9660 = a->format_data;
  6153. struct path_table *pt;
  6154. struct isoent *rootent, *rr_moved;
  6155. struct isoent *np, *last;
  6156. int r;
  6157. pt = &(iso9660->primary.pathtbl[MAX_DEPTH-1]);
  6158. /* There aren't level 8 directories reaching a deeper level. */
  6159. if (pt->cnt == 0)
  6160. return (ARCHIVE_OK);
  6161. rootent = iso9660->primary.rootent;
  6162. /* If "rr_moved" directory is already existing,
  6163. * we have to use it. */
  6164. rr_moved = isoent_find_child(rootent, "rr_moved");
  6165. if (rr_moved != NULL &&
  6166. rr_moved != rootent->children.first) {
  6167. /*
  6168. * It's necessary that rr_move is the first entry
  6169. * of the root.
  6170. */
  6171. /* Remove "rr_moved" entry from children chain. */
  6172. isoent_remove_child(rootent, rr_moved);
  6173. /* Add "rr_moved" entry into the head of children chain. */
  6174. isoent_add_child_head(rootent, rr_moved);
  6175. }
  6176. /*
  6177. * Check level 8 path_table.
  6178. * If find out sub directory entries, that entries move to rr_move.
  6179. */
  6180. np = pt->first;
  6181. while (np != NULL) {
  6182. last = path_table_last_entry(pt);
  6183. for (; np != NULL; np = np->ptnext) {
  6184. struct isoent *mvent;
  6185. struct isoent *newent;
  6186. if (!np->dir)
  6187. continue;
  6188. for (mvent = np->subdirs.first;
  6189. mvent != NULL; mvent = mvent->drnext) {
  6190. r = isoent_rr_move_dir(a, &rr_moved,
  6191. mvent, &newent);
  6192. if (r < 0)
  6193. return (r);
  6194. isoent_collect_dirs(&(iso9660->primary),
  6195. newent, 2);
  6196. }
  6197. }
  6198. /* If new entries are added to level 8 path_talbe,
  6199. * its sub directory entries move to rr_move too.
  6200. */
  6201. np = last->ptnext;
  6202. }
  6203. return (ARCHIVE_OK);
  6204. }
  6205. /*
  6206. * This comparing rule is according to ISO9660 Standard 6.9.1
  6207. */
  6208. static int
  6209. _compare_path_table(const void *v1, const void *v2)
  6210. {
  6211. const struct isoent *p1, *p2;
  6212. const char *s1, *s2;
  6213. int cmp, l;
  6214. p1 = *((const struct isoent **)(uintptr_t)v1);
  6215. p2 = *((const struct isoent **)(uintptr_t)v2);
  6216. /* Compare parent directory number */
  6217. cmp = p1->parent->dir_number - p2->parent->dir_number;
  6218. if (cmp != 0)
  6219. return (cmp);
  6220. /* Compare identifier */
  6221. s1 = p1->identifier;
  6222. s2 = p2->identifier;
  6223. l = p1->ext_off;
  6224. if (l > p2->ext_off)
  6225. l = p2->ext_off;
  6226. cmp = strncmp(s1, s2, l);
  6227. if (cmp != 0)
  6228. return (cmp);
  6229. if (p1->ext_off < p2->ext_off) {
  6230. s2 += l;
  6231. l = p2->ext_off - p1->ext_off;
  6232. while (l--)
  6233. if (0x20 != *s2++)
  6234. return (0x20
  6235. - *(const unsigned char *)(s2 - 1));
  6236. } else if (p1->ext_off > p2->ext_off) {
  6237. s1 += l;
  6238. l = p1->ext_off - p2->ext_off;
  6239. while (l--)
  6240. if (0x20 != *s1++)
  6241. return (*(const unsigned char *)(s1 - 1)
  6242. - 0x20);
  6243. }
  6244. return (0);
  6245. }
  6246. static int
  6247. _compare_path_table_joliet(const void *v1, const void *v2)
  6248. {
  6249. const struct isoent *p1, *p2;
  6250. const unsigned char *s1, *s2;
  6251. int cmp, l;
  6252. p1 = *((const struct isoent **)(uintptr_t)v1);
  6253. p2 = *((const struct isoent **)(uintptr_t)v2);
  6254. /* Compare parent directory number */
  6255. cmp = p1->parent->dir_number - p2->parent->dir_number;
  6256. if (cmp != 0)
  6257. return (cmp);
  6258. /* Compare identifier */
  6259. s1 = (const unsigned char *)p1->identifier;
  6260. s2 = (const unsigned char *)p2->identifier;
  6261. l = p1->ext_off;
  6262. if (l > p2->ext_off)
  6263. l = p2->ext_off;
  6264. cmp = memcmp(s1, s2, l);
  6265. if (cmp != 0)
  6266. return (cmp);
  6267. if (p1->ext_off < p2->ext_off) {
  6268. s2 += l;
  6269. l = p2->ext_off - p1->ext_off;
  6270. while (l--)
  6271. if (0 != *s2++)
  6272. return (- *(const unsigned char *)(s2 - 1));
  6273. } else if (p1->ext_off > p2->ext_off) {
  6274. s1 += l;
  6275. l = p1->ext_off - p2->ext_off;
  6276. while (l--)
  6277. if (0 != *s1++)
  6278. return (*(const unsigned char *)(s1 - 1));
  6279. }
  6280. return (0);
  6281. }
  6282. static inline void
  6283. path_table_add_entry(struct path_table *pathtbl, struct isoent *ent)
  6284. {
  6285. ent->ptnext = NULL;
  6286. *pathtbl->last = ent;
  6287. pathtbl->last = &(ent->ptnext);
  6288. pathtbl->cnt ++;
  6289. }
  6290. static inline struct isoent *
  6291. path_table_last_entry(struct path_table *pathtbl)
  6292. {
  6293. if (pathtbl->first == NULL)
  6294. return (NULL);
  6295. return (((struct isoent *)(void *)
  6296. ((char *)(pathtbl->last) - offsetof(struct isoent, ptnext))));
  6297. }
  6298. /*
  6299. * Sort directory entries in path_table
  6300. * and assign directory number to each entries.
  6301. */
  6302. static int
  6303. isoent_make_path_table_2(struct archive_write *a, struct vdd *vdd,
  6304. int depth, int *dir_number)
  6305. {
  6306. struct isoent *np;
  6307. struct isoent **enttbl;
  6308. struct path_table *pt;
  6309. int i;
  6310. pt = &vdd->pathtbl[depth];
  6311. if (pt->cnt == 0) {
  6312. pt->sorted = NULL;
  6313. return (ARCHIVE_OK);
  6314. }
  6315. enttbl = malloc(pt->cnt * sizeof(struct isoent *));
  6316. if (enttbl == NULL) {
  6317. archive_set_error(&a->archive, ENOMEM,
  6318. "Can't allocate memory");
  6319. return (ARCHIVE_FATAL);
  6320. }
  6321. pt->sorted = enttbl;
  6322. for (np = pt->first; np != NULL; np = np->ptnext)
  6323. *enttbl ++ = np;
  6324. enttbl = pt->sorted;
  6325. switch (vdd->vdd_type) {
  6326. case VDD_PRIMARY:
  6327. case VDD_ENHANCED:
  6328. #ifdef __COMPAR_FN_T
  6329. qsort(enttbl, pt->cnt, sizeof(struct isoent *),
  6330. (__compar_fn_t)_compare_path_table);
  6331. #else
  6332. qsort(enttbl, pt->cnt, sizeof(struct isoent *),
  6333. _compare_path_table);
  6334. #endif
  6335. break;
  6336. case VDD_JOLIET:
  6337. #ifdef __COMPAR_FN_T
  6338. qsort(enttbl, pt->cnt, sizeof(struct isoent *),
  6339. (__compar_fn_t)_compare_path_table_joliet);
  6340. #else
  6341. qsort(enttbl, pt->cnt, sizeof(struct isoent *),
  6342. _compare_path_table_joliet);
  6343. #endif
  6344. break;
  6345. }
  6346. for (i = 0; i < pt->cnt; i++)
  6347. enttbl[i]->dir_number = (*dir_number)++;
  6348. return (ARCHIVE_OK);
  6349. }
  6350. static int
  6351. isoent_alloc_path_table(struct archive_write *a, struct vdd *vdd,
  6352. int max_depth)
  6353. {
  6354. int i;
  6355. vdd->max_depth = max_depth;
  6356. vdd->pathtbl = malloc(sizeof(*vdd->pathtbl) * vdd->max_depth);
  6357. if (vdd->pathtbl == NULL) {
  6358. archive_set_error(&a->archive, ENOMEM,
  6359. "Can't allocate memory");
  6360. return (ARCHIVE_FATAL);
  6361. }
  6362. for (i = 0; i < vdd->max_depth; i++) {
  6363. vdd->pathtbl[i].first = NULL;
  6364. vdd->pathtbl[i].last = &(vdd->pathtbl[i].first);
  6365. vdd->pathtbl[i].sorted = NULL;
  6366. vdd->pathtbl[i].cnt = 0;
  6367. }
  6368. return (ARCHIVE_OK);
  6369. }
  6370. /*
  6371. * Make Path Tables
  6372. */
  6373. static int
  6374. isoent_make_path_table(struct archive_write *a)
  6375. {
  6376. struct iso9660 *iso9660 = a->format_data;
  6377. int depth, r;
  6378. int dir_number;
  6379. /*
  6380. * Init Path Table.
  6381. */
  6382. if (iso9660->dircnt_max >= MAX_DEPTH &&
  6383. (!iso9660->opt.limit_depth || iso9660->opt.iso_level == 4))
  6384. r = isoent_alloc_path_table(a, &(iso9660->primary),
  6385. iso9660->dircnt_max + 1);
  6386. else
  6387. /* The number of levels in the hierarchy cannot exceed
  6388. * eight. */
  6389. r = isoent_alloc_path_table(a, &(iso9660->primary),
  6390. MAX_DEPTH);
  6391. if (r < 0)
  6392. return (r);
  6393. if (iso9660->opt.joliet) {
  6394. r = isoent_alloc_path_table(a, &(iso9660->joliet),
  6395. iso9660->dircnt_max + 1);
  6396. if (r < 0)
  6397. return (r);
  6398. }
  6399. /* Step 0.
  6400. * - Collect directories for primary and joliet.
  6401. */
  6402. isoent_collect_dirs(&(iso9660->primary), NULL, 0);
  6403. if (iso9660->opt.joliet)
  6404. isoent_collect_dirs(&(iso9660->joliet), NULL, 0);
  6405. /*
  6406. * Rockridge; move deeper depth directories to rr_moved.
  6407. */
  6408. if (iso9660->opt.rr) {
  6409. r = isoent_rr_move(a);
  6410. if (r < 0)
  6411. return (r);
  6412. }
  6413. /* Update nlink. */
  6414. isofile_connect_hardlink_files(iso9660);
  6415. /* Step 1.
  6416. * - Renew a value of the depth of that directories.
  6417. * - Resolve hardlinks.
  6418. * - Convert pathnames to ISO9660 name or UCS2(joliet).
  6419. * - Sort files by each directory.
  6420. */
  6421. r = isoent_traverse_tree(a, &(iso9660->primary));
  6422. if (r < 0)
  6423. return (r);
  6424. if (iso9660->opt.joliet) {
  6425. r = isoent_traverse_tree(a, &(iso9660->joliet));
  6426. if (r < 0)
  6427. return (r);
  6428. }
  6429. /* Step 2.
  6430. * - Sort directories.
  6431. * - Assign all directory number.
  6432. */
  6433. dir_number = 1;
  6434. for (depth = 0; depth < iso9660->primary.max_depth; depth++) {
  6435. r = isoent_make_path_table_2(a, &(iso9660->primary),
  6436. depth, &dir_number);
  6437. if (r < 0)
  6438. return (r);
  6439. }
  6440. if (iso9660->opt.joliet) {
  6441. dir_number = 1;
  6442. for (depth = 0; depth < iso9660->joliet.max_depth; depth++) {
  6443. r = isoent_make_path_table_2(a, &(iso9660->joliet),
  6444. depth, &dir_number);
  6445. if (r < 0)
  6446. return (r);
  6447. }
  6448. }
  6449. if (iso9660->opt.limit_dirs && dir_number > 0xffff) {
  6450. /*
  6451. * Maximum number of directories is 65535(0xffff)
  6452. * doe to size(16bit) of Parent Directory Number of
  6453. * the Path Table.
  6454. * See also ISO9660 Standard 9.4.
  6455. */
  6456. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  6457. "Too many directories(%d) over 65535.", dir_number);
  6458. return (ARCHIVE_FATAL);
  6459. }
  6460. /* Get the size of the Path Table. */
  6461. calculate_path_table_size(&(iso9660->primary));
  6462. if (iso9660->opt.joliet)
  6463. calculate_path_table_size(&(iso9660->joliet));
  6464. return (ARCHIVE_OK);
  6465. }
  6466. static int
  6467. isoent_find_out_boot_file(struct archive_write *a, struct isoent *rootent)
  6468. {
  6469. struct iso9660 *iso9660 = a->format_data;
  6470. /* Find a isoent of the boot file. */
  6471. iso9660->el_torito.boot = isoent_find_entry(rootent,
  6472. iso9660->el_torito.boot_filename.s);
  6473. if (iso9660->el_torito.boot == NULL) {
  6474. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  6475. "Can't find the boot image file ``%s''",
  6476. iso9660->el_torito.boot_filename.s);
  6477. return (ARCHIVE_FATAL);
  6478. }
  6479. iso9660->el_torito.boot->file->boot = BOOT_IMAGE;
  6480. return (ARCHIVE_OK);
  6481. }
  6482. static int
  6483. isoent_create_boot_catalog(struct archive_write *a, struct isoent *rootent)
  6484. {
  6485. struct iso9660 *iso9660 = a->format_data;
  6486. struct isofile *file;
  6487. struct isoent *isoent;
  6488. struct archive_entry *entry;
  6489. (void)rootent; /* UNUSED */
  6490. /*
  6491. * Create the entry which is the "boot.catalog" file.
  6492. */
  6493. file = isofile_new(a, NULL);
  6494. if (file == NULL) {
  6495. archive_set_error(&a->archive, ENOMEM,
  6496. "Can't allocate memory");
  6497. return (ARCHIVE_FATAL);
  6498. }
  6499. archive_entry_set_pathname(file->entry,
  6500. iso9660->el_torito.catalog_filename.s);
  6501. archive_entry_set_size(file->entry, LOGICAL_BLOCK_SIZE);
  6502. archive_entry_set_mtime(file->entry, iso9660->birth_time, 0);
  6503. archive_entry_set_atime(file->entry, iso9660->birth_time, 0);
  6504. archive_entry_set_ctime(file->entry, iso9660->birth_time, 0);
  6505. archive_entry_set_uid(file->entry, getuid());
  6506. archive_entry_set_gid(file->entry, getgid());
  6507. archive_entry_set_mode(file->entry, AE_IFREG | 0444);
  6508. archive_entry_set_nlink(file->entry, 1);
  6509. if (isofile_gen_utility_names(a, file) < ARCHIVE_WARN) {
  6510. isofile_free(file);
  6511. return (ARCHIVE_FATAL);
  6512. }
  6513. file->boot = BOOT_CATALOG;
  6514. file->content.size = LOGICAL_BLOCK_SIZE;
  6515. isofile_add_entry(iso9660, file);
  6516. isoent = isoent_new(file);
  6517. if (isoent == NULL) {
  6518. archive_set_error(&a->archive, ENOMEM,
  6519. "Can't allocate memory");
  6520. return (ARCHIVE_FATAL);
  6521. }
  6522. isoent->virtual = 1;
  6523. /* Add the "boot.catalog" entry into tree */
  6524. if (isoent_tree(a, &isoent) != ARCHIVE_OK)
  6525. return (ARCHIVE_FATAL);
  6526. iso9660->el_torito.catalog = isoent;
  6527. /*
  6528. * Get a boot media type.
  6529. */
  6530. switch (iso9660->opt.boot_type) {
  6531. default:
  6532. case OPT_BOOT_TYPE_AUTO:
  6533. /* Try detecting a media type of the boot image. */
  6534. entry = iso9660->el_torito.boot->file->entry;
  6535. if (archive_entry_size(entry) == FD_1_2M_SIZE)
  6536. iso9660->el_torito.media_type =
  6537. BOOT_MEDIA_1_2M_DISKETTE;
  6538. else if (archive_entry_size(entry) == FD_1_44M_SIZE)
  6539. iso9660->el_torito.media_type =
  6540. BOOT_MEDIA_1_44M_DISKETTE;
  6541. else if (archive_entry_size(entry) == FD_2_88M_SIZE)
  6542. iso9660->el_torito.media_type =
  6543. BOOT_MEDIA_2_88M_DISKETTE;
  6544. else
  6545. /* We cannot decide whether the boot image is
  6546. * hard-disk. */
  6547. iso9660->el_torito.media_type =
  6548. BOOT_MEDIA_NO_EMULATION;
  6549. break;
  6550. case OPT_BOOT_TYPE_NO_EMU:
  6551. iso9660->el_torito.media_type = BOOT_MEDIA_NO_EMULATION;
  6552. break;
  6553. case OPT_BOOT_TYPE_HARD_DISK:
  6554. iso9660->el_torito.media_type = BOOT_MEDIA_HARD_DISK;
  6555. break;
  6556. case OPT_BOOT_TYPE_FD:
  6557. entry = iso9660->el_torito.boot->file->entry;
  6558. if (archive_entry_size(entry) <= FD_1_2M_SIZE)
  6559. iso9660->el_torito.media_type =
  6560. BOOT_MEDIA_1_2M_DISKETTE;
  6561. else if (archive_entry_size(entry) <= FD_1_44M_SIZE)
  6562. iso9660->el_torito.media_type =
  6563. BOOT_MEDIA_1_44M_DISKETTE;
  6564. else if (archive_entry_size(entry) <= FD_2_88M_SIZE)
  6565. iso9660->el_torito.media_type =
  6566. BOOT_MEDIA_2_88M_DISKETTE;
  6567. else {
  6568. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  6569. "Boot image file(``%s'') size is too big "
  6570. "for fd type.",
  6571. iso9660->el_torito.boot_filename.s);
  6572. return (ARCHIVE_FATAL);
  6573. }
  6574. break;
  6575. }
  6576. /*
  6577. * Get a system type.
  6578. * TODO: `El Torito' specification says "A copy of byte 5 from the
  6579. * Partition Table found in the boot image".
  6580. */
  6581. iso9660->el_torito.system_type = 0;
  6582. /*
  6583. * Get an ID.
  6584. */
  6585. if (iso9660->opt.publisher)
  6586. archive_string_copy(&(iso9660->el_torito.id),
  6587. &(iso9660->publisher_identifier));
  6588. return (ARCHIVE_OK);
  6589. }
  6590. /*
  6591. * If a media type is floppy, return its image size.
  6592. * otherwise return 0.
  6593. */
  6594. static size_t
  6595. fd_boot_image_size(int media_type)
  6596. {
  6597. switch (media_type) {
  6598. case BOOT_MEDIA_1_2M_DISKETTE:
  6599. return (FD_1_2M_SIZE);
  6600. case BOOT_MEDIA_1_44M_DISKETTE:
  6601. return (FD_1_44M_SIZE);
  6602. case BOOT_MEDIA_2_88M_DISKETTE:
  6603. return (FD_2_88M_SIZE);
  6604. default:
  6605. return (0);
  6606. }
  6607. }
  6608. /*
  6609. * Make a boot catalog image data.
  6610. */
  6611. static int
  6612. make_boot_catalog(struct archive_write *a)
  6613. {
  6614. struct iso9660 *iso9660 = a->format_data;
  6615. unsigned char *block;
  6616. unsigned char *p;
  6617. uint16_t sum, *wp;
  6618. block = wb_buffptr(a);
  6619. memset(block, 0, LOGICAL_BLOCK_SIZE);
  6620. p = block;
  6621. /*
  6622. * Validation Entry
  6623. */
  6624. /* Header ID */
  6625. p[0] = 1;
  6626. /* Platform ID */
  6627. p[1] = iso9660->el_torito.platform_id;
  6628. /* Reserved */
  6629. p[2] = p[3] = 0;
  6630. /* ID */
  6631. if (archive_strlen(&(iso9660->el_torito.id)) > 0)
  6632. strncpy((char *)p+4, iso9660->el_torito.id.s, 23);
  6633. p[27] = 0;
  6634. /* Checksum */
  6635. p[28] = p[29] = 0;
  6636. /* Key */
  6637. p[30] = 0x55;
  6638. p[31] = 0xAA;
  6639. sum = 0;
  6640. wp = (uint16_t *)block;
  6641. while (wp < (uint16_t *)&block[32])
  6642. sum += archive_le16dec(wp++);
  6643. set_num_721(&block[28], (~sum) + 1);
  6644. /*
  6645. * Initial/Default Entry
  6646. */
  6647. p = &block[32];
  6648. /* Boot Indicator */
  6649. p[0] = 0x88;
  6650. /* Boot media type */
  6651. p[1] = iso9660->el_torito.media_type;
  6652. /* Load Segment */
  6653. if (iso9660->el_torito.media_type == BOOT_MEDIA_NO_EMULATION)
  6654. set_num_721(&p[2], iso9660->el_torito.boot_load_seg);
  6655. else
  6656. set_num_721(&p[2], 0);
  6657. /* System Type */
  6658. p[4] = iso9660->el_torito.system_type;
  6659. /* Unused */
  6660. p[5] = 0;
  6661. /* Sector Count */
  6662. if (iso9660->el_torito.media_type == BOOT_MEDIA_NO_EMULATION)
  6663. set_num_721(&p[6], iso9660->el_torito.boot_load_size);
  6664. else
  6665. set_num_721(&p[6], 1);
  6666. /* Load RBA */
  6667. set_num_731(&p[8],
  6668. iso9660->el_torito.boot->file->content.location);
  6669. /* Unused */
  6670. memset(&p[12], 0, 20);
  6671. return (wb_consume(a, LOGICAL_BLOCK_SIZE));
  6672. }
  6673. static int
  6674. setup_boot_information(struct archive_write *a)
  6675. {
  6676. struct iso9660 *iso9660 = a->format_data;
  6677. struct isoent *np;
  6678. int64_t size;
  6679. uint32_t sum;
  6680. unsigned char buff[4096];
  6681. np = iso9660->el_torito.boot;
  6682. lseek(iso9660->temp_fd,
  6683. np->file->content.offset_of_temp + 64, SEEK_SET);
  6684. size = archive_entry_size(np->file->entry) - 64;
  6685. if (size <= 0) {
  6686. archive_set_error(&a->archive, errno,
  6687. "Boot file(%jd) is too small", (intmax_t)size + 64);
  6688. return (ARCHIVE_FATAL);
  6689. }
  6690. sum = 0;
  6691. while (size > 0) {
  6692. size_t rsize;
  6693. ssize_t i, rs;
  6694. if (size > (int64_t)sizeof(buff))
  6695. rsize = sizeof(buff);
  6696. else
  6697. rsize = (size_t)size;
  6698. rs = read(iso9660->temp_fd, buff, rsize);
  6699. if (rs <= 0) {
  6700. archive_set_error(&a->archive, errno,
  6701. "Can't read temporary file(%jd)",
  6702. (intmax_t)rs);
  6703. return (ARCHIVE_FATAL);
  6704. }
  6705. for (i = 0; i < rs; i += 4)
  6706. sum += archive_le32dec(buff + i);
  6707. size -= rs;
  6708. }
  6709. /* Set the location of Primary Volume Descriptor. */
  6710. set_num_731(buff, SYSTEM_AREA_BLOCK);
  6711. /* Set the location of the boot file. */
  6712. set_num_731(buff+4, np->file->content.location);
  6713. /* Set the size of the boot file. */
  6714. size = fd_boot_image_size(iso9660->el_torito.media_type);
  6715. if (size == 0)
  6716. size = archive_entry_size(np->file->entry);
  6717. set_num_731(buff+8, (uint32_t)size);
  6718. /* Set the sum of the boot file. */
  6719. set_num_731(buff+12, sum);
  6720. /* Clear reserved bytes. */
  6721. memset(buff+16, 0, 40);
  6722. /* Overwrite the boot file. */
  6723. lseek(iso9660->temp_fd,
  6724. np->file->content.offset_of_temp + 8, SEEK_SET);
  6725. return (write_to_temp(a, buff, 56));
  6726. }
  6727. #ifdef HAVE_ZLIB_H
  6728. static int
  6729. zisofs_init_zstream(struct archive_write *a)
  6730. {
  6731. struct iso9660 *iso9660 = a->format_data;
  6732. int r;
  6733. iso9660->zisofs.stream.next_in = NULL;
  6734. iso9660->zisofs.stream.avail_in = 0;
  6735. iso9660->zisofs.stream.total_in = 0;
  6736. iso9660->zisofs.stream.total_out = 0;
  6737. if (iso9660->zisofs.stream_valid)
  6738. r = deflateReset(&(iso9660->zisofs.stream));
  6739. else {
  6740. r = deflateInit(&(iso9660->zisofs.stream),
  6741. iso9660->zisofs.compression_level);
  6742. iso9660->zisofs.stream_valid = 1;
  6743. }
  6744. switch (r) {
  6745. case Z_OK:
  6746. break;
  6747. default:
  6748. case Z_STREAM_ERROR:
  6749. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  6750. "Internal error initializing "
  6751. "compression library: invalid setup parameter");
  6752. return (ARCHIVE_FATAL);
  6753. case Z_MEM_ERROR:
  6754. archive_set_error(&a->archive, ENOMEM,
  6755. "Internal error initializing "
  6756. "compression library");
  6757. return (ARCHIVE_FATAL);
  6758. case Z_VERSION_ERROR:
  6759. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  6760. "Internal error initializing "
  6761. "compression library: invalid library version");
  6762. return (ARCHIVE_FATAL);
  6763. }
  6764. return (ARCHIVE_OK);
  6765. }
  6766. #endif /* HAVE_ZLIB_H */
  6767. static int
  6768. zisofs_init(struct archive_write *a, struct isofile *file)
  6769. {
  6770. struct iso9660 *iso9660 = a->format_data;
  6771. #ifdef HAVE_ZLIB_H
  6772. uint64_t tsize;
  6773. size_t _ceil, bpsize;
  6774. int r;
  6775. #endif
  6776. iso9660->zisofs.detect_magic = 0;
  6777. iso9660->zisofs.making = 0;
  6778. if (!iso9660->opt.rr || !iso9660->opt.zisofs)
  6779. return (ARCHIVE_OK);
  6780. if (archive_entry_size(file->entry) >= 24 &&
  6781. archive_entry_size(file->entry) < MULTI_EXTENT_SIZE) {
  6782. /* Acceptable file size for zisofs. */
  6783. iso9660->zisofs.detect_magic = 1;
  6784. iso9660->zisofs.magic_cnt = 0;
  6785. }
  6786. if (!iso9660->zisofs.detect_magic)
  6787. return (ARCHIVE_OK);
  6788. #ifdef HAVE_ZLIB_H
  6789. /* The number of Logical Blocks which uncompressed data
  6790. * will use in iso-image file is the same as the number of
  6791. * Logical Blocks which zisofs(compressed) data will use
  6792. * in ISO-image file. It won't reduce iso-image file size. */
  6793. if (archive_entry_size(file->entry) <= LOGICAL_BLOCK_SIZE)
  6794. return (ARCHIVE_OK);
  6795. /* Initialize compression library */
  6796. r = zisofs_init_zstream(a);
  6797. if (r != ARCHIVE_OK)
  6798. return (ARCHIVE_FATAL);
  6799. /* Mark file->zisofs to create RRIP 'ZF' Use Entry. */
  6800. file->zisofs.header_size = ZF_HEADER_SIZE >> 2;
  6801. file->zisofs.log2_bs = ZF_LOG2_BS;
  6802. file->zisofs.uncompressed_size =
  6803. (uint32_t)archive_entry_size(file->entry);
  6804. /* Calculate a size of Block Pointers of zisofs. */
  6805. _ceil = (file->zisofs.uncompressed_size + ZF_BLOCK_SIZE -1)
  6806. >> file->zisofs.log2_bs;
  6807. iso9660->zisofs.block_pointers_cnt = (int)_ceil + 1;
  6808. iso9660->zisofs.block_pointers_idx = 0;
  6809. /* Ensure a buffer size used for Block Pointers */
  6810. bpsize = iso9660->zisofs.block_pointers_cnt *
  6811. sizeof(iso9660->zisofs.block_pointers[0]);
  6812. if (iso9660->zisofs.block_pointers_allocated < bpsize) {
  6813. free(iso9660->zisofs.block_pointers);
  6814. iso9660->zisofs.block_pointers = malloc(bpsize);
  6815. if (iso9660->zisofs.block_pointers == NULL) {
  6816. archive_set_error(&a->archive, ENOMEM,
  6817. "Can't allocate data");
  6818. return (ARCHIVE_FATAL);
  6819. }
  6820. iso9660->zisofs.block_pointers_allocated = bpsize;
  6821. }
  6822. /*
  6823. * Skip zisofs header and Block Pointers, which we will write
  6824. * after all compressed data of a file written to the temporary
  6825. * file.
  6826. */
  6827. tsize = ZF_HEADER_SIZE + bpsize;
  6828. if (write_null(a, (size_t)tsize) != ARCHIVE_OK)
  6829. return (ARCHIVE_FATAL);
  6830. /*
  6831. * Initialize some variables to make zisofs.
  6832. */
  6833. archive_le32enc(&(iso9660->zisofs.block_pointers[0]),
  6834. (uint32_t)tsize);
  6835. iso9660->zisofs.remaining = file->zisofs.uncompressed_size;
  6836. iso9660->zisofs.making = 1;
  6837. iso9660->zisofs.allzero = 1;
  6838. iso9660->zisofs.block_offset = tsize;
  6839. iso9660->zisofs.total_size = tsize;
  6840. iso9660->cur_file->cur_content->size = tsize;
  6841. #endif
  6842. return (ARCHIVE_OK);
  6843. }
  6844. static void
  6845. zisofs_detect_magic(struct archive_write *a, const void *buff, size_t s)
  6846. {
  6847. struct iso9660 *iso9660 = a->format_data;
  6848. struct isofile *file = iso9660->cur_file;
  6849. const unsigned char *p, *endp;
  6850. const unsigned char *magic_buff;
  6851. uint32_t uncompressed_size;
  6852. unsigned char header_size;
  6853. unsigned char log2_bs;
  6854. size_t _ceil, doff;
  6855. uint32_t bst, bed;
  6856. int magic_max;
  6857. int64_t entry_size;
  6858. entry_size = archive_entry_size(file->entry);
  6859. if ((int64_t)sizeof(iso9660->zisofs.magic_buffer) > entry_size)
  6860. magic_max = (int)entry_size;
  6861. else
  6862. magic_max = sizeof(iso9660->zisofs.magic_buffer);
  6863. if (iso9660->zisofs.magic_cnt == 0 && s >= (size_t)magic_max)
  6864. /* It's unnecessary we copy buffer. */
  6865. magic_buff = buff;
  6866. else {
  6867. if (iso9660->zisofs.magic_cnt < magic_max) {
  6868. size_t l;
  6869. l = sizeof(iso9660->zisofs.magic_buffer)
  6870. - iso9660->zisofs.magic_cnt;
  6871. if (l > s)
  6872. l = s;
  6873. memcpy(iso9660->zisofs.magic_buffer
  6874. + iso9660->zisofs.magic_cnt, buff, l);
  6875. iso9660->zisofs.magic_cnt += (int)l;
  6876. if (iso9660->zisofs.magic_cnt < magic_max)
  6877. return;
  6878. }
  6879. magic_buff = iso9660->zisofs.magic_buffer;
  6880. }
  6881. iso9660->zisofs.detect_magic = 0;
  6882. p = magic_buff;
  6883. /* Check the magic code of zisofs. */
  6884. if (memcmp(p, zisofs_magic, sizeof(zisofs_magic)) != 0)
  6885. /* This is not zisofs file which made by mkzftree. */
  6886. return;
  6887. p += sizeof(zisofs_magic);
  6888. /* Read a zisofs header. */
  6889. uncompressed_size = archive_le32dec(p);
  6890. header_size = p[4];
  6891. log2_bs = p[5];
  6892. if (uncompressed_size < 24 || header_size != 4 ||
  6893. log2_bs > 30 || log2_bs < 7)
  6894. return;/* Invalid or not supported header. */
  6895. /* Calculate a size of Block Pointers of zisofs. */
  6896. _ceil = (uncompressed_size +
  6897. (ARCHIVE_LITERAL_LL(1) << log2_bs) -1) >> log2_bs;
  6898. doff = (_ceil + 1) * 4 + 16;
  6899. if (entry_size < (int64_t)doff)
  6900. return;/* Invalid data. */
  6901. /* Check every Block Pointer has valid value. */
  6902. p = magic_buff + 16;
  6903. endp = magic_buff + magic_max;
  6904. while (_ceil && p + 8 <= endp) {
  6905. bst = archive_le32dec(p);
  6906. if (bst != doff)
  6907. return;/* Invalid data. */
  6908. p += 4;
  6909. bed = archive_le32dec(p);
  6910. if (bed < bst || bed > entry_size)
  6911. return;/* Invalid data. */
  6912. doff += bed - bst;
  6913. _ceil--;
  6914. }
  6915. file->zisofs.uncompressed_size = uncompressed_size;
  6916. file->zisofs.header_size = header_size;
  6917. file->zisofs.log2_bs = log2_bs;
  6918. /* Disable making a zisofs image. */
  6919. iso9660->zisofs.making = 0;
  6920. }
  6921. #ifdef HAVE_ZLIB_H
  6922. /*
  6923. * Compress data and write it to a temporary file.
  6924. */
  6925. static int
  6926. zisofs_write_to_temp(struct archive_write *a, const void *buff, size_t s)
  6927. {
  6928. struct iso9660 *iso9660 = a->format_data;
  6929. struct isofile *file = iso9660->cur_file;
  6930. const unsigned char *b;
  6931. z_stream *zstrm;
  6932. size_t avail, csize;
  6933. int flush, r;
  6934. zstrm = &(iso9660->zisofs.stream);
  6935. zstrm->next_out = wb_buffptr(a);
  6936. zstrm->avail_out = (uInt)wb_remaining(a);
  6937. b = (const unsigned char *)buff;
  6938. do {
  6939. avail = ZF_BLOCK_SIZE - zstrm->total_in;
  6940. if (s < avail) {
  6941. avail = s;
  6942. flush = Z_NO_FLUSH;
  6943. } else
  6944. flush = Z_FINISH;
  6945. iso9660->zisofs.remaining -= avail;
  6946. if (iso9660->zisofs.remaining <= 0)
  6947. flush = Z_FINISH;
  6948. zstrm->next_in = (Bytef *)(uintptr_t)(const void *)b;
  6949. zstrm->avail_in = (uInt)avail;
  6950. /*
  6951. * Check if current data block are all zero.
  6952. */
  6953. if (iso9660->zisofs.allzero) {
  6954. const unsigned char *nonzero = b;
  6955. const unsigned char *nonzeroend = b + avail;
  6956. while (nonzero < nonzeroend)
  6957. if (*nonzero++) {
  6958. iso9660->zisofs.allzero = 0;
  6959. break;
  6960. }
  6961. }
  6962. b += avail;
  6963. s -= avail;
  6964. /*
  6965. * If current data block are all zero, we do not use
  6966. * compressed data.
  6967. */
  6968. if (flush == Z_FINISH && iso9660->zisofs.allzero &&
  6969. avail + zstrm->total_in == ZF_BLOCK_SIZE) {
  6970. if (iso9660->zisofs.block_offset !=
  6971. file->cur_content->size) {
  6972. int64_t diff;
  6973. r = wb_set_offset(a,
  6974. file->cur_content->offset_of_temp +
  6975. iso9660->zisofs.block_offset);
  6976. if (r != ARCHIVE_OK)
  6977. return (r);
  6978. diff = file->cur_content->size -
  6979. iso9660->zisofs.block_offset;
  6980. file->cur_content->size -= diff;
  6981. iso9660->zisofs.total_size -= diff;
  6982. }
  6983. zstrm->avail_in = 0;
  6984. }
  6985. /*
  6986. * Compress file data.
  6987. */
  6988. while (zstrm->avail_in > 0) {
  6989. csize = zstrm->total_out;
  6990. r = deflate(zstrm, flush);
  6991. switch (r) {
  6992. case Z_OK:
  6993. case Z_STREAM_END:
  6994. csize = zstrm->total_out - csize;
  6995. if (wb_consume(a, csize) != ARCHIVE_OK)
  6996. return (ARCHIVE_FATAL);
  6997. iso9660->zisofs.total_size += csize;
  6998. iso9660->cur_file->cur_content->size += csize;
  6999. zstrm->next_out = wb_buffptr(a);
  7000. zstrm->avail_out = (uInt)wb_remaining(a);
  7001. break;
  7002. default:
  7003. archive_set_error(&a->archive,
  7004. ARCHIVE_ERRNO_MISC,
  7005. "Compression failed:"
  7006. " deflate() call returned status %d",
  7007. r);
  7008. return (ARCHIVE_FATAL);
  7009. }
  7010. }
  7011. if (flush == Z_FINISH) {
  7012. /*
  7013. * Save the information of one zisofs block.
  7014. */
  7015. iso9660->zisofs.block_pointers_idx ++;
  7016. archive_le32enc(&(iso9660->zisofs.block_pointers[
  7017. iso9660->zisofs.block_pointers_idx]),
  7018. (uint32_t)iso9660->zisofs.total_size);
  7019. r = zisofs_init_zstream(a);
  7020. if (r != ARCHIVE_OK)
  7021. return (ARCHIVE_FATAL);
  7022. iso9660->zisofs.allzero = 1;
  7023. iso9660->zisofs.block_offset = file->cur_content->size;
  7024. }
  7025. } while (s);
  7026. return (ARCHIVE_OK);
  7027. }
  7028. static int
  7029. zisofs_finish_entry(struct archive_write *a)
  7030. {
  7031. struct iso9660 *iso9660 = a->format_data;
  7032. struct isofile *file = iso9660->cur_file;
  7033. unsigned char buff[16];
  7034. size_t s;
  7035. int64_t tail;
  7036. /* Direct temp file stream to zisofs temp file stream. */
  7037. archive_entry_set_size(file->entry, iso9660->zisofs.total_size);
  7038. /*
  7039. * Save a file pointer which points the end of current zisofs data.
  7040. */
  7041. tail = wb_offset(a);
  7042. /*
  7043. * Make a header.
  7044. *
  7045. * +-----------------+----------------+-----------------+
  7046. * | Header 16 bytes | Block Pointers | Compressed data |
  7047. * +-----------------+----------------+-----------------+
  7048. * 0 16 +X
  7049. * Block Pointers :
  7050. * 4 * (((Uncompressed file size + block_size -1) / block_size) + 1)
  7051. *
  7052. * Write zisofs header.
  7053. * Magic number
  7054. * +----+----+----+----+----+----+----+----+
  7055. * | 37 | E4 | 53 | 96 | C9 | DB | D6 | 07 |
  7056. * +----+----+----+----+----+----+----+----+
  7057. * 0 1 2 3 4 5 6 7 8
  7058. *
  7059. * +------------------------+------------------+
  7060. * | Uncompressed file size | header_size >> 2 |
  7061. * +------------------------+------------------+
  7062. * 8 12 13
  7063. *
  7064. * +-----------------+----------------+
  7065. * | log2 block_size | Reserved(0000) |
  7066. * +-----------------+----------------+
  7067. * 13 14 16
  7068. */
  7069. memcpy(buff, zisofs_magic, 8);
  7070. set_num_731(buff+8, file->zisofs.uncompressed_size);
  7071. buff[12] = file->zisofs.header_size;
  7072. buff[13] = file->zisofs.log2_bs;
  7073. buff[14] = buff[15] = 0;/* Reserved */
  7074. /* Move to the right position to write the header. */
  7075. wb_set_offset(a, file->content.offset_of_temp);
  7076. /* Write the header. */
  7077. if (wb_write_to_temp(a, buff, 16) != ARCHIVE_OK)
  7078. return (ARCHIVE_FATAL);
  7079. /*
  7080. * Write zisofs Block Pointers.
  7081. */
  7082. s = iso9660->zisofs.block_pointers_cnt *
  7083. sizeof(iso9660->zisofs.block_pointers[0]);
  7084. if (wb_write_to_temp(a, iso9660->zisofs.block_pointers, s)
  7085. != ARCHIVE_OK)
  7086. return (ARCHIVE_FATAL);
  7087. /* Set a file pointer back to the end of the temporary file. */
  7088. wb_set_offset(a, tail);
  7089. return (ARCHIVE_OK);
  7090. }
  7091. static int
  7092. zisofs_free(struct archive_write *a)
  7093. {
  7094. struct iso9660 *iso9660 = a->format_data;
  7095. int ret = ARCHIVE_OK;
  7096. free(iso9660->zisofs.block_pointers);
  7097. if (iso9660->zisofs.stream_valid &&
  7098. deflateEnd(&(iso9660->zisofs.stream)) != Z_OK) {
  7099. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  7100. "Failed to clean up compressor");
  7101. ret = ARCHIVE_FATAL;
  7102. }
  7103. iso9660->zisofs.block_pointers = NULL;
  7104. iso9660->zisofs.stream_valid = 0;
  7105. return (ret);
  7106. }
  7107. struct zisofs_extract {
  7108. int pz_log2_bs; /* Log2 of block size */
  7109. uint64_t pz_uncompressed_size;
  7110. size_t uncompressed_buffer_size;
  7111. signed int initialized:1;
  7112. signed int header_passed:1;
  7113. uint32_t pz_offset;
  7114. unsigned char *block_pointers;
  7115. size_t block_pointers_size;
  7116. size_t block_pointers_avail;
  7117. size_t block_off;
  7118. uint32_t block_avail;
  7119. z_stream stream;
  7120. int stream_valid;
  7121. };
  7122. static ssize_t
  7123. zisofs_extract_init(struct archive_write *a, struct zisofs_extract *zisofs,
  7124. const unsigned char *p, size_t bytes)
  7125. {
  7126. size_t avail = bytes;
  7127. size_t _ceil, xsize;
  7128. /* Allocate block pointers buffer. */
  7129. _ceil = (size_t)((zisofs->pz_uncompressed_size +
  7130. (((int64_t)1) << zisofs->pz_log2_bs) - 1)
  7131. >> zisofs->pz_log2_bs);
  7132. xsize = (_ceil + 1) * 4;
  7133. if (zisofs->block_pointers == NULL) {
  7134. size_t alloc = ((xsize >> 10) + 1) << 10;
  7135. zisofs->block_pointers = malloc(alloc);
  7136. if (zisofs->block_pointers == NULL) {
  7137. archive_set_error(&a->archive, ENOMEM,
  7138. "No memory for zisofs decompression");
  7139. return (ARCHIVE_FATAL);
  7140. }
  7141. }
  7142. zisofs->block_pointers_size = xsize;
  7143. /* Allocate uncompressed data buffer. */
  7144. zisofs->uncompressed_buffer_size = (size_t)1UL << zisofs->pz_log2_bs;
  7145. /*
  7146. * Read the file header, and check the magic code of zisofs.
  7147. */
  7148. if (!zisofs->header_passed) {
  7149. int err = 0;
  7150. if (avail < 16) {
  7151. archive_set_error(&a->archive,
  7152. ARCHIVE_ERRNO_FILE_FORMAT,
  7153. "Illegal zisofs file body");
  7154. return (ARCHIVE_FATAL);
  7155. }
  7156. if (memcmp(p, zisofs_magic, sizeof(zisofs_magic)) != 0)
  7157. err = 1;
  7158. else if (archive_le32dec(p + 8) != zisofs->pz_uncompressed_size)
  7159. err = 1;
  7160. else if (p[12] != 4 || p[13] != zisofs->pz_log2_bs)
  7161. err = 1;
  7162. if (err) {
  7163. archive_set_error(&a->archive,
  7164. ARCHIVE_ERRNO_FILE_FORMAT,
  7165. "Illegal zisofs file body");
  7166. return (ARCHIVE_FATAL);
  7167. }
  7168. avail -= 16;
  7169. p += 16;
  7170. zisofs->header_passed = 1;
  7171. }
  7172. /*
  7173. * Read block pointers.
  7174. */
  7175. if (zisofs->header_passed &&
  7176. zisofs->block_pointers_avail < zisofs->block_pointers_size) {
  7177. xsize = zisofs->block_pointers_size
  7178. - zisofs->block_pointers_avail;
  7179. if (avail < xsize)
  7180. xsize = avail;
  7181. memcpy(zisofs->block_pointers
  7182. + zisofs->block_pointers_avail, p, xsize);
  7183. zisofs->block_pointers_avail += xsize;
  7184. avail -= xsize;
  7185. if (zisofs->block_pointers_avail
  7186. == zisofs->block_pointers_size) {
  7187. /* We've got all block pointers and initialize
  7188. * related variables. */
  7189. zisofs->block_off = 0;
  7190. zisofs->block_avail = 0;
  7191. /* Complete a initialization */
  7192. zisofs->initialized = 1;
  7193. }
  7194. }
  7195. return ((ssize_t)avail);
  7196. }
  7197. static ssize_t
  7198. zisofs_extract(struct archive_write *a, struct zisofs_extract *zisofs,
  7199. const unsigned char *p, size_t bytes)
  7200. {
  7201. size_t avail;
  7202. int r;
  7203. if (!zisofs->initialized) {
  7204. ssize_t rs = zisofs_extract_init(a, zisofs, p, bytes);
  7205. if (rs < 0)
  7206. return (rs);
  7207. if (!zisofs->initialized) {
  7208. /* We need more data. */
  7209. zisofs->pz_offset += (uint32_t)bytes;
  7210. return (bytes);
  7211. }
  7212. avail = rs;
  7213. p += bytes - avail;
  7214. } else
  7215. avail = bytes;
  7216. /*
  7217. * Get block offsets from block pointers.
  7218. */
  7219. if (zisofs->block_avail == 0) {
  7220. uint32_t bst, bed;
  7221. if (zisofs->block_off + 4 >= zisofs->block_pointers_size) {
  7222. /* There isn't a pair of offsets. */
  7223. archive_set_error(&a->archive,
  7224. ARCHIVE_ERRNO_FILE_FORMAT,
  7225. "Illegal zisofs block pointers");
  7226. return (ARCHIVE_FATAL);
  7227. }
  7228. bst = archive_le32dec(
  7229. zisofs->block_pointers + zisofs->block_off);
  7230. if (bst != zisofs->pz_offset + (bytes - avail)) {
  7231. archive_set_error(&a->archive,
  7232. ARCHIVE_ERRNO_FILE_FORMAT,
  7233. "Illegal zisofs block pointers(cannot seek)");
  7234. return (ARCHIVE_FATAL);
  7235. }
  7236. bed = archive_le32dec(
  7237. zisofs->block_pointers + zisofs->block_off + 4);
  7238. if (bed < bst) {
  7239. archive_set_error(&a->archive,
  7240. ARCHIVE_ERRNO_FILE_FORMAT,
  7241. "Illegal zisofs block pointers");
  7242. return (ARCHIVE_FATAL);
  7243. }
  7244. zisofs->block_avail = bed - bst;
  7245. zisofs->block_off += 4;
  7246. /* Initialize compression library for new block. */
  7247. if (zisofs->stream_valid)
  7248. r = inflateReset(&zisofs->stream);
  7249. else
  7250. r = inflateInit(&zisofs->stream);
  7251. if (r != Z_OK) {
  7252. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  7253. "Can't initialize zisofs decompression.");
  7254. return (ARCHIVE_FATAL);
  7255. }
  7256. zisofs->stream_valid = 1;
  7257. zisofs->stream.total_in = 0;
  7258. zisofs->stream.total_out = 0;
  7259. }
  7260. /*
  7261. * Make uncompressed data.
  7262. */
  7263. if (zisofs->block_avail == 0) {
  7264. /*
  7265. * It's basically 32K bytes NUL data.
  7266. */
  7267. unsigned char *wb;
  7268. size_t size, wsize;
  7269. size = zisofs->uncompressed_buffer_size;
  7270. while (size) {
  7271. wb = wb_buffptr(a);
  7272. if (size > wb_remaining(a))
  7273. wsize = wb_remaining(a);
  7274. else
  7275. wsize = size;
  7276. memset(wb, 0, wsize);
  7277. r = wb_consume(a, wsize);
  7278. if (r < 0)
  7279. return (r);
  7280. size -= wsize;
  7281. }
  7282. } else {
  7283. zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p;
  7284. if (avail > zisofs->block_avail)
  7285. zisofs->stream.avail_in = zisofs->block_avail;
  7286. else
  7287. zisofs->stream.avail_in = (uInt)avail;
  7288. zisofs->stream.next_out = wb_buffptr(a);
  7289. zisofs->stream.avail_out = (uInt)wb_remaining(a);
  7290. r = inflate(&zisofs->stream, 0);
  7291. switch (r) {
  7292. case Z_OK: /* Decompressor made some progress.*/
  7293. case Z_STREAM_END: /* Found end of stream. */
  7294. break;
  7295. default:
  7296. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  7297. "zisofs decompression failed (%d)", r);
  7298. return (ARCHIVE_FATAL);
  7299. }
  7300. avail -= zisofs->stream.next_in - p;
  7301. zisofs->block_avail -= (uint32_t)(zisofs->stream.next_in - p);
  7302. r = wb_consume(a, wb_remaining(a) - zisofs->stream.avail_out);
  7303. if (r < 0)
  7304. return (r);
  7305. }
  7306. zisofs->pz_offset += (uint32_t)bytes;
  7307. return (bytes - avail);
  7308. }
  7309. static int
  7310. zisofs_rewind_boot_file(struct archive_write *a)
  7311. {
  7312. struct iso9660 *iso9660 = a->format_data;
  7313. struct isofile *file;
  7314. unsigned char *rbuff;
  7315. ssize_t r;
  7316. size_t remaining, rbuff_size;
  7317. struct zisofs_extract zext;
  7318. int64_t read_offset, write_offset, new_offset;
  7319. int fd, ret = ARCHIVE_OK;
  7320. file = iso9660->el_torito.boot->file;
  7321. /*
  7322. * There is nothing to do if this boot file does not have
  7323. * zisofs header.
  7324. */
  7325. if (file->zisofs.header_size == 0)
  7326. return (ARCHIVE_OK);
  7327. /*
  7328. * Uncompress the zisofs'ed file contents.
  7329. */
  7330. memset(&zext, 0, sizeof(zext));
  7331. zext.pz_uncompressed_size = file->zisofs.uncompressed_size;
  7332. zext.pz_log2_bs = file->zisofs.log2_bs;
  7333. fd = iso9660->temp_fd;
  7334. new_offset = wb_offset(a);
  7335. read_offset = file->content.offset_of_temp;
  7336. remaining = (size_t)file->content.size;
  7337. if (remaining > 1024 * 32)
  7338. rbuff_size = 1024 * 32;
  7339. else
  7340. rbuff_size = remaining;
  7341. rbuff = malloc(rbuff_size);
  7342. if (rbuff == NULL) {
  7343. archive_set_error(&a->archive, ENOMEM, "Can't allocate memory");
  7344. return (ARCHIVE_FATAL);
  7345. }
  7346. while (remaining) {
  7347. size_t rsize;
  7348. ssize_t rs;
  7349. /* Get the current file pointer. */
  7350. write_offset = lseek(fd, 0, SEEK_CUR);
  7351. /* Change the file pointer to read. */
  7352. lseek(fd, read_offset, SEEK_SET);
  7353. rsize = rbuff_size;
  7354. if (rsize > remaining)
  7355. rsize = remaining;
  7356. rs = read(iso9660->temp_fd, rbuff, rsize);
  7357. if (rs <= 0) {
  7358. archive_set_error(&a->archive, errno,
  7359. "Can't read temporary file(%jd)", (intmax_t)rs);
  7360. ret = ARCHIVE_FATAL;
  7361. break;
  7362. }
  7363. remaining -= rs;
  7364. read_offset += rs;
  7365. /* Put the file pointer back to write. */
  7366. lseek(fd, write_offset, SEEK_SET);
  7367. r = zisofs_extract(a, &zext, rbuff, rs);
  7368. if (r < 0) {
  7369. ret = (int)r;
  7370. break;
  7371. }
  7372. }
  7373. if (ret == ARCHIVE_OK) {
  7374. /*
  7375. * Change the boot file content from zisofs'ed data
  7376. * to plain data.
  7377. */
  7378. file->content.offset_of_temp = new_offset;
  7379. file->content.size = file->zisofs.uncompressed_size;
  7380. archive_entry_set_size(file->entry, file->content.size);
  7381. /* Set to be no zisofs. */
  7382. file->zisofs.header_size = 0;
  7383. file->zisofs.log2_bs = 0;
  7384. file->zisofs.uncompressed_size = 0;
  7385. r = wb_write_padding_to_temp(a, file->content.size);
  7386. if (r < 0)
  7387. ret = ARCHIVE_FATAL;
  7388. }
  7389. /*
  7390. * Free the resource we used in this function only.
  7391. */
  7392. free(rbuff);
  7393. free(zext.block_pointers);
  7394. if (zext.stream_valid && inflateEnd(&(zext.stream)) != Z_OK) {
  7395. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  7396. "Failed to clean up compressor");
  7397. ret = ARCHIVE_FATAL;
  7398. }
  7399. return (ret);
  7400. }
  7401. #else
  7402. static int
  7403. zisofs_write_to_temp(struct archive_write *a, const void *buff, size_t s)
  7404. {
  7405. (void)buff; /* UNUSED */
  7406. (void)s; /* UNUSED */
  7407. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Programming error");
  7408. return (ARCHIVE_FATAL);
  7409. }
  7410. static int
  7411. zisofs_rewind_boot_file(struct archive_write *a)
  7412. {
  7413. struct iso9660 *iso9660 = a->format_data;
  7414. if (iso9660->el_torito.boot->file->zisofs.header_size != 0) {
  7415. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  7416. "We cannot extract the zisofs imaged boot file;"
  7417. " this may not boot in being zisofs imaged");
  7418. return (ARCHIVE_FAILED);
  7419. }
  7420. return (ARCHIVE_OK);
  7421. }
  7422. static int
  7423. zisofs_finish_entry(struct archive_write *a)
  7424. {
  7425. (void)a; /* UNUSED */
  7426. return (ARCHIVE_OK);
  7427. }
  7428. static int
  7429. zisofs_free(struct archive_write *a)
  7430. {
  7431. (void)a; /* UNUSED */
  7432. return (ARCHIVE_OK);
  7433. }
  7434. #endif /* HAVE_ZLIB_H */