archive_read_support_format_zip.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066
  1. /*-
  2. * Copyright (c) 2004-2013 Tim Kientzle
  3. * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
  4. * Copyright (c) 2013 Konrad Kleine
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_zip.c 201102 2009-12-28 03:11:36Z kientzle $");
  29. /*
  30. * The definitive documentation of the Zip file format is:
  31. * http://www.pkware.com/documents/casestudies/APPNOTE.TXT
  32. *
  33. * The Info-Zip project has pioneered various extensions to better
  34. * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
  35. * "Ux", and 0x7875 "ux" extensions for time and ownership
  36. * information.
  37. *
  38. * History of this code: The streaming Zip reader was first added to
  39. * libarchive in January 2005. Support for seekable input sources was
  40. * added in Nov 2011. Zip64 support (including a significant code
  41. * refactoring) was added in 2014.
  42. */
  43. #ifdef HAVE_ERRNO_H
  44. #include <errno.h>
  45. #endif
  46. #ifdef HAVE_STDLIB_H
  47. #include <stdlib.h>
  48. #endif
  49. #ifdef HAVE_ZLIB_H
  50. #include <cm_zlib.h>
  51. #endif
  52. #include "archive.h"
  53. #include "archive_digest_private.h"
  54. #include "archive_cryptor_private.h"
  55. #include "archive_endian.h"
  56. #include "archive_entry.h"
  57. #include "archive_entry_locale.h"
  58. #include "archive_hmac_private.h"
  59. #include "archive_private.h"
  60. #include "archive_rb.h"
  61. #include "archive_read_private.h"
  62. #ifndef HAVE_ZLIB_H
  63. #include "archive_crc32.h"
  64. #endif
  65. struct zip_entry {
  66. struct archive_rb_node node;
  67. struct zip_entry *next;
  68. int64_t local_header_offset;
  69. int64_t compressed_size;
  70. int64_t uncompressed_size;
  71. int64_t gid;
  72. int64_t uid;
  73. struct archive_string rsrcname;
  74. time_t mtime;
  75. time_t atime;
  76. time_t ctime;
  77. uint32_t crc32;
  78. uint16_t mode;
  79. uint16_t zip_flags; /* From GP Flags Field */
  80. unsigned char compression;
  81. unsigned char system; /* From "version written by" */
  82. unsigned char flags; /* Our extra markers. */
  83. unsigned char decdat;/* Used for Decryption check */
  84. /* WinZip AES encryption extra field should be available
  85. * when compression is 99. */
  86. struct {
  87. /* Vendor version: AE-1 - 0x0001, AE-2 - 0x0002 */
  88. unsigned vendor;
  89. #define AES_VENDOR_AE_1 0x0001
  90. #define AES_VENDOR_AE_2 0x0002
  91. /* AES encryption strength:
  92. * 1 - 128 bits, 2 - 192 bits, 2 - 256 bits. */
  93. unsigned strength;
  94. /* Actual compression method. */
  95. unsigned char compression;
  96. } aes_extra;
  97. };
  98. struct trad_enc_ctx {
  99. uint32_t keys[3];
  100. };
  101. /* Bits used in zip_flags. */
  102. #define ZIP_ENCRYPTED (1 << 0)
  103. #define ZIP_LENGTH_AT_END (1 << 3)
  104. #define ZIP_STRONG_ENCRYPTED (1 << 6)
  105. #define ZIP_UTF8_NAME (1 << 11)
  106. /* See "7.2 Single Password Symmetric Encryption Method"
  107. in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
  108. #define ZIP_CENTRAL_DIRECTORY_ENCRYPTED (1 << 13)
  109. /* Bits used in flags. */
  110. #define LA_USED_ZIP64 (1 << 0)
  111. #define LA_FROM_CENTRAL_DIRECTORY (1 << 1)
  112. /*
  113. * See "WinZip - AES Encryption Information"
  114. * http://www.winzip.com/aes_info.htm
  115. */
  116. /* Value used in compression method. */
  117. #define WINZIP_AES_ENCRYPTION 99
  118. /* Authentication code size. */
  119. #define AUTH_CODE_SIZE 10
  120. /**/
  121. #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
  122. struct zip {
  123. /* Structural information about the archive. */
  124. struct archive_string format_name;
  125. int64_t central_directory_offset;
  126. size_t central_directory_entries_total;
  127. size_t central_directory_entries_on_this_disk;
  128. int has_encrypted_entries;
  129. /* List of entries (seekable Zip only) */
  130. struct zip_entry *zip_entries;
  131. struct archive_rb_tree tree;
  132. struct archive_rb_tree tree_rsrc;
  133. /* Bytes read but not yet consumed via __archive_read_consume() */
  134. size_t unconsumed;
  135. /* Information about entry we're currently reading. */
  136. struct zip_entry *entry;
  137. int64_t entry_bytes_remaining;
  138. /* These count the number of bytes actually read for the entry. */
  139. int64_t entry_compressed_bytes_read;
  140. int64_t entry_uncompressed_bytes_read;
  141. /* Running CRC32 of the decompressed data */
  142. unsigned long entry_crc32;
  143. unsigned long (*crc32func)(unsigned long, const void *,
  144. size_t);
  145. char ignore_crc32;
  146. /* Flags to mark progress of decompression. */
  147. char decompress_init;
  148. char end_of_entry;
  149. #ifdef HAVE_ZLIB_H
  150. unsigned char *uncompressed_buffer;
  151. size_t uncompressed_buffer_size;
  152. z_stream stream;
  153. char stream_valid;
  154. #endif
  155. struct archive_string_conv *sconv;
  156. struct archive_string_conv *sconv_default;
  157. struct archive_string_conv *sconv_utf8;
  158. int init_default_conversion;
  159. int process_mac_extensions;
  160. char init_decryption;
  161. /* Decryption buffer. */
  162. /*
  163. * The decrypted data starts at decrypted_ptr and
  164. * extends for decrypted_bytes_remaining. Decryption
  165. * adds new data to the end of this block, data is returned
  166. * to clients from the beginning. When the block hits the
  167. * end of decrypted_buffer, it has to be shuffled back to
  168. * the beginning of the buffer.
  169. */
  170. unsigned char *decrypted_buffer;
  171. unsigned char *decrypted_ptr;
  172. size_t decrypted_buffer_size;
  173. size_t decrypted_bytes_remaining;
  174. size_t decrypted_unconsumed_bytes;
  175. /* Traditional PKWARE decryption. */
  176. struct trad_enc_ctx tctx;
  177. char tctx_valid;
  178. /* WinZip AES decyption. */
  179. /* Contexts used for AES decryption. */
  180. archive_crypto_ctx cctx;
  181. char cctx_valid;
  182. archive_hmac_sha1_ctx hctx;
  183. char hctx_valid;
  184. /* Strong encryption's decryption header information. */
  185. unsigned iv_size;
  186. unsigned alg_id;
  187. unsigned bit_len;
  188. unsigned flags;
  189. unsigned erd_size;
  190. unsigned v_size;
  191. unsigned v_crc32;
  192. uint8_t *iv;
  193. uint8_t *erd;
  194. uint8_t *v_data;
  195. };
  196. /* Many systems define min or MIN, but not all. */
  197. #define zipmin(a,b) ((a) < (b) ? (a) : (b))
  198. /* ------------------------------------------------------------------------ */
  199. /*
  200. Traditional PKWARE Decryption functions.
  201. */
  202. static void
  203. trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
  204. {
  205. uint8_t t;
  206. #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
  207. ctx->keys[0] = CRC32(ctx->keys[0], c);
  208. ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
  209. t = (ctx->keys[1] >> 24) & 0xff;
  210. ctx->keys[2] = CRC32(ctx->keys[2], t);
  211. #undef CRC32
  212. }
  213. static uint8_t
  214. trad_enc_decypt_byte(struct trad_enc_ctx *ctx)
  215. {
  216. unsigned temp = ctx->keys[2] | 2;
  217. return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
  218. }
  219. static void
  220. trad_enc_decrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
  221. size_t in_len, uint8_t *out, size_t out_len)
  222. {
  223. unsigned i, max;
  224. max = (unsigned)((in_len < out_len)? in_len: out_len);
  225. for (i = 0; i < max; i++) {
  226. uint8_t t = in[i] ^ trad_enc_decypt_byte(ctx);
  227. out[i] = t;
  228. trad_enc_update_keys(ctx, t);
  229. }
  230. }
  231. static int
  232. trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len,
  233. const uint8_t *key, size_t key_len, uint8_t *crcchk)
  234. {
  235. uint8_t header[12];
  236. if (key_len < 12) {
  237. *crcchk = 0xff;
  238. return -1;
  239. }
  240. ctx->keys[0] = 305419896L;
  241. ctx->keys[1] = 591751049L;
  242. ctx->keys[2] = 878082192L;
  243. for (;pw_len; --pw_len)
  244. trad_enc_update_keys(ctx, *pw++);
  245. trad_enc_decrypt_update(ctx, key, 12, header, 12);
  246. /* Return the last byte for CRC check. */
  247. *crcchk = header[11];
  248. return 0;
  249. }
  250. #if 0
  251. static void
  252. crypt_derive_key_sha1(const void *p, int size, unsigned char *key,
  253. int key_size)
  254. {
  255. #define MD_SIZE 20
  256. archive_sha1_ctx ctx;
  257. unsigned char md1[MD_SIZE];
  258. unsigned char md2[MD_SIZE * 2];
  259. unsigned char mkb[64];
  260. int i;
  261. archive_sha1_init(&ctx);
  262. archive_sha1_update(&ctx, p, size);
  263. archive_sha1_final(&ctx, md1);
  264. memset(mkb, 0x36, sizeof(mkb));
  265. for (i = 0; i < MD_SIZE; i++)
  266. mkb[i] ^= md1[i];
  267. archive_sha1_init(&ctx);
  268. archive_sha1_update(&ctx, mkb, sizeof(mkb));
  269. archive_sha1_final(&ctx, md2);
  270. memset(mkb, 0x5C, sizeof(mkb));
  271. for (i = 0; i < MD_SIZE; i++)
  272. mkb[i] ^= md1[i];
  273. archive_sha1_init(&ctx);
  274. archive_sha1_update(&ctx, mkb, sizeof(mkb));
  275. archive_sha1_final(&ctx, md2 + MD_SIZE);
  276. if (key_size > 32)
  277. key_size = 32;
  278. memcpy(key, md2, key_size);
  279. #undef MD_SIZE
  280. }
  281. #endif
  282. /*
  283. * Common code for streaming or seeking modes.
  284. *
  285. * Includes code to read local file headers, decompress data
  286. * from entry bodies, and common API.
  287. */
  288. static unsigned long
  289. real_crc32(unsigned long crc, const void *buff, size_t len)
  290. {
  291. return crc32(crc, buff, (unsigned int)len);
  292. }
  293. /* Used by "ignorecrc32" option to speed up tests. */
  294. static unsigned long
  295. fake_crc32(unsigned long crc, const void *buff, size_t len)
  296. {
  297. (void)crc; /* UNUSED */
  298. (void)buff; /* UNUSED */
  299. (void)len; /* UNUSED */
  300. return 0;
  301. }
  302. static struct {
  303. int id;
  304. const char * name;
  305. } compression_methods[] = {
  306. {0, "uncompressed"}, /* The file is stored (no compression) */
  307. {1, "shrinking"}, /* The file is Shrunk */
  308. {2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
  309. {3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
  310. {4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
  311. {5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
  312. {6, "imploded"}, /* The file is Imploded */
  313. {7, "reserved"}, /* Reserved for Tokenizing compression algorithm */
  314. {8, "deflation"}, /* The file is Deflated */
  315. {9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
  316. {10, "ibm-terse"},/* PKWARE Data Compression Library Imploding
  317. * (old IBM TERSE) */
  318. {11, "reserved"}, /* Reserved by PKWARE */
  319. {12, "bzip"}, /* File is compressed using BZIP2 algorithm */
  320. {13, "reserved"}, /* Reserved by PKWARE */
  321. {14, "lzma"}, /* LZMA (EFS) */
  322. {15, "reserved"}, /* Reserved by PKWARE */
  323. {16, "reserved"}, /* Reserved by PKWARE */
  324. {17, "reserved"}, /* Reserved by PKWARE */
  325. {18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
  326. {19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */
  327. {97, "wav-pack"}, /* WavPack compressed data */
  328. {98, "ppmd-1"}, /* PPMd version I, Rev 1 */
  329. {99, "aes"} /* WinZip AES encryption */
  330. };
  331. static const char *
  332. compression_name(const int compression)
  333. {
  334. static const int num_compression_methods =
  335. sizeof(compression_methods)/sizeof(compression_methods[0]);
  336. int i=0;
  337. while(compression >= 0 && i < num_compression_methods) {
  338. if (compression_methods[i].id == compression)
  339. return compression_methods[i].name;
  340. i++;
  341. }
  342. return "??";
  343. }
  344. /* Convert an MSDOS-style date/time into Unix-style time. */
  345. static time_t
  346. zip_time(const char *p)
  347. {
  348. int msTime, msDate;
  349. struct tm ts;
  350. msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
  351. msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
  352. memset(&ts, 0, sizeof(ts));
  353. ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
  354. ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
  355. ts.tm_mday = msDate & 0x1f; /* Day of month. */
  356. ts.tm_hour = (msTime >> 11) & 0x1f;
  357. ts.tm_min = (msTime >> 5) & 0x3f;
  358. ts.tm_sec = (msTime << 1) & 0x3e;
  359. ts.tm_isdst = -1;
  360. return mktime(&ts);
  361. }
  362. /*
  363. * The extra data is stored as a list of
  364. * id1+size1+data1 + id2+size2+data2 ...
  365. * triplets. id and size are 2 bytes each.
  366. */
  367. static void
  368. process_extra(const char *p, size_t extra_length, struct zip_entry* zip_entry)
  369. {
  370. unsigned offset = 0;
  371. while (offset < extra_length - 4) {
  372. unsigned short headerid = archive_le16dec(p + offset);
  373. unsigned short datasize = archive_le16dec(p + offset + 2);
  374. offset += 4;
  375. if (offset + datasize > extra_length) {
  376. break;
  377. }
  378. #ifdef DEBUG
  379. fprintf(stderr, "Header id 0x%04x, length %d\n",
  380. headerid, datasize);
  381. #endif
  382. switch (headerid) {
  383. case 0x0001:
  384. /* Zip64 extended information extra field. */
  385. zip_entry->flags |= LA_USED_ZIP64;
  386. if (zip_entry->uncompressed_size == 0xffffffff) {
  387. if (datasize < 8)
  388. break;
  389. zip_entry->uncompressed_size =
  390. archive_le64dec(p + offset);
  391. offset += 8;
  392. datasize -= 8;
  393. }
  394. if (zip_entry->compressed_size == 0xffffffff) {
  395. if (datasize < 8)
  396. break;
  397. zip_entry->compressed_size =
  398. archive_le64dec(p + offset);
  399. offset += 8;
  400. datasize -= 8;
  401. }
  402. if (zip_entry->local_header_offset == 0xffffffff) {
  403. if (datasize < 8)
  404. break;
  405. zip_entry->local_header_offset =
  406. archive_le64dec(p + offset);
  407. offset += 8;
  408. datasize -= 8;
  409. }
  410. /* archive_le32dec(p + offset) gives disk
  411. * on which file starts, but we don't handle
  412. * multi-volume Zip files. */
  413. break;
  414. #ifdef DEBUG
  415. case 0x0017:
  416. {
  417. /* Strong encryption field. */
  418. if (archive_le16dec(p + offset) == 2) {
  419. unsigned algId =
  420. archive_le16dec(p + offset + 2);
  421. unsigned bitLen =
  422. archive_le16dec(p + offset + 4);
  423. int flags =
  424. archive_le16dec(p + offset + 6);
  425. fprintf(stderr, "algId=0x%04x, bitLen=%u, "
  426. "flgas=%d\n", algId, bitLen,flags);
  427. }
  428. break;
  429. }
  430. #endif
  431. case 0x5455:
  432. {
  433. /* Extended time field "UT". */
  434. int flags = p[offset];
  435. offset++;
  436. datasize--;
  437. /* Flag bits indicate which dates are present. */
  438. if (flags & 0x01)
  439. {
  440. #ifdef DEBUG
  441. fprintf(stderr, "mtime: %lld -> %d\n",
  442. (long long)zip_entry->mtime,
  443. archive_le32dec(p + offset));
  444. #endif
  445. if (datasize < 4)
  446. break;
  447. zip_entry->mtime = archive_le32dec(p + offset);
  448. offset += 4;
  449. datasize -= 4;
  450. }
  451. if (flags & 0x02)
  452. {
  453. if (datasize < 4)
  454. break;
  455. zip_entry->atime = archive_le32dec(p + offset);
  456. offset += 4;
  457. datasize -= 4;
  458. }
  459. if (flags & 0x04)
  460. {
  461. if (datasize < 4)
  462. break;
  463. zip_entry->ctime = archive_le32dec(p + offset);
  464. offset += 4;
  465. datasize -= 4;
  466. }
  467. break;
  468. }
  469. case 0x5855:
  470. {
  471. /* Info-ZIP Unix Extra Field (old version) "UX". */
  472. if (datasize >= 8) {
  473. zip_entry->atime = archive_le32dec(p + offset);
  474. zip_entry->mtime =
  475. archive_le32dec(p + offset + 4);
  476. }
  477. if (datasize >= 12) {
  478. zip_entry->uid =
  479. archive_le16dec(p + offset + 8);
  480. zip_entry->gid =
  481. archive_le16dec(p + offset + 10);
  482. }
  483. break;
  484. }
  485. case 0x6c78:
  486. {
  487. /* Experimental 'xl' field */
  488. /*
  489. * Introduced Dec 2013 to provide a way to
  490. * include external file attributes (and other
  491. * fields that ordinarily appear only in
  492. * central directory) in local file header.
  493. * This provides file type and permission
  494. * information necessary to support full
  495. * streaming extraction. Currently being
  496. * discussed with other Zip developers
  497. * ... subject to change.
  498. *
  499. * Format:
  500. * The field starts with a bitmap that specifies
  501. * which additional fields are included. The
  502. * bitmap is variable length and can be extended in
  503. * the future.
  504. *
  505. * n bytes - feature bitmap: first byte has low-order
  506. * 7 bits. If high-order bit is set, a subsequent
  507. * byte holds the next 7 bits, etc.
  508. *
  509. * if bitmap & 1, 2 byte "version made by"
  510. * if bitmap & 2, 2 byte "internal file attributes"
  511. * if bitmap & 4, 4 byte "external file attributes"
  512. * if bitmap & 8, 2 byte comment length + n byte comment
  513. */
  514. int bitmap, bitmap_last;
  515. if (datasize < 1)
  516. break;
  517. bitmap_last = bitmap = 0xff & p[offset];
  518. offset += 1;
  519. datasize -= 1;
  520. /* We only support first 7 bits of bitmap; skip rest. */
  521. while ((bitmap_last & 0x80) != 0
  522. && datasize >= 1) {
  523. bitmap_last = p[offset];
  524. offset += 1;
  525. datasize -= 1;
  526. }
  527. if (bitmap & 1) {
  528. /* 2 byte "version made by" */
  529. if (datasize < 2)
  530. break;
  531. zip_entry->system
  532. = archive_le16dec(p + offset) >> 8;
  533. offset += 2;
  534. datasize -= 2;
  535. }
  536. if (bitmap & 2) {
  537. /* 2 byte "internal file attributes" */
  538. uint32_t internal_attributes;
  539. if (datasize < 2)
  540. break;
  541. internal_attributes
  542. = archive_le16dec(p + offset);
  543. /* Not used by libarchive at present. */
  544. (void)internal_attributes; /* UNUSED */
  545. offset += 2;
  546. datasize -= 2;
  547. }
  548. if (bitmap & 4) {
  549. /* 4 byte "external file attributes" */
  550. uint32_t external_attributes;
  551. if (datasize < 4)
  552. break;
  553. external_attributes
  554. = archive_le32dec(p + offset);
  555. if (zip_entry->system == 3) {
  556. zip_entry->mode
  557. = external_attributes >> 16;
  558. } else if (zip_entry->system == 0) {
  559. // Interpret MSDOS directory bit
  560. if (0x10 == (external_attributes & 0x10)) {
  561. zip_entry->mode = AE_IFDIR | 0775;
  562. } else {
  563. zip_entry->mode = AE_IFREG | 0664;
  564. }
  565. if (0x01 == (external_attributes & 0x01)) {
  566. // Read-only bit; strip write permissions
  567. zip_entry->mode &= 0555;
  568. }
  569. } else {
  570. zip_entry->mode = 0;
  571. }
  572. offset += 4;
  573. datasize -= 4;
  574. }
  575. if (bitmap & 8) {
  576. /* 2 byte comment length + comment */
  577. uint32_t comment_length;
  578. if (datasize < 2)
  579. break;
  580. comment_length
  581. = archive_le16dec(p + offset);
  582. offset += 2;
  583. datasize -= 2;
  584. if (datasize < comment_length)
  585. break;
  586. /* Comment is not supported by libarchive */
  587. offset += comment_length;
  588. datasize -= comment_length;
  589. }
  590. break;
  591. }
  592. case 0x7855:
  593. /* Info-ZIP Unix Extra Field (type 2) "Ux". */
  594. #ifdef DEBUG
  595. fprintf(stderr, "uid %d gid %d\n",
  596. archive_le16dec(p + offset),
  597. archive_le16dec(p + offset + 2));
  598. #endif
  599. if (datasize >= 2)
  600. zip_entry->uid = archive_le16dec(p + offset);
  601. if (datasize >= 4)
  602. zip_entry->gid =
  603. archive_le16dec(p + offset + 2);
  604. break;
  605. case 0x7875:
  606. {
  607. /* Info-Zip Unix Extra Field (type 3) "ux". */
  608. int uidsize = 0, gidsize = 0;
  609. /* TODO: support arbitrary uidsize/gidsize. */
  610. if (datasize >= 1 && p[offset] == 1) {/* version=1 */
  611. if (datasize >= 4) {
  612. /* get a uid size. */
  613. uidsize = 0xff & (int)p[offset+1];
  614. if (uidsize == 2)
  615. zip_entry->uid =
  616. archive_le16dec(
  617. p + offset + 2);
  618. else if (uidsize == 4 && datasize >= 6)
  619. zip_entry->uid =
  620. archive_le32dec(
  621. p + offset + 2);
  622. }
  623. if (datasize >= (2 + uidsize + 3)) {
  624. /* get a gid size. */
  625. gidsize = 0xff & (int)p[offset+2+uidsize];
  626. if (gidsize == 2)
  627. zip_entry->gid =
  628. archive_le16dec(
  629. p+offset+2+uidsize+1);
  630. else if (gidsize == 4 &&
  631. datasize >= (2 + uidsize + 5))
  632. zip_entry->gid =
  633. archive_le32dec(
  634. p+offset+2+uidsize+1);
  635. }
  636. }
  637. break;
  638. }
  639. case 0x9901:
  640. /* WinZIp AES extra data field. */
  641. if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
  642. /* Vendor version. */
  643. zip_entry->aes_extra.vendor =
  644. archive_le16dec(p + offset);
  645. /* AES encryption strength. */
  646. zip_entry->aes_extra.strength = p[offset + 4];
  647. /* Actual compression method. */
  648. zip_entry->aes_extra.compression =
  649. p[offset + 5];
  650. }
  651. break;
  652. default:
  653. break;
  654. }
  655. offset += datasize;
  656. }
  657. #ifdef DEBUG
  658. if (offset != extra_length)
  659. {
  660. fprintf(stderr,
  661. "Extra data field contents do not match reported size!\n");
  662. }
  663. #endif
  664. }
  665. /*
  666. * Assumes file pointer is at beginning of local file header.
  667. */
  668. static int
  669. zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
  670. struct zip *zip)
  671. {
  672. const char *p;
  673. const void *h;
  674. const wchar_t *wp;
  675. const char *cp;
  676. size_t len, filename_length, extra_length;
  677. struct archive_string_conv *sconv;
  678. struct zip_entry *zip_entry = zip->entry;
  679. struct zip_entry zip_entry_central_dir;
  680. int ret = ARCHIVE_OK;
  681. char version;
  682. /* Save a copy of the original for consistency checks. */
  683. zip_entry_central_dir = *zip_entry;
  684. zip->decompress_init = 0;
  685. zip->end_of_entry = 0;
  686. zip->entry_uncompressed_bytes_read = 0;
  687. zip->entry_compressed_bytes_read = 0;
  688. zip->entry_crc32 = zip->crc32func(0, NULL, 0);
  689. /* Setup default conversion. */
  690. if (zip->sconv == NULL && !zip->init_default_conversion) {
  691. zip->sconv_default =
  692. archive_string_default_conversion_for_read(&(a->archive));
  693. zip->init_default_conversion = 1;
  694. }
  695. if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) {
  696. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  697. "Truncated ZIP file header");
  698. return (ARCHIVE_FATAL);
  699. }
  700. if (memcmp(p, "PK\003\004", 4) != 0) {
  701. archive_set_error(&a->archive, -1, "Damaged Zip archive");
  702. return ARCHIVE_FATAL;
  703. }
  704. version = p[4];
  705. zip_entry->system = p[5];
  706. zip_entry->zip_flags = archive_le16dec(p + 6);
  707. if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
  708. zip->has_encrypted_entries = 1;
  709. archive_entry_set_is_data_encrypted(entry, 1);
  710. if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED &&
  711. zip_entry->zip_flags & ZIP_ENCRYPTED &&
  712. zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) {
  713. archive_entry_set_is_metadata_encrypted(entry, 1);
  714. return ARCHIVE_FATAL;
  715. }
  716. }
  717. zip->init_decryption = (zip_entry->zip_flags & ZIP_ENCRYPTED);
  718. zip_entry->compression = (char)archive_le16dec(p + 8);
  719. zip_entry->mtime = zip_time(p + 10);
  720. zip_entry->crc32 = archive_le32dec(p + 14);
  721. if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  722. zip_entry->decdat = p[11];
  723. else
  724. zip_entry->decdat = p[17];
  725. zip_entry->compressed_size = archive_le32dec(p + 18);
  726. zip_entry->uncompressed_size = archive_le32dec(p + 22);
  727. filename_length = archive_le16dec(p + 26);
  728. extra_length = archive_le16dec(p + 28);
  729. __archive_read_consume(a, 30);
  730. /* Read the filename. */
  731. if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
  732. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  733. "Truncated ZIP file header");
  734. return (ARCHIVE_FATAL);
  735. }
  736. if (zip_entry->zip_flags & ZIP_UTF8_NAME) {
  737. /* The filename is stored to be UTF-8. */
  738. if (zip->sconv_utf8 == NULL) {
  739. zip->sconv_utf8 =
  740. archive_string_conversion_from_charset(
  741. &a->archive, "UTF-8", 1);
  742. if (zip->sconv_utf8 == NULL)
  743. return (ARCHIVE_FATAL);
  744. }
  745. sconv = zip->sconv_utf8;
  746. } else if (zip->sconv != NULL)
  747. sconv = zip->sconv;
  748. else
  749. sconv = zip->sconv_default;
  750. if (archive_entry_copy_pathname_l(entry,
  751. h, filename_length, sconv) != 0) {
  752. if (errno == ENOMEM) {
  753. archive_set_error(&a->archive, ENOMEM,
  754. "Can't allocate memory for Pathname");
  755. return (ARCHIVE_FATAL);
  756. }
  757. archive_set_error(&a->archive,
  758. ARCHIVE_ERRNO_FILE_FORMAT,
  759. "Pathname cannot be converted "
  760. "from %s to current locale.",
  761. archive_string_conversion_charset_name(sconv));
  762. ret = ARCHIVE_WARN;
  763. }
  764. __archive_read_consume(a, filename_length);
  765. /* Read the extra data. */
  766. if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
  767. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  768. "Truncated ZIP file header");
  769. return (ARCHIVE_FATAL);
  770. }
  771. process_extra(h, extra_length, zip_entry);
  772. __archive_read_consume(a, extra_length);
  773. /* Work around a bug in Info-Zip: When reading from a pipe, it
  774. * stats the pipe instead of synthesizing a file entry. */
  775. if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
  776. zip_entry->mode &= ~ AE_IFMT;
  777. zip_entry->mode |= AE_IFREG;
  778. }
  779. /* If the mode is totally empty, set some sane default. */
  780. if (zip_entry->mode == 0) {
  781. zip_entry->mode |= 0664;
  782. }
  783. /* Make sure that entries with a trailing '/' are marked as directories
  784. * even if the External File Attributes contains bogus values. If this
  785. * is not a directory and there is no type, assume regularfile. */
  786. if ((zip_entry->mode & AE_IFMT) != AE_IFDIR) {
  787. int has_slash;
  788. wp = archive_entry_pathname_w(entry);
  789. if (wp != NULL) {
  790. len = wcslen(wp);
  791. has_slash = len > 0 && wp[len - 1] == L'/';
  792. } else {
  793. cp = archive_entry_pathname(entry);
  794. len = (cp != NULL)?strlen(cp):0;
  795. has_slash = len > 0 && cp[len - 1] == '/';
  796. }
  797. /* Correct file type as needed. */
  798. if (has_slash) {
  799. zip_entry->mode &= ~AE_IFMT;
  800. zip_entry->mode |= AE_IFDIR;
  801. zip_entry->mode |= 0111;
  802. } else if ((zip_entry->mode & AE_IFMT) == 0) {
  803. zip_entry->mode |= AE_IFREG;
  804. }
  805. }
  806. /* Make sure directories end in '/' */
  807. if ((zip_entry->mode & AE_IFMT) == AE_IFDIR) {
  808. wp = archive_entry_pathname_w(entry);
  809. if (wp != NULL) {
  810. len = wcslen(wp);
  811. if (len > 0 && wp[len - 1] != L'/') {
  812. struct archive_wstring s;
  813. archive_string_init(&s);
  814. archive_wstrcat(&s, wp);
  815. archive_wstrappend_wchar(&s, L'/');
  816. archive_entry_copy_pathname_w(entry, s.s);
  817. }
  818. } else {
  819. cp = archive_entry_pathname(entry);
  820. len = (cp != NULL)?strlen(cp):0;
  821. if (len > 0 && cp[len - 1] != '/') {
  822. struct archive_string s;
  823. archive_string_init(&s);
  824. archive_strcat(&s, cp);
  825. archive_strappend_char(&s, '/');
  826. archive_entry_set_pathname(entry, s.s);
  827. }
  828. }
  829. }
  830. if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) {
  831. /* If this came from the central dir, it's size info
  832. * is definitive, so ignore the length-at-end flag. */
  833. zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END;
  834. /* If local header is missing a value, use the one from
  835. the central directory. If both have it, warn about
  836. mismatches. */
  837. if (zip_entry->crc32 == 0) {
  838. zip_entry->crc32 = zip_entry_central_dir.crc32;
  839. } else if (!zip->ignore_crc32
  840. && zip_entry->crc32 != zip_entry_central_dir.crc32) {
  841. archive_set_error(&a->archive,
  842. ARCHIVE_ERRNO_FILE_FORMAT,
  843. "Inconsistent CRC32 values");
  844. ret = ARCHIVE_WARN;
  845. }
  846. if (zip_entry->compressed_size == 0) {
  847. zip_entry->compressed_size
  848. = zip_entry_central_dir.compressed_size;
  849. } else if (zip_entry->compressed_size
  850. != zip_entry_central_dir.compressed_size) {
  851. archive_set_error(&a->archive,
  852. ARCHIVE_ERRNO_FILE_FORMAT,
  853. "Inconsistent compressed size: "
  854. "%jd in central directory, %jd in local header",
  855. (intmax_t)zip_entry_central_dir.compressed_size,
  856. (intmax_t)zip_entry->compressed_size);
  857. ret = ARCHIVE_WARN;
  858. }
  859. if (zip_entry->uncompressed_size == 0) {
  860. zip_entry->uncompressed_size
  861. = zip_entry_central_dir.uncompressed_size;
  862. } else if (zip_entry->uncompressed_size
  863. != zip_entry_central_dir.uncompressed_size) {
  864. archive_set_error(&a->archive,
  865. ARCHIVE_ERRNO_FILE_FORMAT,
  866. "Inconsistent uncompressed size: "
  867. "%jd in central directory, %jd in local header",
  868. (intmax_t)zip_entry_central_dir.uncompressed_size,
  869. (intmax_t)zip_entry->uncompressed_size);
  870. ret = ARCHIVE_WARN;
  871. }
  872. }
  873. /* Populate some additional entry fields: */
  874. archive_entry_set_mode(entry, zip_entry->mode);
  875. archive_entry_set_uid(entry, zip_entry->uid);
  876. archive_entry_set_gid(entry, zip_entry->gid);
  877. archive_entry_set_mtime(entry, zip_entry->mtime, 0);
  878. archive_entry_set_ctime(entry, zip_entry->ctime, 0);
  879. archive_entry_set_atime(entry, zip_entry->atime, 0);
  880. if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
  881. size_t linkname_length;
  882. if (zip_entry->compressed_size > 64 * 1024) {
  883. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  884. "Zip file with oversized link entry");
  885. return ARCHIVE_FATAL;
  886. }
  887. linkname_length = (size_t)zip_entry->compressed_size;
  888. archive_entry_set_size(entry, 0);
  889. p = __archive_read_ahead(a, linkname_length, NULL);
  890. if (p == NULL) {
  891. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  892. "Truncated Zip file");
  893. return ARCHIVE_FATAL;
  894. }
  895. sconv = zip->sconv;
  896. if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME))
  897. sconv = zip->sconv_utf8;
  898. if (sconv == NULL)
  899. sconv = zip->sconv_default;
  900. if (archive_entry_copy_symlink_l(entry, p, linkname_length,
  901. sconv) != 0) {
  902. if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
  903. (zip->entry->zip_flags & ZIP_UTF8_NAME))
  904. archive_entry_copy_symlink_l(entry, p,
  905. linkname_length, NULL);
  906. if (errno == ENOMEM) {
  907. archive_set_error(&a->archive, ENOMEM,
  908. "Can't allocate memory for Symlink");
  909. return (ARCHIVE_FATAL);
  910. }
  911. /*
  912. * Since there is no character-set regulation for
  913. * symlink name, do not report the conversion error
  914. * in an automatic conversion.
  915. */
  916. if (sconv != zip->sconv_utf8 ||
  917. (zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) {
  918. archive_set_error(&a->archive,
  919. ARCHIVE_ERRNO_FILE_FORMAT,
  920. "Symlink cannot be converted "
  921. "from %s to current locale.",
  922. archive_string_conversion_charset_name(
  923. sconv));
  924. ret = ARCHIVE_WARN;
  925. }
  926. }
  927. zip_entry->uncompressed_size = zip_entry->compressed_size = 0;
  928. if (__archive_read_consume(a, linkname_length) < 0) {
  929. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  930. "Read error skipping symlink target name");
  931. return ARCHIVE_FATAL;
  932. }
  933. } else if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  934. || zip_entry->uncompressed_size > 0) {
  935. /* Set the size only if it's meaningful. */
  936. archive_entry_set_size(entry, zip_entry->uncompressed_size);
  937. }
  938. zip->entry_bytes_remaining = zip_entry->compressed_size;
  939. /* If there's no body, force read_data() to return EOF immediately. */
  940. if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  941. && zip->entry_bytes_remaining < 1)
  942. zip->end_of_entry = 1;
  943. /* Set up a more descriptive format name. */
  944. archive_string_sprintf(&zip->format_name, "ZIP %d.%d (%s)",
  945. version / 10, version % 10,
  946. compression_name(zip->entry->compression));
  947. a->archive.archive_format_name = zip->format_name.s;
  948. return (ret);
  949. }
  950. static int
  951. check_authentication_code(struct archive_read *a, const void *_p)
  952. {
  953. struct zip *zip = (struct zip *)(a->format->data);
  954. /* Check authentication code. */
  955. if (zip->hctx_valid) {
  956. const void *p;
  957. uint8_t hmac[20];
  958. size_t hmac_len = 20;
  959. int cmp;
  960. archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
  961. if (_p == NULL) {
  962. /* Read authentication code. */
  963. p = __archive_read_ahead(a, AUTH_CODE_SIZE, NULL);
  964. if (p == NULL) {
  965. archive_set_error(&a->archive,
  966. ARCHIVE_ERRNO_FILE_FORMAT,
  967. "Truncated ZIP file data");
  968. return (ARCHIVE_FATAL);
  969. }
  970. } else {
  971. p = _p;
  972. }
  973. cmp = memcmp(hmac, p, AUTH_CODE_SIZE);
  974. __archive_read_consume(a, AUTH_CODE_SIZE);
  975. if (cmp != 0) {
  976. archive_set_error(&a->archive,
  977. ARCHIVE_ERRNO_MISC,
  978. "ZIP bad Authentication code");
  979. return (ARCHIVE_WARN);
  980. }
  981. }
  982. return (ARCHIVE_OK);
  983. }
  984. /*
  985. * Read "uncompressed" data. There are three cases:
  986. * 1) We know the size of the data. This is always true for the
  987. * seeking reader (we've examined the Central Directory already).
  988. * 2) ZIP_LENGTH_AT_END was set, but only the CRC was deferred.
  989. * Info-ZIP seems to do this; we know the size but have to grab
  990. * the CRC from the data descriptor afterwards.
  991. * 3) We're streaming and ZIP_LENGTH_AT_END was specified and
  992. * we have no size information. In this case, we can do pretty
  993. * well by watching for the data descriptor record. The data
  994. * descriptor is 16 bytes and includes a computed CRC that should
  995. * provide a strong check.
  996. *
  997. * TODO: Technically, the PK\007\010 signature is optional.
  998. * In the original spec, the data descriptor contained CRC
  999. * and size fields but had no leading signature. In practice,
  1000. * newer writers seem to provide the signature pretty consistently.
  1001. *
  1002. * For uncompressed data, the PK\007\010 marker seems essential
  1003. * to be sure we've actually seen the end of the entry.
  1004. *
  1005. * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
  1006. * zip->end_of_entry if it consumes all of the data.
  1007. */
  1008. static int
  1009. zip_read_data_none(struct archive_read *a, const void **_buff,
  1010. size_t *size, int64_t *offset)
  1011. {
  1012. struct zip *zip;
  1013. const char *buff;
  1014. ssize_t bytes_avail;
  1015. int r;
  1016. (void)offset; /* UNUSED */
  1017. zip = (struct zip *)(a->format->data);
  1018. if (zip->entry->zip_flags & ZIP_LENGTH_AT_END) {
  1019. const char *p;
  1020. ssize_t grabbing_bytes = 24;
  1021. if (zip->hctx_valid)
  1022. grabbing_bytes += AUTH_CODE_SIZE;
  1023. /* Grab at least 24 bytes. */
  1024. buff = __archive_read_ahead(a, grabbing_bytes, &bytes_avail);
  1025. if (bytes_avail < grabbing_bytes) {
  1026. /* Zip archives have end-of-archive markers
  1027. that are longer than this, so a failure to get at
  1028. least 24 bytes really does indicate a truncated
  1029. file. */
  1030. archive_set_error(&a->archive,
  1031. ARCHIVE_ERRNO_FILE_FORMAT,
  1032. "Truncated ZIP file data");
  1033. return (ARCHIVE_FATAL);
  1034. }
  1035. /* Check for a complete PK\007\010 signature, followed
  1036. * by the correct 4-byte CRC. */
  1037. p = buff;
  1038. if (zip->hctx_valid)
  1039. p += AUTH_CODE_SIZE;
  1040. if (p[0] == 'P' && p[1] == 'K'
  1041. && p[2] == '\007' && p[3] == '\010'
  1042. && (archive_le32dec(p + 4) == zip->entry_crc32
  1043. || zip->ignore_crc32
  1044. || (zip->hctx_valid
  1045. && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) {
  1046. if (zip->entry->flags & LA_USED_ZIP64) {
  1047. zip->entry->crc32 = archive_le32dec(p + 4);
  1048. zip->entry->compressed_size =
  1049. archive_le64dec(p + 8);
  1050. zip->entry->uncompressed_size =
  1051. archive_le64dec(p + 16);
  1052. zip->unconsumed = 24;
  1053. } else {
  1054. zip->entry->crc32 = archive_le32dec(p + 4);
  1055. zip->entry->compressed_size =
  1056. archive_le32dec(p + 8);
  1057. zip->entry->uncompressed_size =
  1058. archive_le32dec(p + 12);
  1059. zip->unconsumed = 16;
  1060. }
  1061. if (zip->hctx_valid) {
  1062. r = check_authentication_code(a, buff);
  1063. if (r != ARCHIVE_OK)
  1064. return (r);
  1065. }
  1066. zip->end_of_entry = 1;
  1067. return (ARCHIVE_OK);
  1068. }
  1069. /* If not at EOF, ensure we consume at least one byte. */
  1070. ++p;
  1071. /* Scan forward until we see where a PK\007\010 signature
  1072. * might be. */
  1073. /* Return bytes up until that point. On the next call,
  1074. * the code above will verify the data descriptor. */
  1075. while (p < buff + bytes_avail - 4) {
  1076. if (p[3] == 'P') { p += 3; }
  1077. else if (p[3] == 'K') { p += 2; }
  1078. else if (p[3] == '\007') { p += 1; }
  1079. else if (p[3] == '\010' && p[2] == '\007'
  1080. && p[1] == 'K' && p[0] == 'P') {
  1081. if (zip->hctx_valid)
  1082. p -= AUTH_CODE_SIZE;
  1083. break;
  1084. } else { p += 4; }
  1085. }
  1086. bytes_avail = p - buff;
  1087. } else {
  1088. if (zip->entry_bytes_remaining == 0) {
  1089. zip->end_of_entry = 1;
  1090. if (zip->hctx_valid) {
  1091. r = check_authentication_code(a, NULL);
  1092. if (r != ARCHIVE_OK)
  1093. return (r);
  1094. }
  1095. return (ARCHIVE_OK);
  1096. }
  1097. /* Grab a bunch of bytes. */
  1098. buff = __archive_read_ahead(a, 1, &bytes_avail);
  1099. if (bytes_avail <= 0) {
  1100. archive_set_error(&a->archive,
  1101. ARCHIVE_ERRNO_FILE_FORMAT,
  1102. "Truncated ZIP file data");
  1103. return (ARCHIVE_FATAL);
  1104. }
  1105. if (bytes_avail > zip->entry_bytes_remaining)
  1106. bytes_avail = (ssize_t)zip->entry_bytes_remaining;
  1107. }
  1108. if (zip->tctx_valid || zip->cctx_valid) {
  1109. size_t dec_size = bytes_avail;
  1110. if (dec_size > zip->decrypted_buffer_size)
  1111. dec_size = zip->decrypted_buffer_size;
  1112. if (zip->tctx_valid) {
  1113. trad_enc_decrypt_update(&zip->tctx,
  1114. (const uint8_t *)buff, dec_size,
  1115. zip->decrypted_buffer, dec_size);
  1116. } else {
  1117. size_t dsize = dec_size;
  1118. archive_hmac_sha1_update(&zip->hctx,
  1119. (const uint8_t *)buff, dec_size);
  1120. archive_decrypto_aes_ctr_update(&zip->cctx,
  1121. (const uint8_t *)buff, dec_size,
  1122. zip->decrypted_buffer, &dsize);
  1123. }
  1124. bytes_avail = dec_size;
  1125. buff = (const char *)zip->decrypted_buffer;
  1126. }
  1127. *size = bytes_avail;
  1128. zip->entry_bytes_remaining -= bytes_avail;
  1129. zip->entry_uncompressed_bytes_read += bytes_avail;
  1130. zip->entry_compressed_bytes_read += bytes_avail;
  1131. zip->unconsumed += bytes_avail;
  1132. *_buff = buff;
  1133. return (ARCHIVE_OK);
  1134. }
  1135. #ifdef HAVE_ZLIB_H
  1136. static int
  1137. zip_deflate_init(struct archive_read *a, struct zip *zip)
  1138. {
  1139. int r;
  1140. /* If we haven't yet read any data, initialize the decompressor. */
  1141. if (!zip->decompress_init) {
  1142. if (zip->stream_valid)
  1143. r = inflateReset(&zip->stream);
  1144. else
  1145. r = inflateInit2(&zip->stream,
  1146. -15 /* Don't check for zlib header */);
  1147. if (r != Z_OK) {
  1148. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1149. "Can't initialize ZIP decompression.");
  1150. return (ARCHIVE_FATAL);
  1151. }
  1152. /* Stream structure has been set up. */
  1153. zip->stream_valid = 1;
  1154. /* We've initialized decompression for this stream. */
  1155. zip->decompress_init = 1;
  1156. }
  1157. return (ARCHIVE_OK);
  1158. }
  1159. static int
  1160. zip_read_data_deflate(struct archive_read *a, const void **buff,
  1161. size_t *size, int64_t *offset)
  1162. {
  1163. struct zip *zip;
  1164. ssize_t bytes_avail;
  1165. const void *compressed_buff, *sp;
  1166. int r;
  1167. (void)offset; /* UNUSED */
  1168. zip = (struct zip *)(a->format->data);
  1169. /* If the buffer hasn't been allocated, allocate it now. */
  1170. if (zip->uncompressed_buffer == NULL) {
  1171. zip->uncompressed_buffer_size = 256 * 1024;
  1172. zip->uncompressed_buffer
  1173. = (unsigned char *)malloc(zip->uncompressed_buffer_size);
  1174. if (zip->uncompressed_buffer == NULL) {
  1175. archive_set_error(&a->archive, ENOMEM,
  1176. "No memory for ZIP decompression");
  1177. return (ARCHIVE_FATAL);
  1178. }
  1179. }
  1180. r = zip_deflate_init(a, zip);
  1181. if (r != ARCHIVE_OK)
  1182. return (r);
  1183. /*
  1184. * Note: '1' here is a performance optimization.
  1185. * Recall that the decompression layer returns a count of
  1186. * available bytes; asking for more than that forces the
  1187. * decompressor to combine reads by copying data.
  1188. */
  1189. compressed_buff = sp = __archive_read_ahead(a, 1, &bytes_avail);
  1190. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  1191. && bytes_avail > zip->entry_bytes_remaining) {
  1192. bytes_avail = (ssize_t)zip->entry_bytes_remaining;
  1193. }
  1194. if (bytes_avail <= 0) {
  1195. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1196. "Truncated ZIP file body");
  1197. return (ARCHIVE_FATAL);
  1198. }
  1199. if (zip->tctx_valid || zip->cctx_valid) {
  1200. if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) {
  1201. size_t buff_remaining =
  1202. (zip->decrypted_buffer + zip->decrypted_buffer_size)
  1203. - (zip->decrypted_ptr + zip->decrypted_bytes_remaining);
  1204. if (buff_remaining > (size_t)bytes_avail)
  1205. buff_remaining = (size_t)bytes_avail;
  1206. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) &&
  1207. zip->entry_bytes_remaining > 0) {
  1208. if ((int64_t)(zip->decrypted_bytes_remaining
  1209. + buff_remaining)
  1210. > zip->entry_bytes_remaining) {
  1211. if (zip->entry_bytes_remaining <
  1212. (int64_t)zip->decrypted_bytes_remaining)
  1213. buff_remaining = 0;
  1214. else
  1215. buff_remaining =
  1216. (size_t)zip->entry_bytes_remaining
  1217. - zip->decrypted_bytes_remaining;
  1218. }
  1219. }
  1220. if (buff_remaining > 0) {
  1221. if (zip->tctx_valid) {
  1222. trad_enc_decrypt_update(&zip->tctx,
  1223. compressed_buff, buff_remaining,
  1224. zip->decrypted_ptr
  1225. + zip->decrypted_bytes_remaining,
  1226. buff_remaining);
  1227. } else {
  1228. size_t dsize = buff_remaining;
  1229. archive_decrypto_aes_ctr_update(
  1230. &zip->cctx,
  1231. compressed_buff, buff_remaining,
  1232. zip->decrypted_ptr
  1233. + zip->decrypted_bytes_remaining,
  1234. &dsize);
  1235. }
  1236. zip->decrypted_bytes_remaining += buff_remaining;
  1237. }
  1238. }
  1239. bytes_avail = zip->decrypted_bytes_remaining;
  1240. compressed_buff = (const char *)zip->decrypted_ptr;
  1241. }
  1242. /*
  1243. * A bug in zlib.h: stream.next_in should be marked 'const'
  1244. * but isn't (the library never alters data through the
  1245. * next_in pointer, only reads it). The result: this ugly
  1246. * cast to remove 'const'.
  1247. */
  1248. zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
  1249. zip->stream.avail_in = (uInt)bytes_avail;
  1250. zip->stream.total_in = 0;
  1251. zip->stream.next_out = zip->uncompressed_buffer;
  1252. zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size;
  1253. zip->stream.total_out = 0;
  1254. r = inflate(&zip->stream, 0);
  1255. switch (r) {
  1256. case Z_OK:
  1257. break;
  1258. case Z_STREAM_END:
  1259. zip->end_of_entry = 1;
  1260. break;
  1261. case Z_MEM_ERROR:
  1262. archive_set_error(&a->archive, ENOMEM,
  1263. "Out of memory for ZIP decompression");
  1264. return (ARCHIVE_FATAL);
  1265. default:
  1266. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1267. "ZIP decompression failed (%d)", r);
  1268. return (ARCHIVE_FATAL);
  1269. }
  1270. /* Consume as much as the compressor actually used. */
  1271. bytes_avail = zip->stream.total_in;
  1272. if (zip->tctx_valid || zip->cctx_valid) {
  1273. zip->decrypted_bytes_remaining -= bytes_avail;
  1274. if (zip->decrypted_bytes_remaining == 0)
  1275. zip->decrypted_ptr = zip->decrypted_buffer;
  1276. else
  1277. zip->decrypted_ptr += bytes_avail;
  1278. }
  1279. /* Calculate compressed data as much as we used.*/
  1280. if (zip->hctx_valid)
  1281. archive_hmac_sha1_update(&zip->hctx, sp, bytes_avail);
  1282. __archive_read_consume(a, bytes_avail);
  1283. zip->entry_bytes_remaining -= bytes_avail;
  1284. zip->entry_compressed_bytes_read += bytes_avail;
  1285. *size = zip->stream.total_out;
  1286. zip->entry_uncompressed_bytes_read += zip->stream.total_out;
  1287. *buff = zip->uncompressed_buffer;
  1288. if (zip->end_of_entry && zip->hctx_valid) {
  1289. r = check_authentication_code(a, NULL);
  1290. if (r != ARCHIVE_OK)
  1291. return (r);
  1292. }
  1293. if (zip->end_of_entry && (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
  1294. const char *p;
  1295. if (NULL == (p = __archive_read_ahead(a, 24, NULL))) {
  1296. archive_set_error(&a->archive,
  1297. ARCHIVE_ERRNO_FILE_FORMAT,
  1298. "Truncated ZIP end-of-file record");
  1299. return (ARCHIVE_FATAL);
  1300. }
  1301. /* Consume the optional PK\007\010 marker. */
  1302. if (p[0] == 'P' && p[1] == 'K' &&
  1303. p[2] == '\007' && p[3] == '\010') {
  1304. p += 4;
  1305. zip->unconsumed = 4;
  1306. }
  1307. if (zip->entry->flags & LA_USED_ZIP64) {
  1308. zip->entry->crc32 = archive_le32dec(p);
  1309. zip->entry->compressed_size = archive_le64dec(p + 4);
  1310. zip->entry->uncompressed_size = archive_le64dec(p + 12);
  1311. zip->unconsumed += 20;
  1312. } else {
  1313. zip->entry->crc32 = archive_le32dec(p);
  1314. zip->entry->compressed_size = archive_le32dec(p + 4);
  1315. zip->entry->uncompressed_size = archive_le32dec(p + 8);
  1316. zip->unconsumed += 12;
  1317. }
  1318. }
  1319. return (ARCHIVE_OK);
  1320. }
  1321. #endif
  1322. static int
  1323. read_decryption_header(struct archive_read *a)
  1324. {
  1325. struct zip *zip = (struct zip *)(a->format->data);
  1326. const char *p;
  1327. unsigned int remaining_size;
  1328. unsigned int ts;
  1329. /*
  1330. * Read an initialization vector data field.
  1331. */
  1332. p = __archive_read_ahead(a, 2, NULL);
  1333. if (p == NULL)
  1334. goto truncated;
  1335. ts = zip->iv_size;
  1336. zip->iv_size = archive_le16dec(p);
  1337. __archive_read_consume(a, 2);
  1338. if (ts < zip->iv_size) {
  1339. free(zip->iv);
  1340. zip->iv = NULL;
  1341. }
  1342. p = __archive_read_ahead(a, zip->iv_size, NULL);
  1343. if (p == NULL)
  1344. goto truncated;
  1345. if (zip->iv == NULL) {
  1346. zip->iv = malloc(zip->iv_size);
  1347. if (zip->iv == NULL)
  1348. goto nomem;
  1349. }
  1350. memcpy(zip->iv, p, zip->iv_size);
  1351. __archive_read_consume(a, zip->iv_size);
  1352. /*
  1353. * Read a size of remaining decryption header field.
  1354. */
  1355. p = __archive_read_ahead(a, 14, NULL);
  1356. if (p == NULL)
  1357. goto truncated;
  1358. remaining_size = archive_le32dec(p);
  1359. if (remaining_size < 16 || remaining_size > (1 << 18))
  1360. goto corrupted;
  1361. /* Check if format version is supported. */
  1362. if (archive_le16dec(p+4) != 3) {
  1363. archive_set_error(&a->archive,
  1364. ARCHIVE_ERRNO_FILE_FORMAT,
  1365. "Unsupported encryption format version: %u",
  1366. archive_le16dec(p+4));
  1367. return (ARCHIVE_FAILED);
  1368. }
  1369. /*
  1370. * Read an encryption algorithm field.
  1371. */
  1372. zip->alg_id = archive_le16dec(p+6);
  1373. switch (zip->alg_id) {
  1374. case 0x6601:/* DES */
  1375. case 0x6602:/* RC2 */
  1376. case 0x6603:/* 3DES 168 */
  1377. case 0x6609:/* 3DES 112 */
  1378. case 0x660E:/* AES 128 */
  1379. case 0x660F:/* AES 192 */
  1380. case 0x6610:/* AES 256 */
  1381. case 0x6702:/* RC2 (version >= 5.2) */
  1382. case 0x6720:/* Blowfish */
  1383. case 0x6721:/* Twofish */
  1384. case 0x6801:/* RC4 */
  1385. /* Suuported encryption algorithm. */
  1386. break;
  1387. default:
  1388. archive_set_error(&a->archive,
  1389. ARCHIVE_ERRNO_FILE_FORMAT,
  1390. "Unknown encryption algorithm: %u", zip->alg_id);
  1391. return (ARCHIVE_FAILED);
  1392. }
  1393. /*
  1394. * Read a bit length field.
  1395. */
  1396. zip->bit_len = archive_le16dec(p+8);
  1397. /*
  1398. * Read a flags field.
  1399. */
  1400. zip->flags = archive_le16dec(p+10);
  1401. switch (zip->flags & 0xf000) {
  1402. case 0x0001: /* Password is required to decrypt. */
  1403. case 0x0002: /* Certificates only. */
  1404. case 0x0003: /* Password or certificate required to decrypt. */
  1405. break;
  1406. default:
  1407. archive_set_error(&a->archive,
  1408. ARCHIVE_ERRNO_FILE_FORMAT,
  1409. "Unknown encryption flag: %u", zip->flags);
  1410. return (ARCHIVE_FAILED);
  1411. }
  1412. if ((zip->flags & 0xf000) == 0 ||
  1413. (zip->flags & 0xf000) == 0x4000) {
  1414. archive_set_error(&a->archive,
  1415. ARCHIVE_ERRNO_FILE_FORMAT,
  1416. "Unknown encryption flag: %u", zip->flags);
  1417. return (ARCHIVE_FAILED);
  1418. }
  1419. /*
  1420. * Read an encrypted random data field.
  1421. */
  1422. ts = zip->erd_size;
  1423. zip->erd_size = archive_le16dec(p+12);
  1424. __archive_read_consume(a, 14);
  1425. if ((zip->erd_size & 0xf) != 0 ||
  1426. (zip->erd_size + 16) > remaining_size ||
  1427. (zip->erd_size + 16) < zip->erd_size)
  1428. goto corrupted;
  1429. if (ts < zip->erd_size) {
  1430. free(zip->erd);
  1431. zip->erd = NULL;
  1432. }
  1433. p = __archive_read_ahead(a, zip->erd_size, NULL);
  1434. if (p == NULL)
  1435. goto truncated;
  1436. if (zip->erd == NULL) {
  1437. zip->erd = malloc(zip->erd_size);
  1438. if (zip->erd == NULL)
  1439. goto nomem;
  1440. }
  1441. memcpy(zip->erd, p, zip->erd_size);
  1442. __archive_read_consume(a, zip->erd_size);
  1443. /*
  1444. * Read a reserved data field.
  1445. */
  1446. p = __archive_read_ahead(a, 4, NULL);
  1447. if (p == NULL)
  1448. goto truncated;
  1449. /* Reserved data size should be zero. */
  1450. if (archive_le32dec(p) != 0)
  1451. goto corrupted;
  1452. __archive_read_consume(a, 4);
  1453. /*
  1454. * Read a password validation data field.
  1455. */
  1456. p = __archive_read_ahead(a, 2, NULL);
  1457. if (p == NULL)
  1458. goto truncated;
  1459. ts = zip->v_size;
  1460. zip->v_size = archive_le16dec(p);
  1461. __archive_read_consume(a, 2);
  1462. if ((zip->v_size & 0x0f) != 0 ||
  1463. (zip->erd_size + zip->v_size + 16) > remaining_size ||
  1464. (zip->erd_size + zip->v_size + 16) < (zip->erd_size + zip->v_size))
  1465. goto corrupted;
  1466. if (ts < zip->v_size) {
  1467. free(zip->v_data);
  1468. zip->v_data = NULL;
  1469. }
  1470. p = __archive_read_ahead(a, zip->v_size, NULL);
  1471. if (p == NULL)
  1472. goto truncated;
  1473. if (zip->v_data == NULL) {
  1474. zip->v_data = malloc(zip->v_size);
  1475. if (zip->v_data == NULL)
  1476. goto nomem;
  1477. }
  1478. memcpy(zip->v_data, p, zip->v_size);
  1479. __archive_read_consume(a, zip->v_size);
  1480. p = __archive_read_ahead(a, 4, NULL);
  1481. if (p == NULL)
  1482. goto truncated;
  1483. zip->v_crc32 = archive_le32dec(p);
  1484. __archive_read_consume(a, 4);
  1485. /*return (ARCHIVE_OK);
  1486. * This is not fully implemnted yet.*/
  1487. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1488. "Encrypted file is unsupported");
  1489. return (ARCHIVE_FAILED);
  1490. truncated:
  1491. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1492. "Truncated ZIP file data");
  1493. return (ARCHIVE_FATAL);
  1494. corrupted:
  1495. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1496. "Corrupted ZIP file data");
  1497. return (ARCHIVE_FATAL);
  1498. nomem:
  1499. archive_set_error(&a->archive, ENOMEM,
  1500. "No memory for ZIP decryption");
  1501. return (ARCHIVE_FATAL);
  1502. }
  1503. static int
  1504. zip_alloc_decryption_buffer(struct archive_read *a)
  1505. {
  1506. struct zip *zip = (struct zip *)(a->format->data);
  1507. size_t bs = 256 * 1024;
  1508. if (zip->decrypted_buffer == NULL) {
  1509. zip->decrypted_buffer_size = bs;
  1510. zip->decrypted_buffer = malloc(bs);
  1511. if (zip->decrypted_buffer == NULL) {
  1512. archive_set_error(&a->archive, ENOMEM,
  1513. "No memory for ZIP decryption");
  1514. return (ARCHIVE_FATAL);
  1515. }
  1516. }
  1517. zip->decrypted_ptr = zip->decrypted_buffer;
  1518. return (ARCHIVE_OK);
  1519. }
  1520. static int
  1521. init_traditional_PKWARE_decryption(struct archive_read *a)
  1522. {
  1523. struct zip *zip = (struct zip *)(a->format->data);
  1524. const void *p;
  1525. int retry;
  1526. int r;
  1527. if (zip->tctx_valid)
  1528. return (ARCHIVE_OK);
  1529. /*
  1530. Read the 12 bytes encryption header stored at
  1531. the start of the data area.
  1532. */
  1533. #define ENC_HEADER_SIZE 12
  1534. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  1535. && zip->entry_bytes_remaining < ENC_HEADER_SIZE) {
  1536. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1537. "Truncated Zip encrypted body: only %jd bytes available",
  1538. (intmax_t)zip->entry_bytes_remaining);
  1539. return (ARCHIVE_FATAL);
  1540. }
  1541. p = __archive_read_ahead(a, ENC_HEADER_SIZE, NULL);
  1542. if (p == NULL) {
  1543. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1544. "Truncated ZIP file data");
  1545. return (ARCHIVE_FATAL);
  1546. }
  1547. for (retry = 0;; retry++) {
  1548. const char *passphrase;
  1549. uint8_t crcchk;
  1550. passphrase = __archive_read_next_passphrase(a);
  1551. if (passphrase == NULL) {
  1552. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1553. (retry > 0)?
  1554. "Incorrect passphrase":
  1555. "Passphrase required for this entry");
  1556. return (ARCHIVE_FAILED);
  1557. }
  1558. /*
  1559. * Initialize ctx for Traditional PKWARE Decyption.
  1560. */
  1561. r = trad_enc_init(&zip->tctx, passphrase, strlen(passphrase),
  1562. p, ENC_HEADER_SIZE, &crcchk);
  1563. if (r == 0 && crcchk == zip->entry->decdat)
  1564. break;/* The passphrase is OK. */
  1565. if (retry > 10000) {
  1566. /* Avoid infinity loop. */
  1567. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1568. "Too many incorrect passphrases");
  1569. return (ARCHIVE_FAILED);
  1570. }
  1571. }
  1572. __archive_read_consume(a, ENC_HEADER_SIZE);
  1573. zip->tctx_valid = 1;
  1574. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
  1575. zip->entry_bytes_remaining -= ENC_HEADER_SIZE;
  1576. }
  1577. /*zip->entry_uncompressed_bytes_read += ENC_HEADER_SIZE;*/
  1578. zip->entry_compressed_bytes_read += ENC_HEADER_SIZE;
  1579. zip->decrypted_bytes_remaining = 0;
  1580. return (zip_alloc_decryption_buffer(a));
  1581. #undef ENC_HEADER_SIZE
  1582. }
  1583. static int
  1584. init_WinZip_AES_decryption(struct archive_read *a)
  1585. {
  1586. struct zip *zip = (struct zip *)(a->format->data);
  1587. const void *p;
  1588. const uint8_t *pv;
  1589. size_t key_len, salt_len;
  1590. uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
  1591. int retry;
  1592. int r;
  1593. if (zip->cctx_valid || zip->hctx_valid)
  1594. return (ARCHIVE_OK);
  1595. switch (zip->entry->aes_extra.strength) {
  1596. case 1: salt_len = 8; key_len = 16; break;
  1597. case 2: salt_len = 12; key_len = 24; break;
  1598. case 3: salt_len = 16; key_len = 32; break;
  1599. default: goto corrupted;
  1600. }
  1601. p = __archive_read_ahead(a, salt_len + 2, NULL);
  1602. if (p == NULL)
  1603. goto truncated;
  1604. for (retry = 0;; retry++) {
  1605. const char *passphrase;
  1606. passphrase = __archive_read_next_passphrase(a);
  1607. if (passphrase == NULL) {
  1608. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1609. (retry > 0)?
  1610. "Incorrect passphrase":
  1611. "Passphrase required for this entry");
  1612. return (ARCHIVE_FAILED);
  1613. }
  1614. memset(derived_key, 0, sizeof(derived_key));
  1615. r = archive_pbkdf2_sha1(passphrase, strlen(passphrase),
  1616. p, salt_len, 1000, derived_key, key_len * 2 + 2);
  1617. if (r != 0) {
  1618. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1619. "Decryption is unsupported due to lack of "
  1620. "crypto library");
  1621. return (ARCHIVE_FAILED);
  1622. }
  1623. /* Check password verification value. */
  1624. pv = ((const uint8_t *)p) + salt_len;
  1625. if (derived_key[key_len * 2] == pv[0] &&
  1626. derived_key[key_len * 2 + 1] == pv[1])
  1627. break;/* The passphrase is OK. */
  1628. if (retry > 10000) {
  1629. /* Avoid infinity loop. */
  1630. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1631. "Too many incorrect passphrases");
  1632. return (ARCHIVE_FAILED);
  1633. }
  1634. }
  1635. r = archive_decrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
  1636. if (r != 0) {
  1637. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1638. "Decryption is unsupported due to lack of crypto library");
  1639. return (ARCHIVE_FAILED);
  1640. }
  1641. r = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len, key_len);
  1642. if (r != 0) {
  1643. archive_decrypto_aes_ctr_release(&zip->cctx);
  1644. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1645. "Failed to initialize HMAC-SHA1");
  1646. return (ARCHIVE_FAILED);
  1647. }
  1648. zip->cctx_valid = zip->hctx_valid = 1;
  1649. __archive_read_consume(a, salt_len + 2);
  1650. zip->entry_bytes_remaining -= salt_len + 2 + AUTH_CODE_SIZE;
  1651. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  1652. && zip->entry_bytes_remaining < 0)
  1653. goto corrupted;
  1654. zip->entry_compressed_bytes_read += salt_len + 2 + AUTH_CODE_SIZE;
  1655. zip->decrypted_bytes_remaining = 0;
  1656. zip->entry->compression = zip->entry->aes_extra.compression;
  1657. return (zip_alloc_decryption_buffer(a));
  1658. truncated:
  1659. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1660. "Truncated ZIP file data");
  1661. return (ARCHIVE_FATAL);
  1662. corrupted:
  1663. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1664. "Corrupted ZIP file data");
  1665. return (ARCHIVE_FATAL);
  1666. }
  1667. static int
  1668. archive_read_format_zip_read_data(struct archive_read *a,
  1669. const void **buff, size_t *size, int64_t *offset)
  1670. {
  1671. int r;
  1672. struct zip *zip = (struct zip *)(a->format->data);
  1673. if (zip->has_encrypted_entries ==
  1674. ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  1675. zip->has_encrypted_entries = 0;
  1676. }
  1677. *offset = zip->entry_uncompressed_bytes_read;
  1678. *size = 0;
  1679. *buff = NULL;
  1680. /* If we hit end-of-entry last time, return ARCHIVE_EOF. */
  1681. if (zip->end_of_entry)
  1682. return (ARCHIVE_EOF);
  1683. /* Return EOF immediately if this is a non-regular file. */
  1684. if (AE_IFREG != (zip->entry->mode & AE_IFMT))
  1685. return (ARCHIVE_EOF);
  1686. __archive_read_consume(a, zip->unconsumed);
  1687. zip->unconsumed = 0;
  1688. if (zip->init_decryption) {
  1689. zip->has_encrypted_entries = 1;
  1690. if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
  1691. r = read_decryption_header(a);
  1692. else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
  1693. r = init_WinZip_AES_decryption(a);
  1694. else
  1695. r = init_traditional_PKWARE_decryption(a);
  1696. if (r != ARCHIVE_OK)
  1697. return (r);
  1698. zip->init_decryption = 0;
  1699. }
  1700. switch(zip->entry->compression) {
  1701. case 0: /* No compression. */
  1702. r = zip_read_data_none(a, buff, size, offset);
  1703. break;
  1704. #ifdef HAVE_ZLIB_H
  1705. case 8: /* Deflate compression. */
  1706. r = zip_read_data_deflate(a, buff, size, offset);
  1707. break;
  1708. #endif
  1709. default: /* Unsupported compression. */
  1710. /* Return a warning. */
  1711. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1712. "Unsupported ZIP compression method (%s)",
  1713. compression_name(zip->entry->compression));
  1714. /* We can't decompress this entry, but we will
  1715. * be able to skip() it and try the next entry. */
  1716. return (ARCHIVE_FAILED);
  1717. break;
  1718. }
  1719. if (r != ARCHIVE_OK)
  1720. return (r);
  1721. /* Update checksum */
  1722. if (*size)
  1723. zip->entry_crc32 = zip->crc32func(zip->entry_crc32, *buff,
  1724. (unsigned)*size);
  1725. /* If we hit the end, swallow any end-of-data marker. */
  1726. if (zip->end_of_entry) {
  1727. /* Check file size, CRC against these values. */
  1728. if (zip->entry->compressed_size !=
  1729. zip->entry_compressed_bytes_read) {
  1730. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1731. "ZIP compressed data is wrong size "
  1732. "(read %jd, expected %jd)",
  1733. (intmax_t)zip->entry_compressed_bytes_read,
  1734. (intmax_t)zip->entry->compressed_size);
  1735. return (ARCHIVE_WARN);
  1736. }
  1737. /* Size field only stores the lower 32 bits of the actual
  1738. * size. */
  1739. if ((zip->entry->uncompressed_size & UINT32_MAX)
  1740. != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
  1741. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1742. "ZIP uncompressed data is wrong size "
  1743. "(read %jd, expected %jd)\n",
  1744. (intmax_t)zip->entry_uncompressed_bytes_read,
  1745. (intmax_t)zip->entry->uncompressed_size);
  1746. return (ARCHIVE_WARN);
  1747. }
  1748. /* Check computed CRC against header */
  1749. if ((!zip->hctx_valid ||
  1750. zip->entry->aes_extra.vendor != AES_VENDOR_AE_2) &&
  1751. zip->entry->crc32 != zip->entry_crc32
  1752. && !zip->ignore_crc32) {
  1753. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1754. "ZIP bad CRC: 0x%lx should be 0x%lx",
  1755. (unsigned long)zip->entry_crc32,
  1756. (unsigned long)zip->entry->crc32);
  1757. return (ARCHIVE_WARN);
  1758. }
  1759. }
  1760. return (ARCHIVE_OK);
  1761. }
  1762. static int
  1763. archive_read_format_zip_cleanup(struct archive_read *a)
  1764. {
  1765. struct zip *zip;
  1766. struct zip_entry *zip_entry, *next_zip_entry;
  1767. zip = (struct zip *)(a->format->data);
  1768. #ifdef HAVE_ZLIB_H
  1769. if (zip->stream_valid)
  1770. inflateEnd(&zip->stream);
  1771. free(zip->uncompressed_buffer);
  1772. #endif
  1773. if (zip->zip_entries) {
  1774. zip_entry = zip->zip_entries;
  1775. while (zip_entry != NULL) {
  1776. next_zip_entry = zip_entry->next;
  1777. archive_string_free(&zip_entry->rsrcname);
  1778. free(zip_entry);
  1779. zip_entry = next_zip_entry;
  1780. }
  1781. }
  1782. free(zip->decrypted_buffer);
  1783. if (zip->cctx_valid)
  1784. archive_decrypto_aes_ctr_release(&zip->cctx);
  1785. if (zip->hctx_valid)
  1786. archive_hmac_sha1_cleanup(&zip->hctx);
  1787. free(zip->iv);
  1788. free(zip->erd);
  1789. free(zip->v_data);
  1790. archive_string_free(&zip->format_name);
  1791. free(zip);
  1792. (a->format->data) = NULL;
  1793. return (ARCHIVE_OK);
  1794. }
  1795. static int
  1796. archive_read_format_zip_has_encrypted_entries(struct archive_read *_a)
  1797. {
  1798. if (_a && _a->format) {
  1799. struct zip * zip = (struct zip *)_a->format->data;
  1800. if (zip) {
  1801. return zip->has_encrypted_entries;
  1802. }
  1803. }
  1804. return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  1805. }
  1806. static int
  1807. archive_read_format_zip_options(struct archive_read *a,
  1808. const char *key, const char *val)
  1809. {
  1810. struct zip *zip;
  1811. int ret = ARCHIVE_FAILED;
  1812. zip = (struct zip *)(a->format->data);
  1813. if (strcmp(key, "compat-2x") == 0) {
  1814. /* Handle filenames as libarchive 2.x */
  1815. zip->init_default_conversion = (val != NULL) ? 1 : 0;
  1816. return (ARCHIVE_OK);
  1817. } else if (strcmp(key, "hdrcharset") == 0) {
  1818. if (val == NULL || val[0] == 0)
  1819. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1820. "zip: hdrcharset option needs a character-set name"
  1821. );
  1822. else {
  1823. zip->sconv = archive_string_conversion_from_charset(
  1824. &a->archive, val, 0);
  1825. if (zip->sconv != NULL) {
  1826. if (strcmp(val, "UTF-8") == 0)
  1827. zip->sconv_utf8 = zip->sconv;
  1828. ret = ARCHIVE_OK;
  1829. } else
  1830. ret = ARCHIVE_FATAL;
  1831. }
  1832. return (ret);
  1833. } else if (strcmp(key, "ignorecrc32") == 0) {
  1834. /* Mostly useful for testing. */
  1835. if (val == NULL || val[0] == 0) {
  1836. zip->crc32func = real_crc32;
  1837. zip->ignore_crc32 = 0;
  1838. } else {
  1839. zip->crc32func = fake_crc32;
  1840. zip->ignore_crc32 = 1;
  1841. }
  1842. return (ARCHIVE_OK);
  1843. } else if (strcmp(key, "mac-ext") == 0) {
  1844. zip->process_mac_extensions = (val != NULL && val[0] != 0);
  1845. return (ARCHIVE_OK);
  1846. }
  1847. /* Note: The "warn" return is just to inform the options
  1848. * supervisor that we didn't handle it. It will generate
  1849. * a suitable error if no one used this option. */
  1850. return (ARCHIVE_WARN);
  1851. }
  1852. int
  1853. archive_read_support_format_zip(struct archive *a)
  1854. {
  1855. int r;
  1856. r = archive_read_support_format_zip_streamable(a);
  1857. if (r != ARCHIVE_OK)
  1858. return r;
  1859. return (archive_read_support_format_zip_seekable(a));
  1860. }
  1861. /* ------------------------------------------------------------------------ */
  1862. /*
  1863. * Streaming-mode support
  1864. */
  1865. static int
  1866. archive_read_support_format_zip_capabilities_streamable(struct archive_read * a)
  1867. {
  1868. (void)a; /* UNUSED */
  1869. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
  1870. ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  1871. }
  1872. static int
  1873. archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid)
  1874. {
  1875. const char *p;
  1876. (void)best_bid; /* UNUSED */
  1877. if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
  1878. return (-1);
  1879. /*
  1880. * Bid of 29 here comes from:
  1881. * + 16 bits for "PK",
  1882. * + next 16-bit field has 6 options so contributes
  1883. * about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits
  1884. *
  1885. * So we've effectively verified ~29 total bits of check data.
  1886. */
  1887. if (p[0] == 'P' && p[1] == 'K') {
  1888. if ((p[2] == '\001' && p[3] == '\002')
  1889. || (p[2] == '\003' && p[3] == '\004')
  1890. || (p[2] == '\005' && p[3] == '\006')
  1891. || (p[2] == '\006' && p[3] == '\006')
  1892. || (p[2] == '\007' && p[3] == '\010')
  1893. || (p[2] == '0' && p[3] == '0'))
  1894. return (29);
  1895. }
  1896. /* TODO: It's worth looking ahead a little bit for a valid
  1897. * PK signature. In particular, that would make it possible
  1898. * to read some UUEncoded SFX files or SFX files coming from
  1899. * a network socket. */
  1900. return (0);
  1901. }
  1902. static int
  1903. archive_read_format_zip_streamable_read_header(struct archive_read *a,
  1904. struct archive_entry *entry)
  1905. {
  1906. struct zip *zip;
  1907. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  1908. if (a->archive.archive_format_name == NULL)
  1909. a->archive.archive_format_name = "ZIP";
  1910. zip = (struct zip *)(a->format->data);
  1911. /*
  1912. * It should be sufficient to call archive_read_next_header() for
  1913. * a reader to determine if an entry is encrypted or not. If the
  1914. * encryption of an entry is only detectable when calling
  1915. * archive_read_data(), so be it. We'll do the same check there
  1916. * as well.
  1917. */
  1918. if (zip->has_encrypted_entries ==
  1919. ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
  1920. zip->has_encrypted_entries = 0;
  1921. /* Make sure we have a zip_entry structure to use. */
  1922. if (zip->zip_entries == NULL) {
  1923. zip->zip_entries = malloc(sizeof(struct zip_entry));
  1924. if (zip->zip_entries == NULL) {
  1925. archive_set_error(&a->archive, ENOMEM,
  1926. "Out of memory");
  1927. return ARCHIVE_FATAL;
  1928. }
  1929. }
  1930. zip->entry = zip->zip_entries;
  1931. memset(zip->entry, 0, sizeof(struct zip_entry));
  1932. if (zip->cctx_valid)
  1933. archive_decrypto_aes_ctr_release(&zip->cctx);
  1934. if (zip->hctx_valid)
  1935. archive_hmac_sha1_cleanup(&zip->hctx);
  1936. zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
  1937. __archive_read_reset_passphrase(a);
  1938. /* Search ahead for the next local file header. */
  1939. __archive_read_consume(a, zip->unconsumed);
  1940. zip->unconsumed = 0;
  1941. for (;;) {
  1942. int64_t skipped = 0;
  1943. const char *p, *end;
  1944. ssize_t bytes;
  1945. p = __archive_read_ahead(a, 4, &bytes);
  1946. if (p == NULL)
  1947. return (ARCHIVE_FATAL);
  1948. end = p + bytes;
  1949. while (p + 4 <= end) {
  1950. if (p[0] == 'P' && p[1] == 'K') {
  1951. if (p[2] == '\003' && p[3] == '\004') {
  1952. /* Regular file entry. */
  1953. __archive_read_consume(a, skipped);
  1954. return zip_read_local_file_header(a,
  1955. entry, zip);
  1956. }
  1957. /*
  1958. * TODO: We cannot restore permissions
  1959. * based only on the local file headers.
  1960. * Consider scanning the central
  1961. * directory and returning additional
  1962. * entries for at least directories.
  1963. * This would allow us to properly set
  1964. * directory permissions.
  1965. *
  1966. * This won't help us fix symlinks
  1967. * and may not help with regular file
  1968. * permissions, either. <sigh>
  1969. */
  1970. if (p[2] == '\001' && p[3] == '\002') {
  1971. return (ARCHIVE_EOF);
  1972. }
  1973. /* End of central directory? Must be an
  1974. * empty archive. */
  1975. if ((p[2] == '\005' && p[3] == '\006')
  1976. || (p[2] == '\006' && p[3] == '\006'))
  1977. return (ARCHIVE_EOF);
  1978. }
  1979. ++p;
  1980. ++skipped;
  1981. }
  1982. __archive_read_consume(a, skipped);
  1983. }
  1984. }
  1985. static int
  1986. archive_read_format_zip_read_data_skip_streamable(struct archive_read *a)
  1987. {
  1988. struct zip *zip;
  1989. int64_t bytes_skipped;
  1990. zip = (struct zip *)(a->format->data);
  1991. bytes_skipped = __archive_read_consume(a, zip->unconsumed);
  1992. zip->unconsumed = 0;
  1993. if (bytes_skipped < 0)
  1994. return (ARCHIVE_FATAL);
  1995. /* If we've already read to end of data, we're done. */
  1996. if (zip->end_of_entry)
  1997. return (ARCHIVE_OK);
  1998. /* So we know we're streaming... */
  1999. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  2000. || zip->entry->compressed_size > 0) {
  2001. /* We know the compressed length, so we can just skip. */
  2002. bytes_skipped = __archive_read_consume(a,
  2003. zip->entry_bytes_remaining);
  2004. if (bytes_skipped < 0)
  2005. return (ARCHIVE_FATAL);
  2006. return (ARCHIVE_OK);
  2007. }
  2008. if (zip->init_decryption) {
  2009. int r;
  2010. zip->has_encrypted_entries = 1;
  2011. if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
  2012. r = read_decryption_header(a);
  2013. else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
  2014. r = init_WinZip_AES_decryption(a);
  2015. else
  2016. r = init_traditional_PKWARE_decryption(a);
  2017. if (r != ARCHIVE_OK)
  2018. return (r);
  2019. zip->init_decryption = 0;
  2020. }
  2021. /* We're streaming and we don't know the length. */
  2022. /* If the body is compressed and we know the format, we can
  2023. * find an exact end-of-entry by decompressing it. */
  2024. switch (zip->entry->compression) {
  2025. #ifdef HAVE_ZLIB_H
  2026. case 8: /* Deflate compression. */
  2027. while (!zip->end_of_entry) {
  2028. int64_t offset = 0;
  2029. const void *buff = NULL;
  2030. size_t size = 0;
  2031. int r;
  2032. r = zip_read_data_deflate(a, &buff, &size, &offset);
  2033. if (r != ARCHIVE_OK)
  2034. return (r);
  2035. }
  2036. return ARCHIVE_OK;
  2037. #endif
  2038. default: /* Uncompressed or unknown. */
  2039. /* Scan for a PK\007\010 signature. */
  2040. for (;;) {
  2041. const char *p, *buff;
  2042. ssize_t bytes_avail;
  2043. buff = __archive_read_ahead(a, 16, &bytes_avail);
  2044. if (bytes_avail < 16) {
  2045. archive_set_error(&a->archive,
  2046. ARCHIVE_ERRNO_FILE_FORMAT,
  2047. "Truncated ZIP file data");
  2048. return (ARCHIVE_FATAL);
  2049. }
  2050. p = buff;
  2051. while (p <= buff + bytes_avail - 16) {
  2052. if (p[3] == 'P') { p += 3; }
  2053. else if (p[3] == 'K') { p += 2; }
  2054. else if (p[3] == '\007') { p += 1; }
  2055. else if (p[3] == '\010' && p[2] == '\007'
  2056. && p[1] == 'K' && p[0] == 'P') {
  2057. if (zip->entry->flags & LA_USED_ZIP64)
  2058. __archive_read_consume(a,
  2059. p - buff + 24);
  2060. else
  2061. __archive_read_consume(a,
  2062. p - buff + 16);
  2063. return ARCHIVE_OK;
  2064. } else { p += 4; }
  2065. }
  2066. __archive_read_consume(a, p - buff);
  2067. }
  2068. }
  2069. }
  2070. int
  2071. archive_read_support_format_zip_streamable(struct archive *_a)
  2072. {
  2073. struct archive_read *a = (struct archive_read *)_a;
  2074. struct zip *zip;
  2075. int r;
  2076. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  2077. ARCHIVE_STATE_NEW, "archive_read_support_format_zip");
  2078. zip = (struct zip *)calloc(1, sizeof(*zip));
  2079. if (zip == NULL) {
  2080. archive_set_error(&a->archive, ENOMEM,
  2081. "Can't allocate zip data");
  2082. return (ARCHIVE_FATAL);
  2083. }
  2084. /* Streamable reader doesn't support mac extensions. */
  2085. zip->process_mac_extensions = 0;
  2086. /*
  2087. * Until enough data has been read, we cannot tell about
  2088. * any encrypted entries yet.
  2089. */
  2090. zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  2091. zip->crc32func = real_crc32;
  2092. r = __archive_read_register_format(a,
  2093. zip,
  2094. "zip",
  2095. archive_read_format_zip_streamable_bid,
  2096. archive_read_format_zip_options,
  2097. archive_read_format_zip_streamable_read_header,
  2098. archive_read_format_zip_read_data,
  2099. archive_read_format_zip_read_data_skip_streamable,
  2100. NULL,
  2101. archive_read_format_zip_cleanup,
  2102. archive_read_support_format_zip_capabilities_streamable,
  2103. archive_read_format_zip_has_encrypted_entries);
  2104. if (r != ARCHIVE_OK)
  2105. free(zip);
  2106. return (ARCHIVE_OK);
  2107. }
  2108. /* ------------------------------------------------------------------------ */
  2109. /*
  2110. * Seeking-mode support
  2111. */
  2112. static int
  2113. archive_read_support_format_zip_capabilities_seekable(struct archive_read * a)
  2114. {
  2115. (void)a; /* UNUSED */
  2116. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
  2117. ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  2118. }
  2119. /*
  2120. * TODO: This is a performance sink because it forces the read core to
  2121. * drop buffered data from the start of file, which will then have to
  2122. * be re-read again if this bidder loses.
  2123. *
  2124. * We workaround this a little by passing in the best bid so far so
  2125. * that later bidders can do nothing if they know they'll never
  2126. * outbid. But we can certainly do better...
  2127. */
  2128. static int
  2129. read_eocd(struct zip *zip, const char *p, int64_t current_offset)
  2130. {
  2131. /* Sanity-check the EOCD we've found. */
  2132. /* This must be the first volume. */
  2133. if (archive_le16dec(p + 4) != 0)
  2134. return 0;
  2135. /* Central directory must be on this volume. */
  2136. if (archive_le16dec(p + 4) != archive_le16dec(p + 6))
  2137. return 0;
  2138. /* All central directory entries must be on this volume. */
  2139. if (archive_le16dec(p + 10) != archive_le16dec(p + 8))
  2140. return 0;
  2141. /* Central directory can't extend beyond start of EOCD record. */
  2142. if (archive_le32dec(p + 16) + archive_le32dec(p + 12)
  2143. > current_offset)
  2144. return 0;
  2145. /* Save the central directory location for later use. */
  2146. zip->central_directory_offset = archive_le32dec(p + 16);
  2147. /* This is just a tiny bit higher than the maximum
  2148. returned by the streaming Zip bidder. This ensures
  2149. that the more accurate seeking Zip parser wins
  2150. whenever seek is available. */
  2151. return 32;
  2152. }
  2153. /*
  2154. * Examine Zip64 EOCD locator: If it's valid, store the information
  2155. * from it.
  2156. */
  2157. static void
  2158. read_zip64_eocd(struct archive_read *a, struct zip *zip, const char *p)
  2159. {
  2160. int64_t eocd64_offset;
  2161. int64_t eocd64_size;
  2162. /* Sanity-check the locator record. */
  2163. /* Central dir must be on first volume. */
  2164. if (archive_le32dec(p + 4) != 0)
  2165. return;
  2166. /* Must be only a single volume. */
  2167. if (archive_le32dec(p + 16) != 1)
  2168. return;
  2169. /* Find the Zip64 EOCD record. */
  2170. eocd64_offset = archive_le64dec(p + 8);
  2171. if (__archive_read_seek(a, eocd64_offset, SEEK_SET) < 0)
  2172. return;
  2173. if ((p = __archive_read_ahead(a, 56, NULL)) == NULL)
  2174. return;
  2175. /* Make sure we can read all of it. */
  2176. eocd64_size = archive_le64dec(p + 4) + 12;
  2177. if (eocd64_size < 56 || eocd64_size > 16384)
  2178. return;
  2179. if ((p = __archive_read_ahead(a, (size_t)eocd64_size, NULL)) == NULL)
  2180. return;
  2181. /* Sanity-check the EOCD64 */
  2182. if (archive_le32dec(p + 16) != 0) /* Must be disk #0 */
  2183. return;
  2184. if (archive_le32dec(p + 20) != 0) /* CD must be on disk #0 */
  2185. return;
  2186. /* CD can't be split. */
  2187. if (archive_le64dec(p + 24) != archive_le64dec(p + 32))
  2188. return;
  2189. /* Save the central directory offset for later use. */
  2190. zip->central_directory_offset = archive_le64dec(p + 48);
  2191. }
  2192. static int
  2193. archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid)
  2194. {
  2195. struct zip *zip = (struct zip *)a->format->data;
  2196. int64_t file_size, current_offset;
  2197. const char *p;
  2198. int i, tail;
  2199. /* If someone has already bid more than 32, then avoid
  2200. trashing the look-ahead buffers with a seek. */
  2201. if (best_bid > 32)
  2202. return (-1);
  2203. file_size = __archive_read_seek(a, 0, SEEK_END);
  2204. if (file_size <= 0)
  2205. return 0;
  2206. /* Search last 16k of file for end-of-central-directory
  2207. * record (which starts with PK\005\006) */
  2208. tail = (int)zipmin(1024 * 16, file_size);
  2209. current_offset = __archive_read_seek(a, -tail, SEEK_END);
  2210. if (current_offset < 0)
  2211. return 0;
  2212. if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL)
  2213. return 0;
  2214. /* Boyer-Moore search backwards from the end, since we want
  2215. * to match the last EOCD in the file (there can be more than
  2216. * one if there is an uncompressed Zip archive as a member
  2217. * within this Zip archive). */
  2218. for (i = tail - 22; i > 0;) {
  2219. switch (p[i]) {
  2220. case 'P':
  2221. if (memcmp(p + i, "PK\005\006", 4) == 0) {
  2222. int ret = read_eocd(zip, p + i,
  2223. current_offset + i);
  2224. if (ret > 0) {
  2225. /* Zip64 EOCD locator precedes
  2226. * regular EOCD if present. */
  2227. if (i >= 20
  2228. && memcmp(p + i - 20, "PK\006\007", 4) == 0) {
  2229. read_zip64_eocd(a, zip, p + i - 20);
  2230. }
  2231. return (ret);
  2232. }
  2233. }
  2234. i -= 4;
  2235. break;
  2236. case 'K': i -= 1; break;
  2237. case 005: i -= 2; break;
  2238. case 006: i -= 3; break;
  2239. default: i -= 4; break;
  2240. }
  2241. }
  2242. return 0;
  2243. }
  2244. /* The red-black trees are only used in seeking mode to manage
  2245. * the in-memory copy of the central directory. */
  2246. static int
  2247. cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2)
  2248. {
  2249. const struct zip_entry *e1 = (const struct zip_entry *)n1;
  2250. const struct zip_entry *e2 = (const struct zip_entry *)n2;
  2251. if (e1->local_header_offset > e2->local_header_offset)
  2252. return -1;
  2253. if (e1->local_header_offset < e2->local_header_offset)
  2254. return 1;
  2255. return 0;
  2256. }
  2257. static int
  2258. cmp_key(const struct archive_rb_node *n, const void *key)
  2259. {
  2260. /* This function won't be called */
  2261. (void)n; /* UNUSED */
  2262. (void)key; /* UNUSED */
  2263. return 1;
  2264. }
  2265. static const struct archive_rb_tree_ops rb_ops = {
  2266. &cmp_node, &cmp_key
  2267. };
  2268. static int
  2269. rsrc_cmp_node(const struct archive_rb_node *n1,
  2270. const struct archive_rb_node *n2)
  2271. {
  2272. const struct zip_entry *e1 = (const struct zip_entry *)n1;
  2273. const struct zip_entry *e2 = (const struct zip_entry *)n2;
  2274. return (strcmp(e2->rsrcname.s, e1->rsrcname.s));
  2275. }
  2276. static int
  2277. rsrc_cmp_key(const struct archive_rb_node *n, const void *key)
  2278. {
  2279. const struct zip_entry *e = (const struct zip_entry *)n;
  2280. return (strcmp((const char *)key, e->rsrcname.s));
  2281. }
  2282. static const struct archive_rb_tree_ops rb_rsrc_ops = {
  2283. &rsrc_cmp_node, &rsrc_cmp_key
  2284. };
  2285. static const char *
  2286. rsrc_basename(const char *name, size_t name_length)
  2287. {
  2288. const char *s, *r;
  2289. r = s = name;
  2290. for (;;) {
  2291. s = memchr(s, '/', name_length - (s - name));
  2292. if (s == NULL)
  2293. break;
  2294. r = ++s;
  2295. }
  2296. return (r);
  2297. }
  2298. static void
  2299. expose_parent_dirs(struct zip *zip, const char *name, size_t name_length)
  2300. {
  2301. struct archive_string str;
  2302. struct zip_entry *dir;
  2303. char *s;
  2304. archive_string_init(&str);
  2305. archive_strncpy(&str, name, name_length);
  2306. for (;;) {
  2307. s = strrchr(str.s, '/');
  2308. if (s == NULL)
  2309. break;
  2310. *s = '\0';
  2311. /* Transfer the parent directory from zip->tree_rsrc RB
  2312. * tree to zip->tree RB tree to expose. */
  2313. dir = (struct zip_entry *)
  2314. __archive_rb_tree_find_node(&zip->tree_rsrc, str.s);
  2315. if (dir == NULL)
  2316. break;
  2317. __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
  2318. archive_string_free(&dir->rsrcname);
  2319. __archive_rb_tree_insert_node(&zip->tree, &dir->node);
  2320. }
  2321. archive_string_free(&str);
  2322. }
  2323. static int
  2324. slurp_central_directory(struct archive_read *a, struct zip *zip)
  2325. {
  2326. ssize_t i;
  2327. unsigned found;
  2328. int64_t correction;
  2329. ssize_t bytes_avail;
  2330. const char *p;
  2331. /*
  2332. * Find the start of the central directory. The end-of-CD
  2333. * record has our starting point, but there are lots of
  2334. * Zip archives which have had other data prepended to the
  2335. * file, which makes the recorded offsets all too small.
  2336. * So we search forward from the specified offset until we
  2337. * find the real start of the central directory. Then we
  2338. * know the correction we need to apply to account for leading
  2339. * padding.
  2340. */
  2341. if (__archive_read_seek(a, zip->central_directory_offset, SEEK_SET) < 0)
  2342. return ARCHIVE_FATAL;
  2343. found = 0;
  2344. while (!found) {
  2345. if ((p = __archive_read_ahead(a, 20, &bytes_avail)) == NULL)
  2346. return ARCHIVE_FATAL;
  2347. for (found = 0, i = 0; !found && i < bytes_avail - 4;) {
  2348. switch (p[i + 3]) {
  2349. case 'P': i += 3; break;
  2350. case 'K': i += 2; break;
  2351. case 001: i += 1; break;
  2352. case 002:
  2353. if (memcmp(p + i, "PK\001\002", 4) == 0) {
  2354. p += i;
  2355. found = 1;
  2356. } else
  2357. i += 4;
  2358. break;
  2359. case 005: i += 1; break;
  2360. case 006:
  2361. if (memcmp(p + i, "PK\005\006", 4) == 0) {
  2362. p += i;
  2363. found = 1;
  2364. } else if (memcmp(p + i, "PK\006\006", 4) == 0) {
  2365. p += i;
  2366. found = 1;
  2367. } else
  2368. i += 1;
  2369. break;
  2370. default: i += 4; break;
  2371. }
  2372. }
  2373. __archive_read_consume(a, i);
  2374. }
  2375. correction = archive_filter_bytes(&a->archive, 0)
  2376. - zip->central_directory_offset;
  2377. __archive_rb_tree_init(&zip->tree, &rb_ops);
  2378. __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops);
  2379. zip->central_directory_entries_total = 0;
  2380. while (1) {
  2381. struct zip_entry *zip_entry;
  2382. size_t filename_length, extra_length, comment_length;
  2383. uint32_t external_attributes;
  2384. const char *name, *r;
  2385. if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
  2386. return ARCHIVE_FATAL;
  2387. if (memcmp(p, "PK\006\006", 4) == 0
  2388. || memcmp(p, "PK\005\006", 4) == 0) {
  2389. break;
  2390. } else if (memcmp(p, "PK\001\002", 4) != 0) {
  2391. archive_set_error(&a->archive,
  2392. -1, "Invalid central directory signature");
  2393. return ARCHIVE_FATAL;
  2394. }
  2395. if ((p = __archive_read_ahead(a, 46, NULL)) == NULL)
  2396. return ARCHIVE_FATAL;
  2397. zip_entry = calloc(1, sizeof(struct zip_entry));
  2398. zip_entry->next = zip->zip_entries;
  2399. zip_entry->flags |= LA_FROM_CENTRAL_DIRECTORY;
  2400. zip->zip_entries = zip_entry;
  2401. zip->central_directory_entries_total++;
  2402. /* version = p[4]; */
  2403. zip_entry->system = p[5];
  2404. /* version_required = archive_le16dec(p + 6); */
  2405. zip_entry->zip_flags = archive_le16dec(p + 8);
  2406. if (zip_entry->zip_flags
  2407. & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)){
  2408. zip->has_encrypted_entries = 1;
  2409. }
  2410. zip_entry->compression = (char)archive_le16dec(p + 10);
  2411. zip_entry->mtime = zip_time(p + 12);
  2412. zip_entry->crc32 = archive_le32dec(p + 16);
  2413. if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  2414. zip_entry->decdat = p[13];
  2415. else
  2416. zip_entry->decdat = p[19];
  2417. zip_entry->compressed_size = archive_le32dec(p + 20);
  2418. zip_entry->uncompressed_size = archive_le32dec(p + 24);
  2419. filename_length = archive_le16dec(p + 28);
  2420. extra_length = archive_le16dec(p + 30);
  2421. comment_length = archive_le16dec(p + 32);
  2422. /* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */
  2423. /* internal_attributes = archive_le16dec(p + 36); */ /* text bit */
  2424. external_attributes = archive_le32dec(p + 38);
  2425. zip_entry->local_header_offset =
  2426. archive_le32dec(p + 42) + correction;
  2427. /* If we can't guess the mode, leave it zero here;
  2428. when we read the local file header we might get
  2429. more information. */
  2430. if (zip_entry->system == 3) {
  2431. zip_entry->mode = external_attributes >> 16;
  2432. } else if (zip_entry->system == 0) {
  2433. // Interpret MSDOS directory bit
  2434. if (0x10 == (external_attributes & 0x10)) {
  2435. zip_entry->mode = AE_IFDIR | 0775;
  2436. } else {
  2437. zip_entry->mode = AE_IFREG | 0664;
  2438. }
  2439. if (0x01 == (external_attributes & 0x01)) {
  2440. // Read-only bit; strip write permissions
  2441. zip_entry->mode &= 0555;
  2442. }
  2443. } else {
  2444. zip_entry->mode = 0;
  2445. }
  2446. /* We're done with the regular data; get the filename and
  2447. * extra data. */
  2448. __archive_read_consume(a, 46);
  2449. p = __archive_read_ahead(a, filename_length + extra_length,
  2450. NULL);
  2451. if (p == NULL) {
  2452. archive_set_error(&a->archive,
  2453. ARCHIVE_ERRNO_FILE_FORMAT,
  2454. "Truncated ZIP file header");
  2455. return ARCHIVE_FATAL;
  2456. }
  2457. process_extra(p + filename_length, extra_length, zip_entry);
  2458. /*
  2459. * Mac resource fork files are stored under the
  2460. * "__MACOSX/" directory, so we should check if
  2461. * it is.
  2462. */
  2463. if (!zip->process_mac_extensions) {
  2464. /* Treat every entry as a regular entry. */
  2465. __archive_rb_tree_insert_node(&zip->tree,
  2466. &zip_entry->node);
  2467. } else {
  2468. name = p;
  2469. r = rsrc_basename(name, filename_length);
  2470. if (filename_length >= 9 &&
  2471. strncmp("__MACOSX/", name, 9) == 0) {
  2472. /* If this file is not a resource fork nor
  2473. * a directory. We should treat it as a non
  2474. * resource fork file to expose it. */
  2475. if (name[filename_length-1] != '/' &&
  2476. (r - name < 3 || r[0] != '.' || r[1] != '_')) {
  2477. __archive_rb_tree_insert_node(
  2478. &zip->tree, &zip_entry->node);
  2479. /* Expose its parent directories. */
  2480. expose_parent_dirs(zip, name,
  2481. filename_length);
  2482. } else {
  2483. /* This file is a resource fork file or
  2484. * a directory. */
  2485. archive_strncpy(&(zip_entry->rsrcname),
  2486. name, filename_length);
  2487. __archive_rb_tree_insert_node(
  2488. &zip->tree_rsrc, &zip_entry->node);
  2489. }
  2490. } else {
  2491. /* Generate resource fork name to find its
  2492. * resource file at zip->tree_rsrc. */
  2493. archive_strcpy(&(zip_entry->rsrcname),
  2494. "__MACOSX/");
  2495. archive_strncat(&(zip_entry->rsrcname),
  2496. name, r - name);
  2497. archive_strcat(&(zip_entry->rsrcname), "._");
  2498. archive_strncat(&(zip_entry->rsrcname),
  2499. name + (r - name),
  2500. filename_length - (r - name));
  2501. /* Register an entry to RB tree to sort it by
  2502. * file offset. */
  2503. __archive_rb_tree_insert_node(&zip->tree,
  2504. &zip_entry->node);
  2505. }
  2506. }
  2507. /* Skip the comment too ... */
  2508. __archive_read_consume(a,
  2509. filename_length + extra_length + comment_length);
  2510. }
  2511. return ARCHIVE_OK;
  2512. }
  2513. static ssize_t
  2514. zip_get_local_file_header_size(struct archive_read *a, size_t extra)
  2515. {
  2516. const char *p;
  2517. ssize_t filename_length, extra_length;
  2518. if ((p = __archive_read_ahead(a, extra + 30, NULL)) == NULL) {
  2519. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2520. "Truncated ZIP file header");
  2521. return (ARCHIVE_WARN);
  2522. }
  2523. p += extra;
  2524. if (memcmp(p, "PK\003\004", 4) != 0) {
  2525. archive_set_error(&a->archive, -1, "Damaged Zip archive");
  2526. return ARCHIVE_WARN;
  2527. }
  2528. filename_length = archive_le16dec(p + 26);
  2529. extra_length = archive_le16dec(p + 28);
  2530. return (30 + filename_length + extra_length);
  2531. }
  2532. static int
  2533. zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
  2534. struct zip_entry *rsrc)
  2535. {
  2536. struct zip *zip = (struct zip *)a->format->data;
  2537. unsigned char *metadata, *mp;
  2538. int64_t offset = archive_filter_bytes(&a->archive, 0);
  2539. size_t remaining_bytes, metadata_bytes;
  2540. ssize_t hsize;
  2541. int ret = ARCHIVE_OK, eof;
  2542. switch(rsrc->compression) {
  2543. case 0: /* No compression. */
  2544. if (rsrc->uncompressed_size != rsrc->compressed_size) {
  2545. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2546. "Malformed OS X metadata entry: inconsistent size");
  2547. return (ARCHIVE_FATAL);
  2548. }
  2549. #ifdef HAVE_ZLIB_H
  2550. case 8: /* Deflate compression. */
  2551. #endif
  2552. break;
  2553. default: /* Unsupported compression. */
  2554. /* Return a warning. */
  2555. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2556. "Unsupported ZIP compression method (%s)",
  2557. compression_name(rsrc->compression));
  2558. /* We can't decompress this entry, but we will
  2559. * be able to skip() it and try the next entry. */
  2560. return (ARCHIVE_WARN);
  2561. }
  2562. if (rsrc->uncompressed_size > (4 * 1024 * 1024)) {
  2563. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2564. "Mac metadata is too large: %jd > 4M bytes",
  2565. (intmax_t)rsrc->uncompressed_size);
  2566. return (ARCHIVE_WARN);
  2567. }
  2568. if (rsrc->compressed_size > (4 * 1024 * 1024)) {
  2569. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2570. "Mac metadata is too large: %jd > 4M bytes",
  2571. (intmax_t)rsrc->compressed_size);
  2572. return (ARCHIVE_WARN);
  2573. }
  2574. metadata = malloc((size_t)rsrc->uncompressed_size);
  2575. if (metadata == NULL) {
  2576. archive_set_error(&a->archive, ENOMEM,
  2577. "Can't allocate memory for Mac metadata");
  2578. return (ARCHIVE_FATAL);
  2579. }
  2580. if (offset < rsrc->local_header_offset)
  2581. __archive_read_consume(a, rsrc->local_header_offset - offset);
  2582. else if (offset != rsrc->local_header_offset) {
  2583. __archive_read_seek(a, rsrc->local_header_offset, SEEK_SET);
  2584. }
  2585. hsize = zip_get_local_file_header_size(a, 0);
  2586. __archive_read_consume(a, hsize);
  2587. remaining_bytes = (size_t)rsrc->compressed_size;
  2588. metadata_bytes = (size_t)rsrc->uncompressed_size;
  2589. mp = metadata;
  2590. eof = 0;
  2591. while (!eof && remaining_bytes) {
  2592. const unsigned char *p;
  2593. ssize_t bytes_avail;
  2594. size_t bytes_used;
  2595. p = __archive_read_ahead(a, 1, &bytes_avail);
  2596. if (p == NULL) {
  2597. archive_set_error(&a->archive,
  2598. ARCHIVE_ERRNO_FILE_FORMAT,
  2599. "Truncated ZIP file header");
  2600. ret = ARCHIVE_WARN;
  2601. goto exit_mac_metadata;
  2602. }
  2603. if ((size_t)bytes_avail > remaining_bytes)
  2604. bytes_avail = remaining_bytes;
  2605. switch(rsrc->compression) {
  2606. case 0: /* No compression. */
  2607. if ((size_t)bytes_avail > metadata_bytes)
  2608. bytes_avail = metadata_bytes;
  2609. memcpy(mp, p, bytes_avail);
  2610. bytes_used = (size_t)bytes_avail;
  2611. metadata_bytes -= bytes_used;
  2612. mp += bytes_used;
  2613. if (metadata_bytes == 0)
  2614. eof = 1;
  2615. break;
  2616. #ifdef HAVE_ZLIB_H
  2617. case 8: /* Deflate compression. */
  2618. {
  2619. int r;
  2620. ret = zip_deflate_init(a, zip);
  2621. if (ret != ARCHIVE_OK)
  2622. goto exit_mac_metadata;
  2623. zip->stream.next_in =
  2624. (Bytef *)(uintptr_t)(const void *)p;
  2625. zip->stream.avail_in = (uInt)bytes_avail;
  2626. zip->stream.total_in = 0;
  2627. zip->stream.next_out = mp;
  2628. zip->stream.avail_out = (uInt)metadata_bytes;
  2629. zip->stream.total_out = 0;
  2630. r = inflate(&zip->stream, 0);
  2631. switch (r) {
  2632. case Z_OK:
  2633. break;
  2634. case Z_STREAM_END:
  2635. eof = 1;
  2636. break;
  2637. case Z_MEM_ERROR:
  2638. archive_set_error(&a->archive, ENOMEM,
  2639. "Out of memory for ZIP decompression");
  2640. ret = ARCHIVE_FATAL;
  2641. goto exit_mac_metadata;
  2642. default:
  2643. archive_set_error(&a->archive,
  2644. ARCHIVE_ERRNO_MISC,
  2645. "ZIP decompression failed (%d)", r);
  2646. ret = ARCHIVE_FATAL;
  2647. goto exit_mac_metadata;
  2648. }
  2649. bytes_used = zip->stream.total_in;
  2650. metadata_bytes -= zip->stream.total_out;
  2651. mp += zip->stream.total_out;
  2652. break;
  2653. }
  2654. #endif
  2655. default:
  2656. bytes_used = 0;
  2657. break;
  2658. }
  2659. __archive_read_consume(a, bytes_used);
  2660. remaining_bytes -= bytes_used;
  2661. }
  2662. archive_entry_copy_mac_metadata(entry, metadata,
  2663. (size_t)rsrc->uncompressed_size - metadata_bytes);
  2664. exit_mac_metadata:
  2665. __archive_read_seek(a, offset, SEEK_SET);
  2666. zip->decompress_init = 0;
  2667. free(metadata);
  2668. return (ret);
  2669. }
  2670. static int
  2671. archive_read_format_zip_seekable_read_header(struct archive_read *a,
  2672. struct archive_entry *entry)
  2673. {
  2674. struct zip *zip = (struct zip *)a->format->data;
  2675. struct zip_entry *rsrc;
  2676. int64_t offset;
  2677. int r, ret = ARCHIVE_OK;
  2678. /*
  2679. * It should be sufficient to call archive_read_next_header() for
  2680. * a reader to determine if an entry is encrypted or not. If the
  2681. * encryption of an entry is only detectable when calling
  2682. * archive_read_data(), so be it. We'll do the same check there
  2683. * as well.
  2684. */
  2685. if (zip->has_encrypted_entries ==
  2686. ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
  2687. zip->has_encrypted_entries = 0;
  2688. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  2689. if (a->archive.archive_format_name == NULL)
  2690. a->archive.archive_format_name = "ZIP";
  2691. if (zip->zip_entries == NULL) {
  2692. r = slurp_central_directory(a, zip);
  2693. if (r != ARCHIVE_OK)
  2694. return r;
  2695. /* Get first entry whose local header offset is lower than
  2696. * other entries in the archive file. */
  2697. zip->entry =
  2698. (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
  2699. } else if (zip->entry != NULL) {
  2700. /* Get next entry in local header offset order. */
  2701. zip->entry = (struct zip_entry *)__archive_rb_tree_iterate(
  2702. &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT);
  2703. }
  2704. if (zip->entry == NULL)
  2705. return ARCHIVE_EOF;
  2706. if (zip->entry->rsrcname.s)
  2707. rsrc = (struct zip_entry *)__archive_rb_tree_find_node(
  2708. &zip->tree_rsrc, zip->entry->rsrcname.s);
  2709. else
  2710. rsrc = NULL;
  2711. if (zip->cctx_valid)
  2712. archive_decrypto_aes_ctr_release(&zip->cctx);
  2713. if (zip->hctx_valid)
  2714. archive_hmac_sha1_cleanup(&zip->hctx);
  2715. zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
  2716. __archive_read_reset_passphrase(a);
  2717. /* File entries are sorted by the header offset, we should mostly
  2718. * use __archive_read_consume to advance a read point to avoid redundant
  2719. * data reading. */
  2720. offset = archive_filter_bytes(&a->archive, 0);
  2721. if (offset < zip->entry->local_header_offset)
  2722. __archive_read_consume(a,
  2723. zip->entry->local_header_offset - offset);
  2724. else if (offset != zip->entry->local_header_offset) {
  2725. __archive_read_seek(a, zip->entry->local_header_offset,
  2726. SEEK_SET);
  2727. }
  2728. zip->unconsumed = 0;
  2729. r = zip_read_local_file_header(a, entry, zip);
  2730. if (r != ARCHIVE_OK)
  2731. return r;
  2732. if (rsrc) {
  2733. int ret2 = zip_read_mac_metadata(a, entry, rsrc);
  2734. if (ret2 < ret)
  2735. ret = ret2;
  2736. }
  2737. return (ret);
  2738. }
  2739. /*
  2740. * We're going to seek for the next header anyway, so we don't
  2741. * need to bother doing anything here.
  2742. */
  2743. static int
  2744. archive_read_format_zip_read_data_skip_seekable(struct archive_read *a)
  2745. {
  2746. struct zip *zip;
  2747. zip = (struct zip *)(a->format->data);
  2748. zip->unconsumed = 0;
  2749. return (ARCHIVE_OK);
  2750. }
  2751. int
  2752. archive_read_support_format_zip_seekable(struct archive *_a)
  2753. {
  2754. struct archive_read *a = (struct archive_read *)_a;
  2755. struct zip *zip;
  2756. int r;
  2757. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  2758. ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable");
  2759. zip = (struct zip *)calloc(1, sizeof(*zip));
  2760. if (zip == NULL) {
  2761. archive_set_error(&a->archive, ENOMEM,
  2762. "Can't allocate zip data");
  2763. return (ARCHIVE_FATAL);
  2764. }
  2765. #ifdef HAVE_COPYFILE_H
  2766. /* Set this by default on Mac OS. */
  2767. zip->process_mac_extensions = 1;
  2768. #endif
  2769. /*
  2770. * Until enough data has been read, we cannot tell about
  2771. * any encrypted entries yet.
  2772. */
  2773. zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  2774. zip->crc32func = real_crc32;
  2775. r = __archive_read_register_format(a,
  2776. zip,
  2777. "zip",
  2778. archive_read_format_zip_seekable_bid,
  2779. archive_read_format_zip_options,
  2780. archive_read_format_zip_seekable_read_header,
  2781. archive_read_format_zip_read_data,
  2782. archive_read_format_zip_read_data_skip_seekable,
  2783. NULL,
  2784. archive_read_format_zip_cleanup,
  2785. archive_read_support_format_zip_capabilities_seekable,
  2786. archive_read_format_zip_has_encrypted_entries);
  2787. if (r != ARCHIVE_OK)
  2788. free(zip);
  2789. return (ARCHIVE_OK);
  2790. }