archive_read_support_format_zip.c 84 KB

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