zipfile.c 217 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820
  1. /*
  2. zipfile.c - Zip 3
  3. Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
  4. See the accompanying file LICENSE, version 2007-Mar-4 or later
  5. (the contents of which are also included in zip.h) for terms of use.
  6. If, for some reason, all these files are missing, the Info-ZIP license
  7. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  8. */
  9. /*
  10. * zipfile.c by Mark Adler.
  11. */
  12. #define __ZIPFILE_C
  13. #include "zip.h"
  14. #include "revision.h"
  15. #ifdef UNICODE_SUPPORT
  16. # include "crc32.h"
  17. #endif
  18. /* for realloc 2/6/2005 EG */
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. /* for toupper() */
  22. #include <ctype.h>
  23. #ifdef VMS
  24. # include "vms/vms.h"
  25. # include "vms/vmsmunch.h"
  26. # include "vms/vmsdefs.h"
  27. #endif
  28. #ifdef WIN32
  29. # define WIN32_LEAN_AND_MEAN
  30. # include <windows.h>
  31. #endif
  32. /*
  33. * XXX start of zipfile.h
  34. */
  35. #ifdef THEOS
  36. /* Macros cause stack overflow in compiler */
  37. ush SH(uch* p) { return ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8)); }
  38. ulg LG(uch* p) { return ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16)); }
  39. #else /* !THEOS */
  40. /* Macros for converting integers in little-endian to machine format */
  41. # define SH(a) ((ush)(((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8)))
  42. # define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))
  43. # ifdef ZIP64_SUPPORT /* zip64 support 08/31/2003 R.Nausedat */
  44. # define LLG(a) ((zoff_t)LG(a) | ((zoff_t)LG((a)+4) << 32))
  45. # endif
  46. #endif /* ?THEOS */
  47. /* Macros for writing machine integers to little-endian format */
  48. #define PUTSH(a,f) {putc((char)((a) & 0xff),(f)); putc((char)((a) >> 8),(f));}
  49. #define PUTLG(a,f) {PUTSH((a) & 0xffff,(f)) PUTSH((a) >> 16,(f))}
  50. #ifdef ZIP64_SUPPORT /* zip64 support 08/31/2003 R.Nausedat */
  51. # define PUTLLG(a,f) {PUTLG((a) & 0xffffffff,(f)) PUTLG((a) >> 32,(f))}
  52. #endif
  53. /* -- Structure of a ZIP file -- */
  54. /* Signatures for zip file information headers */
  55. #define LOCSIG 0x04034b50L
  56. #define CENSIG 0x02014b50L
  57. #define ENDSIG 0x06054b50L
  58. #define EXTLOCSIG 0x08074b50L
  59. /* Offsets of values in headers */
  60. /* local header */
  61. #define LOCVER 0 /* version needed to extract */
  62. #define LOCFLG 2 /* encrypt, deflate flags */
  63. #define LOCHOW 4 /* compression method */
  64. #define LOCTIM 6 /* last modified file time, DOS format */
  65. #define LOCDAT 8 /* last modified file date, DOS format */
  66. #define LOCCRC 10 /* uncompressed crc-32 for file */
  67. #define LOCSIZ 14 /* compressed size in zip file */
  68. #define LOCLEN 18 /* uncompressed size */
  69. #define LOCNAM 22 /* length of filename */
  70. #define LOCEXT 24 /* length of extra field */
  71. /* extended local header (data descriptor) following file data (if bit 3 set) */
  72. /* if Zip64 then all are 8 byte and not below - 11/1/03 EG */
  73. #define EXTCRC 0 /* uncompressed crc-32 for file */
  74. #define EXTSIZ 4 /* compressed size in zip file */
  75. #define EXTLEN 8 /* uncompressed size */
  76. /* central directory header */
  77. #define CENVEM 0 /* version made by */
  78. #define CENVER 2 /* version needed to extract */
  79. #define CENFLG 4 /* encrypt, deflate flags */
  80. #define CENHOW 6 /* compression method */
  81. #define CENTIM 8 /* last modified file time, DOS format */
  82. #define CENDAT 10 /* last modified file date, DOS format */
  83. #define CENCRC 12 /* uncompressed crc-32 for file */
  84. #define CENSIZ 16 /* compressed size in zip file */
  85. #define CENLEN 20 /* uncompressed size */
  86. #define CENNAM 24 /* length of filename */
  87. #define CENEXT 26 /* length of extra field */
  88. #define CENCOM 28 /* file comment length */
  89. #define CENDSK 30 /* disk number start */
  90. #define CENATT 32 /* internal file attributes */
  91. #define CENATX 34 /* external file attributes */
  92. #define CENOFF 38 /* relative offset of local header */
  93. /* end of central directory record */
  94. #define ENDDSK 0 /* number of this disk */
  95. #define ENDBEG 2 /* number of the starting disk */
  96. #define ENDSUB 4 /* entries on this disk */
  97. #define ENDTOT 6 /* total number of entries */
  98. #define ENDSIZ 8 /* size of entire central directory */
  99. #define ENDOFF 12 /* offset of central on starting disk */
  100. #define ENDCOM 16 /* length of zip file comment */
  101. /* zip64 support 08/31/2003 R.Nausedat */
  102. /* EOCDL_SIG used to detect Zip64 archive */
  103. #define ZIP64_EOCDL_SIG 0x07064b50
  104. /* EOCDL size is used in the empty archive check */
  105. #define ZIP64_EOCDL_OFS_SIZE 20
  106. #define ZIP_UWORD16_MAX 0xFFFF /* border value */
  107. #define ZIP_UWORD32_MAX 0xFFFFFFFF /* border value */
  108. #define ZIP_EF_HEADER_SIZE 4 /* size of pre-header of extra fields */
  109. #ifdef ZIP64_SUPPORT
  110. # define ZIP64_EXTCRC 0 /* uncompressed crc-32 for file */
  111. # define ZIP64_EXTSIZ 4 /* compressed size in zip file */
  112. # define ZIP64_EXTLEN 12 /* uncompressed size */
  113. # define ZIP64_EOCD_SIG 0x06064b50
  114. # define ZIP64_EOCD_OFS_SIZE 40
  115. # define ZIP64_EOCD_OFS_CD_START 48
  116. # define ZIP64_EOCDL_OFS_SIZE 20
  117. # define ZIP64_EOCDL_OFS_EOCD_START 8
  118. # define ZIP64_EOCDL_OFS_TOTALDISKS 16
  119. # define ZIP64_MIN_VER 45 /* min version to set in the CD extra records */
  120. # define ZIP64_CENTRAL_DIR_TAIL_SIZE (56 - 8 - 4) /* size of zip64 central dir tail, minus sig and size field bytes */
  121. # define ZIP64_CENTRAL_DIR_TAIL_SIG 0x06064B50L /* zip64 central dir tail signature */
  122. # define ZIP64_CENTRAL_DIR_TAIL_END_SIG 0x07064B50L /* zip64 end of cen dir locator signature */
  123. # define ZIP64_LARGE_FILE_HEAD_SIZE 32 /* total size of zip64 extra field */
  124. # define ZIP64_EF_TAG 0x0001 /* ID for zip64 extra field */
  125. # define ZIP64_EFIELD_OFS_OSIZE ZIP_EF_HEADER_SIZE /* zip64 extra field: offset to original file size */
  126. # define ZIP64_EFIELD_OFS_CSIZE (ZIP64_EFIELD_OFS_OSIZE + 8) /* zip64 extra field: offset to compressed file size */
  127. # define ZIP64_EFIELD_OFS_OFS (ZIP64_EFIELD_OFS_CSIZE + 8) /* zip64 extra field: offset to offset in archive */
  128. # define ZIP64_EFIELD_OFS_DISK (ZIP64_EFIELD_OFS_OFS + 8) /* zip64 extra field: offset to start disk # */
  129. /* -------------------------------------------------------------------------------------------------------------------------- */
  130. local int adjust_zip_local_entry OF((struct zlist far *));
  131. local void adjust_zip_central_entry OF((struct zlist far *));
  132. #if 0
  133. local int remove_local_extra_field OF((struct zlist far *, ulg));
  134. local int remove_central_extra_field OF((struct zlist far *, ulg));
  135. #endif
  136. local int add_central_zip64_extra_field OF((struct zlist far *));
  137. local int add_local_zip64_extra_field OF((struct zlist far *));
  138. #endif /* ZIP64_SUPPORT */
  139. #ifdef UNICODE_SUPPORT
  140. # define UTF8_PATH_EF_TAG 0x7075 /* ID for Unicode path (up) extra field */
  141. local int add_Unicode_Path_local_extra_field OF((struct zlist far *));
  142. local int add_Unicode_Path_cen_extra_field OF((struct zlist far *));
  143. #endif
  144. /* New General Purpose Bit Flag bit 11 flags when entry path and
  145. comment are in UTF-8 */
  146. #define UTF8_BIT (1 << 11)
  147. /* moved out of ZIP64_SUPPORT - 2/6/2005 EG */
  148. local void write_ushort_to_mem OF((ush, char *)); /* little endian conversions */
  149. local void write_ulong_to_mem OF((ulg, char *));
  150. #ifdef ZIP64_SUPPORT
  151. local void write_int64_to_mem OF((uzoff_t, char *));
  152. #endif /* def ZIP64_SUPPORT */
  153. #ifdef UNICODE_SUPPORT
  154. local void write_string_to_mem OF((char *, char *));
  155. #endif
  156. #if 0
  157. local char *get_extra_field OF((ush, char *, unsigned)); /* zip64 */
  158. #endif
  159. #ifdef UNICODE_SUPPORT
  160. local void read_Unicode_Path_entry OF((struct zlist far *));
  161. local void read_Unicode_Path_local_entry OF((struct zlist far *));
  162. #endif
  163. /* added these self allocators - 2/6/2005 EG */
  164. local void append_ushort_to_mem OF((ush, char **, extent *, extent *));
  165. local void append_ulong_to_mem OF((ulg, char **, extent *, extent *));
  166. #ifdef ZIP64_SUPPORT
  167. local void append_int64_to_mem OF((uzoff_t, char **, extent *, extent *));
  168. #endif /* def ZIP64_SUPPORT */
  169. local void append_string_to_mem OF((char *, int, char**, extent *, extent *));
  170. /* Local functions */
  171. local int find_next_signature OF((FILE *f));
  172. local int find_signature OF((FILE *, ZCONST char *));
  173. local int is_signature OF((ZCONST char *, ZCONST char *));
  174. local int at_signature OF((FILE *, ZCONST char *));
  175. local int zqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
  176. #ifdef UNICODE_SUPPORT
  177. local int zuqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
  178. #endif
  179. #if 0
  180. local int scanzipf_reg OF((FILE *f));
  181. #endif
  182. local int scanzipf_regnew OF((void));
  183. #ifndef UTIL
  184. local int rqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
  185. local int zbcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
  186. # ifdef UNICODE_SUPPORT
  187. local int zubcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
  188. # if 0
  189. local int zuebcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
  190. # endif
  191. # endif /* UNICODE_SUPPORT */
  192. local void zipoddities OF((struct zlist far *));
  193. # if 0
  194. local int scanzipf_fix OF((FILE *f));
  195. # endif
  196. local int scanzipf_fixnew OF((void));
  197. # ifdef USE_EF_UT_TIME
  198. local int ef_scan_ut_time OF((char *ef_buf, extent ef_len, int ef_is_cent,
  199. iztimes *z_utim));
  200. # endif /* USE_EF_UT_TIME */
  201. local void cutpath OF((char *p, int delim));
  202. #endif /* !UTIL */
  203. /*
  204. * XXX end of zipfile.h
  205. */
  206. /* Local data */
  207. #ifdef HANDLE_AMIGA_SFX
  208. ulg amiga_sfx_offset; /* place where size field needs updating */
  209. #endif
  210. local int zqcmp(a, b)
  211. ZCONST zvoid *a, *b; /* pointers to pointers to zip entries */
  212. /* Used by qsort() to compare entries in the zfile list.
  213. * Compares the internal names z->iname */
  214. {
  215. char *aname = (*(struct zlist far **)a)->iname;
  216. char *bname = (*(struct zlist far **)b)->iname;
  217. return namecmp(aname, bname);
  218. }
  219. #ifdef UNICODE_SUPPORT
  220. local int zuqcmp(a, b)
  221. ZCONST zvoid *a, *b; /* pointers to pointers to zip entries */
  222. /* Used by qsort() to compare entries in the zfile list.
  223. * Compares the internal names z->zuname */
  224. {
  225. char *aname = (*(struct zlist far **)a)->iname;
  226. char *bname = (*(struct zlist far **)b)->iname;
  227. /* zuname could be NULL */
  228. if ((*(struct zlist far **)a)->zuname)
  229. aname = (*(struct zlist far **)a)->zuname;
  230. if ((*(struct zlist far **)b)->zuname)
  231. bname = (*(struct zlist far **)b)->zuname;
  232. return namecmp(aname, bname);
  233. }
  234. #endif
  235. #ifndef UTIL
  236. local int rqcmp(a, b)
  237. ZCONST zvoid *a, *b; /* pointers to pointers to zip entries */
  238. /* Used by qsort() to compare entries in the zfile list.
  239. * Compare the internal names z->iname, but in reverse order. */
  240. {
  241. return namecmp((*(struct zlist far **)b)->iname,
  242. (*(struct zlist far **)a)->iname);
  243. }
  244. local int zbcmp(n, z)
  245. ZCONST zvoid *n; /* string to search for */
  246. ZCONST zvoid far *z; /* pointer to a pointer to a zip entry */
  247. /* Used by search() to compare a target to an entry in the zfile list. */
  248. {
  249. return namecmp((char *)n, ((struct zlist far *)z)->zname);
  250. }
  251. #ifdef UNICODE_SUPPORT
  252. /* search unicode paths */
  253. local int zubcmp(n, z)
  254. ZCONST zvoid *n; /* string to search for */
  255. ZCONST zvoid far *z; /* pointer to a pointer to a zip entry */
  256. /* Used by search() to compare a target to an entry in the zfile list. */
  257. {
  258. char *zuname = ((struct zlist far *)z)->zuname;
  259. /* zuname is NULL if no UTF-8 name */
  260. if (zuname == NULL)
  261. zuname = ((struct zlist far *)z)->zname;
  262. return namecmp((char *)n, zuname);
  263. }
  264. #if 0
  265. /* search escaped unicode paths */
  266. local int zuebcmp(n, z)
  267. ZCONST zvoid *n; /* string to search for */
  268. ZCONST zvoid far *z; /* pointer to a pointer to a zip entry */
  269. /* Used by search() to compare a target to an entry in the zfile list. */
  270. {
  271. char *zuname = ((struct zlist far *)z)->zuname;
  272. char *zuename;
  273. int k;
  274. /* zuname is NULL if no UTF-8 name */
  275. if (zuname == NULL)
  276. zuname = ((struct zlist far *)z)->zname;
  277. zuename = local_to_escape_string(zuname);
  278. k = namecmp((char *)n, zuename);
  279. free(zuename);
  280. return k;
  281. }
  282. #endif
  283. #endif
  284. struct zlist far *zsearch(n)
  285. ZCONST char *n; /* name to find */
  286. /* Return a pointer to the entry in zfile with the name n, or NULL if
  287. not found. */
  288. {
  289. zvoid far **p; /* result of search() */
  290. if (zcount) {
  291. if ((p = search(n, (ZCONST zvoid far **)zsort, zcount, zbcmp)) != NULL)
  292. return *(struct zlist far **)p;
  293. #ifdef UNICODE_SUPPORT
  294. else if (unicode_mismatch != 3 && fix != 2 &&
  295. (p = search(n, (ZCONST zvoid far **)zusort, zcount, zubcmp)) != NULL)
  296. return *(struct zlist far **)p;
  297. #endif
  298. else
  299. return NULL;
  300. }
  301. return NULL;
  302. }
  303. #endif /* !UTIL */
  304. #ifndef VMS /* See [.VMS]VMS.C for VMS-specific ziptyp(). */
  305. # ifndef PATHCUT
  306. # define PATHCUT '/'
  307. # endif
  308. char *ziptyp(s)
  309. char *s; /* file name to force to zip */
  310. /* If the file name *s has a dot (other than the first char), or if
  311. the -A option is used (adjust self-extracting file) then return
  312. the name, otherwise append .zip to the name. Allocate the space for
  313. the name in either case. Return a pointer to the new name, or NULL
  314. if malloc() fails. */
  315. {
  316. char *q; /* temporary pointer */
  317. char *t; /* pointer to malloc'ed string */
  318. # ifdef THEOS
  319. char *r; /* temporary pointer */
  320. char *disk;
  321. # endif
  322. if ((t = malloc(strlen(s) + 5)) == NULL)
  323. return NULL;
  324. strcpy(t, s);
  325. # ifdef __human68k__
  326. _toslash(t);
  327. # endif
  328. # ifdef MSDOS
  329. for (q = t; *q; INCSTR(q))
  330. if (*q == '\\')
  331. *q = '/';
  332. # endif /* MSDOS */
  333. # if defined(__RSXNT__) || defined(WIN32_CRT_OEM)
  334. /* RSXNT/EMX C rtl uses OEM charset */
  335. AnsiToOem(t, t);
  336. # endif
  337. if (adjust) return t;
  338. # ifndef RISCOS
  339. # ifndef QDOS
  340. # ifdef AMIGA
  341. if ((q = MBSRCHR(t, '/')) == NULL)
  342. q = MBSRCHR(t, ':');
  343. if (MBSRCHR((q ? q + 1 : t), '.') == NULL)
  344. # else /* !AMIGA */
  345. # ifdef THEOS
  346. /* the argument expansion add a dot to the end of file names when
  347. * there is no extension and at least one of a argument has wild cards.
  348. * So check for at least one character in the extension if there is a dot
  349. * in file name */
  350. if ((q = MBSRCHR((q = MBSRCHR(t, PATHCUT)) == NULL ? t : q + 1, '.')) == NULL
  351. || q[1] == '\0') {
  352. # else /* !THEOS */
  353. # ifdef TANDEM
  354. if (MBSRCHR((q = MBSRCHR(t, '.')) == NULL ? t : q + 1, ' ') == NULL)
  355. # else /* !TANDEM */
  356. if (MBSRCHR((q = MBSRCHR(t, PATHCUT)) == NULL ? t : q + 1, '.') == NULL)
  357. # endif /* ?TANDEM */
  358. # endif /* ?THEOS */
  359. # endif /* ?AMIGA */
  360. # ifdef CMS_MVS
  361. if (strncmp(t,"dd:",3) != 0 && strncmp(t,"DD:",3) != 0)
  362. # endif /* CMS_MVS */
  363. # ifdef THEOS
  364. /* insert .zip extension before disk name */
  365. if ((r = MBSRCHR(t, ':')) != NULL) {
  366. /* save disk name */
  367. if ((disk = strdup(r)) == NULL)
  368. return NULL;
  369. strcpy(r[-1] == '.' ? r - 1 : r, ".zip");
  370. strcat(t, disk);
  371. free(disk);
  372. } else {
  373. if (q != NULL && *q == '.')
  374. strcpy(q, ".zip");
  375. else
  376. strcat(t, ".zip");
  377. }
  378. }
  379. # else /* !THEOS */
  380. # ifdef TANDEM /* Tandem can't cope with extensions */
  381. strcat(t, " ZIP");
  382. # else /* !TANDEM */
  383. strcat(t, ".zip");
  384. # endif /* ?TANDEM */
  385. # endif /* ?THEOS */
  386. # else /* QDOS */
  387. q = LastDir(t);
  388. if(MBSRCHR(q, '_') == NULL && MBSRCHR(q, '.') == NULL)
  389. {
  390. strcat(t, "_zip");
  391. }
  392. # endif /* QDOS */
  393. # endif /* !RISCOS */
  394. return t;
  395. }
  396. #endif /* ndef VMS */
  397. /* ---------------------------------------------------- */
  398. /* moved out of ZIP64_SUPPORT - 2/6/2005 EG */
  399. /* 08/31/2003 R.Nausedat */
  400. local void write_ushort_to_mem( OFT( ush) usValue,
  401. OFT( char *)pPtr)
  402. #ifdef NO_PROTO
  403. ush usValue;
  404. char *pPtr;
  405. #endif /* def NO_PROTO */
  406. {
  407. *pPtr++ = ((char)(usValue) & 0xff);
  408. *pPtr = ((char)(usValue >> 8) & 0xff);
  409. }
  410. local void write_ulong_to_mem(uValue, pPtr)
  411. ulg uValue;
  412. char *pPtr;
  413. {
  414. write_ushort_to_mem((ush)(uValue & 0xffff), pPtr);
  415. write_ushort_to_mem((ush)((uValue >> 16) & 0xffff), pPtr + 2);
  416. }
  417. #ifdef ZIP64_SUPPORT
  418. local void write_int64_to_mem(l64Value,pPtr)
  419. uzoff_t l64Value;
  420. char *pPtr;
  421. {
  422. write_ulong_to_mem((ulg)(l64Value & 0xffffffff),pPtr);
  423. write_ulong_to_mem((ulg)((l64Value >> 32) & 0xffffffff),pPtr + 4);
  424. }
  425. #endif /* def ZIP64_SUPPORT */
  426. #ifdef UNICODE_SUPPORT
  427. /* Write a string to memory */
  428. local void write_string_to_mem(strValue, pPtr)
  429. char *strValue;
  430. char *pPtr;
  431. {
  432. if (strValue != NULL) {
  433. int ssize = strlen(strValue);
  434. int i;
  435. for (i = 0; i < ssize; i++) {
  436. *(pPtr + i) = *(strValue + i);
  437. }
  438. }
  439. }
  440. #endif /* def UNICODE_SUPPORT */
  441. /* same as above but allocate memory as needed and keep track of current end
  442. using offset - 2/6/05 EG */
  443. #if 0 /* ubyte version not used */
  444. local void append_ubyte_to_mem( OFT( unsigned char) ubValue,
  445. OFT( char **) pPtr,
  446. OFT( extent *) offset,
  447. OFT( extent *) blocksize)
  448. #ifdef NO_PROTO
  449. unsigned char ubValue; /* byte to append */
  450. char **pPtr; /* start of block */
  451. extent *offset; /* next byte to write */
  452. extent *blocksize; /* current size of block */
  453. #endif /* def NO_PROTO */
  454. {
  455. if (*pPtr == NULL) {
  456. /* malloc a 1K block */
  457. (*blocksize) = 1024;
  458. *pPtr = (char *) malloc(*blocksize);
  459. if (*pPtr == NULL) {
  460. ziperr(ZE_MEM, "append_ubyte_to_mem");
  461. }
  462. }
  463. /* if (*offset) + 1 > (*blocksize) - 1 */
  464. else if ((*offset) > (*blocksize) - (1 + 1)) {
  465. /* realloc a bigger block in 1 K increments */
  466. (*blocksize) += 1024;
  467. *pPtr = realloc(*pPtr, *blocksize);
  468. if (*pPtr == NULL) {
  469. ziperr(ZE_MEM, "append_ubyte_to_mem");
  470. }
  471. }
  472. *(*pPtr + *offset) = ubValue;
  473. (*offset)++;
  474. }
  475. #endif
  476. local void append_ushort_to_mem( OFT( ush) usValue,
  477. OFT( char **) pPtr,
  478. OFT( extent *) offset,
  479. OFT( extent *) blocksize)
  480. #ifdef NO_PROTO
  481. ush usValue;
  482. char **pPtr;
  483. extent *offset;
  484. extent *blocksize;
  485. #endif /* def NO_PROTO */
  486. {
  487. if (*pPtr == NULL) {
  488. /* malloc a 1K block */
  489. (*blocksize) = 1024;
  490. *pPtr = (char *) malloc(*blocksize);
  491. if (*pPtr == NULL) {
  492. ziperr(ZE_MEM, "append_ushort_to_mem");
  493. }
  494. }
  495. /* if (*offset) + 2 > (*blocksize) - 1 */
  496. else if ((*offset) > (*blocksize) - (1 + 2)) {
  497. /* realloc a bigger block in 1 K increments */
  498. (*blocksize) += 1024;
  499. *pPtr = realloc(*pPtr, (extent)*blocksize);
  500. if (*pPtr == NULL) {
  501. ziperr(ZE_MEM, "append_ushort_to_mem");
  502. }
  503. }
  504. write_ushort_to_mem(usValue, (*pPtr) + (*offset));
  505. (*offset) += 2;
  506. }
  507. local void append_ulong_to_mem(uValue, pPtr, offset, blocksize)
  508. ulg uValue;
  509. char **pPtr;
  510. extent *offset;
  511. extent *blocksize;
  512. {
  513. if (*pPtr == NULL) {
  514. /* malloc a 1K block */
  515. (*blocksize) = 1024;
  516. *pPtr = (char *) malloc(*blocksize);
  517. if (*pPtr == NULL) {
  518. ziperr(ZE_MEM, "append_ulong_to_mem");
  519. }
  520. }
  521. else if ((*offset) > (*blocksize) - (1 + 4)) {
  522. /* realloc a bigger block in 1 K increments */
  523. (*blocksize) += 1024;
  524. *pPtr = realloc(*pPtr, *blocksize);
  525. if (*pPtr == NULL) {
  526. ziperr(ZE_MEM, "append_ulong_to_mem");
  527. }
  528. }
  529. write_ulong_to_mem(uValue, (*pPtr) + (*offset));
  530. (*offset) += 4;
  531. }
  532. #ifdef ZIP64_SUPPORT
  533. local void append_int64_to_mem(l64Value, pPtr, offset, blocksize)
  534. uzoff_t l64Value;
  535. char **pPtr;
  536. extent *offset;
  537. extent *blocksize;
  538. {
  539. if (*pPtr == NULL) {
  540. /* malloc a 1K block */
  541. (*blocksize) = 1024;
  542. *pPtr = (char *) malloc(*blocksize);
  543. if (*pPtr == NULL) {
  544. ziperr(ZE_MEM, "append_int64_to_mem");
  545. }
  546. }
  547. else if ((*offset) > (*blocksize) - (1 + 8)) {
  548. /* realloc a bigger block in 1 K increments */
  549. (*blocksize) += 1024;
  550. *pPtr = realloc(*pPtr, *blocksize);
  551. if (*pPtr == NULL) {
  552. ziperr(ZE_MEM, "append_int64_to_mem");
  553. }
  554. }
  555. write_int64_to_mem(l64Value, (*pPtr) + (*offset));
  556. (*offset) += 8;
  557. }
  558. #endif /* def ZIP64_SUPPORT */
  559. /* Append a string to the memory block. */
  560. local void append_string_to_mem(strValue, strLength, pPtr, offset, blocksize)
  561. char *strValue;
  562. int strLength;
  563. char **pPtr;
  564. extent *offset;
  565. extent *blocksize;
  566. {
  567. if (strValue != NULL) {
  568. unsigned bsize = 1024;
  569. unsigned ssize = strLength;
  570. unsigned i;
  571. if (ssize > bsize) {
  572. bsize = ssize;
  573. }
  574. if (*pPtr == NULL) {
  575. /* malloc a 1K block */
  576. (*blocksize) = bsize;
  577. *pPtr = (char *) malloc(*blocksize);
  578. if (*pPtr == NULL) {
  579. ziperr(ZE_MEM, "append_string_to_mem");
  580. }
  581. }
  582. else if ((*offset) + ssize > (*blocksize) - 1) {
  583. /* realloc a bigger block in 1 K increments */
  584. (*blocksize) += bsize;
  585. *pPtr = realloc(*pPtr, *blocksize);
  586. if (*pPtr == NULL) {
  587. ziperr(ZE_MEM, "append_string_to_mem");
  588. }
  589. }
  590. for (i = 0; i < ssize; i++) {
  591. *(*pPtr + *offset + i) = *(strValue + i);
  592. }
  593. (*offset) += ssize;
  594. }
  595. }
  596. /* ---------------------------------------------------- */
  597. /* zip64 support 08/31/2003 R.Nausedat */
  598. /* moved out of zip64 support 10/22/05 */
  599. /* Searches pExtra for extra field with specified tag.
  600. * If it finds one it returns a pointer to it, else NULL.
  601. * Renamed and made generic. 10/3/03
  602. */
  603. char *get_extra_field( OFT( ush) tag,
  604. OFT( char *) pExtra,
  605. OFT( unsigned) iExtraLen)
  606. #ifdef NO_PROTO
  607. ush tag; /* tag to look for */
  608. char *pExtra; /* pointer to extra field in memory */
  609. unsigned iExtraLen; /* length of extra field */
  610. #endif /* def NO_PROTO */
  611. {
  612. char *pTemp;
  613. ush usBlockTag;
  614. ush usBlockSize;
  615. if( pExtra == NULL )
  616. return NULL;
  617. for (pTemp = pExtra; pTemp < pExtra + iExtraLen - ZIP_EF_HEADER_SIZE;)
  618. {
  619. usBlockTag = SH(pTemp); /* get tag */
  620. usBlockSize = SH(pTemp + 2); /* get field data size */
  621. if (usBlockTag == tag)
  622. return pTemp;
  623. pTemp += (usBlockSize + ZIP_EF_HEADER_SIZE);
  624. }
  625. return NULL;
  626. }
  627. /* copy_nondup_extra_fields
  628. *
  629. * Copy any extra fields in old that are not in new to new.
  630. * Returns the new extra fields block and newLen is new length.
  631. */
  632. char *copy_nondup_extra_fields(oldExtra, oldExtraLen, newExtra, newExtraLen, newLen)
  633. char *oldExtra; /* pointer to old extra fields */
  634. unsigned oldExtraLen; /* length of old extra fields */
  635. char *newExtra; /* pointer to new extra fields */
  636. unsigned newExtraLen; /* length of new extra fields */
  637. unsigned *newLen; /* length of new extra fields after copy */
  638. {
  639. char *returnExtra = NULL;
  640. ush returnExtraLen = 0;
  641. char *tempExtra;
  642. char *pTemp;
  643. ush tag;
  644. ush blocksize;
  645. if( oldExtra == NULL ) {
  646. /* no old extra fields so return copy of newExtra */
  647. if (newExtra == NULL || newExtraLen == 0) {
  648. *newLen = 0;
  649. return NULL;
  650. } else {
  651. if ((returnExtra = malloc(newExtraLen)) == NULL)
  652. ZIPERR(ZE_MEM, "extra field copy");
  653. memcpy(returnExtra, newExtra, newExtraLen);
  654. returnExtraLen = newExtraLen;
  655. *newLen = returnExtraLen;
  656. return returnExtra;
  657. }
  658. }
  659. /* allocate block large enough for all extra fields */
  660. if ((tempExtra = malloc(0xFFFF)) == NULL)
  661. ZIPERR(ZE_MEM, "extra field copy");
  662. /* look for each old extra field in new block */
  663. for (pTemp = oldExtra; pTemp < oldExtra + oldExtraLen;)
  664. {
  665. tag = SH(pTemp); /* get tag */
  666. blocksize = SH(pTemp + 2); /* get field data size */
  667. if (get_extra_field(tag, newExtra, newExtraLen) == NULL) {
  668. /* tag not in new block so add it */
  669. memcpy(tempExtra + returnExtraLen, pTemp, blocksize + 4);
  670. returnExtraLen += blocksize + 4;
  671. }
  672. pTemp += blocksize + 4;
  673. }
  674. /* copy all extra fields from new block */
  675. memcpy(tempExtra + returnExtraLen, newExtra, newExtraLen);
  676. returnExtraLen += newExtraLen;
  677. /* copy tempExtra to returnExtra */
  678. if ((returnExtra = malloc(returnExtraLen)) == NULL)
  679. ZIPERR(ZE_MEM, "extra field copy");
  680. memcpy(returnExtra, tempExtra, returnExtraLen);
  681. free(tempExtra);
  682. *newLen = returnExtraLen;
  683. return returnExtra;
  684. }
  685. #ifdef UNICODE_SUPPORT
  686. /* The latest format is
  687. 1 byte Version of Unicode Path Extra Field
  688. 4 bytes Name Field CRC32 Checksum
  689. variable UTF-8 Version Of Name
  690. */
  691. local void read_Unicode_Path_entry(pZipListEntry)
  692. struct zlist far *pZipListEntry;
  693. {
  694. char *pTemp;
  695. char *UPath;
  696. char *iname;
  697. ush ELen;
  698. uch Version;
  699. ush ULen;
  700. ulg chksum = CRCVAL_INITIAL;
  701. ulg iname_chksum;
  702. /* check if we have a Unicode Path extra field ... */
  703. pTemp = get_extra_field( UTF8_PATH_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext );
  704. pZipListEntry->uname = NULL;
  705. if( pTemp == NULL ) {
  706. return;
  707. }
  708. /* ... if so, update corresponding entries in struct zlist */
  709. pTemp += 2;
  710. /* length of this extra field */
  711. ELen = SH(pTemp);
  712. pTemp += 2;
  713. /* version */
  714. Version = (uch) *pTemp;
  715. pTemp += 1;
  716. if (Version > 1) {
  717. zipwarn("Unicode Path Extra Field version > 1 - skipping", pZipListEntry->oname);
  718. return;
  719. }
  720. /* iname CRC */
  721. iname_chksum = LG(pTemp);
  722. pTemp += 4;
  723. /*
  724. * Compute the CRC-32 checksum of iname
  725. */
  726. /*
  727. crc_16 = crc16f((uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
  728. */
  729. if ((iname = malloc(strlen(pZipListEntry->iname) + 1)) == NULL) {
  730. ZIPERR(ZE_MEM, "write Unicode");
  731. }
  732. strcpy(iname, pZipListEntry->iname);
  733. chksum = crc32(chksum, (uch *)(iname), strlen(iname));
  734. free(iname);
  735. /* chksum = adler16(ADLERVAL_INITIAL,
  736. (uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
  737. */
  738. /* If the checksums's don't match then likely iname has been modified and
  739. * the Unicode Path is no longer valid
  740. */
  741. if (chksum != iname_chksum) {
  742. printf("unicode_mismatch = %d\n", unicode_mismatch);
  743. if (unicode_mismatch == 1) {
  744. /* warn and continue */
  745. zipwarn("Unicode does not match path - ignoring Unicode: ", pZipListEntry->oname);
  746. } else if (unicode_mismatch == 2) {
  747. /* ignore and continue */
  748. } else if (unicode_mismatch == 0) {
  749. /* error */
  750. sprintf(errbuf, "Unicode does not match path: %s\n", pZipListEntry->oname);
  751. strcat(errbuf,
  752. " Likely entry name changed but Unicode not updated\n");
  753. strcat(errbuf,
  754. " Use -UN=i to ignore errors or n for no Unicode paths");
  755. zipwarn(errbuf, "");
  756. ZIPERR(ZE_FORM, "Unicode path error");
  757. }
  758. return;
  759. }
  760. ULen = ELen - 5;
  761. /* UTF-8 Path */
  762. if (ULen == 0) {
  763. /* standard path is UTF-8 so use that */
  764. ULen = pZipListEntry->nam;
  765. if ((UPath = malloc(ULen + 1)) == NULL) {
  766. return;
  767. }
  768. strcpy(UPath, pZipListEntry->name);
  769. } else {
  770. /* use Unicode path */
  771. if ((UPath = malloc(ULen + 1)) == NULL) {
  772. return;
  773. }
  774. strncpy(UPath, pTemp, ULen);
  775. UPath[ULen] = '\0';
  776. }
  777. pZipListEntry->uname = UPath;
  778. return;
  779. }
  780. local void read_Unicode_Path_local_entry(pZipListEntry)
  781. struct zlist far *pZipListEntry;
  782. {
  783. char *pTemp;
  784. char *UPath;
  785. char *iname;
  786. ush ELen;
  787. uch Version;
  788. ush ULen;
  789. ulg chksum = CRCVAL_INITIAL;
  790. ulg iname_chksum;
  791. /* check if we have a Unicode Path extra field ... */
  792. pTemp = get_extra_field( UTF8_PATH_EF_TAG, pZipListEntry->extra, pZipListEntry->ext );
  793. pZipListEntry->uname = NULL;
  794. if( pTemp == NULL ) {
  795. return;
  796. }
  797. /* ... if so, update corresponding entries in struct zlist */
  798. pTemp += 2;
  799. /* length of this extra field */
  800. ELen = SH(pTemp);
  801. pTemp += 2;
  802. /* version */
  803. Version = (uch) *pTemp;
  804. pTemp += 1;
  805. if (Version > 1) {
  806. zipwarn("Unicode Path Extra Field version > 1 - skipping", pZipListEntry->oname);
  807. return;
  808. }
  809. /* iname CRC */
  810. iname_chksum = LG(pTemp);
  811. pTemp += 4;
  812. /*
  813. * Compute 32-bit crc of iname and AND halves to make 16-bit version
  814. */
  815. /*
  816. chksum = adler16(ADLERVAL_INITIAL,
  817. (uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
  818. */
  819. if ((iname = malloc(strlen(pZipListEntry->iname) + 1)) == NULL) {
  820. ZIPERR(ZE_MEM, "write Unicode");
  821. }
  822. strcpy(iname, pZipListEntry->iname);
  823. chksum = crc32(chksum, (uch *)(iname), strlen(iname));
  824. free(iname);
  825. /* If the checksums's don't match then likely iname has been modified and
  826. * the Unicode Path is no longer valid
  827. */
  828. if (chksum != iname_chksum) {
  829. if (unicode_mismatch == 1) {
  830. /* warn and continue */
  831. zipwarn("Unicode does not match path - ignoring Unicode: ", pZipListEntry->oname);
  832. } else if (unicode_mismatch == 2) {
  833. /* ignore and continue */
  834. } else if (unicode_mismatch == 0) {
  835. /* error */
  836. sprintf(errbuf, "Unicode does not match path: %s\n", pZipListEntry->oname);
  837. strcat(errbuf,
  838. " Likely entry name changed but Unicode not updated\n");
  839. strcat(errbuf,
  840. " Use -UN=i to ignore errors or n for no Unicode paths");
  841. zipwarn(errbuf, "");
  842. ZIPERR(ZE_FORM, "Unicode path error");
  843. }
  844. return;
  845. }
  846. ULen = ELen - 5;
  847. /* UTF-8 Path */
  848. if (ULen == 0) {
  849. /* standard path is UTF-8 so use that */
  850. ULen = pZipListEntry->nam;
  851. if ((UPath = malloc(ULen + 1)) == NULL) {
  852. return;
  853. }
  854. strcpy(UPath, pZipListEntry->name);
  855. } else {
  856. /* use Unicode path */
  857. if ((UPath = malloc(ULen + 1)) == NULL) {
  858. return;
  859. }
  860. strncpy(UPath, pTemp, ULen);
  861. UPath[ULen] = '\0';
  862. }
  863. pZipListEntry->uname = UPath;
  864. return;
  865. }
  866. #endif /* def UNICODE_SUPPORT */
  867. #ifdef ZIP64_SUPPORT /* zip64 support 08/31/2003 R.Nausedat */
  868. /* searches the cextra member of zlist for a zip64 extra field. if it finds one it */
  869. /* updates the len, siz and off members of zlist with the corresponding values of */
  870. /* the zip64 extra field, that is if either the len, siz or off member of zlist is */
  871. /* set to its max value we have to use the corresponding value from the zip64 extra */
  872. /* field. as of now the dsk member of zlist is not much of interest since we should */
  873. /* not modify multi volume archives at all. */
  874. local void adjust_zip_central_entry(pZipListEntry)
  875. struct zlist far *pZipListEntry;
  876. {
  877. char *pTemp;
  878. /* assume not using zip64 fields */
  879. zip64_entry = 0;
  880. /* check if we have a "large file" Zip64 extra field ... */
  881. pTemp = get_extra_field( ZIP64_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext );
  882. if( pTemp == NULL )
  883. return;
  884. /* using zip64 field */
  885. zip64_entry = 1;
  886. pTemp += ZIP_EF_HEADER_SIZE;
  887. /* ... if so, update corresponding entries in struct zlist */
  888. if (pZipListEntry->len == ZIP_UWORD32_MAX)
  889. {
  890. pZipListEntry->len = LLG(pTemp);
  891. pTemp += 8;
  892. }
  893. if (pZipListEntry->siz == ZIP_UWORD32_MAX)
  894. {
  895. pZipListEntry->siz = LLG(pTemp);
  896. pTemp += 8;
  897. }
  898. if (pZipListEntry->off == ZIP_UWORD32_MAX)
  899. {
  900. pZipListEntry->off = LLG(pTemp);
  901. pTemp += 8;
  902. }
  903. if (pZipListEntry->dsk == ZIP_UWORD16_MAX)
  904. {
  905. pZipListEntry->dsk = LG(pTemp);
  906. }
  907. }
  908. /* adjust_zip_local_entry
  909. *
  910. * Return 1 if there is a Zip64 extra field and 0 if not
  911. */
  912. local int adjust_zip_local_entry(pZipListEntry)
  913. struct zlist far *pZipListEntry;
  914. {
  915. char *pTemp;
  916. /* assume not using zip64 fields */
  917. zip64_entry = 0;
  918. /* check if we have a "large file" Zip64 extra field ... */
  919. pTemp = get_extra_field(ZIP64_EF_TAG, pZipListEntry->extra, pZipListEntry->ext );
  920. if( pTemp == NULL )
  921. return zip64_entry;
  922. /* using zip64 field */
  923. zip64_entry = 1;
  924. pTemp += ZIP_EF_HEADER_SIZE;
  925. /* ... if so, update corresponding entries in struct zlist */
  926. if (pZipListEntry->len == ZIP_UWORD32_MAX)
  927. {
  928. pZipListEntry->len = LLG(pTemp);
  929. pTemp += 8;
  930. }
  931. if (pZipListEntry->siz == ZIP_UWORD32_MAX)
  932. {
  933. pZipListEntry->siz = LLG(pTemp);
  934. pTemp += 8;
  935. }
  936. return zip64_entry;
  937. }
  938. /* adds a zip64 extra field to the data the cextra member of zlist points to. If
  939. * there is already a zip64 extra field present delete it first.
  940. */
  941. local int add_central_zip64_extra_field(pZipListEntry)
  942. struct zlist far *pZipListEntry;
  943. {
  944. char *pExtraFieldPtr;
  945. char *pTemp;
  946. ush usTemp;
  947. ush efsize = 0;
  948. ush esize;
  949. ush oldefsize;
  950. extent len;
  951. int used_zip64 = 0;
  952. /* get length of ef based on which fields exceed limits */
  953. /* AppNote says:
  954. * The order of the fields in the ZIP64 extended
  955. * information record is fixed, but the fields will
  956. * only appear if the corresponding Local or Central
  957. * directory record field is set to 0xFFFF or 0xFFFFFFFF.
  958. */
  959. efsize = ZIP_EF_HEADER_SIZE; /* type + size */
  960. if (pZipListEntry->len > ZIP_UWORD32_MAX || force_zip64 == 1) {
  961. /* compressed size */
  962. efsize += 8;
  963. used_zip64 = 1;
  964. }
  965. if (pZipListEntry->siz > ZIP_UWORD32_MAX) {
  966. /* uncompressed size */
  967. efsize += 8;
  968. used_zip64 = 1;
  969. }
  970. if (pZipListEntry->off > ZIP_UWORD32_MAX) {
  971. /* offset */
  972. efsize += 8;
  973. used_zip64 = 1;
  974. }
  975. if (pZipListEntry->dsk > ZIP_UWORD16_MAX) {
  976. /* disk number */
  977. efsize += 4;
  978. used_zip64 = 1;
  979. }
  980. if (used_zip64 && force_zip64 == 0) {
  981. zipwarn("Large entry support disabled using -fz- but needed", "");
  982. return ZE_BIG;
  983. }
  984. /* malloc zip64 extra field? */
  985. if( pZipListEntry->cextra == NULL )
  986. {
  987. if (efsize == ZIP_EF_HEADER_SIZE) {
  988. return ZE_OK;
  989. }
  990. if ((pExtraFieldPtr = pZipListEntry->cextra = (char *) malloc(efsize)) == NULL) {
  991. return ZE_MEM;
  992. }
  993. pZipListEntry->cext = efsize;
  994. }
  995. else
  996. {
  997. /* check if we have a "large file" extra field ... */
  998. pExtraFieldPtr = get_extra_field(ZIP64_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext);
  999. if( pExtraFieldPtr == NULL )
  1000. {
  1001. /* ... we don't, so re-malloc enough memory for the old extra data plus
  1002. * the size of the zip64 extra field
  1003. */
  1004. if ((pExtraFieldPtr = (char *) malloc(efsize + pZipListEntry->cext)) == NULL) {
  1005. return ZE_MEM;
  1006. }
  1007. /* move the old extra field */
  1008. memmove(pExtraFieldPtr, pZipListEntry->cextra, pZipListEntry->cext);
  1009. free(pZipListEntry->cextra);
  1010. pZipListEntry->cextra = pExtraFieldPtr;
  1011. pExtraFieldPtr += pZipListEntry->cext;
  1012. pZipListEntry->cext += efsize;
  1013. }
  1014. else
  1015. {
  1016. /* ... we have. sort out the existing zip64 extra field and remove it from
  1017. * pZipListEntry->cextra, re-malloc enough memory for the old extra data
  1018. * left plus the size of the zip64 extra field
  1019. */
  1020. usTemp = SH(pExtraFieldPtr + 2);
  1021. /* if pZipListEntry->cextra == pExtraFieldPtr and pZipListEntry->cext == usTemp + efsize
  1022. * we should have only one extra field, and this is a zip64 extra field. as some
  1023. * zip tools seem to require fixed zip64 extra fields we have to check if
  1024. * usTemp + ZIP_EF_HEADER_SIZE is equal to ZIP64_LARGE_FILE_HEAD_SIZE. if it
  1025. * isn't, we free the old extra field and allocate memory for a new one
  1026. */
  1027. if( pZipListEntry->cext == (extent)(usTemp + ZIP_EF_HEADER_SIZE) )
  1028. {
  1029. /* just Zip64 extra field in extra field */
  1030. if( pZipListEntry->cext != efsize )
  1031. {
  1032. /* wrong size */
  1033. if ((pExtraFieldPtr = (char *) malloc(efsize)) == NULL) {
  1034. return ZE_MEM;
  1035. }
  1036. free(pZipListEntry->cextra);
  1037. pZipListEntry->cextra = pExtraFieldPtr;
  1038. pZipListEntry->cext = efsize;
  1039. }
  1040. }
  1041. else
  1042. {
  1043. /* get the old Zip64 extra field out and add new */
  1044. oldefsize = usTemp + ZIP_EF_HEADER_SIZE;
  1045. if ((pTemp = (char *) malloc(pZipListEntry->cext - oldefsize + efsize)) == NULL) {
  1046. return ZE_MEM;
  1047. }
  1048. len = (extent)(pExtraFieldPtr - pZipListEntry->cextra);
  1049. memcpy(pTemp, pZipListEntry->cextra, len);
  1050. memcpy(pTemp + len, pExtraFieldPtr + oldefsize,
  1051. pZipListEntry->cext - oldefsize - len);
  1052. pZipListEntry->cext -= oldefsize;
  1053. pExtraFieldPtr = pTemp + pZipListEntry->cext;
  1054. pZipListEntry->cext += efsize;
  1055. free(pZipListEntry->cextra);
  1056. pZipListEntry->cextra = pTemp;
  1057. }
  1058. }
  1059. }
  1060. /* set zip64 extra field members */
  1061. write_ushort_to_mem(ZIP64_EF_TAG, pExtraFieldPtr);
  1062. write_ushort_to_mem((ush) (efsize - ZIP_EF_HEADER_SIZE), pExtraFieldPtr + 2);
  1063. esize = ZIP_EF_HEADER_SIZE;
  1064. if (pZipListEntry->len > ZIP_UWORD32_MAX || force_zip64 == 1) {
  1065. write_int64_to_mem(pZipListEntry->len, pExtraFieldPtr + esize);
  1066. esize += 8;
  1067. }
  1068. if (pZipListEntry->siz > ZIP_UWORD32_MAX) {
  1069. write_int64_to_mem(pZipListEntry->siz, pExtraFieldPtr + esize);
  1070. esize += 8;
  1071. }
  1072. if (pZipListEntry->off > ZIP_UWORD32_MAX) {
  1073. write_int64_to_mem(pZipListEntry->off, pExtraFieldPtr + esize);
  1074. esize += 8;
  1075. }
  1076. if (pZipListEntry->dsk > ZIP_UWORD16_MAX) {
  1077. write_ulong_to_mem(pZipListEntry->dsk, pExtraFieldPtr + esize);
  1078. }
  1079. /* un' wech */
  1080. return ZE_OK;
  1081. }
  1082. #if 0
  1083. /* Remove extra field in local extra field
  1084. * Return 1 if found, else 0
  1085. * 12/28/05
  1086. */
  1087. local int remove_local_extra_field(pZEntry, tag)
  1088. struct zlist far *pZEntry;
  1089. ulg tag;
  1090. {
  1091. char *pExtra;
  1092. char *pOldExtra;
  1093. char *pOldTemp;
  1094. char *pTemp;
  1095. ush newEFSize;
  1096. ush usTemp;
  1097. ush blocksize;
  1098. /* check if we have the extra field ... */
  1099. pOldExtra = get_extra_field( (ush)tag, pZEntry->extra, pZEntry->ext );
  1100. if (pOldExtra)
  1101. {
  1102. /* We have. Get rid of it. */
  1103. blocksize = SH( pOldExtra + 2 );
  1104. newEFSize = pZEntry->ext - blocksize;
  1105. pExtra = (char *) malloc( newEFSize );
  1106. if( pExtra == NULL )
  1107. ziperr(ZE_MEM, "Remove Local Extra Field");
  1108. /* move all before EF */
  1109. usTemp = (extent) (pOldExtra - pZEntry->extra);
  1110. pTemp = pExtra;
  1111. memcpy( pTemp, pZEntry->extra, usTemp );
  1112. /* move all after old Zip64 EF */
  1113. pTemp = pExtra + usTemp;
  1114. pOldTemp = pOldExtra + blocksize;
  1115. usTemp = pZEntry->ext - usTemp - blocksize;
  1116. memcpy( pTemp, pOldTemp, usTemp);
  1117. /* replace extra fields */
  1118. pZEntry->ext = newEFSize;
  1119. free(pZEntry->extra);
  1120. pZEntry->extra = pExtra;
  1121. return 1;
  1122. } else {
  1123. return 0;
  1124. }
  1125. }
  1126. /* Remove extra field in central extra field
  1127. * Return 1 if found, else 0
  1128. * 12/28/05
  1129. */
  1130. local int remove_central_extra_field(pZEntry, tag)
  1131. struct zlist far *pZEntry;
  1132. ulg tag;
  1133. {
  1134. char *pExtra;
  1135. char *pOldExtra;
  1136. char *pOldTemp;
  1137. char *pTemp;
  1138. ush newEFSize;
  1139. ush usTemp;
  1140. ush blocksize;
  1141. /* check if we have the extra field ... */
  1142. pOldExtra = get_extra_field( (ush)tag, pZEntry->cextra, pZEntry->cext );
  1143. if (pOldExtra)
  1144. {
  1145. /* We have. Get rid of it. */
  1146. blocksize = SH( pOldExtra + 2 );
  1147. newEFSize = pZEntry->cext - blocksize;
  1148. pExtra = (char *) malloc( newEFSize );
  1149. if( pExtra == NULL )
  1150. ziperr(ZE_MEM, "Remove Local Extra Field");
  1151. /* move all before EF */
  1152. usTemp = (extent) (pOldExtra - pZEntry->cextra);
  1153. pTemp = pExtra;
  1154. memcpy( pTemp, pZEntry->cextra, usTemp );
  1155. /* move all after old Zip64 EF */
  1156. pTemp = pExtra + usTemp;
  1157. pOldTemp = pOldExtra + blocksize;
  1158. usTemp = pZEntry->cext - usTemp - blocksize;
  1159. memcpy( pTemp, pOldTemp, usTemp);
  1160. /* replace extra fields */
  1161. pZEntry->cext = newEFSize;
  1162. free(pZEntry->cextra);
  1163. pZEntry->cextra = pExtra;
  1164. return 1;
  1165. } else {
  1166. return 0;
  1167. }
  1168. }
  1169. #endif
  1170. /* Add Zip64 extra field to local header
  1171. * 10/5/03 EG
  1172. */
  1173. local int add_local_zip64_extra_field(pZEntry)
  1174. struct zlist far *pZEntry;
  1175. {
  1176. char *pZ64Extra;
  1177. char *pOldZ64Extra;
  1178. char *pOldTemp;
  1179. char *pTemp;
  1180. ush newEFSize;
  1181. ush usTemp;
  1182. ush blocksize;
  1183. ush Z64LocalLen = ZIP_EF_HEADER_SIZE + /* tag + EF Data Len */
  1184. 8 + /* original uncompressed length of file */
  1185. 8; /* compressed size of file */
  1186. /* malloc zip64 extra field? */
  1187. /* after the below pZ64Extra should point to start of Zip64 extra field */
  1188. if (pZEntry->ext == 0 || pZEntry->extra == NULL)
  1189. {
  1190. /* get new extra field */
  1191. pZ64Extra = pZEntry->extra = (char *) malloc(Z64LocalLen);
  1192. if (pZEntry->extra == NULL) {
  1193. ziperr( ZE_MEM, "Zip64 local extra field" );
  1194. }
  1195. pZEntry->ext = Z64LocalLen;
  1196. }
  1197. else
  1198. {
  1199. /* check if we have a Zip64 extra field ... */
  1200. pOldZ64Extra = get_extra_field( ZIP64_EF_TAG, pZEntry->extra, pZEntry->ext );
  1201. if (pOldZ64Extra == NULL)
  1202. {
  1203. /* ... we don't, so re-malloc enough memory for the old extra data plus */
  1204. /* the size of the zip64 extra field */
  1205. pZ64Extra = (char *) malloc( Z64LocalLen + pZEntry->ext );
  1206. if (pZ64Extra == NULL)
  1207. ziperr( ZE_MEM, "Zip64 Extra Field" );
  1208. /* move old extra field and update pointer and length */
  1209. memmove( pZ64Extra, pZEntry->extra, pZEntry->ext);
  1210. free( pZEntry->extra );
  1211. pZEntry->extra = pZ64Extra;
  1212. pZ64Extra += pZEntry->ext;
  1213. pZEntry->ext += Z64LocalLen;
  1214. }
  1215. else
  1216. {
  1217. /* ... we have. Sort out the existing zip64 extra field and remove it
  1218. * from pZEntry->extra, re-malloc enough memory for the old extra data
  1219. * left plus the size of the zip64 extra field */
  1220. blocksize = SH( pOldZ64Extra + 2 );
  1221. /* If the right length then go with it, else get rid of it and add a new extra field
  1222. * to existing block. */
  1223. if (blocksize == Z64LocalLen - ZIP_EF_HEADER_SIZE)
  1224. {
  1225. /* looks good */
  1226. pZ64Extra = pOldZ64Extra;
  1227. }
  1228. else
  1229. {
  1230. newEFSize = pZEntry->ext - (blocksize + ZIP_EF_HEADER_SIZE) + Z64LocalLen;
  1231. pZ64Extra = (char *) malloc( newEFSize );
  1232. if( pZ64Extra == NULL )
  1233. ziperr(ZE_MEM, "Zip64 Extra Field");
  1234. /* move all before Zip64 EF */
  1235. usTemp = (extent) (pOldZ64Extra - pZEntry->extra);
  1236. pTemp = pZ64Extra;
  1237. memcpy( pTemp, pZEntry->extra, usTemp );
  1238. /* move all after old Zip64 EF */
  1239. pTemp = pZ64Extra + usTemp;
  1240. pOldTemp = pOldZ64Extra + ZIP_EF_HEADER_SIZE + blocksize;
  1241. usTemp = pZEntry->ext - usTemp - blocksize;
  1242. memcpy( pTemp, pOldTemp, usTemp);
  1243. /* replace extra fields */
  1244. pZEntry->ext = newEFSize;
  1245. free(pZEntry->extra);
  1246. pZEntry->extra = pZ64Extra;
  1247. pZ64Extra = pTemp + usTemp;
  1248. }
  1249. }
  1250. }
  1251. /* set/update zip64 extra field members */
  1252. write_ushort_to_mem(ZIP64_EF_TAG, pZ64Extra);
  1253. write_ushort_to_mem((ush) (Z64LocalLen - ZIP_EF_HEADER_SIZE), pZ64Extra + 2);
  1254. write_int64_to_mem(pZEntry->len, pZ64Extra + 2 + 2);
  1255. write_int64_to_mem(pZEntry->siz, pZ64Extra + 2 + 2 + 8);
  1256. return ZE_OK;
  1257. }
  1258. # endif /* ZIP64_SUPPORT */
  1259. #ifdef UNICODE_SUPPORT
  1260. /* Add UTF-8 path extra field
  1261. * 10/11/05
  1262. */
  1263. local int add_Unicode_Path_local_extra_field(pZEntry)
  1264. struct zlist far *pZEntry;
  1265. {
  1266. char *pUExtra;
  1267. char *pOldUExtra;
  1268. char *pOldTemp;
  1269. char *pTemp;
  1270. #ifdef WIN32_OEM
  1271. char *inameLocal;
  1272. #endif
  1273. ush newEFSize;
  1274. ush usTemp;
  1275. ush ULen = strlen(pZEntry->uname);
  1276. ush blocksize;
  1277. ulg chksum = CRCVAL_INITIAL;
  1278. ush ULocalLen = ZIP_EF_HEADER_SIZE + /* tag + EF Data Len */
  1279. 1 + /* version */
  1280. 4 + /* iname chksum */
  1281. ULen; /* UTF-8 path */
  1282. /* malloc Unicode Path extra field? */
  1283. /* after the below pUExtra should point to start of Unicode Path extra field */
  1284. if (pZEntry->ext == 0 || pZEntry->extra == NULL)
  1285. {
  1286. /* get new extra field */
  1287. pUExtra = pZEntry->extra = (char *) malloc(ULocalLen);
  1288. if (pZEntry->extra == NULL) {
  1289. ziperr( ZE_MEM, "UTF-8 Path local extra field" );
  1290. }
  1291. pZEntry->ext = ULocalLen;
  1292. }
  1293. else
  1294. {
  1295. /* check if we have a Unicode Path extra field ... */
  1296. pOldUExtra = get_extra_field( UTF8_PATH_EF_TAG, pZEntry->extra, pZEntry->ext );
  1297. if (pOldUExtra == NULL)
  1298. {
  1299. /* ... we don't, so re-malloc enough memory for the old extra data plus */
  1300. /* the size of the UTF-8 Path extra field */
  1301. pUExtra = (char *) malloc( ULocalLen + pZEntry->ext );
  1302. if (pUExtra == NULL)
  1303. ziperr( ZE_MEM, "UTF-8 Path Extra Field" );
  1304. /* move old extra field and update pointer and length */
  1305. memmove( pUExtra, pZEntry->extra, pZEntry->ext);
  1306. free( pZEntry->extra );
  1307. pZEntry->extra = pUExtra;
  1308. pUExtra += pZEntry->ext;
  1309. pZEntry->ext += ULocalLen;
  1310. }
  1311. else
  1312. {
  1313. /* ... we have. Sort out the existing UTF-8 Path extra field and remove it
  1314. * from pZEntry->extra, re-malloc enough memory for the old extra data
  1315. * left plus the size of the UTF-8 Path extra field */
  1316. blocksize = SH( pOldUExtra + 2 );
  1317. /* If the right length then go with it, else get rid of it and add a new extra field
  1318. * to existing block. */
  1319. if (blocksize == ULocalLen - ZIP_EF_HEADER_SIZE)
  1320. {
  1321. /* looks good */
  1322. pUExtra = pOldUExtra;
  1323. }
  1324. else
  1325. {
  1326. newEFSize = pZEntry->ext - (blocksize + ZIP_EF_HEADER_SIZE) + ULocalLen;
  1327. pUExtra = (char *) malloc( newEFSize );
  1328. if( pUExtra == NULL )
  1329. ziperr(ZE_MEM, "UTF-8 Path Extra Field");
  1330. /* move all before UTF-8 Path EF */
  1331. usTemp = (extent) (pOldUExtra - pZEntry->extra);
  1332. pTemp = pUExtra;
  1333. memcpy( pTemp, pZEntry->extra, usTemp );
  1334. /* move all after old UTF-8 Path EF */
  1335. pTemp = pUExtra + usTemp;
  1336. pOldTemp = pOldUExtra + ZIP_EF_HEADER_SIZE + blocksize;
  1337. usTemp = pZEntry->ext - usTemp - blocksize;
  1338. memcpy( pTemp, pOldTemp, usTemp);
  1339. /* replace extra fields */
  1340. pZEntry->ext = newEFSize;
  1341. free(pZEntry->extra);
  1342. pZEntry->extra = pUExtra;
  1343. pUExtra = pTemp + usTemp;
  1344. }
  1345. }
  1346. }
  1347. /*
  1348. * Compute the Adler-16 checksum of iname
  1349. */
  1350. /*
  1351. chksum = adler16(ADLERVAL_INITIAL,
  1352. (uch *)(pZEntry->iname), strlen(pZEntry->iname));
  1353. */
  1354. #ifdef WIN32_OEM
  1355. if ((inameLocal = malloc(strlen(pZEntry->iname) + 1)) == NULL) {
  1356. ZIPERR(ZE_MEM, "write Unicode");
  1357. }
  1358. /* if oem translation done convert back for checksum */
  1359. if ((pZEntry->vem & 0xff00) == 0) {
  1360. /* get original */
  1361. INTERN_TO_OEM(pZEntry->iname, inameLocal);
  1362. } else {
  1363. strcpy(inameLocal, pZEntry->iname);
  1364. }
  1365. #else
  1366. # define inameLocal (pZEntry->iname)
  1367. #endif
  1368. chksum = crc32(chksum, (uch *)(inameLocal), strlen(inameLocal));
  1369. #ifdef WIN32_OEM
  1370. free(inameLocal);
  1371. #else
  1372. # undef inameLocal
  1373. #endif
  1374. /* set/update UTF-8 Path extra field members */
  1375. /* tag header */
  1376. write_ushort_to_mem(UTF8_PATH_EF_TAG, pUExtra);
  1377. /* data size */
  1378. write_ushort_to_mem((ush) (ULocalLen - ZIP_EF_HEADER_SIZE), pUExtra + 2);
  1379. /* version */
  1380. *(pUExtra + 2 + 2) = 1;
  1381. /* iname chksum */
  1382. write_ulong_to_mem(chksum, pUExtra + 2 + 2 + 1);
  1383. /* UTF-8 path */
  1384. write_string_to_mem(pZEntry->uname, pUExtra + 2 + 2 + 1 + 4);
  1385. return ZE_OK;
  1386. }
  1387. local int add_Unicode_Path_cen_extra_field(pZEntry)
  1388. struct zlist far *pZEntry;
  1389. {
  1390. char *pUExtra;
  1391. char *pOldUExtra;
  1392. char *pOldTemp;
  1393. char *pTemp;
  1394. #ifdef WIN32_OEM
  1395. char *inameLocal;
  1396. #endif
  1397. ush newEFSize;
  1398. ush usTemp;
  1399. ush ULen = strlen(pZEntry->uname);
  1400. ush blocksize;
  1401. ulg chksum = CRCVAL_INITIAL;
  1402. ush UCenLen = ZIP_EF_HEADER_SIZE + /* tag + EF Data Len */
  1403. 1 + /* version */
  1404. 4 + /* checksum */
  1405. ULen; /* UTF-8 path */
  1406. /* malloc Unicode Path extra field? */
  1407. /* after the below pUExtra should point to start of Unicode Path extra field */
  1408. if (pZEntry->cext == 0 || pZEntry->cextra == NULL)
  1409. {
  1410. /* get new extra field */
  1411. pUExtra = pZEntry->cextra = (char *) malloc(UCenLen);
  1412. if (pZEntry->cextra == NULL) {
  1413. ziperr( ZE_MEM, "UTF-8 Path cen extra field" );
  1414. }
  1415. pZEntry->cext = UCenLen;
  1416. }
  1417. else
  1418. {
  1419. /* check if we have a Unicode Path extra field ... */
  1420. pOldUExtra = get_extra_field( UTF8_PATH_EF_TAG, pZEntry->cextra, pZEntry->cext );
  1421. if (pOldUExtra == NULL)
  1422. {
  1423. /* ... we don't, so re-malloc enough memory for the old extra data plus */
  1424. /* the size of the UTF-8 Path extra field */
  1425. pUExtra = (char *) malloc( UCenLen + pZEntry->cext );
  1426. if (pUExtra == NULL)
  1427. ziperr( ZE_MEM, "UTF-8 Path Extra Field" );
  1428. /* move old extra field and update pointer and length */
  1429. memmove( pUExtra, pZEntry->cextra, pZEntry->cext);
  1430. free( pZEntry->cextra );
  1431. pZEntry->cextra = pUExtra;
  1432. pUExtra += pZEntry->cext;
  1433. pZEntry->cext += UCenLen;
  1434. }
  1435. else
  1436. {
  1437. /* ... we have. Sort out the existing UTF-8 Path extra field and remove it
  1438. * from pZEntry->extra, re-malloc enough memory for the old extra data
  1439. * left plus the size of the UTF-8 Path extra field */
  1440. blocksize = SH( pOldUExtra + 2 );
  1441. /* If the right length then go with it, else get rid of it and add a new extra field
  1442. * to existing block. */
  1443. if (blocksize == UCenLen - ZIP_EF_HEADER_SIZE)
  1444. {
  1445. /* looks good */
  1446. pUExtra = pOldUExtra;
  1447. }
  1448. else
  1449. {
  1450. newEFSize = pZEntry->cext - (blocksize + ZIP_EF_HEADER_SIZE) + UCenLen;
  1451. pUExtra = (char *) malloc( newEFSize );
  1452. if( pUExtra == NULL )
  1453. ziperr(ZE_MEM, "UTF-8 Path Extra Field");
  1454. /* move all before UTF-8 Path EF */
  1455. usTemp = (extent) (pOldUExtra - pZEntry->cextra);
  1456. pTemp = pUExtra;
  1457. memcpy( pTemp, pZEntry->cextra, usTemp );
  1458. /* move all after old UTF-8 Path EF */
  1459. pTemp = pUExtra + usTemp;
  1460. pOldTemp = pOldUExtra + ZIP_EF_HEADER_SIZE + blocksize;
  1461. usTemp = pZEntry->cext - usTemp - blocksize;
  1462. memcpy( pTemp, pOldTemp, usTemp);
  1463. /* replace extra fields */
  1464. pZEntry->cext = newEFSize;
  1465. free(pZEntry->cextra);
  1466. pZEntry->cextra = pUExtra;
  1467. pUExtra = pTemp + usTemp;
  1468. }
  1469. }
  1470. }
  1471. /*
  1472. * Compute the CRC-32 checksum of iname
  1473. */
  1474. #ifdef WIN32_OEM
  1475. if ((inameLocal = malloc(strlen(pZEntry->iname) + 1)) == NULL) {
  1476. ZIPERR(ZE_MEM, "write Unicode");
  1477. }
  1478. /* if oem translation done convert back for checksum */
  1479. if ((pZEntry->vem & 0xff00) == 0) {
  1480. /* get original */
  1481. INTERN_TO_OEM(pZEntry->iname, inameLocal);
  1482. } else {
  1483. strcpy(inameLocal, pZEntry->iname);
  1484. }
  1485. #else
  1486. # define inameLocal (pZEntry->iname)
  1487. #endif
  1488. chksum = crc32(chksum, (uch *)(inameLocal), strlen(inameLocal));
  1489. #ifdef WIN32_OEM
  1490. free(inameLocal);
  1491. #else
  1492. # undef inameLocal
  1493. #endif
  1494. /*
  1495. * Compute the Adler-16 checksum of iname
  1496. */
  1497. /*
  1498. chksum = adler16(ADLERVAL_INITIAL,
  1499. (uch *)(pZEntry->iname), strlen(pZEntry->iname));
  1500. */
  1501. /* set/update UTF-8 Path extra field members */
  1502. /* tag header */
  1503. write_ushort_to_mem(UTF8_PATH_EF_TAG, pUExtra);
  1504. /* data size */
  1505. write_ushort_to_mem((ush) (UCenLen - ZIP_EF_HEADER_SIZE), pUExtra + 2);
  1506. /* version */
  1507. *(pUExtra + 2 + 2) = 1;
  1508. /* iname checksum */
  1509. write_ulong_to_mem(chksum, pUExtra + 2 + 2 + 1);
  1510. /* UTF-8 path */
  1511. write_string_to_mem(pZEntry->uname, pUExtra + 2 + 2 + 1 + 4);
  1512. return ZE_OK;
  1513. }
  1514. #endif /* def UNICODE_SUPPORT */
  1515. zoff_t ffile_size OF((FILE *));
  1516. /* 2004-12-06 SMS.
  1517. * ffile_size() returns reliable file size or EOF.
  1518. * May be used to detect large files in a small-file program.
  1519. */
  1520. zoff_t ffile_size( file)
  1521. FILE *file;
  1522. {
  1523. int sts;
  1524. size_t siz;
  1525. zoff_t ofs;
  1526. char waste[ 4];
  1527. /* Seek to actual EOF. */
  1528. sts = zfseeko( file, 0, SEEK_END);
  1529. if (sts != 0)
  1530. {
  1531. /* fseeko() failed. (Unlikely.) */
  1532. ofs = EOF;
  1533. }
  1534. else
  1535. {
  1536. /* Get apparent offset at EOF. */
  1537. ofs = zftello( file);
  1538. if (ofs < 0)
  1539. {
  1540. /* Offset negative (overflow). File too big. */
  1541. ofs = EOF;
  1542. }
  1543. else
  1544. {
  1545. /* Seek to apparent EOF offset.
  1546. Won't be at actual EOF if offset was truncated.
  1547. */
  1548. sts = zfseeko( file, ofs, SEEK_SET);
  1549. if (sts != 0)
  1550. {
  1551. /* fseeko() failed. (Unlikely.) */
  1552. ofs = EOF;
  1553. }
  1554. else
  1555. {
  1556. /* Read a byte at apparent EOF. Should set EOF flag. */
  1557. siz = fread( waste, 1, 1, file);
  1558. if (feof( file) == 0)
  1559. {
  1560. /* Not at EOF, but should be. File too big. */
  1561. ofs = EOF;
  1562. }
  1563. }
  1564. }
  1565. }
  1566. /* Seek to BOF.
  1567. *
  1568. * 2007-05-23 SMS.
  1569. * Note that a problem in a prehistoric VAX C run-time library
  1570. * requires that rewind() be used instead of fseek(), or else
  1571. * the EOF flag is not cleared properly.
  1572. */
  1573. /* As WIN32 has this same problem (EOF not being cleared) when
  1574. * NO_ZIP64_SUPPORT is set but LARGE_FILE_SUPPORT is set on a
  1575. * small file, seems no reason not to always use rewind().
  1576. * 8/5/07 EG
  1577. */
  1578. #if 0
  1579. #ifdef VAXC
  1580. sts = rewind( file);
  1581. #else /* def VAXC */
  1582. sts = zfseeko( file, 0, SEEK_SET);
  1583. #endif /* def VAXC [else] */
  1584. #endif
  1585. rewind(file);
  1586. return ofs;
  1587. }
  1588. #ifndef UTIL
  1589. local void zipoddities(z)
  1590. struct zlist far *z;
  1591. {
  1592. if ((z->vem >> 8) >= NUM_HOSTS)
  1593. {
  1594. sprintf(errbuf, "made by version %d.%d on system type %d: ",
  1595. (ush)(z->vem & 0xff) / (ush)10, (ush)(z->vem & 0xff) % (ush)10,
  1596. z->vem >> 8);
  1597. zipwarn(errbuf, z->oname);
  1598. }
  1599. if (z->ver != 10 && z->ver != 11 && z->ver != 20)
  1600. {
  1601. sprintf(errbuf, "needs unzip %d.%d on system type %d: ",
  1602. (ush)(z->ver & 0xff) / (ush)10,
  1603. (ush)(z->ver & 0xff) % (ush)10, z->ver >> 8);
  1604. zipwarn(errbuf, z->oname);
  1605. }
  1606. if ((fix == 2) && (z->flg != z->lflg))
  1607. /* The comparision between central and local version of the
  1608. "general purpose bit flag" cannot be used from scanzipf_regnew(),
  1609. because in the "regular" zipfile processing, the local header reads
  1610. have been postponed until the actual entry processing takes place.
  1611. They have not yet been read when "zipoddities()" is called.
  1612. This change was neccessary to support multivolume archives.
  1613. */
  1614. {
  1615. sprintf(errbuf, "local flags = 0x%04x, central = 0x%04x: ",
  1616. z->lflg, z->flg);
  1617. zipwarn(errbuf, z->oname);
  1618. }
  1619. else if (z->flg & ~0xf && (z->flg & ~0xf0) != UTF8_BIT)
  1620. /* Only bit in high byte we support is the new UTF-8 bit */
  1621. {
  1622. sprintf(errbuf, "undefined bits used in flags = 0x%04x: ", z->flg);
  1623. zipwarn(errbuf, z->oname);
  1624. }
  1625. if (z->how > LAST_KNOWN_COMPMETHOD) {
  1626. sprintf(errbuf, "unknown compression method %u: ", z->how);
  1627. zipwarn(errbuf, z->oname);
  1628. }
  1629. if (z->dsk)
  1630. {
  1631. sprintf(errbuf, "starts on disk %lu: ", z->dsk);
  1632. zipwarn(errbuf, z->oname);
  1633. }
  1634. if (z->att!=ASCII && z->att!=BINARY && z->att!=__EBCDIC)
  1635. {
  1636. sprintf(errbuf, "unknown internal attributes = 0x%04x: ", z->att);
  1637. zipwarn(errbuf, z->oname);
  1638. }
  1639. # if 0
  1640. /* This test is ridiculous, it produces an error message for almost every */
  1641. /* platform of origin other than MS-DOS, Unix, VMS, and Acorn! Perhaps */
  1642. /* we could test "if (z->dosflag && z->atx & ~0xffL)", but what for? */
  1643. if (((n = z->vem >> 8) != 3) && n != 2 && n != 13 && z->atx & ~0xffL)
  1644. {
  1645. sprintf(errbuf, "unknown external attributes = 0x%08lx: ", z->atx);
  1646. zipwarn(errbuf, z->oname);
  1647. }
  1648. # endif
  1649. /* This test is just annoying, as Zip itself does not write the same
  1650. extra fields to both the local and central headers. It's much more
  1651. complicated than this test implies. 3/17/05 */
  1652. #if 0
  1653. if (z->ext || z->cext)
  1654. {
  1655. # if 0
  1656. if (z->ext && z->cext && z->extra != z->cextra)
  1657. {
  1658. sprintf(errbuf,
  1659. "local extra (%ld bytes) != central extra (%ld bytes): ",
  1660. (ulg)z->ext, (ulg)z->cext);
  1661. if (noisy) fprintf(mesg, "\tzip info: %s%s\n", errbuf, z->oname);
  1662. }
  1663. # if (!defined(RISCOS) && !defined(CMS_MVS))
  1664. /* in noisy mode, extra field sizes are always reported */
  1665. else if (noisy)
  1666. # else /* RISCOS || CMS_MVS */
  1667. /* avoid warnings for zipfiles created on the same type of OS system! */
  1668. /* or, was this warning really intended (eg. OS/2)? */
  1669. /* Only give info if extra bytes were added by another system */
  1670. else if (noisy && ((z->vem >> 8) != (OS_CODE >> 8)))
  1671. # endif /* ?(RISCOS || CMS_MVS) */
  1672. # endif /* 0 */
  1673. {
  1674. fprintf(mesg, "zip info: %s has %ld bytes of %sextra data\n",
  1675. z->oname, z->ext ? (ulg)z->ext : (ulg)z->cext,
  1676. z->ext ? (z->cext ? "" : "local ") : "central ");
  1677. }
  1678. }
  1679. #endif
  1680. }
  1681. #if 0 /* scanzipf_fix() no longer used */
  1682. /*
  1683. * scanzipf_fix is called with zip -F or zip -FF
  1684. * read the file from front to back and pick up the pieces
  1685. * NOTE: there are still checks missing to see if the header
  1686. * that was found is *VALID*
  1687. *
  1688. * Still much work to do so can handle more cases. 1/18/04 EG
  1689. */
  1690. local int scanzipf_fix(f)
  1691. FILE *f; /* zip file */
  1692. /*
  1693. The name of the zip file is pointed to by the global "zipfile". The globals
  1694. zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
  1695. Return an error code in the ZE_ class.
  1696. */
  1697. {
  1698. ulg a = 0L; /* attributes returned by filetime() */
  1699. char b[CENHEAD]; /* buffer for central headers */
  1700. ush flg; /* general purpose bit flag */
  1701. int m; /* mismatch flag */
  1702. extent n; /* length of name */
  1703. uzoff_t p; /* current file offset */
  1704. uzoff_t s; /* size of data, start of central */
  1705. struct zlist far * far *x; /* pointer last entry's link */
  1706. struct zlist far *z; /* current zip entry structure */
  1707. #ifndef ZIP64_SUPPORT
  1708. /* 2004-12-06 SMS.
  1709. * Check for too-big file before doing any serious work.
  1710. */
  1711. if (ffile_size( f) == EOF)
  1712. return ZE_ZIP64;
  1713. #endif /* ndef ZIP64_SUPPORT */
  1714. /* Get any file attribute valid for this OS, to set in the central
  1715. * directory when fixing the archive:
  1716. */
  1717. # ifndef UTIL
  1718. filetime(zipfile, &a, (zoff_t*)&s, NULL);
  1719. # endif
  1720. x = &zfiles; /* first link */
  1721. p = 0; /* starting file offset */
  1722. # ifdef HANDLE_AMIGA_SFX
  1723. amiga_sfx_offset = 0L;
  1724. # endif
  1725. /* Find start of zip structures */
  1726. for (;;) {
  1727. /* look for signature */
  1728. while ((m = getc(f)) != EOF && m != 0x50) /* 0x50 == 'P' */
  1729. {
  1730. # ifdef HANDLE_AMIGA_SFX
  1731. if (p == 0 && m == 0)
  1732. amiga_sfx_offset = 1L;
  1733. else if (amiga_sfx_offset) {
  1734. if ((p == 1 && m != 0) || (p == 2 && m != 3)
  1735. || (p == 3 && (uch) m != 0xF3))
  1736. amiga_sfx_offset = 0L;
  1737. }
  1738. # endif /* HANDLE_AMIGA_SFX */
  1739. p++;
  1740. }
  1741. /* found a P */
  1742. b[0] = (char) m;
  1743. /* local - 11/2/03 EG */
  1744. if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG)
  1745. break;
  1746. /* why search for ENDSIG if doing only local - 11/2/03 EG
  1747. if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == ENDSIG)
  1748. break;
  1749. */
  1750. /* back up */
  1751. if (zfseeko(f, -3L, SEEK_CUR))
  1752. return ferror(f) ? ZE_READ : ZE_EOF;
  1753. /* move 1 byte forward */
  1754. p++;
  1755. }
  1756. zipbeg = p;
  1757. # ifdef HANDLE_AMIGA_SFX
  1758. if (amiga_sfx_offset && zipbeg >= 12 && (zipbeg & 3) == 0
  1759. && fseek(f, -12L, SEEK_CUR) == 0 && fread(b, 12, 1, f) == 1
  1760. && LG(b + 4) == 0xF1030000 /* 1009 in Motorola byte order */)
  1761. amiga_sfx_offset = zipbeg - 4;
  1762. else
  1763. amiga_sfx_offset = 0L;
  1764. # endif /* HANDLE_AMIGA_SFX */
  1765. /* Read local headers */
  1766. while (LG(b) == LOCSIG)
  1767. {
  1768. if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL ||
  1769. zcount + 1 < zcount)
  1770. return ZE_MEM;
  1771. if (fread(b, LOCHEAD, 1, f) != 1) {
  1772. farfree((zvoid far *)z);
  1773. break;
  1774. }
  1775. z->ver = SH(LOCVER + b);
  1776. z->vem = (ush)(dosify ? 20 : OS_CODE + Z_MAJORVER * 10 + Z_MINORVER);
  1777. z->dosflag = dosify;
  1778. flg = z->flg = z->lflg = SH(LOCFLG + b);
  1779. z->how = SH(LOCHOW + b);
  1780. z->tim = LG(LOCTIM + b); /* time and date into one long */
  1781. z->crc = LG(LOCCRC + b);
  1782. z->siz = LG(LOCSIZ + b);
  1783. z->len = LG(LOCLEN + b);
  1784. n = z->nam = SH(LOCNAM + b);
  1785. z->cext = z->ext = SH(LOCEXT + b);
  1786. z->com = 0;
  1787. z->dsk = 0;
  1788. z->att = 0;
  1789. z->atx = dosify ? a & 0xff : a; /* Attributes from filetime() */
  1790. z->mark = 0;
  1791. z->trash = 0;
  1792. /* attention: this one breaks the VC optimizer (Release Build) */
  1793. /* may be fixed - 11/1/03 EG */
  1794. s = fix > 1 ? 0L : z->siz; /* discard compressed size with -FF */
  1795. /* Initialize all fields pointing to malloced data to NULL */
  1796. z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
  1797. z->oname = NULL;
  1798. #ifdef UNICODE_SUPPORT
  1799. z->uname = z->zuname = z->ouname = NULL;
  1800. #endif
  1801. /* Link into list */
  1802. *x = z;
  1803. z->nxt = NULL;
  1804. x = &z->nxt;
  1805. /* Read file name and extra field and skip data */
  1806. if (n == 0)
  1807. {
  1808. sprintf(errbuf, "%lu", (ulg)zcount + 1);
  1809. zipwarn("zero-length name for entry #", errbuf);
  1810. # ifndef DEBUG
  1811. return ZE_FORM;
  1812. # endif
  1813. }
  1814. if ((z->iname = malloc(n+1)) == NULL ||
  1815. (z->ext && (z->extra = malloc(z->ext)) == NULL))
  1816. return ZE_MEM;
  1817. if (fread(z->iname, n, 1, f) != 1 ||
  1818. (z->ext && fread(z->extra, z->ext, 1, f) != 1))
  1819. return ferror(f) ? ZE_READ : ZE_EOF;
  1820. # ifdef ZIP64_SUPPORT
  1821. /* adjust/update siz,len and off (to come: dsk) entries */
  1822. /* PKZIP does not care of the version set in a CDH: if */
  1823. /* there is a zip64 extra field assigned to a CDH PKZIP */
  1824. /* uses it, we should do so, too. */
  1825. zip64_entry = adjust_zip_local_entry(z);
  1826. /* z->siz may be updated */
  1827. s = fix > 1 ? 0L : z->siz; /* discard compressed size with -FF */
  1828. # endif
  1829. if (s && zfseeko(f, (zoff_t)s, SEEK_CUR))
  1830. return ferror(f) ? ZE_READ : ZE_EOF;
  1831. /* If there is an extended local header, s is either 0 or
  1832. * the correct compressed size.
  1833. */
  1834. z->iname[n] = '\0'; /* terminate name */
  1835. z->zname = in2ex(z->iname); /* convert to external name */
  1836. if (z->zname == NULL)
  1837. return ZE_MEM;
  1838. z->name = z->zname;
  1839. z->cextra = z->extra;
  1840. if (noisy) fprintf(mesg, "zip: reading %s\n", z->zname);
  1841. /* Save offset, update for next header */
  1842. z->off = p;
  1843. p += 4 + LOCHEAD + n + z->ext + s;
  1844. zcount++;
  1845. /* Skip extended local header if there is one */
  1846. if ((flg & 8) != 0) {
  1847. /* Skip the compressed data if compressed size is unknown.
  1848. * For safety, we should use the central directory.
  1849. */
  1850. if (s == 0) {
  1851. for (;;) {
  1852. while ((m = getc(f)) != EOF && m != 0x50) ; /* 0x50 == 'P' */
  1853. b[0] = (char) m;
  1854. if (fread(b+1, 15, 1, f) != 1 || LG(b) == EXTLOCSIG)
  1855. break;
  1856. if (zfseeko(f, -15L, SEEK_CUR))
  1857. return ferror(f) ? ZE_READ : ZE_EOF;
  1858. }
  1859. # ifdef ZIP64_SUPPORT
  1860. if (zip64_entry) { /* from extra field */
  1861. /* all are 8 bytes */
  1862. s = LG(4 + ZIP64_EXTSIZ + b);
  1863. } else {
  1864. s = LG(4 + EXTSIZ + b);
  1865. }
  1866. # else
  1867. s = LG(4 + EXTSIZ + b);
  1868. # endif
  1869. p += s;
  1870. if ((uzoff_t) zftello(f) != p+16L) {
  1871. zipwarn("bad extended local header for ", z->zname);
  1872. return ZE_FORM;
  1873. }
  1874. } else {
  1875. /* compressed size non-zero, assume that it is valid: */
  1876. Assert(p == zftello(f), "bad compressed size with extended header");
  1877. if (zfseeko(f, p, SEEK_SET) || fread(b, 16, 1, f) != 1)
  1878. return ferror(f) ? ZE_READ : ZE_EOF;
  1879. if (LG(b) != EXTLOCSIG) {
  1880. zipwarn("extended local header not found for ", z->zname);
  1881. return ZE_FORM;
  1882. }
  1883. }
  1884. /* overwrite the unknown values of the local header: */
  1885. /* already in host format */
  1886. # ifdef ZIP64_SUPPORT
  1887. z->crc = LG(4 + ZIP64_EXTCRC + b);
  1888. z->siz = s;
  1889. z->len = LG(4 + ZIP64_EXTLEN + b);
  1890. # else
  1891. z->crc = LG(4 + EXTCRC + b);
  1892. z->siz = s;
  1893. z->len = LG(4 + EXTLEN + b);
  1894. # endif
  1895. p += 16L;
  1896. }
  1897. else if (fix > 1) {
  1898. /* Don't trust the compressed size */
  1899. for (;;) {
  1900. while ((m = getc(f)) != EOF && m != 0x50) p++; /* 0x50 == 'P' */
  1901. b[0] = (char) m;
  1902. if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == CENSIG)
  1903. break;
  1904. if (zfseeko(f, -3L, SEEK_CUR))
  1905. return ferror(f) ? ZE_READ : ZE_EOF;
  1906. p++;
  1907. }
  1908. s = p - (z->off + 4 + LOCHEAD + n + z->ext);
  1909. if (s != z->siz) {
  1910. fprintf(mesg, " compressed size %s, actual size %s for %s\n",
  1911. zip_fzofft(z->siz, NULL, "u"), zip_fzofft(s, NULL, "u"),
  1912. z->zname);
  1913. z->siz = s;
  1914. }
  1915. /* next LOCSIG already read at this point, don't read it again: */
  1916. continue;
  1917. }
  1918. /* Read next signature */
  1919. if (fread(b, 4, 1, f) != 1)
  1920. break;
  1921. }
  1922. s = p; /* save start of central */
  1923. if (LG(b) != CENSIG && noisy) {
  1924. fprintf(mesg, "zip warning: %s %s truncated.\n", zipfile,
  1925. fix > 1 ? "has been" : "would be");
  1926. if (fix == 1) {
  1927. fprintf(mesg,
  1928. "Retry with option -qF to truncate, with -FF to attempt full recovery\n");
  1929. ZIPERR(ZE_FORM, NULL);
  1930. }
  1931. }
  1932. cenbeg = s;
  1933. if (zipbeg && noisy)
  1934. fprintf(mesg, "%s: adjusting offsets for a preamble of %s bytes\n",
  1935. zipfile, zip_fzofft(zipbeg, NULL, "u"));
  1936. return ZE_OK;
  1937. } /* end of function scanzipf_fix() */
  1938. #endif /* never, scanzipf_fix() no longer used */
  1939. #endif /* !UTIL */
  1940. /*
  1941. * read_local
  1942. *
  1943. * Read the local header assumed at in_file file pointer.
  1944. * localz is the returned local header, z is the central directory entry.
  1945. *
  1946. * This is used by crypt.c.
  1947. *
  1948. * Return ZE code
  1949. */
  1950. int readlocal(localz, z)
  1951. struct zlist far **localz;
  1952. struct zlist far *z;
  1953. {
  1954. char buf[LOCHEAD + 1];
  1955. struct zlist far *locz;
  1956. #ifndef UTIL
  1957. ulg start_disk = 0;
  1958. uzoff_t start_offset = 0;
  1959. char *split_path;
  1960. start_disk = z->dsk;
  1961. start_offset = z->off;
  1962. /* don't assume reading the right disk */
  1963. if (start_disk != current_in_disk) {
  1964. if (in_file) {
  1965. fclose(in_file);
  1966. in_file = NULL;
  1967. }
  1968. }
  1969. current_in_disk = start_disk;
  1970. /* disks are archive.z01, archive.z02, ..., archive.zip */
  1971. split_path = get_in_split_path(in_path, current_in_disk);
  1972. if (in_file == NULL) {
  1973. while ((in_file = zfopen(split_path, FOPR)) == NULL) {
  1974. /* could not open split */
  1975. /* Ask for directory with split. Updates in_path */
  1976. if (ask_for_split_read_path(start_disk) != ZE_OK) {
  1977. return ZE_ABORT;
  1978. }
  1979. free(split_path);
  1980. split_path = get_in_split_path(in_path, start_disk);
  1981. }
  1982. }
  1983. #endif
  1984. /* For utilities assume archive is on one disk for now */
  1985. if (zfseeko(in_file, z->off, SEEK_SET) != 0) {
  1986. fclose(in_file);
  1987. in_file = NULL;
  1988. zipwarn("reading archive fseek: ", strerror(errno));
  1989. return ZE_READ;
  1990. }
  1991. if (!at_signature(in_file, "PK\03\04")) {
  1992. fclose(in_file);
  1993. in_file = NULL;
  1994. zipwarn("Did not find entry for ", z->iname);
  1995. return ZE_FORM;
  1996. }
  1997. /* read local header */
  1998. if (fread(buf, LOCHEAD, 1, in_file) != 1) {
  1999. int f = ferror(in_file);
  2000. zipwarn("reading local entry: ", strerror(errno));
  2001. fclose(in_file);
  2002. return f ? ZE_READ : ZE_EOF;
  2003. }
  2004. /* Local Header
  2005. local file header signature 4 bytes (0x04034b50)
  2006. version needed to extract 2 bytes
  2007. general purpose bit flag 2 bytes
  2008. compression method 2 bytes
  2009. last mod file time 2 bytes
  2010. last mod file date 2 bytes
  2011. crc-32 4 bytes
  2012. compressed size 4 bytes
  2013. uncompressed size 4 bytes
  2014. file name length 2 bytes
  2015. extra field length 2 bytes
  2016. file name (variable size)
  2017. extra field (variable size)
  2018. */
  2019. if ((locz = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
  2020. zipwarn("reading entry", "");
  2021. fclose(in_file);
  2022. return ZE_MEM;
  2023. }
  2024. locz->ver = SH(LOCVER + buf);
  2025. locz->lflg = SH(LOCFLG + buf);
  2026. locz->how = SH(LOCHOW + buf);
  2027. locz->tim = LG(LOCTIM + buf); /* time and date into one long */
  2028. locz->crc = LG(LOCCRC + buf);
  2029. locz->nam = SH(LOCNAM + buf);
  2030. locz->ext = SH(LOCEXT + buf);
  2031. /* Initialize all fields pointing to malloced data to NULL */
  2032. locz->zname = locz->name = locz->iname = locz->extra = NULL;
  2033. locz->oname = NULL;
  2034. #ifdef UNICODE_SUPPORT
  2035. locz->uname = NULL;
  2036. locz->zuname = NULL;
  2037. locz->ouname = NULL;
  2038. #endif
  2039. /* Read file name, extra field and comment field */
  2040. if ((locz->iname = malloc(locz->nam+1)) == NULL ||
  2041. (locz->ext && (locz->extra = malloc(locz->ext)) == NULL))
  2042. return ZE_MEM;
  2043. if (fread(locz->iname, locz->nam, 1, in_file) != 1 ||
  2044. (locz->ext && fread(locz->extra, locz->ext, 1, in_file) != 1))
  2045. return ferror(in_file) ? ZE_READ : ZE_EOF;
  2046. locz->iname[z->nam] = '\0'; /* terminate name */
  2047. #ifdef UNICODE_SUPPORT
  2048. if (unicode_mismatch != 3)
  2049. read_Unicode_Path_local_entry(locz);
  2050. #endif
  2051. #ifdef WIN32
  2052. {
  2053. /* translate archive name from OEM if came from OEM-charset environment */
  2054. unsigned hostver = (z->vem & 0xff);
  2055. Ext_ASCII_TO_Native(locz->iname, (z->vem >> 8), hostver,
  2056. ((z->atx & 0xffff0000L) != 0), TRUE);
  2057. }
  2058. #endif
  2059. if ((locz->name = malloc(locz->nam+1)) == NULL)
  2060. return ZE_MEM;
  2061. strcpy(locz->name, locz->iname);
  2062. #ifdef ZIP64_SUPPORT
  2063. zip64_entry = adjust_zip_local_entry(locz);
  2064. #endif
  2065. /* Compare localz to z */
  2066. if (locz->ver != z->ver) {
  2067. sprintf(errbuf, "Local Version Needed (%d) does not match CD (%d): ", locz->ver, z->ver);
  2068. zipwarn(errbuf, z->iname);
  2069. }
  2070. if (locz->lflg != z->flg) {
  2071. zipwarn("Local Entry Flag does not match CD: ", z->iname);
  2072. }
  2073. if (locz->crc != z->crc) {
  2074. zipwarn("Local Entry CRC does not match CD: ", z->iname);
  2075. }
  2076. /* as copying get uncompressed and compressed sizes from central directory */
  2077. locz->len = z->len;
  2078. locz->siz = z->siz;
  2079. *localz = locz;
  2080. return ZE_OK;
  2081. } /* end function readlocal() */
  2082. #if 0 /* following functions are not (no longer) used. */
  2083. /*
  2084. * scanzipf_reg starts searching for the End Signature at the end of the file
  2085. * The End Signature points to the Central Directory Signature which points
  2086. * to the Local Directory Signature
  2087. * XXX probably some more consistency checks are needed
  2088. */
  2089. local int scanzipf_reg(f)
  2090. FILE *f; /* zip file */
  2091. /*
  2092. The name of the zip file is pointed to by the global "zipfile". The globals
  2093. zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
  2094. Return an error code in the ZE_ class.
  2095. */
  2096. {
  2097. char b[CENHEAD]; /* buffer for central headers */
  2098. extent n; /* length of name */
  2099. struct zlist far * far *x; /* pointer last entry's link */
  2100. struct zlist far *z; /* current zip entry structure */
  2101. char *t; /* temporary pointer */
  2102. char far *u; /* temporary variable */
  2103. int found;
  2104. char *buf; /* temp buffer for reading zipfile */
  2105. # ifdef ZIP64_SUPPORT
  2106. ulg u4; /* unsigned 4 byte variable */
  2107. char bf[8];
  2108. uzoff_t u8; /* unsigned 8 byte variable */
  2109. uzoff_t censiz; /* size of central directory */
  2110. uzoff_t z64eocd; /* Zip64 End Of Central Directory record byte offset */
  2111. # else
  2112. ush flg; /* general purpose bit flag */
  2113. int m; /* mismatch flag */
  2114. # endif
  2115. zoff_t deltaoff = 0;
  2116. #ifndef ZIP64_SUPPORT
  2117. /* 2004-12-06 SMS.
  2118. * Check for too-big file before doing any serious work.
  2119. */
  2120. if (ffile_size( f) == EOF)
  2121. return ZE_ZIP64;
  2122. #endif /* ndef ZIP64_SUPPORT */
  2123. buf = malloc(4096 + 4);
  2124. if (buf == NULL)
  2125. return ZE_MEM;
  2126. #ifdef HANDLE_AMIGA_SFX
  2127. amiga_sfx_offset = (fread(buf, 1, 4, f) == 4 && LG(buf) == 0xF3030000);
  2128. /* == 1 if this file is an Amiga executable (presumably UnZipSFX) */
  2129. #endif
  2130. /* detect spanning signature */
  2131. zfseeko(f, 0, SEEK_SET);
  2132. read_split_archive = (fread(buf, 1, 4, f) == 4 && LG(buf) == 0x08074b50L);
  2133. found = 0;
  2134. t = &buf[4096];
  2135. t[1] = '\0';
  2136. t[2] = '\0';
  2137. t[3] = '\0';
  2138. /* back up as much as 4k from end */
  2139. /* zip64 support 08/31/2003 R.Nausedat */
  2140. if (zfseeko(f, -4096L, SEEK_END) == 0) {
  2141. zipbeg = (uzoff_t) (zftello(f) + 4096L);
  2142. /* back up 4k blocks and look for End Of CD signature */
  2143. while (!found && zipbeg >= 4096) {
  2144. zipbeg -= 4096L;
  2145. buf[4096] = t[1];
  2146. buf[4097] = t[2];
  2147. buf[4098] = t[3];
  2148. /*
  2149. * XXX error check ??
  2150. */
  2151. fread(buf, 1, 4096, f);
  2152. zfseeko(f, -8192L, SEEK_CUR);
  2153. t = &buf[4095];
  2154. /*
  2155. * XXX far pointer arithmetic in DOS
  2156. */
  2157. while (t >= buf) {
  2158. /* Check for ENDSIG ("PK\5\6" in ASCII) */
  2159. if (LG(t) == ENDSIG) {
  2160. found = 1;
  2161. /*
  2162. * XXX error check ??
  2163. * XXX far pointer arithmetic in DOS
  2164. */
  2165. zipbeg += (uzoff_t) (t - buf);
  2166. zfseeko(f, (zoff_t) zipbeg + 4L, SEEK_SET);
  2167. break;
  2168. }
  2169. --t;
  2170. }
  2171. }
  2172. }
  2173. else
  2174. /* file less than 4k bytes */
  2175. zipbeg = 4096L;
  2176. /*
  2177. * XXX warn: garbage at the end of the file ignored
  2178. */
  2179. if (!found && zipbeg > 0) {
  2180. size_t s;
  2181. zfseeko(f, 0L, SEEK_SET);
  2182. clearerr(f);
  2183. s = fread(buf, 1, (size_t) zipbeg, f);
  2184. /* add 0 bytes at end */
  2185. buf[s] = t[1];
  2186. buf[s + 1] = t[2];
  2187. buf[s + 2] = t[3];
  2188. t = &buf[s - 1];
  2189. /*
  2190. * XXX far pointer comparison in DOS
  2191. */
  2192. while (t >= buf) {
  2193. /* Check for ENDSIG ("PK\5\6" in ASCII) */
  2194. if (LG(t) == ENDSIG) {
  2195. found = 1;
  2196. /*
  2197. * XXX far pointer arithmetic in DOS
  2198. */
  2199. zipbeg = (ulg) (t - buf);
  2200. zfseeko(f, (zoff_t) zipbeg + 4L, SEEK_SET);
  2201. break;
  2202. }
  2203. --t;
  2204. }
  2205. }
  2206. free(buf);
  2207. if (!found) {
  2208. zipwarn("missing end signature--probably not a zip file (did you", "");
  2209. zipwarn("remember to use binary mode when you transferred it?)", "");
  2210. return ZE_FORM;
  2211. }
  2212. /*
  2213. * Check for a Zip64 EOCD Locator signature - 12/10/04 EG
  2214. */
  2215. #ifndef ZIP64_SUPPORT
  2216. /* If Zip64 not enabled check if archive being read is Zip64 */
  2217. /* back up 24 bytes (size of Z64 EOCDL and ENDSIG) */
  2218. if (zfseeko(f, -24, SEEK_CUR) != 0) {
  2219. perror("fseek");
  2220. return ZE_FORM; /* XXX */
  2221. }
  2222. /* read Z64 EOCDL if there */
  2223. if (fread(b, 20, 1, f) != 1) {
  2224. return ZE_READ;
  2225. }
  2226. /* first 4 bytes are the signature if there */
  2227. if (LG(b) == ZIP64_EOCDL_SIG) {
  2228. zipwarn("found Zip64 signature - this may be a Zip64 archive", "");
  2229. zipwarn("PKZIP 4.5 or later needed - set ZIP64_SUPPORT in Zip 3", "");
  2230. return ZE_ZIP64;
  2231. }
  2232. /* now should be back at the EOCD signature */
  2233. if (fread(b, 4, 1, f) != 1) {
  2234. zipwarn("unable to read after relative seek", "");
  2235. return ZE_READ;
  2236. }
  2237. if (LG(b) != ENDSIG) {
  2238. zipwarn("unable to relative seek in archive", "");
  2239. return ZE_FORM;
  2240. }
  2241. #if 0
  2242. if (fseek(f, -4, SEEK_CUR) != 0) {
  2243. perror("fseek");
  2244. return ZE_FORM; /* XXX */
  2245. }
  2246. #endif
  2247. #endif
  2248. /* Read end header */
  2249. if (fread(b, ENDHEAD, 1, f) != 1)
  2250. return ferror(f) ? ZE_READ : ZE_EOF;
  2251. if (SH(ENDDSK + b) || SH(ENDBEG + b) ||
  2252. SH(ENDSUB + b) != SH(ENDTOT + b))
  2253. zipwarn("multiple disk information ignored", "");
  2254. zcomlen = SH(ENDCOM + b);
  2255. if (zcomlen)
  2256. {
  2257. if ((zcomment = malloc(zcomlen)) == NULL)
  2258. return ZE_MEM;
  2259. if (fread(zcomment, zcomlen, 1, f) != 1)
  2260. {
  2261. free((zvoid *)zcomment);
  2262. zcomment = NULL;
  2263. return ferror(f) ? ZE_READ : ZE_EOF;
  2264. }
  2265. #ifdef EBCDIC
  2266. if (zcomment)
  2267. memtoebc(zcomment, zcomment, zcomlen);
  2268. #endif /* EBCDIC */
  2269. }
  2270. #ifdef ZIP64_SUPPORT
  2271. /* account for Zip64 EOCD Record and Zip64 EOCD Locator */
  2272. /* Z64 EOCDL should be just before EOCD (unless this is an empty archive) */
  2273. cenbeg = zipbeg - ZIP64_EOCDL_OFS_SIZE;
  2274. /* check for empty archive */
  2275. /* changed cenbeg to uzoff_t so instead of cenbeg >= 0 use new check - 5/23/05 EG */
  2276. if (zipbeg >= ZIP64_EOCDL_OFS_SIZE) {
  2277. /* look for signature */
  2278. if (zfseeko(f, cenbeg, SEEK_SET)) {
  2279. zipwarn("end of file seeking Z64EOCDL", "");
  2280. return ZE_FORM;
  2281. }
  2282. if (fread(bf, 4, 1, f) != 1) {
  2283. ziperr(ZE_FORM, "read error");
  2284. }
  2285. u4 = LG(bf);
  2286. if (u4 == ZIP64_EOCDL_SIG) {
  2287. /* found Zip64 EOCD Locator */
  2288. /* check for disk information */
  2289. zfseeko(f, cenbeg + ZIP64_EOCDL_OFS_TOTALDISKS, SEEK_SET);
  2290. if (fread(bf, 4, 1, f) != 1) {
  2291. ziperr(ZE_FORM, "read error");
  2292. }
  2293. u4 = LG(bf);
  2294. if (u4 != 1) {
  2295. ziperr(ZE_FORM, "multiple disk archives not yet supported");
  2296. }
  2297. /* look for Zip64 EOCD Record */
  2298. zfseeko(f, cenbeg + ZIP64_EOCDL_OFS_EOCD_START, SEEK_SET);
  2299. if (fread(bf, 8, 1, f) != 1) {
  2300. ziperr(ZE_FORM, "read error");
  2301. }
  2302. z64eocd = LLG(bf);
  2303. if (zfseeko(f, z64eocd, SEEK_SET)) {
  2304. ziperr(ZE_FORM, "error searching for Z64 EOCD Record");
  2305. }
  2306. if (fread(bf, 4, 1, f) != 1) {
  2307. ziperr(ZE_FORM, "read error");
  2308. }
  2309. u4 = LG(bf);
  2310. if (u4 != ZIP64_EOCD_SIG) {
  2311. ziperr(ZE_FORM, "Z64 EOCD not found but Z64 EOCD Locator exists");
  2312. }
  2313. /* get size of CD */
  2314. zfseeko(f, z64eocd + ZIP64_EOCD_OFS_SIZE, SEEK_SET);
  2315. if (fread(bf, 8, 1, f) != 1) {
  2316. ziperr(ZE_FORM, "read error");
  2317. }
  2318. censiz = LLG(bf);
  2319. /* get start of CD */
  2320. zfseeko(f, z64eocd + ZIP64_EOCD_OFS_CD_START, SEEK_SET);
  2321. if (fread(bf, 8, 1, f) == (size_t) -1) {
  2322. ziperr(ZE_FORM, "read error");
  2323. }
  2324. cenbeg = LLG(bf);
  2325. u8 = z64eocd - cenbeg;
  2326. deltaoff = adjust ? u8 - censiz : 0L;
  2327. } else {
  2328. /* assume no Locator and no Zip64 EOCD Record */
  2329. censiz = LG(ENDSIZ + b);
  2330. cenbeg = LG(b + ENDOFF);
  2331. u8 = zipbeg - censiz;
  2332. deltaoff = adjust ? u8 - censiz : 0L;
  2333. }
  2334. }
  2335. #else /* !ZIP64_SUPPORT */
  2336. /*
  2337. * XXX assumes central header immediately precedes end header
  2338. */
  2339. /* start of central directory */
  2340. cenbeg = zipbeg - LG(ENDSIZ + b);
  2341. /*
  2342. printf("start of central directory cenbeg %ld\n", cenbeg);
  2343. */
  2344. /* offset to first entry of archive */
  2345. deltaoff = adjust ? cenbeg - LG(b + ENDOFF) : 0L;
  2346. #endif /* ?ZIP64_SUPPORT */
  2347. if (zipbeg < ZIP64_EOCDL_OFS_SIZE) {
  2348. /* zip file seems empty */
  2349. return ZE_OK;
  2350. }
  2351. if (zfseeko(f, cenbeg, SEEK_SET) != 0) {
  2352. perror("fseek");
  2353. return ZE_FORM; /* XXX */
  2354. }
  2355. x = &zfiles; /* first link */
  2356. if (fread(b, 4, 1, f) != 1)
  2357. return ferror(f) ? ZE_READ : ZE_EOF;
  2358. while (LG(b) == CENSIG) {
  2359. /* Read central header. The portion of the central header that should
  2360. be in common with local header is read raw, for later comparison.
  2361. (this requires that the offset of ext in the zlist structure
  2362. be greater than or equal to LOCHEAD) */
  2363. if (fread(b, CENHEAD, 1, f) != 1)
  2364. return ferror(f) ? ZE_READ : ZE_EOF;
  2365. if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL)
  2366. return ZE_MEM;
  2367. z->vem = SH(CENVEM + b);
  2368. for (u = (char far *)(&(z->ver)), n = 0; n < (CENNAM-CENVER); n++)
  2369. u[n] = b[CENVER + n];
  2370. z->nam = SH(CENNAM + b); /* used before comparing cen vs. loc */
  2371. z->cext = SH(CENEXT + b); /* may be different from z->ext */
  2372. z->com = SH(CENCOM + b);
  2373. z->dsk = SH(CENDSK + b);
  2374. z->att = SH(CENATT + b);
  2375. z->atx = LG(CENATX + b);
  2376. z->off = LG(CENOFF + b) + deltaoff;
  2377. z->dosflag = (z->vem & 0xff00) == 0;
  2378. /* Initialize all fields pointing to malloced data to NULL */
  2379. z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
  2380. z->oname = NULL;
  2381. #ifdef UNICODE_SUPPORT
  2382. z->uname = NULL; /* UTF-8 path */
  2383. z->zuname = NULL; /* Escaped local version of uname */
  2384. z->ouname = NULL; /* Display version of zuname */
  2385. #endif
  2386. /* Link into list */
  2387. *x = z;
  2388. z->nxt = NULL;
  2389. x = &z->nxt;
  2390. /* Read file name, extra field and comment field */
  2391. if (z->nam == 0)
  2392. {
  2393. sprintf(errbuf, "%lu", (ulg)zcount + 1);
  2394. zipwarn("zero-length name for entry #", errbuf);
  2395. #ifndef DEBUG
  2396. farfree((zvoid far *)z);
  2397. return ZE_FORM;
  2398. #endif
  2399. }
  2400. if ((z->iname = malloc(z->nam+1)) == NULL ||
  2401. (z->cext && (z->cextra = malloc(z->cext)) == NULL) ||
  2402. (z->com && (z->comment = malloc(z->com)) == NULL))
  2403. return ZE_MEM;
  2404. if (fread(z->iname, z->nam, 1, f) != 1 ||
  2405. (z->cext && fread(z->cextra, z->cext, 1, f) != 1) ||
  2406. (z->com && fread(z->comment, z->com, 1, f) != 1))
  2407. return ferror(f) ? ZE_READ : ZE_EOF;
  2408. z->iname[z->nam] = '\0'; /* terminate name */
  2409. #ifdef EBCDIC
  2410. if (z->com)
  2411. memtoebc(z->comment, z->comment, z->com);
  2412. #endif /* EBCDIC */
  2413. #ifdef ZIP64_SUPPORT
  2414. /* zip64 support 08/31/2003 R.Nausedat */
  2415. /* here, we have to read the len, siz etc values from the CD */
  2416. /* entry as we might have to adjust them regarding their */
  2417. /* correspronding zip64 extra fields. */
  2418. /* also, we cannot compare the values from the CD entries with */
  2419. /* the values from the LH as they might be different. */
  2420. z->len = LG(CENLEN + b);
  2421. z->siz = LG(CENSIZ + b);
  2422. z->crc = LG(CENCRC + b);
  2423. z->tim = LG(CENTIM + b); /* time and date into one long */
  2424. z->how = SH(CENHOW + b);
  2425. z->flg = SH(CENFLG + b);
  2426. z->ver = SH(CENVER + b);
  2427. /* adjust/update siz,len and off (to come: dsk) entries */
  2428. /* PKZIP does not care of the version set in a CDH: if */
  2429. /* there is a zip64 extra field assigned to a CDH PKZIP */
  2430. /* uses it, we should do so, too. */
  2431. adjust_zip_central_entry(z);
  2432. #endif /* ZIP64_SUPPORT */
  2433. /* Update zipbeg offset, prepare for next header */
  2434. if (z->off < zipbeg)
  2435. zipbeg = z->off;
  2436. zcount++;
  2437. /* Read next signature */
  2438. if (fread(b, 4, 1, f) != 1)
  2439. return ferror(f) ? ZE_READ : ZE_EOF;
  2440. }
  2441. /* Point to start of header list and read local headers */
  2442. z = zfiles;
  2443. while (z != NULL) {
  2444. /* Read next signature */
  2445. if (zfseeko(f, z->off, SEEK_SET) != 0 || fread(b, 4, 1, f) != 1)
  2446. return ferror(f) ? ZE_READ : ZE_EOF;
  2447. if (LG(b) == LOCSIG) {
  2448. if (fread(b, LOCHEAD, 1, f) != 1)
  2449. return ferror(f) ? ZE_READ : ZE_EOF;
  2450. z->lflg = SH(LOCFLG + b);
  2451. n = SH(LOCNAM + b);
  2452. z->ext = SH(LOCEXT + b);
  2453. /* Compare name and extra fields */
  2454. if (n != z->nam)
  2455. {
  2456. #ifdef EBCDIC
  2457. strtoebc(z->iname, z->iname);
  2458. #endif
  2459. zipwarn("name lengths in local and central differ for ", z->iname);
  2460. return ZE_FORM;
  2461. }
  2462. if ((t = malloc(z->nam)) == NULL)
  2463. return ZE_MEM;
  2464. if (fread(t, z->nam, 1, f) != 1)
  2465. {
  2466. free((zvoid *)t);
  2467. return ferror(f) ? ZE_READ : ZE_EOF;
  2468. }
  2469. if (memcmp(t, z->iname, z->nam))
  2470. {
  2471. free((zvoid *)t);
  2472. #ifdef EBCDIC
  2473. strtoebc(z->iname, z->iname);
  2474. #endif
  2475. zipwarn("names in local and central differ for ", z->iname);
  2476. return ZE_FORM;
  2477. }
  2478. free((zvoid *)t);
  2479. if (z->ext)
  2480. {
  2481. if ((z->extra = malloc(z->ext)) == NULL)
  2482. return ZE_MEM;
  2483. if (fread(z->extra, z->ext, 1, f) != 1)
  2484. {
  2485. free((zvoid *)(z->extra));
  2486. return ferror(f) ? ZE_READ : ZE_EOF;
  2487. }
  2488. if (z->ext == z->cext && memcmp(z->extra, z->cextra, z->ext) == 0)
  2489. {
  2490. free((zvoid *)(z->extra));
  2491. z->extra = z->cextra;
  2492. }
  2493. }
  2494. #ifdef ZIP64_SUPPORT /* zip64 support 09/02/2003 R.Nausedat */
  2495. /*
  2496. for now the below is left out if ZIP64_SUPPORT is defined as the fields
  2497. len, siz and off in struct zlist are type of int64 if ZIP64_SUPPORT
  2498. is defined. In either way, the values read from the central directory
  2499. should be valid. comments are welcome
  2500. */
  2501. #else /* !ZIP64_SUPPORT */
  2502. /* Check extended local header if there is one */
  2503. /* bit 3 */
  2504. if ((z->lflg & 8) != 0)
  2505. {
  2506. char buf2[16];
  2507. ulg s; /* size of compressed data */
  2508. s = LG(LOCSIZ + b);
  2509. if (s == 0)
  2510. s = LG((CENSIZ-CENVER) + (char far *)(&(z->ver)));
  2511. if (zfseeko(f, (z->off + (4+LOCHEAD) + z->nam + z->ext + s), SEEK_SET)
  2512. || (fread(buf2, 16, 1, f) != 1))
  2513. return ferror(f) ? ZE_READ : ZE_EOF;
  2514. if (LG(buf2) != EXTLOCSIG)
  2515. {
  2516. # ifdef EBCDIC
  2517. strtoebc(z->iname, z->iname);
  2518. # endif
  2519. zipwarn("extended local header not found for ", z->iname);
  2520. return ZE_FORM;
  2521. }
  2522. /* overwrite the unknown values of the local header: */
  2523. for (n = 0; n < 12; n++)
  2524. b[LOCCRC+n] = buf2[4+n];
  2525. }
  2526. /* Compare local header with that part of central header (except
  2527. for the reserved bits in the general purpose flags and except
  2528. for the already checked entry name length */
  2529. /* If I have read this right we are stepping through the z struct
  2530. here as a byte array. Need to fix this. 5/25/2005 EG */
  2531. u = (char far *)(&(z->ver));
  2532. flg = SH((CENFLG-CENVER) + u); /* Save central flags word */
  2533. u[CENFLG-CENVER+1] &= 0x1f; /* Mask reserved flag bits */
  2534. b[LOCFLG+1] &= 0x1f;
  2535. for (m = 0, n = 0; n < LOCNAM; n++) {
  2536. if (b[n] != u[n])
  2537. {
  2538. if (!m)
  2539. {
  2540. zipwarn("local and central headers differ for ", z->iname);
  2541. m = 1;
  2542. }
  2543. if (noisy)
  2544. {
  2545. sprintf(errbuf, " offset %u--local = %02x, central = %02x",
  2546. (unsigned)n, (uch)b[n], (uch)u[n]);
  2547. zipwarn(errbuf, "");
  2548. }
  2549. }
  2550. }
  2551. if (m && !adjust)
  2552. return ZE_FORM;
  2553. /* Complete the setup of the zlist entry by translating the remaining
  2554. * central header fields in memory, starting with the fields with
  2555. * highest offset. This order of the conversion commands takes into
  2556. * account potential buffer overlaps caused by structure padding.
  2557. */
  2558. z->len = LG((CENLEN-CENVER) + u);
  2559. z->siz = LG((CENSIZ-CENVER) + u);
  2560. z->crc = LG((CENCRC-CENVER) + u);
  2561. z->tim = LG((CENTIM-CENVER) + u); /* time and date into one long */
  2562. z->how = SH((CENHOW-CENVER) + u);
  2563. z->flg = flg; /* may be different from z->lflg */
  2564. z->ver = SH((CENVER-CENVER) + u);
  2565. #endif /* ?ZIP64_SUPPORT */
  2566. /* Clear actions */
  2567. z->mark = 0;
  2568. z->trash = 0;
  2569. #ifdef UNICODE_SUPPORT
  2570. if (unicode_mismatch != 3) {
  2571. read_Unicode_Path_entry(z);
  2572. if (z->uname) {
  2573. /* match based on converted Unicode name */
  2574. z->name = utf8_to_local_string(z->uname);
  2575. # ifdef EBCDIC
  2576. /* z->zname is used for printing and must be coded in native charset */
  2577. strtoebc(z->zname, z->name);
  2578. # else
  2579. if ((z->zname = malloc(strlen(z->name) + 1)) == NULL) {
  2580. ZIPERR(ZE_MEM, "scanzipf_reg");
  2581. }
  2582. strcpy(z->zname, z->name);
  2583. # endif
  2584. z->oname = local_to_display_string(z->zname);
  2585. } else {
  2586. /* no UTF-8 path */
  2587. if ((z->name = malloc(strlen(z->iname) + 1)) == NULL) {
  2588. ZIPERR(ZE_MEM, "scanzipf_reg");
  2589. }
  2590. strcpy(z->name, z->iname);
  2591. if ((z->zname = malloc(strlen(z->iname) + 1)) == NULL) {
  2592. ZIPERR(ZE_MEM, "scanzipf_reg");
  2593. }
  2594. strcpy(z->zname, z->iname);
  2595. z->oname = local_to_display_string(z->iname);
  2596. }
  2597. }
  2598. #else /* !UNICODE_SUPPORT */
  2599. # ifdef UTIL
  2600. /* We only need z->iname in the utils */
  2601. z->name = z->iname;
  2602. # ifdef EBCDIC
  2603. /* z->zname is used for printing and must be coded in native charset */
  2604. if ((z->zname = malloc(z->nam+1)) == NULL)
  2605. return ZE_MEM;
  2606. strtoebc(z->zname, z->iname);
  2607. # else
  2608. z->zname = z->iname;
  2609. # endif
  2610. # else /* !UTIL */
  2611. z->zname = in2ex(z->iname); /* convert to external name */
  2612. if (z->zname == NULL)
  2613. return ZE_MEM;
  2614. z->name = z->zname;
  2615. # endif /* ?UTIL */
  2616. if ((z->oname = malloc(strlen(z->zname) + 1)) == NULL) {
  2617. ZIPERR(ZE_MEM, "scanzipf_reg");
  2618. }
  2619. strcpy(z->oname, z->zname);
  2620. #endif /* ?UNICODE_SUPPORT */
  2621. }
  2622. else {
  2623. #ifdef EBCDIC
  2624. strtoebc(z->iname, z->iname);
  2625. #endif
  2626. zipwarn("local header not found for ", z->iname);
  2627. return ZE_FORM;
  2628. }
  2629. #ifndef UTIL
  2630. if (verbose && fix == 0)
  2631. zipoddities(z);
  2632. #endif
  2633. z = z->nxt;
  2634. }
  2635. if (zipbeg && noisy)
  2636. fprintf(mesg, "%s: %s a preamble of %s bytes\n",
  2637. zipfile, adjust ? "adjusting offsets for" : "found",
  2638. zip_fzofft(zipbeg, NULL, "u"));
  2639. #ifdef HANDLE_AMIGA_SFX
  2640. if (zipbeg < 12 || (zipbeg & 3) != 0 /* must be longword aligned */)
  2641. amiga_sfx_offset = 0;
  2642. else if (amiga_sfx_offset) {
  2643. char buf2[16];
  2644. if (!fseek(f, zipbeg - 12, SEEK_SET) && fread(buf2, 12, 1, f) == 1) {
  2645. if (LG(buf2 + 4) == 0xF1030000 /* 1009 in Motorola byte order */)
  2646. /* could also check if LG(buf2) == 0xF2030000... no for now */
  2647. amiga_sfx_offset = zipbeg - 4;
  2648. else
  2649. amiga_sfx_offset = 0L;
  2650. }
  2651. }
  2652. #endif /* HANDLE_AMIGA_SFX */
  2653. return ZE_OK;
  2654. } /* end of function scanzipf_reg() */
  2655. #endif /* never */
  2656. /* find_next_signature
  2657. *
  2658. * Scan the file forward and look for the next PK signature.
  2659. *
  2660. * Return 1 if find one and leave file pointer pointing to next char
  2661. * after signature and set sigbuf to signature.
  2662. *
  2663. * Return 0 if not. Will be at EOF on return unless error.
  2664. *
  2665. */
  2666. local char sigbuf[4]; /* signature found */
  2667. #if 0 /* currently unused */
  2668. /* copy signature */
  2669. char *copy_sig(copyto, copyfrom)
  2670. char *copyto;
  2671. char *copyfrom;
  2672. {
  2673. int i;
  2674. for (i = 0; i < 4; i++) {
  2675. copyto[i] = copyfrom[i];
  2676. }
  2677. return copyto;
  2678. }
  2679. #endif /* currently unused */
  2680. local int find_next_signature(f)
  2681. FILE *f;
  2682. {
  2683. int m;
  2684. /*
  2685. zoff_t here;
  2686. */
  2687. /* look for P K ? ? signature */
  2688. m = getc(f);
  2689. /*
  2690. here = zftello(f);
  2691. */
  2692. while (m != EOF)
  2693. {
  2694. if (m == 0x50 /*'P' except EBCDIC*/) {
  2695. /* found a P */
  2696. sigbuf[0] = (char) m;
  2697. if ((m = getc(f)) == EOF)
  2698. break;
  2699. if (m != 0x4b /*'K' except EBCDIC*/) {
  2700. /* not a signature */
  2701. ungetc(m, f);
  2702. } else {
  2703. /* found P K */
  2704. sigbuf[1] = (char) m;
  2705. if ((m = getc(f)) == EOF)
  2706. break;
  2707. if (m == 0x50 /*'P' except EBCDIC*/) {
  2708. /* not a signature but maybe start of new one */
  2709. ungetc(m, f);
  2710. continue;
  2711. } else if (m >= 16) {
  2712. /* last 2 chars expect < 16 for signature */
  2713. continue;
  2714. }
  2715. sigbuf[2] = (char) m;
  2716. if ((m = getc(f)) == EOF)
  2717. break;
  2718. if (m == 0x50 /*'P' except EBCDIC*/) {
  2719. /* not a signature but maybe start of new one */
  2720. ungetc(m, f);
  2721. continue;
  2722. } else if (m >= 16) {
  2723. /* last 2 chars expect < 16 */
  2724. continue;
  2725. }
  2726. sigbuf[3] = (char) m;
  2727. /* found possible signature */
  2728. return 1;
  2729. }
  2730. }
  2731. m = getc(f);
  2732. }
  2733. if (ferror(f)) {
  2734. return 0;
  2735. }
  2736. /* found nothing */
  2737. return 0;
  2738. }
  2739. /* find_signature
  2740. *
  2741. * Find signature.
  2742. *
  2743. * Return 1 if found and leave file pointing to next character
  2744. * after signature. Set sigbuf with signature.
  2745. *
  2746. * Return 0 if not found.
  2747. */
  2748. local int find_signature(f, signature)
  2749. FILE *f;
  2750. ZCONST char *signature;
  2751. {
  2752. int i;
  2753. char sig[4];
  2754. /*
  2755. zoff_t here = zftello(f);
  2756. */
  2757. for (i = 0; i < 4; i++)
  2758. sig[i] = signature[i];
  2759. /* for EBCDIC */
  2760. if (sig[0] == 'P')
  2761. sig[0] = 0x50;
  2762. if (sig[1] == 'K')
  2763. sig[1] = 0x4b;
  2764. while (!feof(f)) {
  2765. if (!find_next_signature(f)) {
  2766. return 0;
  2767. } else {
  2768. for (i = 0; i < 4; i++) {
  2769. if (sig[i] != sigbuf[i]) {
  2770. /* not a match */
  2771. break;
  2772. }
  2773. }
  2774. if (i == 4) {
  2775. /* found it */
  2776. return 1;
  2777. }
  2778. }
  2779. }
  2780. return 0;
  2781. }
  2782. /* is_signature
  2783. *
  2784. * Compare signatures
  2785. *
  2786. * Return 1 if the signatures match.
  2787. */
  2788. local int is_signature(sig1, sig2)
  2789. ZCONST char *sig1;
  2790. ZCONST char *sig2;
  2791. {
  2792. int i;
  2793. char tsig1[4];
  2794. char tsig2[4];
  2795. for (i = 0; i < 4; i++) {
  2796. tsig1[i] = sig1[i];
  2797. tsig2[i] = sig2[i];
  2798. }
  2799. /* for EBCDIC */
  2800. if (tsig1[0] == 'P')
  2801. tsig1[0] = 0x50;
  2802. if (tsig1[1] == 'K')
  2803. tsig1[1] = 0x4b;
  2804. if (tsig2[0] == 'P')
  2805. tsig2[0] = 0x50;
  2806. if (tsig2[1] == 'K')
  2807. tsig2[1] = 0x4b;
  2808. for (i = 0; i < 4; i++) {
  2809. if (tsig1[i] != tsig2[i]) {
  2810. /* not a match */
  2811. break;
  2812. }
  2813. }
  2814. if (i == 4) {
  2815. /* found it */
  2816. return 1;
  2817. }
  2818. return 0;
  2819. }
  2820. /* at_signature
  2821. *
  2822. * Is at signature in file
  2823. *
  2824. * Return 1 if at the signature and leave file pointing to next character
  2825. * after signature.
  2826. *
  2827. * Return 0 if not.
  2828. */
  2829. local int at_signature(f, signature)
  2830. FILE *f;
  2831. ZCONST char *signature;
  2832. {
  2833. int i;
  2834. extent m;
  2835. char sig[4];
  2836. char b[4];
  2837. for (i = 0; i < 4; i++)
  2838. sig[i] = signature[i];
  2839. /* for EBCDIC */
  2840. if (sig[0] == 'P')
  2841. sig[0] = 0x50;
  2842. if (sig[1] == 'K')
  2843. sig[1] = 0x4b;
  2844. m = fread(b, 1, 4, f);
  2845. if (m != 4) {
  2846. return 0;
  2847. } else {
  2848. for (i = 0; i < 4; i++) {
  2849. if (sig[i] != b[i]) {
  2850. /* not a match */
  2851. break;
  2852. }
  2853. }
  2854. if (i == 4) {
  2855. /* found it */
  2856. return 1;
  2857. }
  2858. }
  2859. return 0;
  2860. }
  2861. #ifndef UTIL
  2862. local int scanzipf_fixnew()
  2863. /*
  2864. Scan an assumed broke archive from the beginning, salvaging what can.
  2865. Generally scanzipf_regnew() is used for reading archives normally and
  2866. for fixing archives with a readable central directory using -F. This
  2867. scan is used by -FF and is for an archive that is unreadable by
  2868. scanzipf_regnew().
  2869. Start with the first file of the archive, either .z01 or .zip, and
  2870. look for local entries. Read local entries found and create zlist
  2871. entries for them. If we find central directory entries, read them
  2872. and update the zlist created while reading local entries.
  2873. The input path for the .zip file is in in_path. If this is a multiple disk
  2874. archive get the paths for splits from in_path as we go. If a split is not in
  2875. the same directory as the last split we ask the user where it is and update
  2876. in_path.
  2877. */
  2878. /*
  2879. This is old:
  2880. The name of the zip file is pointed to by the global "zipfile". The globals
  2881. zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
  2882. Return an error code in the ZE_ class.
  2883. */
  2884. {
  2885. /* This function only reads the standard End-of-CentralDir record and the
  2886. standard CentralDir-Entry records directly. To conserve stack space,
  2887. only a buffer of minimal size is declared.
  2888. */
  2889. # if CENHEAD > ENDHEAD
  2890. # define FIXSCAN_BUFSIZE CENHEAD
  2891. # else
  2892. # define FIXSCAN_BUFSIZE ENDHEAD
  2893. # endif
  2894. char scbuf[FIXSCAN_BUFSIZE]; /* buffer big enough for headers */
  2895. char *split_path;
  2896. ulg eocdr_disk;
  2897. uzoff_t eocdr_offset;
  2898. uzoff_t current_offset = 0; /* offset before */
  2899. uzoff_t offset = 0; /* location after return from seek */
  2900. int skip_disk = 0; /* 1 if user asks to skip current disk */
  2901. int skipped_disk = 0; /* 1 if skipped start disk and start offset is useless */
  2902. int r = 0; /* zipcopy return */
  2903. uzoff_t s; /* size of data, start of central */
  2904. struct zlist far * far *x; /* pointer last entry's link */
  2905. struct zlist far *z; /* current zip entry structure */
  2906. int plen;
  2907. char *in_path_ext;
  2908. int in_central_directory = 0; /* found a central directory record */
  2909. struct zlist far *cz;
  2910. uzoff_t cd_total_entries = 0; /* number of entries according to EOCDR */
  2911. ulg in_cd_start_disk; /* central directory start disk */
  2912. uzoff_t in_cd_start_offset; /* offset of start of cd on cd start disk */
  2913. total_disks = 1000000;
  2914. /* open the zipfile */
  2915. /* This must be .zip file, even if it doesn't exist */
  2916. /* see if zipfile name ends in .zip */
  2917. plen = strlen(in_path);
  2918. #ifdef VMS
  2919. /* On VMS, adjust plen (and in_path_ext) to avoid the file version. */
  2920. plen -= strlen(vms_file_version(in_path));
  2921. #endif /* def VMS */
  2922. in_path_ext = zipfile + plen - 4;
  2923. if (plen >= 4 &&
  2924. in_path_ext[0] == '.' &&
  2925. toupper(in_path_ext[1]) == 'Z' &&
  2926. in_path_ext[2] >= '0' && in_path_ext[2] <= '9' &&
  2927. in_path_ext[3] >= '0' && in_path_ext[3] <= '9' &&
  2928. (plen == 4 || (in_path_ext[4] >= '0' && in_path_ext[4] <= '9'))) {
  2929. /* This may be a split but not the end split */
  2930. strcpy(errbuf, "if archive to fix is split archive, need to provide\n");
  2931. strcat(errbuf, " path of the last split with .zip extension,\n");
  2932. strcat(errbuf, " even if it doesn't exist (zip will ask for splits)");
  2933. zipwarn(errbuf, "");
  2934. return ZE_FORM;
  2935. }
  2936. if ((in_file = zfopen(in_path, FOPR)) == NULL) {
  2937. zipwarn("could not open input archive: ", in_path);
  2938. }
  2939. else
  2940. {
  2941. #ifndef ZIP64_SUPPORT
  2942. /* 2004-12-06 SMS.
  2943. * Check for too-big file before doing any serious work.
  2944. */
  2945. if (ffile_size( in_file) == EOF) {
  2946. fclose(in_file);
  2947. in_file = NULL;
  2948. zipwarn("input file requires Zip64 support: ", in_path);
  2949. return ZE_ZIP64;
  2950. }
  2951. #endif /* ndef ZIP64_SUPPORT */
  2952. /* look for End Of Central Directory Record */
  2953. /* back up 64k (the max size of the EOCDR) from end */
  2954. if (zfseeko(in_file, -0x40000L, SEEK_END) != 0) {
  2955. /* assume file is less than 64 KB so backup to beginning */
  2956. if (zfseeko(in_file, 0L, SEEK_SET) != 0) {
  2957. fclose(in_file);
  2958. in_file = NULL;
  2959. zipwarn("unable to seek in input file ", in_path);
  2960. return ZE_READ;
  2961. }
  2962. }
  2963. /* find EOCD Record signature */
  2964. if (!find_signature(in_file, "PK\05\06")) {
  2965. /* No End Of Central Directory Record */
  2966. strcpy(errbuf, "Missing end (EOCDR) signature - either this archive\n");
  2967. strcat(errbuf, " is not readable or the end is damaged");
  2968. zipwarn(errbuf, "");
  2969. }
  2970. else
  2971. {
  2972. /* at start of data after EOCDR signature */
  2973. eocdr_offset = (uzoff_t) zftello(in_file);
  2974. /* OK, it is possible this is not the last EOCDR signature (might be
  2975. EOCDR signature from a stored archive in the last 64 KB) and so not
  2976. the one we want.
  2977. The below assumes the signature does not appear in the assumed
  2978. ASCII text .ZIP file comment. Even if something like UTF-8
  2979. is stored in the comment, it's unlikely the binary \05 and \06
  2980. will be in the comment text.
  2981. */
  2982. while (find_signature(in_file, "PK\05\06")) {
  2983. eocdr_offset = (uzoff_t) zftello(in_file);
  2984. }
  2985. /* found EOCDR */
  2986. /* format is
  2987. end of central dir signature 4 bytes (0x06054b50)
  2988. number of this disk 2 bytes
  2989. number of the disk with the
  2990. start of the central directory 2 bytes
  2991. total number of entries in the
  2992. central directory on this disk 2 bytes
  2993. total number of entries in
  2994. the central directory 2 bytes
  2995. size of the central directory 4 bytes
  2996. offset of start of central
  2997. directory with respect to
  2998. the starting disk number 4 bytes
  2999. .ZIP file comment length 2 bytes
  3000. .ZIP file comment (variable size)
  3001. */
  3002. if (zfseeko(in_file, eocdr_offset, SEEK_SET) != 0) {
  3003. fclose(in_file);
  3004. in_file = NULL;
  3005. zipwarn("unable to seek in input file ", in_path);
  3006. return ZE_READ;
  3007. }
  3008. /* read the EOCDR */
  3009. s = fread(scbuf, 1, ENDHEAD, in_file);
  3010. /* make sure we read enough bytes */
  3011. if (s < ENDHEAD) {
  3012. sprintf(errbuf, "End record (EOCDR) only %s bytes - assume truncated",
  3013. zip_fzofft(s, NULL, "u"));
  3014. zipwarn(errbuf, "");
  3015. }
  3016. else
  3017. {
  3018. /* the first field should be number of this (the last) disk */
  3019. eocdr_disk = (ulg)SH(scbuf);
  3020. total_disks = eocdr_disk + 1;
  3021. /* assume this is this disk - if Zip64 it may not be as the
  3022. disk number may be bigger than this field can hold
  3023. */
  3024. current_in_disk = total_disks - 1;
  3025. /* Central Directory disk, offset, and total entries */
  3026. in_cd_start_disk = (ulg)SH(scbuf + 2);
  3027. in_cd_start_offset = (uzoff_t)LG(scbuf + 12);
  3028. cd_total_entries = (uzoff_t)SH(scbuf + 6);
  3029. /* the in_cd_start_disk should always be less than the total_disks,
  3030. unless the -1 flags are being used */
  3031. if (total_disks < 0x10000 && in_cd_start_disk > total_disks) {
  3032. zipwarn("End record (EOCDR) has bad disk numbers - ignoring EOCDR", "");
  3033. total_disks = 0;
  3034. }
  3035. else
  3036. {
  3037. /* length of zipfile comment */
  3038. zcomlen = SH(scbuf + ENDCOM);
  3039. if (zcomlen)
  3040. {
  3041. if ((zcomment = malloc(zcomlen + 1)) == NULL)
  3042. return ZE_MEM;
  3043. if (fread(zcomment, zcomlen, 1, in_file) != 1)
  3044. {
  3045. free((zvoid *)zcomment);
  3046. zcomment = NULL;
  3047. zipwarn("zipfile comment truncated - ignoring", "");
  3048. } else {
  3049. zcomment[zcomlen] = '\0';
  3050. }
  3051. #ifdef EBCDIC
  3052. if (zcomment)
  3053. memtoebc(zcomment, zcomment, zcomlen);
  3054. #endif /* EBCDIC */
  3055. }
  3056. }
  3057. if (total_disks != 1)
  3058. sprintf(errbuf, " Found end record (EOCDR) - says expect %lu splits", total_disks);
  3059. else
  3060. sprintf(errbuf, " Found end record (EOCDR) - says expect single disk archive");
  3061. zipmessage(errbuf, "");
  3062. if (zcomment)
  3063. zipmessage(" Found archive comment", "");
  3064. } /* good EOCDR */
  3065. } /* found EOCDR */
  3066. /* if total disks is other than 1 then this is not start disk */
  3067. /* if the EOCDR is bad, total_disks is 0 */
  3068. /* if total_disks = 0, then guess if this is a single-disk archive
  3069. by seeing if starts with local header */
  3070. if (total_disks == 0) {
  3071. int issig;
  3072. /* seek to top */
  3073. if (zfseeko(in_file, 0, SEEK_SET) != 0) {
  3074. fclose(in_file);
  3075. in_file = NULL;
  3076. zipwarn("unable to seek in input file ", in_path);
  3077. return ZE_READ;
  3078. }
  3079. /* get next signature */
  3080. issig = find_next_signature(in_file);
  3081. if (issig) {
  3082. current_in_offset = zftello(in_file);
  3083. if (current_in_offset == 4 && is_signature(sigbuf, "PK\03\03")) {
  3084. /* could be multi-disk aborted signature at top */
  3085. /* skip */
  3086. issig = find_next_signature(in_file);
  3087. } else if (current_in_offset <= 4 && is_signature(sigbuf, "PK\03\03")) {
  3088. /* multi-disk spanning signature */
  3089. total_disks = 99999;
  3090. }
  3091. }
  3092. if (issig && total_disks == 0) {
  3093. current_in_offset = zftello(in_file);
  3094. if (current_in_offset == 8 && is_signature(sigbuf, "PK\03\04")) {
  3095. /* Local Header Record at top */
  3096. printf("Is this a single-disk archive? (y/n): ");
  3097. fflush(stdout);
  3098. if (fgets(errbuf, 100, stdin) != NULL) {
  3099. if (errbuf[0] == 'y' || errbuf[0] == 'Y') {
  3100. total_disks = 1;
  3101. zipmessage(" Assuming single-disk archive", "");
  3102. }
  3103. }
  3104. }
  3105. }
  3106. }
  3107. if (!noisy)
  3108. /* if quiet assume single-disk archive */
  3109. total_disks = 1;
  3110. if (total_disks == 1000000) {
  3111. /* still don't know, so ask */
  3112. printf("Is this a single-disk archive? (y/n): ");
  3113. fflush(stdout);
  3114. if (fgets(errbuf, 100, stdin) != NULL) {
  3115. if (errbuf[0] == 'y' || errbuf[0] == 'Y') {
  3116. total_disks = 1;
  3117. zipmessage(" Assuming single-disk archive", "");
  3118. }
  3119. }
  3120. }
  3121. if (total_disks == 1000000) {
  3122. /* assume max */
  3123. total_disks = 100000;
  3124. }
  3125. } /* .zip file exists */
  3126. /* Skip reading the Zip64 EOCDL, Zip64 EOCDR, or central directory */
  3127. /* Now read the archive starting with first disk. Find local headers,
  3128. create entry in zlist, then copy entry to new archive */
  3129. /* Multi-volume file names end in .z01, .z02, ..., .z10, .zip for 11 disk archive */
  3130. /* Unless quiet, always close the in_path disk and ask user for first disk,
  3131. unless there is an End Of Central Directory record and that says there is
  3132. only one disk.
  3133. If quiet, assume the file pointed to is a single file archive to fix. */
  3134. if (noisy && in_file) {
  3135. fclose(in_file);
  3136. in_file = NULL;
  3137. }
  3138. /* Read the archive disks - no idea how many disks there are
  3139. since we can't trust the EOCDR and other end records
  3140. */
  3141. zipmessage("Scanning for entries...", "");
  3142. for (current_in_disk = 0; current_in_disk < total_disks; current_in_disk++) {
  3143. /* get the path for this disk */
  3144. split_path = get_in_split_path(in_path, current_in_disk);
  3145. /* if in_file is not NULL then in_file is already open */
  3146. if (in_file == NULL) {
  3147. /* open the split */
  3148. while ((in_file = zfopen(split_path, FOPR)) == NULL) {
  3149. int result;
  3150. /* could not open split */
  3151. /* Ask for directory with split. Updates global variable in_path */
  3152. result = ask_for_split_read_path(current_in_disk);
  3153. if (result == ZE_ABORT) {
  3154. zipwarn("could not find split: ", split_path);
  3155. return ZE_ABORT;
  3156. } else if (result == ZE_EOF) {
  3157. zipmessage_nl("", 1);
  3158. zipwarn("user ended reading - closing archive", "");
  3159. return ZE_EOF;
  3160. } else if (result == ZE_FORM) {
  3161. /* user asked to skip this disk */
  3162. zipmessage_nl("", 1);
  3163. sprintf(errbuf, "skipping disk %lu ...\n", current_in_disk);
  3164. zipwarn(errbuf, "");
  3165. skip_disk = 1;
  3166. break;
  3167. }
  3168. split_path = get_in_split_path(in_path, current_in_disk);
  3169. }
  3170. if (skip_disk) {
  3171. /* skip this current disk - this works because central directory entries
  3172. can't be split across splits */
  3173. skip_disk = 0;
  3174. skipped_disk = 1;
  3175. continue;
  3176. }
  3177. }
  3178. if (skipped_disk) {
  3179. /* Not much to do here as between entries. Entries are copied
  3180. in zipcopy() and that has to handle missing disks while
  3181. reading data for an entry.
  3182. */
  3183. }
  3184. /* Main loop */
  3185. /* Look for next signature and process it */
  3186. while (find_next_signature(in_file)) {
  3187. current_in_offset = zftello(in_file);
  3188. if (is_signature(sigbuf, "PK\05\06")) {
  3189. /* End Of Central Directory Record */
  3190. sprintf(errbuf, "EOCDR found (%2lu %6s)...",
  3191. current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
  3192. zipmessage_nl(errbuf, 1);
  3193. } else if (is_signature(sigbuf, "PK\06\06")) {
  3194. /* Zip64 End Of Central Directory Record */
  3195. sprintf(errbuf, "Zip64 EOCDR found (%2lu %6s)...",
  3196. current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
  3197. zipmessage_nl(errbuf, 1);
  3198. } else if (is_signature(sigbuf, "PK\06\07")) {
  3199. /* Zip64 End Of Central Directory Locator */
  3200. sprintf(errbuf, "Zip64 EOCDL found (%2lu %6s)...",
  3201. current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
  3202. zipmessage_nl(errbuf, 1);
  3203. } else if (is_signature(sigbuf, "PK\03\04")) {
  3204. /* Local Header Record */
  3205. if (verbose) {
  3206. sprintf(errbuf, " Local (%2lu %6s):",
  3207. current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
  3208. zipmessage_nl(errbuf, 0);
  3209. }
  3210. /* Create zlist entry. Most will be filled in by zipcopy(). */
  3211. if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
  3212. zipwarn("reading central directory", "");
  3213. return ZE_MEM;
  3214. }
  3215. z->vem = 0;
  3216. z->ver = 0;
  3217. z->flg = 0;
  3218. z->how = 0;
  3219. z->tim = 0; /* time and date into one long */
  3220. z->crc = 0;
  3221. z->siz = 0;
  3222. z->len = 0;
  3223. z->nam = 0; /* used before comparing cen vs. loc */
  3224. z->cext = 0; /* may be different from z->ext */
  3225. z->com = 0;
  3226. z->dsk = 0;
  3227. z->att = 0;
  3228. z->atx = 0;
  3229. z->off = 0;
  3230. z->dosflag = 0;
  3231. /* Initialize all fields pointing to malloced data to NULL */
  3232. z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
  3233. z->oname = NULL;
  3234. #ifdef UNICODE_SUPPORT
  3235. z->uname = z->zuname = z->ouname = NULL;
  3236. #endif
  3237. /* Attempt to copy entry */
  3238. r = zipcopy(z);
  3239. if (in_central_directory) {
  3240. sprintf(errbuf, "Entry after central directory found (%2lu %6s)...",
  3241. current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
  3242. zipmessage_nl(errbuf, 1);
  3243. in_central_directory = 0;
  3244. }
  3245. if (r == ZE_EOF)
  3246. /* user said no more splits */
  3247. break;
  3248. else if (r == ZE_OK) {
  3249. zcount++;
  3250. files_total++;
  3251. bytes_total += z->siz;
  3252. /* Link into list */
  3253. if (zfiles == NULL)
  3254. /* first link */
  3255. x = &zfiles;
  3256. /* Link into list */
  3257. *x = z;
  3258. z->nxt = NULL;
  3259. x = &z->nxt;
  3260. }
  3261. } else if (is_signature(sigbuf, "PK\01\02")) {
  3262. /* Central directory header */
  3263. /* sort the zlist */
  3264. if (in_central_directory == 0) {
  3265. zipmessage("Central Directory found...", "");
  3266. /* If one or more files, sort by name */
  3267. if (zcount)
  3268. {
  3269. struct zlist far * far *x; /* pointer into zsort array */
  3270. struct zlist far *z; /* pointer into zfiles linked list */
  3271. int i = 0;
  3272. extent zl_size = zcount * sizeof(struct zlist far *);
  3273. if (zl_size / sizeof(struct zlist far *) != zcount ||
  3274. (x = zsort = (struct zlist far **)malloc(zl_size)) == NULL)
  3275. return ZE_MEM;
  3276. for (z = zfiles; z != NULL; z = z->nxt)
  3277. x[i++] = z;
  3278. qsort((char *)zsort, zcount, sizeof(struct zlist far *), zqcmp);
  3279. /* Skip Unicode searching */
  3280. }
  3281. }
  3282. if (verbose) {
  3283. sprintf(errbuf, " Cen (%2lu %6s): ",
  3284. current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
  3285. zipmessage_nl(errbuf, 0);
  3286. }
  3287. in_central_directory = 1;
  3288. /* Read central directory entry */
  3289. /* central directory signature */
  3290. /* The format of a central directory record
  3291. central file header signature 4 bytes (0x02014b50)
  3292. version made by 2 bytes
  3293. version needed to extract 2 bytes
  3294. general purpose bit flag 2 bytes
  3295. compression method 2 bytes
  3296. last mod file time 2 bytes
  3297. last mod file date 2 bytes
  3298. crc-32 4 bytes
  3299. compressed size 4 bytes
  3300. uncompressed size 4 bytes
  3301. file name length 2 bytes
  3302. extra field length 2 bytes
  3303. file comment length 2 bytes
  3304. disk number start 2 bytes
  3305. internal file attributes 2 bytes
  3306. external file attributes 4 bytes
  3307. relative offset of local header 4 bytes
  3308. file name (variable size)
  3309. extra field (variable size)
  3310. file comment (variable size)
  3311. */
  3312. if (fread(scbuf, CENHEAD, 1, in_file) != 1) {
  3313. zipwarn("reading central directory: ", strerror(errno));
  3314. zipwarn("bad archive - error reading central directory", "");
  3315. zipwarn("skipping this entry...", "");
  3316. continue;
  3317. }
  3318. if ((cz = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
  3319. zipwarn("reading central directory", "");
  3320. return ZE_MEM;
  3321. }
  3322. cz->vem = SH(CENVEM + scbuf);
  3323. cz->ver = SH(CENVER + scbuf);
  3324. cz->flg = SH(CENFLG + scbuf);
  3325. cz->how = SH(CENHOW + scbuf);
  3326. cz->tim = LG(CENTIM + scbuf); /* time and date into one long */
  3327. cz->crc = LG(CENCRC + scbuf);
  3328. cz->siz = LG(CENSIZ + scbuf);
  3329. cz->len = LG(CENLEN + scbuf);
  3330. cz->nam = SH(CENNAM + scbuf); /* used before comparing cen vs. loc */
  3331. cz->cext = SH(CENEXT + scbuf); /* may be different from z->ext */
  3332. cz->com = SH(CENCOM + scbuf);
  3333. cz->dsk = SH(CENDSK + scbuf);
  3334. cz->att = SH(CENATT + scbuf);
  3335. cz->atx = LG(CENATX + scbuf);
  3336. cz->off = LG(CENOFF + scbuf);
  3337. cz->dosflag = (cz->vem & 0xff00) == 0;
  3338. /* Initialize all fields pointing to malloced data to NULL */
  3339. cz->zname = cz->name = cz->iname = cz->extra = cz->cextra = NULL;
  3340. cz->comment = cz->oname = NULL;
  3341. #ifdef UNICODE_SUPPORT
  3342. cz->uname = cz->zuname = cz->ouname = NULL;
  3343. #endif
  3344. /* Read file name, extra field and comment field */
  3345. if (cz->nam == 0)
  3346. {
  3347. sprintf(errbuf, "%lu", (ulg)zcount + 1);
  3348. zipwarn("zero-length name for entry #", errbuf);
  3349. zipwarn("skipping this entry...", "");
  3350. continue;
  3351. }
  3352. if ((cz->iname = malloc(cz->nam+1)) == NULL ||
  3353. (cz->cext && (cz->cextra = malloc(cz->cext + 1)) == NULL) ||
  3354. (cz->com && (cz->comment = malloc(cz->com + 1)) == NULL))
  3355. return ZE_MEM;
  3356. if (fread(cz->iname, cz->nam, 1, in_file) != 1 ||
  3357. (cz->cext && fread(cz->cextra, cz->cext, 1, in_file) != 1) ||
  3358. (cz->com && fread(cz->comment, cz->com, 1, in_file) != 1)) {
  3359. zipwarn("error reading entry: ", strerror(errno));
  3360. zipwarn("skipping this entry...", "");
  3361. continue;
  3362. }
  3363. cz->iname[cz->nam] = '\0'; /* terminate name */
  3364. /* Look up this name in zlist from local entries */
  3365. z = zsearch(cz->iname);
  3366. if (z && z->tim == cz->tim) {
  3367. /* Apparently as iname and date and time match this central
  3368. directory entry goes with this zlist entry */
  3369. if (verbose) {
  3370. /* cen dir name matches a local name */
  3371. sprintf(errbuf, "updating: %s", cz->iname);
  3372. zipmessage_nl(errbuf, 0);
  3373. }
  3374. if (z->crc != cz->crc) {
  3375. sprintf(errbuf, "local (%lu) and cen (%lu) crc mismatch", z->crc, cz->crc);
  3376. zipwarn(errbuf, "");
  3377. }
  3378. z->vem = cz->vem;
  3379. /* z->ver = cz->ver; */
  3380. /* z->flg = cz->flg; */
  3381. /* z->how = cz->how; */
  3382. /* z->tim = cz->tim; */ /* time and date into one long */
  3383. /* z->crc = cz->crc; */
  3384. /* z->siz = cz->siz; */
  3385. /* z->len = cz->len; */
  3386. /* z->nam = cz->nam; */ /* used before comparing cen vs. loc */
  3387. z->cext = cz->cext; /* may be different from z->ext */
  3388. z->com = cz->com;
  3389. z->cextra = cz->cextra;
  3390. z->comment = cz->comment;
  3391. /* z->dsk = cz->dsk; */
  3392. z->att = cz->att;
  3393. z->atx = cz->atx;
  3394. /* z->off = cz->off; */
  3395. z->dosflag = cz->dosflag;
  3396. #ifdef UNICODE_SUPPORT
  3397. if (unicode_mismatch != 3 && z->uname == NULL) {
  3398. if (z->flg & UTF8_BIT) {
  3399. /* path is UTF-8 */
  3400. if ((z->uname = malloc(strlen(z->iname) + 1)) == NULL) {
  3401. ZIPERR(ZE_MEM, "reading archive");
  3402. }
  3403. strcpy(z->uname, z->iname);
  3404. } else {
  3405. /* check for UTF-8 path extra field */
  3406. read_Unicode_Path_entry(z);
  3407. }
  3408. }
  3409. #endif
  3410. #ifdef WIN32
  3411. /* Input path may be OEM */
  3412. {
  3413. unsigned hostver = (z->vem & 0xff);
  3414. Ext_ASCII_TO_Native(z->iname, (z->vem >> 8), hostver,
  3415. ((z->atx & 0xffff0000L) != 0), FALSE);
  3416. }
  3417. #endif
  3418. #ifdef EBCDIC
  3419. if (z->com)
  3420. memtoebc(z->comment, z->comment, z->com);
  3421. #endif /* EBCDIC */
  3422. #ifdef WIN32
  3423. /* Comment may be OEM */
  3424. {
  3425. unsigned hostver = (z->vem & 0xff);
  3426. Ext_ASCII_TO_Native(z->comment, (z->vem >> 8), hostver,
  3427. ((z->atx & 0xffff0000L) != 0), FALSE);
  3428. }
  3429. #endif
  3430. #ifdef ZIP64_SUPPORT
  3431. /* zip64 support 08/31/2003 R.Nausedat */
  3432. /* here, we have to read the len, siz etc values from the CD */
  3433. /* entry as we might have to adjust them regarding their */
  3434. /* correspronding zip64 extra fields. */
  3435. /* also, we cannot compare the values from the CD entries with */
  3436. /* the values from the LH as they might be different. */
  3437. /* adjust/update siz,len and off (to come: dsk) entries */
  3438. /* PKZIP does not care of the version set in a CDH: if */
  3439. /* there is a zip64 extra field assigned to a CDH PKZIP */
  3440. /* uses it, we should do so, too. */
  3441. /*
  3442. adjust_zip_central_entry(z);
  3443. */
  3444. #endif
  3445. /* Update zipbeg beginning of archive offset, prepare for next header */
  3446. /*
  3447. if (z->dsk == 0 && (!zipbegset || z->off < zipbeg)) {
  3448. zipbeg = z->off;
  3449. zipbegset = 1;
  3450. }
  3451. zcount++;
  3452. */
  3453. #ifndef UTIL
  3454. if (verbose)
  3455. zipoddities(z);
  3456. #endif
  3457. current_offset = zftello(y);
  3458. if (zfseeko(y, z->off, SEEK_SET) != 0) {
  3459. fclose(in_file);
  3460. in_file = NULL;
  3461. zipwarn("writing archive seek: ", strerror(errno));
  3462. return ZE_WRITE;
  3463. }
  3464. if (putlocal(z, PUTLOCAL_REWRITE) != ZE_OK)
  3465. zipwarn("Error rewriting local header", "");
  3466. if (zfseeko(y, current_offset, SEEK_SET) != 0) {
  3467. fclose(in_file);
  3468. in_file = NULL;
  3469. zipwarn("write archive seek: ", strerror(errno));
  3470. return ZE_WRITE;
  3471. }
  3472. offset = zftello(y);
  3473. if (current_offset != offset) {
  3474. fclose(in_file);
  3475. in_file = NULL;
  3476. zipwarn("seek after local: ", strerror(errno));
  3477. return ZE_WRITE;
  3478. }
  3479. if (verbose)
  3480. zipmessage_nl("", 1);
  3481. } else {
  3482. /* cen dir name does not match local name */
  3483. sprintf(errbuf, "no local entry: %s", cz->iname);
  3484. zipmessage_nl(errbuf, 1);
  3485. }
  3486. } else if (zfiles == NULL && is_signature(sigbuf, "PK\07\010")) {
  3487. /* assume spanning signature at top of archive */
  3488. if (total_disks == 1) {
  3489. zipmessage(" Found spanning marker, but did not expect split (multi-disk) archive...", "");
  3490. } else if (total_disks > 1) {
  3491. zipmessage(" Found spanning marker - expected as this is split (multi-disk) archive...", "");
  3492. } else {
  3493. zipmessage(" Found spanning marker - could be split archive...", "");
  3494. }
  3495. } else {
  3496. /* this signature shouldn't be here */
  3497. int c;
  3498. char errbuftemp[40];
  3499. strcpy(errbuf, "unexpected signature ");
  3500. for (c = 0; c < 4; c++) {
  3501. sprintf(errbuftemp, "%02x ", sigbuf[c]);
  3502. strcat(errbuf, errbuftemp);
  3503. }
  3504. sprintf(errbuftemp, "on disk %lu at %s\n", current_in_disk,
  3505. zip_fzofft(current_in_offset - 4, NULL, "u"));
  3506. strcat(errbuf, errbuftemp);
  3507. zipwarn(errbuf, "");
  3508. zipwarn("skipping this signature...", "");
  3509. }
  3510. } /* while reading file */
  3511. /* close disk and do next disk */
  3512. if (in_file)
  3513. fclose(in_file);
  3514. in_file = NULL;
  3515. free(split_path);
  3516. if (r == ZE_EOF)
  3517. /* user says no more splits */
  3518. break;
  3519. } /* for each disk */
  3520. return ZE_OK;
  3521. } /* end of function scanzipf_fixnew() */
  3522. #endif /* !UTIL */
  3523. /* ---------------------- */
  3524. /* New regular scan */
  3525. /*
  3526. * scanzipf_regnew is similar to the orignal scanzipf_reg in that it
  3527. * reads the end of the archive and goes from there. Unlike that
  3528. * scan this one stops after reading the central directory and does
  3529. * not read the local headers. After the directory scan for new
  3530. * files is done in zip.c the zlist created here is used to read
  3531. * the old archive entries there. The local headers are read using
  3532. * readlocal() in zipcopy().
  3533. *
  3534. * This scan assumes the zip file is well structured. If not it may
  3535. * fail and the new scanzipf_fixnew should be used.
  3536. *
  3537. * 2006-2-4, 2007-12-10 EG
  3538. */
  3539. local int scanzipf_regnew()
  3540. /*
  3541. The input path for the .zip file is in in_path. If a split archive,
  3542. the path for each split is created from the current disk number
  3543. and in_path. If a split is not in the same directory as the last
  3544. split we ask the user where it is and update in_path.
  3545. */
  3546. /*
  3547. This is old but more or less still applies:
  3548. The name of the zip file is pointed to by the global "zipfile". The globals
  3549. zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
  3550. Return an error code in the ZE_ class.
  3551. */
  3552. {
  3553. /* In this function, a local buffer is used to read in the following Zip
  3554. structures:
  3555. End-of-CentralDir record (EOCDR) (ENDHEAD)
  3556. Zip64-End-of-CentralDir-Record locator (Zip64 EOCDL) (EC64LOC)
  3557. Zip64-End-of-CentralDir record (Zip64 EOCDR) (EC64REC)
  3558. CentralDir-Entry record (CENHEAD)
  3559. To conserve valuable stack space, this buffer is sized to the largest
  3560. of these structures.
  3561. */
  3562. # if CENHEAD > ENDHEAD
  3563. # define SCAN_BUFSIZE CENHEAD /* CENHEAD should be the larger struct */
  3564. # else
  3565. # define SCAN_BUFSIZE ENDHEAD
  3566. # endif
  3567. #ifdef ZIP64_SUPPORT
  3568. # if EC64REC > SCAN_BUFSIZE
  3569. # undef SCAN_BUFSIZE
  3570. # define SCAN_BUFSIZE EC64REC /* EC64 record should be largest struct */
  3571. # endif
  3572. # if EC64LOC > SCAN_BUFSIZE
  3573. # undef SCAN_BUFSIZE
  3574. # define SCAN_BUFSIZE EC64LOC
  3575. # endif
  3576. #endif
  3577. char scbuf[SCAN_BUFSIZE]; /* buffer just enough for all header types */
  3578. char *split_path;
  3579. ulg eocdr_disk;
  3580. uzoff_t eocdr_offset;
  3581. # ifdef ZIP64_SUPPORT
  3582. ulg z64eocdr_disk;
  3583. uzoff_t z64eocdr_offset;
  3584. uzoff_t z64eocdr_size;
  3585. ush version_made;
  3586. ush version_needed = 0;
  3587. zoff_t zip64_eocdr_start;
  3588. zoff_t z64eocdl_offset;
  3589. # endif /* def ZIP64_SUPPORT */
  3590. uzoff_t cd_total_entries; /* num of entries as read from (Zip64) EOCDR */
  3591. ulg in_cd_start_disk; /* central directory start disk */
  3592. uzoff_t in_cd_start_offset; /* offset of start of cd on cd start disk */
  3593. uzoff_t adjust_offset = 0; /* bytes before first entry (size of sfx prefix) */
  3594. uzoff_t cd_total_size = 0; /* total size of cd */
  3595. int first_CD = 1; /* looking for first CD entry */
  3596. int zipbegset = 0;
  3597. int skip_disk = 0; /* 1 if user asks to skip current disk */
  3598. int skipped_disk = 0; /* 1 if skipped start disk and start offset is useless */
  3599. uzoff_t s; /* size of data, start of central */
  3600. struct zlist far * far *x; /* pointer last entry's link */
  3601. struct zlist far *z; /* current zip entry structure */
  3602. /* open the zipfile */
  3603. if ((in_file = zfopen(in_path, FOPR)) == NULL) {
  3604. zipwarn("could not open input archive", in_path);
  3605. return ZE_OPEN;
  3606. }
  3607. #ifndef ZIP64_SUPPORT
  3608. /* 2004-12-06 SMS.
  3609. * Check for too-big file before doing any serious work.
  3610. */
  3611. if (ffile_size( in_file) == EOF) {
  3612. fclose(in_file);
  3613. in_file = NULL;
  3614. zipwarn("input file requires Zip64 support: ", in_path);
  3615. return ZE_ZIP64;
  3616. }
  3617. #endif /* ndef ZIP64_SUPPORT */
  3618. /* look for End Of Central Directory Record */
  3619. /* In a valid Zip archive, the EOCDR can be at most (64k-1 + ENDHEAD + 4)
  3620. bytes (=65557 bytes) from the end of the file.
  3621. We back up 128k, to allow some junk being appended to a Zip file.
  3622. */
  3623. if ((zfseeko(in_file, -0x20000L, SEEK_END) != 0) ||
  3624. /* Some fseek() implementations (e.g. MSC 8.0 16-bit) fail to signal
  3625. an error when seeking before the beginning of the file.
  3626. As work-around, we check the position returned by zftello()
  3627. for the error value -1.
  3628. */
  3629. (zftello(in_file) == (zoff_t)-1L)) {
  3630. /* file is less than 128 KB so back up to beginning */
  3631. if (zfseeko(in_file, 0L, SEEK_SET) != 0) {
  3632. fclose(in_file);
  3633. in_file = NULL;
  3634. zipwarn("unable to seek in input file ", in_path);
  3635. return ZE_READ;
  3636. }
  3637. }
  3638. /* find EOCD Record signature */
  3639. if (!find_signature(in_file, "PK\05\06")) {
  3640. /* No End Of Central Directory Record */
  3641. fclose(in_file);
  3642. in_file = NULL;
  3643. if (fix == 1) {
  3644. zipwarn("bad archive - missing end signature", "");
  3645. zipwarn("(If downloaded, was binary mode used? If not, the", "");
  3646. zipwarn(" archive may be scrambled and not recoverable)", "");
  3647. zipwarn("Can't use -F to fix (try -FF)", "");
  3648. } else{
  3649. zipwarn("missing end signature--probably not a zip file (did you", "");
  3650. zipwarn("remember to use binary mode when you transferred it?)", "");
  3651. zipwarn("(if you are trying to read a damaged archive try -F)", "");
  3652. }
  3653. return ZE_FORM;
  3654. }
  3655. /* at start of data after EOCDR signature */
  3656. eocdr_offset = (uzoff_t) zftello(in_file);
  3657. /* OK, it is possible this is not the last EOCDR signature (might be
  3658. EOCDR signature from a stored archive in the last 128 KB) and so not
  3659. the one we want.
  3660. The below assumes the signature does not appear in the assumed ASCII text
  3661. .ZIP file comment.
  3662. */
  3663. while (find_signature(in_file, "PK\05\06")) {
  3664. /* previous one was not the one */
  3665. eocdr_offset = (uzoff_t) zftello(in_file);
  3666. }
  3667. /* found EOCDR */
  3668. /* format is
  3669. end of central dir signature 4 bytes (0x06054b50)
  3670. number of this disk 2 bytes
  3671. number of the disk with the
  3672. start of the central directory 2 bytes
  3673. total number of entries in the
  3674. central directory on this disk 2 bytes
  3675. total number of entries in
  3676. the central directory 2 bytes
  3677. size of the central directory 4 bytes
  3678. offset of start of central
  3679. directory with respect to
  3680. the starting disk number 4 bytes
  3681. .ZIP file comment length 2 bytes
  3682. .ZIP file comment (variable size)
  3683. */
  3684. if (zfseeko(in_file, eocdr_offset, SEEK_SET) != 0) {
  3685. fclose(in_file);
  3686. in_file = NULL;
  3687. zipwarn("unable to seek in input file ", in_path);
  3688. return ZE_READ;
  3689. }
  3690. /* read the EOCDR */
  3691. s = fread(scbuf, 1, ENDHEAD, in_file);
  3692. /* the first field should be number of this (the last) disk */
  3693. eocdr_disk = (ulg)SH(scbuf);
  3694. total_disks = eocdr_disk + 1;
  3695. /* Assume EOCDR disk is this disk. If a lot of disks, the Zip64 field
  3696. may be needed and this EOCDR field could be set to the Zip64 flag
  3697. value as the disk number may be bigger than this field can hold.
  3698. */
  3699. current_in_disk = total_disks - 1;
  3700. /* Central Directory disk, offset, and total entries */
  3701. in_cd_start_disk = (ulg)SH(scbuf + ENDBEG);
  3702. in_cd_start_offset = (uzoff_t)LG(scbuf + ENDOFF);
  3703. cd_total_entries = (uzoff_t)SH(scbuf + ENDTOT);
  3704. cd_total_size = (uzoff_t)LG(scbuf + ENDSIZ);
  3705. /* length of zipfile comment */
  3706. zcomlen = SH(scbuf + ENDCOM);
  3707. if (zcomlen)
  3708. {
  3709. if ((zcomment = malloc(zcomlen + 1)) == NULL)
  3710. return ZE_MEM;
  3711. if (fread(zcomment, zcomlen, 1, in_file) != 1)
  3712. {
  3713. free((zvoid *)zcomment);
  3714. zcomment = NULL;
  3715. return ferror(in_file) ? ZE_READ : ZE_EOF;
  3716. }
  3717. zcomment[zcomlen] = '\0';
  3718. #ifdef EBCDIC
  3719. if (zcomment)
  3720. memtoebc(zcomment, zcomment, zcomlen);
  3721. #endif /* EBCDIC */
  3722. }
  3723. if (cd_total_entries == 0) {
  3724. /* empty archive */
  3725. fclose(in_file);
  3726. in_file = NULL;
  3727. return ZE_OK;
  3728. }
  3729. /* if total disks is other than 1 then multi-disk archive */
  3730. if (total_disks != 1) {
  3731. /* zipfile name must end in .zip for split archives */
  3732. int plen = strlen(in_path);
  3733. char *in_path_ext;
  3734. if (adjust) {
  3735. zipwarn("Adjusting split archives not yet supported", "");
  3736. return ZE_FORM;
  3737. }
  3738. #ifdef VMS
  3739. /* On VMS, adjust plen (and in_path_ext) to avoid the file version. */
  3740. plen -= strlen(vms_file_version(in_path));
  3741. #endif /* def VMS */
  3742. in_path_ext = zipfile + plen - 4;
  3743. if (plen < 4 ||
  3744. in_path_ext[0] != '.' ||
  3745. toupper(in_path_ext[1]) != 'Z' ||
  3746. toupper(in_path_ext[2]) != 'I' ||
  3747. toupper(in_path_ext[3]) != 'P') {
  3748. zipwarn("archive name must end in .zip for splits", "");
  3749. fclose(in_file);
  3750. in_file = NULL;
  3751. return ZE_PARMS;
  3752. }
  3753. }
  3754. /* if input or output are split archives, must be different archives */
  3755. if ((total_disks != 1 || split_method) && !show_files &&
  3756. strcmp(in_path, out_path) == 0) {
  3757. fclose(in_file);
  3758. in_file = NULL;
  3759. zipwarn("cannot update a split archive (use --out option)", "");
  3760. return ZE_PARMS;
  3761. }
  3762. /* if fixing archive, input and output must be different archives */
  3763. if (fix == 1 && strcmp(in_path, out_path) == 0) {
  3764. fclose(in_file);
  3765. in_file = NULL;
  3766. zipwarn("must use --out when fixing an archive", "");
  3767. return ZE_PARMS;
  3768. }
  3769. /* Get sfx offset if adjusting. Above we made sure not split archive. */
  3770. /* Also check for an offset if fix and single disk archive. */
  3771. if ((fix == 1 && total_disks == 1) || adjust) {
  3772. zoff_t cd_start;
  3773. # ifdef ZIP64_SUPPORT
  3774. zoff_t zip64_eocdr_start;
  3775. # endif
  3776. /* First attempt. If the CD start offset and size are valid in the EOCDR
  3777. (meaning they are not the Zip64 flag values that say the actual values
  3778. are in the Zip64 EOCDR), we can use them to get the offset */
  3779. if (in_cd_start_offset != 0xFFFFFFFF && cd_total_size != 0xFFFFFFFF) {
  3780. /* Search for start of central directory */
  3781. /* There still might be a Zip64 EOCDR. This assumes if there is
  3782. a Zip64 EOCDR, it's version 1 and 52 bytes */
  3783. cd_start = eocdr_offset - cd_total_size - 24 - 56;
  3784. if (zfseeko(in_file, cd_start, SEEK_SET) != 0) {
  3785. fclose(in_file);
  3786. in_file = NULL;
  3787. if (fix == 1) {
  3788. zipwarn("could not seek back to start of central directory: ", strerror(errno));
  3789. zipwarn("(try -FF)", "");
  3790. } else {
  3791. zipwarn("reading archive fseek: ", strerror(errno));
  3792. }
  3793. return ZE_FORM;
  3794. }
  3795. if (find_signature(in_file, "PK\01\02")) {
  3796. /* Should now be after first central directory header signature in archive */
  3797. adjust_offset = zftello(in_file) - 4 - in_cd_start_offset;
  3798. } else {
  3799. zipwarn("central dir not where expected - could not adjust offsets", "");
  3800. zipwarn("(try -FF)", "");
  3801. return ZE_FORM;
  3802. }
  3803. } else {
  3804. /* Second attempt. We need the Zip64 EOCDL to get the offset */
  3805. /*
  3806. * Check for a Zip64 EOCD Locator signature
  3807. */
  3808. /* Format of Z64EOCD Locator is
  3809. zip64 end of central dir locator
  3810. signature 4 bytes (0x07064b50)
  3811. number of the disk with the
  3812. start of the zip64 end of
  3813. central directory 4 bytes
  3814. relative offset of the zip64
  3815. end of central directory record 8 bytes
  3816. total number of disks 4 bytes
  3817. */
  3818. /* back up 20 bytes from EOCDR to Z64 EOCDL */
  3819. if (zfseeko(in_file, eocdr_offset - 24, SEEK_SET) != 0) {
  3820. fclose(in_file);
  3821. in_file = NULL;
  3822. if (fix == 1) {
  3823. zipwarn("could not seek back to Zip64 EOCDL: ", strerror(errno));
  3824. zipwarn("(try -FF)", "");
  3825. } else {
  3826. zipwarn("reading archive fseek: ", strerror(errno));
  3827. }
  3828. return ZE_FORM;
  3829. }
  3830. if (at_signature(in_file, "PK\06\07"))
  3831. #ifndef ZIP64_SUPPORT
  3832. {
  3833. fclose(in_file);
  3834. in_file = NULL;
  3835. zipwarn("found Zip64 signature - this may be a Zip64 archive", "");
  3836. zipwarn("Need PKZIP 4.5 or later compatible zip", "");
  3837. zipwarn("Set ZIP64_SUPPORT in Zip 3", "");
  3838. return ZE_ZIP64;
  3839. }
  3840. #else /* ZIP64_SUPPORT */
  3841. {
  3842. z64eocdl_offset = zftello(in_file) - 4;
  3843. /* read Z64 EOCDL */
  3844. if (fread(scbuf, EC64LOC, 1, in_file) != 1) {
  3845. fclose(in_file);
  3846. in_file = NULL;
  3847. zipwarn("reading archive: ", strerror(errno));
  3848. return ZE_READ;
  3849. }
  3850. /* now should be back at the EOCD signature */
  3851. if (!at_signature(in_file, "PK\05\06")) {
  3852. fclose(in_file);
  3853. in_file = NULL;
  3854. zipwarn("unable to read EOCD after seek: ", in_path);
  3855. return ZE_READ;
  3856. }
  3857. /* read disk and offset to Zip64 EOCDR and total disks */
  3858. z64eocdr_disk = LG(scbuf);
  3859. z64eocdr_offset = LLG(scbuf + 4);
  3860. total_disks = LG(scbuf + 12);
  3861. /* For now no split archives */
  3862. if (total_disks != 1) {
  3863. zipwarn("Adjusting split archives not supported: ", in_path);
  3864. zipwarn("(try -FF)", "");
  3865. return ZE_FORM;
  3866. }
  3867. /* go to the Zip64 EOCDR */
  3868. if (zfseeko(in_file, z64eocdr_offset, SEEK_SET) != 0) {
  3869. fclose(in_file);
  3870. in_file = NULL;
  3871. zipwarn("reading archive fseek: ", strerror(errno));
  3872. return ZE_FORM;
  3873. }
  3874. /* Should be at Zip64 EOCDR signature */
  3875. if (at_signature(in_file, "PK\06\06")) {
  3876. /* apparently no offset */
  3877. } else {
  3878. /* Wasn't there, so calculate based on Zip64 EOCDL offset */
  3879. zip64_eocdr_start = z64eocdl_offset - 24 - 56;
  3880. if (zfseeko(in_file, zip64_eocdr_start, SEEK_SET) != 0) {
  3881. fclose(in_file);
  3882. in_file = NULL;
  3883. if (fix == 1) {
  3884. zipwarn("could not seek back to Zip64 EOCDR: ", strerror(errno));
  3885. zipwarn("(try -FF)", "");
  3886. } else {
  3887. zipwarn("reading archive fseek: ", strerror(errno));
  3888. }
  3889. return ZE_FORM;
  3890. }
  3891. if (find_next_signature(in_file) && is_signature(sigbuf, "PK\06\06")) {
  3892. /* Should now be after Zip64 EOCDR signature in archive */
  3893. adjust_offset = zftello(in_file) - 4 - z64eocdr_offset;
  3894. } else {
  3895. zipwarn("Could not determine offset of entries", "");
  3896. zipwarn("(try -FF)", "");
  3897. return ZE_FORM;
  3898. }
  3899. }
  3900. }
  3901. #endif
  3902. }
  3903. if (noisy) {
  3904. if (adjust_offset) {
  3905. sprintf(errbuf, "Zip entry offsets appear off by %s bytes - correcting...",
  3906. zip_fzofft(adjust_offset, NULL, NULL));
  3907. } else {
  3908. sprintf(errbuf, "Zip entry offsets do not need adjusting");
  3909. }
  3910. zipmessage(errbuf, "");
  3911. }
  3912. }
  3913. /*
  3914. * Check for a Zip64 EOCD Locator signature
  3915. */
  3916. /* Format of Z64EOCD Locator is
  3917. zip64 end of central dir locator
  3918. signature 4 bytes (0x07064b50)
  3919. number of the disk with the
  3920. start of the zip64 end of
  3921. central directory 4 bytes
  3922. relative offset of the zip64
  3923. end of central directory record 8 bytes
  3924. total number of disks 4 bytes
  3925. */
  3926. /* back up 20 bytes from EOCDR to Z64 EOCDL */
  3927. if (zfseeko(in_file, eocdr_offset - 24, SEEK_SET) != 0) {
  3928. fclose(in_file);
  3929. in_file = NULL;
  3930. if (fix == 1) {
  3931. zipwarn("bad archive - could not seek back to Zip64 EOCDL: ", strerror(errno));
  3932. zipwarn("(try -FF)", "");
  3933. } else {
  3934. zipwarn("reading archive fseek: ", strerror(errno));
  3935. }
  3936. return ZE_FORM;
  3937. }
  3938. if (at_signature(in_file, "PK\06\07"))
  3939. #ifndef ZIP64_SUPPORT
  3940. {
  3941. fclose(in_file);
  3942. in_file = NULL;
  3943. zipwarn("found Zip64 signature - this may be a Zip64 archive", "");
  3944. zipwarn("Need PKZIP 4.5 or later compatible zip", "");
  3945. zipwarn("Set ZIP64_SUPPORT in Zip 3", "");
  3946. return ZE_ZIP64;
  3947. }
  3948. #else /* ZIP64_SUPPORT */
  3949. {
  3950. z64eocdl_offset = zftello(in_file) - 4;
  3951. /* read Z64 EOCDL */
  3952. if (fread(scbuf, EC64LOC, 1, in_file) != 1) {
  3953. fclose(in_file);
  3954. in_file = NULL;
  3955. zipwarn("reading archive: ", strerror(errno));
  3956. return ZE_READ;
  3957. }
  3958. /* now should be back at the EOCD signature */
  3959. if (!at_signature(in_file, "PK\05\06")) {
  3960. fclose(in_file);
  3961. in_file = NULL;
  3962. zipwarn("unable to read EOCD after seek: ", in_path);
  3963. return ZE_READ;
  3964. }
  3965. /* read disk and offset to Zip64 EOCDR and total disks */
  3966. z64eocdr_disk = LG(scbuf);
  3967. z64eocdr_offset = LLG(scbuf + 4) + adjust_offset;
  3968. total_disks = LG(scbuf + 12);
  3969. /* set the current disk */
  3970. current_in_disk = total_disks - 1;
  3971. /* Now need to read the Zip64 EOCD Record to get version needed
  3972. to extract */
  3973. if (z64eocdr_disk != total_disks - 1) {
  3974. /* Zip64 EOCDR not on this disk */
  3975. /* done with this disk (since apparently there are no CD entries
  3976. on it) */
  3977. fclose(in_file);
  3978. in_file = NULL;
  3979. /* get the path for the disk with the Zip64 EOCDR */
  3980. split_path = get_in_split_path(in_path, z64eocdr_disk);
  3981. while ((in_file = zfopen(split_path, FOPR)) == NULL) {
  3982. /* could not open split */
  3983. /* Ask where this split is. This call also updates global in_path. */
  3984. if (ask_for_split_read_path(z64eocdr_disk) != ZE_OK) {
  3985. return ZE_ABORT;
  3986. }
  3987. free(split_path);
  3988. split_path = get_in_split_path(in_path, z64eocdr_disk);
  3989. }
  3990. free(split_path);
  3991. }
  3992. current_in_disk = z64eocdr_disk;
  3993. /* go to the Zip64 EOCDR */
  3994. if (zfseeko(in_file, z64eocdr_offset, SEEK_SET) != 0) {
  3995. fclose(in_file);
  3996. in_file = NULL;
  3997. zipwarn("reading archive fseek: ", strerror(errno));
  3998. return ZE_FORM;
  3999. }
  4000. /* Should be at Zip64 EOCDR signature */
  4001. if (!at_signature(in_file, "PK\06\06")) {
  4002. /* Wasn't there, so calculate based on Zip64 EOCDL offset */
  4003. zip64_eocdr_start = z64eocdl_offset - 24 - 56;
  4004. if (zfseeko(in_file, zip64_eocdr_start, SEEK_SET) != 0) {
  4005. fclose(in_file);
  4006. in_file = NULL;
  4007. if (fix == 1) {
  4008. zipwarn("bad archive - could not seek back to Zip64 EOCDR: ", strerror(errno));
  4009. zipwarn("(try -FF)", "");
  4010. } else {
  4011. zipwarn("reading archive fseek: ", strerror(errno));
  4012. }
  4013. return ZE_FORM;
  4014. }
  4015. if (find_next_signature(in_file) && is_signature(sigbuf, "PK\06\06")) {
  4016. /* Should now be after Zip64 EOCDR signature in archive */
  4017. adjust_offset = zftello(in_file) - 4 - z64eocdr_offset;
  4018. zipwarn("Zip64 EOCDR not found where expected - compensating", "");
  4019. zipwarn("(try -A to adjust offsets)", "");
  4020. } else {
  4021. fclose(in_file);
  4022. in_file = NULL;
  4023. if (fix == 1) {
  4024. zipwarn("bad archive - Zip64 EOCDR not found in split: ", in_path);
  4025. zipwarn("(try -FF)", "");
  4026. } else {
  4027. zipwarn("Zip64 End Of Central Directory Record not found: ", in_path);
  4028. }
  4029. return ZE_FORM;
  4030. }
  4031. }
  4032. /*
  4033. * Read the Z64 End Of Central Directory Record
  4034. */
  4035. /* The format of the Z64 EOCDR is
  4036. zip64 end of central dir
  4037. signature 4 bytes (0x06064b50)
  4038. size of zip64 end of central
  4039. directory record 8 bytes
  4040. version made by 2 bytes
  4041. version needed to extract 2 bytes
  4042. number of this disk 4 bytes
  4043. number of the disk with the
  4044. start of the central directory 4 bytes
  4045. total number of entries in the
  4046. central directory on this disk 8 bytes
  4047. total number of entries in the
  4048. central directory 8 bytes
  4049. size of the central directory 8 bytes
  4050. offset of start of central
  4051. directory with respect to
  4052. the starting disk number 8 bytes
  4053. (version 2 of the Zip64 EOCDR has more after this)
  4054. zip64 extensible data sector (variable size)
  4055. */
  4056. /* read the first 52 bytes of the Zip64 EOCDR (we don't support
  4057. version 2, which supports PKZip licensed features)
  4058. */
  4059. s = fread(scbuf, 1, EC64REC, in_file);
  4060. if (s < EC64REC) {
  4061. if (fix == 1) {
  4062. zipwarn("bad archive - Zip64 EOCDR bad or truncated", "");
  4063. zipwarn("(try -FF)", "");
  4064. } else {
  4065. zipwarn("Zip64 EOCD Record bad or truncated", "");
  4066. }
  4067. fclose(in_file);
  4068. in_file = NULL;
  4069. return ZE_FORM;
  4070. }
  4071. z64eocdr_size = LLG(scbuf);
  4072. version_made = SH(scbuf + 8);
  4073. version_needed = SH(scbuf + 10);
  4074. in_cd_start_disk = LG(scbuf + 16);
  4075. cd_total_entries = LLG(scbuf + 28);
  4076. in_cd_start_offset = LLG(scbuf + 44) + adjust_offset;
  4077. if (version_needed > 46) {
  4078. int major = version_needed / 10;
  4079. int minor = version_needed - (major * 10);
  4080. sprintf(errbuf, "This archive requires version %d.%d", major, minor);
  4081. zipwarn(errbuf, "");
  4082. zipwarn("Zip currently only supports up to version 4.6 archives", "");
  4083. zipwarn("(up to 4.5 if bzip2 is not compiled in)", "");
  4084. if (fix == 1)
  4085. zipwarn("If -F fails try -FF to try to salvage something", "");
  4086. else if (fix == 2)
  4087. zipwarn("Attempting to salvage what can", "");
  4088. else {
  4089. zipwarn("Try -F to attempt to read anyway", "");
  4090. fclose(in_file);
  4091. in_file = NULL;
  4092. return ZE_FORM;
  4093. }
  4094. }
  4095. }
  4096. #endif /* ?ZIP64_SUPPORT */
  4097. /* Now read the central directory and create the zlist */
  4098. /* Multi-volume file names end in .z01, .z02, ..., .z10, .zip for 11 disk archive */
  4099. in_cd_start_offset += adjust_offset;
  4100. cenbeg = in_cd_start_offset;
  4101. zipbegset = 0;
  4102. zipbeg = 0;
  4103. first_CD = 1;
  4104. /* if the central directory starts on other than this disk, close this disk */
  4105. if (current_in_disk != in_cd_start_disk) {
  4106. /* close current disk */
  4107. fclose(in_file);
  4108. in_file = NULL;
  4109. }
  4110. /* Read the disks with the central directory in order - usually the
  4111. central directory fits on the last disk, but it doesn't have to.
  4112. */
  4113. for (current_in_disk = in_cd_start_disk;
  4114. current_in_disk < total_disks;
  4115. current_in_disk++) {
  4116. /* get the path for this disk */
  4117. if (current_in_disk == total_disks - 1) {
  4118. /* last disk is archive.zip */
  4119. if ((split_path = malloc(strlen(in_path) + 1)) == NULL) {
  4120. zipwarn("reading archive: ", in_path);
  4121. return ZE_MEM;
  4122. }
  4123. strcpy(split_path, in_path);
  4124. } else {
  4125. /* other disks are archive.z01, archive.z02, ... */
  4126. split_path = get_in_split_path(in_path, current_in_disk);
  4127. }
  4128. /* if in_file is not NULL then in_file is already open */
  4129. if (in_file == NULL) {
  4130. /* open the split */
  4131. while ((in_file = zfopen(split_path, FOPR)) == NULL) {
  4132. int result;
  4133. /* could not open split */
  4134. /* Ask for directory with split. Updates global variable in_path */
  4135. result = ask_for_split_read_path(current_in_disk);
  4136. if (result == ZE_ABORT) {
  4137. zipwarn("could not find split: ", split_path);
  4138. return ZE_ABORT;
  4139. } else if (result == ZE_FORM) {
  4140. /* user asked to skip this disk */
  4141. sprintf(errbuf, "skipping disk %lu ...\n", current_in_disk);
  4142. zipwarn(errbuf, "");
  4143. skip_disk = 1;
  4144. break;
  4145. }
  4146. if (current_in_disk == total_disks - 1) {
  4147. /* last disk is archive.zip */
  4148. if ((split_path = malloc(strlen(in_path) + 1)) == NULL) {
  4149. zipwarn("reading archive: ", in_path);
  4150. return ZE_MEM;
  4151. }
  4152. strcpy(split_path, in_path);
  4153. } else {
  4154. /* other disks are archive.z01, archive.z02, ... */
  4155. split_path = get_in_split_path(zipfile, current_in_disk);
  4156. }
  4157. }
  4158. if (skip_disk) {
  4159. /* skip this current disk - this works because central directory entries
  4160. can't be split across splits */
  4161. skip_disk = 0;
  4162. skipped_disk = 1;
  4163. continue;
  4164. }
  4165. }
  4166. if (skipped_disk) {
  4167. /* skipped start CD disk so start searching for CD signature at start of disk */
  4168. first_CD = 0;
  4169. } else {
  4170. /* seek to the first CD entry */
  4171. if (first_CD) {
  4172. if (zfseeko(in_file, in_cd_start_offset, SEEK_SET) != 0) {
  4173. fclose(in_file);
  4174. in_file = NULL;
  4175. zipwarn("unable to seek in input file ", split_path);
  4176. return ZE_READ;
  4177. }
  4178. first_CD = 0;
  4179. x = &zfiles; /* first link */
  4180. }
  4181. }
  4182. /* Main loop */
  4183. /* Look for next signature and process it */
  4184. while (find_next_signature(in_file)) {
  4185. current_in_offset = zftello(in_file);
  4186. if (is_signature(sigbuf, "PK\05\06")) {
  4187. /* End Of Central Directory Record */
  4188. /*
  4189. fprintf(mesg, "EOCDR signature at %d / %I64d\n",
  4190. current_in_disk, current_in_offset - 4);
  4191. */
  4192. break;
  4193. } else if (is_signature(sigbuf, "PK\06\06")) {
  4194. /* Zip64 End Of Central Directory Record */
  4195. /*
  4196. fprintf(mesg, "Zip64 EOCDR signature at %d / %I64d\n",
  4197. current_in_disk, current_in_offset - 4);
  4198. */
  4199. break;
  4200. } else if (!is_signature(sigbuf, "PK\01\02")) {
  4201. /* Not Central Directory Record */
  4202. /* this signature shouldn't be here */
  4203. if (fix == 1) {
  4204. int c;
  4205. char errbuftemp[40];
  4206. strcpy(errbuf, "bad archive - unexpected signature ");
  4207. for (c = 0; c < 4; c++) {
  4208. sprintf(errbuftemp, "%02x ", sigbuf[c]);
  4209. strcat(errbuf, errbuftemp);
  4210. }
  4211. sprintf(errbuftemp, "on disk %lu at %s\n", current_in_disk,
  4212. zip_fzofft(current_in_offset - 4, NULL, "u"));
  4213. strcat(errbuf, errbuftemp);
  4214. zipwarn(errbuf, "");
  4215. zipwarn("skipping this signature...", "");
  4216. continue;
  4217. } else {
  4218. sprintf(errbuf, "unexpected signature on disk %lu at %s\n",
  4219. current_in_disk, zip_fzofft(current_in_offset - 4, NULL, "u"));
  4220. zipwarn(errbuf, "");
  4221. zipwarn("archive not in correct format: ", split_path);
  4222. zipwarn("(try -F to attempt recovery)", "");
  4223. fclose(in_file);
  4224. in_file = NULL;
  4225. return ZE_FORM;
  4226. }
  4227. }
  4228. /* central directory signature */
  4229. if (verbose && fix == 1) {
  4230. fprintf(mesg, "central directory header signature on disk %lu at %s\n",
  4231. current_in_disk, zip_fzofft(current_in_offset - 4, NULL, "u"));
  4232. }
  4233. /* The format of a central directory record
  4234. central file header signature 4 bytes (0x02014b50)
  4235. version made by 2 bytes
  4236. version needed to extract 2 bytes
  4237. general purpose bit flag 2 bytes
  4238. compression method 2 bytes
  4239. last mod file time 2 bytes
  4240. last mod file date 2 bytes
  4241. crc-32 4 bytes
  4242. compressed size 4 bytes
  4243. uncompressed size 4 bytes
  4244. file name length 2 bytes
  4245. extra field length 2 bytes
  4246. file comment length 2 bytes
  4247. disk number start 2 bytes
  4248. internal file attributes 2 bytes
  4249. external file attributes 4 bytes
  4250. relative offset of local header 4 bytes
  4251. file name (variable size)
  4252. extra field (variable size)
  4253. file comment (variable size)
  4254. */
  4255. if (fread(scbuf, CENHEAD, 1, in_file) != 1) {
  4256. zipwarn("reading central directory: ", strerror(errno));
  4257. if (fix == 1) {
  4258. zipwarn("bad archive - error reading central directory", "");
  4259. zipwarn("skipping this entry...", "");
  4260. continue;
  4261. } else {
  4262. return ferror(in_file) ? ZE_READ : ZE_EOF;
  4263. }
  4264. }
  4265. if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
  4266. zipwarn("reading central directory", "");
  4267. return ZE_MEM;
  4268. }
  4269. z->vem = SH(CENVEM + scbuf);
  4270. z->ver = SH(CENVER + scbuf);
  4271. z->flg = SH(CENFLG + scbuf);
  4272. z->how = SH(CENHOW + scbuf);
  4273. z->tim = LG(CENTIM + scbuf); /* time and date into one long */
  4274. z->crc = LG(CENCRC + scbuf);
  4275. z->siz = LG(CENSIZ + scbuf);
  4276. z->len = LG(CENLEN + scbuf);
  4277. z->nam = SH(CENNAM + scbuf); /* used before comparing cen vs. loc */
  4278. z->cext = SH(CENEXT + scbuf); /* may be different from z->ext */
  4279. z->com = SH(CENCOM + scbuf);
  4280. z->dsk = SH(CENDSK + scbuf);
  4281. z->att = SH(CENATT + scbuf);
  4282. z->atx = LG(CENATX + scbuf);
  4283. z->off = LG(CENOFF + scbuf); /* adjust_offset is added below */
  4284. z->dosflag = (z->vem & 0xff00) == 0;
  4285. /* Initialize all fields pointing to malloced data to NULL */
  4286. z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
  4287. z->oname = NULL;
  4288. #ifdef UNICODE_SUPPORT
  4289. z->uname = z->zuname = z->ouname = NULL;
  4290. #endif
  4291. /* Read file name, extra field and comment field */
  4292. if (z->nam == 0)
  4293. {
  4294. sprintf(errbuf, "%lu", (ulg)zcount + 1);
  4295. zipwarn("zero-length name for entry #", errbuf);
  4296. if (fix == 1) {
  4297. zipwarn("skipping this entry...", "");
  4298. continue;
  4299. }
  4300. #ifndef DEBUG
  4301. return ZE_FORM;
  4302. #endif
  4303. }
  4304. if ((z->iname = malloc(z->nam+1)) == NULL ||
  4305. (z->cext && (z->cextra = malloc(z->cext)) == NULL) ||
  4306. (z->com && (z->comment = malloc(z->com)) == NULL))
  4307. return ZE_MEM;
  4308. if (fread(z->iname, z->nam, 1, in_file) != 1 ||
  4309. (z->cext && fread(z->cextra, z->cext, 1, in_file) != 1) ||
  4310. (z->com && fread(z->comment, z->com, 1, in_file) != 1)) {
  4311. if (fix == 1) {
  4312. zipwarn("error reading entry: ", strerror(errno));
  4313. zipwarn("skipping this entry...", "");
  4314. continue;
  4315. }
  4316. return ferror(in_file) ? ZE_READ : ZE_EOF;
  4317. }
  4318. z->iname[z->nam] = '\0'; /* terminate name */
  4319. #ifdef UNICODE_SUPPORT
  4320. if (unicode_mismatch != 3) {
  4321. if (z->flg & UTF8_BIT) {
  4322. char *iname;
  4323. /* path is UTF-8 */
  4324. if ((z->uname = malloc(strlen(z->iname) + 1)) == NULL) {
  4325. zipwarn("could not allocate memory: scanzipf_reg", "");
  4326. return ZE_MEM;
  4327. }
  4328. strcpy(z->uname, z->iname);
  4329. /* Create a local name. If UTF-8 system this should also be UTF-8 */
  4330. iname = utf8_to_local_string(z->uname);
  4331. if (iname) {
  4332. free(z->iname);
  4333. z->iname = iname;
  4334. }
  4335. else
  4336. zipwarn("illegal UTF-8 name: ", z->uname);
  4337. } else {
  4338. /* check for UTF-8 path extra field */
  4339. read_Unicode_Path_entry(z);
  4340. }
  4341. }
  4342. #endif
  4343. #ifdef WIN32
  4344. /* Input path may be OEM */
  4345. {
  4346. unsigned hostver = (z->vem & 0xff);
  4347. Ext_ASCII_TO_Native(z->iname, (z->vem >> 8), hostver,
  4348. ((z->atx & 0xffff0000L) != 0), FALSE);
  4349. }
  4350. #endif
  4351. #ifdef EBCDIC
  4352. if (z->com)
  4353. memtoebc(z->comment, z->comment, z->com);
  4354. #endif /* EBCDIC */
  4355. #ifdef WIN32
  4356. /* Comment may be OEM */
  4357. {
  4358. unsigned hostver = (z->vem & 0xff);
  4359. Ext_ASCII_TO_Native(z->comment, (z->vem >> 8), hostver,
  4360. ((z->atx & 0xffff0000L) != 0), FALSE);
  4361. }
  4362. #endif
  4363. #ifdef ZIP64_SUPPORT
  4364. /* zip64 support 08/31/2003 R.Nausedat */
  4365. /* here, we have to read the len, siz etc values from the CD */
  4366. /* entry as we might have to adjust them regarding their */
  4367. /* correspronding zip64 extra fields. */
  4368. /* also, we cannot compare the values from the CD entries with */
  4369. /* the values from the LH as they might be different. */
  4370. /* adjust/update siz,len and off (to come: dsk) entries */
  4371. /* PKZIP does not care of the version set in a CDH: if */
  4372. /* there is a zip64 extra field assigned to a CDH PKZIP */
  4373. /* uses it, we should do so, too. */
  4374. adjust_zip_central_entry(z);
  4375. #endif
  4376. /* if adjusting for sfx prefix, add the offset */
  4377. if ((fix ==1 && total_disks == 1) || adjust) z->off += adjust_offset;
  4378. /* Update zipbeg beginning of archive offset, prepare for next header */
  4379. if (z->dsk == 0 && (!zipbegset || z->off < zipbeg)) {
  4380. zipbeg = z->off;
  4381. zipbegset = 1;
  4382. }
  4383. zcount++;
  4384. /* Clear actions */
  4385. z->mark = 0;
  4386. z->trash = 0;
  4387. #if defined(UNICODE_SUPPORT) && !defined(UTIL)
  4388. z->zname = in2ex(z->iname); /* convert to external name */
  4389. if (z->zname == NULL)
  4390. return ZE_MEM;
  4391. if ((z->name = malloc(strlen(z->zname) + 1)) == NULL) {
  4392. zipwarn("could not allocate memory: scanzipf_reg", "");
  4393. return ZE_MEM;
  4394. }
  4395. strcpy(z->name, z->zname);
  4396. z->oname = local_to_display_string(z->iname);
  4397. # ifdef WIN32
  4398. z->namew = NULL;
  4399. z->inamew = NULL;
  4400. z->znamew = NULL;
  4401. # endif
  4402. if (unicode_mismatch != 3) {
  4403. if (z->uname) {
  4404. /* create zuname which is alternate zname for matching based on
  4405. converted Unicode name */
  4406. char *name;
  4407. /* Convert UTF-8 to current local character set */
  4408. name = utf8_to_local_string(z->uname);
  4409. if (name == NULL) {
  4410. /*
  4411. zipwarn("illegal UTF-8 name: ", z->uname);
  4412. */
  4413. /* not able to convert name, so use iname */
  4414. if ((name = malloc(strlen(z->iname) + 1)) == NULL) {
  4415. zipwarn("could not allocate memory: scanzipf_reg", "");
  4416. return ZE_MEM;
  4417. }
  4418. strcpy(name, z->iname);
  4419. }
  4420. # ifdef EBCDIC
  4421. /* z->zname is used for printing and must be coded in native charset */
  4422. strtoebc(z->zuname, name);
  4423. # else /* !EBCDIC */
  4424. if ((z->zuname = malloc(strlen(name) + 1)) == NULL) {
  4425. zipwarn("could not allocate memory: scanzipf_reg", "");
  4426. return ZE_MEM;
  4427. }
  4428. strcpy(z->zuname, name);
  4429. /* For output to terminal */
  4430. if (unicode_escape_all) {
  4431. char *ouname;
  4432. /* Escape anything not 7-bit ASCII */
  4433. ouname = utf8_to_escape_string(z->uname);
  4434. if (ouname)
  4435. z->ouname = ouname;
  4436. else {
  4437. if ((z->ouname = malloc(strlen(name) + 1)) == NULL) {
  4438. zipwarn("could not allocate memory: scanzipf_reg", "");
  4439. return ZE_MEM;
  4440. }
  4441. strcpy(z->ouname, name);
  4442. }
  4443. } else {
  4444. if ((z->ouname = malloc(strlen(name) + 1)) == NULL) {
  4445. zipwarn("could not allocate memory: scanzipf_reg", "");
  4446. return ZE_MEM;
  4447. }
  4448. strcpy(z->ouname, name);
  4449. }
  4450. # ifdef WIN32
  4451. if (!no_win32_wide) {
  4452. z->inamew = utf8_to_wchar_string(z->uname);
  4453. z->znamew = in2exw(z->inamew); /* convert to external name */
  4454. if (z->znamew == NULL)
  4455. return ZE_MEM;
  4456. }
  4457. local_to_oem_string(z->ouname, z->ouname);
  4458. /* For matching. There seems to be something lost
  4459. in the translation from displaying a name in a
  4460. console window using zip -su on Win32 and using
  4461. that name in a command line to match what's in
  4462. the archive. This is klugy though.
  4463. */
  4464. if ((z->wuname = malloc(strlen(z->ouname) + 1)) == NULL) {
  4465. zipwarn("could not allocate memory: scanzipf_reg", "");
  4466. return ZE_MEM;
  4467. }
  4468. strcpy(z->wuname, z->ouname);
  4469. oem_to_local_string(z->wuname, z->wuname);
  4470. # endif /* WIN32 */
  4471. # endif /* ?EBCDIC */
  4472. } else {
  4473. /* no uname */
  4474. # ifdef WIN32
  4475. if (!no_win32_wide) {
  4476. z->inamew = local_to_wchar_string(z->iname);
  4477. z->znamew = in2exw(z->inamew); /* convert to external name */
  4478. if (z->znamew == NULL)
  4479. return ZE_MEM;
  4480. }
  4481. # endif
  4482. }
  4483. }
  4484. #else /* !(UNICODE_SUPPORT && !UTIL) */
  4485. # ifdef UTIL
  4486. /* We only need z->iname in the utils */
  4487. z->name = z->iname;
  4488. # ifdef EBCDIC
  4489. /* z->zname is used for printing and must be coded in native charset */
  4490. if ((z->zname = malloc(z->nam+1)) == NULL) {
  4491. zipwarn("could not allocate memory: scanzipf_reg", "");
  4492. return ZE_MEM;
  4493. }
  4494. strtoebc(z->zname, z->iname);
  4495. # else
  4496. z->zname = z->iname;
  4497. # endif
  4498. # else /* !UTIL */
  4499. z->zname = in2ex(z->iname); /* convert to external name */
  4500. if (z->zname == NULL)
  4501. return ZE_MEM;
  4502. z->name = z->zname;
  4503. # endif /* ?UTIL */
  4504. if ((z->oname = malloc(strlen(z->zname) + 1)) == NULL) {
  4505. zipwarn("could not allocate memory: scanzipf_reg", "");
  4506. return ZE_MEM;
  4507. }
  4508. strcpy(z->oname, z->zname);
  4509. #endif /* ?(UNICODE_SUPPORT && !UTIL) */
  4510. #ifndef UTIL
  4511. if (verbose && fix == 0)
  4512. zipoddities(z);
  4513. #endif
  4514. /* Link into list */
  4515. *x = z;
  4516. z->nxt = NULL;
  4517. x = &z->nxt;
  4518. } /* while reading file */
  4519. /* close disk and do next disk */
  4520. fclose(in_file);
  4521. in_file = NULL;
  4522. free(split_path);
  4523. if (!is_signature(sigbuf, "PK\01\02")) {
  4524. /* if the last signature is not a CD signature and we get here then
  4525. hit either the Zip64 EOCDR or the EOCDR and done */
  4526. break;
  4527. }
  4528. } /* for each disk */
  4529. if (zcount != cd_total_entries) {
  4530. sprintf(errbuf, "expected %s entries but found %s",
  4531. zip_fzofft(cd_total_entries, NULL, "u"),
  4532. zip_fzofft(zcount, NULL, "u"));
  4533. zipwarn(errbuf, "");
  4534. return ZE_FORM;
  4535. }
  4536. return ZE_OK;
  4537. } /* end of function scanzipf_regnew() */
  4538. /* ---------------------- */
  4539. /*
  4540. * readzipfile initializes the global variables that hold the zipfile
  4541. * directory info and opens the zipfile. For the actual zipfile scan,
  4542. * the subroutine scanzipf_reg() or scanzipf_fix() is called,
  4543. * depending on the mode of operation (regular processing, or zipfix mode).
  4544. */
  4545. int readzipfile()
  4546. /*
  4547. The name of the zip file is pointed to by the global "zipfile".
  4548. The globals zipbeg, zfiles, zcount, and zcomlen are initialized.
  4549. Return an error code in the ZE_ class.
  4550. */
  4551. {
  4552. FILE *f; /* zip file */
  4553. int retval; /* return code */
  4554. int readable; /* 1 if zipfile exists and is readable */
  4555. /* Initialize zip file info */
  4556. zipbeg = 0;
  4557. zfiles = NULL; /* Points to first header */
  4558. zcount = 0; /* number of files */
  4559. zcomlen = 0; /* zip file comment length */
  4560. retval = ZE_OK;
  4561. f = NULL; /* shut up some compilers */
  4562. zipfile_exists = 0;
  4563. /* If zip file exists, read headers and check structure */
  4564. #ifdef VMS
  4565. if (zipfile == NULL || !(*zipfile) || !strcmp(zipfile, "-"))
  4566. return ZE_OK;
  4567. {
  4568. int rtype;
  4569. if ((VMSmunch(zipfile, GET_RTYPE, (char *)&rtype) == RMS$_NORMAL) &&
  4570. (rtype == FAT$C_VARIABLE)) {
  4571. fprintf(mesg,
  4572. "\n Error: zipfile is in variable-length record format. Please\n\
  4573. run \"bilf b %s\" to convert the zipfile to fixed-length\n\
  4574. record format.\n\n", zipfile);
  4575. return ZE_FORM;
  4576. }
  4577. }
  4578. readable = ((f = zfopen(zipfile, FOPR)) != NULL);
  4579. #else /* !VMS */
  4580. readable = (zipfile != NULL && *zipfile && strcmp(zipfile, "-"));
  4581. if (readable) {
  4582. readable = ((f = zfopen(zipfile, FOPR)) != NULL);
  4583. }
  4584. #endif /* ?VMS */
  4585. /* skip check if streaming */
  4586. if (!readable) {
  4587. if (!zip_to_stdout && fix != 2 && strcmp(in_path, out_path)) {
  4588. /* If -O used then in_path must exist */
  4589. if (fix == 1)
  4590. zipwarn("No .zip file found\n ",
  4591. "(If all you have are splits (.z01, .z02, ...) and no .zip, try -FF)");
  4592. ZIPERR(ZE_OPEN, zipfile);
  4593. }
  4594. } else {
  4595. zipfile_exists = 1;
  4596. }
  4597. #ifdef MVS
  4598. /* Very nasty special case for MVS. Just because the zipfile has been
  4599. * opened for reading does not mean that we can actually read the data.
  4600. * Typical JCL to create a zipfile is
  4601. *
  4602. * //ZIPFILE DD DISP=(NEW,CATLG),DSN=prefix.ZIP,
  4603. * // SPACE=(CYL,(10,10))
  4604. *
  4605. * That creates a VTOC entry with an end of file marker (DS1LSTAR) of zero.
  4606. * Alas the VTOC end of file marker is only used when the file is opened in
  4607. * append mode. When a file is opened in read mode, the "other" end of file
  4608. * marker is used, a zero length data block signals end of file when reading.
  4609. * With a brand new file which has not been written to yet, it is undefined
  4610. * what you read off the disk. In fact you read whatever data was in the same
  4611. * disk tracks before the zipfile was allocated. You would be amazed at the
  4612. * number of application programmers who still do not understand this. Makes
  4613. * for interesting and semi-random errors, GIGO.
  4614. *
  4615. * Newer versions of SMS will automatically write a zero length block when a
  4616. * file is allocated. However not all sites run SMS or they run older levels
  4617. * so we cannot rely on that. The only safe thing to do is close the file,
  4618. * open in append mode (we already know that the file exists), close it again,
  4619. * reopen in read mode and try to read a data block. Opening and closing in
  4620. * append mode will write a zero length block where DS1LSTAR points, making
  4621. * sure that the VTOC and internal end of file markers are in sync. Then it
  4622. * is safe to read data. If we cannot read one byte of data after all that,
  4623. * it is a brand new zipfile and must not be read.
  4624. */
  4625. if (readable)
  4626. {
  4627. char c;
  4628. fclose(f);
  4629. /* append mode */
  4630. if ((f = zfopen(zipfile, "ab")) == NULL) {
  4631. ZIPERR(ZE_OPEN, zipfile);
  4632. }
  4633. fclose(f);
  4634. /* read mode again */
  4635. if ((f = zfopen(zipfile, FOPR)) == NULL) {
  4636. ZIPERR(ZE_OPEN, zipfile);
  4637. }
  4638. if (fread(&c, 1, 1, f) != 1) {
  4639. /* no actual data */
  4640. readable = 0;
  4641. fclose(f);
  4642. }
  4643. else{
  4644. fseek(f, 0, SEEK_SET); /* at least one byte in zipfile, back to the start */
  4645. }
  4646. }
  4647. #endif /* MVS */
  4648. /* ------------------------ */
  4649. /* new file read */
  4650. #ifndef UTIL
  4651. if (fix == 2) {
  4652. scanzipf_fixnew();
  4653. }
  4654. else
  4655. #endif
  4656. if (readable)
  4657. {
  4658. /* close file as the new scan opens the splits as needed */
  4659. fclose(f);
  4660. # ifndef UTIL
  4661. retval = (fix == 2 && !adjust) ? scanzipf_fixnew() : scanzipf_regnew();
  4662. # else
  4663. retval = scanzipf_regnew();
  4664. # endif
  4665. }
  4666. if (fix != 2 && readable)
  4667. {
  4668. /* If one or more files, sort by name */
  4669. if (zcount)
  4670. {
  4671. struct zlist far * far *x; /* pointer into zsort array */
  4672. struct zlist far *z; /* pointer into zfiles linked list */
  4673. extent zl_size = zcount * sizeof(struct zlist far *);
  4674. if (zl_size / sizeof(struct zlist far *) != zcount ||
  4675. (x = zsort = (struct zlist far **)malloc(zl_size)) == NULL)
  4676. return ZE_MEM;
  4677. for (z = zfiles; z != NULL; z = z->nxt)
  4678. *x++ = z;
  4679. qsort((char *)zsort, zcount, sizeof(struct zlist far *), zqcmp);
  4680. #ifdef UNICODE_SUPPORT
  4681. /* sort by zuname (local conversion of UTF-8 name) */
  4682. if (zl_size / sizeof(struct zlist far *) != zcount ||
  4683. (x = zusort = (struct zlist far **)malloc(zl_size)) == NULL)
  4684. return ZE_MEM;
  4685. for (z = zfiles; z != NULL; z = z->nxt)
  4686. *x++ = z;
  4687. qsort((char *)zusort, zcount, sizeof(struct zlist far *), zuqcmp);
  4688. #endif
  4689. }
  4690. }
  4691. /* ------------------------ */
  4692. return retval;
  4693. } /* end of function readzipfile() */
  4694. int putlocal(z, rewrite)
  4695. struct zlist far *z; /* zip entry to write local header for */
  4696. int rewrite; /* did seek to rewrite */
  4697. /* Write a local header described by *z to file *f. Return an error code
  4698. in the ZE_ class. */
  4699. {
  4700. /* If any of compressed size (siz), uncompressed size (len), offset(off), or
  4701. disk number (dsk) is larger than can fit in the below standard fields then a
  4702. Zip64 flag value is stored and a Zip64 extra field is created.
  4703. Only siz and len are in the local header while all can be in the central
  4704. directory header.
  4705. For the local header if the extra field is created must store both
  4706. uncompressed and compressed sizes.
  4707. This assumes that for large entries the compressed size won't need a
  4708. Zip64 extra field if the uncompressed size did not. This assumption should
  4709. only fail for a large file of nearly totally uncompressable data.
  4710. If streaming stdin in and use_descriptors is set then always create a Zip64
  4711. extra field flagging the data descriptor as being in Zip64 format. This is
  4712. needed as don't know if need Zip64 or not when need to set Zip64 flag in
  4713. local header.
  4714. If rewrite is set then don't count bytes written for splits
  4715. */
  4716. char *block = NULL; /* mem block to write to */
  4717. extent offset = 0; /* offset into block */
  4718. extent blocksize = 0; /* size of block */
  4719. #ifdef UNICODE_SUPPORT
  4720. ush nam = z->nam; /* size of name to write to header */
  4721. int use_uname = 0; /* write uname to header */
  4722. #endif
  4723. #ifdef ZIP64_SUPPORT
  4724. int streaming_in = 0; /* streaming stdin */
  4725. int was_zip64 = 0;
  4726. /* If input is stdin then streaming stdin. No problem with that.
  4727. The problem is updating the local header data in the output once the sizes
  4728. and crc are known. If the output is not seekable, then need data descriptors
  4729. and also need to assume Zip64 will be needed as don't know yet. Even if the
  4730. output is seekable, if the input is streamed need to write the Zip64 extra field
  4731. before writing the data or there won't be room for it later if we need it.
  4732. */
  4733. streaming_in = (strcmp(z->name, "-") == 0);
  4734. if (!rewrite) {
  4735. zip64_entry = 0;
  4736. /* initial local header */
  4737. if (z->siz > ZIP_UWORD32_MAX || z->len > ZIP_UWORD32_MAX ||
  4738. force_zip64 == 1 || (force_zip64 != 0 && streaming_in))
  4739. {
  4740. /* assume Zip64 */
  4741. if (force_zip64 == 0) {
  4742. zipwarn("Entry too big:", z->oname);
  4743. ZIPERR(ZE_BIG, "Large entry support disabled with -fz- but needed");
  4744. }
  4745. zip64_entry = 1; /* header of this entry has a field needing Zip64 */
  4746. if (z->ver < ZIP64_MIN_VER)
  4747. z->ver = ZIP64_MIN_VER;
  4748. was_zip64 = 1;
  4749. }
  4750. } else {
  4751. /* rewrite */
  4752. was_zip64 = zip64_entry;
  4753. zip64_entry = 0;
  4754. if (z->siz > ZIP_UWORD32_MAX || z->len > ZIP_UWORD32_MAX ||
  4755. force_zip64 == 1 || (force_zip64 != 0 && streaming_in))
  4756. {
  4757. /* Zip64 entry */
  4758. zip64_entry = 1;
  4759. }
  4760. if (force_zip64 == 0 && zip64_entry) {
  4761. /* tried to force into standard entry but needed Zip64 entry */
  4762. zipwarn("Entry too big:", z->oname);
  4763. ZIPERR(ZE_BIG, "Large entry support disabled with -fz- but entry needs");
  4764. }
  4765. /* Normally for a large archive if the input file is less than 4 GB then
  4766. the compressed or stored version should be less than 4 GB. If this
  4767. assumption is wrong this catches it. This is a problem even if not
  4768. streaming as the Zip64 extra field was not written and now there's no
  4769. room for it. */
  4770. if (was_zip64 == 0 && zip64_entry == 1) {
  4771. /* guessed wrong and need Zip64 */
  4772. zipwarn("Entry too big:", z->oname);
  4773. if (force_zip64 == 0) {
  4774. ZIPERR(ZE_BIG, "Compressed/stored entry unexpectedly large - do not use -fz-");
  4775. } else {
  4776. ZIPERR(ZE_BIG, "Poor compression resulted in unexpectedly large entry - try -fz");
  4777. }
  4778. }
  4779. if (zip64_entry) {
  4780. /* Zip64 entry still */
  4781. /* this archive needs Zip64 (version 4.5 unzipper) */
  4782. zip64_archive = 1;
  4783. if (z->ver < ZIP64_MIN_VER)
  4784. z->ver = ZIP64_MIN_VER;
  4785. } else {
  4786. /* it turns out we do not need Zip64 */
  4787. zip64_entry = 0;
  4788. }
  4789. if (was_zip64 && zip64_entry != 1) {
  4790. z->ver = 20;
  4791. }
  4792. }
  4793. #endif /* ZIP64_SUPPORT */
  4794. /* Instead of writing to the file as we go, to do splits we have to write it
  4795. to memory and see if it will fit before writing the entire local header.
  4796. If the local header doesn't fit we need to save it for the next disk.
  4797. */
  4798. #ifdef ZIP64_SUPPORT
  4799. if (zip64_entry || was_zip64)
  4800. /* update extra field */
  4801. add_local_zip64_extra_field( z );
  4802. #endif /* ZIP64_SUPPORT */
  4803. #ifdef UNICODE_SUPPORT
  4804. # if 0
  4805. /* if UTF-8 bit is set on an existing entry, assume it should be */
  4806. /* clear the UTF-8 flag */
  4807. z->flg &= ~UTF8_BIT;
  4808. z->lflg &= ~UTF8_BIT;
  4809. # endif
  4810. if (z->uname) {
  4811. /* need UTF-8 name */
  4812. if (utf8_force || using_utf8) {
  4813. z->lflg |= UTF8_BIT;
  4814. z->flg |= UTF8_BIT;
  4815. }
  4816. if (z->flg & UTF8_BIT) {
  4817. /* If this flag is set, then restore UTF-8 as path name */
  4818. use_uname = 1;
  4819. nam = strlen(z->uname);
  4820. } else {
  4821. /* use extra field */
  4822. add_Unicode_Path_local_extra_field(z);
  4823. }
  4824. } else {
  4825. /* clear UTF-8 bit as not needed */
  4826. z->flg &= ~UTF8_BIT;
  4827. z->lflg &= ~UTF8_BIT;
  4828. }
  4829. #endif
  4830. append_ulong_to_mem(LOCSIG, &block, &offset, &blocksize); /* local file header signature */
  4831. append_ushort_to_mem(z->ver, &block, &offset, &blocksize); /* version needed to extract */
  4832. append_ushort_to_mem(z->lflg, &block, &offset, &blocksize); /* general purpose bit flag */
  4833. append_ushort_to_mem(z->how, &block, &offset, &blocksize); /* compression method */
  4834. append_ulong_to_mem(z->tim, &block, &offset, &blocksize); /* last mod file date time */
  4835. append_ulong_to_mem(z->crc, &block, &offset, &blocksize); /* crc-32 */
  4836. #ifdef ZIP64_SUPPORT /* zip64 support 09/02/2003 R.Nausedat */
  4837. /* changes 10/5/03 EG */
  4838. if (zip64_entry) {
  4839. append_ulong_to_mem(0xFFFFFFFF, &block, &offset, &blocksize); /* compressed size */
  4840. append_ulong_to_mem(0xFFFFFFFF, &block, &offset, &blocksize); /* uncompressed size */
  4841. } else {
  4842. append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize);/* compressed size */
  4843. append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize);/* uncompressed size */
  4844. }
  4845. #else
  4846. append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize); /* compressed size */
  4847. append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize); /* uncompressed size */
  4848. #endif
  4849. #ifdef UNICODE_SUPPORT
  4850. append_ushort_to_mem(nam, &block, &offset, &blocksize); /* file name length */
  4851. #else
  4852. append_ushort_to_mem(z->nam, &block, &offset, &blocksize); /* file name length */
  4853. #endif
  4854. append_ushort_to_mem(z->ext, &block, &offset, &blocksize); /* extra field length */
  4855. #ifdef UNICODE_SUPPORT
  4856. if (use_uname) {
  4857. /* path is UTF-8 */
  4858. append_string_to_mem(z->uname, nam, &block, &offset, &blocksize);
  4859. } else
  4860. #endif
  4861. #ifdef WIN32_OEM
  4862. /* store name in OEM character set in archive */
  4863. if ((z->vem & 0xff00) == 0)
  4864. {
  4865. char *oem;
  4866. if ((oem = malloc(strlen(z->iname) + 1)) == NULL)
  4867. ZIPERR(ZE_MEM, "putlocal oem");
  4868. INTERN_TO_OEM(z->iname, oem);
  4869. append_string_to_mem(oem, z->nam, &block, &offset, &blocksize); /* file name */
  4870. free(oem);
  4871. } else {
  4872. append_string_to_mem(z->iname, z->nam, &block, &offset, &blocksize); /* file name */
  4873. }
  4874. #else
  4875. append_string_to_mem(z->iname, z->nam, &block, &offset, &blocksize); /* file name */
  4876. #endif
  4877. if (z->ext) {
  4878. append_string_to_mem(z->extra, z->ext, &block, &offset, &blocksize); /* extra field */
  4879. }
  4880. /* write the header */
  4881. if (rewrite == PUTLOCAL_REWRITE) {
  4882. /* use fwrite as seeked back and not extending the archive */
  4883. /* also if split_method 1 write to file with local header */
  4884. if (split_method == 1) {
  4885. if (fwrite(block, 1, offset, current_local_file) != offset) {
  4886. free(block);
  4887. return ZE_TEMP;
  4888. }
  4889. /* now can close the split if local header on previous split */
  4890. if (current_local_disk != current_disk) {
  4891. close_split(current_local_disk, current_local_file, current_local_tempname);
  4892. current_local_file = NULL;
  4893. free(current_local_tempname);
  4894. }
  4895. } else {
  4896. /* not doing splits */
  4897. if (fwrite(block, 1, offset, y) != offset) {
  4898. free(block);
  4899. return ZE_TEMP;
  4900. }
  4901. }
  4902. } else {
  4903. /* do same if archive not split or split_method 2 with descriptors */
  4904. /* use bfwrite which counts bytes for splits */
  4905. if (bfwrite(block, 1, offset, BFWRITE_LOCALHEADER) != offset) {
  4906. free(block);
  4907. return ZE_TEMP;
  4908. }
  4909. }
  4910. free(block);
  4911. return ZE_OK;
  4912. }
  4913. int putextended(z)
  4914. struct zlist far *z; /* zip entry to write local header for */
  4915. /* This is the data descriptor.
  4916. * Write an extended local header described by *z to file *f.
  4917. * Return an error code in the ZE_ class. */
  4918. {
  4919. /* write to mem block then write to file 3/10/2005 */
  4920. char *block = NULL; /* mem block to write to */
  4921. extent offset = 0; /* offset into block */
  4922. extent blocksize = 0; /* size of block */
  4923. append_ulong_to_mem(EXTLOCSIG, &block, &offset, &blocksize); /* extended local signature */
  4924. append_ulong_to_mem(z->crc, &block, &offset, &blocksize); /* crc-32 */
  4925. #ifdef ZIP64_SUPPORT
  4926. if (zip64_entry) {
  4927. /* use Zip64 entries */
  4928. append_int64_to_mem(z->siz, &block, &offset, &blocksize); /* compressed size */
  4929. append_int64_to_mem(z->len, &block, &offset, &blocksize); /* uncompressed size */
  4930. /* This is rather klugy as the AppNote handles this poorly. Typically
  4931. we don't know at this point if we are writing a Zip64 archive or not,
  4932. unless a file has needed Zip64. This is particularly annoying here
  4933. when deciding the size of the data descriptor (extended local header)
  4934. fields as the appnote says the uncompressed and compressed sizes
  4935. should be 8 bytes if the archive is Zip64 and 4 bytes if not.
  4936. One interpretation is the version of the archive is determined from
  4937. the Version Needed To Extract field in the Zip64 End Of Central Directory
  4938. record and so either an archive should start as Zip64 and write all data
  4939. descriptors with 8-byte fields or store everything until all the files
  4940. are processed and then write everything to the archive as changing the
  4941. sizes of the data descriptors is messy and just not feasible when
  4942. streaming to standard output. This is not easily workable and others
  4943. use the different interpretation below.
  4944. This was the old thought:
  4945. We always write a standard data descriptor. If the file has a large
  4946. uncompressed or compressed size we set the field to the max field
  4947. value, which we are defining as flagging the field as having a Zip64
  4948. value that doesn't fit. As the CRC happens before the variable size
  4949. fields the CRC is still valid and can be used to check the file. We
  4950. always use deflate if streaming so signatures should not appear in
  4951. the data and all local header signatures should be valid, allowing a
  4952. streaming unzip to find entries by local header signatures, if max size
  4953. values in the data descriptor sizes ignore them, and extract the file and
  4954. check it using the CRC. If not streaming the central directory is available
  4955. so just use those values which are correct.
  4956. After discussions with other groups this is the current thinking:
  4957. Apparent industry interpretation for data descriptors:
  4958. Data descriptor size is determined for each entry. If the local header
  4959. version needed to extract is 45 or higher then the entry can use Zip64
  4960. data descriptors but more checking is needed. If Zip64 extra field is
  4961. present then assume data descriptor is Zip64 and local version needed
  4962. to extract should be 45 or higher. If standard data descriptor then
  4963. local size fields are set to 0 and correct sizes are in standard data descriptor.
  4964. If Zip64 data descriptor then local sizes are set to -1, Zip64 extra field
  4965. sizes are set to 0, and the correct sizes are in the Zip64 data descriptor.
  4966. So do this:
  4967. If an entry is standard and the archive is updatable then seek back and
  4968. update the local header. No change.
  4969. If an entry is zip64 and the archive is updatable assume the Zip64 extra
  4970. field was created and update it. No change.
  4971. If data descriptors are needed then assume the archive is Zip64. This is
  4972. a change and means if ZIP64_SUPPORT is enabled that any non-updatable archive
  4973. will be in Zip64 format and use Zip64 data descriptors. This should be
  4974. compatible with other zippers that depend on the current (though not perfect)
  4975. AppNote description.
  4976. If anyone has some ideas on this I'd like to hear them.
  4977. 3/20/05 EG
  4978. Only assume need Zip64 if the input size is unknown. If the input size is
  4979. known we can assume Zip64 if the input is larger than 4 GB and assume not
  4980. otherwise. If the output is seekable we still need to create the Zip64
  4981. extra field if the input size is unknown so we can seek back and update it.
  4982. 12/28/05 EG
  4983. Updated 5/21/06 EG
  4984. */
  4985. } else {
  4986. /* for encryption */
  4987. append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize); /* compressed size */
  4988. append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize); /* uncompressed size */
  4989. }
  4990. #else
  4991. append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize); /* compressed size */
  4992. append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize); /* uncompressed size */
  4993. #endif
  4994. /* write the header */
  4995. if (bfwrite(block, 1, offset, BFWRITE_HEADER) != offset) {
  4996. free(block);
  4997. return ZE_TEMP;
  4998. }
  4999. free(block);
  5000. return ZE_OK;
  5001. }
  5002. int putcentral(z)
  5003. struct zlist far *z; /* zip entry to write central header for */
  5004. /* Write a central header described by *z to file *f. Return an error code
  5005. in the ZE_ class. */
  5006. /* output now uses bfwrite which writes global y */
  5007. {
  5008. /* If any of compressed size (siz), uncompressed size (len), offset(off), or
  5009. disk number (dsk) is larger than can fit in the below standard fields then a
  5010. Zip64 flag value is stored and a Zip64 extra field is created.
  5011. Only siz and len are in the local header while all are in the central directory
  5012. header.
  5013. For the central directory header just store the fields required. All previous fields
  5014. must be stored though. So can store none (no extra field), just uncompressed size
  5015. (len), len then siz, len then siz then off, or len then siz then off then dsk, in
  5016. those orders. 10/6/03 EG
  5017. */
  5018. /* write to mem block then write to file 3/10/2005 EG */
  5019. char *block = NULL; /* mem block to write to */
  5020. extent offset = 0; /* offset into block */
  5021. extent blocksize = 0; /* size of block */
  5022. uzoff_t off = 0; /* offset to start of local header */
  5023. ush nam = z->nam; /* size of name to write to header */
  5024. #ifdef UNICODE_SUPPORT
  5025. int use_uname = 0; /* write uname to header */
  5026. #endif
  5027. #ifdef ZIP64_SUPPORT /* zip64 support 09/02/2003 R.Nausedat */
  5028. int iRes;
  5029. #endif
  5030. #ifdef UNICODE_SUPPORT
  5031. if (z->uname) {
  5032. if (utf8_force) {
  5033. z->flg |= UTF8_BIT;
  5034. }
  5035. if (z->flg & UTF8_BIT) {
  5036. /* If this flag is set, then restore UTF-8 as path name */
  5037. use_uname = 1;
  5038. nam = strlen(z->uname);
  5039. } else {
  5040. add_Unicode_Path_cen_extra_field(z);
  5041. }
  5042. } else {
  5043. /* clear UTF-8 bit as not needed */
  5044. z->flg &= ~UTF8_BIT;
  5045. z->lflg &= ~UTF8_BIT;
  5046. }
  5047. #endif
  5048. off = z->off;
  5049. #ifdef ZIP64_SUPPORT /* zip64 support 09/02/2003 R.Nausedat */
  5050. if (z->siz > ZIP_UWORD32_MAX || z->len > ZIP_UWORD32_MAX ||
  5051. z->off > ZIP_UWORD32_MAX || z->dsk > ZIP_UWORD16_MAX || (force_zip64 == 1))
  5052. {
  5053. iRes = add_central_zip64_extra_field(z);
  5054. if( iRes != ZE_OK )
  5055. return iRes;
  5056. }
  5057. append_ulong_to_mem(CENSIG, &block, &offset, &blocksize); /* central file header signature */
  5058. append_ushort_to_mem(z->vem, &block, &offset, &blocksize); /* version made by */
  5059. append_ushort_to_mem(z->ver, &block, &offset, &blocksize); /* version needed to extract */
  5060. append_ushort_to_mem(z->flg, &block, &offset, &blocksize); /* general purpose bit flag */
  5061. append_ushort_to_mem(z->how, &block, &offset, &blocksize); /* compression method */
  5062. append_ulong_to_mem(z->tim, &block, &offset, &blocksize); /* last mod file date time */
  5063. append_ulong_to_mem(z->crc, &block, &offset, &blocksize); /* crc-32 */
  5064. if (z->siz > ZIP_UWORD32_MAX)
  5065. {
  5066. /* instead of z->siz */
  5067. append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize); /* compressed size */
  5068. }
  5069. else
  5070. {
  5071. append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize); /* compressed size */
  5072. }
  5073. /* if forcing Zip64 just force first ef field */
  5074. if (z->len > ZIP_UWORD32_MAX || (force_zip64 == 1))
  5075. {
  5076. /* instead of z->len */
  5077. append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize); /* uncompressed size */
  5078. }
  5079. else
  5080. {
  5081. append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize); /* uncompressed size */
  5082. }
  5083. append_ushort_to_mem(nam, &block, &offset, &blocksize); /* file name length */
  5084. append_ushort_to_mem(z->cext, &block, &offset, &blocksize); /* extra field length */
  5085. append_ushort_to_mem(z->com, &block, &offset, &blocksize); /* file comment length */
  5086. if (z->dsk > ZIP_UWORD16_MAX)
  5087. {
  5088. /* instead of z->dsk */
  5089. append_ushort_to_mem((ush)ZIP_UWORD16_MAX, &block, &offset, &blocksize); /* Zip64 flag */
  5090. }
  5091. else
  5092. {
  5093. append_ushort_to_mem((ush)z->dsk, &block, &offset, &blocksize); /* disk number start */
  5094. }
  5095. append_ushort_to_mem(z->att, &block, &offset, &blocksize); /* internal file attributes */
  5096. append_ulong_to_mem(z->atx, &block, &offset, &blocksize); /* external file attributes */
  5097. if (off > ZIP_UWORD32_MAX)
  5098. {
  5099. /* instead of z->off */
  5100. append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize); /* Zip64 flag */
  5101. }
  5102. else
  5103. {
  5104. append_ulong_to_mem((ulg)off, &block, &offset, &blocksize); /* offset of local header */
  5105. }
  5106. #else /* !ZIP64_SUPPORT */
  5107. append_ulong_to_mem(CENSIG, &block, &offset, &blocksize); /* central file header signature */
  5108. append_ushort_to_mem(z->vem, &block, &offset, &blocksize); /* version made by */
  5109. append_ushort_to_mem(z->ver, &block, &offset, &blocksize); /* version needed to extract */
  5110. append_ushort_to_mem(z->flg, &block, &offset, &blocksize); /* general purpose bit flag */
  5111. append_ushort_to_mem(z->how, &block, &offset, &blocksize); /* compression method */
  5112. append_ulong_to_mem(z->tim, &block, &offset, &blocksize); /* last mod file date time */
  5113. append_ulong_to_mem(z->crc, &block, &offset, &blocksize); /* crc-32 */
  5114. append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize); /* compressed size */
  5115. append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize); /* uncompressed size */
  5116. append_ushort_to_mem(nam, &block, &offset, &blocksize); /* file name length */
  5117. append_ushort_to_mem(z->cext, &block, &offset, &blocksize); /* extra field length */
  5118. append_ushort_to_mem(z->com, &block, &offset, &blocksize); /* file comment length */
  5119. append_ushort_to_mem((ush)z->dsk, &block, &offset, &blocksize); /* disk number start */
  5120. append_ushort_to_mem(z->att, &block, &offset, &blocksize); /* internal file attributes */
  5121. append_ulong_to_mem(z->atx, &block, &offset, &blocksize); /* external file attributes */
  5122. append_ulong_to_mem((ulg)off, &block, &offset, &blocksize); /* relative offset of local header */
  5123. #endif /* ZIP64_SUPPORT */
  5124. #ifdef EBCDIC
  5125. if (z->com)
  5126. memtoasc(z->comment, z->comment, z->com);
  5127. #endif /* EBCDIC */
  5128. #ifdef UNICODE_SUPPORT
  5129. if (use_uname) {
  5130. /* path is UTF-8 */
  5131. append_string_to_mem(z->uname, nam, &block, &offset, &blocksize);
  5132. } else
  5133. #endif
  5134. #ifdef WIN32_OEM
  5135. /* store name in OEM character set in archive */
  5136. if ((z->vem & 0xff00) == 0)
  5137. {
  5138. char *oem;
  5139. if ((oem = malloc(strlen(z->iname) + 1)) == NULL)
  5140. ZIPERR(ZE_MEM, "putcentral oem");
  5141. INTERN_TO_OEM(z->iname, oem);
  5142. append_string_to_mem(oem, z->nam, &block, &offset, &blocksize);
  5143. free(oem);
  5144. } else {
  5145. append_string_to_mem(z->iname, z->nam, &block, &offset, &blocksize);
  5146. }
  5147. #else
  5148. append_string_to_mem(z->iname, z->nam, &block, &offset, &blocksize);
  5149. #endif
  5150. if (z->cext) {
  5151. append_string_to_mem(z->cextra, z->cext, &block, &offset, &blocksize);
  5152. }
  5153. if (z->com) {
  5154. #ifdef WIN32_OEM
  5155. /* store comment in OEM character set in archive */
  5156. if ((z->vem & 0xff00) == 0)
  5157. {
  5158. char *oem;
  5159. if ((oem = malloc(strlen(z->comment) + 1)) == NULL)
  5160. ZIPERR(ZE_MEM, "putcentral oem comment");
  5161. INTERN_TO_OEM(z->comment, oem);
  5162. append_string_to_mem(oem, z->com, &block, &offset, &blocksize);
  5163. free(oem);
  5164. } else {
  5165. append_string_to_mem(z->comment, z->com, &block, &offset, &blocksize);
  5166. }
  5167. #else
  5168. append_string_to_mem(z->comment, z->com, &block, &offset, &blocksize);
  5169. #endif
  5170. }
  5171. /* write the header */
  5172. if (bfwrite(block, 1, offset, BFWRITE_CENTRALHEADER) != offset) {
  5173. free(block);
  5174. return ZE_TEMP;
  5175. }
  5176. free(block);
  5177. return ZE_OK;
  5178. }
  5179. /* Write the end of central directory data to file y. Return an error code
  5180. in the ZE_ class. */
  5181. int putend( OFT( uzoff_t) n,
  5182. OFT( uzoff_t) s,
  5183. OFT( uzoff_t) c,
  5184. OFT( extent) m,
  5185. OFT( char *) z
  5186. )
  5187. #ifdef NO_PROTO
  5188. uzoff_t n; /* number of entries in central directory */
  5189. uzoff_t s; /* size of central directory */
  5190. uzoff_t c; /* offset of central directory */
  5191. extent m; /* length of zip file comment (0 if none) */
  5192. char *z; /* zip file comment if m != 0 */
  5193. #endif /* def NO_PROTO */
  5194. {
  5195. #ifdef ZIP64_SUPPORT /* zip64 support 09/05/2003 R.Nausedat */
  5196. ush vem; /* version made by */
  5197. int iNeedZip64 = 0;
  5198. char *block = NULL; /* mem block to write to */
  5199. extent offset = 0; /* offset into block */
  5200. extent blocksize = 0; /* size of block */
  5201. /* we have to create a zip64 archive if we have more than 64k - 1 entries, */
  5202. /* if the CD is > 4 GB or if the offset to the CD > 4 GB. even if the CD start */
  5203. /* is < 4 GB and CD start + CD size > 4GB we do not need a zip64 archive since */
  5204. /* the offset entry in the CD tail is still valid. [note that there are other */
  5205. /* reasons for needing a Zip64 archive though, such as an uncompressed */
  5206. /* size > 4 GB for an entry but the entry compresses below 4 GB, so the archive */
  5207. /* is Zip64 but the CD does not need Zip64.] */
  5208. /* order of the zip/zip64 records in a zip64 archive: */
  5209. /* central directory */
  5210. /* zip64 end of central directory record */
  5211. /* zip64 end of central directory locator */
  5212. /* end of central directory record */
  5213. /* check zip64_archive instead of force_zip64 3/19/05 */
  5214. zip64_eocd_disk = current_disk;
  5215. zip64_eocd_offset = bytes_this_split;
  5216. if( n > ZIP_UWORD16_MAX || s > ZIP_UWORD32_MAX || c > ZIP_UWORD32_MAX ||
  5217. zip64_archive )
  5218. {
  5219. ++iNeedZip64;
  5220. /* write zip64 central dir tail: */
  5221. /* */
  5222. /* 4 bytes zip64 end of central dir signature (0x06064b50) */
  5223. append_ulong_to_mem((ulg)ZIP64_CENTRAL_DIR_TAIL_SIG, &block, &offset, &blocksize);
  5224. /* 8 bytes size of zip64 end of central directory record */
  5225. /* a fixed size unless the end zip64 extensible data sector is used. - 3/19/05 EG */
  5226. /* also note that AppNote 6.2 creates version 2 of this record for
  5227. central directory encryption - 3/19/05 EG */
  5228. append_int64_to_mem((zoff_t)ZIP64_CENTRAL_DIR_TAIL_SIZE, &block, &offset, &blocksize);
  5229. /* 2 bytes version made by */
  5230. vem = OS_CODE + Z_MAJORVER * 10 + Z_MINORVER;
  5231. append_ushort_to_mem(vem, &block, &offset, &blocksize);
  5232. /* APPNOTE says that zip64 archives should have at least version 4.5
  5233. in the "version needed to extract" field */
  5234. /* 2 bytes version needed to extract */
  5235. append_ushort_to_mem(ZIP64_MIN_VER, &block, &offset, &blocksize);
  5236. /* 4 bytes number of this disk */
  5237. append_ulong_to_mem(current_disk, &block, &offset, &blocksize);
  5238. /* 4 bytes number of the disk with the start of the central directory */
  5239. append_ulong_to_mem(cd_start_disk, &block, &offset, &blocksize);
  5240. /* 8 bytes total number of entries in the central directory on this disk */
  5241. append_int64_to_mem(cd_entries_this_disk, &block, &offset, &blocksize);
  5242. /* 8 bytes total number of entries in the central directory */
  5243. append_int64_to_mem(n, &block, &offset, &blocksize);
  5244. /* 8 bytes size of the central directory */
  5245. append_int64_to_mem(s, &block, &offset, &blocksize);
  5246. /* 8 bytes offset of start of central directory with respect to the starting disk number */
  5247. append_int64_to_mem(cd_start_offset, &block, &offset, &blocksize);
  5248. /* zip64 extensible data sector (variable size), we don't use it... */
  5249. /* write zip64 end of central directory locator: */
  5250. /* */
  5251. /* 4 bytes zip64 end of central dir locator signature (0x07064b50) */
  5252. append_ulong_to_mem(ZIP64_CENTRAL_DIR_TAIL_END_SIG, &block, &offset, &blocksize);
  5253. /* 4 bytes number of the disk with the start of the zip64 end of central directory */
  5254. append_ulong_to_mem(zip64_eocd_disk, &block, &offset, &blocksize);
  5255. /* 8 bytes relative offset of the zip64 end of central directory record, that is */
  5256. /* offset of CD + CD size */
  5257. append_int64_to_mem(zip64_eocd_offset, &block, &offset, &blocksize);
  5258. /* PUTLLG(l64Temp, f); */
  5259. /* 4 bytes total number of disks */
  5260. append_ulong_to_mem(current_disk + 1, &block, &offset, &blocksize);
  5261. }
  5262. /* end of central dir signature */
  5263. append_ulong_to_mem(ENDSIG, &block, &offset, &blocksize);
  5264. /* mv archives to come :) */
  5265. /* for now use n for all */
  5266. /* 2 bytes number of this disk */
  5267. if (current_disk < 0xFFFF)
  5268. append_ushort_to_mem((ush)current_disk, &block, &offset, &blocksize);
  5269. else
  5270. append_ushort_to_mem((ush)0xFFFF, &block, &offset, &blocksize);
  5271. /* 2 bytes number of the disk with the start of the central directory */
  5272. if (cd_start_disk == (ulg)-1)
  5273. cd_start_disk = 0;
  5274. if (cd_start_disk < 0xFFFF)
  5275. append_ushort_to_mem((ush)cd_start_disk, &block, &offset, &blocksize);
  5276. else
  5277. append_ushort_to_mem((ush)0xFFFF, &block, &offset, &blocksize);
  5278. /* 2 bytes total number of entries in the central directory on this disk */
  5279. if (cd_entries_this_disk < 0xFFFF)
  5280. append_ushort_to_mem((ush)cd_entries_this_disk, &block, &offset, &blocksize);
  5281. else
  5282. append_ushort_to_mem((ush)0xFFFF, &block, &offset, &blocksize);
  5283. /* 2 bytes total number of entries in the central directory */
  5284. if (total_cd_entries < 0xFFFF)
  5285. append_ushort_to_mem((ush)total_cd_entries, &block, &offset, &blocksize);
  5286. else
  5287. append_ushort_to_mem((ush)0xFFFF, &block, &offset, &blocksize);
  5288. if( s > ZIP_UWORD32_MAX )
  5289. /* instead of s */
  5290. append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize);
  5291. else
  5292. /* 4 bytes size of the central directory */
  5293. append_ulong_to_mem((ulg)s, &block, &offset, &blocksize);
  5294. if(force_zip64 == 1 || cd_start_offset > ZIP_UWORD32_MAX)
  5295. /* instead of cd_start_offset */
  5296. append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize);
  5297. else
  5298. /* 4 bytes offset of start of central directory with respect to the starting disk number */
  5299. append_ulong_to_mem((ulg)cd_start_offset, &block, &offset, &blocksize);
  5300. #else /* !ZIP64_SUPPORT */
  5301. char *block = NULL; /* mem block to write to */
  5302. extent offset = 0; /* offset into block */
  5303. extent blocksize = 0; /* size of block */
  5304. /* end of central dir signature */
  5305. append_ulong_to_mem(ENDSIG, &block, &offset, &blocksize);
  5306. /* 2 bytes number of this disk */
  5307. append_ushort_to_mem((ush)current_disk, &block, &offset, &blocksize);
  5308. /* 2 bytes number of the disk with the start of the central directory */
  5309. append_ushort_to_mem((ush)cd_start_disk, &block, &offset, &blocksize);
  5310. /* 2 bytes total number of entries in the central directory on this disk */
  5311. append_ushort_to_mem((ush)cd_entries_this_disk, &block, &offset, &blocksize);
  5312. /* 2 bytes total number of entries in the central directory */
  5313. append_ushort_to_mem((ush)n, &block, &offset, &blocksize);
  5314. /* 4 bytes size of the central directory */
  5315. append_ulong_to_mem((ulg)s, &block, &offset, &blocksize);
  5316. /* 4 bytes offset of start of central directory with respect to the starting disk number */
  5317. append_ulong_to_mem((ulg)cd_start_offset, &block, &offset, &blocksize);
  5318. #endif /* ZIP64_SUPPORT */
  5319. /* size of comment */
  5320. append_ushort_to_mem((ush)m, &block, &offset, &blocksize);
  5321. /* Write the comment, if any */
  5322. #ifdef EBCDIC
  5323. memtoasc(z, z, m);
  5324. #endif
  5325. if (m) {
  5326. /* PKWare defines the archive comment to be ASCII only so no OEM conversion */
  5327. append_string_to_mem(z, m, &block, &offset, &blocksize);
  5328. }
  5329. /* write the block */
  5330. if (bfwrite(block, 1, offset, BFWRITE_HEADER) != offset) {
  5331. free(block);
  5332. return ZE_TEMP;
  5333. }
  5334. free(block);
  5335. #ifdef HANDLE_AMIGA_SFX
  5336. if (amiga_sfx_offset && zipbeg /* -J zeroes this */) {
  5337. s = zftello(y);
  5338. while (s & 3) s++, putc(0, f); /* final marker must be longword aligned */
  5339. PUTLG(0xF2030000 /* 1010 in Motorola byte order */, f);
  5340. c = (s - amiga_sfx_offset - 4) / 4; /* size of archive part in longwords */
  5341. if (zfseeko(y, amiga_sfx_offset, SEEK_SET) != 0)
  5342. return ZE_TEMP;
  5343. c = ((c >> 24) & 0xFF) | ((c >> 8) & 0xFF00)
  5344. | ((c & 0xFF00) << 8) | ((c & 0xFF) << 24); /* invert byte order */
  5345. PUTLG(c, y);
  5346. zfseeko(y, 0, SEEK_END); /* just in case */
  5347. }
  5348. #endif
  5349. return ZE_OK;
  5350. } /* end function putend() */
  5351. /* Note: a zip "entry" includes a local header (which includes the file
  5352. name), an encryption header if encrypting, the compressed data
  5353. and possibly an extended local header. */
  5354. int zipcopy(z)
  5355. struct zlist far *z; /* zip entry to copy */
  5356. /* Copy the zip entry described by *z from in_file to y. Return an
  5357. error code in the ZE_ class. Also update tempzn by the number of bytes
  5358. copied. */
  5359. /* Now copies to global output file y */
  5360. /* Handle entries that span disks */
  5361. /* If fix == 2, assume in_file is pointing to a local header and fill
  5362. in z from local header */
  5363. {
  5364. uzoff_t n; /* holds local header offset */
  5365. ulg e = 0; /* extended local header size */
  5366. ulg start_disk = 0;
  5367. uzoff_t start_offset = 0;
  5368. char *split_path;
  5369. char buf[LOCHEAD + 1];
  5370. struct zlist far *localz;
  5371. int r;
  5372. Trace((stderr, "zipcopy %s\n", z->zname));
  5373. /* if fix == 2 assume in_file open and pointing at local header */
  5374. if (fix != 2) {
  5375. start_disk = z->dsk;
  5376. start_offset = z->off;
  5377. /* don't assume reading the right disk */
  5378. /* if start not on current disk then close current disk */
  5379. if (start_disk != current_in_disk) {
  5380. if (in_file) {
  5381. fclose(in_file);
  5382. in_file = NULL;
  5383. }
  5384. }
  5385. current_in_disk = start_disk;
  5386. /* disks are archive.z01, archive.z02, ..., archive.zip */
  5387. split_path = get_in_split_path(in_path, current_in_disk);
  5388. if (in_file == NULL) {
  5389. while ((in_file = zfopen(split_path, FOPR)) == NULL) {
  5390. /* could not open split */
  5391. if (!noisy) {
  5392. ZIPERR(ZE_OPEN, split_path);
  5393. }
  5394. /* Ask for directory with split. Updates global in_path */
  5395. r = ask_for_split_read_path(start_disk);
  5396. if (r == ZE_ABORT) {
  5397. /* user abort */
  5398. return ZE_ABORT;
  5399. } else if ((fix == 1 || fix == 2) && r == ZE_FORM) {
  5400. /* user asks to skip this disk */
  5401. return ZE_FORM;
  5402. }
  5403. free(split_path);
  5404. split_path = get_in_split_path(in_path, start_disk);
  5405. }
  5406. }
  5407. if (zfseeko(in_file, start_offset, SEEK_SET) != 0) {
  5408. fclose(in_file);
  5409. in_file = NULL;
  5410. zipwarn("reading archive fseek: ", strerror(errno));
  5411. return ZE_READ;
  5412. }
  5413. } /* fix != 2 */
  5414. if (fix != 2 && !at_signature(in_file, "PK\03\04")) {
  5415. fclose(in_file);
  5416. in_file = NULL;
  5417. zipwarn("Did not find entry for ", z->iname);
  5418. return ZE_FORM;
  5419. }
  5420. /* read local header */
  5421. if (fread(buf, LOCHEAD, 1, in_file) != 1) {
  5422. int f = ferror(in_file);
  5423. zipwarn("reading local entry: ", strerror(errno));
  5424. if (fix != 2)
  5425. fclose(in_file);
  5426. return f ? ZE_READ : ZE_EOF;
  5427. }
  5428. /* Local Header
  5429. local file header signature 4 bytes (0x04034b50)
  5430. version needed to extract 2 bytes
  5431. general purpose bit flag 2 bytes
  5432. compression method 2 bytes
  5433. last mod file time 2 bytes
  5434. last mod file date 2 bytes
  5435. crc-32 4 bytes
  5436. compressed size 4 bytes
  5437. uncompressed size 4 bytes
  5438. file name length 2 bytes
  5439. extra field length 2 bytes
  5440. file name (variable size)
  5441. extra field (variable size)
  5442. */
  5443. if ((localz = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
  5444. zipwarn("reading entry", "");
  5445. if (fix != 2)
  5446. fclose(in_file);
  5447. return ZE_MEM;
  5448. }
  5449. localz->ver = SH(LOCVER + buf);
  5450. localz->lflg = SH(LOCFLG + buf);
  5451. localz->how = SH(LOCHOW + buf);
  5452. localz->tim = LG(LOCTIM + buf); /* time and date into one long */
  5453. localz->crc = LG(LOCCRC + buf);
  5454. localz->nam = SH(LOCNAM + buf);
  5455. localz->ext = SH(LOCEXT + buf);
  5456. if (fix == 2) {
  5457. localz->siz = LG(LOCSIZ + buf);
  5458. localz->len = LG(LOCLEN + buf);
  5459. }
  5460. if (fix == 2) {
  5461. /* Do some sanity checks to make reasonably sure this is a local header */
  5462. ush os = localz->ver >> 8;
  5463. ush pkver = localz->ver - os;
  5464. /* OS - currently 0 - 18 (AppNote 6.3) and 30 (ATHEOS) */
  5465. if (os > 40) {
  5466. sprintf(errbuf, "Illegal host system mapping in local header: %d", os);
  5467. zipwarn(errbuf, "");
  5468. zipwarn("Skipping: ", z->iname);
  5469. return ZE_FORM;
  5470. }
  5471. /* PK Version - currently 10 - 62 (AppNote 6.2.2) */
  5472. /* If PKZip central directory encryption is used (62), the local header
  5473. values could be masked values. Specifically, as of AppNote 6.2.2
  5474. the time, crc-32, and uncompressed file size are masked and the
  5475. file name is also replaced with a hex entry count. Should
  5476. still be able to recover the entries, but they may be unreadable
  5477. without the 62 support fields. */
  5478. if (pkver > 100) {
  5479. sprintf(errbuf, "Illegal PK version mapping in local header: %d", pkver);
  5480. zipwarn(errbuf, "");
  5481. zipwarn("Skipping: ", z->iname);
  5482. return ZE_FORM;
  5483. }
  5484. /* Currently compression method is defined as 0 - 19 and 98 (AppNote 6.3) */
  5485. /* We can still copy an entry we can't read, but something over 200 is
  5486. probably illegal */
  5487. if (localz->how > 200) {
  5488. sprintf(errbuf, "Unrecognized compression method in local header: %d", localz->how);
  5489. zipwarn(errbuf, "");
  5490. zipwarn("Skipping: ", z->iname);
  5491. return ZE_FORM;
  5492. }
  5493. /* It's hard to make guesses on the other fields. Suggestions welcome. */
  5494. }
  5495. /* Initialize all fields pointing to malloced data to NULL */
  5496. localz->zname = localz->name = localz->iname = localz->extra = NULL;
  5497. localz->oname = NULL;
  5498. #ifdef UNICODE_SUPPORT
  5499. localz->uname = NULL;
  5500. #endif
  5501. /* Read file name, extra field and comment field */
  5502. if ((localz->iname = malloc(localz->nam+1)) == NULL ||
  5503. (localz->ext && (localz->extra = malloc(localz->ext)) == NULL))
  5504. return ZE_MEM;
  5505. if (fread(localz->iname, localz->nam, 1, in_file) != 1 ||
  5506. (localz->ext && fread(localz->extra, localz->ext, 1, in_file) != 1))
  5507. return ferror(in_file) ? ZE_READ : ZE_EOF;
  5508. localz->iname[localz->nam] = '\0'; /* terminate name */
  5509. if ((localz->name = malloc(localz->nam+1)) == NULL)
  5510. return ZE_MEM;
  5511. strcpy(localz->name, localz->iname);
  5512. #ifdef ZIP64_SUPPORT
  5513. zip64_entry = adjust_zip_local_entry(localz);
  5514. #endif
  5515. localz->vem = 0;
  5516. if (fix != 2) {
  5517. /* Need vem to determine if iname is Win32 OEM name */
  5518. localz->vem = z->vem;
  5519. #ifdef UNICODE_SUPPORT
  5520. if (unicode_mismatch != 3) {
  5521. if (z->flg & UTF8_BIT) {
  5522. char *iname;
  5523. /* path is UTF-8 */
  5524. localz->uname = localz->iname;
  5525. iname = utf8_to_local_string(localz->uname);
  5526. if (iname == NULL) {
  5527. /* a bad UTF-8 character in name likely - go with (probably messed up) uname */
  5528. if ((localz->iname = malloc(strlen(localz->uname) + 1)) == NULL) {
  5529. return ZE_MEM;
  5530. }
  5531. strcpy(localz->iname, localz->uname);
  5532. } else {
  5533. /* go with local character set iname */
  5534. localz->iname = iname;
  5535. }
  5536. } else {
  5537. /* check for UTF-8 path extra field */
  5538. read_Unicode_Path_local_entry(localz);
  5539. }
  5540. }
  5541. #endif
  5542. #ifdef WIN32_OEM
  5543. /* If fix == 2 and reading local headers first, vem is not in the local
  5544. header so we don't know when to do OEM translation, as the ver field
  5545. is set to MSDOS (0) by all unless something specific is needed.
  5546. However, if local header has a Unicode path extra field, we can get
  5547. the real file name from there. */
  5548. if ((z->vem & 0xff00) == 0)
  5549. /* assume archive name is OEM if from DOS */
  5550. oem_to_local_string(localz->iname, localz->iname);
  5551. #endif
  5552. }
  5553. if (fix == 2) {
  5554. # ifdef WIN32
  5555. # ifdef UNICODE_SUPPORT
  5556. localz->namew = NULL;
  5557. localz->inamew = NULL;
  5558. localz->znamew = NULL;
  5559. z->namew = NULL;
  5560. z->inamew = NULL;
  5561. z->znamew = NULL;
  5562. # endif
  5563. # endif
  5564. /* set z from localz */
  5565. z->flg = localz->lflg;
  5566. z->len = localz->len;
  5567. z->siz = localz->siz;
  5568. } else {
  5569. /* Compare localz to z */
  5570. if (localz->ver != z->ver) {
  5571. zipwarn("Local Version Needed To Extract does not match CD: ", z->iname);
  5572. }
  5573. if (localz->lflg != z->flg) {
  5574. zipwarn("Local Entry Flag does not match CD: ", z->iname);
  5575. }
  5576. if (!(z->flg & 8)) {
  5577. if (localz->crc != z->crc) {
  5578. zipwarn("Local Entry CRC does not match CD: ", z->iname);
  5579. }
  5580. }
  5581. if (fix != 3 && strcmp(localz->iname, z->iname) != 0) {
  5582. zipwarn("Local Entry name does not match CD: ", z->iname);
  5583. }
  5584. /* as copying get uncompressed and compressed sizes from central directory */
  5585. localz->len = z->len;
  5586. localz->siz = z->siz;
  5587. }
  5588. #if 0
  5589. if (fix > 1) {
  5590. if (zfseeko(in_file, z->off + n, SEEK_SET)) /* seek to compressed data */
  5591. return ferror(in_file) ? ZE_READ : ZE_EOF;
  5592. if (fix > 2) {
  5593. /* Update length of entry's name, it may have been changed. This is
  5594. needed to support the ZipNote ability to rename archive entries. */
  5595. z->nam = strlen(z->iname);
  5596. n = (uzoff_t)((LOCHEAD) + (ulg)z->nam + (ulg)z->ext);
  5597. }
  5598. /* do not trust the old compressed size */
  5599. if (putlocal(z, PUTLOCAL_WRITE) != ZE_OK)
  5600. return ZE_TEMP;
  5601. z->off = tempzn;
  5602. tempzn += n;
  5603. n = z->siz;
  5604. } else {
  5605. if (zfseeko(in_file, z->off, SEEK_SET)) /* seek to local header */
  5606. return ferror(in_file) ? ZE_READ : ZE_EOF;
  5607. z->off = tempzn;
  5608. n += z->siz;
  5609. }
  5610. #endif
  5611. /* from zipnote */
  5612. if (fix == 3) {
  5613. /* Update length of entry's name, as it may have been changed. This is
  5614. needed to support the ZipNote ability to rename archive entries. */
  5615. localz->nam = z->nam = strlen(z->iname);
  5616. /* update local name */
  5617. free(localz->iname);
  5618. if ((localz->iname = malloc(strlen(z->iname) + 1)) == NULL) {
  5619. zipwarn("out of memory in zipcopy", "");
  5620. return ZE_MEM;
  5621. }
  5622. strcpy(localz->iname, z->iname);
  5623. }
  5624. /* update disk and offset */
  5625. z->dsk = current_disk;
  5626. z->off = bytes_this_split;
  5627. /* copy the compressed data and the extended local header if there is one */
  5628. /* copy the compressed data. We recreate the local header as the local
  5629. header can't be split and putlocal ensures it won't. Also, since we
  5630. use siz and len from the central directory, we don't need the extended
  5631. local header if there is one, unless the file is encrypted as then the
  5632. extended header is used to indicate crypt head uses file time instead
  5633. of crc as the password check.
  5634. If fix = 2 then we don't have the central directory yet so keep
  5635. any data descriptors. */
  5636. if (fix != 2 && !(z->flg & 1)) {
  5637. /* Not encrypted */
  5638. localz->flg = z->flg &= ~8;
  5639. z->lflg = localz->lflg &= ~8;
  5640. }
  5641. e = 0;
  5642. if (z->lflg & 8) {
  5643. #ifdef ZIP64_SUPPORT
  5644. if (zip64_entry)
  5645. e = 24;
  5646. else
  5647. #endif
  5648. e = 16;
  5649. }
  5650. /* 4 is signature */
  5651. n = 4 + (uzoff_t)((LOCHEAD) + (ulg)(localz->nam) + (ulg)(localz->ext));
  5652. n += e + z->siz;
  5653. tempzn += n;
  5654. /* Output name */
  5655. if (fix == 2) {
  5656. if ((z->oname = malloc(strlen(localz->iname) + 1)) == NULL) {
  5657. return ZE_MEM;
  5658. }
  5659. strcpy(z->oname, localz->iname);
  5660. #ifndef UTIL
  5661. # ifdef WIN32
  5662. /* Win9x console always uses OEM character coding, and
  5663. WinNT console is set to OEM charset by default, too */
  5664. _INTERN_OEM(z->oname);
  5665. # endif
  5666. #endif
  5667. sprintf(errbuf, " copying: %s ", z->oname);
  5668. zipmessage_nl(errbuf, 0);
  5669. }
  5670. if (fix == 2)
  5671. z->crc = localz->crc;
  5672. else
  5673. localz->crc = z->crc;
  5674. if (putlocal(localz, PUTLOCAL_WRITE) != ZE_OK)
  5675. return ZE_TEMP;
  5676. /*
  5677. if (zfseeko(in_file, start_offset, SEEK_SET) != 0) {
  5678. fclose(in_file);
  5679. in_file = NULL;
  5680. zipwarn("reading archive fseek: ", strerror(errno));
  5681. return ZE_READ;
  5682. }
  5683. */
  5684. /* copy the data */
  5685. if (fix == 2 && localz->lflg & 8)
  5686. /* read to data descriptor */
  5687. r = bfcopy((uzoff_t) -2);
  5688. else
  5689. r = bfcopy(localz->siz);
  5690. if (r == ZE_ABORT) {
  5691. if (localz->ext) free(localz->extra);
  5692. if (localz->nam) free(localz->iname);
  5693. if (localz->nam) free(localz->name);
  5694. #ifdef UNICODE_SUPPORT
  5695. if (localz->uname) free(localz->uname);
  5696. #endif
  5697. free(localz);
  5698. ZIPERR(ZE_ABORT, "Could not find split");
  5699. }
  5700. if (r == ZE_EOF || skip_this_disk) {
  5701. /* missing disk */
  5702. zipwarn("aborting: ", z->oname);
  5703. if (r == ZE_OK)
  5704. r = ZE_FORM;
  5705. if (fix == 2) {
  5706. #ifdef DEBUG
  5707. zoff_t here = zftello(y);
  5708. #endif
  5709. /* fix == 2 skips right to next disk */
  5710. skip_this_disk = 0;
  5711. /* seek back in output to start of this entry so can overwrite */
  5712. if (zfseeko(y, current_local_offset, SEEK_SET) != 0) {
  5713. ZIPERR(ZE_WRITE, "seek failed on output file");
  5714. }
  5715. bytes_this_split = current_local_offset;
  5716. tempzn = current_local_offset;
  5717. }
  5718. /* tell scan to skip this entry */
  5719. if (localz->ext) free(localz->extra);
  5720. if (localz->nam) free(localz->iname);
  5721. if (localz->nam) free(localz->name);
  5722. #ifdef UNICODE_SUPPORT
  5723. if (localz->uname) free(localz->uname);
  5724. #endif
  5725. free(localz);
  5726. return r;
  5727. }
  5728. if (fix == 2 && z->flg & 8) {
  5729. /* this entry should have a data descriptor */
  5730. /* only -FF needs to read the descriptor as other modes
  5731. rely on the central directory */
  5732. if (des_good) {
  5733. /* found an apparently good data descriptor */
  5734. localz->crc = des_crc;
  5735. localz->siz = des_csize;
  5736. localz->len = des_usize;
  5737. } else {
  5738. /* no end to this entry found */
  5739. zipwarn("no end of stream entry found: ", z->oname);
  5740. zipwarn("rewinding and scanning for later entries", "");
  5741. /* seek back in output to start of this entry so can overwrite */
  5742. if (zfseeko(y, current_local_offset, SEEK_SET) != 0){
  5743. }
  5744. /* tell scan to skip this entry */
  5745. if (localz->ext) free(localz->extra);
  5746. if (localz->nam) free(localz->iname);
  5747. if (localz->nam) free(localz->name);
  5748. #ifdef UNICODE_SUPPORT
  5749. if (localz->uname) free(localz->uname);
  5750. #endif
  5751. free(localz);
  5752. return ZE_FORM;
  5753. }
  5754. }
  5755. if (z->flg & 8) {
  5756. putextended(localz);
  5757. }
  5758. /* now can close the split if local header on previous split */
  5759. if (split_method == 1 && current_local_disk != current_disk) {
  5760. close_split(current_local_disk, current_local_file, current_local_tempname);
  5761. current_local_file = NULL;
  5762. free(current_local_tempname);
  5763. }
  5764. /* update local header and close start split */
  5765. /* to use this need to seek back, do this, then come back
  5766. if (putlocal(localz, PUTLOCAL_REWRITE) != ZE_OK)
  5767. r = ZE_TEMP;
  5768. */
  5769. if (fix == 2) {
  5770. z->ver = localz->ver;
  5771. z->how = localz->how;
  5772. z->tim = localz->tim;
  5773. z->crc = localz->crc;
  5774. z->lflg = localz->lflg;
  5775. z->flg = localz->lflg;
  5776. z->len = localz->len;
  5777. z->siz = localz->siz;
  5778. z->nam = localz->nam;
  5779. z->ext = localz->ext;
  5780. z->extra = localz->extra;
  5781. /* copy local extra fields to central directory for now */
  5782. z->cext = localz->ext;
  5783. z->cextra = NULL;
  5784. if (localz->ext) {
  5785. if ((z->cextra = malloc(localz->ext + 1)) == NULL) {
  5786. return ZE_MEM;
  5787. }
  5788. strcpy(z->cextra, localz->extra);
  5789. }
  5790. z->com = 0;
  5791. z->att = 0;
  5792. z->atx = 0;
  5793. z->name = localz->name;
  5794. z->iname = localz->iname;
  5795. #ifdef UNICODE_SUPPORT
  5796. z->uname = localz->uname;
  5797. #endif
  5798. if ((z->zname = malloc(localz->nam + 1)) == NULL) {
  5799. return ZE_MEM;
  5800. }
  5801. strcpy(z->zname, z->iname);
  5802. } else {
  5803. if (localz->ext) free(localz->extra);
  5804. if (localz->nam) free(localz->iname);
  5805. if (localz->nam) free(localz->name);
  5806. #ifdef UNICODE_SUPPORT
  5807. if (localz->uname) free(localz->uname);
  5808. #endif
  5809. free(localz);
  5810. }
  5811. if (fix == 2) {
  5812. sprintf(errbuf, " (%s bytes)", zip_fzofft(z->siz, NULL, "u"));
  5813. zipmessage_nl(errbuf, 1);
  5814. if (r == ZE_READ) {
  5815. zipwarn("entry truncated: ", z->oname);
  5816. sprintf(errbuf, "expected compressed/stored size %s, actual %s",
  5817. zip_fzofft(localz->siz, NULL, "u"), zip_fzofft(bytes_this_entry, NULL, "u"));
  5818. zipwarn(errbuf, "");
  5819. }
  5820. }
  5821. return r;
  5822. }
  5823. #ifndef UTIL
  5824. #ifdef USE_EF_UT_TIME
  5825. local int ef_scan_ut_time(ef_buf, ef_len, ef_is_cent, z_utim)
  5826. char *ef_buf; /* buffer containing extra field */
  5827. extent ef_len; /* total length of extra field */
  5828. int ef_is_cent; /* flag indicating "is central extra field" */
  5829. iztimes *z_utim; /* return storage: atime, mtime, ctime */
  5830. /* This function scans the extra field for EF_TIME or EF_IZUNIX blocks
  5831. * containing Unix style time_t (GMT) values for the entry's access, creation
  5832. * and modification time.
  5833. * If a valid block is found, all time stamps are copied to the iztimes
  5834. * structure.
  5835. * The presence of an EF_TIME or EF_IZUNIX2 block results in ignoring
  5836. * all data from probably present obsolete EF_IZUNIX blocks.
  5837. * If multiple blocks of the same type are found, only the information from
  5838. * the last block is used.
  5839. * The return value is the EF_TIME Flags field (simulated in case of an
  5840. * EF_IZUNIX block) or 0 in case of failure.
  5841. */
  5842. {
  5843. int flags = 0;
  5844. unsigned eb_id;
  5845. extent eb_len;
  5846. int have_new_type_eb = FALSE;
  5847. if (ef_len == 0 || ef_buf == NULL)
  5848. return 0;
  5849. Trace((stderr,"\nef_scan_ut_time: scanning extra field of length %u\n",
  5850. (unsigned)ef_len));
  5851. while (ef_len >= EB_HEADSIZE) {
  5852. eb_id = SH(EB_ID + ef_buf);
  5853. eb_len = SH(EB_LEN + ef_buf);
  5854. if (eb_len > (ef_len - EB_HEADSIZE)) {
  5855. /* Discovered some extra field inconsistency! */
  5856. Trace((stderr,"ef_scan_ut_time: block length %u > rest ef_size %u\n",
  5857. (unsigned)eb_len, (unsigned)(ef_len - EB_HEADSIZE)));
  5858. break;
  5859. }
  5860. switch (eb_id) {
  5861. case EF_TIME:
  5862. flags &= ~0x00ff; /* ignore previous IZUNIX or EF_TIME fields */
  5863. have_new_type_eb = TRUE;
  5864. if ( eb_len >= EB_UT_MINLEN && z_utim != NULL) {
  5865. unsigned eb_idx = EB_UT_TIME1;
  5866. Trace((stderr,"ef_scan_ut_time: Found TIME extra field\n"));
  5867. flags |= (ef_buf[EB_HEADSIZE+EB_UT_FLAGS] & 0x00ff);
  5868. if ((flags & EB_UT_FL_MTIME)) {
  5869. if ((eb_idx+4) <= eb_len) {
  5870. z_utim->mtime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
  5871. eb_idx += 4;
  5872. Trace((stderr," Unix EF modtime = %ld\n", z_utim->mtime));
  5873. } else {
  5874. flags &= ~EB_UT_FL_MTIME;
  5875. Trace((stderr," Unix EF truncated, no modtime\n"));
  5876. }
  5877. }
  5878. if (ef_is_cent) {
  5879. break; /* central version of TIME field ends here */
  5880. }
  5881. if (flags & EB_UT_FL_ATIME) {
  5882. if ((eb_idx+4) <= eb_len) {
  5883. z_utim->atime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
  5884. eb_idx += 4;
  5885. Trace((stderr," Unix EF acctime = %ld\n", z_utim->atime));
  5886. } else {
  5887. flags &= ~EB_UT_FL_ATIME;
  5888. }
  5889. }
  5890. if (flags & EB_UT_FL_CTIME) {
  5891. if ((eb_idx+4) <= eb_len) {
  5892. z_utim->ctime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
  5893. /* eb_idx += 4; */ /* superfluous for now ... */
  5894. Trace((stderr," Unix EF cretime = %ld\n", z_utim->ctime));
  5895. } else {
  5896. flags &= ~EB_UT_FL_CTIME;
  5897. }
  5898. }
  5899. }
  5900. break;
  5901. case EF_IZUNIX2:
  5902. if (!have_new_type_eb) {
  5903. flags &= ~0x00ff; /* ignore any previous IZUNIX field */
  5904. have_new_type_eb = TRUE;
  5905. }
  5906. break;
  5907. case EF_IZUNIX:
  5908. if (eb_len >= EB_UX_MINLEN) {
  5909. Trace((stderr,"ef_scan_ut_time: Found IZUNIX extra field\n"));
  5910. if (have_new_type_eb) {
  5911. break; /* Ignore IZUNIX extra field block ! */
  5912. }
  5913. z_utim->atime = LG((EB_HEADSIZE+EB_UX_ATIME) + ef_buf);
  5914. z_utim->mtime = LG((EB_HEADSIZE+EB_UX_MTIME) + ef_buf);
  5915. Trace((stderr," Unix EF access time = %ld\n",z_utim->atime));
  5916. Trace((stderr," Unix EF modif. time = %ld\n",z_utim->mtime));
  5917. flags |= (EB_UT_FL_MTIME | EB_UT_FL_ATIME); /* signal success */
  5918. }
  5919. break;
  5920. case EF_THEOS:
  5921. /* printf("Not implemented yet\n"); */
  5922. break;
  5923. default:
  5924. break;
  5925. }
  5926. /* Skip this extra field block */
  5927. ef_buf += (eb_len + EB_HEADSIZE);
  5928. ef_len -= (eb_len + EB_HEADSIZE);
  5929. }
  5930. return flags;
  5931. }
  5932. int get_ef_ut_ztime(z, z_utim)
  5933. struct zlist far *z;
  5934. iztimes *z_utim;
  5935. {
  5936. int r;
  5937. #ifdef IZ_CHECK_TZ
  5938. if (!zp_tz_is_valid) return 0;
  5939. #endif
  5940. /* First, scan local extra field. */
  5941. r = ef_scan_ut_time(z->extra, z->ext, FALSE, z_utim);
  5942. /* If this was not successful, try central extra field, but only if
  5943. it is really different. */
  5944. if (!r && z->cext > 0 && z->cextra != z->extra)
  5945. r = ef_scan_ut_time(z->cextra, z->cext, TRUE, z_utim);
  5946. return r;
  5947. }
  5948. #endif /* USE_EF_UT_TIME */
  5949. local void cutpath(p, delim)
  5950. char *p; /* path string */
  5951. int delim; /* path component separator char */
  5952. /* Cut the last path component off the name *p in place.
  5953. * This should work on both internal and external names.
  5954. */
  5955. {
  5956. char *r; /* pointer to last path delimiter */
  5957. #ifdef VMS /* change [w.x.y]z to [w.x]y.DIR */
  5958. if ((r = MBSRCHR(p, ']')) != NULL)
  5959. {
  5960. *r = 0;
  5961. if ((r = MBSRCHR(p, '.')) != NULL)
  5962. {
  5963. *r = ']';
  5964. strcat(r, ".DIR;1"); /* this assumes a little padding--see PAD */
  5965. } else {
  5966. *p = 0;
  5967. }
  5968. } else {
  5969. if ((r = MBSRCHR(p, delim)) != NULL)
  5970. *r = 0;
  5971. else
  5972. *p = 0;
  5973. }
  5974. #else /* !VMS */
  5975. if ((r = MBSRCHR(p, delim)) != NULL)
  5976. *r = 0;
  5977. else
  5978. *p = 0;
  5979. #endif /* ?VMS */
  5980. }
  5981. int trash()
  5982. /* Delete the compressed files and the directories that contained the deleted
  5983. files, if empty. Return an error code in the ZE_ class. Failure of
  5984. destroy() or deletedir() is ignored. */
  5985. {
  5986. extent i; /* counter on deleted names */
  5987. extent n; /* number of directories to delete */
  5988. struct zlist far **s; /* table of zip entries to handle, sorted */
  5989. struct zlist far *z; /* current zip entry */
  5990. /* Delete marked names and count directories */
  5991. n = 0;
  5992. for (z = zfiles; z != NULL; z = z->nxt)
  5993. if (z->mark == 1 || z->trash)
  5994. {
  5995. z->mark = 1;
  5996. if (z->iname[z->nam - 1] != (char)0x2f) { /* don't unlink directory */
  5997. if (verbose)
  5998. fprintf(mesg, "zip diagnostic: deleting file %s\n", z->name);
  5999. if (destroy(z->name)) {
  6000. zipwarn("error deleting ", z->name);
  6001. }
  6002. /* Try to delete all paths that lead up to marked names. This is
  6003. * necessary only with the -D option.
  6004. */
  6005. if (!dirnames) {
  6006. cutpath(z->name, '/'); /* XXX wrong ??? */
  6007. /* Below apparently does not work for Russian OEM but
  6008. '/' should be same as 0x2f for ascii and most ports so
  6009. changed it. Did not trace through the mappings but
  6010. maybe 0x2F is mapped differently on OEM_RUSS - EG 2/28/2003 */
  6011. /* CS, 5/14/2005: iname is the byte array read from and written
  6012. to the zip archive; it MUST be ASCII (compatible)!!!
  6013. If something goes wrong with OEM_RUSS, there is a charcode
  6014. mapping error between external name (z->name) and iname somewhere
  6015. in the in2ex & ex2in code. The charcode translation should be
  6016. checked.
  6017. This code line is changed back to the original code. */
  6018. /* CS, 6/12/2005: What is handled here is the difference between
  6019. ASCII charsets and non-ASCII charsets like the family of EBCDIC
  6020. charsets. On these systems, the slash character '/' is not coded
  6021. as 0x2f but as 0x61 (the ASCII 'a'). The iname struct member holds
  6022. the name as stored in the Zip file, which are ASCII or translated
  6023. into ASCII for new entries, whereas the "name" struct member hold
  6024. the external name, coded in the native charset of the system
  6025. (EBCDIC on EBCDIC systems) */
  6026. /* cutpath(z->iname, '/'); */ /* QQQ ??? */
  6027. cutpath(z->iname, 0x2f); /* 0x2f = ascii['/'] */
  6028. z->nam = strlen(z->iname);
  6029. if (z->nam > 0) {
  6030. z->iname[z->nam - 1] = (char)0x2f;
  6031. z->iname[z->nam++] = '\0';
  6032. }
  6033. if (z->nam > 0) n++;
  6034. }
  6035. } else {
  6036. n++;
  6037. }
  6038. }
  6039. /* Construct the list of all marked directories. Some may be duplicated
  6040. * if -D was used.
  6041. */
  6042. if (n)
  6043. {
  6044. if ((s = (struct zlist far **)malloc(n*sizeof(struct zlist far *))) ==
  6045. NULL)
  6046. return ZE_MEM;
  6047. n = 0;
  6048. for (z = zfiles; z != NULL; z = z->nxt) {
  6049. if (z->mark && z->nam > 0 && z->iname[z->nam - 1] == (char)0x2f /* '/' */
  6050. && (n == 0 || strcmp(z->name, s[n-1]->name) != 0)) {
  6051. s[n++] = z;
  6052. }
  6053. }
  6054. /* Sort the files in reverse order to get subdirectories first.
  6055. * To avoid problems with strange naming conventions as in VMS,
  6056. * we sort on the internal names, so x/y/z will always be removed
  6057. * before x/y. On VMS, x/y/z > x/y but [x.y.z] < [x.y]
  6058. */
  6059. qsort((char *)s, n, sizeof(struct zlist far *), rqcmp);
  6060. for (i = 0; i < n; i++) {
  6061. char *p = s[i]->name;
  6062. if (*p == '\0') continue;
  6063. if (p[strlen(p) - 1] == '/') { /* keep VMS [x.y]z.dir;1 intact */
  6064. p[strlen(p) - 1] = '\0';
  6065. }
  6066. if (i == 0 || strcmp(s[i]->name, s[i-1]->name) != 0) {
  6067. if (verbose) {
  6068. fprintf(mesg, "deleting directory %s (if empty) \n",
  6069. s[i]->name);
  6070. }
  6071. deletedir(s[i]->name);
  6072. }
  6073. }
  6074. free((zvoid *)s);
  6075. }
  6076. return ZE_OK;
  6077. }
  6078. #endif /* !UTIL */