libzip_all.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194
  1. /*
  2. zipint.h -- internal declarations.
  3. Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <zlib.h>
  31. /*
  32. #ifdef _MSC_VER
  33. #define ZIP_EXTERN __declspec(dllimport)
  34. #endif
  35. */
  36. /*
  37. zip.h -- exported declarations.
  38. Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
  39. This file is part of libzip, a library to manipulate ZIP archives.
  40. The authors can be contacted at <libzip@nih.at>
  41. Redistribution and use in source and binary forms, with or without
  42. modification, are permitted provided that the following conditions
  43. are met:
  44. 1. Redistributions of source code must retain the above copyright
  45. notice, this list of conditions and the following disclaimer.
  46. 2. Redistributions in binary form must reproduce the above copyright
  47. notice, this list of conditions and the following disclaimer in
  48. the documentation and/or other materials provided with the
  49. distribution.
  50. 3. The names of the authors may not be used to endorse or promote
  51. products derived from this software without specific prior
  52. written permission.
  53. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  54. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  55. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  56. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  57. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  58. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  59. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  60. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  61. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  62. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  63. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  64. */
  65. #ifndef ZIP_EXTERN
  66. #define ZIP_EXTERN
  67. #endif
  68. #ifdef __cplusplus
  69. extern "C" {
  70. #endif
  71. #include <sys/types.h>
  72. #include <stdio.h>
  73. #include <time.h>
  74. /* flags for zip_open */
  75. #define ZIP_CREATE 1
  76. #define ZIP_EXCL 2
  77. #define ZIP_CHECKCONS 4
  78. /* flags for zip_name_locate, zip_fopen, zip_stat, ... */
  79. #define ZIP_FL_NOCASE 1 /* ignore case on name lookup */
  80. #define ZIP_FL_NODIR 2 /* ignore directory component */
  81. #define ZIP_FL_COMPRESSED 4 /* read compressed data */
  82. #define ZIP_FL_UNCHANGED 8 /* use original data, ignoring changes */
  83. #define ZIP_FL_RECOMPRESS 16 /* force recompression of data */
  84. /* archive global flags flags */
  85. #define ZIP_AFL_TORRENT 1 /* torrent zipped */
  86. /* libzip error codes */
  87. #define ZIP_ER_OK 0 /* N No error */
  88. #define ZIP_ER_MULTIDISK 1 /* N Multi-disk zip archives not supported */
  89. #define ZIP_ER_RENAME 2 /* S Renaming temporary file failed */
  90. #define ZIP_ER_CLOSE 3 /* S Closing zip archive failed */
  91. #define ZIP_ER_SEEK 4 /* S Seek error */
  92. #define ZIP_ER_READ 5 /* S Read error */
  93. #define ZIP_ER_WRITE 6 /* S Write error */
  94. #define ZIP_ER_CRC 7 /* N CRC error */
  95. #define ZIP_ER_ZIPCLOSED 8 /* N Containing zip archive was closed */
  96. #define ZIP_ER_NOENT 9 /* N No such file */
  97. #define ZIP_ER_EXISTS 10 /* N File already exists */
  98. #define ZIP_ER_OPEN 11 /* S Can't open file */
  99. #define ZIP_ER_TMPOPEN 12 /* S Failure to create temporary file */
  100. #define ZIP_ER_ZLIB 13 /* Z Zlib error */
  101. #define ZIP_ER_MEMORY 14 /* N Malloc failure */
  102. #define ZIP_ER_CHANGED 15 /* N Entry has been changed */
  103. #define ZIP_ER_COMPNOTSUPP 16 /* N Compression method not supported */
  104. #define ZIP_ER_EOF 17 /* N Premature EOF */
  105. #define ZIP_ER_INVAL 18 /* N Invalid argument */
  106. #define ZIP_ER_NOZIP 19 /* N Not a zip archive */
  107. #define ZIP_ER_INTERNAL 20 /* N Internal error */
  108. #define ZIP_ER_INCONS 21 /* N Zip archive inconsistent */
  109. #define ZIP_ER_REMOVE 22 /* S Can't remove file */
  110. #define ZIP_ER_DELETED 23 /* N Entry has been deleted */
  111. /* type of system error value */
  112. #define ZIP_ET_NONE 0 /* sys_err unused */
  113. #define ZIP_ET_SYS 1 /* sys_err is errno */
  114. #define ZIP_ET_ZLIB 2 /* sys_err is zlib error code */
  115. /* compression methods */
  116. #define ZIP_CM_DEFAULT -1 /* better of deflate or store */
  117. #define ZIP_CM_STORE 0 /* stored (uncompressed) */
  118. #define ZIP_CM_SHRINK 1 /* shrunk */
  119. #define ZIP_CM_REDUCE_1 2 /* reduced with factor 1 */
  120. #define ZIP_CM_REDUCE_2 3 /* reduced with factor 2 */
  121. #define ZIP_CM_REDUCE_3 4 /* reduced with factor 3 */
  122. #define ZIP_CM_REDUCE_4 5 /* reduced with factor 4 */
  123. #define ZIP_CM_IMPLODE 6 /* imploded */
  124. /* 7 - Reserved for Tokenizing compression algorithm */
  125. #define ZIP_CM_DEFLATE 8 /* deflated */
  126. #define ZIP_CM_DEFLATE64 9 /* deflate64 */
  127. #define ZIP_CM_PKWARE_IMPLODE 10 /* PKWARE imploding */
  128. /* 11 - Reserved by PKWARE */
  129. #define ZIP_CM_BZIP2 12 /* compressed using BZIP2 algorithm */
  130. /* 13 - Reserved by PKWARE */
  131. #define ZIP_CM_LZMA 14 /* LZMA (EFS) */
  132. /* 15-17 - Reserved by PKWARE */
  133. #define ZIP_CM_TERSE 18 /* compressed using IBM TERSE (new) */
  134. #define ZIP_CM_LZ77 19 /* IBM LZ77 z Architecture (PFS) */
  135. #define ZIP_CM_WAVPACK 97 /* WavPack compressed data */
  136. #define ZIP_CM_PPMD 98 /* PPMd version I, Rev 1 */
  137. /* encryption methods */
  138. #define ZIP_EM_NONE 0 /* not encrypted */
  139. #define ZIP_EM_TRAD_PKWARE 1 /* traditional PKWARE encryption */
  140. #if 0 /* Strong Encryption Header not parsed yet */
  141. #define ZIP_EM_DES 0x6601 /* strong encryption: DES */
  142. #define ZIP_EM_RC2_OLD 0x6602 /* strong encryption: RC2, version < 5.2 */
  143. #define ZIP_EM_3DES_168 0x6603
  144. #define ZIP_EM_3DES_112 0x6609
  145. #define ZIP_EM_AES_128 0x660e
  146. #define ZIP_EM_AES_192 0x660f
  147. #define ZIP_EM_AES_256 0x6610
  148. #define ZIP_EM_RC2 0x6702 /* strong encryption: RC2, version >= 5.2 */
  149. #define ZIP_EM_RC4 0x6801
  150. #endif
  151. #define ZIP_EM_UNKNOWN 0xffff /* unknown algorithm */
  152. typedef long myoff_t; /* XXX: 64 bit support */
  153. enum zip_source_cmd {
  154. ZIP_SOURCE_OPEN, /* prepare for reading */
  155. ZIP_SOURCE_READ, /* read data */
  156. ZIP_SOURCE_CLOSE, /* reading is done */
  157. ZIP_SOURCE_STAT, /* get meta information */
  158. ZIP_SOURCE_ERROR, /* get error information */
  159. ZIP_SOURCE_FREE /* cleanup and free resources */
  160. };
  161. typedef ssize_t (*zip_source_callback)(void *state, void *data,
  162. size_t len, enum zip_source_cmd cmd);
  163. struct zip_stat {
  164. const char *name; /* name of the file */
  165. int index; /* index within archive */
  166. unsigned int crc; /* crc of file data */
  167. time_t mtime; /* modification time */
  168. myoff_t size; /* size of file (uncompressed) */
  169. myoff_t comp_size; /* size of file (compressed) */
  170. unsigned short comp_method; /* compression method used */
  171. unsigned short encryption_method; /* encryption method used */
  172. };
  173. struct zip;
  174. struct zip_file;
  175. struct zip_source;
  176. ZIP_EXTERN int zip_add(struct zip *, const char *, struct zip_source *);
  177. ZIP_EXTERN int zip_add_dir(struct zip *, const char *);
  178. ZIP_EXTERN int zip_close(struct zip *);
  179. ZIP_EXTERN int zip_delete(struct zip *, int);
  180. ZIP_EXTERN void zip_error_clear(struct zip *);
  181. ZIP_EXTERN void zip_error_get(struct zip *, int *, int *);
  182. ZIP_EXTERN int zip_error_get_sys_type(int);
  183. ZIP_EXTERN int zip_error_to_str(char *, size_t, int, int);
  184. ZIP_EXTERN int zip_fclose(struct zip_file *);
  185. ZIP_EXTERN void zip_file_error_clear(struct zip_file *);
  186. ZIP_EXTERN void zip_file_error_get(struct zip_file *, int *, int *);
  187. ZIP_EXTERN const char *zip_file_strerror(struct zip_file *);
  188. ZIP_EXTERN struct zip_file *zip_fopen(struct zip *, const char *, int);
  189. ZIP_EXTERN struct zip_file *zip_fopen_index(struct zip *, int, int);
  190. ZIP_EXTERN ssize_t zip_fread(struct zip_file *, void *, size_t);
  191. ZIP_EXTERN const char *zip_get_archive_comment(struct zip *, int *, int);
  192. ZIP_EXTERN int zip_get_archive_flag(struct zip *, int, int);
  193. ZIP_EXTERN const char *zip_get_file_comment(struct zip *, int, int *, int);
  194. ZIP_EXTERN const char *zip_get_name(struct zip *, int, int);
  195. ZIP_EXTERN int zip_get_num_files(struct zip *);
  196. ZIP_EXTERN int zip_name_locate(struct zip *, const char *, int);
  197. ZIP_EXTERN struct zip *zip_open(const char *, int, int *);
  198. ZIP_EXTERN int zip_rename(struct zip *, int, const char *);
  199. ZIP_EXTERN int zip_replace(struct zip *, int, struct zip_source *);
  200. ZIP_EXTERN int zip_set_archive_comment(struct zip *, const char *, int);
  201. ZIP_EXTERN int zip_set_archive_flag(struct zip *, int, int);
  202. ZIP_EXTERN int zip_set_file_comment(struct zip *, int, const char *, int);
  203. ZIP_EXTERN struct zip_source *zip_source_buffer(struct zip *, const void *,
  204. myoff_t, int);
  205. ZIP_EXTERN struct zip_source *zip_source_file(struct zip *, const char *,
  206. myoff_t, myoff_t);
  207. ZIP_EXTERN struct zip_source *zip_source_filep(struct zip *, FILE *,
  208. myoff_t, myoff_t);
  209. ZIP_EXTERN void zip_source_free(struct zip_source *);
  210. ZIP_EXTERN struct zip_source *zip_source_function(struct zip *,
  211. zip_source_callback, void *);
  212. ZIP_EXTERN struct zip_source *zip_source_zip(struct zip *, struct zip *,
  213. int, int, myoff_t, myoff_t);
  214. ZIP_EXTERN int zip_stat(struct zip *, const char *, int, struct zip_stat *);
  215. ZIP_EXTERN int zip_stat_index(struct zip *, int, int, struct zip_stat *);
  216. ZIP_EXTERN void zip_stat_init(struct zip_stat *);
  217. ZIP_EXTERN const char *zip_strerror(struct zip *);
  218. ZIP_EXTERN int zip_unchange(struct zip *, int);
  219. ZIP_EXTERN int zip_unchange_all(struct zip *);
  220. ZIP_EXTERN int zip_unchange_archive(struct zip *);
  221. #ifdef __cplusplus
  222. }
  223. #endif
  224. /* config.h. Generated from config.h.in by configure. */
  225. /* config.h.in. Generated from configure.ac by autoheader. */
  226. /* Define to 1 if you have the declaration of `tzname', and to 0 if you don't.
  227. */
  228. /* #undef HAVE_DECL_TZNAME */
  229. #define HAVE_CONFIG_H 1
  230. /* Define to 1 if you have the <dlfcn.h> header file. */
  231. #define HAVE_DLFCN_H 1
  232. /* Define to 1 if you have the `fseeko' function. */
  233. #define HAVE_FSEEKO 1
  234. /* Define to 1 if you have the `ftello' function. */
  235. #define HAVE_FTELLO 1
  236. /* Define to 1 if you have the <inttypes.h> header file. */
  237. #define HAVE_INTTYPES_H 1
  238. /* Define to 1 if you have the `z' library (-lz). */
  239. #define HAVE_LIBZ 1
  240. /* Define to 1 if you have the <memory.h> header file. */
  241. #define HAVE_MEMORY_H 1
  242. /* Define to 1 if you have the `mkstemp' function. */
  243. #define HAVE_MKSTEMP 1
  244. /* Define to 1 if you have the `MoveFileExA' function. */
  245. /* #undef HAVE_MOVEFILEEXA */
  246. /* Define to 1 if you have the <stdint.h> header file. */
  247. #define HAVE_STDINT_H 1
  248. /* Define to 1 if you have the <stdlib.h> header file. */
  249. #define HAVE_STDLIB_H 1
  250. /* Define to 1 if you have the <strings.h> header file. */
  251. #define HAVE_STRINGS_H 1
  252. /* Define to 1 if you have the <string.h> header file. */
  253. #define HAVE_STRING_H 1
  254. /* Define to 1 if `tm_zone' is member of `struct tm'. */
  255. #ifdef WIN32
  256. #undef HAVE_STRUCT_TM_TM_ZONE
  257. #else
  258. #define HAVE_STRUCT_TM_TM_ZONE 1
  259. #endif
  260. /* Define to 1 if you have the <sys/stat.h> header file. */
  261. #define HAVE_SYS_STAT_H 1
  262. /* Define to 1 if you have the <sys/types.h> header file. */
  263. #define HAVE_SYS_TYPES_H 1
  264. /* Define to 1 if your `struct tm' has `tm_zone'. Deprecated, use
  265. `HAVE_STRUCT_TM_TM_ZONE' instead. */
  266. #define HAVE_TM_ZONE 1
  267. /* Define to 1 if you don't have `tm_zone' but do have the external array
  268. `tzname'. */
  269. /* #undef HAVE_TZNAME */
  270. /* Define to 1 if you have the <unistd.h> header file. */
  271. #define HAVE_UNISTD_H 1
  272. /* Define to 1 if your C compiler doesn't accept -c and -o together. */
  273. /* #undef NO_MINUS_C_MINUS_O */
  274. /* Name of package */
  275. #define PACKAGE "libzip"
  276. /* Define to the address where bug reports for this package should be sent. */
  277. #define PACKAGE_BUGREPORT "libzip@nih.at"
  278. /* Define to the full name of this package. */
  279. #define PACKAGE_NAME "libzip"
  280. /* Define to the full name and version of this package. */
  281. #define PACKAGE_STRING "libzip 0.9"
  282. /* Define to the one symbol short name of this package. */
  283. #define PACKAGE_TARNAME "libzip"
  284. /* Define to the version of this package. */
  285. #define PACKAGE_VERSION "0.9"
  286. /* Define to 1 if you have the ANSI C header files. */
  287. #define STDC_HEADERS 1
  288. /* Define to 1 if your <sys/time.h> declares `struct tm'. */
  289. /* #undef TM_IN_SYS_TIME */
  290. /* Version number of package */
  291. #define VERSION "0.9"
  292. #ifndef HAVE_MKSTEMP
  293. int _zip_mkstemp(char *);
  294. #define mkstemp _zip_mkstemp
  295. #endif
  296. #ifdef HAVE_MOVEFILEEXA
  297. #include <windows.h>
  298. #define _zip_rename(s, t) \
  299. (!MoveFileExA((s), (t), \
  300. MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING))
  301. #else
  302. #define _zip_rename rename
  303. #endif
  304. #ifndef HAVE_FSEEKO
  305. #define fseeko(s, o, w) (fseek((s), (long int)(o), (w)))
  306. #endif
  307. #ifndef HAVE_FTELLO
  308. #define ftello(s) ((long)ftell((s)))
  309. #endif
  310. #define CENTRAL_MAGIC "PK\1\2"
  311. #define LOCAL_MAGIC "PK\3\4"
  312. #define EOCD_MAGIC "PK\5\6"
  313. #define DATADES_MAGIC "PK\7\8"
  314. #define TORRENT_SIG "TORRENTZIPPED-"
  315. #define TORRENT_SIG_LEN 14
  316. #define TORRENT_CRC_LEN 8
  317. #define TORRENT_MEM_LEVEL 8
  318. #define CDENTRYSIZE 46u
  319. #define LENTRYSIZE 30
  320. #define MAXCOMLEN 65536
  321. #define EOCDLEN 22
  322. #define CDBUFSIZE (MAXCOMLEN+EOCDLEN)
  323. #define BUFSIZE 8192
  324. /* state of change of a file in zip archive */
  325. enum zip_state { ZIP_ST_UNCHANGED, ZIP_ST_DELETED, ZIP_ST_REPLACED,
  326. ZIP_ST_ADDED, ZIP_ST_RENAMED };
  327. /* constants for struct zip_file's member flags */
  328. #define ZIP_ZF_EOF 1 /* EOF reached */
  329. #define ZIP_ZF_DECOMP 2 /* decompress data */
  330. #define ZIP_ZF_CRC 4 /* compute and compare CRC */
  331. /* directory entry: general purpose bit flags */
  332. #define ZIP_GPBF_ENCRYPTED 0x0001 /* is encrypted */
  333. #define ZIP_GPBF_DATA_DESCRIPTOR 0x0008 /* crc/size after file data */
  334. #define ZIP_GPBF_STRONG_ENCRYPTION 0x0040 /* uses strong encryption */
  335. /* error information */
  336. struct zip_error {
  337. int zip_err; /* libzip error code (ZIP_ER_*) */
  338. int sys_err; /* copy of errno (E*) or zlib error code */
  339. char *str; /* string representation or NULL */
  340. };
  341. /* zip archive, part of API */
  342. struct zip {
  343. char *zn; /* file name */
  344. FILE *zp; /* file */
  345. struct zip_error error; /* error information */
  346. unsigned int flags; /* archive global flags */
  347. unsigned int ch_flags; /* changed archive global flags */
  348. struct zip_cdir *cdir; /* central directory */
  349. char *ch_comment; /* changed archive comment */
  350. int ch_comment_len; /* length of changed zip archive
  351. * comment, -1 if unchanged */
  352. int nentry; /* number of entries */
  353. int nentry_alloc; /* number of entries allocated */
  354. struct zip_entry *entry; /* entries */
  355. int nfile; /* number of opened files within archive */
  356. int nfile_alloc; /* number of files allocated */
  357. struct zip_file **file; /* opened files within archive */
  358. };
  359. /* file in zip archive, part of API */
  360. struct zip_file {
  361. struct zip *za; /* zip archive containing this file */
  362. struct zip_error error; /* error information */
  363. int flags; /* -1: eof, >0: error */
  364. int method; /* compression method */
  365. myoff_t fpos; /* position within zip file (fread/fwrite) */
  366. unsigned long bytes_left; /* number of bytes left to read */
  367. unsigned long cbytes_left; /* number of bytes of compressed data left */
  368. unsigned long crc; /* CRC so far */
  369. unsigned long crc_orig; /* CRC recorded in archive */
  370. char *buffer;
  371. z_stream *zstr;
  372. };
  373. /* zip archive directory entry (central or local) */
  374. struct zip_dirent {
  375. unsigned short version_madeby; /* (c) version of creator */
  376. unsigned short version_needed; /* (cl) version needed to extract */
  377. unsigned short bitflags; /* (cl) general purpose bit flag */
  378. unsigned short comp_method; /* (cl) compression method used */
  379. time_t last_mod; /* (cl) time of last modification */
  380. unsigned int crc; /* (cl) CRC-32 of uncompressed data */
  381. unsigned int comp_size; /* (cl) size of commpressed data */
  382. unsigned int uncomp_size; /* (cl) size of uncommpressed data */
  383. char *filename; /* (cl) file name (NUL-terminated) */
  384. unsigned short filename_len; /* (cl) length of filename (w/o NUL) */
  385. char *extrafield; /* (cl) extra field */
  386. unsigned short extrafield_len; /* (cl) length of extra field */
  387. char *comment; /* (c) file comment */
  388. unsigned short comment_len; /* (c) length of file comment */
  389. unsigned short disk_number; /* (c) disk number start */
  390. unsigned short int_attrib; /* (c) internal file attributes */
  391. unsigned int ext_attrib; /* (c) external file attributes */
  392. unsigned int offset; /* (c) offset of local header */
  393. };
  394. /* zip archive central directory */
  395. struct zip_cdir {
  396. struct zip_dirent *entry; /* directory entries */
  397. int nentry; /* number of entries */
  398. unsigned int size; /* size of central direcotry */
  399. unsigned int offset; /* offset of central directory in file */
  400. char *comment; /* zip archive comment */
  401. unsigned short comment_len; /* length of zip archive comment */
  402. };
  403. struct zip_source {
  404. zip_source_callback f;
  405. void *ud;
  406. };
  407. /* entry in zip archive directory */
  408. struct zip_entry {
  409. enum zip_state state;
  410. struct zip_source *source;
  411. char *ch_filename;
  412. char *ch_comment;
  413. int ch_comment_len;
  414. };
  415. extern const char * const _zip_err_str[];
  416. extern const int _zip_nerr_str;
  417. extern const int _zip_err_type[];
  418. #define ZIP_ENTRY_DATA_CHANGED(x) \
  419. ((x)->state == ZIP_ST_REPLACED \
  420. || (x)->state == ZIP_ST_ADDED)
  421. int _zip_cdir_compute_crc(struct zip *, uLong *);
  422. void _zip_cdir_free(struct zip_cdir *);
  423. struct zip_cdir *_zip_cdir_new(int, struct zip_error *);
  424. int _zip_cdir_write(struct zip_cdir *, FILE *, struct zip_error *);
  425. void _zip_dirent_finalize(struct zip_dirent *);
  426. void _zip_dirent_init(struct zip_dirent *);
  427. int _zip_dirent_read(struct zip_dirent *, FILE *,
  428. unsigned char **, unsigned int, int, struct zip_error *);
  429. void _zip_dirent_torrent_normalize(struct zip_dirent *);
  430. int _zip_dirent_write(struct zip_dirent *, FILE *, int, struct zip_error *);
  431. void _zip_entry_free(struct zip_entry *);
  432. void _zip_entry_init(struct zip *, int);
  433. struct zip_entry *_zip_entry_new(struct zip *);
  434. void _zip_error_clear(struct zip_error *);
  435. void _zip_error_copy(struct zip_error *, struct zip_error *);
  436. void _zip_error_fini(struct zip_error *);
  437. void _zip_error_get(struct zip_error *, int *, int *);
  438. void _zip_error_init(struct zip_error *);
  439. void _zip_error_set(struct zip_error *, int, int);
  440. const char *_zip_error_strerror(struct zip_error *);
  441. int _zip_file_fillbuf(void *, size_t, struct zip_file *);
  442. unsigned int _zip_file_get_offset(struct zip *, int);
  443. int _zip_filerange_crc(FILE *, myoff_t, myoff_t, uLong *, struct zip_error *);
  444. struct zip_source *_zip_source_file_or_p(struct zip *, const char *, FILE *,
  445. myoff_t, myoff_t);
  446. void _zip_free(struct zip *);
  447. const char *_zip_get_name(struct zip *, int, int, struct zip_error *);
  448. int _zip_local_header_read(struct zip *, int);
  449. void *_zip_memdup(const void *, size_t, struct zip_error *);
  450. int _zip_name_locate(struct zip *, const char *, int, struct zip_error *);
  451. struct zip *_zip_new(struct zip_error *);
  452. unsigned short _zip_read2(unsigned char **);
  453. unsigned int _zip_read4(unsigned char **);
  454. int _zip_replace(struct zip *, int, const char *, struct zip_source *);
  455. int _zip_set_name(struct zip *, int, const char *);
  456. int _zip_unchange(struct zip *, int, int);
  457. void _zip_unchange_data(struct zip_entry *);
  458. #include <errno.h>
  459. #include <stdio.h>
  460. #include <stdlib.h>
  461. #include <string.h>
  462. const char *
  463. _zip_error_strerror(struct zip_error *err)
  464. {
  465. const char *zs, *ss;
  466. char buf[128], *s;
  467. _zip_error_fini(err);
  468. if (err->zip_err < 0 || err->zip_err >= _zip_nerr_str) {
  469. sprintf(buf, "Unknown error %d", err->zip_err);
  470. zs = NULL;
  471. ss = buf;
  472. }
  473. else {
  474. zs = _zip_err_str[err->zip_err];
  475. switch (_zip_err_type[err->zip_err]) {
  476. case ZIP_ET_SYS:
  477. ss = strerror(err->sys_err);
  478. break;
  479. case ZIP_ET_ZLIB:
  480. ss = zError(err->sys_err);
  481. break;
  482. default:
  483. ss = NULL;
  484. }
  485. }
  486. if (ss == NULL)
  487. return zs;
  488. else {
  489. if ((s=(char *)malloc(strlen(ss)
  490. + (zs ? strlen(zs)+2 : 0) + 1)) == NULL)
  491. return _zip_err_str[ZIP_ER_MEMORY];
  492. sprintf(s, "%s%s%s",
  493. (zs ? zs : ""),
  494. (zs ? ": " : ""),
  495. ss);
  496. err->str = s;
  497. return s;
  498. }
  499. }
  500. #include <stdlib.h>
  501. void
  502. _zip_error_clear(struct zip_error *err)
  503. {
  504. err->zip_err = ZIP_ER_OK;
  505. err->sys_err = 0;
  506. }
  507. void
  508. _zip_error_copy(struct zip_error *dst, struct zip_error *src)
  509. {
  510. dst->zip_err = src->zip_err;
  511. dst->sys_err = src->sys_err;
  512. }
  513. void
  514. _zip_error_fini(struct zip_error *err)
  515. {
  516. free(err->str);
  517. err->str = NULL;
  518. }
  519. void
  520. _zip_error_get(struct zip_error *err, int *zep, int *sep)
  521. {
  522. if (zep)
  523. *zep = err->zip_err;
  524. if (sep) {
  525. if (zip_error_get_sys_type(err->zip_err) != ZIP_ET_NONE)
  526. *sep = err->sys_err;
  527. else
  528. *sep = 0;
  529. }
  530. }
  531. void
  532. _zip_error_init(struct zip_error *err)
  533. {
  534. err->zip_err = ZIP_ER_OK;
  535. err->sys_err = 0;
  536. err->str = NULL;
  537. }
  538. void
  539. _zip_error_set(struct zip_error *err, int ze, int se)
  540. {
  541. if (err) {
  542. err->zip_err = ze;
  543. err->sys_err = se;
  544. }
  545. }
  546. #include <sys/types.h>
  547. #include <sys/stat.h>
  548. #include <assert.h>
  549. #include <ctype.h>
  550. #include <errno.h>
  551. #include <fcntl.h>
  552. #include <stdio.h>
  553. #include <stdlib.h>
  554. #ifndef O_BINARY
  555. #define O_BINARY 0
  556. #endif
  557. int
  558. _zip_mkstemp(char *path)
  559. {
  560. int fd;
  561. char *start, *trv;
  562. struct stat sbuf;
  563. pid_t pid;
  564. /* To guarantee multiple calls generate unique names even if
  565. the file is not created. 676 different possibilities with 7
  566. or more X's, 26 with 6 or less. */
  567. static char xtra[2] = "aa";
  568. int xcnt = 0;
  569. pid = getpid();
  570. /* Move to end of path and count trailing X's. */
  571. for (trv = path; *trv; ++trv)
  572. if (*trv == 'X')
  573. xcnt++;
  574. else
  575. xcnt = 0;
  576. /* Use at least one from xtra. Use 2 if more than 6 X's. */
  577. if (*(trv - 1) == 'X')
  578. *--trv = xtra[0];
  579. if (xcnt > 6 && *(trv - 1) == 'X')
  580. *--trv = xtra[1];
  581. /* Set remaining X's to pid digits with 0's to the left. */
  582. while (*--trv == 'X') {
  583. *trv = (pid % 10) + '0';
  584. pid /= 10;
  585. }
  586. /* update xtra for next call. */
  587. if (xtra[0] != 'z')
  588. xtra[0]++;
  589. else {
  590. xtra[0] = 'a';
  591. if (xtra[1] != 'z')
  592. xtra[1]++;
  593. else
  594. xtra[1] = 'a';
  595. }
  596. /*
  597. * check the target directory; if you have six X's and it
  598. * doesn't exist this runs for a *very* long time.
  599. */
  600. for (start = trv + 1;; --trv) {
  601. if (trv <= path)
  602. break;
  603. if (*trv == '/') {
  604. *trv = '\0';
  605. if (stat(path, &sbuf))
  606. return (0);
  607. if (!S_ISDIR(sbuf.st_mode)) {
  608. errno = ENOTDIR;
  609. return (0);
  610. }
  611. *trv = '/';
  612. break;
  613. }
  614. }
  615. for (;;) {
  616. if ((fd=open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0)
  617. return (fd);
  618. if (errno != EEXIST)
  619. return (0);
  620. /* tricky little algorithm for backward compatibility */
  621. for (trv = start;;) {
  622. if (!*trv)
  623. return (0);
  624. if (*trv == 'z')
  625. *trv++ = 'a';
  626. else {
  627. if (isdigit((unsigned char)*trv))
  628. *trv = 'a';
  629. else
  630. ++*trv;
  631. break;
  632. }
  633. }
  634. }
  635. /*NOTREACHED*/
  636. }
  637. #include <stdio.h>
  638. #include <stdlib.h>
  639. #include <string.h>
  640. #include <errno.h>
  641. #include <sys/types.h>
  642. #include <sys/stat.h>
  643. static time_t _zip_d2u_time(int, int);
  644. static char *_zip_readfpstr(FILE *, unsigned int, int, struct zip_error *);
  645. static char *_zip_readstr(unsigned char **, int, int, struct zip_error *);
  646. static void _zip_u2d_time(time_t, unsigned short *, unsigned short *);
  647. static void _zip_write2(unsigned short, FILE *);
  648. static void _zip_write4(unsigned int, FILE *);
  649. void
  650. _zip_cdir_free(struct zip_cdir *cd)
  651. {
  652. int i;
  653. if (!cd)
  654. return;
  655. for (i=0; i<cd->nentry; i++)
  656. _zip_dirent_finalize(cd->entry+i);
  657. free(cd->comment);
  658. free(cd->entry);
  659. free(cd);
  660. }
  661. struct zip_cdir *
  662. _zip_cdir_new(int nentry, struct zip_error *error)
  663. {
  664. struct zip_cdir *cd;
  665. if ((cd=(struct zip_cdir *)malloc(sizeof(*cd))) == NULL) {
  666. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  667. return NULL;
  668. }
  669. if ((cd->entry=(struct zip_dirent *)malloc(sizeof(*(cd->entry))*nentry))
  670. == NULL) {
  671. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  672. free(cd);
  673. return NULL;
  674. }
  675. /* entries must be initialized by caller */
  676. cd->nentry = nentry;
  677. cd->size = cd->offset = 0;
  678. cd->comment = NULL;
  679. cd->comment_len = 0;
  680. return cd;
  681. }
  682. int
  683. _zip_cdir_write(struct zip_cdir *cd, FILE *fp, struct zip_error *error)
  684. {
  685. int i;
  686. cd->offset = ftello(fp);
  687. for (i=0; i<cd->nentry; i++) {
  688. if (_zip_dirent_write(cd->entry+i, fp, 0, error) != 0)
  689. return -1;
  690. }
  691. cd->size = ftello(fp) - cd->offset;
  692. /* clearerr(fp); */
  693. fwrite(EOCD_MAGIC, 1, 4, fp);
  694. _zip_write4(0, fp);
  695. _zip_write2((unsigned short)cd->nentry, fp);
  696. _zip_write2((unsigned short)cd->nentry, fp);
  697. _zip_write4(cd->size, fp);
  698. _zip_write4(cd->offset, fp);
  699. _zip_write2(cd->comment_len, fp);
  700. fwrite(cd->comment, 1, cd->comment_len, fp);
  701. if (ferror(fp)) {
  702. _zip_error_set(error, ZIP_ER_WRITE, errno);
  703. return -1;
  704. }
  705. return 0;
  706. }
  707. void
  708. _zip_dirent_finalize(struct zip_dirent *zde)
  709. {
  710. free(zde->filename);
  711. zde->filename = NULL;
  712. free(zde->extrafield);
  713. zde->extrafield = NULL;
  714. free(zde->comment);
  715. zde->comment = NULL;
  716. }
  717. void
  718. _zip_dirent_init(struct zip_dirent *de)
  719. {
  720. de->version_madeby = 0;
  721. de->version_needed = 20; /* 2.0 */
  722. de->bitflags = 0;
  723. de->comp_method = 0;
  724. de->last_mod = 0;
  725. de->crc = 0;
  726. de->comp_size = 0;
  727. de->uncomp_size = 0;
  728. de->filename = NULL;
  729. de->filename_len = 0;
  730. de->extrafield = NULL;
  731. de->extrafield_len = 0;
  732. de->comment = NULL;
  733. de->comment_len = 0;
  734. de->disk_number = 0;
  735. de->int_attrib = 0;
  736. de->ext_attrib = 0;
  737. de->offset = 0;
  738. }
  739. /* _zip_dirent_read(zde, fp, bufp, left, localp, error):
  740. Fills the zip directory entry zde.
  741. If bufp is non-NULL, data is taken from there and bufp is advanced
  742. by the amount of data used; no more than left bytes are used.
  743. Otherwise data is read from fp as needed.
  744. If localp != 0, it reads a local header instead of a central
  745. directory entry.
  746. Returns 0 if successful. On error, error is filled in and -1 is
  747. returned.
  748. */
  749. int
  750. _zip_dirent_read(struct zip_dirent *zde, FILE *fp,
  751. unsigned char **bufp, unsigned int left, int localp,
  752. struct zip_error *error)
  753. {
  754. unsigned char buf[CDENTRYSIZE];
  755. unsigned char *cur;
  756. unsigned short dostime, dosdate;
  757. unsigned int size;
  758. if (localp)
  759. size = LENTRYSIZE;
  760. else
  761. size = CDENTRYSIZE;
  762. if (bufp) {
  763. /* use data from buffer */
  764. cur = *bufp;
  765. if (left < size) {
  766. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  767. return -1;
  768. }
  769. }
  770. else {
  771. /* read entry from disk */
  772. if ((fread(buf, 1, size, fp)<size)) {
  773. _zip_error_set(error, ZIP_ER_READ, errno);
  774. return -1;
  775. }
  776. left = size;
  777. cur = buf;
  778. }
  779. if (memcmp(cur, (localp ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) {
  780. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  781. return -1;
  782. }
  783. cur += 4;
  784. /* convert buffercontents to zip_dirent */
  785. if (!localp)
  786. zde->version_madeby = _zip_read2(&cur);
  787. else
  788. zde->version_madeby = 0;
  789. zde->version_needed = _zip_read2(&cur);
  790. zde->bitflags = _zip_read2(&cur);
  791. zde->comp_method = _zip_read2(&cur);
  792. /* convert to time_t */
  793. dostime = _zip_read2(&cur);
  794. dosdate = _zip_read2(&cur);
  795. zde->last_mod = _zip_d2u_time(dostime, dosdate);
  796. zde->crc = _zip_read4(&cur);
  797. zde->comp_size = _zip_read4(&cur);
  798. zde->uncomp_size = _zip_read4(&cur);
  799. zde->filename_len = _zip_read2(&cur);
  800. zde->extrafield_len = _zip_read2(&cur);
  801. if (localp) {
  802. zde->comment_len = 0;
  803. zde->disk_number = 0;
  804. zde->int_attrib = 0;
  805. zde->ext_attrib = 0;
  806. zde->offset = 0;
  807. } else {
  808. zde->comment_len = _zip_read2(&cur);
  809. zde->disk_number = _zip_read2(&cur);
  810. zde->int_attrib = _zip_read2(&cur);
  811. zde->ext_attrib = _zip_read4(&cur);
  812. zde->offset = _zip_read4(&cur);
  813. }
  814. zde->filename = NULL;
  815. zde->extrafield = NULL;
  816. zde->comment = NULL;
  817. if (bufp) {
  818. if (left < CDENTRYSIZE + (zde->filename_len+zde->extrafield_len
  819. +zde->comment_len)) {
  820. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  821. return -1;
  822. }
  823. if (zde->filename_len) {
  824. zde->filename = _zip_readstr(&cur, zde->filename_len, 1, error);
  825. if (!zde->filename)
  826. return -1;
  827. }
  828. if (zde->extrafield_len) {
  829. zde->extrafield = _zip_readstr(&cur, zde->extrafield_len, 0,
  830. error);
  831. if (!zde->extrafield)
  832. return -1;
  833. }
  834. if (zde->comment_len) {
  835. zde->comment = _zip_readstr(&cur, zde->comment_len, 0, error);
  836. if (!zde->comment)
  837. return -1;
  838. }
  839. }
  840. else {
  841. if (zde->filename_len) {
  842. zde->filename = _zip_readfpstr(fp, zde->filename_len, 1, error);
  843. if (!zde->filename)
  844. return -1;
  845. }
  846. if (zde->extrafield_len) {
  847. zde->extrafield = _zip_readfpstr(fp, zde->extrafield_len, 0,
  848. error);
  849. if (!zde->extrafield)
  850. return -1;
  851. }
  852. if (zde->comment_len) {
  853. zde->comment = _zip_readfpstr(fp, zde->comment_len, 0, error);
  854. if (!zde->comment)
  855. return -1;
  856. }
  857. }
  858. if (bufp)
  859. *bufp = cur;
  860. return 0;
  861. }
  862. /* _zip_dirent_torrent_normalize(de);
  863. Set values suitable for torrentzip.
  864. */
  865. void
  866. _zip_dirent_torrent_normalize(struct zip_dirent *de)
  867. {
  868. static struct tm torrenttime;
  869. static time_t last_mod = 0;
  870. if (last_mod == 0) {
  871. #ifdef HAVE_STRUCT_TM_TM_ZONE
  872. time_t now;
  873. struct tm *l;
  874. #endif
  875. torrenttime.tm_sec = 0;
  876. torrenttime.tm_min = 32;
  877. torrenttime.tm_hour = 23;
  878. torrenttime.tm_mday = 24;
  879. torrenttime.tm_mon = 11;
  880. torrenttime.tm_year = 96;
  881. torrenttime.tm_wday = 0;
  882. torrenttime.tm_yday = 0;
  883. torrenttime.tm_isdst = 0;
  884. #ifdef HAVE_STRUCT_TM_TM_ZONE
  885. time(&now);
  886. l = localtime(&now);
  887. torrenttime.tm_gmtoff = l->tm_gmtoff;
  888. torrenttime.tm_zone = l->tm_zone;
  889. #endif
  890. last_mod = mktime(&torrenttime);
  891. }
  892. de->version_madeby = 0;
  893. de->version_needed = 20; /* 2.0 */
  894. de->bitflags = 2; /* maximum compression */
  895. de->comp_method = ZIP_CM_DEFLATE;
  896. de->last_mod = last_mod;
  897. de->disk_number = 0;
  898. de->int_attrib = 0;
  899. de->ext_attrib = 0;
  900. de->offset = 0;
  901. free(de->extrafield);
  902. de->extrafield = NULL;
  903. de->extrafield_len = 0;
  904. free(de->comment);
  905. de->comment = NULL;
  906. de->comment_len = 0;
  907. }
  908. /* _zip_dirent_write(zde, fp, localp, error):
  909. Writes zip directory entry zde to file fp.
  910. If localp != 0, it writes a local header instead of a central
  911. directory entry.
  912. Returns 0 if successful. On error, error is filled in and -1 is
  913. returned.
  914. */
  915. int
  916. _zip_dirent_write(struct zip_dirent *zde, FILE *fp, int localp,
  917. struct zip_error *error)
  918. {
  919. unsigned short dostime, dosdate;
  920. fwrite(localp ? LOCAL_MAGIC : CENTRAL_MAGIC, 1, 4, fp);
  921. if (!localp)
  922. _zip_write2(zde->version_madeby, fp);
  923. _zip_write2(zde->version_needed, fp);
  924. _zip_write2(zde->bitflags, fp);
  925. _zip_write2(zde->comp_method, fp);
  926. _zip_u2d_time(zde->last_mod, &dostime, &dosdate);
  927. _zip_write2(dostime, fp);
  928. _zip_write2(dosdate, fp);
  929. _zip_write4(zde->crc, fp);
  930. _zip_write4(zde->comp_size, fp);
  931. _zip_write4(zde->uncomp_size, fp);
  932. _zip_write2(zde->filename_len, fp);
  933. _zip_write2(zde->extrafield_len, fp);
  934. if (!localp) {
  935. _zip_write2(zde->comment_len, fp);
  936. _zip_write2(zde->disk_number, fp);
  937. _zip_write2(zde->int_attrib, fp);
  938. _zip_write4(zde->ext_attrib, fp);
  939. _zip_write4(zde->offset, fp);
  940. }
  941. if (zde->filename_len)
  942. fwrite(zde->filename, 1, zde->filename_len, fp);
  943. if (zde->extrafield_len)
  944. fwrite(zde->extrafield, 1, zde->extrafield_len, fp);
  945. if (!localp) {
  946. if (zde->comment_len)
  947. fwrite(zde->comment, 1, zde->comment_len, fp);
  948. }
  949. if (ferror(fp)) {
  950. _zip_error_set(error, ZIP_ER_WRITE, errno);
  951. return -1;
  952. }
  953. return 0;
  954. }
  955. static time_t
  956. _zip_d2u_time(int dtime, int ddate)
  957. {
  958. struct tm *tm;
  959. time_t now;
  960. now = time(NULL);
  961. tm = localtime(&now);
  962. /* let mktime decide if DST is in effect */
  963. tm->tm_isdst = -1;
  964. tm->tm_year = ((ddate>>9)&127) + 1980 - 1900;
  965. tm->tm_mon = ((ddate>>5)&15) - 1;
  966. tm->tm_mday = ddate&31;
  967. tm->tm_hour = (dtime>>11)&31;
  968. tm->tm_min = (dtime>>5)&63;
  969. tm->tm_sec = (dtime<<1)&62;
  970. return mktime(tm);
  971. }
  972. unsigned short
  973. _zip_read2(unsigned char **a)
  974. {
  975. unsigned short ret;
  976. ret = (*a)[0]+((*a)[1]<<8);
  977. *a += 2;
  978. return ret;
  979. }
  980. unsigned int
  981. _zip_read4(unsigned char **a)
  982. {
  983. unsigned int ret;
  984. ret = ((((((*a)[3]<<8)+(*a)[2])<<8)+(*a)[1])<<8)+(*a)[0];
  985. *a += 4;
  986. return ret;
  987. }
  988. static char *
  989. _zip_readfpstr(FILE *fp, unsigned int len, int nulp, struct zip_error *error)
  990. {
  991. char *r, *o;
  992. r = (char *)malloc(nulp ? len+1 : len);
  993. if (!r) {
  994. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  995. return NULL;
  996. }
  997. if (fread(r, 1, len, fp)<len) {
  998. free(r);
  999. _zip_error_set(error, ZIP_ER_READ, errno);
  1000. return NULL;
  1001. }
  1002. if (nulp) {
  1003. /* replace any in-string NUL characters with spaces */
  1004. r[len] = 0;
  1005. for (o=r; o<r+len; o++)
  1006. if (*o == '\0')
  1007. *o = ' ';
  1008. }
  1009. return r;
  1010. }
  1011. static char *
  1012. _zip_readstr(unsigned char **buf, int len, int nulp, struct zip_error *error)
  1013. {
  1014. char *r, *o;
  1015. r = (char *)malloc(nulp ? len+1 : len);
  1016. if (!r) {
  1017. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  1018. return NULL;
  1019. }
  1020. memcpy(r, *buf, len);
  1021. *buf += len;
  1022. if (nulp) {
  1023. /* replace any in-string NUL characters with spaces */
  1024. r[len] = 0;
  1025. for (o=r; o<r+len; o++)
  1026. if (*o == '\0')
  1027. *o = ' ';
  1028. }
  1029. return r;
  1030. }
  1031. static void
  1032. _zip_write2(unsigned short i, FILE *fp)
  1033. {
  1034. putc(i&0xff, fp);
  1035. putc((i>>8)&0xff, fp);
  1036. return;
  1037. }
  1038. static void
  1039. _zip_write4(unsigned int i, FILE *fp)
  1040. {
  1041. putc(i&0xff, fp);
  1042. putc((i>>8)&0xff, fp);
  1043. putc((i>>16)&0xff, fp);
  1044. putc((i>>24)&0xff, fp);
  1045. return;
  1046. }
  1047. static void
  1048. _zip_u2d_time(time_t time, unsigned short *dtime, unsigned short *ddate)
  1049. {
  1050. struct tm *tm;
  1051. tm = localtime(&time);
  1052. *ddate = ((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5)
  1053. + tm->tm_mday;
  1054. *dtime = ((tm->tm_hour)<<11) + ((tm->tm_min)<<5)
  1055. + ((tm->tm_sec)>>1);
  1056. return;
  1057. }
  1058. ZIP_EXTERN int
  1059. zip_delete(struct zip *za, int idx)
  1060. {
  1061. if (idx < 0 || idx >= za->nentry) {
  1062. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  1063. return -1;
  1064. }
  1065. /* allow duplicate file names, because the file will
  1066. * be removed directly afterwards */
  1067. if (_zip_unchange(za, idx, 1) != 0)
  1068. return -1;
  1069. za->entry[idx].state = ZIP_ST_DELETED;
  1070. return 0;
  1071. }
  1072. ZIP_EXTERN void
  1073. zip_error_clear(struct zip *za)
  1074. {
  1075. _zip_error_clear(&za->error);
  1076. }
  1077. ZIP_EXTERN int
  1078. zip_add(struct zip *za, const char *name, struct zip_source *source)
  1079. {
  1080. if (name == NULL || source == NULL) {
  1081. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  1082. return -1;
  1083. }
  1084. return _zip_replace(za, -1, name, source);
  1085. }
  1086. ZIP_EXTERN int
  1087. zip_error_get_sys_type(int ze)
  1088. {
  1089. if (ze < 0 || ze >= _zip_nerr_str)
  1090. return 0;
  1091. return _zip_err_type[ze];
  1092. }
  1093. ZIP_EXTERN void
  1094. zip_error_get(struct zip *za, int *zep, int *sep)
  1095. {
  1096. _zip_error_get(&za->error, zep, sep);
  1097. }
  1098. const char * const _zip_err_str[] = {
  1099. "No error",
  1100. "Multi-disk zip archives not supported",
  1101. "Renaming temporary file failed",
  1102. "Closing zip archive failed",
  1103. "Seek error",
  1104. "Read error",
  1105. "Write error",
  1106. "CRC error",
  1107. "Containing zip archive was closed",
  1108. "No such file",
  1109. "File already exists",
  1110. "Can't open file",
  1111. "Failure to create temporary file",
  1112. "Zlib error",
  1113. "Malloc failure",
  1114. "Entry has been changed",
  1115. "Compression method not supported",
  1116. "Premature EOF",
  1117. "Invalid argument",
  1118. "Not a zip archive",
  1119. "Internal error",
  1120. "Zip archive inconsistent",
  1121. "Can't remove file",
  1122. "Entry has been deleted",
  1123. };
  1124. const int _zip_nerr_str = sizeof(_zip_err_str)/sizeof(_zip_err_str[0]);
  1125. #define N ZIP_ET_NONE
  1126. #define S ZIP_ET_SYS
  1127. #define Z ZIP_ET_ZLIB
  1128. const int _zip_err_type[] = {
  1129. N,
  1130. N,
  1131. S,
  1132. S,
  1133. S,
  1134. S,
  1135. S,
  1136. N,
  1137. N,
  1138. N,
  1139. N,
  1140. S,
  1141. S,
  1142. Z,
  1143. N,
  1144. N,
  1145. N,
  1146. N,
  1147. N,
  1148. N,
  1149. N,
  1150. N,
  1151. S,
  1152. N,
  1153. };
  1154. struct zip_entry *
  1155. _zip_entry_new(struct zip *za)
  1156. {
  1157. struct zip_entry *ze;
  1158. if (!za) {
  1159. ze = (struct zip_entry *)malloc(sizeof(struct zip_entry));
  1160. if (!ze) {
  1161. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  1162. return NULL;
  1163. }
  1164. }
  1165. else {
  1166. if (za->nentry >= za->nentry_alloc-1) {
  1167. za->nentry_alloc += 16;
  1168. za->entry = (struct zip_entry *)realloc(za->entry,
  1169. sizeof(struct zip_entry)
  1170. * za->nentry_alloc);
  1171. if (!za->entry) {
  1172. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  1173. return NULL;
  1174. }
  1175. }
  1176. ze = za->entry+za->nentry;
  1177. }
  1178. ze->state = ZIP_ST_UNCHANGED;
  1179. ze->ch_filename = NULL;
  1180. ze->ch_comment = NULL;
  1181. ze->ch_comment_len = -1;
  1182. ze->source = NULL;
  1183. if (za)
  1184. za->nentry++;
  1185. return ze;
  1186. }
  1187. void
  1188. _zip_entry_free(struct zip_entry *ze)
  1189. {
  1190. free(ze->ch_filename);
  1191. ze->ch_filename = NULL;
  1192. free(ze->ch_comment);
  1193. ze->ch_comment = NULL;
  1194. ze->ch_comment_len = -1;
  1195. _zip_unchange_data(ze);
  1196. }
  1197. static int add_data(struct zip *, struct zip_source *, struct zip_dirent *,
  1198. FILE *);
  1199. static int add_data_comp(zip_source_callback, void *, struct zip_stat *,
  1200. FILE *, struct zip_error *);
  1201. static int add_data_uncomp(struct zip *, zip_source_callback, void *,
  1202. struct zip_stat *, FILE *);
  1203. static void ch_set_error(struct zip_error *, zip_source_callback, void *);
  1204. static int copy_data(FILE *, myoff_t, FILE *, struct zip_error *);
  1205. static int write_cdir(struct zip *, struct zip_cdir *, FILE *);
  1206. static int _zip_cdir_set_comment(struct zip_cdir *, struct zip *);
  1207. static int _zip_changed(struct zip *, int *);
  1208. static char *_zip_create_temp_output(struct zip *, FILE **);
  1209. static int _zip_torrentzip_cmp(const void *, const void *);
  1210. struct filelist {
  1211. int idx;
  1212. const char *name;
  1213. };
  1214. ZIP_EXTERN int
  1215. zip_close(struct zip *za)
  1216. {
  1217. int survivors;
  1218. int i, j, error;
  1219. char *temp;
  1220. FILE *out;
  1221. mode_t mask;
  1222. struct zip_cdir *cd;
  1223. struct zip_dirent de;
  1224. struct filelist *filelist;
  1225. int reopen_on_error;
  1226. int new_torrentzip;
  1227. reopen_on_error = 0;
  1228. if (za == NULL)
  1229. return -1;
  1230. if (!_zip_changed(za, &survivors)) {
  1231. _zip_free(za);
  1232. return 0;
  1233. }
  1234. /* don't create zip files with no entries */
  1235. if (survivors == 0) {
  1236. if (za->zn && za->zp) {
  1237. if (remove(za->zn) != 0) {
  1238. _zip_error_set(&za->error, ZIP_ER_REMOVE, errno);
  1239. return -1;
  1240. }
  1241. }
  1242. _zip_free(za);
  1243. return 0;
  1244. }
  1245. if ((filelist=(struct filelist *)malloc(sizeof(filelist[0])*survivors))
  1246. == NULL)
  1247. return -1;
  1248. if ((cd=_zip_cdir_new(survivors, &za->error)) == NULL) {
  1249. free(filelist);
  1250. return -1;
  1251. }
  1252. for (i=0; i<survivors; i++)
  1253. _zip_dirent_init(&cd->entry[i]);
  1254. /* archive comment is special for torrentzip */
  1255. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) {
  1256. cd->comment = _zip_memdup(TORRENT_SIG "XXXXXXXX",
  1257. TORRENT_SIG_LEN + TORRENT_CRC_LEN,
  1258. &za->error);
  1259. if (cd->comment == NULL) {
  1260. _zip_cdir_free(cd);
  1261. free(filelist);
  1262. return -1;
  1263. }
  1264. cd->comment_len = TORRENT_SIG_LEN + TORRENT_CRC_LEN;
  1265. }
  1266. else if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0) {
  1267. if (_zip_cdir_set_comment(cd, za) == -1) {
  1268. _zip_cdir_free(cd);
  1269. free(filelist);
  1270. return -1;
  1271. }
  1272. }
  1273. if ((temp=_zip_create_temp_output(za, &out)) == NULL) {
  1274. _zip_cdir_free(cd);
  1275. return -1;
  1276. }
  1277. /* create list of files with index into original archive */
  1278. for (i=j=0; i<za->nentry; i++) {
  1279. if (za->entry[i].state == ZIP_ST_DELETED)
  1280. continue;
  1281. filelist[j].idx = i;
  1282. filelist[j].name = zip_get_name(za, i, 0);
  1283. j++;
  1284. }
  1285. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
  1286. qsort(filelist, survivors, sizeof(filelist[0]),
  1287. _zip_torrentzip_cmp);
  1288. new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1
  1289. && zip_get_archive_flag(za, ZIP_AFL_TORRENT,
  1290. ZIP_FL_UNCHANGED) == 0);
  1291. error = 0;
  1292. for (j=0; j<survivors; j++) {
  1293. i = filelist[j].idx;
  1294. /* create new local directory entry */
  1295. if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) {
  1296. _zip_dirent_init(&de);
  1297. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
  1298. _zip_dirent_torrent_normalize(&de);
  1299. /* use it as central directory entry */
  1300. memcpy(cd->entry+j, &de, sizeof(cd->entry[j]));
  1301. /* set/update file name */
  1302. if (za->entry[i].ch_filename == NULL) {
  1303. if (za->entry[i].state == ZIP_ST_ADDED) {
  1304. de.filename = strdup("-");
  1305. de.filename_len = 1;
  1306. cd->entry[j].filename = "-";
  1307. }
  1308. else {
  1309. de.filename = strdup(za->cdir->entry[i].filename);
  1310. de.filename_len = strlen(de.filename);
  1311. cd->entry[j].filename = za->cdir->entry[i].filename;
  1312. cd->entry[j].filename_len = de.filename_len;
  1313. }
  1314. }
  1315. }
  1316. else {
  1317. /* copy existing directory entries */
  1318. if (fseeko(za->zp, za->cdir->entry[i].offset, SEEK_SET) != 0) {
  1319. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  1320. error = 1;
  1321. break;
  1322. }
  1323. if (_zip_dirent_read(&de, za->zp, NULL, 0, 1, &za->error) != 0) {
  1324. error = 1;
  1325. break;
  1326. }
  1327. if (de.bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
  1328. de.crc = za->cdir->entry[i].crc;
  1329. de.comp_size = za->cdir->entry[i].comp_size;
  1330. de.uncomp_size = za->cdir->entry[i].uncomp_size;
  1331. de.bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
  1332. }
  1333. memcpy(cd->entry+j, za->cdir->entry+i, sizeof(cd->entry[j]));
  1334. }
  1335. if (za->entry[i].ch_filename) {
  1336. free(de.filename);
  1337. if ((de.filename=strdup(za->entry[i].ch_filename)) == NULL) {
  1338. error = 1;
  1339. break;
  1340. }
  1341. de.filename_len = strlen(de.filename);
  1342. cd->entry[j].filename = za->entry[i].ch_filename;
  1343. cd->entry[j].filename_len = de.filename_len;
  1344. }
  1345. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0
  1346. && za->entry[i].ch_comment_len != -1) {
  1347. /* as the rest of cd entries, its malloc/free is done by za */
  1348. cd->entry[j].comment = za->entry[i].ch_comment;
  1349. cd->entry[j].comment_len = za->entry[i].ch_comment_len;
  1350. }
  1351. cd->entry[j].offset = ftello(out);
  1352. if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) {
  1353. struct zip_source *zs;
  1354. zs = NULL;
  1355. if (!ZIP_ENTRY_DATA_CHANGED(za->entry+i)) {
  1356. if ((zs=zip_source_zip(za, za, i, ZIP_FL_RECOMPRESS, 0, -1))
  1357. == NULL) {
  1358. error = 1;
  1359. break;
  1360. }
  1361. }
  1362. if (add_data(za, zs ? zs : za->entry[i].source, &de, out) < 0) {
  1363. error = 1;
  1364. break;
  1365. }
  1366. cd->entry[j].last_mod = de.last_mod;
  1367. cd->entry[j].comp_method = de.comp_method;
  1368. cd->entry[j].comp_size = de.comp_size;
  1369. cd->entry[j].uncomp_size = de.uncomp_size;
  1370. cd->entry[j].crc = de.crc;
  1371. }
  1372. else {
  1373. if (_zip_dirent_write(&de, out, 1, &za->error) < 0) {
  1374. error = 1;
  1375. break;
  1376. }
  1377. /* we just read the local dirent, file is at correct position */
  1378. if (copy_data(za->zp, cd->entry[j].comp_size, out,
  1379. &za->error) < 0) {
  1380. error = 1;
  1381. break;
  1382. }
  1383. }
  1384. _zip_dirent_finalize(&de);
  1385. }
  1386. if (!error) {
  1387. if (write_cdir(za, cd, out) < 0)
  1388. error = 1;
  1389. }
  1390. /* pointers in cd entries are owned by za */
  1391. cd->nentry = 0;
  1392. _zip_cdir_free(cd);
  1393. if (error) {
  1394. _zip_dirent_finalize(&de);
  1395. fclose(out);
  1396. remove(temp);
  1397. free(temp);
  1398. return -1;
  1399. }
  1400. if (fclose(out) != 0) {
  1401. _zip_error_set(&za->error, ZIP_ER_CLOSE, errno);
  1402. remove(temp);
  1403. free(temp);
  1404. return -1;
  1405. }
  1406. if (za->zp) {
  1407. fclose(za->zp);
  1408. za->zp = NULL;
  1409. reopen_on_error = 1;
  1410. }
  1411. if (_zip_rename(temp, za->zn) != 0) {
  1412. _zip_error_set(&za->error, ZIP_ER_RENAME, errno);
  1413. remove(temp);
  1414. free(temp);
  1415. if (reopen_on_error) {
  1416. /* ignore errors, since we're already in an error case */
  1417. za->zp = fopen(za->zn, "rb");
  1418. }
  1419. return -1;
  1420. }
  1421. mask = umask(0);
  1422. umask(mask);
  1423. chmod(za->zn, 0666&~mask);
  1424. _zip_free(za);
  1425. free(temp);
  1426. return 0;
  1427. }
  1428. static int
  1429. add_data(struct zip *za, struct zip_source *zs, struct zip_dirent *de, FILE *ft)
  1430. {
  1431. myoff_t offstart, offend;
  1432. zip_source_callback cb;
  1433. void *ud;
  1434. struct zip_stat st;
  1435. cb = zs->f;
  1436. ud = zs->ud;
  1437. if (cb(ud, &st, sizeof(st), ZIP_SOURCE_STAT) < (ssize_t)sizeof(st)) {
  1438. ch_set_error(&za->error, cb, ud);
  1439. return -1;
  1440. }
  1441. if (cb(ud, NULL, 0, ZIP_SOURCE_OPEN) < 0) {
  1442. ch_set_error(&za->error, cb, ud);
  1443. return -1;
  1444. }
  1445. offstart = ftello(ft);
  1446. if (_zip_dirent_write(de, ft, 1, &za->error) < 0)
  1447. return -1;
  1448. if (st.comp_method != ZIP_CM_STORE) {
  1449. if (add_data_comp(cb, ud, &st, ft, &za->error) < 0)
  1450. return -1;
  1451. }
  1452. else {
  1453. if (add_data_uncomp(za, cb, ud, &st, ft) < 0)
  1454. return -1;
  1455. }
  1456. if (cb(ud, NULL, 0, ZIP_SOURCE_CLOSE) < 0) {
  1457. ch_set_error(&za->error, cb, ud);
  1458. return -1;
  1459. }
  1460. offend = ftello(ft);
  1461. if (fseeko(ft, offstart, SEEK_SET) < 0) {
  1462. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  1463. return -1;
  1464. }
  1465. de->last_mod = st.mtime;
  1466. de->comp_method = st.comp_method;
  1467. de->crc = st.crc;
  1468. de->uncomp_size = st.size;
  1469. de->comp_size = st.comp_size;
  1470. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
  1471. _zip_dirent_torrent_normalize(de);
  1472. if (_zip_dirent_write(de, ft, 1, &za->error) < 0)
  1473. return -1;
  1474. if (fseeko(ft, offend, SEEK_SET) < 0) {
  1475. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  1476. return -1;
  1477. }
  1478. return 0;
  1479. }
  1480. static int
  1481. add_data_comp(zip_source_callback cb, void *ud, struct zip_stat *st,FILE *ft,
  1482. struct zip_error *error)
  1483. {
  1484. char buf[BUFSIZE];
  1485. ssize_t n;
  1486. st->comp_size = 0;
  1487. while ((n=cb(ud, buf, sizeof(buf), ZIP_SOURCE_READ)) > 0) {
  1488. if (fwrite(buf, 1, n, ft) != (size_t)n) {
  1489. _zip_error_set(error, ZIP_ER_WRITE, errno);
  1490. return -1;
  1491. }
  1492. st->comp_size += n;
  1493. }
  1494. if (n < 0) {
  1495. ch_set_error(error, cb, ud);
  1496. return -1;
  1497. }
  1498. return 0;
  1499. }
  1500. static int
  1501. add_data_uncomp(struct zip *za, zip_source_callback cb, void *ud,
  1502. struct zip_stat *st, FILE *ft)
  1503. {
  1504. char b1[BUFSIZE], b2[BUFSIZE];
  1505. int end, flush, ret;
  1506. ssize_t n;
  1507. size_t n2;
  1508. z_stream zstr;
  1509. int mem_level;
  1510. st->comp_method = ZIP_CM_DEFLATE;
  1511. st->comp_size = st->size = 0;
  1512. st->crc = crc32(0, NULL, 0);
  1513. zstr.zalloc = Z_NULL;
  1514. zstr.zfree = Z_NULL;
  1515. zstr.opaque = NULL;
  1516. zstr.avail_in = 0;
  1517. zstr.avail_out = 0;
  1518. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
  1519. mem_level = TORRENT_MEM_LEVEL;
  1520. else
  1521. mem_level = MAX_MEM_LEVEL;
  1522. /* -MAX_WBITS: undocumented feature of zlib to _not_ write a zlib header */
  1523. deflateInit2(&zstr, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, mem_level,
  1524. Z_DEFAULT_STRATEGY);
  1525. zstr.next_out = (Bytef *)b2;
  1526. zstr.avail_out = sizeof(b2);
  1527. zstr.avail_in = 0;
  1528. flush = 0;
  1529. end = 0;
  1530. while (!end) {
  1531. if (zstr.avail_in == 0 && !flush) {
  1532. if ((n=cb(ud, b1, sizeof(b1), ZIP_SOURCE_READ)) < 0) {
  1533. ch_set_error(&za->error, cb, ud);
  1534. deflateEnd(&zstr);
  1535. return -1;
  1536. }
  1537. if (n > 0) {
  1538. zstr.avail_in = n;
  1539. zstr.next_in = (Bytef *)b1;
  1540. st->size += n;
  1541. st->crc = crc32(st->crc, (Bytef *)b1, n);
  1542. }
  1543. else
  1544. flush = Z_FINISH;
  1545. }
  1546. ret = deflate(&zstr, flush);
  1547. if (ret != Z_OK && ret != Z_STREAM_END) {
  1548. _zip_error_set(&za->error, ZIP_ER_ZLIB, ret);
  1549. return -1;
  1550. }
  1551. if (zstr.avail_out != sizeof(b2)) {
  1552. n2 = sizeof(b2) - zstr.avail_out;
  1553. if (fwrite(b2, 1, n2, ft) != n2) {
  1554. _zip_error_set(&za->error, ZIP_ER_WRITE, errno);
  1555. return -1;
  1556. }
  1557. zstr.next_out = (Bytef *)b2;
  1558. zstr.avail_out = sizeof(b2);
  1559. st->comp_size += n2;
  1560. }
  1561. if (ret == Z_STREAM_END) {
  1562. deflateEnd(&zstr);
  1563. end = 1;
  1564. }
  1565. }
  1566. return 0;
  1567. }
  1568. static void
  1569. ch_set_error(struct zip_error *error, zip_source_callback cb, void *ud)
  1570. {
  1571. int e[2];
  1572. if ((cb(ud, e, sizeof(e), ZIP_SOURCE_ERROR)) < (ssize_t)sizeof(e)) {
  1573. error->zip_err = ZIP_ER_INTERNAL;
  1574. error->sys_err = 0;
  1575. }
  1576. else {
  1577. error->zip_err = e[0];
  1578. error->sys_err = e[1];
  1579. }
  1580. }
  1581. static int
  1582. copy_data(FILE *fs, myoff_t len, FILE *ft, struct zip_error *error)
  1583. {
  1584. char buf[BUFSIZE];
  1585. int n, nn;
  1586. if (len == 0)
  1587. return 0;
  1588. while (len > 0) {
  1589. nn = len > sizeof(buf) ? sizeof(buf) : len;
  1590. if ((n=fread(buf, 1, nn, fs)) < 0) {
  1591. _zip_error_set(error, ZIP_ER_READ, errno);
  1592. return -1;
  1593. }
  1594. else if (n == 0) {
  1595. _zip_error_set(error, ZIP_ER_EOF, 0);
  1596. return -1;
  1597. }
  1598. if (fwrite(buf, 1, n, ft) != (size_t)n) {
  1599. _zip_error_set(error, ZIP_ER_WRITE, errno);
  1600. return -1;
  1601. }
  1602. len -= n;
  1603. }
  1604. return 0;
  1605. }
  1606. static int
  1607. write_cdir(struct zip *za, struct zip_cdir *cd, FILE *out)
  1608. {
  1609. myoff_t offset;
  1610. uLong crc;
  1611. char buf[TORRENT_CRC_LEN+1];
  1612. if (_zip_cdir_write(cd, out, &za->error) < 0)
  1613. return -1;
  1614. if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0)
  1615. return 0;
  1616. /* fix up torrentzip comment */
  1617. offset = ftello(out);
  1618. if (_zip_filerange_crc(out, cd->offset, cd->size, &crc, &za->error) < 0)
  1619. return -1;
  1620. snprintf(buf, sizeof(buf), "%08lX", (long)crc);
  1621. if (fseeko(out, offset-TORRENT_CRC_LEN, SEEK_SET) < 0) {
  1622. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  1623. return -1;
  1624. }
  1625. if (fwrite(buf, TORRENT_CRC_LEN, 1, out) != 1) {
  1626. _zip_error_set(&za->error, ZIP_ER_WRITE, errno);
  1627. return -1;
  1628. }
  1629. return 0;
  1630. }
  1631. static int
  1632. _zip_cdir_set_comment(struct zip_cdir *dest, struct zip *src)
  1633. {
  1634. if (src->ch_comment_len != -1) {
  1635. dest->comment = _zip_memdup(src->ch_comment,
  1636. src->ch_comment_len, &src->error);
  1637. if (dest->comment == NULL)
  1638. return -1;
  1639. dest->comment_len = src->ch_comment_len;
  1640. } else {
  1641. if (src->cdir && src->cdir->comment) {
  1642. dest->comment = _zip_memdup(src->cdir->comment,
  1643. src->cdir->comment_len, &src->error);
  1644. if (dest->comment == NULL)
  1645. return -1;
  1646. dest->comment_len = src->cdir->comment_len;
  1647. }
  1648. }
  1649. return 0;
  1650. }
  1651. static int
  1652. _zip_changed(struct zip *za, int *survivorsp)
  1653. {
  1654. int changed, i, survivors;
  1655. changed = survivors = 0;
  1656. if (za->ch_comment_len != -1
  1657. || za->ch_flags != za->flags)
  1658. changed = 1;
  1659. for (i=0; i<za->nentry; i++) {
  1660. if ((za->entry[i].state != ZIP_ST_UNCHANGED)
  1661. || (za->entry[i].ch_comment_len != -1))
  1662. changed = 1;
  1663. if (za->entry[i].state != ZIP_ST_DELETED)
  1664. survivors++;
  1665. }
  1666. *survivorsp = survivors;
  1667. return changed;
  1668. }
  1669. static char *
  1670. _zip_create_temp_output(struct zip *za, FILE **outp)
  1671. {
  1672. char *temp;
  1673. int tfd;
  1674. FILE *tfp;
  1675. if ((temp=(char *)malloc(strlen(za->zn)+8)) == NULL) {
  1676. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  1677. return NULL;
  1678. }
  1679. sprintf(temp, "%s.XXXXXX", za->zn);
  1680. if ((tfd=mkstemp(temp)) == -1) {
  1681. _zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
  1682. free(temp);
  1683. return NULL;
  1684. }
  1685. if ((tfp=fdopen(tfd, "r+b")) == NULL) {
  1686. _zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
  1687. close(tfd);
  1688. remove(temp);
  1689. free(temp);
  1690. return NULL;
  1691. }
  1692. *outp = tfp;
  1693. return temp;
  1694. }
  1695. static int
  1696. _zip_torrentzip_cmp(const void *a, const void *b)
  1697. {
  1698. return strcasecmp(((const struct filelist *)a)->name,
  1699. ((const struct filelist *)b)->name);
  1700. }
  1701. ZIP_EXTERN int
  1702. zip_add_dir(struct zip *za, const char *name)
  1703. {
  1704. int len, ret;
  1705. char *s;
  1706. struct zip_source *source;
  1707. if (name == NULL) {
  1708. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  1709. return -1;
  1710. }
  1711. s = NULL;
  1712. len = strlen(name);
  1713. if (name[len-1] != '/') {
  1714. if ((s=(char *)malloc(len+2)) == NULL) {
  1715. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  1716. return -1;
  1717. }
  1718. strcpy(s, name);
  1719. s[len] = '/';
  1720. s[len+1] = '\0';
  1721. }
  1722. if ((source=zip_source_buffer(za, NULL, 0, 0)) == NULL) {
  1723. free(s);
  1724. return -1;
  1725. }
  1726. ret = _zip_replace(za, -1, s ? s : name, source);
  1727. free(s);
  1728. if (ret < 0)
  1729. zip_source_free(source);
  1730. return ret;
  1731. }
  1732. ZIP_EXTERN int
  1733. zip_error_to_str(char *buf, size_t len, int ze, int se)
  1734. {
  1735. const char *zs, *ss;
  1736. if (ze < 0 || ze >= _zip_nerr_str)
  1737. return snprintf(buf, len, "Unknown error %d", ze);
  1738. zs = _zip_err_str[ze];
  1739. switch (_zip_err_type[ze]) {
  1740. case ZIP_ET_SYS:
  1741. ss = strerror(se);
  1742. break;
  1743. case ZIP_ET_ZLIB:
  1744. ss = zError(se);
  1745. break;
  1746. default:
  1747. ss = NULL;
  1748. }
  1749. return snprintf(buf, len, "%s%s%s",
  1750. zs, (ss ? ": " : ""), (ss ? ss : ""));
  1751. }
  1752. ZIP_EXTERN void
  1753. zip_file_error_clear(struct zip_file *zf)
  1754. {
  1755. _zip_error_clear(&zf->error);
  1756. }
  1757. ZIP_EXTERN int
  1758. zip_fclose(struct zip_file *zf)
  1759. {
  1760. int i, ret;
  1761. if (zf->zstr)
  1762. inflateEnd(zf->zstr);
  1763. free(zf->buffer);
  1764. free(zf->zstr);
  1765. for (i=0; i<zf->za->nfile; i++) {
  1766. if (zf->za->file[i] == zf) {
  1767. zf->za->file[i] = zf->za->file[zf->za->nfile-1];
  1768. zf->za->nfile--;
  1769. break;
  1770. }
  1771. }
  1772. ret = 0;
  1773. if (zf->error.zip_err)
  1774. ret = zf->error.zip_err;
  1775. else if ((zf->flags & ZIP_ZF_CRC) && (zf->flags & ZIP_ZF_EOF)) {
  1776. /* if EOF, compare CRC */
  1777. if (zf->crc_orig != zf->crc)
  1778. ret = ZIP_ER_CRC;
  1779. }
  1780. free(zf);
  1781. return ret;
  1782. }
  1783. int
  1784. _zip_filerange_crc(FILE *fp, myoff_t start, myoff_t len, uLong *crcp,
  1785. struct zip_error *errp)
  1786. {
  1787. Bytef buf[BUFSIZE];
  1788. size_t n;
  1789. *crcp = crc32(0L, Z_NULL, 0);
  1790. if (fseeko(fp, start, SEEK_SET) != 0) {
  1791. _zip_error_set(errp, ZIP_ER_SEEK, errno);
  1792. return -1;
  1793. }
  1794. while (len > 0) {
  1795. n = len > BUFSIZE ? BUFSIZE : len;
  1796. if ((n=fread(buf, 1, n, fp)) <= 0) {
  1797. _zip_error_set(errp, ZIP_ER_READ, errno);
  1798. return -1;
  1799. }
  1800. *crcp = crc32(*crcp, buf, n);
  1801. len-= n;
  1802. }
  1803. return 0;
  1804. }
  1805. ZIP_EXTERN const char *
  1806. zip_file_strerror(struct zip_file *zf)
  1807. {
  1808. return _zip_error_strerror(&zf->error);
  1809. }
  1810. /* _zip_file_get_offset(za, ze):
  1811. Returns the offset of the file data for entry ze.
  1812. On error, fills in za->error and returns 0.
  1813. */
  1814. unsigned int
  1815. _zip_file_get_offset(struct zip *za, int idx)
  1816. {
  1817. struct zip_dirent de;
  1818. unsigned int offset;
  1819. offset = za->cdir->entry[idx].offset;
  1820. if (fseeko(za->zp, offset, SEEK_SET) != 0) {
  1821. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  1822. return 0;
  1823. }
  1824. if (_zip_dirent_read(&de, za->zp, NULL, 0, 1, &za->error) != 0)
  1825. return 0;
  1826. offset += LENTRYSIZE + de.filename_len + de.extrafield_len;
  1827. _zip_dirent_finalize(&de);
  1828. return offset;
  1829. }
  1830. ZIP_EXTERN void
  1831. zip_file_error_get(struct zip_file *zf, int *zep, int *sep)
  1832. {
  1833. _zip_error_get(&zf->error, zep, sep);
  1834. }
  1835. static struct zip_file *_zip_file_new(struct zip *za);
  1836. ZIP_EXTERN struct zip_file *
  1837. zip_fopen_index(struct zip *za, int fileno, int flags)
  1838. {
  1839. int len, ret;
  1840. int zfflags;
  1841. struct zip_file *zf;
  1842. if ((fileno < 0) || (fileno >= za->nentry)) {
  1843. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  1844. return NULL;
  1845. }
  1846. if ((flags & ZIP_FL_UNCHANGED) == 0
  1847. && ZIP_ENTRY_DATA_CHANGED(za->entry+fileno)) {
  1848. _zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
  1849. return NULL;
  1850. }
  1851. if (fileno >= za->cdir->nentry) {
  1852. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  1853. return NULL;
  1854. }
  1855. zfflags = 0;
  1856. switch (za->cdir->entry[fileno].comp_method) {
  1857. case ZIP_CM_STORE:
  1858. zfflags |= ZIP_ZF_CRC;
  1859. break;
  1860. case ZIP_CM_DEFLATE:
  1861. if ((flags & ZIP_FL_COMPRESSED) == 0)
  1862. zfflags |= ZIP_ZF_CRC | ZIP_ZF_DECOMP;
  1863. break;
  1864. default:
  1865. if ((flags & ZIP_FL_COMPRESSED) == 0) {
  1866. _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
  1867. return NULL;
  1868. }
  1869. break;
  1870. }
  1871. zf = _zip_file_new(za);
  1872. zf->flags = zfflags;
  1873. /* zf->name = za->cdir->entry[fileno].filename; */
  1874. zf->method = za->cdir->entry[fileno].comp_method;
  1875. zf->bytes_left = za->cdir->entry[fileno].uncomp_size;
  1876. zf->cbytes_left = za->cdir->entry[fileno].comp_size;
  1877. zf->crc_orig = za->cdir->entry[fileno].crc;
  1878. if ((zf->fpos=_zip_file_get_offset(za, fileno)) == 0) {
  1879. zip_fclose(zf);
  1880. return NULL;
  1881. }
  1882. if ((zf->flags & ZIP_ZF_DECOMP) == 0)
  1883. zf->bytes_left = zf->cbytes_left;
  1884. else {
  1885. if ((zf->buffer=(char *)malloc(BUFSIZE)) == NULL) {
  1886. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  1887. zip_fclose(zf);
  1888. return NULL;
  1889. }
  1890. len = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
  1891. if (len <= 0) {
  1892. _zip_error_copy(&za->error, &zf->error);
  1893. zip_fclose(zf);
  1894. return NULL;
  1895. }
  1896. if ((zf->zstr = (z_stream *)malloc(sizeof(z_stream))) == NULL) {
  1897. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  1898. zip_fclose(zf);
  1899. return NULL;
  1900. }
  1901. zf->zstr->zalloc = Z_NULL;
  1902. zf->zstr->zfree = Z_NULL;
  1903. zf->zstr->opaque = NULL;
  1904. zf->zstr->next_in = (Bytef *)zf->buffer;
  1905. zf->zstr->avail_in = len;
  1906. /* negative value to tell zlib that there is no header */
  1907. if ((ret=inflateInit2(zf->zstr, -MAX_WBITS)) != Z_OK) {
  1908. _zip_error_set(&za->error, ZIP_ER_ZLIB, ret);
  1909. zip_fclose(zf);
  1910. return NULL;
  1911. }
  1912. }
  1913. return zf;
  1914. }
  1915. int
  1916. _zip_file_fillbuf(void *buf, size_t buflen, struct zip_file *zf)
  1917. {
  1918. int i, j;
  1919. if (zf->error.zip_err != ZIP_ER_OK)
  1920. return -1;
  1921. if ((zf->flags & ZIP_ZF_EOF) || zf->cbytes_left <= 0 || buflen <= 0)
  1922. return 0;
  1923. if (fseeko(zf->za->zp, zf->fpos, SEEK_SET) < 0) {
  1924. _zip_error_set(&zf->error, ZIP_ER_SEEK, errno);
  1925. return -1;
  1926. }
  1927. if (buflen < zf->cbytes_left)
  1928. i = buflen;
  1929. else
  1930. i = zf->cbytes_left;
  1931. j = fread(buf, 1, i, zf->za->zp);
  1932. if (j == 0) {
  1933. _zip_error_set(&zf->error, ZIP_ER_EOF, 0);
  1934. j = -1;
  1935. }
  1936. else if (j < 0)
  1937. _zip_error_set(&zf->error, ZIP_ER_READ, errno);
  1938. else {
  1939. zf->fpos += j;
  1940. zf->cbytes_left -= j;
  1941. }
  1942. return j;
  1943. }
  1944. static struct zip_file *
  1945. _zip_file_new(struct zip *za)
  1946. {
  1947. struct zip_file *zf, **file;
  1948. int n;
  1949. if ((zf=(struct zip_file *)malloc(sizeof(struct zip_file))) == NULL) {
  1950. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  1951. return NULL;
  1952. }
  1953. if (za->nfile >= za->nfile_alloc-1) {
  1954. n = za->nfile_alloc + 10;
  1955. file = (struct zip_file **)realloc(za->file,
  1956. n*sizeof(struct zip_file *));
  1957. if (file == NULL) {
  1958. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  1959. free(zf);
  1960. return NULL;
  1961. }
  1962. za->nfile_alloc = n;
  1963. za->file = file;
  1964. }
  1965. za->file[za->nfile++] = zf;
  1966. zf->za = za;
  1967. _zip_error_init(&zf->error);
  1968. zf->flags = 0;
  1969. zf->crc = crc32(0L, Z_NULL, 0);
  1970. zf->crc_orig = 0;
  1971. zf->method = -1;
  1972. zf->bytes_left = zf->cbytes_left = 0;
  1973. zf->fpos = 0;
  1974. zf->buffer = NULL;
  1975. zf->zstr = NULL;
  1976. return zf;
  1977. }
  1978. ZIP_EXTERN struct zip_file *
  1979. zip_fopen(struct zip *za, const char *fname, int flags)
  1980. {
  1981. int idx;
  1982. if ((idx=zip_name_locate(za, fname, flags)) < 0)
  1983. return NULL;
  1984. return zip_fopen_index(za, idx, flags);
  1985. }
  1986. ZIP_EXTERN int
  1987. zip_set_file_comment(struct zip *za, int idx, const char *comment, int len)
  1988. {
  1989. char *tmpcom;
  1990. if (idx < 0 || idx >= za->nentry
  1991. || len < 0 || len > MAXCOMLEN
  1992. || (len > 0 && comment == NULL)) {
  1993. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  1994. return -1;
  1995. }
  1996. if (len > 0) {
  1997. if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
  1998. return -1;
  1999. }
  2000. else
  2001. tmpcom = NULL;
  2002. free(za->entry[idx].ch_comment);
  2003. za->entry[idx].ch_comment = tmpcom;
  2004. za->entry[idx].ch_comment_len = len;
  2005. return 0;
  2006. }
  2007. ZIP_EXTERN struct zip_source *
  2008. zip_source_file(struct zip *za, const char *fname, myoff_t start, myoff_t len)
  2009. {
  2010. if (za == NULL)
  2011. return NULL;
  2012. if (fname == NULL || start < 0 || len < -1) {
  2013. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2014. return NULL;
  2015. }
  2016. return _zip_source_file_or_p(za, fname, NULL, start, len);
  2017. }
  2018. struct read_data {
  2019. const char *buf, *data, *end;
  2020. time_t mtime;
  2021. int freep;
  2022. };
  2023. static ssize_t read_data(void *state, void *data, size_t len,
  2024. enum zip_source_cmd cmd);
  2025. ZIP_EXTERN struct zip_source *
  2026. zip_source_buffer(struct zip *za, const void *data, myoff_t len, int freep)
  2027. {
  2028. struct read_data *f;
  2029. struct zip_source *zs;
  2030. if (za == NULL)
  2031. return NULL;
  2032. if (len < 0 || (data == NULL && len > 0)) {
  2033. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2034. return NULL;
  2035. }
  2036. if ((f=(struct read_data *)malloc(sizeof(*f))) == NULL) {
  2037. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  2038. return NULL;
  2039. }
  2040. f->data = (const char *)data;
  2041. f->end = ((const char *)data)+len;
  2042. f->freep = freep;
  2043. f->mtime = time(NULL);
  2044. if ((zs=zip_source_function(za, read_data, f)) == NULL) {
  2045. free(f);
  2046. return NULL;
  2047. }
  2048. return zs;
  2049. }
  2050. static ssize_t
  2051. read_data(void *state, void *data, size_t len, enum zip_source_cmd cmd)
  2052. {
  2053. struct read_data *z;
  2054. char *buf;
  2055. size_t n;
  2056. z = (struct read_data *)state;
  2057. buf = (char *)data;
  2058. switch (cmd) {
  2059. case ZIP_SOURCE_OPEN:
  2060. z->buf = z->data;
  2061. return 0;
  2062. case ZIP_SOURCE_READ:
  2063. n = z->end - z->buf;
  2064. if (n > len)
  2065. n = len;
  2066. if (n) {
  2067. memcpy(buf, z->buf, n);
  2068. z->buf += n;
  2069. }
  2070. return n;
  2071. case ZIP_SOURCE_CLOSE:
  2072. return 0;
  2073. case ZIP_SOURCE_STAT:
  2074. {
  2075. struct zip_stat *st;
  2076. if (len < sizeof(*st))
  2077. return -1;
  2078. st = (struct zip_stat *)data;
  2079. zip_stat_init(st);
  2080. st->mtime = z->mtime;
  2081. st->size = z->end - z->data;
  2082. return sizeof(*st);
  2083. }
  2084. case ZIP_SOURCE_ERROR:
  2085. {
  2086. int *e;
  2087. if (len < sizeof(int)*2)
  2088. return -1;
  2089. e = (int *)data;
  2090. e[0] = e[1] = 0;
  2091. }
  2092. return sizeof(int)*2;
  2093. case ZIP_SOURCE_FREE:
  2094. if (z->freep) {
  2095. free((void *)z->data);
  2096. z->data = NULL;
  2097. }
  2098. free(z);
  2099. return 0;
  2100. default:
  2101. ;
  2102. }
  2103. return -1;
  2104. }
  2105. int
  2106. _zip_set_name(struct zip *za, int idx, const char *name)
  2107. {
  2108. char *s;
  2109. int i;
  2110. if (idx < 0 || idx >= za->nentry || name == NULL) {
  2111. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2112. return -1;
  2113. }
  2114. if ((i=_zip_name_locate(za, name, 0, NULL)) != -1 && i != idx) {
  2115. _zip_error_set(&za->error, ZIP_ER_EXISTS, 0);
  2116. return -1;
  2117. }
  2118. /* no effective name change */
  2119. if (i == idx)
  2120. return 0;
  2121. if ((s=strdup(name)) == NULL) {
  2122. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  2123. return -1;
  2124. }
  2125. if (za->entry[idx].state == ZIP_ST_UNCHANGED)
  2126. za->entry[idx].state = ZIP_ST_RENAMED;
  2127. free(za->entry[idx].ch_filename);
  2128. za->entry[idx].ch_filename = s;
  2129. return 0;
  2130. }
  2131. ZIP_EXTERN int
  2132. zip_set_archive_flag(struct zip *za, int flag, int value)
  2133. {
  2134. if (value)
  2135. za->ch_flags |= flag;
  2136. else
  2137. za->ch_flags &= ~flag;
  2138. return 0;
  2139. }
  2140. void
  2141. _zip_unchange_data(struct zip_entry *ze)
  2142. {
  2143. if (ze->source) {
  2144. (void)ze->source->f(ze->source->ud, NULL, 0, ZIP_SOURCE_FREE);
  2145. free(ze->source);
  2146. ze->source = NULL;
  2147. }
  2148. ze->state = ze->ch_filename ? ZIP_ST_RENAMED : ZIP_ST_UNCHANGED;
  2149. }
  2150. ZIP_EXTERN int
  2151. zip_unchange_archive(struct zip *za)
  2152. {
  2153. free(za->ch_comment);
  2154. za->ch_comment = NULL;
  2155. za->ch_comment_len = -1;
  2156. za->ch_flags = za->flags;
  2157. return 0;
  2158. }
  2159. ZIP_EXTERN int
  2160. zip_unchange(struct zip *za, int idx)
  2161. {
  2162. return _zip_unchange(za, idx, 0);
  2163. }
  2164. int
  2165. _zip_unchange(struct zip *za, int idx, int allow_duplicates)
  2166. {
  2167. int i;
  2168. if (idx < 0 || idx >= za->nentry) {
  2169. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2170. return -1;
  2171. }
  2172. if (za->entry[idx].ch_filename) {
  2173. if (!allow_duplicates) {
  2174. i = _zip_name_locate(za,
  2175. _zip_get_name(za, idx, ZIP_FL_UNCHANGED, NULL),
  2176. 0, NULL);
  2177. if (i != -1 && i != idx) {
  2178. _zip_error_set(&za->error, ZIP_ER_EXISTS, 0);
  2179. return -1;
  2180. }
  2181. }
  2182. free(za->entry[idx].ch_filename);
  2183. za->entry[idx].ch_filename = NULL;
  2184. }
  2185. free(za->entry[idx].ch_comment);
  2186. za->entry[idx].ch_comment = NULL;
  2187. za->entry[idx].ch_comment_len = -1;
  2188. _zip_unchange_data(za->entry+idx);
  2189. return 0;
  2190. }
  2191. ZIP_EXTERN int
  2192. zip_unchange_all(struct zip *za)
  2193. {
  2194. int ret, i;
  2195. ret = 0;
  2196. for (i=0; i<za->nentry; i++)
  2197. ret |= _zip_unchange(za, i, 1);
  2198. ret |= zip_unchange_archive(za);
  2199. return ret;
  2200. }
  2201. ZIP_EXTERN int
  2202. zip_set_archive_comment(struct zip *za, const char *comment, int len)
  2203. {
  2204. char *tmpcom;
  2205. if (len < 0 || len > MAXCOMLEN
  2206. || (len > 0 && comment == NULL)) {
  2207. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2208. return -1;
  2209. }
  2210. if (len > 0) {
  2211. if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
  2212. return -1;
  2213. }
  2214. else
  2215. tmpcom = NULL;
  2216. free(za->ch_comment);
  2217. za->ch_comment = tmpcom;
  2218. za->ch_comment_len = len;
  2219. return 0;
  2220. }
  2221. ZIP_EXTERN int
  2222. zip_replace(struct zip *za, int idx, struct zip_source *source)
  2223. {
  2224. if (idx < 0 || idx >= za->nentry || source == NULL) {
  2225. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2226. return -1;
  2227. }
  2228. if (_zip_replace(za, idx, NULL, source) == -1)
  2229. return -1;
  2230. return 0;
  2231. }
  2232. int
  2233. _zip_replace(struct zip *za, int idx, const char *name,
  2234. struct zip_source *source)
  2235. {
  2236. if (idx == -1) {
  2237. if (_zip_entry_new(za) == NULL)
  2238. return -1;
  2239. idx = za->nentry - 1;
  2240. }
  2241. _zip_unchange_data(za->entry+idx);
  2242. if (name && _zip_set_name(za, idx, name) != 0)
  2243. return -1;
  2244. za->entry[idx].state = ((za->cdir == NULL || idx >= za->cdir->nentry)
  2245. ? ZIP_ST_ADDED : ZIP_ST_REPLACED);
  2246. za->entry[idx].source = source;
  2247. return idx;
  2248. }
  2249. ZIP_EXTERN int
  2250. zip_rename(struct zip *za, int idx, const char *name)
  2251. {
  2252. const char *old_name;
  2253. int old_is_dir, new_is_dir;
  2254. if (idx >= za->nentry || idx < 0 || name[0] == '\0') {
  2255. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2256. return -1;
  2257. }
  2258. if ((old_name=zip_get_name(za, idx, 0)) == NULL)
  2259. return -1;
  2260. new_is_dir = (name[strlen(name)-1] == '/');
  2261. old_is_dir = (old_name[strlen(old_name)-1] == '/');
  2262. if (new_is_dir != old_is_dir) {
  2263. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2264. return -1;
  2265. }
  2266. return _zip_set_name(za, idx, name);
  2267. }
  2268. #include <sys/stat.h>
  2269. #include <errno.h>
  2270. #include <limits.h>
  2271. #include <stdio.h>
  2272. #include <stdlib.h>
  2273. #include <string.h>
  2274. static void set_error(int *, struct zip_error *, int);
  2275. static struct zip *_zip_allocate_new(const char *, int *);
  2276. static int _zip_checkcons(FILE *, struct zip_cdir *, struct zip_error *);
  2277. static void _zip_check_torrentzip(struct zip *);
  2278. static struct zip_cdir *_zip_find_central_dir(FILE *, int, int *, myoff_t);
  2279. static int _zip_file_exists(const char *, int, int *);
  2280. static int _zip_headercomp(struct zip_dirent *, int,
  2281. struct zip_dirent *, int);
  2282. static unsigned char *_zip_memmem(const unsigned char *, int,
  2283. const unsigned char *, int);
  2284. static struct zip_cdir *_zip_readcdir(FILE *, unsigned char *, unsigned char *,
  2285. int, int, struct zip_error *);
  2286. ZIP_EXTERN struct zip *
  2287. zip_open(const char *fn, int flags, int *zep)
  2288. {
  2289. FILE *fp;
  2290. struct zip *za;
  2291. struct zip_cdir *cdir;
  2292. int i;
  2293. myoff_t len;
  2294. switch (_zip_file_exists(fn, flags, zep)) {
  2295. case -1:
  2296. return NULL;
  2297. case 0:
  2298. return _zip_allocate_new(fn, zep);
  2299. default:
  2300. break;
  2301. }
  2302. if ((fp=fopen(fn, "rb")) == NULL) {
  2303. set_error(zep, NULL, ZIP_ER_OPEN);
  2304. return NULL;
  2305. }
  2306. fseeko(fp, 0, SEEK_END);
  2307. len = ftello(fp);
  2308. /* treat empty files as empty archives */
  2309. if (len == 0) {
  2310. if ((za=_zip_allocate_new(fn, zep)) == NULL)
  2311. fclose(fp);
  2312. else
  2313. za->zp = fp;
  2314. return za;
  2315. }
  2316. cdir = _zip_find_central_dir(fp, flags, zep, len);
  2317. if (cdir == NULL) {
  2318. fclose(fp);
  2319. return NULL;
  2320. }
  2321. if ((za=_zip_allocate_new(fn, zep)) == NULL) {
  2322. _zip_cdir_free(cdir);
  2323. fclose(fp);
  2324. return NULL;
  2325. }
  2326. za->cdir = cdir;
  2327. za->zp = fp;
  2328. if ((za->entry=(struct zip_entry *)malloc(sizeof(*(za->entry))
  2329. * cdir->nentry)) == NULL) {
  2330. set_error(zep, NULL, ZIP_ER_MEMORY);
  2331. _zip_free(za);
  2332. return NULL;
  2333. }
  2334. for (i=0; i<cdir->nentry; i++)
  2335. _zip_entry_new(za);
  2336. _zip_check_torrentzip(za);
  2337. za->ch_flags = za->flags;
  2338. return za;
  2339. }
  2340. static void
  2341. set_error(int *zep, struct zip_error *err, int ze)
  2342. {
  2343. int se;
  2344. if (err) {
  2345. _zip_error_get(err, &ze, &se);
  2346. if (zip_error_get_sys_type(ze) == ZIP_ET_SYS)
  2347. errno = se;
  2348. }
  2349. if (zep)
  2350. *zep = ze;
  2351. }
  2352. /* _zip_readcdir:
  2353. tries to find a valid end-of-central-directory at the beginning of
  2354. buf, and then the corresponding central directory entries.
  2355. Returns a struct zip_cdir which contains the central directory
  2356. entries, or NULL if unsuccessful. */
  2357. static struct zip_cdir *
  2358. _zip_readcdir(FILE *fp, unsigned char *buf, unsigned char *eocd, int buflen,
  2359. int flags, struct zip_error *error)
  2360. {
  2361. struct zip_cdir *cd;
  2362. unsigned char *cdp, **bufp;
  2363. int i, comlen, nentry;
  2364. comlen = buf + buflen - eocd - EOCDLEN;
  2365. if (comlen < 0) {
  2366. /* not enough bytes left for comment */
  2367. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  2368. return NULL;
  2369. }
  2370. /* check for end-of-central-dir magic */
  2371. if (memcmp(eocd, EOCD_MAGIC, 4) != 0) {
  2372. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  2373. return NULL;
  2374. }
  2375. if (memcmp(eocd+4, "\0\0\0\0", 4) != 0) {
  2376. _zip_error_set(error, ZIP_ER_MULTIDISK, 0);
  2377. return NULL;
  2378. }
  2379. cdp = eocd + 8;
  2380. /* number of cdir-entries on this disk */
  2381. i = _zip_read2(&cdp);
  2382. /* number of cdir-entries */
  2383. nentry = _zip_read2(&cdp);
  2384. if ((cd=_zip_cdir_new(nentry, error)) == NULL)
  2385. return NULL;
  2386. cd->size = _zip_read4(&cdp);
  2387. cd->offset = _zip_read4(&cdp);
  2388. cd->comment = NULL;
  2389. cd->comment_len = _zip_read2(&cdp);
  2390. if ((comlen < cd->comment_len) || (cd->nentry != i)) {
  2391. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  2392. free(cd);
  2393. return NULL;
  2394. }
  2395. if ((flags & ZIP_CHECKCONS) && comlen != cd->comment_len) {
  2396. _zip_error_set(error, ZIP_ER_INCONS, 0);
  2397. free(cd);
  2398. return NULL;
  2399. }
  2400. if (cd->comment_len) {
  2401. if ((cd->comment=(char *)_zip_memdup(eocd+EOCDLEN,
  2402. cd->comment_len, error))
  2403. == NULL) {
  2404. free(cd);
  2405. return NULL;
  2406. }
  2407. }
  2408. cdp = eocd;
  2409. if (cd->size < (unsigned int)(eocd-buf)) {
  2410. /* if buffer already read in, use it */
  2411. cdp = eocd - cd->size;
  2412. bufp = &cdp;
  2413. }
  2414. else {
  2415. /* go to start of cdir and read it entry by entry */
  2416. bufp = NULL;
  2417. clearerr(fp);
  2418. fseeko(fp, cd->offset, SEEK_SET);
  2419. /* possible consistency check: cd->offset =
  2420. len-(cd->size+cd->comment_len+EOCDLEN) ? */
  2421. if (ferror(fp) || ((unsigned long)ftello(fp) != cd->offset)) {
  2422. /* seek error or offset of cdir wrong */
  2423. if (ferror(fp))
  2424. _zip_error_set(error, ZIP_ER_SEEK, errno);
  2425. else
  2426. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  2427. free(cd);
  2428. return NULL;
  2429. }
  2430. }
  2431. for (i=0; i<cd->nentry; i++) {
  2432. if ((_zip_dirent_read(cd->entry+i, fp, bufp, eocd-cdp, 0,
  2433. error)) < 0) {
  2434. cd->nentry = i;
  2435. _zip_cdir_free(cd);
  2436. return NULL;
  2437. }
  2438. }
  2439. return cd;
  2440. }
  2441. /* _zip_checkcons:
  2442. Checks the consistency of the central directory by comparing central
  2443. directory entries with local headers and checking for plausible
  2444. file and header offsets. Returns -1 if not plausible, else the
  2445. difference between the lowest and the highest fileposition reached */
  2446. static int
  2447. _zip_checkcons(FILE *fp, struct zip_cdir *cd, struct zip_error *error)
  2448. {
  2449. int i;
  2450. unsigned int min, max, j;
  2451. struct zip_dirent temp;
  2452. if (cd->nentry) {
  2453. max = cd->entry[0].offset;
  2454. min = cd->entry[0].offset;
  2455. }
  2456. else
  2457. min = max = 0;
  2458. for (i=0; i<cd->nentry; i++) {
  2459. if (cd->entry[i].offset < min)
  2460. min = cd->entry[i].offset;
  2461. if (min > cd->offset) {
  2462. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  2463. return -1;
  2464. }
  2465. j = cd->entry[i].offset + cd->entry[i].comp_size
  2466. + cd->entry[i].filename_len + LENTRYSIZE;
  2467. if (j > max)
  2468. max = j;
  2469. if (max > cd->offset) {
  2470. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  2471. return -1;
  2472. }
  2473. if (fseeko(fp, cd->entry[i].offset, SEEK_SET) != 0) {
  2474. _zip_error_set(error, ZIP_ER_SEEK, 0);
  2475. return -1;
  2476. }
  2477. if (_zip_dirent_read(&temp, fp, NULL, 0, 1, error) == -1)
  2478. return -1;
  2479. if (_zip_headercomp(cd->entry+i, 0, &temp, 1) != 0) {
  2480. _zip_error_set(error, ZIP_ER_INCONS, 0);
  2481. _zip_dirent_finalize(&temp);
  2482. return -1;
  2483. }
  2484. _zip_dirent_finalize(&temp);
  2485. }
  2486. return max - min;
  2487. }
  2488. /* _zip_check_torrentzip:
  2489. check wether ZA has a valid TORRENTZIP comment, i.e. is torrentzipped */
  2490. static void
  2491. _zip_check_torrentzip(struct zip *za)
  2492. {
  2493. uLong crc_got, crc_should;
  2494. char buf[8+1];
  2495. char *end;
  2496. if (za->zp == NULL || za->cdir == NULL)
  2497. return;
  2498. if (za->cdir->comment_len != TORRENT_SIG_LEN+8
  2499. || strncmp(za->cdir->comment, TORRENT_SIG, TORRENT_SIG_LEN) != 0)
  2500. return;
  2501. memcpy(buf, za->cdir->comment+TORRENT_SIG_LEN, 8);
  2502. buf[8] = '\0';
  2503. errno = 0;
  2504. crc_should = strtoul(buf, &end, 16);
  2505. if ((crc_should == UINT_MAX && errno != 0) || (end && *end))
  2506. return;
  2507. if (_zip_filerange_crc(za->zp, za->cdir->offset, za->cdir->size,
  2508. &crc_got, NULL) < 0)
  2509. return;
  2510. if (crc_got == crc_should)
  2511. za->flags |= ZIP_AFL_TORRENT;
  2512. }
  2513. /* _zip_headercomp:
  2514. compares two headers h1 and h2; if they are local headers, set
  2515. local1p or local2p respectively to 1, else 0. Return 0 if they
  2516. are identical, -1 if not. */
  2517. static int
  2518. _zip_headercomp(struct zip_dirent *h1, int local1p, struct zip_dirent *h2,
  2519. int local2p)
  2520. {
  2521. if ((h1->version_needed != h2->version_needed)
  2522. #if 0
  2523. /* some zip-files have different values in local
  2524. and global headers for the bitflags */
  2525. || (h1->bitflags != h2->bitflags)
  2526. #endif
  2527. || (h1->comp_method != h2->comp_method)
  2528. || (h1->last_mod != h2->last_mod)
  2529. || (h1->filename_len != h2->filename_len)
  2530. || !h1->filename || !h2->filename
  2531. || strcmp(h1->filename, h2->filename))
  2532. return -1;
  2533. /* check that CRC and sizes are zero if data descriptor is used */
  2534. if ((h1->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) && local1p
  2535. && (h1->crc != 0
  2536. || h1->comp_size != 0
  2537. || h1->uncomp_size != 0))
  2538. return -1;
  2539. if ((h2->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) && local2p
  2540. && (h2->crc != 0
  2541. || h2->comp_size != 0
  2542. || h2->uncomp_size != 0))
  2543. return -1;
  2544. /* check that CRC and sizes are equal if no data descriptor is used */
  2545. if (((h1->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local1p == 0)
  2546. && ((h2->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local2p == 0)) {
  2547. if ((h1->crc != h2->crc)
  2548. || (h1->comp_size != h2->comp_size)
  2549. || (h1->uncomp_size != h2->uncomp_size))
  2550. return -1;
  2551. }
  2552. if ((local1p == local2p)
  2553. && ((h1->extrafield_len != h2->extrafield_len)
  2554. || (h1->extrafield_len && h2->extrafield
  2555. && memcmp(h1->extrafield, h2->extrafield,
  2556. h1->extrafield_len))))
  2557. return -1;
  2558. /* if either is local, nothing more to check */
  2559. if (local1p || local2p)
  2560. return 0;
  2561. if ((h1->version_madeby != h2->version_madeby)
  2562. || (h1->disk_number != h2->disk_number)
  2563. || (h1->int_attrib != h2->int_attrib)
  2564. || (h1->ext_attrib != h2->ext_attrib)
  2565. || (h1->offset != h2->offset)
  2566. || (h1->comment_len != h2->comment_len)
  2567. || (h1->comment_len && h2->comment
  2568. && memcmp(h1->comment, h2->comment, h1->comment_len)))
  2569. return -1;
  2570. return 0;
  2571. }
  2572. static struct zip *
  2573. _zip_allocate_new(const char *fn, int *zep)
  2574. {
  2575. struct zip *za;
  2576. struct zip_error error;
  2577. if ((za=_zip_new(&error)) == NULL) {
  2578. set_error(zep, &error, 0);
  2579. return NULL;
  2580. }
  2581. za->zn = strdup(fn);
  2582. if (!za->zn) {
  2583. _zip_free(za);
  2584. set_error(zep, NULL, ZIP_ER_MEMORY);
  2585. return NULL;
  2586. }
  2587. return za;
  2588. }
  2589. static int
  2590. _zip_file_exists(const char *fn, int flags, int *zep)
  2591. {
  2592. struct stat st;
  2593. if (fn == NULL) {
  2594. set_error(zep, NULL, ZIP_ER_INVAL);
  2595. return -1;
  2596. }
  2597. if (stat(fn, &st) != 0) {
  2598. if (flags & ZIP_CREATE)
  2599. return 0;
  2600. else {
  2601. set_error(zep, NULL, ZIP_ER_OPEN);
  2602. return -1;
  2603. }
  2604. }
  2605. else if ((flags & ZIP_EXCL)) {
  2606. set_error(zep, NULL, ZIP_ER_EXISTS);
  2607. return -1;
  2608. }
  2609. /* ZIP_CREATE gets ignored if file exists and not ZIP_EXCL,
  2610. just like open() */
  2611. return 1;
  2612. }
  2613. static struct zip_cdir *
  2614. _zip_find_central_dir(FILE *fp, int flags, int *zep, myoff_t len)
  2615. {
  2616. struct zip_cdir *cdir, *cdirnew;
  2617. unsigned char *buf, *match;
  2618. int a, best, buflen, i;
  2619. struct zip_error zerr;
  2620. i = fseeko(fp, -(len < CDBUFSIZE ? len : CDBUFSIZE), SEEK_END);
  2621. if (i == -1 && errno != EFBIG) {
  2622. /* seek before start of file on my machine */
  2623. set_error(zep, NULL, ZIP_ER_SEEK);
  2624. return NULL;
  2625. }
  2626. /* 64k is too much for stack */
  2627. if ((buf=(unsigned char *)malloc(CDBUFSIZE)) == NULL) {
  2628. set_error(zep, NULL, ZIP_ER_MEMORY);
  2629. return NULL;
  2630. }
  2631. clearerr(fp);
  2632. buflen = fread(buf, 1, CDBUFSIZE, fp);
  2633. if (ferror(fp)) {
  2634. set_error(zep, NULL, ZIP_ER_READ);
  2635. free(buf);
  2636. return NULL;
  2637. }
  2638. best = -1;
  2639. cdir = NULL;
  2640. match = buf;
  2641. _zip_error_set(&zerr, ZIP_ER_NOZIP, 0);
  2642. while ((match=_zip_memmem(match, buflen-(match-buf)-18,
  2643. (const unsigned char *)EOCD_MAGIC, 4))!=NULL) {
  2644. /* found match -- check, if good */
  2645. /* to avoid finding the same match all over again */
  2646. match++;
  2647. if ((cdirnew=_zip_readcdir(fp, buf, match-1, buflen, flags,
  2648. &zerr)) == NULL)
  2649. continue;
  2650. if (cdir) {
  2651. if (best <= 0)
  2652. best = _zip_checkcons(fp, cdir, &zerr);
  2653. a = _zip_checkcons(fp, cdirnew, &zerr);
  2654. if (best < a) {
  2655. _zip_cdir_free(cdir);
  2656. cdir = cdirnew;
  2657. best = a;
  2658. }
  2659. else
  2660. _zip_cdir_free(cdirnew);
  2661. }
  2662. else {
  2663. cdir = cdirnew;
  2664. if (flags & ZIP_CHECKCONS)
  2665. best = _zip_checkcons(fp, cdir, &zerr);
  2666. else
  2667. best = 0;
  2668. }
  2669. cdirnew = NULL;
  2670. }
  2671. free(buf);
  2672. if (best < 0) {
  2673. set_error(zep, &zerr, 0);
  2674. _zip_cdir_free(cdir);
  2675. return NULL;
  2676. }
  2677. return cdir;
  2678. }
  2679. static unsigned char *
  2680. _zip_memmem(const unsigned char *big, int biglen, const unsigned char *little,
  2681. int littlelen)
  2682. {
  2683. const unsigned char *p;
  2684. if ((biglen < littlelen) || (littlelen == 0))
  2685. return NULL;
  2686. p = big-1;
  2687. while ((p=(const unsigned char *)
  2688. memchr(p+1, little[0], (size_t)(big-(p+1)+biglen-littlelen+1)))
  2689. != NULL) {
  2690. if (memcmp(p+1, little+1, littlelen-1)==0)
  2691. return (unsigned char *)p;
  2692. }
  2693. return NULL;
  2694. }
  2695. /* _zip_new:
  2696. creates a new zipfile struct, and sets the contents to zero; returns
  2697. the new struct. */
  2698. struct zip *
  2699. _zip_new(struct zip_error *error)
  2700. {
  2701. struct zip *za;
  2702. za = (struct zip *)malloc(sizeof(struct zip));
  2703. if (!za) {
  2704. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  2705. return NULL;
  2706. }
  2707. za->zn = NULL;
  2708. za->zp = NULL;
  2709. _zip_error_init(&za->error);
  2710. za->cdir = NULL;
  2711. za->ch_comment = NULL;
  2712. za->ch_comment_len = -1;
  2713. za->nentry = za->nentry_alloc = 0;
  2714. za->entry = NULL;
  2715. za->nfile = za->nfile_alloc = 0;
  2716. za->file = NULL;
  2717. za->flags = za->ch_flags = 0;
  2718. return za;
  2719. }
  2720. void *
  2721. _zip_memdup(const void *mem, size_t len, struct zip_error *error)
  2722. {
  2723. void *ret;
  2724. ret = malloc(len);
  2725. if (!ret) {
  2726. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  2727. return NULL;
  2728. }
  2729. memcpy(ret, mem, len);
  2730. return ret;
  2731. }
  2732. ZIP_EXTERN int
  2733. zip_get_num_files(struct zip *za)
  2734. {
  2735. if (za == NULL)
  2736. return -1;
  2737. return za->nentry;
  2738. }
  2739. ZIP_EXTERN const char *
  2740. zip_get_name(struct zip *za, int idx, int flags)
  2741. {
  2742. return _zip_get_name(za, idx, flags, &za->error);
  2743. }
  2744. const char *
  2745. _zip_get_name(struct zip *za, int idx, int flags, struct zip_error *error)
  2746. {
  2747. if (idx < 0 || idx >= za->nentry) {
  2748. _zip_error_set(error, ZIP_ER_INVAL, 0);
  2749. return NULL;
  2750. }
  2751. if ((flags & ZIP_FL_UNCHANGED) == 0) {
  2752. if (za->entry[idx].state == ZIP_ST_DELETED) {
  2753. _zip_error_set(error, ZIP_ER_DELETED, 0);
  2754. return NULL;
  2755. }
  2756. if (za->entry[idx].ch_filename)
  2757. return za->entry[idx].ch_filename;
  2758. }
  2759. if (za->cdir == NULL || idx >= za->cdir->nentry) {
  2760. _zip_error_set(error, ZIP_ER_INVAL, 0);
  2761. return NULL;
  2762. }
  2763. return za->cdir->entry[idx].filename;
  2764. }
  2765. ZIP_EXTERN const char *
  2766. zip_get_file_comment(struct zip *za, int idx, int *lenp, int flags)
  2767. {
  2768. if (idx < 0 || idx >= za->nentry) {
  2769. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2770. return NULL;
  2771. }
  2772. if ((flags & ZIP_FL_UNCHANGED)
  2773. || (za->entry[idx].ch_comment_len == -1)) {
  2774. if (lenp != NULL)
  2775. *lenp = za->cdir->entry[idx].comment_len;
  2776. return za->cdir->entry[idx].comment;
  2777. }
  2778. if (lenp != NULL)
  2779. *lenp = za->entry[idx].ch_comment_len;
  2780. return za->entry[idx].ch_comment;
  2781. }
  2782. ZIP_EXTERN int
  2783. zip_get_archive_flag(struct zip *za, int flag, int flags)
  2784. {
  2785. int fl;
  2786. fl = (flags & ZIP_FL_UNCHANGED) ? za->flags : za->ch_flags;
  2787. return (fl & flag) ? 1 : 0;
  2788. }
  2789. ZIP_EXTERN const char *
  2790. zip_get_archive_comment(struct zip *za, int *lenp, int flags)
  2791. {
  2792. if ((flags & ZIP_FL_UNCHANGED)
  2793. || (za->ch_comment_len == -1)) {
  2794. if (za->cdir) {
  2795. if (lenp != NULL)
  2796. *lenp = za->cdir->comment_len;
  2797. return za->cdir->comment;
  2798. }
  2799. else {
  2800. if (lenp != NULL)
  2801. *lenp = -1;
  2802. return NULL;
  2803. }
  2804. }
  2805. if (lenp != NULL)
  2806. *lenp = za->ch_comment_len;
  2807. return za->ch_comment;
  2808. }
  2809. /* _zip_free:
  2810. frees the space allocated to a zipfile struct, and closes the
  2811. corresponding file. */
  2812. void
  2813. _zip_free(struct zip *za)
  2814. {
  2815. int i;
  2816. if (za == NULL)
  2817. return;
  2818. if (za->zn)
  2819. free(za->zn);
  2820. if (za->zp)
  2821. fclose(za->zp);
  2822. _zip_cdir_free(za->cdir);
  2823. if (za->entry) {
  2824. for (i=0; i<za->nentry; i++) {
  2825. _zip_entry_free(za->entry+i);
  2826. }
  2827. free(za->entry);
  2828. }
  2829. for (i=0; i<za->nfile; i++) {
  2830. if (za->file[i]->error.zip_err == ZIP_ER_OK) {
  2831. _zip_error_set(&za->file[i]->error, ZIP_ER_ZIPCLOSED, 0);
  2832. za->file[i]->za = NULL;
  2833. }
  2834. }
  2835. free(za->file);
  2836. free(za);
  2837. return;
  2838. }
  2839. ZIP_EXTERN ssize_t
  2840. zip_fread(struct zip_file *zf, void *outbuf, size_t toread)
  2841. {
  2842. int ret;
  2843. size_t out_before, len;
  2844. int i;
  2845. if (!zf)
  2846. return -1;
  2847. if (zf->error.zip_err != 0)
  2848. return -1;
  2849. if ((zf->flags & ZIP_ZF_EOF) || (toread == 0))
  2850. return 0;
  2851. if (zf->bytes_left == 0) {
  2852. zf->flags |= ZIP_ZF_EOF;
  2853. if (zf->flags & ZIP_ZF_CRC) {
  2854. if (zf->crc != zf->crc_orig) {
  2855. _zip_error_set(&zf->error, ZIP_ER_CRC, 0);
  2856. return -1;
  2857. }
  2858. }
  2859. return 0;
  2860. }
  2861. if ((zf->flags & ZIP_ZF_DECOMP) == 0) {
  2862. ret = _zip_file_fillbuf(outbuf, toread, zf);
  2863. if (ret > 0) {
  2864. if (zf->flags & ZIP_ZF_CRC)
  2865. zf->crc = crc32(zf->crc, (Bytef *)outbuf, ret);
  2866. zf->bytes_left -= ret;
  2867. }
  2868. return ret;
  2869. }
  2870. zf->zstr->next_out = (Bytef *)outbuf;
  2871. zf->zstr->avail_out = toread;
  2872. out_before = zf->zstr->total_out;
  2873. /* endless loop until something has been accomplished */
  2874. for (;;) {
  2875. ret = inflate(zf->zstr, Z_SYNC_FLUSH);
  2876. switch (ret) {
  2877. case Z_OK:
  2878. case Z_STREAM_END:
  2879. /* all ok */
  2880. /* Z_STREAM_END probably won't happen, since we didn't
  2881. have a header */
  2882. len = zf->zstr->total_out - out_before;
  2883. if (len >= zf->bytes_left || len >= toread) {
  2884. if (zf->flags & ZIP_ZF_CRC)
  2885. zf->crc = crc32(zf->crc, (Bytef *)outbuf, len);
  2886. zf->bytes_left -= len;
  2887. return len;
  2888. }
  2889. break;
  2890. case Z_BUF_ERROR:
  2891. if (zf->zstr->avail_in == 0) {
  2892. i = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
  2893. if (i == 0) {
  2894. _zip_error_set(&zf->error, ZIP_ER_INCONS, 0);
  2895. return -1;
  2896. }
  2897. else if (i < 0)
  2898. return -1;
  2899. zf->zstr->next_in = (Bytef *)zf->buffer;
  2900. zf->zstr->avail_in = i;
  2901. continue;
  2902. }
  2903. /* fallthrough */
  2904. case Z_NEED_DICT:
  2905. case Z_DATA_ERROR:
  2906. case Z_STREAM_ERROR:
  2907. case Z_MEM_ERROR:
  2908. _zip_error_set(&zf->error, ZIP_ER_ZLIB, ret);
  2909. return -1;
  2910. }
  2911. }
  2912. }
  2913. ZIP_EXTERN const char *
  2914. zip_strerror(struct zip *za)
  2915. {
  2916. return _zip_error_strerror(&za->error);
  2917. }
  2918. ZIP_EXTERN void
  2919. zip_stat_init(struct zip_stat *st)
  2920. {
  2921. st->name = NULL;
  2922. st->index = -1;
  2923. st->crc = 0;
  2924. st->mtime = (time_t)-1;
  2925. st->size = -1;
  2926. st->comp_size = -1;
  2927. st->comp_method = ZIP_CM_STORE;
  2928. st->encryption_method = ZIP_EM_NONE;
  2929. }
  2930. ZIP_EXTERN int
  2931. zip_stat_index(struct zip *za, int index, int flags, struct zip_stat *st)
  2932. {
  2933. const char *name;
  2934. if (index < 0 || index >= za->nentry) {
  2935. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2936. return -1;
  2937. }
  2938. if ((name=zip_get_name(za, index, flags)) == NULL)
  2939. return -1;
  2940. if ((flags & ZIP_FL_UNCHANGED) == 0
  2941. && ZIP_ENTRY_DATA_CHANGED(za->entry+index)) {
  2942. if (za->entry[index].source->f(za->entry[index].source->ud,
  2943. st, sizeof(*st), ZIP_SOURCE_STAT) < 0) {
  2944. _zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
  2945. return -1;
  2946. }
  2947. }
  2948. else {
  2949. if (za->cdir == NULL || index >= za->cdir->nentry) {
  2950. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  2951. return -1;
  2952. }
  2953. st->crc = za->cdir->entry[index].crc;
  2954. st->size = za->cdir->entry[index].uncomp_size;
  2955. st->mtime = za->cdir->entry[index].last_mod;
  2956. st->comp_size = za->cdir->entry[index].comp_size;
  2957. st->comp_method = za->cdir->entry[index].comp_method;
  2958. if (za->cdir->entry[index].bitflags & ZIP_GPBF_ENCRYPTED) {
  2959. if (za->cdir->entry[index].bitflags & ZIP_GPBF_STRONG_ENCRYPTION) {
  2960. /* XXX */
  2961. st->encryption_method = ZIP_EM_UNKNOWN;
  2962. }
  2963. else
  2964. st->encryption_method = ZIP_EM_TRAD_PKWARE;
  2965. }
  2966. else
  2967. st->encryption_method = ZIP_EM_NONE;
  2968. /* st->bitflags = za->cdir->entry[index].bitflags; */
  2969. }
  2970. st->index = index;
  2971. st->name = name;
  2972. return 0;
  2973. }
  2974. ZIP_EXTERN int
  2975. zip_stat(struct zip *za, const char *fname, int flags, struct zip_stat *st)
  2976. {
  2977. int idx;
  2978. if ((idx=zip_name_locate(za, fname, flags)) < 0)
  2979. return -1;
  2980. return zip_stat_index(za, idx, flags, st);
  2981. }
  2982. struct read_zip {
  2983. struct zip_file *zf;
  2984. struct zip_stat st;
  2985. myoff_t off, len;
  2986. };
  2987. static ssize_t read_zip(void *st, void *data, size_t len,
  2988. enum zip_source_cmd cmd);
  2989. ZIP_EXTERN struct zip_source *
  2990. zip_source_zip(struct zip *za, struct zip *srcza, int srcidx, int flags,
  2991. myoff_t start, myoff_t len)
  2992. {
  2993. struct zip_error error;
  2994. struct zip_source *zs;
  2995. struct read_zip *p;
  2996. /* XXX: ZIP_FL_RECOMPRESS */
  2997. if (za == NULL)
  2998. return NULL;
  2999. if (srcza == NULL || start < 0 || len < -1 || srcidx < 0 || srcidx >= srcza->nentry) {
  3000. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  3001. return NULL;
  3002. }
  3003. if ((flags & ZIP_FL_UNCHANGED) == 0
  3004. && ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx)) {
  3005. _zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
  3006. return NULL;
  3007. }
  3008. if (len == 0)
  3009. len = -1;
  3010. if (start == 0 && len == -1 && (flags & ZIP_FL_RECOMPRESS) == 0)
  3011. flags |= ZIP_FL_COMPRESSED;
  3012. else
  3013. flags &= ~ZIP_FL_COMPRESSED;
  3014. if ((p=(struct read_zip *)malloc(sizeof(*p))) == NULL) {
  3015. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  3016. return NULL;
  3017. }
  3018. _zip_error_copy(&error, &srcza->error);
  3019. if (zip_stat_index(srcza, srcidx, flags, &p->st) < 0
  3020. || (p->zf=zip_fopen_index(srcza, srcidx, flags)) == NULL) {
  3021. free(p);
  3022. _zip_error_copy(&za->error, &srcza->error);
  3023. _zip_error_copy(&srcza->error, &error);
  3024. return NULL;
  3025. }
  3026. p->off = start;
  3027. p->len = len;
  3028. if ((flags & ZIP_FL_COMPRESSED) == 0) {
  3029. p->st.size = p->st.comp_size = len;
  3030. p->st.comp_method = ZIP_CM_STORE;
  3031. p->st.crc = 0;
  3032. }
  3033. if ((zs=zip_source_function(za, read_zip, p)) == NULL) {
  3034. free(p);
  3035. return NULL;
  3036. }
  3037. return zs;
  3038. }
  3039. static ssize_t
  3040. read_zip(void *state, void *data, size_t len, enum zip_source_cmd cmd)
  3041. {
  3042. struct read_zip *z;
  3043. char b[8192], *buf;
  3044. int i, n;
  3045. z = (struct read_zip *)state;
  3046. buf = (char *)data;
  3047. switch (cmd) {
  3048. case ZIP_SOURCE_OPEN:
  3049. for (n=0; n<z->off; n+= i) {
  3050. i = (z->off-n > sizeof(b) ? sizeof(b) : z->off-n);
  3051. if ((i=zip_fread(z->zf, b, i)) < 0) {
  3052. zip_fclose(z->zf);
  3053. z->zf = NULL;
  3054. return -1;
  3055. }
  3056. }
  3057. return 0;
  3058. case ZIP_SOURCE_READ:
  3059. if (z->len != -1)
  3060. n = len > z->len ? z->len : len;
  3061. else
  3062. n = len;
  3063. if ((i=zip_fread(z->zf, buf, n)) < 0)
  3064. return -1;
  3065. if (z->len != -1)
  3066. z->len -= i;
  3067. return i;
  3068. case ZIP_SOURCE_CLOSE:
  3069. return 0;
  3070. case ZIP_SOURCE_STAT:
  3071. if (len < sizeof(z->st))
  3072. return -1;
  3073. len = sizeof(z->st);
  3074. memcpy(data, &z->st, len);
  3075. return len;
  3076. case ZIP_SOURCE_ERROR:
  3077. {
  3078. int *e;
  3079. if (len < sizeof(int)*2)
  3080. return -1;
  3081. e = (int *)data;
  3082. zip_file_error_get(z->zf, e, e+1);
  3083. }
  3084. return sizeof(int)*2;
  3085. case ZIP_SOURCE_FREE:
  3086. zip_fclose(z->zf);
  3087. free(z);
  3088. return 0;
  3089. default:
  3090. ;
  3091. }
  3092. return -1;
  3093. }
  3094. ZIP_EXTERN struct zip_source *
  3095. zip_source_function(struct zip *za, zip_source_callback zcb, void *ud)
  3096. {
  3097. struct zip_source *zs;
  3098. if (za == NULL)
  3099. return NULL;
  3100. if ((zs=(struct zip_source *)malloc(sizeof(*zs))) == NULL) {
  3101. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  3102. return NULL;
  3103. }
  3104. zs->f = zcb;
  3105. zs->ud = ud;
  3106. return zs;
  3107. }
  3108. ZIP_EXTERN void
  3109. zip_source_free(struct zip_source *source)
  3110. {
  3111. if (source == NULL)
  3112. return;
  3113. (void)source->f(source->ud, NULL, 0, ZIP_SOURCE_FREE);
  3114. free(source);
  3115. }
  3116. struct read_file {
  3117. char *fname; /* name of file to copy from */
  3118. FILE *f; /* file to copy from */
  3119. myoff_t off; /* start offset of */
  3120. myoff_t len; /* lengt of data to copy */
  3121. myoff_t remain; /* bytes remaining to be copied */
  3122. int e[2]; /* error codes */
  3123. };
  3124. static ssize_t read_file(void *state, void *data, size_t len,
  3125. enum zip_source_cmd cmd);
  3126. ZIP_EXTERN struct zip_source *
  3127. zip_source_filep(struct zip *za, FILE *file, myoff_t start, myoff_t len)
  3128. {
  3129. if (za == NULL)
  3130. return NULL;
  3131. if (file == NULL || start < 0 || len < -1) {
  3132. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  3133. return NULL;
  3134. }
  3135. return _zip_source_file_or_p(za, NULL, file, start, len);
  3136. }
  3137. struct zip_source *
  3138. _zip_source_file_or_p(struct zip *za, const char *fname, FILE *file,
  3139. myoff_t start, myoff_t len)
  3140. {
  3141. struct read_file *f;
  3142. struct zip_source *zs;
  3143. if (file == NULL && fname == NULL) {
  3144. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  3145. return NULL;
  3146. }
  3147. if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
  3148. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  3149. return NULL;
  3150. }
  3151. f->fname = NULL;
  3152. if (fname) {
  3153. if ((f->fname=strdup(fname)) == NULL) {
  3154. _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  3155. free(f);
  3156. return NULL;
  3157. }
  3158. }
  3159. f->f = file;
  3160. f->off = start;
  3161. f->len = (len ? len : -1);
  3162. if ((zs=zip_source_function(za, read_file, f)) == NULL) {
  3163. free(f);
  3164. return NULL;
  3165. }
  3166. return zs;
  3167. }
  3168. static ssize_t
  3169. read_file(void *state, void *data, size_t len, enum zip_source_cmd cmd)
  3170. {
  3171. struct read_file *z;
  3172. char *buf;
  3173. int i, n;
  3174. z = (struct read_file *)state;
  3175. buf = (char *)data;
  3176. switch (cmd) {
  3177. case ZIP_SOURCE_OPEN:
  3178. if (z->fname) {
  3179. if ((z->f=fopen(z->fname, "rb")) == NULL) {
  3180. z->e[0] = ZIP_ER_OPEN;
  3181. z->e[1] = errno;
  3182. return -1;
  3183. }
  3184. }
  3185. if (fseeko(z->f, z->off, SEEK_SET) < 0) {
  3186. z->e[0] = ZIP_ER_SEEK;
  3187. z->e[1] = errno;
  3188. return -1;
  3189. }
  3190. z->remain = z->len;
  3191. return 0;
  3192. case ZIP_SOURCE_READ:
  3193. if (z->remain != -1)
  3194. n = len > z->remain ? z->remain : len;
  3195. else
  3196. n = len;
  3197. if ((i=fread(buf, 1, n, z->f)) < 0) {
  3198. z->e[0] = ZIP_ER_READ;
  3199. z->e[1] = errno;
  3200. return -1;
  3201. }
  3202. if (z->remain != -1)
  3203. z->remain -= i;
  3204. return i;
  3205. case ZIP_SOURCE_CLOSE:
  3206. if (z->fname) {
  3207. fclose(z->f);
  3208. z->f = NULL;
  3209. }
  3210. return 0;
  3211. case ZIP_SOURCE_STAT:
  3212. {
  3213. struct zip_stat *st;
  3214. struct stat fst;
  3215. int err;
  3216. if (len < sizeof(*st))
  3217. return -1;
  3218. if (z->f)
  3219. err = fstat(fileno(z->f), &fst);
  3220. else
  3221. err = stat(z->fname, &fst);
  3222. if (err != 0) {
  3223. z->e[0] = ZIP_ER_READ; /* best match */
  3224. z->e[1] = errno;
  3225. return -1;
  3226. }
  3227. st = (struct zip_stat *)data;
  3228. zip_stat_init(st);
  3229. st->mtime = fst.st_mtime;
  3230. if (z->len != -1)
  3231. st->size = z->len;
  3232. else if ((fst.st_mode&S_IFMT) == S_IFREG)
  3233. st->size = fst.st_size;
  3234. return sizeof(*st);
  3235. }
  3236. case ZIP_SOURCE_ERROR:
  3237. if (len < sizeof(int)*2)
  3238. return -1;
  3239. memcpy(data, z->e, sizeof(int)*2);
  3240. return sizeof(int)*2;
  3241. case ZIP_SOURCE_FREE:
  3242. free(z->fname);
  3243. if (z->f)
  3244. fclose(z->f);
  3245. free(z);
  3246. return 0;
  3247. default:
  3248. ;
  3249. }
  3250. return -1;
  3251. }
  3252. ZIP_EXTERN int
  3253. zip_name_locate(struct zip *za, const char *fname, int flags)
  3254. {
  3255. return _zip_name_locate(za, fname, flags, &za->error);
  3256. }
  3257. int
  3258. _zip_name_locate(struct zip *za, const char *fname, int flags,
  3259. struct zip_error *error)
  3260. {
  3261. int (*cmp)(const char *, const char *);
  3262. const char *fn, *p;
  3263. int i, n;
  3264. if (fname == NULL) {
  3265. _zip_error_set(error, ZIP_ER_INVAL, 0);
  3266. return -1;
  3267. }
  3268. cmp = (flags & ZIP_FL_NOCASE) ? strcasecmp : strcmp;
  3269. n = (flags & ZIP_FL_UNCHANGED) ? za->cdir->nentry : za->nentry;
  3270. for (i=0; i<n; i++) {
  3271. if (flags & ZIP_FL_UNCHANGED)
  3272. fn = za->cdir->entry[i].filename;
  3273. else
  3274. fn = _zip_get_name(za, i, flags, error);
  3275. /* newly added (partially filled) entry */
  3276. if (fn == NULL)
  3277. continue;
  3278. if (flags & ZIP_FL_NODIR) {
  3279. p = strrchr(fn, '/');
  3280. if (p)
  3281. fn = p+1;
  3282. }
  3283. if (cmp(fname, fn) == 0)
  3284. return i;
  3285. }
  3286. _zip_error_set(error, ZIP_ER_NOENT, 0);
  3287. return -1;
  3288. }