archive_read_support_format_zip.c 88 KB

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