archive_read_support_format_zip.c 89 KB

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