archive_read_support_format_zip.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  1. /*-
  2. * Copyright (c) 2004-2013 Tim Kientzle
  3. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  4. * Copyright (c) 2013 Konrad Kleine
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_zip.c 201102 2009-12-28 03:11:36Z kientzle $");
  29. /*
  30. * The definitive documentation of the Zip file format is:
  31. * http://www.pkware.com/documents/casestudies/APPNOTE.TXT
  32. *
  33. * The Info-Zip project has pioneered various extensions to better
  34. * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
  35. * "Ux", and 0x7875 "ux" extensions for time and ownership
  36. * information.
  37. *
  38. * History of this code: The streaming Zip reader was first added to
  39. * libarchive in January 2005. Support for seekable input sources was
  40. * added in Nov 2011.
  41. */
  42. #ifdef HAVE_ERRNO_H
  43. #include <errno.h>
  44. #endif
  45. #ifdef HAVE_STDLIB_H
  46. #include <stdlib.h>
  47. #endif
  48. #ifdef HAVE_ZLIB_H
  49. #include <cm_zlib.h>
  50. #endif
  51. #include "archive.h"
  52. #include "archive_endian.h"
  53. #include "archive_entry.h"
  54. #include "archive_entry_locale.h"
  55. #include "archive_private.h"
  56. #include "archive_rb.h"
  57. #include "archive_read_private.h"
  58. #ifndef HAVE_ZLIB_H
  59. #include "archive_crc32.h"
  60. #endif
  61. #if defined(_WIN32) && !defined(__CYGWIN__)
  62. # define snprintf _snprintf
  63. #endif
  64. struct zip_entry {
  65. struct archive_rb_node node;
  66. struct zip_entry *next;
  67. int64_t local_header_offset;
  68. int64_t compressed_size;
  69. int64_t uncompressed_size;
  70. int64_t gid;
  71. int64_t uid;
  72. struct archive_string rsrcname;
  73. time_t mtime;
  74. time_t atime;
  75. time_t ctime;
  76. uint32_t crc32;
  77. uint16_t mode;
  78. uint16_t zip_flags; /* From GP Flags Field */
  79. unsigned char compression;
  80. unsigned char system; /* From "version written by" */
  81. unsigned char flags; /* Our extra markers. */
  82. };
  83. /* Bits used in zip_flags. */
  84. #define ZIP_ENCRYPTED (1 << 0)
  85. #define ZIP_LENGTH_AT_END (1 << 3)
  86. #define ZIP_STRONG_ENCRYPTED (1 << 6)
  87. #define ZIP_UTF8_NAME (1 << 11)
  88. /* See "7.2 Single Password Symmetric Encryption Method"
  89. in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
  90. #define ZIP_CENTRAL_DIRECTORY_ENCRYPTED (1 << 13)
  91. /* Bits used in flags. */
  92. #define LA_USED_ZIP64 (1 << 0)
  93. #define LA_FROM_CENTRAL_DIRECTORY (1 << 1)
  94. struct zip {
  95. /* Structural information about the archive. */
  96. char format_name[64];
  97. int64_t central_directory_offset;
  98. size_t central_directory_entries_total;
  99. size_t central_directory_entries_on_this_disk;
  100. int has_encrypted_entries;
  101. /* List of entries (seekable Zip only) */
  102. struct zip_entry *zip_entries;
  103. struct archive_rb_tree tree;
  104. struct archive_rb_tree tree_rsrc;
  105. /* Bytes read but not yet consumed via __archive_read_consume() */
  106. size_t unconsumed;
  107. /* Information about entry we're currently reading. */
  108. struct zip_entry *entry;
  109. int64_t entry_bytes_remaining;
  110. /* These count the number of bytes actually read for the entry. */
  111. int64_t entry_compressed_bytes_read;
  112. int64_t entry_uncompressed_bytes_read;
  113. /* Running CRC32 of the decompressed data */
  114. unsigned long entry_crc32;
  115. unsigned long (*crc32func)(unsigned long, const void *, size_t);
  116. char ignore_crc32;
  117. /* Flags to mark progress of decompression. */
  118. char decompress_init;
  119. char end_of_entry;
  120. #ifdef HAVE_ZLIB_H
  121. unsigned char *uncompressed_buffer;
  122. size_t uncompressed_buffer_size;
  123. z_stream stream;
  124. char stream_valid;
  125. #endif
  126. struct archive_string_conv *sconv;
  127. struct archive_string_conv *sconv_default;
  128. struct archive_string_conv *sconv_utf8;
  129. int init_default_conversion;
  130. int process_mac_extensions;
  131. };
  132. /* Many systems define min or MIN, but not all. */
  133. #define zipmin(a,b) ((a) < (b) ? (a) : (b))
  134. /* ------------------------------------------------------------------------ */
  135. /*
  136. * Common code for streaming or seeking modes.
  137. *
  138. * Includes code to read local file headers, decompress data
  139. * from entry bodies, and common API.
  140. */
  141. static unsigned long
  142. real_crc32(unsigned long crc, const void *buff, size_t len)
  143. {
  144. return crc32(crc, buff, len);
  145. }
  146. static unsigned long
  147. fake_crc32(unsigned long crc, const void *buff, size_t len)
  148. {
  149. (void)crc; /* UNUSED */
  150. (void)buff; /* UNUSED */
  151. (void)len; /* UNUSED */
  152. return 0;
  153. }
  154. static struct {
  155. int id;
  156. const char * name;
  157. } compression_methods[] = {
  158. {0, "uncompressed"}, /* The file is stored (no compression) */
  159. {1, "shrinking"}, /* The file is Shrunk */
  160. {2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
  161. {3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
  162. {4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
  163. {5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
  164. {6, "imploded"}, /* The file is Imploded */
  165. {7, "reserved"}, /* Reserved for Tokenizing compression algorithm */
  166. {8, "deflation"}, /* The file is Deflated */
  167. {9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
  168. {10, "ibm-terse"}, /* PKWARE Data Compression Library Imploding (old IBM TERSE) */
  169. {11, "reserved"}, /* Reserved by PKWARE */
  170. {12, "bzip"}, /* File is compressed using BZIP2 algorithm */
  171. {13, "reserved"}, /* Reserved by PKWARE */
  172. {14, "lzma"}, /* LZMA (EFS) */
  173. {15, "reserved"}, /* Reserved by PKWARE */
  174. {16, "reserved"}, /* Reserved by PKWARE */
  175. {17, "reserved"}, /* Reserved by PKWARE */
  176. {18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
  177. {19, "ibm-lz777"}, /* IBM LZ77 z Architecture (PFS) */
  178. {97, "wav-pack"}, /* WavPack compressed data */
  179. {98, "ppmd-1"} /* PPMd version I, Rev 1 */
  180. };
  181. static const char *
  182. compression_name(const int compression)
  183. {
  184. static const int num_compression_methods = sizeof(compression_methods)/sizeof(compression_methods[0]);
  185. int i=0;
  186. while(compression >= 0 && i < num_compression_methods) {
  187. if (compression_methods[i].id == compression) {
  188. return compression_methods[i].name;
  189. }
  190. i++;
  191. }
  192. return "??";
  193. }
  194. /* Convert an MSDOS-style date/time into Unix-style time. */
  195. static time_t
  196. zip_time(const char *p)
  197. {
  198. int msTime, msDate;
  199. struct tm ts;
  200. msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
  201. msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
  202. memset(&ts, 0, sizeof(ts));
  203. ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
  204. ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
  205. ts.tm_mday = msDate & 0x1f; /* Day of month. */
  206. ts.tm_hour = (msTime >> 11) & 0x1f;
  207. ts.tm_min = (msTime >> 5) & 0x3f;
  208. ts.tm_sec = (msTime << 1) & 0x3e;
  209. ts.tm_isdst = -1;
  210. return mktime(&ts);
  211. }
  212. /*
  213. * The extra data is stored as a list of
  214. * id1+size1+data1 + id2+size2+data2 ...
  215. * triplets. id and size are 2 bytes each.
  216. */
  217. static void
  218. process_extra(const char *p, size_t extra_length, struct zip_entry* zip_entry)
  219. {
  220. unsigned offset = 0;
  221. while (offset < extra_length - 4)
  222. {
  223. unsigned short headerid = archive_le16dec(p + offset);
  224. unsigned short datasize = archive_le16dec(p + offset + 2);
  225. offset += 4;
  226. if (offset + datasize > extra_length)
  227. break;
  228. #ifdef DEBUG
  229. fprintf(stderr, "Header id 0x%x, length %d\n",
  230. headerid, datasize);
  231. #endif
  232. switch (headerid) {
  233. case 0x0001:
  234. /* Zip64 extended information extra field. */
  235. zip_entry->flags |= LA_USED_ZIP64;
  236. if (zip_entry->uncompressed_size == 0xffffffff) {
  237. if (datasize < 8)
  238. break;
  239. zip_entry->uncompressed_size =
  240. archive_le64dec(p + offset);
  241. offset += 8;
  242. datasize -= 8;
  243. }
  244. if (zip_entry->compressed_size == 0xffffffff) {
  245. if (datasize < 8)
  246. break;
  247. zip_entry->compressed_size =
  248. archive_le64dec(p + offset);
  249. offset += 8;
  250. datasize -= 8;
  251. }
  252. if (zip_entry->local_header_offset == 0xffffffff) {
  253. if (datasize < 8)
  254. break;
  255. zip_entry->local_header_offset =
  256. archive_le64dec(p + offset);
  257. offset += 8;
  258. datasize -= 8;
  259. }
  260. /* archive_le32dec(p + offset) gives disk
  261. * on which file starts, but we don't handle
  262. * multi-volume Zip files. */
  263. break;
  264. case 0x5455:
  265. {
  266. /* Extended time field "UT". */
  267. int flags = p[offset];
  268. offset++;
  269. datasize--;
  270. /* Flag bits indicate which dates are present. */
  271. if (flags & 0x01)
  272. {
  273. #ifdef DEBUG
  274. fprintf(stderr, "mtime: %lld -> %d\n",
  275. (long long)zip_entry->mtime,
  276. archive_le32dec(p + offset));
  277. #endif
  278. if (datasize < 4)
  279. break;
  280. zip_entry->mtime = archive_le32dec(p + offset);
  281. offset += 4;
  282. datasize -= 4;
  283. }
  284. if (flags & 0x02)
  285. {
  286. if (datasize < 4)
  287. break;
  288. zip_entry->atime = archive_le32dec(p + offset);
  289. offset += 4;
  290. datasize -= 4;
  291. }
  292. if (flags & 0x04)
  293. {
  294. if (datasize < 4)
  295. break;
  296. zip_entry->ctime = archive_le32dec(p + offset);
  297. offset += 4;
  298. datasize -= 4;
  299. }
  300. break;
  301. }
  302. case 0x5855:
  303. {
  304. /* Info-ZIP Unix Extra Field (old version) "UX". */
  305. if (datasize >= 8) {
  306. zip_entry->atime = archive_le32dec(p + offset);
  307. zip_entry->mtime =
  308. archive_le32dec(p + offset + 4);
  309. }
  310. if (datasize >= 12) {
  311. zip_entry->uid =
  312. archive_le16dec(p + offset + 8);
  313. zip_entry->gid =
  314. archive_le16dec(p + offset + 10);
  315. }
  316. break;
  317. }
  318. case 0x6c65:
  319. {
  320. /* Experimental 'el' field */
  321. /*
  322. * Introduced Dec 2013 to provide a way to
  323. * include external file attributes in local file
  324. * header. This provides file type and permission
  325. * information necessary to support full streaming
  326. * extraction. Currently being discussed with
  327. * other Zip developers... subject to change.
  328. */
  329. int bitmap, bitmap_last;
  330. if (datasize < 1)
  331. break;
  332. bitmap_last = bitmap = 0xff & p[offset];
  333. offset += 1;
  334. datasize -= 1;
  335. /* We only support first 7 bits of bitmap; skip rest. */
  336. while ((bitmap_last & 0x80) != 0
  337. && datasize >= 1) {
  338. bitmap_last = p[offset];
  339. offset += 1;
  340. datasize -= 1;
  341. }
  342. if (bitmap & 1) {
  343. // 2 byte "version made by"
  344. if (datasize < 2)
  345. break;
  346. zip_entry->system
  347. = archive_le16dec(p + offset) >> 8;
  348. offset += 2;
  349. datasize -= 2;
  350. }
  351. if (bitmap & 2) {
  352. // 2 byte "internal file attributes"
  353. uint32_t internal_attributes;
  354. if (datasize < 2)
  355. break;
  356. internal_attributes
  357. = archive_le16dec(p + offset);
  358. // Not used by libarchive at present.
  359. (void)internal_attributes; /* UNUSED */
  360. offset += 2;
  361. datasize -= 2;
  362. }
  363. if (bitmap & 4) {
  364. // 4 byte "external file attributes"
  365. uint32_t external_attributes;
  366. if (datasize < 4)
  367. break;
  368. external_attributes
  369. = archive_le32dec(p + offset);
  370. if (zip_entry->system == 3) {
  371. zip_entry->mode
  372. = external_attributes >> 16;
  373. }
  374. offset += 4;
  375. datasize -= 4;
  376. }
  377. if (bitmap & 8) {
  378. // 2 byte comment length + comment
  379. uint32_t comment_length;
  380. if (datasize < 2)
  381. break;
  382. comment_length
  383. = archive_le16dec(p + offset);
  384. offset += 2;
  385. datasize -= 2;
  386. if (datasize < comment_length)
  387. break;
  388. // Comment is not supported by libarchive
  389. offset += comment_length;
  390. datasize -= comment_length;
  391. }
  392. break;
  393. }
  394. case 0x7855:
  395. /* Info-ZIP Unix Extra Field (type 2) "Ux". */
  396. #ifdef DEBUG
  397. fprintf(stderr, "uid %d gid %d\n",
  398. archive_le16dec(p + offset),
  399. archive_le16dec(p + offset + 2));
  400. #endif
  401. if (datasize >= 2)
  402. zip_entry->uid = archive_le16dec(p + offset);
  403. if (datasize >= 4)
  404. zip_entry->gid =
  405. archive_le16dec(p + offset + 2);
  406. break;
  407. case 0x7875:
  408. {
  409. /* Info-Zip Unix Extra Field (type 3) "ux". */
  410. int uidsize = 0, gidsize = 0;
  411. /* TODO: support arbitrary uidsize/gidsize. */
  412. if (datasize >= 1 && p[offset] == 1) {/* version=1 */
  413. if (datasize >= 4) {
  414. /* get a uid size. */
  415. uidsize = p[offset+1];
  416. if (uidsize == 2)
  417. zip_entry->uid =
  418. archive_le16dec(
  419. p + offset + 2);
  420. else if (uidsize == 4 && datasize >= 6)
  421. zip_entry->uid =
  422. archive_le32dec(
  423. p + offset + 2);
  424. }
  425. if (datasize >= (2 + uidsize + 3)) {
  426. /* get a gid size. */
  427. gidsize = p[offset+2+uidsize];
  428. if (gidsize == 2)
  429. zip_entry->gid =
  430. archive_le16dec(
  431. p+offset+2+uidsize+1);
  432. else if (gidsize == 4 &&
  433. datasize >= (2 + uidsize + 5))
  434. zip_entry->gid =
  435. archive_le32dec(
  436. p+offset+2+uidsize+1);
  437. }
  438. }
  439. break;
  440. }
  441. default:
  442. break;
  443. }
  444. offset += datasize;
  445. }
  446. #ifdef DEBUG
  447. if (offset != extra_length)
  448. {
  449. fprintf(stderr,
  450. "Extra data field contents do not match reported size!\n");
  451. }
  452. #endif
  453. }
  454. /*
  455. * Assumes file pointer is at beginning of local file header.
  456. */
  457. static int
  458. zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
  459. struct zip *zip)
  460. {
  461. const char *p;
  462. const void *h;
  463. const wchar_t *wp;
  464. const char *cp;
  465. size_t len, filename_length, extra_length;
  466. struct archive_string_conv *sconv;
  467. struct zip_entry *zip_entry = zip->entry;
  468. struct zip_entry zip_entry_central_dir;
  469. int ret = ARCHIVE_OK;
  470. char version;
  471. /* Save a copy of the original for consistency checks. */
  472. zip_entry_central_dir = *zip_entry;
  473. zip->decompress_init = 0;
  474. zip->end_of_entry = 0;
  475. zip->entry_uncompressed_bytes_read = 0;
  476. zip->entry_compressed_bytes_read = 0;
  477. zip->entry_crc32 = zip->crc32func(0, NULL, 0);
  478. /* Setup default conversion. */
  479. if (zip->sconv == NULL && !zip->init_default_conversion) {
  480. zip->sconv_default =
  481. archive_string_default_conversion_for_read(&(a->archive));
  482. zip->init_default_conversion = 1;
  483. }
  484. if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) {
  485. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  486. "Truncated ZIP file header");
  487. return (ARCHIVE_FATAL);
  488. }
  489. if (memcmp(p, "PK\003\004", 4) != 0) {
  490. archive_set_error(&a->archive, -1, "Damaged Zip archive");
  491. return ARCHIVE_FATAL;
  492. }
  493. version = p[4];
  494. zip_entry->system = p[5];
  495. zip_entry->zip_flags = archive_le16dec(p + 6);
  496. if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
  497. zip->has_encrypted_entries = 1;
  498. archive_entry_set_is_data_encrypted(entry, 1);
  499. if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED &&
  500. zip_entry->zip_flags & ZIP_ENCRYPTED &&
  501. zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) {
  502. archive_entry_set_is_metadata_encrypted(entry, 1);
  503. return ARCHIVE_FATAL;
  504. }
  505. }
  506. zip_entry->compression = (char)archive_le16dec(p + 8);
  507. zip_entry->mtime = zip_time(p + 10);
  508. zip_entry->crc32 = archive_le32dec(p + 14);
  509. zip_entry->compressed_size = archive_le32dec(p + 18);
  510. zip_entry->uncompressed_size = archive_le32dec(p + 22);
  511. filename_length = archive_le16dec(p + 26);
  512. extra_length = archive_le16dec(p + 28);
  513. __archive_read_consume(a, 30);
  514. /* Read the filename. */
  515. if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
  516. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  517. "Truncated ZIP file header");
  518. return (ARCHIVE_FATAL);
  519. }
  520. if (zip_entry->zip_flags & ZIP_UTF8_NAME) {
  521. /* The filename is stored to be UTF-8. */
  522. if (zip->sconv_utf8 == NULL) {
  523. zip->sconv_utf8 =
  524. archive_string_conversion_from_charset(
  525. &a->archive, "UTF-8", 1);
  526. if (zip->sconv_utf8 == NULL)
  527. return (ARCHIVE_FATAL);
  528. }
  529. sconv = zip->sconv_utf8;
  530. } else if (zip->sconv != NULL)
  531. sconv = zip->sconv;
  532. else
  533. sconv = zip->sconv_default;
  534. if (archive_entry_copy_pathname_l(entry,
  535. h, filename_length, sconv) != 0) {
  536. if (errno == ENOMEM) {
  537. archive_set_error(&a->archive, ENOMEM,
  538. "Can't allocate memory for Pathname");
  539. return (ARCHIVE_FATAL);
  540. }
  541. archive_set_error(&a->archive,
  542. ARCHIVE_ERRNO_FILE_FORMAT,
  543. "Pathname cannot be converted "
  544. "from %s to current locale.",
  545. archive_string_conversion_charset_name(sconv));
  546. ret = ARCHIVE_WARN;
  547. }
  548. __archive_read_consume(a, filename_length);
  549. /* Work around a bug in Info-Zip: When reading from a pipe, it
  550. * stats the pipe instead of synthesizing a file entry. */
  551. if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
  552. zip_entry->mode &= ~ AE_IFMT;
  553. zip_entry->mode |= AE_IFREG;
  554. }
  555. if ((zip_entry->mode & AE_IFMT) == 0) {
  556. /* Especially in streaming mode, we can end up
  557. here without having seen proper mode information.
  558. Guess from the filename. */
  559. wp = archive_entry_pathname_w(entry);
  560. if (wp != NULL) {
  561. len = wcslen(wp);
  562. if (len > 0 && wp[len - 1] == L'/')
  563. zip_entry->mode |= AE_IFDIR;
  564. else
  565. zip_entry->mode |= AE_IFREG;
  566. } else {
  567. cp = archive_entry_pathname(entry);
  568. len = (cp != NULL)?strlen(cp):0;
  569. if (len > 0 && cp[len - 1] == '/')
  570. zip_entry->mode |= AE_IFDIR;
  571. else
  572. zip_entry->mode |= AE_IFREG;
  573. }
  574. if (zip_entry->mode == AE_IFDIR) {
  575. zip_entry->mode |= 0775;
  576. } else if (zip_entry->mode == AE_IFREG) {
  577. zip_entry->mode |= 0664;
  578. }
  579. }
  580. /* Read the extra data. */
  581. if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
  582. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  583. "Truncated ZIP file header");
  584. return (ARCHIVE_FATAL);
  585. }
  586. process_extra(h, extra_length, zip_entry);
  587. __archive_read_consume(a, extra_length);
  588. if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) {
  589. /* If this came from the central dir, it's size info
  590. * is definitive, so ignore the length-at-end flag. */
  591. zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END;
  592. /* If local header is missing a value, use the one from
  593. the central directory. If both have it, warn about
  594. mismatches. */
  595. if (zip_entry->crc32 == 0) {
  596. zip_entry->crc32 = zip_entry_central_dir.crc32;
  597. } else if (!zip->ignore_crc32
  598. && zip_entry->crc32 != zip_entry_central_dir.crc32) {
  599. archive_set_error(&a->archive,
  600. ARCHIVE_ERRNO_FILE_FORMAT,
  601. "Inconsistent CRC32 values");
  602. ret = ARCHIVE_WARN;
  603. }
  604. if (zip_entry->compressed_size == 0) {
  605. zip_entry->compressed_size
  606. = zip_entry_central_dir.compressed_size;
  607. } else if (zip_entry->compressed_size
  608. != zip_entry_central_dir.compressed_size) {
  609. archive_set_error(&a->archive,
  610. ARCHIVE_ERRNO_FILE_FORMAT,
  611. "Inconsistent compressed size: "
  612. "%jd in central directory, %jd in local header",
  613. (intmax_t)zip_entry_central_dir.compressed_size,
  614. (intmax_t)zip_entry->compressed_size);
  615. ret = ARCHIVE_WARN;
  616. }
  617. if (zip_entry->uncompressed_size == 0) {
  618. zip_entry->uncompressed_size
  619. = zip_entry_central_dir.uncompressed_size;
  620. } else if (zip_entry->uncompressed_size
  621. != zip_entry_central_dir.uncompressed_size) {
  622. archive_set_error(&a->archive,
  623. ARCHIVE_ERRNO_FILE_FORMAT,
  624. "Inconsistent uncompressed size: "
  625. "%jd in central directory, %jd in local header",
  626. (intmax_t)zip_entry_central_dir.uncompressed_size,
  627. (intmax_t)zip_entry->uncompressed_size);
  628. ret = ARCHIVE_WARN;
  629. }
  630. }
  631. /* Populate some additional entry fields: */
  632. archive_entry_set_mode(entry, zip_entry->mode);
  633. archive_entry_set_uid(entry, zip_entry->uid);
  634. archive_entry_set_gid(entry, zip_entry->gid);
  635. archive_entry_set_mtime(entry, zip_entry->mtime, 0);
  636. archive_entry_set_ctime(entry, zip_entry->ctime, 0);
  637. archive_entry_set_atime(entry, zip_entry->atime, 0);
  638. if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
  639. size_t linkname_length = zip_entry->compressed_size;
  640. archive_entry_set_size(entry, 0);
  641. p = __archive_read_ahead(a, linkname_length, NULL);
  642. if (p == NULL) {
  643. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  644. "Truncated Zip file");
  645. return ARCHIVE_FATAL;
  646. }
  647. if (__archive_read_consume(a, linkname_length) < 0) {
  648. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  649. "Read error skipping symlink target name");
  650. return ARCHIVE_FATAL;
  651. }
  652. sconv = zip->sconv;
  653. if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME))
  654. sconv = zip->sconv_utf8;
  655. if (sconv == NULL)
  656. sconv = zip->sconv_default;
  657. if (archive_entry_copy_symlink_l(entry, p, linkname_length,
  658. sconv) != 0) {
  659. if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
  660. (zip->entry->zip_flags & ZIP_UTF8_NAME))
  661. archive_entry_copy_symlink_l(entry, p,
  662. linkname_length, NULL);
  663. if (errno == ENOMEM) {
  664. archive_set_error(&a->archive, ENOMEM,
  665. "Can't allocate memory for Symlink");
  666. return (ARCHIVE_FATAL);
  667. }
  668. /*
  669. * Since there is no character-set regulation for
  670. * symlink name, do not report the conversion error
  671. * in an automatic conversion.
  672. */
  673. if (sconv != zip->sconv_utf8 ||
  674. (zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) {
  675. archive_set_error(&a->archive,
  676. ARCHIVE_ERRNO_FILE_FORMAT,
  677. "Symlink cannot be converted "
  678. "from %s to current locale.",
  679. archive_string_conversion_charset_name(
  680. sconv));
  681. ret = ARCHIVE_WARN;
  682. }
  683. }
  684. zip_entry->uncompressed_size = zip_entry->compressed_size = 0;
  685. } else if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  686. || zip_entry->uncompressed_size > 0) {
  687. /* Set the size only if it's meaningful. */
  688. archive_entry_set_size(entry, zip_entry->uncompressed_size);
  689. }
  690. zip->entry_bytes_remaining = zip_entry->compressed_size;
  691. /* If there's no body, force read_data() to return EOF immediately. */
  692. if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
  693. && zip->entry_bytes_remaining < 1)
  694. zip->end_of_entry = 1;
  695. /* Set up a more descriptive format name. */
  696. snprintf(zip->format_name, sizeof(zip->format_name), "ZIP %d.%d (%s)",
  697. version / 10, version % 10,
  698. compression_name(zip->entry->compression));
  699. a->archive.archive_format_name = zip->format_name;
  700. return (ret);
  701. }
  702. /*
  703. * Read "uncompressed" data. There are three cases:
  704. * 1) We know the size of the data. This is always true for the
  705. * seeking reader (we've examined the Central Directory already).
  706. * 2) ZIP_LENGTH_AT_END was set, but only the CRC was deferred.
  707. * Info-ZIP seems to do this; we know the size but have to grab
  708. * the CRC from the data descriptor afterwards.
  709. * 3) We're streaming and ZIP_LENGTH_AT_END was specified and
  710. * we have no size information. In this case, we can do pretty
  711. * well by watching for the data descriptor record. The data
  712. * descriptor is 16 bytes and includes a computed CRC that should
  713. * provide a strong check.
  714. *
  715. * TODO: Technically, the PK\007\010 signature is optional.
  716. * In the original spec, the data descriptor contained CRC
  717. * and size fields but had no leading signature. In practice,
  718. * newer writers seem to provide the signature pretty consistently.
  719. *
  720. * For uncompressed data, the PK\007\010 marker seems essential
  721. * to be sure we've actually seen the end of the entry.
  722. *
  723. * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
  724. * zip->end_of_entry if it consumes all of the data.
  725. */
  726. static int
  727. zip_read_data_none(struct archive_read *a, const void **_buff,
  728. size_t *size, int64_t *offset)
  729. {
  730. struct zip *zip;
  731. const char *buff;
  732. ssize_t bytes_avail;
  733. (void)offset; /* UNUSED */
  734. zip = (struct zip *)(a->format->data);
  735. if (zip->entry->zip_flags & ZIP_LENGTH_AT_END) {
  736. const char *p;
  737. /* Grab at least 24 bytes. */
  738. buff = __archive_read_ahead(a, 24, &bytes_avail);
  739. if (bytes_avail < 24) {
  740. /* Zip archives have end-of-archive markers
  741. that are longer than this, so a failure to get at
  742. least 24 bytes really does indicate a truncated
  743. file. */
  744. archive_set_error(&a->archive,
  745. ARCHIVE_ERRNO_FILE_FORMAT,
  746. "Truncated ZIP file data");
  747. return (ARCHIVE_FATAL);
  748. }
  749. /* Check for a complete PK\007\010 signature, followed
  750. * by the correct 4-byte CRC. */
  751. p = buff;
  752. if (p[0] == 'P' && p[1] == 'K'
  753. && p[2] == '\007' && p[3] == '\010'
  754. && (archive_le32dec(p + 4) == zip->entry_crc32
  755. || zip->ignore_crc32)) {
  756. if (zip->entry->flags & LA_USED_ZIP64) {
  757. zip->entry->crc32 = archive_le32dec(p + 4);
  758. zip->entry->compressed_size = archive_le64dec(p + 8);
  759. zip->entry->uncompressed_size = archive_le64dec(p + 16);
  760. zip->unconsumed = 24;
  761. } else {
  762. zip->entry->crc32 = archive_le32dec(p + 4);
  763. zip->entry->compressed_size = archive_le32dec(p + 8);
  764. zip->entry->uncompressed_size = archive_le32dec(p + 12);
  765. zip->unconsumed = 16;
  766. }
  767. zip->end_of_entry = 1;
  768. return (ARCHIVE_OK);
  769. }
  770. /* If not at EOF, ensure we consume at least one byte. */
  771. ++p;
  772. /* Scan forward until we see where a PK\007\010 signature
  773. * might be. */
  774. /* Return bytes up until that point. On the next call,
  775. * the code above will verify the data descriptor. */
  776. while (p < buff + bytes_avail - 4) {
  777. if (p[3] == 'P') { p += 3; }
  778. else if (p[3] == 'K') { p += 2; }
  779. else if (p[3] == '\007') { p += 1; }
  780. else if (p[3] == '\010' && p[2] == '\007'
  781. && p[1] == 'K' && p[0] == 'P') {
  782. break;
  783. } else { p += 4; }
  784. }
  785. bytes_avail = p - buff;
  786. } else {
  787. if (zip->entry_bytes_remaining == 0) {
  788. zip->end_of_entry = 1;
  789. return (ARCHIVE_OK);
  790. }
  791. /* Grab a bunch of bytes. */
  792. buff = __archive_read_ahead(a, 1, &bytes_avail);
  793. if (bytes_avail <= 0) {
  794. archive_set_error(&a->archive,
  795. ARCHIVE_ERRNO_FILE_FORMAT,
  796. "Truncated ZIP file data");
  797. return (ARCHIVE_FATAL);
  798. }
  799. if (bytes_avail > zip->entry_bytes_remaining)
  800. bytes_avail = (ssize_t)zip->entry_bytes_remaining;
  801. }
  802. *size = bytes_avail;
  803. zip->entry_bytes_remaining -= bytes_avail;
  804. zip->entry_uncompressed_bytes_read += bytes_avail;
  805. zip->entry_compressed_bytes_read += bytes_avail;
  806. zip->unconsumed += bytes_avail;
  807. *_buff = buff;
  808. return (ARCHIVE_OK);
  809. }
  810. #ifdef HAVE_ZLIB_H
  811. static int
  812. zip_deflate_init(struct archive_read *a, struct zip *zip)
  813. {
  814. int r;
  815. /* If we haven't yet read any data, initialize the decompressor. */
  816. if (!zip->decompress_init) {
  817. if (zip->stream_valid)
  818. r = inflateReset(&zip->stream);
  819. else
  820. r = inflateInit2(&zip->stream,
  821. -15 /* Don't check for zlib header */);
  822. if (r != Z_OK) {
  823. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  824. "Can't initialize ZIP decompression.");
  825. return (ARCHIVE_FATAL);
  826. }
  827. /* Stream structure has been set up. */
  828. zip->stream_valid = 1;
  829. /* We've initialized decompression for this stream. */
  830. zip->decompress_init = 1;
  831. }
  832. return (ARCHIVE_OK);
  833. }
  834. static int
  835. zip_read_data_deflate(struct archive_read *a, const void **buff,
  836. size_t *size, int64_t *offset)
  837. {
  838. struct zip *zip;
  839. ssize_t bytes_avail;
  840. const void *compressed_buff;
  841. int r;
  842. (void)offset; /* UNUSED */
  843. zip = (struct zip *)(a->format->data);
  844. /* If the buffer hasn't been allocated, allocate it now. */
  845. if (zip->uncompressed_buffer == NULL) {
  846. zip->uncompressed_buffer_size = 256 * 1024;
  847. zip->uncompressed_buffer
  848. = (unsigned char *)malloc(zip->uncompressed_buffer_size);
  849. if (zip->uncompressed_buffer == NULL) {
  850. archive_set_error(&a->archive, ENOMEM,
  851. "No memory for ZIP decompression");
  852. return (ARCHIVE_FATAL);
  853. }
  854. }
  855. r = zip_deflate_init(a, zip);
  856. if (r != ARCHIVE_OK)
  857. return (r);
  858. /*
  859. * Note: '1' here is a performance optimization.
  860. * Recall that the decompression layer returns a count of
  861. * available bytes; asking for more than that forces the
  862. * decompressor to combine reads by copying data.
  863. */
  864. compressed_buff = __archive_read_ahead(a, 1, &bytes_avail);
  865. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  866. && bytes_avail > zip->entry_bytes_remaining) {
  867. bytes_avail = (ssize_t)zip->entry_bytes_remaining;
  868. }
  869. if (bytes_avail <= 0) {
  870. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  871. "Truncated ZIP file body");
  872. return (ARCHIVE_FATAL);
  873. }
  874. /*
  875. * A bug in zlib.h: stream.next_in should be marked 'const'
  876. * but isn't (the library never alters data through the
  877. * next_in pointer, only reads it). The result: this ugly
  878. * cast to remove 'const'.
  879. */
  880. zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
  881. zip->stream.avail_in = (uInt)bytes_avail;
  882. zip->stream.total_in = 0;
  883. zip->stream.next_out = zip->uncompressed_buffer;
  884. zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size;
  885. zip->stream.total_out = 0;
  886. r = inflate(&zip->stream, 0);
  887. switch (r) {
  888. case Z_OK:
  889. break;
  890. case Z_STREAM_END:
  891. zip->end_of_entry = 1;
  892. break;
  893. case Z_MEM_ERROR:
  894. archive_set_error(&a->archive, ENOMEM,
  895. "Out of memory for ZIP decompression");
  896. return (ARCHIVE_FATAL);
  897. default:
  898. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  899. "ZIP decompression failed (%d)", r);
  900. return (ARCHIVE_FATAL);
  901. }
  902. /* Consume as much as the compressor actually used. */
  903. bytes_avail = zip->stream.total_in;
  904. __archive_read_consume(a, bytes_avail);
  905. zip->entry_bytes_remaining -= bytes_avail;
  906. zip->entry_compressed_bytes_read += bytes_avail;
  907. *size = zip->stream.total_out;
  908. zip->entry_uncompressed_bytes_read += zip->stream.total_out;
  909. *buff = zip->uncompressed_buffer;
  910. if (zip->end_of_entry && (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
  911. const char *p;
  912. if (NULL == (p = __archive_read_ahead(a, 24, NULL))) {
  913. archive_set_error(&a->archive,
  914. ARCHIVE_ERRNO_FILE_FORMAT,
  915. "Truncated ZIP end-of-file record");
  916. return (ARCHIVE_FATAL);
  917. }
  918. /* Consume the optional PK\007\010 marker. */
  919. if (p[0] == 'P' && p[1] == 'K' &&
  920. p[2] == '\007' && p[3] == '\010') {
  921. p += 4;
  922. zip->unconsumed = 4;
  923. }
  924. if (zip->entry->flags & LA_USED_ZIP64) {
  925. zip->entry->crc32 = archive_le32dec(p);
  926. zip->entry->compressed_size = archive_le64dec(p + 4);
  927. zip->entry->uncompressed_size = archive_le64dec(p + 12);
  928. zip->unconsumed += 20;
  929. } else {
  930. zip->entry->crc32 = archive_le32dec(p);
  931. zip->entry->compressed_size = archive_le32dec(p + 4);
  932. zip->entry->uncompressed_size = archive_le32dec(p + 8);
  933. zip->unconsumed += 12;
  934. }
  935. }
  936. return (ARCHIVE_OK);
  937. }
  938. #endif
  939. static int
  940. archive_read_format_zip_read_data(struct archive_read *a,
  941. const void **buff, size_t *size, int64_t *offset)
  942. {
  943. int r;
  944. struct zip *zip = (struct zip *)(a->format->data);
  945. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  946. zip->has_encrypted_entries = 0;
  947. }
  948. *offset = zip->entry_uncompressed_bytes_read;
  949. *size = 0;
  950. *buff = NULL;
  951. /* If we hit end-of-entry last time, return ARCHIVE_EOF. */
  952. if (zip->end_of_entry)
  953. return (ARCHIVE_EOF);
  954. /* Return EOF immediately if this is a non-regular file. */
  955. if (AE_IFREG != (zip->entry->mode & AE_IFMT))
  956. return (ARCHIVE_EOF);
  957. if (zip->entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
  958. zip->has_encrypted_entries = 1;
  959. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  960. "Encrypted file is unsupported");
  961. return (ARCHIVE_FAILED);
  962. }
  963. __archive_read_consume(a, zip->unconsumed);
  964. zip->unconsumed = 0;
  965. switch(zip->entry->compression) {
  966. case 0: /* No compression. */
  967. r = zip_read_data_none(a, buff, size, offset);
  968. break;
  969. #ifdef HAVE_ZLIB_H
  970. case 8: /* Deflate compression. */
  971. r = zip_read_data_deflate(a, buff, size, offset);
  972. break;
  973. #endif
  974. default: /* Unsupported compression. */
  975. /* Return a warning. */
  976. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  977. "Unsupported ZIP compression method (%s)",
  978. compression_name(zip->entry->compression));
  979. /* We can't decompress this entry, but we will
  980. * be able to skip() it and try the next entry. */
  981. return (ARCHIVE_FAILED);
  982. break;
  983. }
  984. if (r != ARCHIVE_OK)
  985. return (r);
  986. /* Update checksum */
  987. if (*size)
  988. zip->entry_crc32 = zip->crc32func(zip->entry_crc32, *buff,
  989. (unsigned)*size);
  990. /* If we hit the end, swallow any end-of-data marker. */
  991. if (zip->end_of_entry) {
  992. /* Check file size, CRC against these values. */
  993. if (zip->entry->compressed_size !=
  994. zip->entry_compressed_bytes_read) {
  995. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  996. "ZIP compressed data is wrong size "
  997. "(read %jd, expected %jd)",
  998. (intmax_t)zip->entry_compressed_bytes_read,
  999. (intmax_t)zip->entry->compressed_size);
  1000. return (ARCHIVE_WARN);
  1001. }
  1002. /* Size field only stores the lower 32 bits of the actual
  1003. * size. */
  1004. if ((zip->entry->uncompressed_size & UINT32_MAX)
  1005. != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
  1006. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1007. "ZIP uncompressed data is wrong size "
  1008. "(read %jd, expected %jd)\n",
  1009. (intmax_t)zip->entry_uncompressed_bytes_read,
  1010. (intmax_t)zip->entry->uncompressed_size);
  1011. return (ARCHIVE_WARN);
  1012. }
  1013. /* Check computed CRC against header */
  1014. if (zip->entry->crc32 != zip->entry_crc32
  1015. && !zip->ignore_crc32) {
  1016. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1017. "ZIP bad CRC: 0x%lx should be 0x%lx",
  1018. (unsigned long)zip->entry_crc32,
  1019. (unsigned long)zip->entry->crc32);
  1020. return (ARCHIVE_WARN);
  1021. }
  1022. }
  1023. return (ARCHIVE_OK);
  1024. }
  1025. static int
  1026. archive_read_format_zip_cleanup(struct archive_read *a)
  1027. {
  1028. struct zip *zip;
  1029. struct zip_entry *zip_entry, *next_zip_entry;
  1030. zip = (struct zip *)(a->format->data);
  1031. #ifdef HAVE_ZLIB_H
  1032. if (zip->stream_valid)
  1033. inflateEnd(&zip->stream);
  1034. free(zip->uncompressed_buffer);
  1035. #endif
  1036. if (zip->zip_entries) {
  1037. zip_entry = zip->zip_entries;
  1038. while (zip_entry != NULL) {
  1039. next_zip_entry = zip_entry->next;
  1040. archive_string_free(&zip_entry->rsrcname);
  1041. free(zip_entry);
  1042. zip_entry = next_zip_entry;
  1043. }
  1044. }
  1045. free(zip);
  1046. (a->format->data) = NULL;
  1047. return (ARCHIVE_OK);
  1048. }
  1049. static int
  1050. archive_read_format_zip_has_encrypted_entries(struct archive_read *_a)
  1051. {
  1052. if (_a && _a->format) {
  1053. struct zip * zip = (struct zip *)_a->format->data;
  1054. if (zip) {
  1055. return zip->has_encrypted_entries;
  1056. }
  1057. }
  1058. return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  1059. }
  1060. static int
  1061. archive_read_format_zip_options(struct archive_read *a,
  1062. const char *key, const char *val)
  1063. {
  1064. struct zip *zip;
  1065. int ret = ARCHIVE_FAILED;
  1066. zip = (struct zip *)(a->format->data);
  1067. if (strcmp(key, "compat-2x") == 0) {
  1068. /* Handle filenames as libarchive 2.x */
  1069. zip->init_default_conversion = (val != NULL) ? 1 : 0;
  1070. return (ARCHIVE_OK);
  1071. } else if (strcmp(key, "hdrcharset") == 0) {
  1072. if (val == NULL || val[0] == 0)
  1073. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1074. "zip: hdrcharset option needs a character-set name"
  1075. );
  1076. else {
  1077. zip->sconv = archive_string_conversion_from_charset(
  1078. &a->archive, val, 0);
  1079. if (zip->sconv != NULL) {
  1080. if (strcmp(val, "UTF-8") == 0)
  1081. zip->sconv_utf8 = zip->sconv;
  1082. ret = ARCHIVE_OK;
  1083. } else
  1084. ret = ARCHIVE_FATAL;
  1085. }
  1086. return (ret);
  1087. } else if (strcmp(key, "ignorecrc32") == 0) {
  1088. /* Mostly useful for testing. */
  1089. if (val == NULL || val[0] == 0) {
  1090. zip->crc32func = real_crc32;
  1091. zip->ignore_crc32 = 0;
  1092. } else {
  1093. zip->crc32func = fake_crc32;
  1094. zip->ignore_crc32 = 1;
  1095. }
  1096. return (ARCHIVE_OK);
  1097. } else if (strcmp(key, "mac-ext") == 0) {
  1098. zip->process_mac_extensions = (val != NULL && val[0] != 0);
  1099. return (ARCHIVE_OK);
  1100. }
  1101. /* Note: The "warn" return is just to inform the options
  1102. * supervisor that we didn't handle it. It will generate
  1103. * a suitable error if no one used this option. */
  1104. return (ARCHIVE_WARN);
  1105. }
  1106. int
  1107. archive_read_support_format_zip(struct archive *a)
  1108. {
  1109. int r;
  1110. r = archive_read_support_format_zip_streamable(a);
  1111. if (r != ARCHIVE_OK)
  1112. return r;
  1113. return (archive_read_support_format_zip_seekable(a));
  1114. }
  1115. /* ------------------------------------------------------------------------ */
  1116. /*
  1117. * Streaming-mode support
  1118. */
  1119. static int
  1120. archive_read_support_format_zip_capabilities_streamable(struct archive_read * a)
  1121. {
  1122. (void)a; /* UNUSED */
  1123. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  1124. }
  1125. static int
  1126. archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid)
  1127. {
  1128. const char *p;
  1129. (void)best_bid; /* UNUSED */
  1130. if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
  1131. return (-1);
  1132. /*
  1133. * Bid of 29 here comes from:
  1134. * + 16 bits for "PK",
  1135. * + next 16-bit field has 6 options so contributes
  1136. * about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits
  1137. *
  1138. * So we've effectively verified ~29 total bits of check data.
  1139. */
  1140. if (p[0] == 'P' && p[1] == 'K') {
  1141. if ((p[2] == '\001' && p[3] == '\002')
  1142. || (p[2] == '\003' && p[3] == '\004')
  1143. || (p[2] == '\005' && p[3] == '\006')
  1144. || (p[2] == '\006' && p[3] == '\006')
  1145. || (p[2] == '\007' && p[3] == '\010')
  1146. || (p[2] == '0' && p[3] == '0'))
  1147. return (29);
  1148. }
  1149. /* TODO: It's worth looking ahead a little bit for a valid
  1150. * PK signature. In particular, that would make it possible
  1151. * to read some UUEncoded SFX files or SFX files coming from
  1152. * a network socket. */
  1153. return (0);
  1154. }
  1155. static int
  1156. archive_read_format_zip_streamable_read_header(struct archive_read *a,
  1157. struct archive_entry *entry)
  1158. {
  1159. struct zip *zip;
  1160. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  1161. if (a->archive.archive_format_name == NULL)
  1162. a->archive.archive_format_name = "ZIP";
  1163. zip = (struct zip *)(a->format->data);
  1164. /*
  1165. * It should be sufficient to call archive_read_next_header() for
  1166. * a reader to determine if an entry is encrypted or not. If the
  1167. * encryption of an entry is only detectable when calling
  1168. * archive_read_data(), so be it. We'll do the same check there
  1169. * as well.
  1170. */
  1171. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  1172. zip->has_encrypted_entries = 0;
  1173. }
  1174. /* Make sure we have a zip_entry structure to use. */
  1175. if (zip->zip_entries == NULL) {
  1176. zip->zip_entries = malloc(sizeof(struct zip_entry));
  1177. if (zip->zip_entries == NULL) {
  1178. archive_set_error(&a->archive, ENOMEM,
  1179. "Out of memory");
  1180. return ARCHIVE_FATAL;
  1181. }
  1182. }
  1183. zip->entry = zip->zip_entries;
  1184. memset(zip->entry, 0, sizeof(struct zip_entry));
  1185. /* Search ahead for the next local file header. */
  1186. __archive_read_consume(a, zip->unconsumed);
  1187. zip->unconsumed = 0;
  1188. for (;;) {
  1189. int64_t skipped = 0;
  1190. const char *p, *end;
  1191. ssize_t bytes;
  1192. p = __archive_read_ahead(a, 4, &bytes);
  1193. if (p == NULL)
  1194. return (ARCHIVE_FATAL);
  1195. end = p + bytes;
  1196. while (p + 4 <= end) {
  1197. if (p[0] == 'P' && p[1] == 'K') {
  1198. if (p[2] == '\003' && p[3] == '\004') {
  1199. /* Regular file entry. */
  1200. __archive_read_consume(a, skipped);
  1201. return zip_read_local_file_header(a,
  1202. entry, zip);
  1203. }
  1204. /*
  1205. * TODO: We cannot restore permissions
  1206. * based only on the local file headers.
  1207. * Consider scanning the central
  1208. * directory and returning additional
  1209. * entries for at least directories.
  1210. * This would allow us to properly set
  1211. * directory permissions.
  1212. *
  1213. * This won't help us fix symlinks
  1214. * and may not help with regular file
  1215. * permissions, either. <sigh>
  1216. */
  1217. if (p[2] == '\001' && p[3] == '\002') {
  1218. return (ARCHIVE_EOF);
  1219. }
  1220. /* End of central directory? Must be an
  1221. * empty archive. */
  1222. if ((p[2] == '\005' && p[3] == '\006')
  1223. || (p[2] == '\006' && p[3] == '\006'))
  1224. return (ARCHIVE_EOF);
  1225. }
  1226. ++p;
  1227. ++skipped;
  1228. }
  1229. __archive_read_consume(a, skipped);
  1230. }
  1231. }
  1232. static int
  1233. archive_read_format_zip_read_data_skip_streamable(struct archive_read *a)
  1234. {
  1235. struct zip *zip;
  1236. int64_t bytes_skipped;
  1237. zip = (struct zip *)(a->format->data);
  1238. bytes_skipped = __archive_read_consume(a, zip->unconsumed);
  1239. zip->unconsumed = 0;
  1240. if (bytes_skipped < 0)
  1241. return (ARCHIVE_FATAL);
  1242. /* If we've already read to end of data, we're done. */
  1243. if (zip->end_of_entry)
  1244. return (ARCHIVE_OK);
  1245. /* So we know we're streaming... */
  1246. if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
  1247. || zip->entry->compressed_size > 0) {
  1248. /* We know the compressed length, so we can just skip. */
  1249. bytes_skipped = __archive_read_consume(a, zip->entry_bytes_remaining);
  1250. if (bytes_skipped < 0)
  1251. return (ARCHIVE_FATAL);
  1252. return (ARCHIVE_OK);
  1253. }
  1254. /* We're streaming and we don't know the length. */
  1255. /* If the body is compressed and we know the format, we can
  1256. * find an exact end-of-entry by decompressing it. */
  1257. switch (zip->entry->compression) {
  1258. #ifdef HAVE_ZLIB_H
  1259. case 8: /* Deflate compression. */
  1260. while (!zip->end_of_entry) {
  1261. int64_t offset = 0;
  1262. const void *buff = NULL;
  1263. size_t size = 0;
  1264. int r;
  1265. r = zip_read_data_deflate(a, &buff, &size, &offset);
  1266. if (r != ARCHIVE_OK)
  1267. return (r);
  1268. }
  1269. return ARCHIVE_OK;
  1270. #endif
  1271. default: /* Uncompressed or unknown. */
  1272. /* Scan for a PK\007\010 signature. */
  1273. for (;;) {
  1274. const char *p, *buff;
  1275. ssize_t bytes_avail;
  1276. buff = __archive_read_ahead(a, 16, &bytes_avail);
  1277. if (bytes_avail < 16) {
  1278. archive_set_error(&a->archive,
  1279. ARCHIVE_ERRNO_FILE_FORMAT,
  1280. "Truncated ZIP file data");
  1281. return (ARCHIVE_FATAL);
  1282. }
  1283. p = buff;
  1284. while (p <= buff + bytes_avail - 16) {
  1285. if (p[3] == 'P') { p += 3; }
  1286. else if (p[3] == 'K') { p += 2; }
  1287. else if (p[3] == '\007') { p += 1; }
  1288. else if (p[3] == '\010' && p[2] == '\007'
  1289. && p[1] == 'K' && p[0] == 'P') {
  1290. if (zip->entry->flags & LA_USED_ZIP64)
  1291. __archive_read_consume(a, p - buff + 24);
  1292. else
  1293. __archive_read_consume(a, p - buff + 16);
  1294. return ARCHIVE_OK;
  1295. } else { p += 4; }
  1296. }
  1297. __archive_read_consume(a, p - buff);
  1298. }
  1299. }
  1300. }
  1301. int
  1302. archive_read_support_format_zip_streamable(struct archive *_a)
  1303. {
  1304. struct archive_read *a = (struct archive_read *)_a;
  1305. struct zip *zip;
  1306. int r;
  1307. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  1308. ARCHIVE_STATE_NEW, "archive_read_support_format_zip");
  1309. zip = (struct zip *)malloc(sizeof(*zip));
  1310. if (zip == NULL) {
  1311. archive_set_error(&a->archive, ENOMEM,
  1312. "Can't allocate zip data");
  1313. return (ARCHIVE_FATAL);
  1314. }
  1315. memset(zip, 0, sizeof(*zip));
  1316. /* Streamable reader doesn't support mac extensions. */
  1317. zip->process_mac_extensions = 0;
  1318. /*
  1319. * Until enough data has been read, we cannot tell about
  1320. * any encrypted entries yet.
  1321. */
  1322. zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  1323. zip->crc32func = real_crc32;
  1324. r = __archive_read_register_format(a,
  1325. zip,
  1326. "zip",
  1327. archive_read_format_zip_streamable_bid,
  1328. archive_read_format_zip_options,
  1329. archive_read_format_zip_streamable_read_header,
  1330. archive_read_format_zip_read_data,
  1331. archive_read_format_zip_read_data_skip_streamable,
  1332. NULL,
  1333. archive_read_format_zip_cleanup,
  1334. archive_read_support_format_zip_capabilities_streamable,
  1335. archive_read_format_zip_has_encrypted_entries);
  1336. if (r != ARCHIVE_OK)
  1337. free(zip);
  1338. return (ARCHIVE_OK);
  1339. }
  1340. /* ------------------------------------------------------------------------ */
  1341. /*
  1342. * Seeking-mode support
  1343. */
  1344. static int
  1345. archive_read_support_format_zip_capabilities_seekable(struct archive_read * a)
  1346. {
  1347. (void)a; /* UNUSED */
  1348. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  1349. }
  1350. /*
  1351. * TODO: This is a performance sink because it forces the read core to
  1352. * drop buffered data from the start of file, which will then have to
  1353. * be re-read again if this bidder loses.
  1354. *
  1355. * We workaround this a little by passing in the best bid so far so
  1356. * that later bidders can do nothing if they know they'll never
  1357. * outbid. But we can certainly do better...
  1358. */
  1359. static int
  1360. read_eocd(struct zip *zip, const char *p, int64_t current_offset)
  1361. {
  1362. /* Sanity-check the EOCD we've found. */
  1363. /* This must be the first volume. */
  1364. if (archive_le16dec(p + 4) != 0)
  1365. return 0;
  1366. /* Central directory must be on this volume. */
  1367. if (archive_le16dec(p + 4) != archive_le16dec(p + 6))
  1368. return 0;
  1369. /* All central directory entries must be on this volume. */
  1370. if (archive_le16dec(p + 10) != archive_le16dec(p + 8))
  1371. return 0;
  1372. /* Central directory can't extend beyond start of EOCD record. */
  1373. if (archive_le32dec(p + 16) + archive_le32dec(p + 12)
  1374. > current_offset)
  1375. return 0;
  1376. /* Save the central directory location for later use. */
  1377. zip->central_directory_offset = archive_le32dec(p + 16);
  1378. /* This is just a tiny bit higher than the maximum
  1379. returned by the streaming Zip bidder. This ensures
  1380. that the more accurate seeking Zip parser wins
  1381. whenever seek is available. */
  1382. return 32;
  1383. }
  1384. static int
  1385. read_zip64_eocd(struct archive_read *a, struct zip *zip, const char *p)
  1386. {
  1387. int64_t eocd64_offset;
  1388. int64_t eocd64_size;
  1389. /* Sanity-check the locator record. */
  1390. /* Central dir must be on first volume. */
  1391. if (archive_le32dec(p + 4) != 0)
  1392. return 0;
  1393. /* Must be only a single volume. */
  1394. if (archive_le32dec(p + 16) != 1)
  1395. return 0;
  1396. /* Find the Zip64 EOCD record. */
  1397. eocd64_offset = archive_le64dec(p + 8);
  1398. if (__archive_read_seek(a, eocd64_offset, SEEK_SET) < 0)
  1399. return 0;
  1400. if ((p = __archive_read_ahead(a, 56, NULL)) == NULL)
  1401. return 0;
  1402. /* Make sure we can read all of it. */
  1403. eocd64_size = archive_le64dec(p + 4) + 12;
  1404. if (eocd64_size < 56 || eocd64_size > 16384)
  1405. return 0;
  1406. if ((p = __archive_read_ahead(a, eocd64_size, NULL)) == NULL)
  1407. return 0;
  1408. /* Sanity-check the EOCD64 */
  1409. if (archive_le32dec(p + 16) != 0) /* Must be disk #0 */
  1410. return 0;
  1411. if (archive_le32dec(p + 20) != 0) /* CD must be on disk #0 */
  1412. return 0;
  1413. /* CD can't be split. */
  1414. if (archive_le64dec(p + 24) != archive_le64dec(p + 32))
  1415. return 0;
  1416. /* Save the central directory offset for later use. */
  1417. zip->central_directory_offset = archive_le64dec(p + 48);
  1418. return 32;
  1419. }
  1420. static int
  1421. archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid)
  1422. {
  1423. struct zip *zip = (struct zip *)a->format->data;
  1424. int64_t file_size, current_offset;
  1425. const char *p;
  1426. int i, tail;
  1427. /* If someone has already bid more than 32, then avoid
  1428. trashing the look-ahead buffers with a seek. */
  1429. if (best_bid > 32)
  1430. return (-1);
  1431. file_size = __archive_read_seek(a, 0, SEEK_END);
  1432. if (file_size <= 0)
  1433. return 0;
  1434. /* Search last 16k of file for end-of-central-directory
  1435. * record (which starts with PK\005\006) or Zip64 locator
  1436. * record (which begins with PK\006\007) */
  1437. tail = zipmin(1024 * 16, file_size);
  1438. current_offset = __archive_read_seek(a, -tail, SEEK_END);
  1439. if (current_offset < 0)
  1440. return 0;
  1441. if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL)
  1442. return 0;
  1443. /* TODO: Rework this to search backwards from the end. We
  1444. * normally expect the EOCD record to be at the very end, so
  1445. * that should be significantly faster. Tricky part: Make
  1446. * sure we still prefer the Zip64 locator if it's present. */
  1447. for (i = 0; i <= tail - 22;) {
  1448. switch (p[i + 3]) {
  1449. case 'P': i += 3; break;
  1450. case 'K': i += 2; break;
  1451. case 005: i += 1; break;
  1452. case 006:
  1453. if (memcmp(p + i, "PK\005\006", 4) == 0) {
  1454. int ret = read_eocd(zip, p + i, current_offset + i);
  1455. if (ret > 0)
  1456. return (ret);
  1457. }
  1458. i += 1; /* Look for PK\006\007 next */
  1459. break;
  1460. case 007:
  1461. if (memcmp(p + i, "PK\006\007", 4) == 0) {
  1462. int ret = read_zip64_eocd(a, zip, p + i);
  1463. if (ret > 0)
  1464. return (ret);
  1465. }
  1466. i += 4;
  1467. break;
  1468. default: i += 4; break;
  1469. }
  1470. }
  1471. return 0;
  1472. }
  1473. /* The red-black trees are only used in seeking mode to manage
  1474. * the in-memory copy of the central directory. */
  1475. static int
  1476. cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2)
  1477. {
  1478. const struct zip_entry *e1 = (const struct zip_entry *)n1;
  1479. const struct zip_entry *e2 = (const struct zip_entry *)n2;
  1480. if (e1->local_header_offset > e2->local_header_offset)
  1481. return -1;
  1482. if (e1->local_header_offset < e2->local_header_offset)
  1483. return 1;
  1484. return 0;
  1485. }
  1486. static int
  1487. cmp_key(const struct archive_rb_node *n, const void *key)
  1488. {
  1489. /* This function won't be called */
  1490. (void)n; /* UNUSED */
  1491. (void)key; /* UNUSED */
  1492. return 1;
  1493. }
  1494. static const struct archive_rb_tree_ops rb_ops = {
  1495. &cmp_node, &cmp_key
  1496. };
  1497. static int
  1498. rsrc_cmp_node(const struct archive_rb_node *n1,
  1499. const struct archive_rb_node *n2)
  1500. {
  1501. const struct zip_entry *e1 = (const struct zip_entry *)n1;
  1502. const struct zip_entry *e2 = (const struct zip_entry *)n2;
  1503. return (strcmp(e2->rsrcname.s, e1->rsrcname.s));
  1504. }
  1505. static int
  1506. rsrc_cmp_key(const struct archive_rb_node *n, const void *key)
  1507. {
  1508. const struct zip_entry *e = (const struct zip_entry *)n;
  1509. return (strcmp((const char *)key, e->rsrcname.s));
  1510. }
  1511. static const struct archive_rb_tree_ops rb_rsrc_ops = {
  1512. &rsrc_cmp_node, &rsrc_cmp_key
  1513. };
  1514. static const char *
  1515. rsrc_basename(const char *name, size_t name_length)
  1516. {
  1517. const char *s, *r;
  1518. r = s = name;
  1519. for (;;) {
  1520. s = memchr(s, '/', name_length - (s - name));
  1521. if (s == NULL)
  1522. break;
  1523. r = ++s;
  1524. }
  1525. return (r);
  1526. }
  1527. static void
  1528. expose_parent_dirs(struct zip *zip, const char *name, size_t name_length)
  1529. {
  1530. struct archive_string str;
  1531. struct zip_entry *dir;
  1532. char *s;
  1533. archive_string_init(&str);
  1534. archive_strncpy(&str, name, name_length);
  1535. for (;;) {
  1536. s = strrchr(str.s, '/');
  1537. if (s == NULL)
  1538. break;
  1539. *s = '\0';
  1540. /* Transfer the parent directory from zip->tree_rsrc RB
  1541. * tree to zip->tree RB tree to expose. */
  1542. dir = (struct zip_entry *)
  1543. __archive_rb_tree_find_node(&zip->tree_rsrc, str.s);
  1544. if (dir == NULL)
  1545. break;
  1546. __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
  1547. archive_string_free(&dir->rsrcname);
  1548. __archive_rb_tree_insert_node(&zip->tree, &dir->node);
  1549. }
  1550. archive_string_free(&str);
  1551. }
  1552. static int
  1553. slurp_central_directory(struct archive_read *a, struct zip *zip)
  1554. {
  1555. ssize_t i;
  1556. unsigned found;
  1557. int64_t correction;
  1558. ssize_t bytes_avail;
  1559. const char *p;
  1560. /*
  1561. * Find the start of the central directory. The end-of-CD
  1562. * record has our starting point, but there are lots of
  1563. * Zip archives which have had other data prepended to the
  1564. * file, which makes the recorded offsets all too small.
  1565. * So we search forward from the specified offset until we
  1566. * find the real start of the central directory. Then we
  1567. * know the correction we need to apply to account for leading
  1568. * padding.
  1569. */
  1570. if (__archive_read_seek(a, zip->central_directory_offset, SEEK_SET) < 0)
  1571. return ARCHIVE_FATAL;
  1572. found = 0;
  1573. while (!found) {
  1574. if ((p = __archive_read_ahead(a, 20, &bytes_avail)) == NULL)
  1575. return ARCHIVE_FATAL;
  1576. for (found = 0, i = 0; !found && i < bytes_avail - 4;) {
  1577. switch (p[i + 3]) {
  1578. case 'P': i += 3; break;
  1579. case 'K': i += 2; break;
  1580. case 001: i += 1; break;
  1581. case 002:
  1582. if (memcmp(p + i, "PK\001\002", 4) == 0) {
  1583. p += i;
  1584. found = 1;
  1585. } else
  1586. i += 4;
  1587. break;
  1588. case 005: i += 1; break;
  1589. case 006:
  1590. if (memcmp(p + i, "PK\005\006", 4) == 0) {
  1591. p += i;
  1592. found = 1;
  1593. } else if (memcmp(p + i, "PK\006\006", 4) == 0) {
  1594. p += i;
  1595. found = 1;
  1596. } else
  1597. i += 1;
  1598. break;
  1599. default: i += 4; break;
  1600. }
  1601. }
  1602. __archive_read_consume(a, i);
  1603. }
  1604. correction = archive_filter_bytes(&a->archive, 0) - zip->central_directory_offset;
  1605. __archive_rb_tree_init(&zip->tree, &rb_ops);
  1606. __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops);
  1607. zip->central_directory_entries_total = 0;
  1608. while (1) {
  1609. struct zip_entry *zip_entry;
  1610. size_t filename_length, extra_length, comment_length;
  1611. uint32_t external_attributes;
  1612. const char *name, *r;
  1613. if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
  1614. return ARCHIVE_FATAL;
  1615. if (memcmp(p, "PK\006\006", 4) == 0
  1616. || memcmp(p, "PK\005\006", 4) == 0) {
  1617. break;
  1618. } else if (memcmp(p, "PK\001\002", 4) != 0) {
  1619. archive_set_error(&a->archive,
  1620. -1, "Invalid central directory signature");
  1621. return ARCHIVE_FATAL;
  1622. }
  1623. if ((p = __archive_read_ahead(a, 46, NULL)) == NULL)
  1624. return ARCHIVE_FATAL;
  1625. zip_entry = calloc(1, sizeof(struct zip_entry));
  1626. zip_entry->next = zip->zip_entries;
  1627. zip_entry->flags |= LA_FROM_CENTRAL_DIRECTORY;
  1628. zip->zip_entries = zip_entry;
  1629. zip->central_directory_entries_total++;
  1630. /* version = p[4]; */
  1631. zip_entry->system = p[5];
  1632. /* version_required = archive_le16dec(p + 6); */
  1633. zip_entry->zip_flags = archive_le16dec(p + 8);
  1634. if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)){
  1635. zip->has_encrypted_entries = 1;
  1636. }
  1637. zip_entry->compression = (char)archive_le16dec(p + 10);
  1638. zip_entry->mtime = zip_time(p + 12);
  1639. zip_entry->crc32 = archive_le32dec(p + 16);
  1640. zip_entry->compressed_size = archive_le32dec(p + 20);
  1641. zip_entry->uncompressed_size = archive_le32dec(p + 24);
  1642. filename_length = archive_le16dec(p + 28);
  1643. extra_length = archive_le16dec(p + 30);
  1644. comment_length = archive_le16dec(p + 32);
  1645. /* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */
  1646. /* internal_attributes = archive_le16dec(p + 36); */ /* text bit */
  1647. external_attributes = archive_le32dec(p + 38);
  1648. zip_entry->local_header_offset =
  1649. archive_le32dec(p + 42) + correction;
  1650. /* If we can't guess the mode, leave it zero here;
  1651. when we read the local file header we might get
  1652. more information. */
  1653. zip_entry->mode = 0;
  1654. if (zip_entry->system == 3) {
  1655. zip_entry->mode = external_attributes >> 16;
  1656. }
  1657. /* We're done with the regular data; get the filename and
  1658. * extra data. */
  1659. __archive_read_consume(a, 46);
  1660. if ((p = __archive_read_ahead(a, filename_length + extra_length, NULL))
  1661. == NULL) {
  1662. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1663. "Truncated ZIP file header");
  1664. return ARCHIVE_FATAL;
  1665. }
  1666. process_extra(p + filename_length, extra_length, zip_entry);
  1667. /*
  1668. * Mac resource fork files are stored under the
  1669. * "__MACOSX/" directory, so we should check if
  1670. * it is.
  1671. */
  1672. if (!zip->process_mac_extensions) {
  1673. /* Treat every entry as a regular entry. */
  1674. __archive_rb_tree_insert_node(&zip->tree,
  1675. &zip_entry->node);
  1676. } else {
  1677. name = p;
  1678. r = rsrc_basename(name, filename_length);
  1679. if (filename_length >= 9 &&
  1680. strncmp("__MACOSX/", name, 9) == 0) {
  1681. /* If this file is not a resource fork nor
  1682. * a directory. We should treat it as a non
  1683. * resource fork file to expose it. */
  1684. if (name[filename_length-1] != '/' &&
  1685. (r - name < 3 || r[0] != '.' || r[1] != '_')) {
  1686. __archive_rb_tree_insert_node(&zip->tree,
  1687. &zip_entry->node);
  1688. /* Expose its parent directories. */
  1689. expose_parent_dirs(zip, name, filename_length);
  1690. } else {
  1691. /* This file is a resource fork file or
  1692. * a directory. */
  1693. archive_strncpy(&(zip_entry->rsrcname), name,
  1694. filename_length);
  1695. __archive_rb_tree_insert_node(&zip->tree_rsrc,
  1696. &zip_entry->node);
  1697. }
  1698. } else {
  1699. /* Generate resource fork name to find its resource
  1700. * file at zip->tree_rsrc. */
  1701. archive_strcpy(&(zip_entry->rsrcname), "__MACOSX/");
  1702. archive_strncat(&(zip_entry->rsrcname), name, r - name);
  1703. archive_strcat(&(zip_entry->rsrcname), "._");
  1704. archive_strncat(&(zip_entry->rsrcname),
  1705. name + (r - name), filename_length - (r - name));
  1706. /* Register an entry to RB tree to sort it by
  1707. * file offset. */
  1708. __archive_rb_tree_insert_node(&zip->tree,
  1709. &zip_entry->node);
  1710. }
  1711. }
  1712. /* Skip the comment too ... */
  1713. __archive_read_consume(a,
  1714. filename_length + extra_length + comment_length);
  1715. }
  1716. return ARCHIVE_OK;
  1717. }
  1718. static ssize_t
  1719. zip_get_local_file_header_size(struct archive_read *a, size_t extra)
  1720. {
  1721. const char *p;
  1722. ssize_t filename_length, extra_length;
  1723. if ((p = __archive_read_ahead(a, extra + 30, NULL)) == NULL) {
  1724. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1725. "Truncated ZIP file header");
  1726. return (ARCHIVE_WARN);
  1727. }
  1728. p += extra;
  1729. if (memcmp(p, "PK\003\004", 4) != 0) {
  1730. archive_set_error(&a->archive, -1, "Damaged Zip archive");
  1731. return ARCHIVE_WARN;
  1732. }
  1733. filename_length = archive_le16dec(p + 26);
  1734. extra_length = archive_le16dec(p + 28);
  1735. return (30 + filename_length + extra_length);
  1736. }
  1737. static int
  1738. zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
  1739. struct zip_entry *rsrc)
  1740. {
  1741. struct zip *zip = (struct zip *)a->format->data;
  1742. unsigned char *metadata, *mp;
  1743. int64_t offset = archive_filter_bytes(&a->archive, 0);
  1744. size_t remaining_bytes, metadata_bytes;
  1745. ssize_t hsize;
  1746. int ret = ARCHIVE_OK, eof;
  1747. switch(rsrc->compression) {
  1748. case 0: /* No compression. */
  1749. #ifdef HAVE_ZLIB_H
  1750. case 8: /* Deflate compression. */
  1751. #endif
  1752. break;
  1753. default: /* Unsupported compression. */
  1754. /* Return a warning. */
  1755. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1756. "Unsupported ZIP compression method (%s)",
  1757. compression_name(rsrc->compression));
  1758. /* We can't decompress this entry, but we will
  1759. * be able to skip() it and try the next entry. */
  1760. return (ARCHIVE_WARN);
  1761. }
  1762. if (rsrc->uncompressed_size > (4 * 1024 * 1024)) {
  1763. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1764. "Mac metadata is too large: %jd > 4M bytes",
  1765. (intmax_t)rsrc->uncompressed_size);
  1766. return (ARCHIVE_WARN);
  1767. }
  1768. metadata = malloc((size_t)rsrc->uncompressed_size);
  1769. if (metadata == NULL) {
  1770. archive_set_error(&a->archive, ENOMEM,
  1771. "Can't allocate memory for Mac metadata");
  1772. return (ARCHIVE_FATAL);
  1773. }
  1774. if (offset < rsrc->local_header_offset)
  1775. __archive_read_consume(a, rsrc->local_header_offset - offset);
  1776. else if (offset != rsrc->local_header_offset) {
  1777. __archive_read_seek(a, rsrc->local_header_offset, SEEK_SET);
  1778. }
  1779. hsize = zip_get_local_file_header_size(a, 0);
  1780. __archive_read_consume(a, hsize);
  1781. remaining_bytes = (size_t)rsrc->compressed_size;
  1782. metadata_bytes = (size_t)rsrc->uncompressed_size;
  1783. mp = metadata;
  1784. eof = 0;
  1785. while (!eof && remaining_bytes) {
  1786. const unsigned char *p;
  1787. ssize_t bytes_avail;
  1788. size_t bytes_used;
  1789. p = __archive_read_ahead(a, 1, &bytes_avail);
  1790. if (p == NULL) {
  1791. archive_set_error(&a->archive,
  1792. ARCHIVE_ERRNO_FILE_FORMAT,
  1793. "Truncated ZIP file header");
  1794. ret = ARCHIVE_WARN;
  1795. goto exit_mac_metadata;
  1796. }
  1797. if ((size_t)bytes_avail > remaining_bytes)
  1798. bytes_avail = remaining_bytes;
  1799. switch(rsrc->compression) {
  1800. case 0: /* No compression. */
  1801. memcpy(mp, p, bytes_avail);
  1802. bytes_used = (size_t)bytes_avail;
  1803. metadata_bytes -= bytes_used;
  1804. mp += bytes_used;
  1805. if (metadata_bytes == 0)
  1806. eof = 1;
  1807. break;
  1808. #ifdef HAVE_ZLIB_H
  1809. case 8: /* Deflate compression. */
  1810. {
  1811. int r;
  1812. ret = zip_deflate_init(a, zip);
  1813. if (ret != ARCHIVE_OK)
  1814. goto exit_mac_metadata;
  1815. zip->stream.next_in =
  1816. (Bytef *)(uintptr_t)(const void *)p;
  1817. zip->stream.avail_in = (uInt)bytes_avail;
  1818. zip->stream.total_in = 0;
  1819. zip->stream.next_out = mp;
  1820. zip->stream.avail_out = (uInt)metadata_bytes;
  1821. zip->stream.total_out = 0;
  1822. r = inflate(&zip->stream, 0);
  1823. switch (r) {
  1824. case Z_OK:
  1825. break;
  1826. case Z_STREAM_END:
  1827. eof = 1;
  1828. break;
  1829. case Z_MEM_ERROR:
  1830. archive_set_error(&a->archive, ENOMEM,
  1831. "Out of memory for ZIP decompression");
  1832. ret = ARCHIVE_FATAL;
  1833. goto exit_mac_metadata;
  1834. default:
  1835. archive_set_error(&a->archive,
  1836. ARCHIVE_ERRNO_MISC,
  1837. "ZIP decompression failed (%d)", r);
  1838. ret = ARCHIVE_FATAL;
  1839. goto exit_mac_metadata;
  1840. }
  1841. bytes_used = zip->stream.total_in;
  1842. metadata_bytes -= zip->stream.total_out;
  1843. mp += zip->stream.total_out;
  1844. break;
  1845. }
  1846. #endif
  1847. default:
  1848. bytes_used = 0;
  1849. break;
  1850. }
  1851. __archive_read_consume(a, bytes_used);
  1852. remaining_bytes -= bytes_used;
  1853. }
  1854. archive_entry_copy_mac_metadata(entry, metadata,
  1855. (size_t)rsrc->uncompressed_size - metadata_bytes);
  1856. exit_mac_metadata:
  1857. __archive_read_seek(a, offset, SEEK_SET);
  1858. zip->decompress_init = 0;
  1859. free(metadata);
  1860. return (ret);
  1861. }
  1862. static int
  1863. archive_read_format_zip_seekable_read_header(struct archive_read *a,
  1864. struct archive_entry *entry)
  1865. {
  1866. struct zip *zip = (struct zip *)a->format->data;
  1867. struct zip_entry *rsrc;
  1868. int64_t offset;
  1869. int r, ret = ARCHIVE_OK;
  1870. /*
  1871. * It should be sufficient to call archive_read_next_header() for
  1872. * a reader to determine if an entry is encrypted or not. If the
  1873. * encryption of an entry is only detectable when calling
  1874. * archive_read_data(), so be it. We'll do the same check there
  1875. * as well.
  1876. */
  1877. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  1878. zip->has_encrypted_entries = 0;
  1879. }
  1880. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  1881. if (a->archive.archive_format_name == NULL)
  1882. a->archive.archive_format_name = "ZIP";
  1883. if (zip->zip_entries == NULL) {
  1884. r = slurp_central_directory(a, zip);
  1885. if (r != ARCHIVE_OK)
  1886. return r;
  1887. /* Get first entry whose local header offset is lower than
  1888. * other entries in the archive file. */
  1889. zip->entry =
  1890. (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
  1891. } else if (zip->entry != NULL) {
  1892. /* Get next entry in local header offset order. */
  1893. zip->entry = (struct zip_entry *)__archive_rb_tree_iterate(
  1894. &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT);
  1895. }
  1896. if (zip->entry == NULL)
  1897. return ARCHIVE_EOF;
  1898. if (zip->entry->rsrcname.s)
  1899. rsrc = (struct zip_entry *)__archive_rb_tree_find_node(
  1900. &zip->tree_rsrc, zip->entry->rsrcname.s);
  1901. else
  1902. rsrc = NULL;
  1903. /* File entries are sorted by the header offset, we should mostly
  1904. * use __archive_read_consume to advance a read point to avoid redundant
  1905. * data reading. */
  1906. offset = archive_filter_bytes(&a->archive, 0);
  1907. if (offset < zip->entry->local_header_offset)
  1908. __archive_read_consume(a,
  1909. zip->entry->local_header_offset - offset);
  1910. else if (offset != zip->entry->local_header_offset) {
  1911. __archive_read_seek(a, zip->entry->local_header_offset, SEEK_SET);
  1912. }
  1913. zip->unconsumed = 0;
  1914. r = zip_read_local_file_header(a, entry, zip);
  1915. if (r != ARCHIVE_OK)
  1916. return r;
  1917. if (rsrc) {
  1918. int ret2 = zip_read_mac_metadata(a, entry, rsrc);
  1919. if (ret2 < ret)
  1920. ret = ret2;
  1921. }
  1922. return (ret);
  1923. }
  1924. /*
  1925. * We're going to seek for the next header anyway, so we don't
  1926. * need to bother doing anything here.
  1927. */
  1928. static int
  1929. archive_read_format_zip_read_data_skip_seekable(struct archive_read *a)
  1930. {
  1931. struct zip *zip;
  1932. zip = (struct zip *)(a->format->data);
  1933. zip->unconsumed = 0;
  1934. return (ARCHIVE_OK);
  1935. }
  1936. int
  1937. archive_read_support_format_zip_seekable(struct archive *_a)
  1938. {
  1939. struct archive_read *a = (struct archive_read *)_a;
  1940. struct zip *zip;
  1941. int r;
  1942. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  1943. ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable");
  1944. zip = (struct zip *)malloc(sizeof(*zip));
  1945. if (zip == NULL) {
  1946. archive_set_error(&a->archive, ENOMEM,
  1947. "Can't allocate zip data");
  1948. return (ARCHIVE_FATAL);
  1949. }
  1950. memset(zip, 0, sizeof(*zip));
  1951. #ifdef HAVE_COPYFILE_H
  1952. /* Set this by default on Mac OS. */
  1953. zip->process_mac_extensions = 1;
  1954. #endif
  1955. /*
  1956. * Until enough data has been read, we cannot tell about
  1957. * any encrypted entries yet.
  1958. */
  1959. zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  1960. zip->crc32func = real_crc32;
  1961. r = __archive_read_register_format(a,
  1962. zip,
  1963. "zip",
  1964. archive_read_format_zip_seekable_bid,
  1965. archive_read_format_zip_options,
  1966. archive_read_format_zip_seekable_read_header,
  1967. archive_read_format_zip_read_data,
  1968. archive_read_format_zip_read_data_skip_seekable,
  1969. NULL,
  1970. archive_read_format_zip_cleanup,
  1971. archive_read_support_format_zip_capabilities_seekable,
  1972. archive_read_format_zip_has_encrypted_entries);
  1973. if (r != ARCHIVE_OK)
  1974. free(zip);
  1975. return (ARCHIVE_OK);
  1976. }