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