HardenedBSD src tree
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3867 lines
97KB

  1. /*-
  2. * Copyright (c) 2011 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_BZLIB_H
  34. #include <bzlib.h>
  35. #endif
  36. #ifdef HAVE_LZMA_H
  37. #include <lzma.h>
  38. #endif
  39. #ifdef HAVE_ZLIB_H
  40. #include <zlib.h>
  41. #endif
  42. #include "archive.h"
  43. #include "archive_entry.h"
  44. #include "archive_entry_locale.h"
  45. #include "archive_ppmd7_private.h"
  46. #include "archive_private.h"
  47. #include "archive_read_private.h"
  48. #include "archive_endian.h"
  49. #ifndef HAVE_ZLIB_H
  50. #include "archive_crc32.h"
  51. #endif
  52. #define _7ZIP_SIGNATURE "7z\xBC\xAF\x27\x1C"
  53. #define SFX_MIN_ADDR 0x27000
  54. #define SFX_MAX_ADDR 0x60000
  55. /*
  56. * Codec ID
  57. */
  58. #define _7Z_COPY 0
  59. #define _7Z_LZMA 0x030101
  60. #define _7Z_LZMA2 0x21
  61. #define _7Z_DEFLATE 0x040108
  62. #define _7Z_BZ2 0x040202
  63. #define _7Z_PPMD 0x030401
  64. #define _7Z_DELTA 0x03
  65. #define _7Z_CRYPTO_MAIN_ZIP 0x06F10101 /* Main Zip crypto algo */
  66. #define _7Z_CRYPTO_RAR_29 0x06F10303 /* Rar29 AES-128 + (modified SHA-1) */
  67. #define _7Z_CRYPTO_AES_256_SHA_256 0x06F10701 /* AES-256 + SHA-256 */
  68. #define _7Z_X86 0x03030103
  69. #define _7Z_X86_BCJ2 0x0303011B
  70. #define _7Z_POWERPC 0x03030205
  71. #define _7Z_IA64 0x03030401
  72. #define _7Z_ARM 0x03030501
  73. #define _7Z_ARMTHUMB 0x03030701
  74. #define _7Z_SPARC 0x03030805
  75. /*
  76. * 7-Zip header property IDs.
  77. */
  78. #define kEnd 0x00
  79. #define kHeader 0x01
  80. #define kArchiveProperties 0x02
  81. #define kAdditionalStreamsInfo 0x03
  82. #define kMainStreamsInfo 0x04
  83. #define kFilesInfo 0x05
  84. #define kPackInfo 0x06
  85. #define kUnPackInfo 0x07
  86. #define kSubStreamsInfo 0x08
  87. #define kSize 0x09
  88. #define kCRC 0x0A
  89. #define kFolder 0x0B
  90. #define kCodersUnPackSize 0x0C
  91. #define kNumUnPackStream 0x0D
  92. #define kEmptyStream 0x0E
  93. #define kEmptyFile 0x0F
  94. #define kAnti 0x10
  95. #define kName 0x11
  96. #define kCTime 0x12
  97. #define kATime 0x13
  98. #define kMTime 0x14
  99. #define kAttributes 0x15
  100. #define kEncodedHeader 0x17
  101. #define kDummy 0x19
  102. struct _7z_digests {
  103. unsigned char *defineds;
  104. uint32_t *digests;
  105. };
  106. struct _7z_folder {
  107. uint64_t numCoders;
  108. struct _7z_coder {
  109. unsigned long codec;
  110. uint64_t numInStreams;
  111. uint64_t numOutStreams;
  112. uint64_t propertiesSize;
  113. unsigned char *properties;
  114. } *coders;
  115. uint64_t numBindPairs;
  116. struct {
  117. uint64_t inIndex;
  118. uint64_t outIndex;
  119. } *bindPairs;
  120. uint64_t numPackedStreams;
  121. uint64_t *packedStreams;
  122. uint64_t numInStreams;
  123. uint64_t numOutStreams;
  124. uint64_t *unPackSize;
  125. unsigned char digest_defined;
  126. uint32_t digest;
  127. uint64_t numUnpackStreams;
  128. uint32_t packIndex;
  129. /* Unoperated bytes. */
  130. uint64_t skipped_bytes;
  131. };
  132. struct _7z_coders_info {
  133. uint64_t numFolders;
  134. struct _7z_folder *folders;
  135. uint64_t dataStreamIndex;
  136. };
  137. struct _7z_pack_info {
  138. uint64_t pos;
  139. uint64_t numPackStreams;
  140. uint64_t *sizes;
  141. struct _7z_digests digest;
  142. /* Calculated from pos and numPackStreams. */
  143. uint64_t *positions;
  144. };
  145. struct _7z_substream_info {
  146. size_t unpack_streams;
  147. uint64_t *unpackSizes;
  148. unsigned char *digestsDefined;
  149. uint32_t *digests;
  150. };
  151. struct _7z_stream_info {
  152. struct _7z_pack_info pi;
  153. struct _7z_coders_info ci;
  154. struct _7z_substream_info ss;
  155. };
  156. struct _7z_header_info {
  157. uint64_t dataIndex;
  158. unsigned char *emptyStreamBools;
  159. unsigned char *emptyFileBools;
  160. unsigned char *antiBools;
  161. unsigned char *attrBools;
  162. };
  163. struct _7zip_entry {
  164. size_t name_len;
  165. unsigned char *utf16name;
  166. #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
  167. const wchar_t *wname;
  168. #endif
  169. uint32_t folderIndex;
  170. uint32_t ssIndex;
  171. unsigned flg;
  172. #define MTIME_IS_SET (1<<0)
  173. #define ATIME_IS_SET (1<<1)
  174. #define CTIME_IS_SET (1<<2)
  175. #define CRC32_IS_SET (1<<3)
  176. #define HAS_STREAM (1<<4)
  177. time_t mtime;
  178. time_t atime;
  179. time_t ctime;
  180. long mtime_ns;
  181. long atime_ns;
  182. long ctime_ns;
  183. uint32_t mode;
  184. uint32_t attr;
  185. };
  186. struct _7zip {
  187. /* Structural information about the archive. */
  188. struct _7z_stream_info si;
  189. int header_is_being_read;
  190. int header_is_encoded;
  191. uint64_t header_bytes_remaining;
  192. unsigned long header_crc32;
  193. /* Header offset to check that reading points of the file contents
  194. * will not exceed the header. */
  195. uint64_t header_offset;
  196. /* Base offset of the archive file for a seek in case reading SFX. */
  197. uint64_t seek_base;
  198. /* List of entries */
  199. size_t entries_remaining;
  200. uint64_t numFiles;
  201. struct _7zip_entry *entries;
  202. struct _7zip_entry *entry;
  203. unsigned char *entry_names;
  204. /* entry_bytes_remaining is the number of bytes we expect. */
  205. int64_t entry_offset;
  206. uint64_t entry_bytes_remaining;
  207. /* Running CRC32 of the decompressed data */
  208. unsigned long entry_crc32;
  209. /* Flags to mark progress of decompression. */
  210. char end_of_entry;
  211. /* Uncompressed buffer control. */
  212. #define UBUFF_SIZE (64 * 1024)
  213. unsigned char *uncompressed_buffer;
  214. unsigned char *uncompressed_buffer_pointer;
  215. size_t uncompressed_buffer_size;
  216. size_t uncompressed_buffer_bytes_remaining;
  217. /* Offset of the compressed data. */
  218. int64_t stream_offset;
  219. /*
  220. * Decompressing control data.
  221. */
  222. unsigned folder_index;
  223. uint64_t folder_outbytes_remaining;
  224. unsigned pack_stream_index;
  225. unsigned pack_stream_remaining;
  226. uint64_t pack_stream_inbytes_remaining;
  227. size_t pack_stream_bytes_unconsumed;
  228. /* The codec information of a folder. */
  229. unsigned long codec;
  230. unsigned long codec2;
  231. /*
  232. * Decompressor controllers.
  233. */
  234. /* Decoding LZMA1 and LZMA2 data. */
  235. #ifdef HAVE_LZMA_H
  236. lzma_stream lzstream;
  237. int lzstream_valid;
  238. #endif
  239. /* Decoding bzip2 data. */
  240. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  241. bz_stream bzstream;
  242. int bzstream_valid;
  243. #endif
  244. /* Decoding deflate data. */
  245. #ifdef HAVE_ZLIB_H
  246. z_stream stream;
  247. int stream_valid;
  248. #endif
  249. /* Decoding PPMd data. */
  250. int ppmd7_stat;
  251. CPpmd7 ppmd7_context;
  252. CPpmd7z_RangeDec range_dec;
  253. IByteIn bytein;
  254. struct {
  255. const unsigned char *next_in;
  256. int64_t avail_in;
  257. int64_t total_in;
  258. unsigned char *next_out;
  259. int64_t avail_out;
  260. int64_t total_out;
  261. int overconsumed;
  262. } ppstream;
  263. int ppmd7_valid;
  264. /* Decoding BCJ and BCJ2 data. */
  265. uint32_t bcj_state;
  266. size_t odd_bcj_size;
  267. unsigned char odd_bcj[4];
  268. /* Decoding BCJ data. */
  269. size_t bcj_prevPosT;
  270. uint32_t bcj_prevMask;
  271. uint32_t bcj_ip;
  272. /* Decoding BCJ2 data. */
  273. size_t main_stream_bytes_remaining;
  274. unsigned char *sub_stream_buff[3];
  275. size_t sub_stream_size[3];
  276. size_t sub_stream_bytes_remaining[3];
  277. unsigned char *tmp_stream_buff;
  278. size_t tmp_stream_buff_size;
  279. size_t tmp_stream_bytes_avail;
  280. size_t tmp_stream_bytes_remaining;
  281. #ifdef _LZMA_PROB32
  282. #define CProb uint32_t
  283. #else
  284. #define CProb uint16_t
  285. #endif
  286. CProb bcj2_p[256 + 2];
  287. uint8_t bcj2_prevByte;
  288. uint32_t bcj2_range;
  289. uint32_t bcj2_code;
  290. uint64_t bcj2_outPos;
  291. /* Filename character-set conversion data. */
  292. struct archive_string_conv *sconv;
  293. char format_name[64];
  294. /* Custom value that is non-zero if this archive contains encrypted entries. */
  295. int has_encrypted_entries;
  296. };
  297. /* Maximum entry size. This limitation prevents reading intentional
  298. * corrupted 7-zip files on assuming there are not so many entries in
  299. * the files. */
  300. #define UMAX_ENTRY ARCHIVE_LITERAL_ULL(100000000)
  301. static int archive_read_format_7zip_has_encrypted_entries(struct archive_read *);
  302. static int archive_read_support_format_7zip_capabilities(struct archive_read *a);
  303. static int archive_read_format_7zip_bid(struct archive_read *, int);
  304. static int archive_read_format_7zip_cleanup(struct archive_read *);
  305. static int archive_read_format_7zip_read_data(struct archive_read *,
  306. const void **, size_t *, int64_t *);
  307. static int archive_read_format_7zip_read_data_skip(struct archive_read *);
  308. static int archive_read_format_7zip_read_header(struct archive_read *,
  309. struct archive_entry *);
  310. static int check_7zip_header_in_sfx(const char *);
  311. static unsigned long decode_codec_id(const unsigned char *, size_t);
  312. static int decode_encoded_header_info(struct archive_read *,
  313. struct _7z_stream_info *);
  314. static int decompress(struct archive_read *, struct _7zip *,
  315. void *, size_t *, const void *, size_t *);
  316. static ssize_t extract_pack_stream(struct archive_read *, size_t);
  317. static void fileTimeToUtc(uint64_t, time_t *, long *);
  318. static uint64_t folder_uncompressed_size(struct _7z_folder *);
  319. static void free_CodersInfo(struct _7z_coders_info *);
  320. static void free_Digest(struct _7z_digests *);
  321. static void free_Folder(struct _7z_folder *);
  322. static void free_Header(struct _7z_header_info *);
  323. static void free_PackInfo(struct _7z_pack_info *);
  324. static void free_StreamsInfo(struct _7z_stream_info *);
  325. static void free_SubStreamsInfo(struct _7z_substream_info *);
  326. static int free_decompression(struct archive_read *, struct _7zip *);
  327. static ssize_t get_uncompressed_data(struct archive_read *, const void **,
  328. size_t, size_t);
  329. static const unsigned char * header_bytes(struct archive_read *, size_t);
  330. static int init_decompression(struct archive_read *, struct _7zip *,
  331. const struct _7z_coder *, const struct _7z_coder *);
  332. static int parse_7zip_uint64(struct archive_read *, uint64_t *);
  333. static int read_Bools(struct archive_read *, unsigned char *, size_t);
  334. static int read_CodersInfo(struct archive_read *,
  335. struct _7z_coders_info *);
  336. static int read_Digests(struct archive_read *, struct _7z_digests *,
  337. size_t);
  338. static int read_Folder(struct archive_read *, struct _7z_folder *);
  339. static int read_Header(struct archive_read *, struct _7z_header_info *,
  340. int);
  341. static int read_PackInfo(struct archive_read *, struct _7z_pack_info *);
  342. static int read_StreamsInfo(struct archive_read *,
  343. struct _7z_stream_info *);
  344. static int read_SubStreamsInfo(struct archive_read *,
  345. struct _7z_substream_info *, struct _7z_folder *, size_t);
  346. static int read_Times(struct archive_read *, struct _7z_header_info *,
  347. int);
  348. static void read_consume(struct archive_read *);
  349. static ssize_t read_stream(struct archive_read *, const void **, size_t,
  350. size_t);
  351. static int seek_pack(struct archive_read *);
  352. static int64_t skip_stream(struct archive_read *, size_t);
  353. static int skip_sfx(struct archive_read *, ssize_t);
  354. static int slurp_central_directory(struct archive_read *, struct _7zip *,
  355. struct _7z_header_info *);
  356. static int setup_decode_folder(struct archive_read *, struct _7z_folder *,
  357. int);
  358. static void x86_Init(struct _7zip *);
  359. static size_t x86_Convert(struct _7zip *, uint8_t *, size_t);
  360. static ssize_t Bcj2_Decode(struct _7zip *, uint8_t *, size_t);
  361. int
  362. archive_read_support_format_7zip(struct archive *_a)
  363. {
  364. struct archive_read *a = (struct archive_read *)_a;
  365. struct _7zip *zip;
  366. int r;
  367. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  368. ARCHIVE_STATE_NEW, "archive_read_support_format_7zip");
  369. zip = calloc(1, sizeof(*zip));
  370. if (zip == NULL) {
  371. archive_set_error(&a->archive, ENOMEM,
  372. "Can't allocate 7zip data");
  373. return (ARCHIVE_FATAL);
  374. }
  375. /*
  376. * Until enough data has been read, we cannot tell about
  377. * any encrypted entries yet.
  378. */
  379. zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  380. r = __archive_read_register_format(a,
  381. zip,
  382. "7zip",
  383. archive_read_format_7zip_bid,
  384. NULL,
  385. archive_read_format_7zip_read_header,
  386. archive_read_format_7zip_read_data,
  387. archive_read_format_7zip_read_data_skip,
  388. NULL,
  389. archive_read_format_7zip_cleanup,
  390. archive_read_support_format_7zip_capabilities,
  391. archive_read_format_7zip_has_encrypted_entries);
  392. if (r != ARCHIVE_OK)
  393. free(zip);
  394. return (ARCHIVE_OK);
  395. }
  396. static int
  397. archive_read_support_format_7zip_capabilities(struct archive_read * a)
  398. {
  399. (void)a; /* UNUSED */
  400. return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
  401. ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  402. }
  403. static int
  404. archive_read_format_7zip_has_encrypted_entries(struct archive_read *_a)
  405. {
  406. if (_a && _a->format) {
  407. struct _7zip * zip = (struct _7zip *)_a->format->data;
  408. if (zip) {
  409. return zip->has_encrypted_entries;
  410. }
  411. }
  412. return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  413. }
  414. static int
  415. archive_read_format_7zip_bid(struct archive_read *a, int best_bid)
  416. {
  417. const char *p;
  418. /* If someone has already bid more than 32, then avoid
  419. trashing the look-ahead buffers with a seek. */
  420. if (best_bid > 32)
  421. return (-1);
  422. if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
  423. return (0);
  424. /* If first six bytes are the 7-Zip signature,
  425. * return the bid right now. */
  426. if (memcmp(p, _7ZIP_SIGNATURE, 6) == 0)
  427. return (48);
  428. /*
  429. * It may a 7-Zip SFX archive file. If first two bytes are
  430. * 'M' and 'Z' available on Windows or first four bytes are
  431. * "\x7F\x45LF" available on posix like system, seek the 7-Zip
  432. * signature. Although we will perform a seek when reading
  433. * a header, what we do not use __archive_read_seek() here is
  434. * due to a bidding performance.
  435. */
  436. if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
  437. ssize_t offset = SFX_MIN_ADDR;
  438. ssize_t window = 4096;
  439. ssize_t bytes_avail;
  440. while (offset + window <= (SFX_MAX_ADDR)) {
  441. const char *buff = __archive_read_ahead(a,
  442. offset + window, &bytes_avail);
  443. if (buff == NULL) {
  444. /* Remaining bytes are less than window. */
  445. window >>= 1;
  446. if (window < 0x40)
  447. return (0);
  448. continue;
  449. }
  450. p = buff + offset;
  451. while (p + 32 < buff + bytes_avail) {
  452. int step = check_7zip_header_in_sfx(p);
  453. if (step == 0)
  454. return (48);
  455. p += step;
  456. }
  457. offset = p - buff;
  458. }
  459. }
  460. return (0);
  461. }
  462. static int
  463. check_7zip_header_in_sfx(const char *p)
  464. {
  465. switch ((unsigned char)p[5]) {
  466. case 0x1C:
  467. if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0)
  468. return (6);
  469. /*
  470. * Test the CRC because its extraction code has 7-Zip
  471. * Magic Code, so we should do this in order not to
  472. * make a mis-detection.
  473. */
  474. if (crc32(0, (const unsigned char *)p + 12, 20)
  475. != archive_le32dec(p + 8))
  476. return (6);
  477. /* Hit the header! */
  478. return (0);
  479. case 0x37: return (5);
  480. case 0x7A: return (4);
  481. case 0xBC: return (3);
  482. case 0xAF: return (2);
  483. case 0x27: return (1);
  484. default: return (6);
  485. }
  486. }
  487. static int
  488. skip_sfx(struct archive_read *a, ssize_t bytes_avail)
  489. {
  490. const void *h;
  491. const char *p, *q;
  492. size_t skip, offset;
  493. ssize_t bytes, window;
  494. /*
  495. * If bytes_avail > SFX_MIN_ADDR we do not have to call
  496. * __archive_read_seek() at this time since we have
  497. * already had enough data.
  498. */
  499. if (bytes_avail > SFX_MIN_ADDR)
  500. __archive_read_consume(a, SFX_MIN_ADDR);
  501. else if (__archive_read_seek(a, SFX_MIN_ADDR, SEEK_SET) < 0)
  502. return (ARCHIVE_FATAL);
  503. offset = 0;
  504. window = 1;
  505. while (offset + window <= SFX_MAX_ADDR - SFX_MIN_ADDR) {
  506. h = __archive_read_ahead(a, window, &bytes);
  507. if (h == NULL) {
  508. /* Remaining bytes are less than window. */
  509. window >>= 1;
  510. if (window < 0x40)
  511. goto fatal;
  512. continue;
  513. }
  514. if (bytes < 6) {
  515. /* This case might happen when window == 1. */
  516. window = 4096;
  517. continue;
  518. }
  519. p = (const char *)h;
  520. q = p + bytes;
  521. /*
  522. * Scan ahead until we find something that looks
  523. * like the 7-Zip header.
  524. */
  525. while (p + 32 < q) {
  526. int step = check_7zip_header_in_sfx(p);
  527. if (step == 0) {
  528. struct _7zip *zip =
  529. (struct _7zip *)a->format->data;
  530. skip = p - (const char *)h;
  531. __archive_read_consume(a, skip);
  532. zip->seek_base = SFX_MIN_ADDR + offset + skip;
  533. return (ARCHIVE_OK);
  534. }
  535. p += step;
  536. }
  537. skip = p - (const char *)h;
  538. __archive_read_consume(a, skip);
  539. offset += skip;
  540. if (window == 1)
  541. window = 4096;
  542. }
  543. fatal:
  544. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  545. "Couldn't find out 7-Zip header");
  546. return (ARCHIVE_FATAL);
  547. }
  548. static int
  549. archive_read_format_7zip_read_header(struct archive_read *a,
  550. struct archive_entry *entry)
  551. {
  552. struct _7zip *zip = (struct _7zip *)a->format->data;
  553. struct _7zip_entry *zip_entry;
  554. int r, ret = ARCHIVE_OK;
  555. struct _7z_folder *folder = 0;
  556. uint64_t fidx = 0;
  557. /*
  558. * It should be sufficient to call archive_read_next_header() for
  559. * a reader to determine if an entry is encrypted or not. If the
  560. * encryption of an entry is only detectable when calling
  561. * archive_read_data(), so be it. We'll do the same check there
  562. * as well.
  563. */
  564. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  565. zip->has_encrypted_entries = 0;
  566. }
  567. a->archive.archive_format = ARCHIVE_FORMAT_7ZIP;
  568. if (a->archive.archive_format_name == NULL)
  569. a->archive.archive_format_name = "7-Zip";
  570. if (zip->entries == NULL) {
  571. struct _7z_header_info header;
  572. memset(&header, 0, sizeof(header));
  573. r = slurp_central_directory(a, zip, &header);
  574. free_Header(&header);
  575. if (r != ARCHIVE_OK)
  576. return (r);
  577. zip->entries_remaining = (size_t)zip->numFiles;
  578. zip->entry = zip->entries;
  579. } else {
  580. ++zip->entry;
  581. }
  582. zip_entry = zip->entry;
  583. if (zip->entries_remaining <= 0 || zip_entry == NULL)
  584. return ARCHIVE_EOF;
  585. --zip->entries_remaining;
  586. zip->entry_offset = 0;
  587. zip->end_of_entry = 0;
  588. zip->entry_crc32 = crc32(0, NULL, 0);
  589. /* Setup a string conversion for a filename. */
  590. if (zip->sconv == NULL) {
  591. zip->sconv = archive_string_conversion_from_charset(
  592. &a->archive, "UTF-16LE", 1);
  593. if (zip->sconv == NULL)
  594. return (ARCHIVE_FATAL);
  595. }
  596. /* Figure out if the entry is encrypted by looking at the folder
  597. that is associated to the current 7zip entry. If the folder
  598. has a coder with a _7Z_CRYPTO codec then the folder is encrypted.
  599. Hence the entry must also be encrypted. */
  600. if (zip_entry && zip_entry->folderIndex < zip->si.ci.numFolders) {
  601. folder = &(zip->si.ci.folders[zip_entry->folderIndex]);
  602. for (fidx=0; folder && fidx<folder->numCoders; fidx++) {
  603. switch(folder->coders[fidx].codec) {
  604. case _7Z_CRYPTO_MAIN_ZIP:
  605. case _7Z_CRYPTO_RAR_29:
  606. case _7Z_CRYPTO_AES_256_SHA_256: {
  607. archive_entry_set_is_data_encrypted(entry, 1);
  608. zip->has_encrypted_entries = 1;
  609. break;
  610. }
  611. }
  612. }
  613. }
  614. /* Now that we've checked for encryption, if there were still no
  615. * encrypted entries found we can say for sure that there are none.
  616. */
  617. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  618. zip->has_encrypted_entries = 0;
  619. }
  620. if (archive_entry_copy_pathname_l(entry,
  621. (const char *)zip_entry->utf16name,
  622. zip_entry->name_len, zip->sconv) != 0) {
  623. if (errno == ENOMEM) {
  624. archive_set_error(&a->archive, ENOMEM,
  625. "Can't allocate memory for Pathname");
  626. return (ARCHIVE_FATAL);
  627. }
  628. archive_set_error(&a->archive,
  629. ARCHIVE_ERRNO_FILE_FORMAT,
  630. "Pathname cannot be converted "
  631. "from %s to current locale.",
  632. archive_string_conversion_charset_name(zip->sconv));
  633. ret = ARCHIVE_WARN;
  634. }
  635. /* Populate some additional entry fields: */
  636. archive_entry_set_mode(entry, zip_entry->mode);
  637. if (zip_entry->flg & MTIME_IS_SET)
  638. archive_entry_set_mtime(entry, zip_entry->mtime,
  639. zip_entry->mtime_ns);
  640. if (zip_entry->flg & CTIME_IS_SET)
  641. archive_entry_set_ctime(entry, zip_entry->ctime,
  642. zip_entry->ctime_ns);
  643. if (zip_entry->flg & ATIME_IS_SET)
  644. archive_entry_set_atime(entry, zip_entry->atime,
  645. zip_entry->atime_ns);
  646. if (zip_entry->ssIndex != (uint32_t)-1) {
  647. zip->entry_bytes_remaining =
  648. zip->si.ss.unpackSizes[zip_entry->ssIndex];
  649. archive_entry_set_size(entry, zip->entry_bytes_remaining);
  650. } else {
  651. zip->entry_bytes_remaining = 0;
  652. archive_entry_set_size(entry, 0);
  653. }
  654. /* If there's no body, force read_data() to return EOF immediately. */
  655. if (zip->entry_bytes_remaining < 1)
  656. zip->end_of_entry = 1;
  657. if ((zip_entry->mode & AE_IFMT) == AE_IFLNK) {
  658. unsigned char *symname = NULL;
  659. size_t symsize = 0;
  660. /*
  661. * Symbolic-name is recorded as its contents. We have to
  662. * read the contents at this time.
  663. */
  664. while (zip->entry_bytes_remaining > 0) {
  665. const void *buff;
  666. unsigned char *mem;
  667. size_t size;
  668. int64_t offset;
  669. r = archive_read_format_7zip_read_data(a, &buff,
  670. &size, &offset);
  671. if (r < ARCHIVE_WARN) {
  672. free(symname);
  673. return (r);
  674. }
  675. mem = realloc(symname, symsize + size + 1);
  676. if (mem == NULL) {
  677. free(symname);
  678. archive_set_error(&a->archive, ENOMEM,
  679. "Can't allocate memory for Symname");
  680. return (ARCHIVE_FATAL);
  681. }
  682. symname = mem;
  683. memcpy(symname+symsize, buff, size);
  684. symsize += size;
  685. }
  686. if (symsize == 0) {
  687. /* If there is no symname, handle it as a regular
  688. * file. */
  689. zip_entry->mode &= ~AE_IFMT;
  690. zip_entry->mode |= AE_IFREG;
  691. archive_entry_set_mode(entry, zip_entry->mode);
  692. } else {
  693. symname[symsize] = '\0';
  694. archive_entry_copy_symlink(entry,
  695. (const char *)symname);
  696. }
  697. free(symname);
  698. archive_entry_set_size(entry, 0);
  699. }
  700. /* Set up a more descriptive format name. */
  701. sprintf(zip->format_name, "7-Zip");
  702. a->archive.archive_format_name = zip->format_name;
  703. return (ret);
  704. }
  705. static int
  706. archive_read_format_7zip_read_data(struct archive_read *a,
  707. const void **buff, size_t *size, int64_t *offset)
  708. {
  709. struct _7zip *zip;
  710. ssize_t bytes;
  711. int ret = ARCHIVE_OK;
  712. zip = (struct _7zip *)(a->format->data);
  713. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  714. zip->has_encrypted_entries = 0;
  715. }
  716. if (zip->pack_stream_bytes_unconsumed)
  717. read_consume(a);
  718. *offset = zip->entry_offset;
  719. *size = 0;
  720. *buff = NULL;
  721. /*
  722. * If we hit end-of-entry last time, clean up and return
  723. * ARCHIVE_EOF this time.
  724. */
  725. if (zip->end_of_entry)
  726. return (ARCHIVE_EOF);
  727. bytes = read_stream(a, buff,
  728. (size_t)zip->entry_bytes_remaining, 0);
  729. if (bytes < 0)
  730. return ((int)bytes);
  731. if (bytes == 0) {
  732. archive_set_error(&a->archive,
  733. ARCHIVE_ERRNO_FILE_FORMAT,
  734. "Truncated 7-Zip file body");
  735. return (ARCHIVE_FATAL);
  736. }
  737. zip->entry_bytes_remaining -= bytes;
  738. if (zip->entry_bytes_remaining == 0)
  739. zip->end_of_entry = 1;
  740. /* Update checksum */
  741. if ((zip->entry->flg & CRC32_IS_SET) && bytes)
  742. zip->entry_crc32 = crc32(zip->entry_crc32, *buff,
  743. (unsigned)bytes);
  744. /* If we hit the end, swallow any end-of-data marker. */
  745. if (zip->end_of_entry) {
  746. /* Check computed CRC against file contents. */
  747. if ((zip->entry->flg & CRC32_IS_SET) &&
  748. zip->si.ss.digests[zip->entry->ssIndex] !=
  749. zip->entry_crc32) {
  750. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  751. "7-Zip bad CRC: 0x%lx should be 0x%lx",
  752. (unsigned long)zip->entry_crc32,
  753. (unsigned long)zip->si.ss.digests[
  754. zip->entry->ssIndex]);
  755. ret = ARCHIVE_WARN;
  756. }
  757. }
  758. *size = bytes;
  759. *offset = zip->entry_offset;
  760. zip->entry_offset += bytes;
  761. return (ret);
  762. }
  763. static int
  764. archive_read_format_7zip_read_data_skip(struct archive_read *a)
  765. {
  766. struct _7zip *zip;
  767. int64_t bytes_skipped;
  768. zip = (struct _7zip *)(a->format->data);
  769. if (zip->pack_stream_bytes_unconsumed)
  770. read_consume(a);
  771. /* If we've already read to end of data, we're done. */
  772. if (zip->end_of_entry)
  773. return (ARCHIVE_OK);
  774. /*
  775. * If the length is at the beginning, we can skip the
  776. * compressed data much more quickly.
  777. */
  778. bytes_skipped = skip_stream(a, (size_t)zip->entry_bytes_remaining);
  779. if (bytes_skipped < 0)
  780. return (ARCHIVE_FATAL);
  781. zip->entry_bytes_remaining = 0;
  782. /* This entry is finished and done. */
  783. zip->end_of_entry = 1;
  784. return (ARCHIVE_OK);
  785. }
  786. static int
  787. archive_read_format_7zip_cleanup(struct archive_read *a)
  788. {
  789. struct _7zip *zip;
  790. zip = (struct _7zip *)(a->format->data);
  791. free_StreamsInfo(&(zip->si));
  792. free(zip->entries);
  793. free(zip->entry_names);
  794. free_decompression(a, zip);
  795. free(zip->uncompressed_buffer);
  796. free(zip->sub_stream_buff[0]);
  797. free(zip->sub_stream_buff[1]);
  798. free(zip->sub_stream_buff[2]);
  799. free(zip->tmp_stream_buff);
  800. free(zip);
  801. (a->format->data) = NULL;
  802. return (ARCHIVE_OK);
  803. }
  804. static void
  805. read_consume(struct archive_read *a)
  806. {
  807. struct _7zip *zip = (struct _7zip *)a->format->data;
  808. if (zip->pack_stream_bytes_unconsumed) {
  809. __archive_read_consume(a, zip->pack_stream_bytes_unconsumed);
  810. zip->stream_offset += zip->pack_stream_bytes_unconsumed;
  811. zip->pack_stream_bytes_unconsumed = 0;
  812. }
  813. }
  814. #ifdef HAVE_LZMA_H
  815. /*
  816. * Set an error code and choose an error message for liblzma.
  817. */
  818. static void
  819. set_error(struct archive_read *a, int ret)
  820. {
  821. switch (ret) {
  822. case LZMA_STREAM_END: /* Found end of stream. */
  823. case LZMA_OK: /* Decompressor made some progress. */
  824. break;
  825. case LZMA_MEM_ERROR:
  826. archive_set_error(&a->archive, ENOMEM,
  827. "Lzma library error: Cannot allocate memory");
  828. break;
  829. case LZMA_MEMLIMIT_ERROR:
  830. archive_set_error(&a->archive, ENOMEM,
  831. "Lzma library error: Out of memory");
  832. break;
  833. case LZMA_FORMAT_ERROR:
  834. archive_set_error(&a->archive,
  835. ARCHIVE_ERRNO_MISC,
  836. "Lzma library error: format not recognized");
  837. break;
  838. case LZMA_OPTIONS_ERROR:
  839. archive_set_error(&a->archive,
  840. ARCHIVE_ERRNO_MISC,
  841. "Lzma library error: Invalid options");
  842. break;
  843. case LZMA_DATA_ERROR:
  844. archive_set_error(&a->archive,
  845. ARCHIVE_ERRNO_MISC,
  846. "Lzma library error: Corrupted input data");
  847. break;
  848. case LZMA_BUF_ERROR:
  849. archive_set_error(&a->archive,
  850. ARCHIVE_ERRNO_MISC,
  851. "Lzma library error: No progress is possible");
  852. break;
  853. default:
  854. /* Return an error. */
  855. archive_set_error(&a->archive,
  856. ARCHIVE_ERRNO_MISC,
  857. "Lzma decompression failed: Unknown error");
  858. break;
  859. }
  860. }
  861. #endif
  862. static unsigned long
  863. decode_codec_id(const unsigned char *codecId, size_t id_size)
  864. {
  865. unsigned i;
  866. unsigned long id = 0;
  867. for (i = 0; i < id_size; i++) {
  868. id <<= 8;
  869. id += codecId[i];
  870. }
  871. return (id);
  872. }
  873. static Byte
  874. ppmd_read(void *p)
  875. {
  876. struct archive_read *a = ((IByteIn*)p)->a;
  877. struct _7zip *zip = (struct _7zip *)(a->format->data);
  878. Byte b;
  879. if (zip->ppstream.avail_in == 0) {
  880. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  881. "Truncated RAR file data");
  882. zip->ppstream.overconsumed = 1;
  883. return (0);
  884. }
  885. b = *zip->ppstream.next_in++;
  886. zip->ppstream.avail_in--;
  887. zip->ppstream.total_in++;
  888. return (b);
  889. }
  890. static int
  891. init_decompression(struct archive_read *a, struct _7zip *zip,
  892. const struct _7z_coder *coder1, const struct _7z_coder *coder2)
  893. {
  894. int r;
  895. zip->codec = coder1->codec;
  896. zip->codec2 = -1;
  897. switch (zip->codec) {
  898. case _7Z_COPY:
  899. case _7Z_BZ2:
  900. case _7Z_DEFLATE:
  901. case _7Z_PPMD:
  902. if (coder2 != NULL) {
  903. if (coder2->codec != _7Z_X86 &&
  904. coder2->codec != _7Z_X86_BCJ2) {
  905. archive_set_error(&a->archive,
  906. ARCHIVE_ERRNO_MISC,
  907. "Unsupported filter %lx for %lx",
  908. coder2->codec, coder1->codec);
  909. return (ARCHIVE_FAILED);
  910. }
  911. zip->codec2 = coder2->codec;
  912. zip->bcj_state = 0;
  913. if (coder2->codec == _7Z_X86)
  914. x86_Init(zip);
  915. }
  916. break;
  917. default:
  918. break;
  919. }
  920. switch (zip->codec) {
  921. case _7Z_COPY:
  922. break;
  923. case _7Z_LZMA: case _7Z_LZMA2:
  924. #ifdef HAVE_LZMA_H
  925. #if LZMA_VERSION_MAJOR >= 5
  926. /* Effectively disable the limiter. */
  927. #define LZMA_MEMLIMIT UINT64_MAX
  928. #else
  929. /* NOTE: This needs to check memory size which running system has. */
  930. #define LZMA_MEMLIMIT (1U << 30)
  931. #endif
  932. {
  933. lzma_options_delta delta_opt;
  934. lzma_filter filters[LZMA_FILTERS_MAX], *ff;
  935. int fi = 0;
  936. if (zip->lzstream_valid) {
  937. lzma_end(&(zip->lzstream));
  938. zip->lzstream_valid = 0;
  939. }
  940. /*
  941. * NOTE: liblzma incompletely handle the BCJ+LZMA compressed
  942. * data made by 7-Zip because 7-Zip does not add End-Of-
  943. * Payload Marker(EOPM) at the end of LZMA compressed data,
  944. * and so liblzma cannot know the end of the compressed data
  945. * without EOPM. So consequently liblzma will not return last
  946. * three or four bytes of uncompressed data because
  947. * LZMA_FILTER_X86 filter does not handle input data if its
  948. * data size is less than five bytes. If liblzma detect EOPM
  949. * or know the uncompressed data size, liblzma will flush out
  950. * the remaining that three or four bytes of uncompressed
  951. * data. That is why we have to use our converting program
  952. * for BCJ+LZMA. If we were able to tell the uncompressed
  953. * size to liblzma when using lzma_raw_decoder() liblzma
  954. * could correctly deal with BCJ+LZMA. But unfortunately
  955. * there is no way to do that.
  956. * Discussion about this can be found at XZ Utils forum.
  957. */
  958. if (coder2 != NULL) {
  959. zip->codec2 = coder2->codec;
  960. filters[fi].options = NULL;
  961. switch (zip->codec2) {
  962. case _7Z_X86:
  963. if (zip->codec == _7Z_LZMA2) {
  964. filters[fi].id = LZMA_FILTER_X86;
  965. fi++;
  966. } else
  967. /* Use our filter. */
  968. x86_Init(zip);
  969. break;
  970. case _7Z_X86_BCJ2:
  971. /* Use our filter. */
  972. zip->bcj_state = 0;
  973. break;
  974. case _7Z_DELTA:
  975. filters[fi].id = LZMA_FILTER_DELTA;
  976. memset(&delta_opt, 0, sizeof(delta_opt));
  977. delta_opt.type = LZMA_DELTA_TYPE_BYTE;
  978. delta_opt.dist = 1;
  979. filters[fi].options = &delta_opt;
  980. fi++;
  981. break;
  982. /* Following filters have not been tested yet. */
  983. case _7Z_POWERPC:
  984. filters[fi].id = LZMA_FILTER_POWERPC;
  985. fi++;
  986. break;
  987. case _7Z_IA64:
  988. filters[fi].id = LZMA_FILTER_IA64;
  989. fi++;
  990. break;
  991. case _7Z_ARM:
  992. filters[fi].id = LZMA_FILTER_ARM;
  993. fi++;
  994. break;
  995. case _7Z_ARMTHUMB:
  996. filters[fi].id = LZMA_FILTER_ARMTHUMB;
  997. fi++;
  998. break;
  999. case _7Z_SPARC:
  1000. filters[fi].id = LZMA_FILTER_SPARC;
  1001. fi++;
  1002. break;
  1003. default:
  1004. archive_set_error(&a->archive,
  1005. ARCHIVE_ERRNO_MISC,
  1006. "Unexpected codec ID: %lX", zip->codec2);
  1007. return (ARCHIVE_FAILED);
  1008. }
  1009. }
  1010. if (zip->codec == _7Z_LZMA2)
  1011. filters[fi].id = LZMA_FILTER_LZMA2;
  1012. else
  1013. filters[fi].id = LZMA_FILTER_LZMA1;
  1014. filters[fi].options = NULL;
  1015. ff = &filters[fi];
  1016. r = lzma_properties_decode(&filters[fi], NULL,
  1017. coder1->properties, (size_t)coder1->propertiesSize);
  1018. if (r != LZMA_OK) {
  1019. set_error(a, r);
  1020. return (ARCHIVE_FAILED);
  1021. }
  1022. fi++;
  1023. filters[fi].id = LZMA_VLI_UNKNOWN;
  1024. filters[fi].options = NULL;
  1025. r = lzma_raw_decoder(&(zip->lzstream), filters);
  1026. free(ff->options);
  1027. if (r != LZMA_OK) {
  1028. set_error(a, r);
  1029. return (ARCHIVE_FAILED);
  1030. }
  1031. zip->lzstream_valid = 1;
  1032. zip->lzstream.total_in = 0;
  1033. zip->lzstream.total_out = 0;
  1034. break;
  1035. }
  1036. #else
  1037. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1038. "LZMA codec is unsupported");
  1039. return (ARCHIVE_FAILED);
  1040. #endif
  1041. case _7Z_BZ2:
  1042. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1043. if (zip->bzstream_valid) {
  1044. BZ2_bzDecompressEnd(&(zip->bzstream));
  1045. zip->bzstream_valid = 0;
  1046. }
  1047. r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 0);
  1048. if (r == BZ_MEM_ERROR)
  1049. r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 1);
  1050. if (r != BZ_OK) {
  1051. int err = ARCHIVE_ERRNO_MISC;
  1052. const char *detail = NULL;
  1053. switch (r) {
  1054. case BZ_PARAM_ERROR:
  1055. detail = "invalid setup parameter";
  1056. break;
  1057. case BZ_MEM_ERROR:
  1058. err = ENOMEM;
  1059. detail = "out of memory";
  1060. break;
  1061. case BZ_CONFIG_ERROR:
  1062. detail = "mis-compiled library";
  1063. break;
  1064. }
  1065. archive_set_error(&a->archive, err,
  1066. "Internal error initializing decompressor: %s",
  1067. detail != NULL ? detail : "??");
  1068. zip->bzstream_valid = 0;
  1069. return (ARCHIVE_FAILED);
  1070. }
  1071. zip->bzstream_valid = 1;
  1072. zip->bzstream.total_in_lo32 = 0;
  1073. zip->bzstream.total_in_hi32 = 0;
  1074. zip->bzstream.total_out_lo32 = 0;
  1075. zip->bzstream.total_out_hi32 = 0;
  1076. break;
  1077. #else
  1078. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1079. "BZ2 codec is unsupported");
  1080. return (ARCHIVE_FAILED);
  1081. #endif
  1082. case _7Z_DEFLATE:
  1083. #ifdef HAVE_ZLIB_H
  1084. if (zip->stream_valid)
  1085. r = inflateReset(&(zip->stream));
  1086. else
  1087. r = inflateInit2(&(zip->stream),
  1088. -15 /* Don't check for zlib header */);
  1089. if (r != Z_OK) {
  1090. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1091. "Couldn't initialize zlib stream.");
  1092. return (ARCHIVE_FAILED);
  1093. }
  1094. zip->stream_valid = 1;
  1095. zip->stream.total_in = 0;
  1096. zip->stream.total_out = 0;
  1097. break;
  1098. #else
  1099. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1100. "DEFLATE codec is unsupported");
  1101. return (ARCHIVE_FAILED);
  1102. #endif
  1103. case _7Z_PPMD:
  1104. {
  1105. unsigned order;
  1106. uint32_t msize;
  1107. if (zip->ppmd7_valid) {
  1108. __archive_ppmd7_functions.Ppmd7_Free(
  1109. &zip->ppmd7_context);
  1110. zip->ppmd7_valid = 0;
  1111. }
  1112. if (coder1->propertiesSize < 5) {
  1113. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1114. "Malformed PPMd parameter");
  1115. return (ARCHIVE_FAILED);
  1116. }
  1117. order = coder1->properties[0];
  1118. msize = archive_le32dec(&(coder1->properties[1]));
  1119. if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER ||
  1120. msize < PPMD7_MIN_MEM_SIZE || msize > PPMD7_MAX_MEM_SIZE) {
  1121. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1122. "Malformed PPMd parameter");
  1123. return (ARCHIVE_FAILED);
  1124. }
  1125. __archive_ppmd7_functions.Ppmd7_Construct(&zip->ppmd7_context);
  1126. r = __archive_ppmd7_functions.Ppmd7_Alloc(
  1127. &zip->ppmd7_context, msize);
  1128. if (r == 0) {
  1129. archive_set_error(&a->archive, ENOMEM,
  1130. "Coludn't allocate memory for PPMd");
  1131. return (ARCHIVE_FATAL);
  1132. }
  1133. __archive_ppmd7_functions.Ppmd7_Init(
  1134. &zip->ppmd7_context, order);
  1135. __archive_ppmd7_functions.Ppmd7z_RangeDec_CreateVTable(
  1136. &zip->range_dec);
  1137. zip->ppmd7_valid = 1;
  1138. zip->ppmd7_stat = 0;
  1139. zip->ppstream.overconsumed = 0;
  1140. zip->ppstream.total_in = 0;
  1141. zip->ppstream.total_out = 0;
  1142. break;
  1143. }
  1144. case _7Z_X86:
  1145. case _7Z_X86_BCJ2:
  1146. case _7Z_POWERPC:
  1147. case _7Z_IA64:
  1148. case _7Z_ARM:
  1149. case _7Z_ARMTHUMB:
  1150. case _7Z_SPARC:
  1151. case _7Z_DELTA:
  1152. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1153. "Unexpected codec ID: %lX", zip->codec);
  1154. return (ARCHIVE_FAILED);
  1155. case _7Z_CRYPTO_MAIN_ZIP:
  1156. case _7Z_CRYPTO_RAR_29:
  1157. case _7Z_CRYPTO_AES_256_SHA_256:
  1158. if (a->entry) {
  1159. archive_entry_set_is_metadata_encrypted(a->entry, 1);
  1160. archive_entry_set_is_data_encrypted(a->entry, 1);
  1161. zip->has_encrypted_entries = 1;
  1162. }
  1163. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1164. "Crypto codec not supported yet (ID: 0x%lX)", zip->codec);
  1165. return (ARCHIVE_FAILED);
  1166. default:
  1167. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1168. "Unknown codec ID: %lX", zip->codec);
  1169. return (ARCHIVE_FAILED);
  1170. }
  1171. return (ARCHIVE_OK);
  1172. }
  1173. static int
  1174. decompress(struct archive_read *a, struct _7zip *zip,
  1175. void *buff, size_t *outbytes, const void *b, size_t *used)
  1176. {
  1177. const uint8_t *t_next_in;
  1178. uint8_t *t_next_out;
  1179. size_t o_avail_in, o_avail_out;
  1180. size_t t_avail_in, t_avail_out;
  1181. uint8_t *bcj2_next_out;
  1182. size_t bcj2_avail_out;
  1183. int r, ret = ARCHIVE_OK;
  1184. t_avail_in = o_avail_in = *used;
  1185. t_avail_out = o_avail_out = *outbytes;
  1186. t_next_in = b;
  1187. t_next_out = buff;
  1188. if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
  1189. int i;
  1190. /* Do not copy out the BCJ remaining bytes when the output
  1191. * buffer size is less than five bytes. */
  1192. if (o_avail_in != 0 && t_avail_out < 5 && zip->odd_bcj_size) {
  1193. *used = 0;
  1194. *outbytes = 0;
  1195. return (ret);
  1196. }
  1197. for (i = 0; zip->odd_bcj_size > 0 && t_avail_out; i++) {
  1198. *t_next_out++ = zip->odd_bcj[i];
  1199. t_avail_out--;
  1200. zip->odd_bcj_size--;
  1201. }
  1202. if (o_avail_in == 0 || t_avail_out == 0) {
  1203. *used = o_avail_in - t_avail_in;
  1204. *outbytes = o_avail_out - t_avail_out;
  1205. if (o_avail_in == 0)
  1206. ret = ARCHIVE_EOF;
  1207. return (ret);
  1208. }
  1209. }
  1210. bcj2_next_out = t_next_out;
  1211. bcj2_avail_out = t_avail_out;
  1212. if (zip->codec2 == _7Z_X86_BCJ2) {
  1213. /*
  1214. * Decord a remaining decompressed main stream for BCJ2.
  1215. */
  1216. if (zip->tmp_stream_bytes_remaining) {
  1217. ssize_t bytes;
  1218. size_t remaining = zip->tmp_stream_bytes_remaining;
  1219. bytes = Bcj2_Decode(zip, t_next_out, t_avail_out);
  1220. if (bytes < 0) {
  1221. archive_set_error(&(a->archive),
  1222. ARCHIVE_ERRNO_MISC,
  1223. "BCJ2 conversion Failed");
  1224. return (ARCHIVE_FAILED);
  1225. }
  1226. zip->main_stream_bytes_remaining -=
  1227. remaining - zip->tmp_stream_bytes_remaining;
  1228. t_avail_out -= bytes;
  1229. if (o_avail_in == 0 || t_avail_out == 0) {
  1230. *used = 0;
  1231. *outbytes = o_avail_out - t_avail_out;
  1232. if (o_avail_in == 0 &&
  1233. zip->tmp_stream_bytes_remaining)
  1234. ret = ARCHIVE_EOF;
  1235. return (ret);
  1236. }
  1237. t_next_out += bytes;
  1238. bcj2_next_out = t_next_out;
  1239. bcj2_avail_out = t_avail_out;
  1240. }
  1241. t_next_out = zip->tmp_stream_buff;
  1242. t_avail_out = zip->tmp_stream_buff_size;
  1243. }
  1244. switch (zip->codec) {
  1245. case _7Z_COPY:
  1246. {
  1247. size_t bytes =
  1248. (t_avail_in > t_avail_out)?t_avail_out:t_avail_in;
  1249. memcpy(t_next_out, t_next_in, bytes);
  1250. t_avail_in -= bytes;
  1251. t_avail_out -= bytes;
  1252. if (o_avail_in == 0)
  1253. ret = ARCHIVE_EOF;
  1254. break;
  1255. }
  1256. #ifdef HAVE_LZMA_H
  1257. case _7Z_LZMA: case _7Z_LZMA2:
  1258. zip->lzstream.next_in = t_next_in;
  1259. zip->lzstream.avail_in = t_avail_in;
  1260. zip->lzstream.next_out = t_next_out;
  1261. zip->lzstream.avail_out = t_avail_out;
  1262. r = lzma_code(&(zip->lzstream), LZMA_RUN);
  1263. switch (r) {
  1264. case LZMA_STREAM_END: /* Found end of stream. */
  1265. lzma_end(&(zip->lzstream));
  1266. zip->lzstream_valid = 0;
  1267. ret = ARCHIVE_EOF;
  1268. break;
  1269. case LZMA_OK: /* Decompressor made some progress. */
  1270. break;
  1271. default:
  1272. archive_set_error(&(a->archive),
  1273. ARCHIVE_ERRNO_MISC,
  1274. "Decompression failed(%d)",
  1275. r);
  1276. return (ARCHIVE_FAILED);
  1277. }
  1278. t_avail_in = zip->lzstream.avail_in;
  1279. t_avail_out = zip->lzstream.avail_out;
  1280. break;
  1281. #endif
  1282. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1283. case _7Z_BZ2:
  1284. zip->bzstream.next_in = (char *)(uintptr_t)t_next_in;
  1285. zip->bzstream.avail_in = t_avail_in;
  1286. zip->bzstream.next_out = (char *)(uintptr_t)t_next_out;
  1287. zip->bzstream.avail_out = t_avail_out;
  1288. r = BZ2_bzDecompress(&(zip->bzstream));
  1289. switch (r) {
  1290. case BZ_STREAM_END: /* Found end of stream. */
  1291. switch (BZ2_bzDecompressEnd(&(zip->bzstream))) {
  1292. case BZ_OK:
  1293. break;
  1294. default:
  1295. archive_set_error(&(a->archive),
  1296. ARCHIVE_ERRNO_MISC,
  1297. "Failed to clean up decompressor");
  1298. return (ARCHIVE_FAILED);
  1299. }
  1300. zip->bzstream_valid = 0;
  1301. ret = ARCHIVE_EOF;
  1302. break;
  1303. case BZ_OK: /* Decompressor made some progress. */
  1304. break;
  1305. default:
  1306. archive_set_error(&(a->archive),
  1307. ARCHIVE_ERRNO_MISC,
  1308. "bzip decompression failed");
  1309. return (ARCHIVE_FAILED);
  1310. }
  1311. t_avail_in = zip->bzstream.avail_in;
  1312. t_avail_out = zip->bzstream.avail_out;
  1313. break;
  1314. #endif
  1315. #ifdef HAVE_ZLIB_H
  1316. case _7Z_DEFLATE:
  1317. zip->stream.next_in = (Bytef *)(uintptr_t)t_next_in;
  1318. zip->stream.avail_in = (uInt)t_avail_in;
  1319. zip->stream.next_out = t_next_out;
  1320. zip->stream.avail_out = (uInt)t_avail_out;
  1321. r = inflate(&(zip->stream), 0);
  1322. switch (r) {
  1323. case Z_STREAM_END: /* Found end of stream. */
  1324. ret = ARCHIVE_EOF;
  1325. break;
  1326. case Z_OK: /* Decompressor made some progress.*/
  1327. break;
  1328. default:
  1329. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1330. "File decompression failed (%d)", r);
  1331. return (ARCHIVE_FAILED);
  1332. }
  1333. t_avail_in = zip->stream.avail_in;
  1334. t_avail_out = zip->stream.avail_out;
  1335. break;
  1336. #endif
  1337. case _7Z_PPMD:
  1338. {
  1339. uint64_t flush_bytes;
  1340. if (!zip->ppmd7_valid || zip->ppmd7_stat < 0 ||
  1341. t_avail_out <= 0) {
  1342. archive_set_error(&(a->archive),
  1343. ARCHIVE_ERRNO_MISC,
  1344. "Decompression internal error");
  1345. return (ARCHIVE_FAILED);
  1346. }
  1347. zip->ppstream.next_in = t_next_in;
  1348. zip->ppstream.avail_in = t_avail_in;
  1349. zip->ppstream.next_out = t_next_out;
  1350. zip->ppstream.avail_out = t_avail_out;
  1351. if (zip->ppmd7_stat == 0) {
  1352. zip->bytein.a = a;
  1353. zip->bytein.Read = &ppmd_read;
  1354. zip->range_dec.Stream = &zip->bytein;
  1355. r = __archive_ppmd7_functions.Ppmd7z_RangeDec_Init(
  1356. &(zip->range_dec));
  1357. if (r == 0) {
  1358. zip->ppmd7_stat = -1;
  1359. archive_set_error(&a->archive,
  1360. ARCHIVE_ERRNO_MISC,
  1361. "Failed to initialize PPMd range decorder");
  1362. return (ARCHIVE_FAILED);
  1363. }
  1364. if (zip->ppstream.overconsumed) {
  1365. zip->ppmd7_stat = -1;
  1366. return (ARCHIVE_FAILED);
  1367. }
  1368. zip->ppmd7_stat = 1;
  1369. }
  1370. if (t_avail_in == 0)
  1371. /* XXX Flush out remaining decoded data XXX */
  1372. flush_bytes = zip->folder_outbytes_remaining;
  1373. else
  1374. flush_bytes = 0;
  1375. do {
  1376. int sym;
  1377. sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
  1378. &(zip->ppmd7_context), &(zip->range_dec.p));
  1379. if (sym < 0) {
  1380. zip->ppmd7_stat = -1;
  1381. archive_set_error(&a->archive,
  1382. ARCHIVE_ERRNO_FILE_FORMAT,
  1383. "Failed to decode PPMd");
  1384. return (ARCHIVE_FAILED);
  1385. }
  1386. if (zip->ppstream.overconsumed) {
  1387. zip->ppmd7_stat = -1;
  1388. return (ARCHIVE_FAILED);
  1389. }
  1390. *zip->ppstream.next_out++ = (unsigned char)sym;
  1391. zip->ppstream.avail_out--;
  1392. zip->ppstream.total_out++;
  1393. if (flush_bytes)
  1394. flush_bytes--;
  1395. } while (zip->ppstream.avail_out &&
  1396. (zip->ppstream.avail_in || flush_bytes));
  1397. t_avail_in = (size_t)zip->ppstream.avail_in;
  1398. t_avail_out = (size_t)zip->ppstream.avail_out;
  1399. break;
  1400. }
  1401. default:
  1402. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  1403. "Decompression internal error");
  1404. return (ARCHIVE_FAILED);
  1405. }
  1406. if (ret != ARCHIVE_OK && ret != ARCHIVE_EOF)
  1407. return (ret);
  1408. *used = o_avail_in - t_avail_in;
  1409. *outbytes = o_avail_out - t_avail_out;
  1410. /*
  1411. * Decord BCJ.
  1412. */
  1413. if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
  1414. size_t l = x86_Convert(zip, buff, *outbytes);
  1415. zip->odd_bcj_size = *outbytes - l;
  1416. if (zip->odd_bcj_size > 0 && zip->odd_bcj_size <= 4 &&
  1417. o_avail_in && ret != ARCHIVE_EOF) {
  1418. memcpy(zip->odd_bcj, ((unsigned char *)buff) + l,
  1419. zip->odd_bcj_size);
  1420. *outbytes = l;
  1421. } else
  1422. zip->odd_bcj_size = 0;
  1423. }
  1424. /*
  1425. * Decord BCJ2 with a decompressed main stream.
  1426. */
  1427. if (zip->codec2 == _7Z_X86_BCJ2) {
  1428. ssize_t bytes;
  1429. zip->tmp_stream_bytes_avail =
  1430. zip->tmp_stream_buff_size - t_avail_out;
  1431. if (zip->tmp_stream_bytes_avail >
  1432. zip->main_stream_bytes_remaining)
  1433. zip->tmp_stream_bytes_avail =
  1434. zip->main_stream_bytes_remaining;
  1435. zip->tmp_stream_bytes_remaining = zip->tmp_stream_bytes_avail;
  1436. bytes = Bcj2_Decode(zip, bcj2_next_out, bcj2_avail_out);
  1437. if (bytes < 0) {
  1438. archive_set_error(&(a->archive),
  1439. ARCHIVE_ERRNO_MISC, "BCJ2 conversion Failed");
  1440. return (ARCHIVE_FAILED);
  1441. }
  1442. zip->main_stream_bytes_remaining -=
  1443. zip->tmp_stream_bytes_avail
  1444. - zip->tmp_stream_bytes_remaining;
  1445. bcj2_avail_out -= bytes;
  1446. *outbytes = o_avail_out - bcj2_avail_out;
  1447. }
  1448. return (ret);
  1449. }
  1450. static int
  1451. free_decompression(struct archive_read *a, struct _7zip *zip)
  1452. {
  1453. int r = ARCHIVE_OK;
  1454. #if !defined(HAVE_ZLIB_H) &&\
  1455. !(defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR))
  1456. (void)a;/* UNUSED */
  1457. #endif
  1458. #ifdef HAVE_LZMA_H
  1459. if (zip->lzstream_valid)
  1460. lzma_end(&(zip->lzstream));
  1461. #endif
  1462. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1463. if (zip->bzstream_valid) {
  1464. if (BZ2_bzDecompressEnd(&(zip->bzstream)) != BZ_OK) {
  1465. archive_set_error(&a->archive,
  1466. ARCHIVE_ERRNO_MISC,
  1467. "Failed to clean up bzip2 decompressor");
  1468. r = ARCHIVE_FATAL;
  1469. }
  1470. zip->bzstream_valid = 0;
  1471. }
  1472. #endif
  1473. #ifdef HAVE_ZLIB_H
  1474. if (zip->stream_valid) {
  1475. if (inflateEnd(&(zip->stream)) != Z_OK) {
  1476. archive_set_error(&a->archive,
  1477. ARCHIVE_ERRNO_MISC,
  1478. "Failed to clean up zlib decompressor");
  1479. r = ARCHIVE_FATAL;
  1480. }
  1481. zip->stream_valid = 0;
  1482. }
  1483. #endif
  1484. if (zip->ppmd7_valid) {
  1485. __archive_ppmd7_functions.Ppmd7_Free(
  1486. &zip->ppmd7_context);
  1487. zip->ppmd7_valid = 0;
  1488. }
  1489. return (r);
  1490. }
  1491. static int
  1492. parse_7zip_uint64(struct archive_read *a, uint64_t *val)
  1493. {
  1494. const unsigned char *p;
  1495. unsigned char avail, mask;
  1496. int i;
  1497. if ((p = header_bytes(a, 1)) == NULL)
  1498. return (-1);
  1499. avail = *p;
  1500. mask = 0x80;
  1501. *val = 0;
  1502. for (i = 0; i < 8; i++) {
  1503. if (avail & mask) {
  1504. if ((p = header_bytes(a, 1)) == NULL)
  1505. return (-1);
  1506. *val |= ((uint64_t)*p) << (8 * i);
  1507. mask >>= 1;
  1508. continue;
  1509. }
  1510. *val += ((uint64_t)(avail & (mask -1))) << (8 * i);
  1511. break;
  1512. }
  1513. return (0);
  1514. }
  1515. static int
  1516. read_Bools(struct archive_read *a, unsigned char *data, size_t num)
  1517. {
  1518. const unsigned char *p;
  1519. unsigned i, mask = 0, avail = 0;
  1520. for (i = 0; i < num; i++) {
  1521. if (mask == 0) {
  1522. if ((p = header_bytes(a, 1)) == NULL)
  1523. return (-1);
  1524. avail = *p;
  1525. mask = 0x80;
  1526. }
  1527. data[i] = (avail & mask)?1:0;
  1528. mask >>= 1;
  1529. }
  1530. return (0);
  1531. }
  1532. static void
  1533. free_Digest(struct _7z_digests *d)
  1534. {
  1535. free(d->defineds);
  1536. free(d->digests);
  1537. }
  1538. static int
  1539. read_Digests(struct archive_read *a, struct _7z_digests *d, size_t num)
  1540. {
  1541. const unsigned char *p;
  1542. unsigned i;
  1543. if (num == 0)
  1544. return (-1);
  1545. memset(d, 0, sizeof(*d));
  1546. d->defineds = malloc(num);
  1547. if (d->defineds == NULL)
  1548. return (-1);
  1549. /*
  1550. * Read Bools.
  1551. */
  1552. if ((p = header_bytes(a, 1)) == NULL)
  1553. return (-1);
  1554. if (*p == 0) {
  1555. if (read_Bools(a, d->defineds, num) < 0)
  1556. return (-1);
  1557. } else
  1558. /* All are defined */
  1559. memset(d->defineds, 1, num);
  1560. d->digests = calloc(num, sizeof(*d->digests));
  1561. if (d->digests == NULL)
  1562. return (-1);
  1563. for (i = 0; i < num; i++) {
  1564. if (d->defineds[i]) {
  1565. if ((p = header_bytes(a, 4)) == NULL)
  1566. return (-1);
  1567. d->digests[i] = archive_le32dec(p);
  1568. }
  1569. }
  1570. return (0);
  1571. }
  1572. static void
  1573. free_PackInfo(struct _7z_pack_info *pi)
  1574. {
  1575. free(pi->sizes);
  1576. free(pi->positions);
  1577. free_Digest(&(pi->digest));
  1578. }
  1579. static int
  1580. read_PackInfo(struct archive_read *a, struct _7z_pack_info *pi)
  1581. {
  1582. const unsigned char *p;
  1583. unsigned i;
  1584. memset(pi, 0, sizeof(*pi));
  1585. /*
  1586. * Read PackPos.
  1587. */
  1588. if (parse_7zip_uint64(a, &(pi->pos)) < 0)
  1589. return (-1);
  1590. /*
  1591. * Read NumPackStreams.
  1592. */
  1593. if (parse_7zip_uint64(a, &(pi->numPackStreams)) < 0)
  1594. return (-1);
  1595. if (pi->numPackStreams == 0)
  1596. return (-1);
  1597. if (UMAX_ENTRY < pi->numPackStreams)
  1598. return (-1);
  1599. /*
  1600. * Read PackSizes[num]
  1601. */
  1602. if ((p = header_bytes(a, 1)) == NULL)
  1603. return (-1);
  1604. if (*p == kEnd)
  1605. /* PackSizes[num] are not present. */
  1606. return (0);
  1607. if (*p != kSize)
  1608. return (-1);
  1609. pi->sizes = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
  1610. pi->positions = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
  1611. if (pi->sizes == NULL || pi->positions == NULL)
  1612. return (-1);
  1613. for (i = 0; i < pi->numPackStreams; i++) {
  1614. if (parse_7zip_uint64(a, &(pi->sizes[i])) < 0)
  1615. return (-1);
  1616. }
  1617. /*
  1618. * Read PackStreamDigests[num]
  1619. */
  1620. if ((p = header_bytes(a, 1)) == NULL)
  1621. return (-1);
  1622. if (*p == kEnd) {
  1623. /* PackStreamDigests[num] are not present. */
  1624. pi->digest.defineds =
  1625. calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.defineds));
  1626. pi->digest.digests =
  1627. calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.digests));
  1628. if (pi->digest.defineds == NULL || pi->digest.digests == NULL)
  1629. return (-1);
  1630. return (0);
  1631. }
  1632. if (*p != kCRC)
  1633. return (-1);
  1634. if (read_Digests(a, &(pi->digest), (size_t)pi->numPackStreams) < 0)
  1635. return (-1);
  1636. /*
  1637. * Must be marked by kEnd.
  1638. */
  1639. if ((p = header_bytes(a, 1)) == NULL)
  1640. return (-1);
  1641. if (*p != kEnd)
  1642. return (-1);
  1643. return (0);
  1644. }
  1645. static void
  1646. free_Folder(struct _7z_folder *f)
  1647. {
  1648. unsigned i;
  1649. if (f->coders) {
  1650. for (i = 0; i< f->numCoders; i++) {
  1651. free(f->coders[i].properties);
  1652. }
  1653. free(f->coders);
  1654. }
  1655. free(f->bindPairs);
  1656. free(f->packedStreams);
  1657. free(f->unPackSize);
  1658. }
  1659. static int
  1660. read_Folder(struct archive_read *a, struct _7z_folder *f)
  1661. {
  1662. struct _7zip *zip = (struct _7zip *)a->format->data;
  1663. const unsigned char *p;
  1664. uint64_t numInStreamsTotal = 0;
  1665. uint64_t numOutStreamsTotal = 0;
  1666. unsigned i;
  1667. memset(f, 0, sizeof(*f));
  1668. /*
  1669. * Read NumCoders.
  1670. */
  1671. if (parse_7zip_uint64(a, &(f->numCoders)) < 0)
  1672. return (-1);
  1673. if (f->numCoders > 4)
  1674. /* Too many coders. */
  1675. return (-1);
  1676. f->coders = calloc((size_t)f->numCoders, sizeof(*f->coders));
  1677. if (f->coders == NULL)
  1678. return (-1);
  1679. for (i = 0; i< f->numCoders; i++) {
  1680. size_t codec_size;
  1681. int simple, attr;
  1682. if ((p = header_bytes(a, 1)) == NULL)
  1683. return (-1);
  1684. /*
  1685. * 0:3 CodecIdSize
  1686. * 4: 0 - IsSimple
  1687. * 1 - Is not Simple
  1688. * 5: 0 - No Attributes
  1689. * 1 - There are Attributes;
  1690. * 7: Must be zero.
  1691. */
  1692. codec_size = *p & 0xf;
  1693. simple = (*p & 0x10)?0:1;
  1694. attr = *p & 0x20;
  1695. if (*p & 0x80)
  1696. return (-1);/* Not supported. */
  1697. /*
  1698. * Read Decompression Method IDs.
  1699. */
  1700. if ((p = header_bytes(a, codec_size)) == NULL)
  1701. return (-1);
  1702. f->coders[i].codec = decode_codec_id(p, codec_size);
  1703. if (simple) {
  1704. f->coders[i].numInStreams = 1;
  1705. f->coders[i].numOutStreams = 1;
  1706. } else {
  1707. if (parse_7zip_uint64(
  1708. a, &(f->coders[i].numInStreams)) < 0)
  1709. return (-1);
  1710. if (UMAX_ENTRY < f->coders[i].numInStreams)
  1711. return (-1);
  1712. if (parse_7zip_uint64(
  1713. a, &(f->coders[i].numOutStreams)) < 0)
  1714. return (-1);
  1715. if (UMAX_ENTRY < f->coders[i].numOutStreams)
  1716. return (-1);
  1717. }
  1718. if (attr) {
  1719. if (parse_7zip_uint64(
  1720. a, &(f->coders[i].propertiesSize)) < 0)
  1721. return (-1);
  1722. if ((p = header_bytes(
  1723. a, (size_t)f->coders[i].propertiesSize)) == NULL)
  1724. return (-1);
  1725. f->coders[i].properties =
  1726. malloc((size_t)f->coders[i].propertiesSize);
  1727. if (f->coders[i].properties == NULL)
  1728. return (-1);
  1729. memcpy(f->coders[i].properties, p,
  1730. (size_t)f->coders[i].propertiesSize);
  1731. }
  1732. numInStreamsTotal += f->coders[i].numInStreams;
  1733. numOutStreamsTotal += f->coders[i].numOutStreams;
  1734. }
  1735. if (numOutStreamsTotal == 0 ||
  1736. numInStreamsTotal < numOutStreamsTotal-1)
  1737. return (-1);
  1738. f->numBindPairs = numOutStreamsTotal - 1;
  1739. if (zip->header_bytes_remaining < f->numBindPairs)
  1740. return (-1);
  1741. if (f->numBindPairs > 0) {
  1742. f->bindPairs =
  1743. calloc((size_t)f->numBindPairs, sizeof(*f->bindPairs));
  1744. if (f->bindPairs == NULL)
  1745. return (-1);
  1746. } else
  1747. f->bindPairs = NULL;
  1748. for (i = 0; i < f->numBindPairs; i++) {
  1749. if (parse_7zip_uint64(a, &(f->bindPairs[i].inIndex)) < 0)
  1750. return (-1);
  1751. if (UMAX_ENTRY < f->bindPairs[i].inIndex)
  1752. return (-1);
  1753. if (parse_7zip_uint64(a, &(f->bindPairs[i].outIndex)) < 0)
  1754. return (-1);
  1755. if (UMAX_ENTRY < f->bindPairs[i].outIndex)
  1756. return (-1);
  1757. }
  1758. f->numPackedStreams = numInStreamsTotal - f->numBindPairs;
  1759. f->packedStreams =
  1760. calloc((size_t)f->numPackedStreams, sizeof(*f->packedStreams));
  1761. if (f->packedStreams == NULL)
  1762. return (-1);
  1763. if (f->numPackedStreams == 1) {
  1764. for (i = 0; i < numInStreamsTotal; i++) {
  1765. unsigned j;
  1766. for (j = 0; j < f->numBindPairs; j++) {
  1767. if (f->bindPairs[j].inIndex == i)
  1768. break;
  1769. }
  1770. if (j == f->numBindPairs)
  1771. break;
  1772. }
  1773. if (i == numInStreamsTotal)
  1774. return (-1);
  1775. f->packedStreams[0] = i;
  1776. } else {
  1777. for (i = 0; i < f->numPackedStreams; i++) {
  1778. if (parse_7zip_uint64(a, &(f->packedStreams[i])) < 0)
  1779. return (-1);
  1780. if (UMAX_ENTRY < f->packedStreams[i])
  1781. return (-1);
  1782. }
  1783. }
  1784. f->numInStreams = numInStreamsTotal;
  1785. f->numOutStreams = numOutStreamsTotal;
  1786. return (0);
  1787. }
  1788. static void
  1789. free_CodersInfo(struct _7z_coders_info *ci)
  1790. {
  1791. unsigned i;
  1792. if (ci->folders) {
  1793. for (i = 0; i < ci->numFolders; i++)
  1794. free_Folder(&(ci->folders[i]));
  1795. free(ci->folders);
  1796. }
  1797. }
  1798. static int
  1799. read_CodersInfo(struct archive_read *a, struct _7z_coders_info *ci)
  1800. {
  1801. const unsigned char *p;
  1802. struct _7z_digests digest;
  1803. unsigned i;
  1804. memset(ci, 0, sizeof(*ci));
  1805. memset(&digest, 0, sizeof(digest));
  1806. if ((p = header_bytes(a, 1)) == NULL)
  1807. goto failed;
  1808. if (*p != kFolder)
  1809. goto failed;
  1810. /*
  1811. * Read NumFolders.
  1812. */
  1813. if (parse_7zip_uint64(a, &(ci->numFolders)) < 0)
  1814. goto failed;
  1815. if (UMAX_ENTRY < ci->numFolders)
  1816. return (-1);
  1817. /*
  1818. * Read External.
  1819. */
  1820. if ((p = header_bytes(a, 1)) == NULL)
  1821. goto failed;
  1822. switch (*p) {
  1823. case 0:
  1824. ci->folders =
  1825. calloc((size_t)ci->numFolders, sizeof(*ci->folders));
  1826. if (ci->folders == NULL)
  1827. return (-1);
  1828. for (i = 0; i < ci->numFolders; i++) {
  1829. if (read_Folder(a, &(ci->folders[i])) < 0)
  1830. goto failed;
  1831. }
  1832. break;
  1833. case 1:
  1834. if (parse_7zip_uint64(a, &(ci->dataStreamIndex)) < 0)
  1835. return (-1);
  1836. if (UMAX_ENTRY < ci->dataStreamIndex)
  1837. return (-1);
  1838. if (ci->numFolders > 0) {
  1839. archive_set_error(&a->archive, -1,
  1840. "Malformed 7-Zip archive");
  1841. goto failed;
  1842. }
  1843. break;
  1844. default:
  1845. archive_set_error(&a->archive, -1,
  1846. "Malformed 7-Zip archive");
  1847. goto failed;
  1848. }
  1849. if ((p = header_bytes(a, 1)) == NULL)
  1850. goto failed;
  1851. if (*p != kCodersUnPackSize)
  1852. goto failed;
  1853. for (i = 0; i < ci->numFolders; i++) {
  1854. struct _7z_folder *folder = &(ci->folders[i]);
  1855. unsigned j;
  1856. folder->unPackSize =
  1857. calloc((size_t)folder->numOutStreams, sizeof(*folder->unPackSize));
  1858. if (folder->unPackSize == NULL)
  1859. goto failed;
  1860. for (j = 0; j < folder->numOutStreams; j++) {
  1861. if (parse_7zip_uint64(a, &(folder->unPackSize[j])) < 0)
  1862. goto failed;
  1863. }
  1864. }
  1865. /*
  1866. * Read CRCs.
  1867. */
  1868. if ((p = header_bytes(a, 1)) == NULL)
  1869. goto failed;
  1870. if (*p == kEnd)
  1871. return (0);
  1872. if (*p != kCRC)
  1873. goto failed;
  1874. if (read_Digests(a, &digest, (size_t)ci->numFolders) < 0)
  1875. goto failed;
  1876. for (i = 0; i < ci->numFolders; i++) {
  1877. ci->folders[i].digest_defined = digest.defineds[i];
  1878. ci->folders[i].digest = digest.digests[i];
  1879. }
  1880. /*
  1881. * Must be kEnd.
  1882. */
  1883. if ((p = header_bytes(a, 1)) == NULL)
  1884. goto failed;
  1885. if (*p != kEnd)
  1886. goto failed;
  1887. free_Digest(&digest);
  1888. return (0);
  1889. failed:
  1890. free_Digest(&digest);
  1891. return (-1);
  1892. }
  1893. static uint64_t
  1894. folder_uncompressed_size(struct _7z_folder *f)
  1895. {
  1896. int n = (int)f->numOutStreams;
  1897. unsigned pairs = (unsigned)f->numBindPairs;
  1898. while (--n >= 0) {
  1899. unsigned i;
  1900. for (i = 0; i < pairs; i++) {
  1901. if (f->bindPairs[i].outIndex == (uint64_t)n)
  1902. break;
  1903. }
  1904. if (i >= pairs)
  1905. return (f->unPackSize[n]);
  1906. }
  1907. return (0);
  1908. }
  1909. static void
  1910. free_SubStreamsInfo(struct _7z_substream_info *ss)
  1911. {
  1912. free(ss->unpackSizes);
  1913. free(ss->digestsDefined);
  1914. free(ss->digests);
  1915. }
  1916. static int
  1917. read_SubStreamsInfo(struct archive_read *a, struct _7z_substream_info *ss,
  1918. struct _7z_folder *f, size_t numFolders)
  1919. {
  1920. const unsigned char *p;
  1921. uint64_t *usizes;
  1922. size_t unpack_streams;
  1923. int type;
  1924. unsigned i;
  1925. uint32_t numDigests;
  1926. memset(ss, 0, sizeof(*ss));
  1927. for (i = 0; i < numFolders; i++)
  1928. f[i].numUnpackStreams = 1;
  1929. if ((p = header_bytes(a, 1)) == NULL)
  1930. return (-1);
  1931. type = *p;
  1932. if (type == kNumUnPackStream) {
  1933. unpack_streams = 0;
  1934. for (i = 0; i < numFolders; i++) {
  1935. if (parse_7zip_uint64(a, &(f[i].numUnpackStreams)) < 0)
  1936. return (-1);
  1937. if (UMAX_ENTRY < f[i].numUnpackStreams)
  1938. return (-1);
  1939. if (unpack_streams > SIZE_MAX - UMAX_ENTRY) {
  1940. return (-1);
  1941. }
  1942. unpack_streams += (size_t)f[i].numUnpackStreams;
  1943. }
  1944. if ((p = header_bytes(a, 1)) == NULL)
  1945. return (-1);
  1946. type = *p;
  1947. } else
  1948. unpack_streams = numFolders;
  1949. ss->unpack_streams = unpack_streams;
  1950. if (unpack_streams) {
  1951. ss->unpackSizes = calloc(unpack_streams,
  1952. sizeof(*ss->unpackSizes));
  1953. ss->digestsDefined = calloc(unpack_streams,
  1954. sizeof(*ss->digestsDefined));
  1955. ss->digests = calloc(unpack_streams,
  1956. sizeof(*ss->digests));
  1957. if (ss->unpackSizes == NULL || ss->digestsDefined == NULL ||
  1958. ss->digests == NULL)
  1959. return (-1);
  1960. }
  1961. usizes = ss->unpackSizes;
  1962. for (i = 0; i < numFolders; i++) {
  1963. unsigned pack;
  1964. uint64_t sum;
  1965. if (f[i].numUnpackStreams == 0)
  1966. continue;
  1967. sum = 0;
  1968. if (type == kSize) {
  1969. for (pack = 1; pack < f[i].numUnpackStreams; pack++) {
  1970. if (parse_7zip_uint64(a, usizes) < 0)
  1971. return (-1);
  1972. sum += *usizes++;
  1973. }
  1974. }
  1975. *usizes++ = folder_uncompressed_size(&f[i]) - sum;
  1976. }
  1977. if (type == kSize) {
  1978. if ((p = header_bytes(a, 1)) == NULL)
  1979. return (-1);
  1980. type = *p;
  1981. }
  1982. for (i = 0; i < unpack_streams; i++) {
  1983. ss->digestsDefined[i] = 0;
  1984. ss->digests[i] = 0;
  1985. }
  1986. numDigests = 0;
  1987. for (i = 0; i < numFolders; i++) {
  1988. if (f[i].numUnpackStreams != 1 || !f[i].digest_defined)
  1989. numDigests += (uint32_t)f[i].numUnpackStreams;
  1990. }
  1991. if (type == kCRC) {
  1992. struct _7z_digests tmpDigests;
  1993. unsigned char *digestsDefined = ss->digestsDefined;
  1994. uint32_t * digests = ss->digests;
  1995. int di = 0;
  1996. memset(&tmpDigests, 0, sizeof(tmpDigests));
  1997. if (read_Digests(a, &(tmpDigests), numDigests) < 0) {
  1998. free_Digest(&tmpDigests);
  1999. return (-1);
  2000. }
  2001. for (i = 0; i < numFolders; i++) {
  2002. if (f[i].numUnpackStreams == 1 && f[i].digest_defined) {
  2003. *digestsDefined++ = 1;
  2004. *digests++ = f[i].digest;
  2005. } else {
  2006. unsigned j;
  2007. for (j = 0; j < f[i].numUnpackStreams;
  2008. j++, di++) {
  2009. *digestsDefined++ =
  2010. tmpDigests.defineds[di];
  2011. *digests++ =
  2012. tmpDigests.digests[di];
  2013. }
  2014. }
  2015. }
  2016. free_Digest(&tmpDigests);
  2017. if ((p = header_bytes(a, 1)) == NULL)
  2018. return (-1);
  2019. type = *p;
  2020. }
  2021. /*
  2022. * Must be kEnd.
  2023. */
  2024. if (type != kEnd)
  2025. return (-1);
  2026. return (0);
  2027. }
  2028. static void
  2029. free_StreamsInfo(struct _7z_stream_info *si)
  2030. {
  2031. free_PackInfo(&(si->pi));
  2032. free_CodersInfo(&(si->ci));
  2033. free_SubStreamsInfo(&(si->ss));
  2034. }
  2035. static int
  2036. read_StreamsInfo(struct archive_read *a, struct _7z_stream_info *si)
  2037. {
  2038. struct _7zip *zip = (struct _7zip *)a->format->data;
  2039. const unsigned char *p;
  2040. unsigned i;
  2041. memset(si, 0, sizeof(*si));
  2042. if ((p = header_bytes(a, 1)) == NULL)
  2043. return (-1);
  2044. if (*p == kPackInfo) {
  2045. uint64_t packPos;
  2046. if (read_PackInfo(a, &(si->pi)) < 0)
  2047. return (-1);
  2048. if (si->pi.positions == NULL || si->pi.sizes == NULL)
  2049. return (-1);
  2050. /*
  2051. * Calculate packed stream positions.
  2052. */
  2053. packPos = si->pi.pos;
  2054. for (i = 0; i < si->pi.numPackStreams; i++) {
  2055. si->pi.positions[i] = packPos;
  2056. packPos += si->pi.sizes[i];
  2057. if (packPos > zip->header_offset)
  2058. return (-1);
  2059. }
  2060. if ((p = header_bytes(a, 1)) == NULL)
  2061. return (-1);
  2062. }
  2063. if (*p == kUnPackInfo) {
  2064. uint32_t packIndex;
  2065. struct _7z_folder *f;
  2066. if (read_CodersInfo(a, &(si->ci)) < 0)
  2067. return (-1);
  2068. /*
  2069. * Calculate packed stream indexes.
  2070. */
  2071. packIndex = 0;
  2072. f = si->ci.folders;
  2073. for (i = 0; i < si->ci.numFolders; i++) {
  2074. f[i].packIndex = packIndex;
  2075. packIndex += (uint32_t)f[i].numPackedStreams;
  2076. if (packIndex > si->pi.numPackStreams)
  2077. return (-1);
  2078. }
  2079. if ((p = header_bytes(a, 1)) == NULL)
  2080. return (-1);
  2081. }
  2082. if (*p == kSubStreamsInfo) {
  2083. if (read_SubStreamsInfo(a, &(si->ss),
  2084. si->ci.folders, (size_t)si->ci.numFolders) < 0)
  2085. return (-1);
  2086. if ((p = header_bytes(a, 1)) == NULL)
  2087. return (-1);
  2088. }
  2089. /*
  2090. * Must be kEnd.
  2091. */
  2092. if (*p != kEnd)
  2093. return (-1);
  2094. return (0);
  2095. }
  2096. static void
  2097. free_Header(struct _7z_header_info *h)
  2098. {
  2099. free(h->emptyStreamBools);
  2100. free(h->emptyFileBools);
  2101. free(h->antiBools);
  2102. free(h->attrBools);
  2103. }
  2104. static int
  2105. read_Header(struct archive_read *a, struct _7z_header_info *h,
  2106. int check_header_id)
  2107. {
  2108. struct _7zip *zip = (struct _7zip *)a->format->data;
  2109. const unsigned char *p;
  2110. struct _7z_folder *folders;
  2111. struct _7z_stream_info *si = &(zip->si);
  2112. struct _7zip_entry *entries;
  2113. uint32_t folderIndex, indexInFolder;
  2114. unsigned i;
  2115. int eindex, empty_streams, sindex;
  2116. if (check_header_id) {
  2117. /*
  2118. * Read Header.
  2119. */
  2120. if ((p = header_bytes(a, 1)) == NULL)
  2121. return (-1);
  2122. if (*p != kHeader)
  2123. return (-1);
  2124. }
  2125. /*
  2126. * Read ArchiveProperties.
  2127. */
  2128. if ((p = header_bytes(a, 1)) == NULL)
  2129. return (-1);
  2130. if (*p == kArchiveProperties) {
  2131. for (;;) {
  2132. uint64_t size;
  2133. if ((p = header_bytes(a, 1)) == NULL)
  2134. return (-1);
  2135. if (*p == 0)
  2136. break;
  2137. if (parse_7zip_uint64(a, &size) < 0)
  2138. return (-1);
  2139. }
  2140. if ((p = header_bytes(a, 1)) == NULL)
  2141. return (-1);
  2142. }
  2143. /*
  2144. * Read MainStreamsInfo.
  2145. */
  2146. if (*p == kMainStreamsInfo) {
  2147. if (read_StreamsInfo(a, &(zip->si)) < 0)
  2148. return (-1);
  2149. if ((p = header_bytes(a, 1)) == NULL)
  2150. return (-1);
  2151. }
  2152. if (*p == kEnd)
  2153. return (0);
  2154. /*
  2155. * Read FilesInfo.
  2156. */
  2157. if (*p != kFilesInfo)
  2158. return (-1);
  2159. if (parse_7zip_uint64(a, &(zip->numFiles)) < 0)
  2160. return (-1);
  2161. if (UMAX_ENTRY < zip->numFiles)
  2162. return (-1);
  2163. zip->entries = calloc((size_t)zip->numFiles, sizeof(*zip->entries));
  2164. if (zip->entries == NULL)
  2165. return (-1);
  2166. entries = zip->entries;
  2167. empty_streams = 0;
  2168. for (;;) {
  2169. int type;
  2170. uint64_t size;
  2171. size_t ll;
  2172. if ((p = header_bytes(a, 1)) == NULL)
  2173. return (-1);
  2174. type = *p;
  2175. if (type == kEnd)
  2176. break;
  2177. if (parse_7zip_uint64(a, &size) < 0)
  2178. return (-1);
  2179. if (zip->header_bytes_remaining < size)
  2180. return (-1);
  2181. ll = (size_t)size;
  2182. switch (type) {
  2183. case kEmptyStream:
  2184. if (h->emptyStreamBools != NULL)
  2185. return (-1);
  2186. h->emptyStreamBools = calloc((size_t)zip->numFiles,
  2187. sizeof(*h->emptyStreamBools));
  2188. if (h->emptyStreamBools == NULL)
  2189. return (-1);
  2190. if (read_Bools(
  2191. a, h->emptyStreamBools, (size_t)zip->numFiles) < 0)
  2192. return (-1);
  2193. empty_streams = 0;
  2194. for (i = 0; i < zip->numFiles; i++) {
  2195. if (h->emptyStreamBools[i])
  2196. empty_streams++;
  2197. }
  2198. break;
  2199. case kEmptyFile:
  2200. if (empty_streams <= 0) {
  2201. /* Unexcepted sequence. Skip this. */
  2202. if (header_bytes(a, ll) == NULL)
  2203. return (-1);
  2204. break;
  2205. }
  2206. if (h->emptyFileBools != NULL)
  2207. return (-1);
  2208. h->emptyFileBools = calloc(empty_streams,
  2209. sizeof(*h->emptyFileBools));
  2210. if (h->emptyFileBools == NULL)
  2211. return (-1);
  2212. if (read_Bools(a, h->emptyFileBools, empty_streams) < 0)
  2213. return (-1);
  2214. break;
  2215. case kAnti:
  2216. if (empty_streams <= 0) {
  2217. /* Unexcepted sequence. Skip this. */
  2218. if (header_bytes(a, ll) == NULL)
  2219. return (-1);
  2220. break;
  2221. }
  2222. if (h->antiBools != NULL)
  2223. return (-1);
  2224. h->antiBools = calloc(empty_streams,
  2225. sizeof(*h->antiBools));
  2226. if (h->antiBools == NULL)
  2227. return (-1);
  2228. if (read_Bools(a, h->antiBools, empty_streams) < 0)
  2229. return (-1);
  2230. break;
  2231. case kCTime:
  2232. case kATime:
  2233. case kMTime:
  2234. if (read_Times(a, h, type) < 0)
  2235. return (-1);
  2236. break;
  2237. case kName:
  2238. {
  2239. unsigned char *np;
  2240. size_t nl, nb;
  2241. /* Skip one byte. */
  2242. if ((p = header_bytes(a, 1)) == NULL)
  2243. return (-1);
  2244. ll--;
  2245. if ((ll & 1) || ll < zip->numFiles * 4)
  2246. return (-1);
  2247. if (zip->entry_names != NULL)
  2248. return (-1);
  2249. zip->entry_names = malloc(ll);
  2250. if (zip->entry_names == NULL)
  2251. return (-1);
  2252. np = zip->entry_names;
  2253. nb = ll;
  2254. /*
  2255. * Copy whole file names.
  2256. * NOTE: This loop prevents from expanding
  2257. * the uncompressed buffer in order not to
  2258. * use extra memory resource.
  2259. */
  2260. while (nb) {
  2261. size_t b;
  2262. if (nb > UBUFF_SIZE)
  2263. b = UBUFF_SIZE;
  2264. else
  2265. b = nb;
  2266. if ((p = header_bytes(a, b)) == NULL)
  2267. return (-1);
  2268. memcpy(np, p, b);
  2269. np += b;
  2270. nb -= b;
  2271. }
  2272. np = zip->entry_names;
  2273. nl = ll;
  2274. for (i = 0; i < zip->numFiles; i++) {
  2275. entries[i].utf16name = np;
  2276. #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
  2277. entries[i].wname = (wchar_t *)np;
  2278. #endif
  2279. /* Find a terminator. */
  2280. while (nl >= 2 && (np[0] || np[1])) {
  2281. np += 2;
  2282. nl -= 2;
  2283. }
  2284. if (nl < 2)
  2285. return (-1);/* Terminator not found */
  2286. entries[i].name_len = np - entries[i].utf16name;
  2287. np += 2;
  2288. nl -= 2;
  2289. }
  2290. break;
  2291. }
  2292. case kAttributes:
  2293. {
  2294. int allAreDefined;
  2295. if ((p = header_bytes(a, 2)) == NULL)
  2296. return (-1);
  2297. allAreDefined = *p;
  2298. if (h->attrBools != NULL)
  2299. return (-1);
  2300. h->attrBools = calloc((size_t)zip->numFiles,
  2301. sizeof(*h->attrBools));
  2302. if (h->attrBools == NULL)
  2303. return (-1);
  2304. if (allAreDefined)
  2305. memset(h->attrBools, 1, (size_t)zip->numFiles);
  2306. else {
  2307. if (read_Bools(a, h->attrBools,
  2308. (size_t)zip->numFiles) < 0)
  2309. return (-1);
  2310. }
  2311. for (i = 0; i < zip->numFiles; i++) {
  2312. if (h->attrBools[i]) {
  2313. if ((p = header_bytes(a, 4)) == NULL)
  2314. return (-1);
  2315. entries[i].attr = archive_le32dec(p);
  2316. }
  2317. }
  2318. break;
  2319. }
  2320. case kDummy:
  2321. if (ll == 0)
  2322. break;
  2323. __LA_FALLTHROUGH;
  2324. default:
  2325. if (header_bytes(a, ll) == NULL)
  2326. return (-1);
  2327. break;
  2328. }
  2329. }
  2330. /*
  2331. * Set up entry's attributes.
  2332. */
  2333. folders = si->ci.folders;
  2334. eindex = sindex = 0;
  2335. folderIndex = indexInFolder = 0;
  2336. for (i = 0; i < zip->numFiles; i++) {
  2337. if (h->emptyStreamBools == NULL || h->emptyStreamBools[i] == 0)
  2338. entries[i].flg |= HAS_STREAM;
  2339. /* The high 16 bits of attributes is a posix file mode. */
  2340. entries[i].mode = entries[i].attr >> 16;
  2341. if (entries[i].flg & HAS_STREAM) {
  2342. if ((size_t)sindex >= si->ss.unpack_streams)
  2343. return (-1);
  2344. if (entries[i].mode == 0)
  2345. entries[i].mode = AE_IFREG | 0666;
  2346. if (si->ss.digestsDefined[sindex])
  2347. entries[i].flg |= CRC32_IS_SET;
  2348. entries[i].ssIndex = sindex;
  2349. sindex++;
  2350. } else {
  2351. int dir;
  2352. if (h->emptyFileBools == NULL)
  2353. dir = 1;
  2354. else {
  2355. if (h->emptyFileBools[eindex])
  2356. dir = 0;
  2357. else
  2358. dir = 1;
  2359. eindex++;
  2360. }
  2361. if (entries[i].mode == 0) {
  2362. if (dir)
  2363. entries[i].mode = AE_IFDIR | 0777;
  2364. else
  2365. entries[i].mode = AE_IFREG | 0666;
  2366. } else if (dir &&
  2367. (entries[i].mode & AE_IFMT) != AE_IFDIR) {
  2368. entries[i].mode &= ~AE_IFMT;
  2369. entries[i].mode |= AE_IFDIR;
  2370. }
  2371. if ((entries[i].mode & AE_IFMT) == AE_IFDIR &&
  2372. entries[i].name_len >= 2 &&
  2373. (entries[i].utf16name[entries[i].name_len-2] != '/' ||
  2374. entries[i].utf16name[entries[i].name_len-1] != 0)) {
  2375. entries[i].utf16name[entries[i].name_len] = '/';
  2376. entries[i].utf16name[entries[i].name_len+1] = 0;
  2377. entries[i].name_len += 2;
  2378. }
  2379. entries[i].ssIndex = -1;
  2380. }
  2381. if (entries[i].attr & 0x01)
  2382. entries[i].mode &= ~0222;/* Read only. */
  2383. if ((entries[i].flg & HAS_STREAM) == 0 && indexInFolder == 0) {
  2384. /*
  2385. * The entry is an empty file or a directory file,
  2386. * those both have no contents.
  2387. */
  2388. entries[i].folderIndex = -1;
  2389. continue;
  2390. }
  2391. if (indexInFolder == 0) {
  2392. for (;;) {
  2393. if (folderIndex >= si->ci.numFolders)
  2394. return (-1);
  2395. if (folders[folderIndex].numUnpackStreams)
  2396. break;
  2397. folderIndex++;
  2398. }
  2399. }
  2400. entries[i].folderIndex = folderIndex;
  2401. if ((entries[i].flg & HAS_STREAM) == 0)
  2402. continue;
  2403. indexInFolder++;
  2404. if (indexInFolder >= folders[folderIndex].numUnpackStreams) {
  2405. folderIndex++;
  2406. indexInFolder = 0;
  2407. }
  2408. }
  2409. return (0);
  2410. }
  2411. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  2412. static void
  2413. fileTimeToUtc(uint64_t fileTime, time_t *timep, long *ns)
  2414. {
  2415. if (fileTime >= EPOC_TIME) {
  2416. fileTime -= EPOC_TIME;
  2417. /* milli seconds base */
  2418. *timep = (time_t)(fileTime / 10000000);
  2419. /* nano seconds base */
  2420. *ns = (long)(fileTime % 10000000) * 100;
  2421. } else {
  2422. *timep = 0;
  2423. *ns = 0;
  2424. }
  2425. }
  2426. static int
  2427. read_Times(struct archive_read *a, struct _7z_header_info *h, int type)
  2428. {
  2429. struct _7zip *zip = (struct _7zip *)a->format->data;
  2430. const unsigned char *p;
  2431. struct _7zip_entry *entries = zip->entries;
  2432. unsigned char *timeBools;
  2433. int allAreDefined;
  2434. unsigned i;
  2435. timeBools = calloc((size_t)zip->numFiles, sizeof(*timeBools));
  2436. if (timeBools == NULL)
  2437. return (-1);
  2438. /* Read allAreDefined. */
  2439. if ((p = header_bytes(a, 1)) == NULL)
  2440. goto failed;
  2441. allAreDefined = *p;
  2442. if (allAreDefined)
  2443. memset(timeBools, 1, (size_t)zip->numFiles);
  2444. else {
  2445. if (read_Bools(a, timeBools, (size_t)zip->numFiles) < 0)
  2446. goto failed;
  2447. }
  2448. /* Read external. */
  2449. if ((p = header_bytes(a, 1)) == NULL)
  2450. goto failed;
  2451. if (*p) {
  2452. if (parse_7zip_uint64(a, &(h->dataIndex)) < 0)
  2453. goto failed;
  2454. if (UMAX_ENTRY < h->dataIndex)
  2455. goto failed;
  2456. }
  2457. for (i = 0; i < zip->numFiles; i++) {
  2458. if (!timeBools[i])
  2459. continue;
  2460. if ((p = header_bytes(a, 8)) == NULL)
  2461. goto failed;
  2462. switch (type) {
  2463. case kCTime:
  2464. fileTimeToUtc(archive_le64dec(p),
  2465. &(entries[i].ctime),
  2466. &(entries[i].ctime_ns));
  2467. entries[i].flg |= CTIME_IS_SET;
  2468. break;
  2469. case kATime:
  2470. fileTimeToUtc(archive_le64dec(p),
  2471. &(entries[i].atime),
  2472. &(entries[i].atime_ns));
  2473. entries[i].flg |= ATIME_IS_SET;
  2474. break;
  2475. case kMTime:
  2476. fileTimeToUtc(archive_le64dec(p),
  2477. &(entries[i].mtime),
  2478. &(entries[i].mtime_ns));
  2479. entries[i].flg |= MTIME_IS_SET;
  2480. break;
  2481. }
  2482. }
  2483. free(timeBools);
  2484. return (0);
  2485. failed:
  2486. free(timeBools);
  2487. return (-1);
  2488. }
  2489. static int
  2490. decode_encoded_header_info(struct archive_read *a, struct _7z_stream_info *si)
  2491. {
  2492. struct _7zip *zip = (struct _7zip *)a->format->data;
  2493. errno = 0;
  2494. if (read_StreamsInfo(a, si) < 0) {
  2495. if (errno == ENOMEM)
  2496. archive_set_error(&a->archive, -1,
  2497. "Couldn't allocate memory");
  2498. else
  2499. archive_set_error(&a->archive, -1,
  2500. "Malformed 7-Zip archive");
  2501. return (ARCHIVE_FATAL);
  2502. }
  2503. if (si->pi.numPackStreams == 0 || si->ci.numFolders == 0) {
  2504. archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
  2505. return (ARCHIVE_FATAL);
  2506. }
  2507. if (zip->header_offset < si->pi.pos + si->pi.sizes[0] ||
  2508. (int64_t)(si->pi.pos + si->pi.sizes[0]) < 0 ||
  2509. si->pi.sizes[0] == 0 || (int64_t)si->pi.pos < 0) {
  2510. archive_set_error(&a->archive, -1, "Malformed Header offset");
  2511. return (ARCHIVE_FATAL);
  2512. }
  2513. return (ARCHIVE_OK);
  2514. }
  2515. static const unsigned char *
  2516. header_bytes(struct archive_read *a, size_t rbytes)
  2517. {
  2518. struct _7zip *zip = (struct _7zip *)a->format->data;
  2519. const unsigned char *p;
  2520. if (zip->header_bytes_remaining < rbytes)
  2521. return (NULL);
  2522. if (zip->pack_stream_bytes_unconsumed)
  2523. read_consume(a);
  2524. if (zip->header_is_encoded == 0) {
  2525. p = __archive_read_ahead(a, rbytes, NULL);
  2526. if (p == NULL)
  2527. return (NULL);
  2528. zip->header_bytes_remaining -= rbytes;
  2529. zip->pack_stream_bytes_unconsumed = rbytes;
  2530. } else {
  2531. const void *buff;
  2532. ssize_t bytes;
  2533. bytes = read_stream(a, &buff, rbytes, rbytes);
  2534. if (bytes <= 0)
  2535. return (NULL);
  2536. zip->header_bytes_remaining -= bytes;
  2537. p = buff;
  2538. }
  2539. /* Update checksum */
  2540. zip->header_crc32 = crc32(zip->header_crc32, p, (unsigned)rbytes);
  2541. return (p);
  2542. }
  2543. static int
  2544. slurp_central_directory(struct archive_read *a, struct _7zip *zip,
  2545. struct _7z_header_info *header)
  2546. {
  2547. const unsigned char *p;
  2548. uint64_t next_header_offset;
  2549. uint64_t next_header_size;
  2550. uint32_t next_header_crc;
  2551. ssize_t bytes_avail;
  2552. int check_header_crc, r;
  2553. if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
  2554. return (ARCHIVE_FATAL);
  2555. if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
  2556. /* This is an executable ? Must be self-extracting... */
  2557. r = skip_sfx(a, bytes_avail);
  2558. if (r < ARCHIVE_WARN)
  2559. return (r);
  2560. if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
  2561. return (ARCHIVE_FATAL);
  2562. }
  2563. zip->seek_base += 32;
  2564. if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0) {
  2565. archive_set_error(&a->archive, -1, "Not 7-Zip archive file");
  2566. return (ARCHIVE_FATAL);
  2567. }
  2568. /* CRC check. */
  2569. if (crc32(0, (const unsigned char *)p + 12, 20)
  2570. != archive_le32dec(p + 8)) {
  2571. archive_set_error(&a->archive, -1, "Header CRC error");
  2572. return (ARCHIVE_FATAL);
  2573. }
  2574. next_header_offset = archive_le64dec(p + 12);
  2575. next_header_size = archive_le64dec(p + 20);
  2576. next_header_crc = archive_le32dec(p + 28);
  2577. if (next_header_size == 0)
  2578. /* There is no entry in an archive file. */
  2579. return (ARCHIVE_EOF);
  2580. if (((int64_t)next_header_offset) < 0) {
  2581. archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
  2582. return (ARCHIVE_FATAL);
  2583. }
  2584. __archive_read_consume(a, 32);
  2585. if (next_header_offset != 0) {
  2586. if (bytes_avail >= (ssize_t)next_header_offset)
  2587. __archive_read_consume(a, next_header_offset);
  2588. else if (__archive_read_seek(a,
  2589. next_header_offset + zip->seek_base, SEEK_SET) < 0)
  2590. return (ARCHIVE_FATAL);
  2591. }
  2592. zip->stream_offset = next_header_offset;
  2593. zip->header_offset = next_header_offset;
  2594. zip->header_bytes_remaining = next_header_size;
  2595. zip->header_crc32 = 0;
  2596. zip->header_is_encoded = 0;
  2597. zip->header_is_being_read = 1;
  2598. zip->has_encrypted_entries = 0;
  2599. check_header_crc = 1;
  2600. if ((p = header_bytes(a, 1)) == NULL) {
  2601. archive_set_error(&a->archive,
  2602. ARCHIVE_ERRNO_FILE_FORMAT,
  2603. "Truncated 7-Zip file body");
  2604. return (ARCHIVE_FATAL);
  2605. }
  2606. /* Parse ArchiveProperties. */
  2607. switch (p[0]) {
  2608. case kEncodedHeader:
  2609. /*
  2610. * The archive has an encoded header and we have to decode it
  2611. * in order to parse the header correctly.
  2612. */
  2613. r = decode_encoded_header_info(a, &(zip->si));
  2614. /* Check the EncodedHeader CRC.*/
  2615. if (r == 0 && zip->header_crc32 != next_header_crc) {
  2616. archive_set_error(&a->archive, -1,
  2617. "Damaged 7-Zip archive");
  2618. r = -1;
  2619. }
  2620. if (r == 0) {
  2621. if (zip->si.ci.folders[0].digest_defined)
  2622. next_header_crc = zip->si.ci.folders[0].digest;
  2623. else
  2624. check_header_crc = 0;
  2625. if (zip->pack_stream_bytes_unconsumed)
  2626. read_consume(a);
  2627. r = setup_decode_folder(a, zip->si.ci.folders, 1);
  2628. if (r == 0) {
  2629. zip->header_bytes_remaining =
  2630. zip->folder_outbytes_remaining;
  2631. r = seek_pack(a);
  2632. }
  2633. }
  2634. /* Clean up StreamsInfo. */
  2635. free_StreamsInfo(&(zip->si));
  2636. memset(&(zip->si), 0, sizeof(zip->si));
  2637. if (r < 0)
  2638. return (ARCHIVE_FATAL);
  2639. zip->header_is_encoded = 1;
  2640. zip->header_crc32 = 0;
  2641. /* FALL THROUGH */
  2642. case kHeader:
  2643. /*
  2644. * Parse the header.
  2645. */
  2646. errno = 0;
  2647. r = read_Header(a, header, zip->header_is_encoded);
  2648. if (r < 0) {
  2649. if (errno == ENOMEM)
  2650. archive_set_error(&a->archive, -1,
  2651. "Couldn't allocate memory");
  2652. else
  2653. archive_set_error(&a->archive, -1,
  2654. "Damaged 7-Zip archive");
  2655. return (ARCHIVE_FATAL);
  2656. }
  2657. /*
  2658. * Must be kEnd.
  2659. */
  2660. if ((p = header_bytes(a, 1)) == NULL ||*p != kEnd) {
  2661. archive_set_error(&a->archive, -1,
  2662. "Malformed 7-Zip archive");
  2663. return (ARCHIVE_FATAL);
  2664. }
  2665. /* Check the Header CRC.*/
  2666. if (check_header_crc && zip->header_crc32 != next_header_crc) {
  2667. archive_set_error(&a->archive, -1,
  2668. "Malformed 7-Zip archive");
  2669. return (ARCHIVE_FATAL);
  2670. }
  2671. break;
  2672. default:
  2673. archive_set_error(&a->archive, -1,
  2674. "Unexpected Property ID = %X", p[0]);
  2675. return (ARCHIVE_FATAL);
  2676. }
  2677. /* Clean up variables be used for decoding the archive header */
  2678. zip->pack_stream_remaining = 0;
  2679. zip->pack_stream_index = 0;
  2680. zip->folder_outbytes_remaining = 0;
  2681. zip->uncompressed_buffer_bytes_remaining = 0;
  2682. zip->pack_stream_bytes_unconsumed = 0;
  2683. zip->header_is_being_read = 0;
  2684. return (ARCHIVE_OK);
  2685. }
  2686. static ssize_t
  2687. get_uncompressed_data(struct archive_read *a, const void **buff, size_t size,
  2688. size_t minimum)
  2689. {
  2690. struct _7zip *zip = (struct _7zip *)a->format->data;
  2691. ssize_t bytes_avail;
  2692. if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
  2693. /* Copy mode. */
  2694. *buff = __archive_read_ahead(a, minimum, &bytes_avail);
  2695. if (bytes_avail <= 0) {
  2696. archive_set_error(&a->archive,
  2697. ARCHIVE_ERRNO_FILE_FORMAT,
  2698. "Truncated 7-Zip file data");
  2699. return (ARCHIVE_FATAL);
  2700. }
  2701. if ((size_t)bytes_avail >
  2702. zip->uncompressed_buffer_bytes_remaining)
  2703. bytes_avail = (ssize_t)
  2704. zip->uncompressed_buffer_bytes_remaining;
  2705. if ((size_t)bytes_avail > size)
  2706. bytes_avail = (ssize_t)size;
  2707. zip->pack_stream_bytes_unconsumed = bytes_avail;
  2708. } else if (zip->uncompressed_buffer_pointer == NULL) {
  2709. /* Decompression has failed. */
  2710. archive_set_error(&(a->archive),
  2711. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2712. return (ARCHIVE_FATAL);
  2713. } else {
  2714. /* Packed mode. */
  2715. if (minimum > zip->uncompressed_buffer_bytes_remaining) {
  2716. /*
  2717. * If remaining uncompressed data size is less than
  2718. * the minimum size, fill the buffer up to the
  2719. * minimum size.
  2720. */
  2721. if (extract_pack_stream(a, minimum) < 0)
  2722. return (ARCHIVE_FATAL);
  2723. }
  2724. if (size > zip->uncompressed_buffer_bytes_remaining)
  2725. bytes_avail = (ssize_t)
  2726. zip->uncompressed_buffer_bytes_remaining;
  2727. else
  2728. bytes_avail = (ssize_t)size;
  2729. *buff = zip->uncompressed_buffer_pointer;
  2730. zip->uncompressed_buffer_pointer += bytes_avail;
  2731. }
  2732. zip->uncompressed_buffer_bytes_remaining -= bytes_avail;
  2733. return (bytes_avail);
  2734. }
  2735. static ssize_t
  2736. extract_pack_stream(struct archive_read *a, size_t minimum)
  2737. {
  2738. struct _7zip *zip = (struct _7zip *)a->format->data;
  2739. ssize_t bytes_avail;
  2740. int r;
  2741. if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
  2742. if (minimum == 0)
  2743. minimum = 1;
  2744. if (__archive_read_ahead(a, minimum, &bytes_avail) == NULL
  2745. || bytes_avail <= 0) {
  2746. archive_set_error(&a->archive,
  2747. ARCHIVE_ERRNO_FILE_FORMAT,
  2748. "Truncated 7-Zip file body");
  2749. return (ARCHIVE_FATAL);
  2750. }
  2751. if (bytes_avail > (ssize_t)zip->pack_stream_inbytes_remaining)
  2752. bytes_avail = (ssize_t)zip->pack_stream_inbytes_remaining;
  2753. zip->pack_stream_inbytes_remaining -= bytes_avail;
  2754. if (bytes_avail > (ssize_t)zip->folder_outbytes_remaining)
  2755. bytes_avail = (ssize_t)zip->folder_outbytes_remaining;
  2756. zip->folder_outbytes_remaining -= bytes_avail;
  2757. zip->uncompressed_buffer_bytes_remaining = bytes_avail;
  2758. return (ARCHIVE_OK);
  2759. }
  2760. /* If the buffer hasn't been allocated, allocate it now. */
  2761. if (zip->uncompressed_buffer == NULL) {
  2762. zip->uncompressed_buffer_size = UBUFF_SIZE;
  2763. if (zip->uncompressed_buffer_size < minimum) {
  2764. zip->uncompressed_buffer_size = minimum + 1023;
  2765. zip->uncompressed_buffer_size &= ~0x3ff;
  2766. }
  2767. zip->uncompressed_buffer =
  2768. malloc(zip->uncompressed_buffer_size);
  2769. if (zip->uncompressed_buffer == NULL) {
  2770. archive_set_error(&a->archive, ENOMEM,
  2771. "No memory for 7-Zip decompression");
  2772. return (ARCHIVE_FATAL);
  2773. }
  2774. zip->uncompressed_buffer_bytes_remaining = 0;
  2775. } else if (zip->uncompressed_buffer_size < minimum ||
  2776. zip->uncompressed_buffer_bytes_remaining < minimum) {
  2777. /*
  2778. * Make sure the uncompressed buffer can have bytes
  2779. * at least `minimum' bytes.
  2780. * NOTE: This case happen when reading the header.
  2781. */
  2782. size_t used;
  2783. if (zip->uncompressed_buffer_pointer != 0)
  2784. used = zip->uncompressed_buffer_pointer -
  2785. zip->uncompressed_buffer;
  2786. else
  2787. used = 0;
  2788. if (zip->uncompressed_buffer_size < minimum) {
  2789. /*
  2790. * Expand the uncompressed buffer up to
  2791. * the minimum size.
  2792. */
  2793. void *p;
  2794. size_t new_size;
  2795. new_size = minimum + 1023;
  2796. new_size &= ~0x3ff;
  2797. p = realloc(zip->uncompressed_buffer, new_size);
  2798. if (p == NULL) {
  2799. archive_set_error(&a->archive, ENOMEM,
  2800. "No memory for 7-Zip decompression");
  2801. return (ARCHIVE_FATAL);
  2802. }
  2803. zip->uncompressed_buffer = (unsigned char *)p;
  2804. zip->uncompressed_buffer_size = new_size;
  2805. }
  2806. /*
  2807. * Move unconsumed bytes to the head.
  2808. */
  2809. if (used) {
  2810. memmove(zip->uncompressed_buffer,
  2811. zip->uncompressed_buffer + used,
  2812. zip->uncompressed_buffer_bytes_remaining);
  2813. }
  2814. } else
  2815. zip->uncompressed_buffer_bytes_remaining = 0;
  2816. zip->uncompressed_buffer_pointer = NULL;
  2817. for (;;) {
  2818. size_t bytes_in, bytes_out;
  2819. const void *buff_in;
  2820. unsigned char *buff_out;
  2821. int end_of_data;
  2822. /*
  2823. * Note: '1' here is a performance optimization.
  2824. * Recall that the decompression layer returns a count of
  2825. * available bytes; asking for more than that forces the
  2826. * decompressor to combine reads by copying data.
  2827. */
  2828. buff_in = __archive_read_ahead(a, 1, &bytes_avail);
  2829. if (bytes_avail <= 0) {
  2830. archive_set_error(&a->archive,
  2831. ARCHIVE_ERRNO_FILE_FORMAT,
  2832. "Truncated 7-Zip file body");
  2833. return (ARCHIVE_FATAL);
  2834. }
  2835. buff_out = zip->uncompressed_buffer
  2836. + zip->uncompressed_buffer_bytes_remaining;
  2837. bytes_out = zip->uncompressed_buffer_size
  2838. - zip->uncompressed_buffer_bytes_remaining;
  2839. bytes_in = bytes_avail;
  2840. if (bytes_in > zip->pack_stream_inbytes_remaining)
  2841. bytes_in = (size_t)zip->pack_stream_inbytes_remaining;
  2842. /* Drive decompression. */
  2843. r = decompress(a, zip, buff_out, &bytes_out,
  2844. buff_in, &bytes_in);
  2845. switch (r) {
  2846. case ARCHIVE_OK:
  2847. end_of_data = 0;
  2848. break;
  2849. case ARCHIVE_EOF:
  2850. end_of_data = 1;
  2851. break;
  2852. default:
  2853. return (ARCHIVE_FATAL);
  2854. }
  2855. zip->pack_stream_inbytes_remaining -= bytes_in;
  2856. if (bytes_out > zip->folder_outbytes_remaining)
  2857. bytes_out = (size_t)zip->folder_outbytes_remaining;
  2858. zip->folder_outbytes_remaining -= bytes_out;
  2859. zip->uncompressed_buffer_bytes_remaining += bytes_out;
  2860. zip->pack_stream_bytes_unconsumed = bytes_in;
  2861. /*
  2862. * Continue decompression until uncompressed_buffer is full.
  2863. */
  2864. if (zip->uncompressed_buffer_bytes_remaining ==
  2865. zip->uncompressed_buffer_size)
  2866. break;
  2867. if (zip->codec2 == _7Z_X86 && zip->odd_bcj_size &&
  2868. zip->uncompressed_buffer_bytes_remaining + 5 >
  2869. zip->uncompressed_buffer_size)
  2870. break;
  2871. if (zip->pack_stream_inbytes_remaining == 0 &&
  2872. zip->folder_outbytes_remaining == 0)
  2873. break;
  2874. if (end_of_data || (bytes_in == 0 && bytes_out == 0)) {
  2875. archive_set_error(&(a->archive),
  2876. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2877. return (ARCHIVE_FATAL);
  2878. }
  2879. read_consume(a);
  2880. }
  2881. if (zip->uncompressed_buffer_bytes_remaining < minimum) {
  2882. archive_set_error(&(a->archive),
  2883. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2884. return (ARCHIVE_FATAL);
  2885. }
  2886. zip->uncompressed_buffer_pointer = zip->uncompressed_buffer;
  2887. return (ARCHIVE_OK);
  2888. }
  2889. static int
  2890. seek_pack(struct archive_read *a)
  2891. {
  2892. struct _7zip *zip = (struct _7zip *)a->format->data;
  2893. int64_t pack_offset;
  2894. if (zip->pack_stream_remaining <= 0) {
  2895. archive_set_error(&(a->archive),
  2896. ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
  2897. return (ARCHIVE_FATAL);
  2898. }
  2899. zip->pack_stream_inbytes_remaining =
  2900. zip->si.pi.sizes[zip->pack_stream_index];
  2901. pack_offset = zip->si.pi.positions[zip->pack_stream_index];
  2902. if (zip->stream_offset != pack_offset) {
  2903. if (0 > __archive_read_seek(a, pack_offset + zip->seek_base,
  2904. SEEK_SET))
  2905. return (ARCHIVE_FATAL);
  2906. zip->stream_offset = pack_offset;
  2907. }
  2908. zip->pack_stream_index++;
  2909. zip->pack_stream_remaining--;
  2910. return (ARCHIVE_OK);
  2911. }
  2912. static ssize_t
  2913. read_stream(struct archive_read *a, const void **buff, size_t size,
  2914. size_t minimum)
  2915. {
  2916. struct _7zip *zip = (struct _7zip *)a->format->data;
  2917. uint64_t skip_bytes = 0;
  2918. ssize_t r;
  2919. if (zip->uncompressed_buffer_bytes_remaining == 0) {
  2920. if (zip->pack_stream_inbytes_remaining > 0) {
  2921. r = extract_pack_stream(a, 0);
  2922. if (r < 0)
  2923. return (r);
  2924. return (get_uncompressed_data(a, buff, size, minimum));
  2925. } else if (zip->folder_outbytes_remaining > 0) {
  2926. /* Extract a remaining pack stream. */
  2927. r = extract_pack_stream(a, 0);
  2928. if (r < 0)
  2929. return (r);
  2930. return (get_uncompressed_data(a, buff, size, minimum));
  2931. }
  2932. } else
  2933. return (get_uncompressed_data(a, buff, size, minimum));
  2934. /*
  2935. * Current pack stream has been consumed.
  2936. */
  2937. if (zip->pack_stream_remaining == 0) {
  2938. if (zip->header_is_being_read) {
  2939. /* Invalid sequence. This might happen when
  2940. * reading a malformed archive. */
  2941. archive_set_error(&(a->archive),
  2942. ARCHIVE_ERRNO_MISC, "Malformed 7-Zip archive");
  2943. return (ARCHIVE_FATAL);
  2944. }
  2945. /*
  2946. * All current folder's pack streams have been
  2947. * consumed. Switch to next folder.
  2948. */
  2949. if (zip->folder_index == 0 &&
  2950. (zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
  2951. || zip->folder_index != zip->entry->folderIndex)) {
  2952. zip->folder_index = zip->entry->folderIndex;
  2953. skip_bytes =
  2954. zip->si.ci.folders[zip->folder_index].skipped_bytes;
  2955. }
  2956. if (zip->folder_index >= zip->si.ci.numFolders) {
  2957. /*
  2958. * We have consumed all folders and its pack streams.
  2959. */
  2960. *buff = NULL;
  2961. return (0);
  2962. }
  2963. r = setup_decode_folder(a,
  2964. &(zip->si.ci.folders[zip->folder_index]), 0);
  2965. if (r != ARCHIVE_OK)
  2966. return (ARCHIVE_FATAL);
  2967. zip->folder_index++;
  2968. }
  2969. /*
  2970. * Switch to next pack stream.
  2971. */
  2972. r = seek_pack(a);
  2973. if (r < 0)
  2974. return (r);
  2975. /* Extract a new pack stream. */
  2976. r = extract_pack_stream(a, 0);
  2977. if (r < 0)
  2978. return (r);
  2979. /*
  2980. * Skip the bytes we already has skipped in skip_stream().
  2981. */
  2982. while (skip_bytes) {
  2983. ssize_t skipped;
  2984. if (zip->uncompressed_buffer_bytes_remaining == 0) {
  2985. if (zip->pack_stream_inbytes_remaining > 0) {
  2986. r = extract_pack_stream(a, 0);
  2987. if (r < 0)
  2988. return (r);
  2989. } else if (zip->folder_outbytes_remaining > 0) {
  2990. /* Extract a remaining pack stream. */
  2991. r = extract_pack_stream(a, 0);
  2992. if (r < 0)
  2993. return (r);
  2994. } else {
  2995. archive_set_error(&a->archive,
  2996. ARCHIVE_ERRNO_FILE_FORMAT,
  2997. "Truncated 7-Zip file body");
  2998. return (ARCHIVE_FATAL);
  2999. }
  3000. }
  3001. skipped = get_uncompressed_data(
  3002. a, buff, (size_t)skip_bytes, 0);
  3003. if (skipped < 0)
  3004. return (skipped);
  3005. skip_bytes -= skipped;
  3006. if (zip->pack_stream_bytes_unconsumed)
  3007. read_consume(a);
  3008. }
  3009. return (get_uncompressed_data(a, buff, size, minimum));
  3010. }
  3011. static int
  3012. setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
  3013. int header)
  3014. {
  3015. struct _7zip *zip = (struct _7zip *)a->format->data;
  3016. const struct _7z_coder *coder1, *coder2;
  3017. const char *cname = (header)?"archive header":"file content";
  3018. unsigned i;
  3019. int r, found_bcj2 = 0;
  3020. /*
  3021. * Release the memory which the previous folder used for BCJ2.
  3022. */
  3023. for (i = 0; i < 3; i++) {
  3024. free(zip->sub_stream_buff[i]);
  3025. zip->sub_stream_buff[i] = NULL;
  3026. }
  3027. /*
  3028. * Initialize a stream reader.
  3029. */
  3030. zip->pack_stream_remaining = (unsigned)folder->numPackedStreams;
  3031. zip->pack_stream_index = (unsigned)folder->packIndex;
  3032. zip->folder_outbytes_remaining = folder_uncompressed_size(folder);
  3033. zip->uncompressed_buffer_bytes_remaining = 0;
  3034. /*
  3035. * Check coder types.
  3036. */
  3037. for (i = 0; i < folder->numCoders; i++) {
  3038. switch(folder->coders[i].codec) {
  3039. case _7Z_CRYPTO_MAIN_ZIP:
  3040. case _7Z_CRYPTO_RAR_29:
  3041. case _7Z_CRYPTO_AES_256_SHA_256: {
  3042. /* For entry that is associated with this folder, mark
  3043. it as encrypted (data+metadata). */
  3044. zip->has_encrypted_entries = 1;
  3045. if (a->entry) {
  3046. archive_entry_set_is_data_encrypted(a->entry, 1);
  3047. archive_entry_set_is_metadata_encrypted(a->entry, 1);
  3048. }
  3049. archive_set_error(&(a->archive),
  3050. ARCHIVE_ERRNO_MISC,
  3051. "The %s is encrypted, "
  3052. "but currently not supported", cname);
  3053. return (ARCHIVE_FATAL);
  3054. }
  3055. case _7Z_X86_BCJ2: {
  3056. found_bcj2++;
  3057. break;
  3058. }
  3059. }
  3060. }
  3061. /* Now that we've checked for encryption, if there were still no
  3062. * encrypted entries found we can say for sure that there are none.
  3063. */
  3064. if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
  3065. zip->has_encrypted_entries = 0;
  3066. }
  3067. if ((folder->numCoders > 2 && !found_bcj2) || found_bcj2 > 1) {
  3068. archive_set_error(&(a->archive),
  3069. ARCHIVE_ERRNO_MISC,
  3070. "The %s is encoded with many filters, "
  3071. "but currently not supported", cname);
  3072. return (ARCHIVE_FATAL);
  3073. }
  3074. coder1 = &(folder->coders[0]);
  3075. if (folder->numCoders == 2)
  3076. coder2 = &(folder->coders[1]);
  3077. else
  3078. coder2 = NULL;
  3079. if (found_bcj2) {
  3080. /*
  3081. * Preparation to decode BCJ2.
  3082. * Decoding BCJ2 requires four sources. Those are at least,
  3083. * as far as I know, two types of the storage form.
  3084. */
  3085. const struct _7z_coder *fc = folder->coders;
  3086. static const struct _7z_coder coder_copy = {0, 1, 1, 0, NULL};
  3087. const struct _7z_coder *scoder[3] =
  3088. {&coder_copy, &coder_copy, &coder_copy};
  3089. const void *buff;
  3090. ssize_t bytes;
  3091. unsigned char *b[3] = {NULL, NULL, NULL};
  3092. uint64_t sunpack[3] ={-1, -1, -1};
  3093. size_t s[3] = {0, 0, 0};
  3094. int idx[3] = {0, 1, 2};
  3095. if (folder->numCoders == 4 && fc[3].codec == _7Z_X86_BCJ2 &&
  3096. folder->numInStreams == 7 && folder->numOutStreams == 4 &&
  3097. zip->pack_stream_remaining == 4) {
  3098. /* Source type 1 made by 7zr or 7z with -m options. */
  3099. if (folder->bindPairs[0].inIndex == 5) {
  3100. /* The form made by 7zr */
  3101. idx[0] = 1; idx[1] = 2; idx[2] = 0;
  3102. scoder[1] = &(fc[1]);
  3103. scoder[2] = &(fc[0]);
  3104. sunpack[1] = folder->unPackSize[1];
  3105. sunpack[2] = folder->unPackSize[0];
  3106. coder1 = &(fc[2]);
  3107. } else {
  3108. /*
  3109. * NOTE: Some patterns do not work.
  3110. * work:
  3111. * 7z a -m0=BCJ2 -m1=COPY -m2=COPY
  3112. * -m3=(any)
  3113. * 7z a -m0=BCJ2 -m1=COPY -m2=(any)
  3114. * -m3=COPY
  3115. * 7z a -m0=BCJ2 -m1=(any) -m2=COPY
  3116. * -m3=COPY
  3117. * not work:
  3118. * other patterns.
  3119. *
  3120. * We have to handle this like `pipe' or
  3121. * our libarchive7s filter frame work,
  3122. * decoding the BCJ2 main stream sequentially,
  3123. * m3 -> m2 -> m1 -> BCJ2.
  3124. *
  3125. */
  3126. if (fc[0].codec == _7Z_COPY &&
  3127. fc[1].codec == _7Z_COPY)
  3128. coder1 = &(folder->coders[2]);
  3129. else if (fc[0].codec == _7Z_COPY &&
  3130. fc[2].codec == _7Z_COPY)
  3131. coder1 = &(folder->coders[1]);
  3132. else if (fc[1].codec == _7Z_COPY &&
  3133. fc[2].codec == _7Z_COPY)
  3134. coder1 = &(folder->coders[0]);
  3135. else {
  3136. archive_set_error(&(a->archive),
  3137. ARCHIVE_ERRNO_MISC,
  3138. "Unsupported form of "
  3139. "BCJ2 streams");
  3140. return (ARCHIVE_FATAL);
  3141. }
  3142. }
  3143. coder2 = &(fc[3]);
  3144. zip->main_stream_bytes_remaining =
  3145. (size_t)folder->unPackSize[2];
  3146. } else if (coder2 != NULL && coder2->codec == _7Z_X86_BCJ2 &&
  3147. zip->pack_stream_remaining == 4 &&
  3148. folder->numInStreams == 5 && folder->numOutStreams == 2) {
  3149. /* Source type 0 made by 7z */
  3150. zip->main_stream_bytes_remaining =
  3151. (size_t)folder->unPackSize[0];
  3152. } else {
  3153. /* We got an unexpected form. */
  3154. archive_set_error(&(a->archive),
  3155. ARCHIVE_ERRNO_MISC,
  3156. "Unsupported form of BCJ2 streams");
  3157. return (ARCHIVE_FATAL);
  3158. }
  3159. /* Skip the main stream at this time. */
  3160. if ((r = seek_pack(a)) < 0)
  3161. return (r);
  3162. zip->pack_stream_bytes_unconsumed =
  3163. (size_t)zip->pack_stream_inbytes_remaining;
  3164. read_consume(a);
  3165. /* Read following three sub streams. */
  3166. for (i = 0; i < 3; i++) {
  3167. const struct _7z_coder *coder = scoder[i];
  3168. if ((r = seek_pack(a)) < 0) {
  3169. free(b[0]); free(b[1]); free(b[2]);
  3170. return (r);
  3171. }
  3172. if (sunpack[i] == (uint64_t)-1)
  3173. zip->folder_outbytes_remaining =
  3174. zip->pack_stream_inbytes_remaining;
  3175. else
  3176. zip->folder_outbytes_remaining = sunpack[i];
  3177. r = init_decompression(a, zip, coder, NULL);
  3178. if (r != ARCHIVE_OK) {
  3179. free(b[0]); free(b[1]); free(b[2]);
  3180. return (ARCHIVE_FATAL);
  3181. }
  3182. /* Allocate memory for the decoded data of a sub
  3183. * stream. */
  3184. b[i] = malloc((size_t)zip->folder_outbytes_remaining);
  3185. if (b[i] == NULL) {
  3186. free(b[0]); free(b[1]); free(b[2]);
  3187. archive_set_error(&a->archive, ENOMEM,
  3188. "No memory for 7-Zip decompression");
  3189. return (ARCHIVE_FATAL);
  3190. }
  3191. /* Extract a sub stream. */
  3192. while (zip->pack_stream_inbytes_remaining > 0) {
  3193. r = (int)extract_pack_stream(a, 0);
  3194. if (r < 0) {
  3195. free(b[0]); free(b[1]); free(b[2]);
  3196. return (r);
  3197. }
  3198. bytes = get_uncompressed_data(a, &buff,
  3199. zip->uncompressed_buffer_bytes_remaining,
  3200. 0);
  3201. if (bytes < 0) {
  3202. free(b[0]); free(b[1]); free(b[2]);
  3203. return ((int)bytes);
  3204. }
  3205. memcpy(b[i]+s[i], buff, bytes);
  3206. s[i] += bytes;
  3207. if (zip->pack_stream_bytes_unconsumed)
  3208. read_consume(a);
  3209. }
  3210. }
  3211. /* Set the sub streams to the right place. */
  3212. for (i = 0; i < 3; i++) {
  3213. zip->sub_stream_buff[i] = b[idx[i]];
  3214. zip->sub_stream_size[i] = s[idx[i]];
  3215. zip->sub_stream_bytes_remaining[i] = s[idx[i]];
  3216. }
  3217. /* Allocate memory used for decoded main stream bytes. */
  3218. if (zip->tmp_stream_buff == NULL) {
  3219. zip->tmp_stream_buff_size = 32 * 1024;
  3220. zip->tmp_stream_buff =
  3221. malloc(zip->tmp_stream_buff_size);
  3222. if (zip->tmp_stream_buff == NULL) {
  3223. archive_set_error(&a->archive, ENOMEM,
  3224. "No memory for 7-Zip decompression");
  3225. return (ARCHIVE_FATAL);
  3226. }
  3227. }
  3228. zip->tmp_stream_bytes_avail = 0;
  3229. zip->tmp_stream_bytes_remaining = 0;
  3230. zip->odd_bcj_size = 0;
  3231. zip->bcj2_outPos = 0;
  3232. /*
  3233. * Reset a stream reader in order to read the main stream
  3234. * of BCJ2.
  3235. */
  3236. zip->pack_stream_remaining = 1;
  3237. zip->pack_stream_index = (unsigned)folder->packIndex;
  3238. zip->folder_outbytes_remaining =
  3239. folder_uncompressed_size(folder);
  3240. zip->uncompressed_buffer_bytes_remaining = 0;
  3241. }
  3242. /*
  3243. * Initialize the decompressor for the new folder's pack streams.
  3244. */
  3245. r = init_decompression(a, zip, coder1, coder2);
  3246. if (r != ARCHIVE_OK)
  3247. return (ARCHIVE_FATAL);
  3248. return (ARCHIVE_OK);
  3249. }
  3250. static int64_t
  3251. skip_stream(struct archive_read *a, size_t skip_bytes)
  3252. {
  3253. struct _7zip *zip = (struct _7zip *)a->format->data;
  3254. const void *p;
  3255. int64_t skipped_bytes;
  3256. size_t bytes = skip_bytes;
  3257. if (zip->folder_index == 0) {
  3258. /*
  3259. * Optimization for a list mode.
  3260. * Avoid unnecessary decoding operations.
  3261. */
  3262. zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
  3263. += skip_bytes;
  3264. return (skip_bytes);
  3265. }
  3266. while (bytes) {
  3267. skipped_bytes = read_stream(a, &p, bytes, 0);
  3268. if (skipped_bytes < 0)
  3269. return (skipped_bytes);
  3270. if (skipped_bytes == 0) {
  3271. archive_set_error(&a->archive,
  3272. ARCHIVE_ERRNO_FILE_FORMAT,
  3273. "Truncated 7-Zip file body");
  3274. return (ARCHIVE_FATAL);
  3275. }
  3276. bytes -= (size_t)skipped_bytes;
  3277. if (zip->pack_stream_bytes_unconsumed)
  3278. read_consume(a);
  3279. }
  3280. return (skip_bytes);
  3281. }
  3282. /*
  3283. * Brought from LZMA SDK.
  3284. *
  3285. * Bra86.c -- Converter for x86 code (BCJ)
  3286. * 2008-10-04 : Igor Pavlov : Public domain
  3287. *
  3288. */
  3289. #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
  3290. static void
  3291. x86_Init(struct _7zip *zip)
  3292. {
  3293. zip->bcj_state = 0;
  3294. zip->bcj_prevPosT = (size_t)0 - 1;
  3295. zip->bcj_prevMask = 0;
  3296. zip->bcj_ip = 5;
  3297. }
  3298. static size_t
  3299. x86_Convert(struct _7zip *zip, uint8_t *data, size_t size)
  3300. {
  3301. static const uint8_t kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
  3302. static const uint8_t kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
  3303. size_t bufferPos, prevPosT;
  3304. uint32_t ip, prevMask;
  3305. if (size < 5)
  3306. return 0;
  3307. bufferPos = 0;
  3308. prevPosT = zip->bcj_prevPosT;
  3309. prevMask = zip->bcj_prevMask;
  3310. ip = zip->bcj_ip;
  3311. for (;;) {
  3312. uint8_t *p = data + bufferPos;
  3313. uint8_t *limit = data + size - 4;
  3314. for (; p < limit; p++)
  3315. if ((*p & 0xFE) == 0xE8)
  3316. break;
  3317. bufferPos = (size_t)(p - data);
  3318. if (p >= limit)
  3319. break;
  3320. prevPosT = bufferPos - prevPosT;
  3321. if (prevPosT > 3)
  3322. prevMask = 0;
  3323. else {
  3324. prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
  3325. if (prevMask != 0) {
  3326. unsigned char b =
  3327. p[4 - kMaskToBitNumber[prevMask]];
  3328. if (!kMaskToAllowedStatus[prevMask] ||
  3329. Test86MSByte(b)) {
  3330. prevPosT = bufferPos;
  3331. prevMask = ((prevMask << 1) & 0x7) | 1;
  3332. bufferPos++;
  3333. continue;
  3334. }
  3335. }
  3336. }
  3337. prevPosT = bufferPos;
  3338. if (Test86MSByte(p[4])) {
  3339. uint32_t src = ((uint32_t)p[4] << 24) |
  3340. ((uint32_t)p[3] << 16) | ((uint32_t)p[2] << 8) |
  3341. ((uint32_t)p[1]);
  3342. uint32_t dest;
  3343. for (;;) {
  3344. uint8_t b;
  3345. int b_index;
  3346. dest = src - (ip + (uint32_t)bufferPos);
  3347. if (prevMask == 0)
  3348. break;
  3349. b_index = kMaskToBitNumber[prevMask] * 8;
  3350. b = (uint8_t)(dest >> (24 - b_index));
  3351. if (!Test86MSByte(b))
  3352. break;
  3353. src = dest ^ ((1 << (32 - b_index)) - 1);
  3354. }
  3355. p[4] = (uint8_t)(~(((dest >> 24) & 1) - 1));
  3356. p[3] = (uint8_t)(dest >> 16);
  3357. p[2] = (uint8_t)(dest >> 8);
  3358. p[1] = (uint8_t)dest;
  3359. bufferPos += 5;
  3360. } else {
  3361. prevMask = ((prevMask << 1) & 0x7) | 1;
  3362. bufferPos++;
  3363. }
  3364. }
  3365. zip->bcj_prevPosT = prevPosT;
  3366. zip->bcj_prevMask = prevMask;
  3367. zip->bcj_ip += (uint32_t)bufferPos;
  3368. return (bufferPos);
  3369. }
  3370. /*
  3371. * Brought from LZMA SDK.
  3372. *
  3373. * Bcj2.c -- Converter for x86 code (BCJ2)
  3374. * 2008-10-04 : Igor Pavlov : Public domain
  3375. *
  3376. */
  3377. #define SZ_ERROR_DATA ARCHIVE_FAILED
  3378. #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
  3379. #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
  3380. #define kNumTopBits 24
  3381. #define kTopValue ((uint32_t)1 << kNumTopBits)
  3382. #define kNumBitModelTotalBits 11
  3383. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  3384. #define kNumMoveBits 5
  3385. #define RC_READ_BYTE (*buffer++)
  3386. #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
  3387. #define RC_INIT2 zip->bcj2_code = 0; zip->bcj2_range = 0xFFFFFFFF; \
  3388. { int ii; for (ii = 0; ii < 5; ii++) { RC_TEST; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }}
  3389. #define NORMALIZE if (zip->bcj2_range < kTopValue) { RC_TEST; zip->bcj2_range <<= 8; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }
  3390. #define IF_BIT_0(p) ttt = *(p); bound = (zip->bcj2_range >> kNumBitModelTotalBits) * ttt; if (zip->bcj2_code < bound)
  3391. #define UPDATE_0(p) zip->bcj2_range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
  3392. #define UPDATE_1(p) zip->bcj2_range -= bound; zip->bcj2_code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
  3393. static ssize_t
  3394. Bcj2_Decode(struct _7zip *zip, uint8_t *outBuf, size_t outSize)
  3395. {
  3396. size_t inPos = 0, outPos = 0;
  3397. const uint8_t *buf0, *buf1, *buf2, *buf3;
  3398. size_t size0, size1, size2, size3;
  3399. const uint8_t *buffer, *bufferLim;
  3400. unsigned int i, j;
  3401. size0 = zip->tmp_stream_bytes_remaining;
  3402. buf0 = zip->tmp_stream_buff + zip->tmp_stream_bytes_avail - size0;
  3403. size1 = zip->sub_stream_bytes_remaining[0];
  3404. buf1 = zip->sub_stream_buff[0] + zip->sub_stream_size[0] - size1;
  3405. size2 = zip->sub_stream_bytes_remaining[1];
  3406. buf2 = zip->sub_stream_buff[1] + zip->sub_stream_size[1] - size2;
  3407. size3 = zip->sub_stream_bytes_remaining[2];
  3408. buf3 = zip->sub_stream_buff[2] + zip->sub_stream_size[2] - size3;
  3409. buffer = buf3;
  3410. bufferLim = buffer + size3;
  3411. if (zip->bcj_state == 0) {
  3412. /*
  3413. * Initialize.
  3414. */
  3415. zip->bcj2_prevByte = 0;
  3416. for (i = 0;
  3417. i < sizeof(zip->bcj2_p) / sizeof(zip->bcj2_p[0]); i++)
  3418. zip->bcj2_p[i] = kBitModelTotal >> 1;
  3419. RC_INIT2;
  3420. zip->bcj_state = 1;
  3421. }
  3422. /*
  3423. * Gather the odd bytes of a previous call.
  3424. */
  3425. for (i = 0; zip->odd_bcj_size > 0 && outPos < outSize; i++) {
  3426. outBuf[outPos++] = zip->odd_bcj[i];
  3427. zip->odd_bcj_size--;
  3428. }
  3429. if (outSize == 0) {
  3430. zip->bcj2_outPos += outPos;
  3431. return (outPos);
  3432. }
  3433. for (;;) {
  3434. uint8_t b;
  3435. CProb *prob;
  3436. uint32_t bound;
  3437. uint32_t ttt;
  3438. size_t limit = size0 - inPos;
  3439. if (outSize - outPos < limit)
  3440. limit = outSize - outPos;
  3441. if (zip->bcj_state == 1) {
  3442. while (limit != 0) {
  3443. uint8_t bb = buf0[inPos];
  3444. outBuf[outPos++] = bb;
  3445. if (IsJ(zip->bcj2_prevByte, bb)) {
  3446. zip->bcj_state = 2;
  3447. break;
  3448. }
  3449. inPos++;
  3450. zip->bcj2_prevByte = bb;
  3451. limit--;
  3452. }
  3453. }
  3454. if (limit == 0 || outPos == outSize)
  3455. break;
  3456. zip->bcj_state = 1;
  3457. b = buf0[inPos++];
  3458. if (b == 0xE8)
  3459. prob = zip->bcj2_p + zip->bcj2_prevByte;
  3460. else if (b == 0xE9)
  3461. prob = zip->bcj2_p + 256;
  3462. else
  3463. prob = zip->bcj2_p + 257;
  3464. IF_BIT_0(prob) {
  3465. UPDATE_0(prob)
  3466. zip->bcj2_prevByte = b;
  3467. } else {
  3468. uint32_t dest;
  3469. const uint8_t *v;
  3470. uint8_t out[4];
  3471. UPDATE_1(prob)
  3472. if (b == 0xE8) {
  3473. v = buf1;
  3474. if (size1 < 4)
  3475. return SZ_ERROR_DATA;
  3476. buf1 += 4;
  3477. size1 -= 4;
  3478. } else {
  3479. v = buf2;
  3480. if (size2 < 4)
  3481. return SZ_ERROR_DATA;
  3482. buf2 += 4;
  3483. size2 -= 4;
  3484. }
  3485. dest = (((uint32_t)v[0] << 24) |
  3486. ((uint32_t)v[1] << 16) |
  3487. ((uint32_t)v[2] << 8) |
  3488. ((uint32_t)v[3])) -
  3489. ((uint32_t)zip->bcj2_outPos + (uint32_t)outPos + 4);
  3490. out[0] = (uint8_t)dest;
  3491. out[1] = (uint8_t)(dest >> 8);
  3492. out[2] = (uint8_t)(dest >> 16);
  3493. out[3] = zip->bcj2_prevByte = (uint8_t)(dest >> 24);
  3494. for (i = 0; i < 4 && outPos < outSize; i++)
  3495. outBuf[outPos++] = out[i];
  3496. if (i < 4) {
  3497. /*
  3498. * Save odd bytes which we could not add into
  3499. * the output buffer because of out of space.
  3500. */
  3501. zip->odd_bcj_size = 4 -i;
  3502. for (; i < 4; i++) {
  3503. j = i - 4 + (unsigned)zip->odd_bcj_size;
  3504. zip->odd_bcj[j] = out[i];
  3505. }
  3506. break;
  3507. }
  3508. }
  3509. }
  3510. zip->tmp_stream_bytes_remaining -= inPos;
  3511. zip->sub_stream_bytes_remaining[0] = size1;
  3512. zip->sub_stream_bytes_remaining[1] = size2;
  3513. zip->sub_stream_bytes_remaining[2] = bufferLim - buffer;
  3514. zip->bcj2_outPos += outPos;
  3515. return ((ssize_t)outPos);
  3516. }