0007-unzip-do-not-use-CDF.extra_len-read-local-file-heade.patch 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. From ee72302ac5e3b0b2217f616ab316d3c89e5a1f4c Mon Sep 17 00:00:00 2001
  2. From: Denys Vlasenko <vda.linux@googlemail.com>
  3. Date: Sun, 8 Jan 2017 14:14:19 +0100
  4. Subject: [PATCH] unzip: do not use CDF.extra_len, read local file header.
  5. Closes 9536
  6. While at it, shorten many field and variable names.
  7. function old new delta
  8. unzip_main 2334 2376 +42
  9. Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
  10. Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
  11. ---
  12. archival/unzip.c | 236 ++++++++++++++++++++++++++------------------------
  13. testsuite/unzip.tests | 4 +-
  14. 2 files changed, 125 insertions(+), 115 deletions(-)
  15. diff --git a/archival/unzip.c b/archival/unzip.c
  16. index 98a71c09d..921493591 100644
  17. --- a/archival/unzip.c
  18. +++ b/archival/unzip.c
  19. @@ -62,8 +62,8 @@
  20. enum {
  21. #if BB_BIG_ENDIAN
  22. ZIP_FILEHEADER_MAGIC = 0x504b0304,
  23. - ZIP_CDF_MAGIC = 0x504b0102, /* central directory's file header */
  24. - ZIP_CDE_MAGIC = 0x504b0506, /* "end of central directory" record */
  25. + ZIP_CDF_MAGIC = 0x504b0102, /* CDF item */
  26. + ZIP_CDE_MAGIC = 0x504b0506, /* End of CDF */
  27. ZIP_DD_MAGIC = 0x504b0708,
  28. #else
  29. ZIP_FILEHEADER_MAGIC = 0x04034b50,
  30. @@ -91,16 +91,16 @@ typedef union {
  31. /* filename follows (not NUL terminated) */
  32. /* extra field follows */
  33. /* data follows */
  34. - } formatted PACKED;
  35. + } fmt PACKED;
  36. } zip_header_t; /* PACKED - gcc 4.2.1 doesn't like it (spews warning) */
  37. -#define FIX_ENDIANNESS_ZIP(zip_header) \
  38. +#define FIX_ENDIANNESS_ZIP(zip) \
  39. do { if (BB_BIG_ENDIAN) { \
  40. - (zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \
  41. - (zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \
  42. - (zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \
  43. - (zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \
  44. - (zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \
  45. + (zip).fmt.crc32 = SWAP_LE32((zip).fmt.crc32 ); \
  46. + (zip).fmt.cmpsize = SWAP_LE32((zip).fmt.cmpsize ); \
  47. + (zip).fmt.ucmpsize = SWAP_LE32((zip).fmt.ucmpsize ); \
  48. + (zip).fmt.filename_len = SWAP_LE16((zip).fmt.filename_len); \
  49. + (zip).fmt.extra_len = SWAP_LE16((zip).fmt.extra_len ); \
  50. }} while (0)
  51. #define CDF_HEADER_LEN 42
  52. @@ -118,39 +118,39 @@ typedef union {
  53. uint32_t crc32; /* 12-15 */
  54. uint32_t cmpsize; /* 16-19 */
  55. uint32_t ucmpsize; /* 20-23 */
  56. - uint16_t file_name_length; /* 24-25 */
  57. - uint16_t extra_field_length; /* 26-27 */
  58. + uint16_t filename_len; /* 24-25 */
  59. + uint16_t extra_len; /* 26-27 */
  60. uint16_t file_comment_length; /* 28-29 */
  61. uint16_t disk_number_start; /* 30-31 */
  62. - uint16_t internal_file_attributes; /* 32-33 */
  63. - uint32_t external_file_attributes PACKED; /* 34-37 */
  64. + uint16_t internal_attributes; /* 32-33 */
  65. + uint32_t external_attributes PACKED; /* 34-37 */
  66. uint32_t relative_offset_of_local_header PACKED; /* 38-41 */
  67. /* filename follows (not NUL terminated) */
  68. /* extra field follows */
  69. - /* comment follows */
  70. - } formatted PACKED;
  71. + /* file comment follows */
  72. + } fmt PACKED;
  73. } cdf_header_t;
  74. -#define FIX_ENDIANNESS_CDF(cdf_header) \
  75. +#define FIX_ENDIANNESS_CDF(cdf) \
  76. do { if (BB_BIG_ENDIAN) { \
  77. - (cdf_header).formatted.version_made_by = SWAP_LE16((cdf_header).formatted.version_made_by); \
  78. - (cdf_header).formatted.version_needed = SWAP_LE16((cdf_header).formatted.version_needed); \
  79. - (cdf_header).formatted.method = SWAP_LE16((cdf_header).formatted.method ); \
  80. - (cdf_header).formatted.modtime = SWAP_LE16((cdf_header).formatted.modtime ); \
  81. - (cdf_header).formatted.moddate = SWAP_LE16((cdf_header).formatted.moddate ); \
  82. - (cdf_header).formatted.crc32 = SWAP_LE32((cdf_header).formatted.crc32 ); \
  83. - (cdf_header).formatted.cmpsize = SWAP_LE32((cdf_header).formatted.cmpsize ); \
  84. - (cdf_header).formatted.ucmpsize = SWAP_LE32((cdf_header).formatted.ucmpsize ); \
  85. - (cdf_header).formatted.file_name_length = SWAP_LE16((cdf_header).formatted.file_name_length); \
  86. - (cdf_header).formatted.extra_field_length = SWAP_LE16((cdf_header).formatted.extra_field_length); \
  87. - (cdf_header).formatted.file_comment_length = SWAP_LE16((cdf_header).formatted.file_comment_length); \
  88. - (cdf_header).formatted.external_file_attributes = SWAP_LE32((cdf_header).formatted.external_file_attributes); \
  89. + (cdf).fmt.version_made_by = SWAP_LE16((cdf).fmt.version_made_by); \
  90. + (cdf).fmt.version_needed = SWAP_LE16((cdf).fmt.version_needed); \
  91. + (cdf).fmt.method = SWAP_LE16((cdf).fmt.method ); \
  92. + (cdf).fmt.modtime = SWAP_LE16((cdf).fmt.modtime ); \
  93. + (cdf).fmt.moddate = SWAP_LE16((cdf).fmt.moddate ); \
  94. + (cdf).fmt.crc32 = SWAP_LE32((cdf).fmt.crc32 ); \
  95. + (cdf).fmt.cmpsize = SWAP_LE32((cdf).fmt.cmpsize ); \
  96. + (cdf).fmt.ucmpsize = SWAP_LE32((cdf).fmt.ucmpsize ); \
  97. + (cdf).fmt.filename_len = SWAP_LE16((cdf).fmt.filename_len); \
  98. + (cdf).fmt.extra_len = SWAP_LE16((cdf).fmt.extra_len ); \
  99. + (cdf).fmt.file_comment_length = SWAP_LE16((cdf).fmt.file_comment_length); \
  100. + (cdf).fmt.external_attributes = SWAP_LE32((cdf).fmt.external_attributes); \
  101. }} while (0)
  102. -#define CDE_HEADER_LEN 16
  103. +#define CDE_LEN 16
  104. typedef union {
  105. - uint8_t raw[CDE_HEADER_LEN];
  106. + uint8_t raw[CDE_LEN];
  107. struct {
  108. /* uint32_t signature; 50 4b 05 06 */
  109. uint16_t this_disk_no;
  110. @@ -159,14 +159,14 @@ typedef union {
  111. uint16_t cdf_entries_total;
  112. uint32_t cdf_size;
  113. uint32_t cdf_offset;
  114. - /* uint16_t file_comment_length; */
  115. - /* .ZIP file comment (variable size) */
  116. - } formatted PACKED;
  117. -} cde_header_t;
  118. + /* uint16_t archive_comment_length; */
  119. + /* archive comment follows */
  120. + } fmt PACKED;
  121. +} cde_t;
  122. -#define FIX_ENDIANNESS_CDE(cde_header) \
  123. +#define FIX_ENDIANNESS_CDE(cde) \
  124. do { if (BB_BIG_ENDIAN) { \
  125. - (cde_header).formatted.cdf_offset = SWAP_LE32((cde_header).formatted.cdf_offset); \
  126. + (cde).fmt.cdf_offset = SWAP_LE32((cde).fmt.cdf_offset); \
  127. }} while (0)
  128. struct BUG {
  129. @@ -175,13 +175,13 @@ struct BUG {
  130. * even though the elements are all in the right place.
  131. */
  132. char BUG_zip_header_must_be_26_bytes[
  133. - offsetof(zip_header_t, formatted.extra_len) + 2
  134. + offsetof(zip_header_t, fmt.extra_len) + 2
  135. == ZIP_HEADER_LEN ? 1 : -1];
  136. char BUG_cdf_header_must_be_42_bytes[
  137. - offsetof(cdf_header_t, formatted.relative_offset_of_local_header) + 4
  138. + offsetof(cdf_header_t, fmt.relative_offset_of_local_header) + 4
  139. == CDF_HEADER_LEN ? 1 : -1];
  140. - char BUG_cde_header_must_be_16_bytes[
  141. - sizeof(cde_header_t) == CDE_HEADER_LEN ? 1 : -1];
  142. + char BUG_cde_must_be_16_bytes[
  143. + sizeof(cde_t) == CDE_LEN ? 1 : -1];
  144. };
  145. @@ -207,7 +207,7 @@ enum { zip_fd = 3 };
  146. /* NB: does not preserve file position! */
  147. static uint32_t find_cdf_offset(void)
  148. {
  149. - cde_header_t cde_header;
  150. + cde_t cde;
  151. unsigned char *buf;
  152. unsigned char *p;
  153. off_t end;
  154. @@ -228,7 +228,7 @@ static uint32_t find_cdf_offset(void)
  155. found = BAD_CDF_OFFSET;
  156. p = buf;
  157. - while (p <= buf + PEEK_FROM_END - CDE_HEADER_LEN - 4) {
  158. + while (p <= buf + PEEK_FROM_END - CDE_LEN - 4) {
  159. if (*p != 'P') {
  160. p++;
  161. continue;
  162. @@ -240,19 +240,19 @@ static uint32_t find_cdf_offset(void)
  163. if (*++p != 6)
  164. continue;
  165. /* we found CDE! */
  166. - memcpy(cde_header.raw, p + 1, CDE_HEADER_LEN);
  167. - FIX_ENDIANNESS_CDE(cde_header);
  168. + memcpy(cde.raw, p + 1, CDE_LEN);
  169. + FIX_ENDIANNESS_CDE(cde);
  170. /*
  171. * I've seen .ZIP files with seemingly valid CDEs
  172. * where cdf_offset points past EOF - ??
  173. * This check ignores such CDEs:
  174. */
  175. - if (cde_header.formatted.cdf_offset < end + (p - buf)) {
  176. - found = cde_header.formatted.cdf_offset;
  177. + if (cde.fmt.cdf_offset < end + (p - buf)) {
  178. + found = cde.fmt.cdf_offset;
  179. dbg("Possible cdf_offset:0x%x at 0x%"OFF_FMT"x",
  180. (unsigned)found, end + (p-3 - buf));
  181. dbg(" cdf_offset+cdf_size:0x%x",
  182. - (unsigned)(found + SWAP_LE32(cde_header.formatted.cdf_size)));
  183. + (unsigned)(found + SWAP_LE32(cde.fmt.cdf_size)));
  184. /*
  185. * We do not "break" here because only the last CDE is valid.
  186. * I've seen a .zip archive which contained a .zip file,
  187. @@ -266,7 +266,7 @@ static uint32_t find_cdf_offset(void)
  188. return found;
  189. };
  190. -static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr)
  191. +static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf)
  192. {
  193. uint32_t magic;
  194. @@ -276,23 +276,25 @@ static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr)
  195. dbg("Reading CDF at 0x%x", (unsigned)cdf_offset);
  196. xlseek(zip_fd, cdf_offset, SEEK_SET);
  197. xread(zip_fd, &magic, 4);
  198. - /* Central Directory End? */
  199. + /* Central Directory End? Assume CDF has ended.
  200. + * (more correct method is to use cde.cdf_entries_total counter)
  201. + */
  202. if (magic == ZIP_CDE_MAGIC) {
  203. dbg("got ZIP_CDE_MAGIC");
  204. return 0; /* EOF */
  205. }
  206. - xread(zip_fd, cdf_ptr->raw, CDF_HEADER_LEN);
  207. + xread(zip_fd, cdf->raw, CDF_HEADER_LEN);
  208. - FIX_ENDIANNESS_CDF(*cdf_ptr);
  209. - dbg(" file_name_length:%u extra_field_length:%u file_comment_length:%u",
  210. - (unsigned)cdf_ptr->formatted.file_name_length,
  211. - (unsigned)cdf_ptr->formatted.extra_field_length,
  212. - (unsigned)cdf_ptr->formatted.file_comment_length
  213. + FIX_ENDIANNESS_CDF(*cdf);
  214. + dbg(" filename_len:%u extra_len:%u file_comment_length:%u",
  215. + (unsigned)cdf->fmt.filename_len,
  216. + (unsigned)cdf->fmt.extra_len,
  217. + (unsigned)cdf->fmt.file_comment_length
  218. );
  219. cdf_offset += 4 + CDF_HEADER_LEN
  220. - + cdf_ptr->formatted.file_name_length
  221. - + cdf_ptr->formatted.extra_field_length
  222. - + cdf_ptr->formatted.file_comment_length;
  223. + + cdf->fmt.filename_len
  224. + + cdf->fmt.extra_len
  225. + + cdf->fmt.file_comment_length;
  226. return cdf_offset;
  227. };
  228. @@ -315,28 +317,28 @@ static void unzip_create_leading_dirs(const char *fn)
  229. free(name);
  230. }
  231. -static void unzip_extract(zip_header_t *zip_header, int dst_fd)
  232. +static void unzip_extract(zip_header_t *zip, int dst_fd)
  233. {
  234. - if (zip_header->formatted.method == 0) {
  235. + if (zip->fmt.method == 0) {
  236. /* Method 0 - stored (not compressed) */
  237. - off_t size = zip_header->formatted.ucmpsize;
  238. + off_t size = zip->fmt.ucmpsize;
  239. if (size)
  240. bb_copyfd_exact_size(zip_fd, dst_fd, size);
  241. } else {
  242. /* Method 8 - inflate */
  243. transformer_state_t xstate;
  244. init_transformer_state(&xstate);
  245. - xstate.bytes_in = zip_header->formatted.cmpsize;
  246. + xstate.bytes_in = zip->fmt.cmpsize;
  247. xstate.src_fd = zip_fd;
  248. xstate.dst_fd = dst_fd;
  249. if (inflate_unzip(&xstate) < 0)
  250. bb_error_msg_and_die("inflate error");
  251. /* Validate decompression - crc */
  252. - if (zip_header->formatted.crc32 != (xstate.crc32 ^ 0xffffffffL)) {
  253. + if (zip->fmt.crc32 != (xstate.crc32 ^ 0xffffffffL)) {
  254. bb_error_msg_and_die("crc error");
  255. }
  256. /* Validate decompression - size */
  257. - if (zip_header->formatted.ucmpsize != xstate.bytes_out) {
  258. + if (zip->fmt.ucmpsize != xstate.bytes_out) {
  259. /* Don't die. Who knows, maybe len calculation
  260. * was botched somewhere. After all, crc matched! */
  261. bb_error_msg("bad length");
  262. @@ -563,7 +565,7 @@ int unzip_main(int argc, char **argv)
  263. total_entries = 0;
  264. cdf_offset = find_cdf_offset(); /* try to seek to the end, find CDE and CDF start */
  265. while (1) {
  266. - zip_header_t zip_header;
  267. + zip_header_t zip;
  268. mode_t dir_mode = 0777;
  269. #if ENABLE_FEATURE_UNZIP_CDF
  270. mode_t file_mode = 0666;
  271. @@ -589,7 +591,7 @@ int unzip_main(int argc, char **argv)
  272. /* Check magic number */
  273. xread(zip_fd, &magic, 4);
  274. - /* Central directory? It's at the end, so exit */
  275. + /* CDF item? Assume there are no more files, exit */
  276. if (magic == ZIP_CDF_MAGIC) {
  277. dbg("got ZIP_CDF_MAGIC");
  278. break;
  279. @@ -605,71 +607,74 @@ int unzip_main(int argc, char **argv)
  280. bb_error_msg_and_die("invalid zip magic %08X", (int)magic);
  281. dbg("got ZIP_FILEHEADER_MAGIC");
  282. - xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN);
  283. - FIX_ENDIANNESS_ZIP(zip_header);
  284. - if ((zip_header.formatted.method != 0)
  285. - && (zip_header.formatted.method != 8)
  286. + xread(zip_fd, zip.raw, ZIP_HEADER_LEN);
  287. + FIX_ENDIANNESS_ZIP(zip);
  288. + if ((zip.fmt.method != 0)
  289. + && (zip.fmt.method != 8)
  290. ) {
  291. /* TODO? method 12: bzip2, method 14: LZMA */
  292. - bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method);
  293. + bb_error_msg_and_die("unsupported method %d", zip.fmt.method);
  294. }
  295. - if (zip_header.formatted.zip_flags & SWAP_LE16(0x0009)) {
  296. + if (zip.fmt.zip_flags & SWAP_LE16(0x0009)) {
  297. bb_error_msg_and_die("zip flags 1 and 8 are not supported");
  298. }
  299. }
  300. #if ENABLE_FEATURE_UNZIP_CDF
  301. else {
  302. /* cdf_offset is valid (and we know the file is seekable) */
  303. - cdf_header_t cdf_header;
  304. - cdf_offset = read_next_cdf(cdf_offset, &cdf_header);
  305. + cdf_header_t cdf;
  306. + cdf_offset = read_next_cdf(cdf_offset, &cdf);
  307. if (cdf_offset == 0) /* EOF? */
  308. break;
  309. -# if 0
  310. +# if 1
  311. xlseek(zip_fd,
  312. - SWAP_LE32(cdf_header.formatted.relative_offset_of_local_header) + 4,
  313. + SWAP_LE32(cdf.fmt.relative_offset_of_local_header) + 4,
  314. SEEK_SET);
  315. - xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN);
  316. - FIX_ENDIANNESS_ZIP(zip_header);
  317. - if (zip_header.formatted.zip_flags & SWAP_LE16(0x0008)) {
  318. + xread(zip_fd, zip.raw, ZIP_HEADER_LEN);
  319. + FIX_ENDIANNESS_ZIP(zip);
  320. + if (zip.fmt.zip_flags & SWAP_LE16(0x0008)) {
  321. /* 0x0008 - streaming. [u]cmpsize can be reliably gotten
  322. * only from Central Directory.
  323. */
  324. - zip_header.formatted.crc32 = cdf_header.formatted.crc32;
  325. - zip_header.formatted.cmpsize = cdf_header.formatted.cmpsize;
  326. - zip_header.formatted.ucmpsize = cdf_header.formatted.ucmpsize;
  327. + zip.fmt.crc32 = cdf.fmt.crc32;
  328. + zip.fmt.cmpsize = cdf.fmt.cmpsize;
  329. + zip.fmt.ucmpsize = cdf.fmt.ucmpsize;
  330. }
  331. # else
  332. - /* CDF has the same data as local header, no need to read the latter */
  333. - memcpy(&zip_header.formatted.version,
  334. - &cdf_header.formatted.version_needed, ZIP_HEADER_LEN);
  335. + /* CDF has the same data as local header, no need to read the latter...
  336. + * ...not really. An archive was seen with cdf.extra_len == 6 but
  337. + * zip.extra_len == 0.
  338. + */
  339. + memcpy(&zip.fmt.version,
  340. + &cdf.fmt.version_needed, ZIP_HEADER_LEN);
  341. xlseek(zip_fd,
  342. - SWAP_LE32(cdf_header.formatted.relative_offset_of_local_header) + 4 + ZIP_HEADER_LEN,
  343. + SWAP_LE32(cdf.fmt.relative_offset_of_local_header) + 4 + ZIP_HEADER_LEN,
  344. SEEK_SET);
  345. # endif
  346. - if ((cdf_header.formatted.version_made_by >> 8) == 3) {
  347. + if ((cdf.fmt.version_made_by >> 8) == 3) {
  348. /* This archive is created on Unix */
  349. - dir_mode = file_mode = (cdf_header.formatted.external_file_attributes >> 16);
  350. + dir_mode = file_mode = (cdf.fmt.external_attributes >> 16);
  351. }
  352. }
  353. #endif
  354. - if (zip_header.formatted.zip_flags & SWAP_LE16(0x0001)) {
  355. + if (zip.fmt.zip_flags & SWAP_LE16(0x0001)) {
  356. /* 0x0001 - encrypted */
  357. bb_error_msg_and_die("zip flag 1 (encryption) is not supported");
  358. }
  359. dbg("File cmpsize:0x%x extra_len:0x%x ucmpsize:0x%x",
  360. - (unsigned)zip_header.formatted.cmpsize,
  361. - (unsigned)zip_header.formatted.extra_len,
  362. - (unsigned)zip_header.formatted.ucmpsize
  363. + (unsigned)zip.fmt.cmpsize,
  364. + (unsigned)zip.fmt.extra_len,
  365. + (unsigned)zip.fmt.ucmpsize
  366. );
  367. /* Read filename */
  368. free(dst_fn);
  369. - dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
  370. - xread(zip_fd, dst_fn, zip_header.formatted.filename_len);
  371. + dst_fn = xzalloc(zip.fmt.filename_len + 1);
  372. + xread(zip_fd, dst_fn, zip.fmt.filename_len);
  373. /* Skip extra header bytes */
  374. - unzip_skip(zip_header.formatted.extra_len);
  375. + unzip_skip(zip.fmt.extra_len);
  376. /* Guard against "/abspath", "/../" and similar attacks */
  377. overlapping_strcpy(dst_fn, strip_unsafe_prefix(dst_fn));
  378. @@ -684,32 +689,32 @@ int unzip_main(int argc, char **argv)
  379. /* List entry */
  380. char dtbuf[sizeof("mm-dd-yyyy hh:mm")];
  381. sprintf(dtbuf, "%02u-%02u-%04u %02u:%02u",
  382. - (zip_header.formatted.moddate >> 5) & 0xf, // mm: 0x01e0
  383. - (zip_header.formatted.moddate) & 0x1f, // dd: 0x001f
  384. - (zip_header.formatted.moddate >> 9) + 1980, // yy: 0xfe00
  385. - (zip_header.formatted.modtime >> 11), // hh: 0xf800
  386. - (zip_header.formatted.modtime >> 5) & 0x3f // mm: 0x07e0
  387. - // seconds/2 are not shown, encoded in ----------- 0x001f
  388. + (zip.fmt.moddate >> 5) & 0xf, // mm: 0x01e0
  389. + (zip.fmt.moddate) & 0x1f, // dd: 0x001f
  390. + (zip.fmt.moddate >> 9) + 1980, // yy: 0xfe00
  391. + (zip.fmt.modtime >> 11), // hh: 0xf800
  392. + (zip.fmt.modtime >> 5) & 0x3f // mm: 0x07e0
  393. + // seconds/2 not shown, encoded in -- 0x001f
  394. );
  395. if (!verbose) {
  396. // " Length Date Time Name\n"
  397. // "--------- ---------- ----- ----"
  398. printf( "%9u " "%s " "%s\n",
  399. - (unsigned)zip_header.formatted.ucmpsize,
  400. + (unsigned)zip.fmt.ucmpsize,
  401. dtbuf,
  402. dst_fn);
  403. } else {
  404. - unsigned long percents = zip_header.formatted.ucmpsize - zip_header.formatted.cmpsize;
  405. + unsigned long percents = zip.fmt.ucmpsize - zip.fmt.cmpsize;
  406. if ((int32_t)percents < 0)
  407. percents = 0; /* happens if ucmpsize < cmpsize */
  408. percents = percents * 100;
  409. - if (zip_header.formatted.ucmpsize)
  410. - percents /= zip_header.formatted.ucmpsize;
  411. + if (zip.fmt.ucmpsize)
  412. + percents /= zip.fmt.ucmpsize;
  413. // " Length Method Size Cmpr Date Time CRC-32 Name\n"
  414. // "-------- ------ ------- ---- ---------- ----- -------- ----"
  415. printf( "%8u %s" "%9u%4u%% " "%s " "%08x " "%s\n",
  416. - (unsigned)zip_header.formatted.ucmpsize,
  417. - zip_header.formatted.method == 0 ? "Stored" : "Defl:N", /* Defl is method 8 */
  418. + (unsigned)zip.fmt.ucmpsize,
  419. + zip.fmt.method == 0 ? "Stored" : "Defl:N", /* Defl is method 8 */
  420. /* TODO: show other methods?
  421. * 1 - Shrunk
  422. * 2 - Reduced with compression factor 1
  423. @@ -722,15 +727,16 @@ int unzip_main(int argc, char **argv)
  424. * 10 - PKWARE Data Compression Library Imploding
  425. * 11 - Reserved by PKWARE
  426. * 12 - BZIP2
  427. + * 14 - LZMA
  428. */
  429. - (unsigned)zip_header.formatted.cmpsize,
  430. + (unsigned)zip.fmt.cmpsize,
  431. (unsigned)percents,
  432. dtbuf,
  433. - zip_header.formatted.crc32,
  434. + zip.fmt.crc32,
  435. dst_fn);
  436. - total_size += zip_header.formatted.cmpsize;
  437. + total_size += zip.fmt.cmpsize;
  438. }
  439. - total_usize += zip_header.formatted.ucmpsize;
  440. + total_usize += zip.fmt.ucmpsize;
  441. i = 'n';
  442. } else if (dst_fd == STDOUT_FILENO) {
  443. /* Extracting to STDOUT */
  444. @@ -798,9 +804,11 @@ int unzip_main(int argc, char **argv)
  445. #endif
  446. case -1: /* Unzip */
  447. if (!quiet) {
  448. - printf(" inflating: %s\n", dst_fn);
  449. + printf(/* zip.fmt.method == 0
  450. + ? " extracting: %s\n"
  451. + : */ " inflating: %s\n", dst_fn);
  452. }
  453. - unzip_extract(&zip_header, dst_fd);
  454. + unzip_extract(&zip, dst_fd);
  455. if (dst_fd != STDOUT_FILENO) {
  456. /* closing STDOUT is potentially bad for future business */
  457. close(dst_fd);
  458. @@ -811,7 +819,7 @@ int unzip_main(int argc, char **argv)
  459. overwrite = O_NEVER;
  460. case 'n':
  461. /* Skip entry data */
  462. - unzip_skip(zip_header.formatted.cmpsize);
  463. + unzip_skip(zip.fmt.cmpsize);
  464. break;
  465. case 'r':
  466. diff --git a/testsuite/unzip.tests b/testsuite/unzip.tests
  467. index d9c45242c..2e4becdb8 100755
  468. --- a/testsuite/unzip.tests
  469. +++ b/testsuite/unzip.tests
  470. @@ -34,7 +34,9 @@ rm foo.zip
  471. optional FEATURE_UNZIP_CDF
  472. testing "unzip (bad archive)" "uudecode; unzip bad.zip 2>&1; echo \$?" \
  473. "Archive: bad.zip
  474. -unzip: short read
  475. + inflating: ]3j½r«IK-%Ix
  476. +unzip: corrupted data
  477. +unzip: inflate error
  478. 1
  479. " \
  480. "" "\
  481. --
  482. 2.11.0