2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011 Andres Mejia
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
35 #include <zlib.h> /* crc32 */
40 #include "archive_crc32.h"
42 #include "archive_endian.h"
43 #include "archive_entry.h"
44 #include "archive_entry_locale.h"
45 #include "archive_ppmd7_private.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
49 /* RAR signature, also known as the mark header */
50 #define RAR_SIGNATURE "\x52\x61\x72\x21\x1A\x07\x00"
53 #define MARK_HEAD 0x72
54 #define MAIN_HEAD 0x73
55 #define FILE_HEAD 0x74
56 #define COMM_HEAD 0x75
59 #define PROTECT_HEAD 0x78
60 #define SIGN_HEAD 0x79
61 #define NEWSUB_HEAD 0x7a
62 #define ENDARC_HEAD 0x7b
64 /* Main Header Flags */
65 #define MHD_VOLUME 0x0001
66 #define MHD_COMMENT 0x0002
67 #define MHD_LOCK 0x0004
68 #define MHD_SOLID 0x0008
69 #define MHD_NEWNUMBERING 0x0010
71 #define MHD_PROTECT 0x0040
72 #define MHD_PASSWORD 0x0080
73 #define MHD_FIRSTVOLUME 0x0100
74 #define MHD_ENCRYPTVER 0x0200
76 /* Flags common to all headers */
77 #define HD_MARKDELETION 0x4000
78 #define HD_ADD_SIZE_PRESENT 0x8000
80 /* File Header Flags */
81 #define FHD_SPLIT_BEFORE 0x0001
82 #define FHD_SPLIT_AFTER 0x0002
83 #define FHD_PASSWORD 0x0004
84 #define FHD_COMMENT 0x0008
85 #define FHD_SOLID 0x0010
86 #define FHD_LARGE 0x0100
87 #define FHD_UNICODE 0x0200
88 #define FHD_SALT 0x0400
89 #define FHD_VERSION 0x0800
90 #define FHD_EXTTIME 0x1000
91 #define FHD_EXTFLAGS 0x2000
93 /* File dictionary sizes */
94 #define DICTIONARY_SIZE_64 0x00
95 #define DICTIONARY_SIZE_128 0x20
96 #define DICTIONARY_SIZE_256 0x40
97 #define DICTIONARY_SIZE_512 0x60
98 #define DICTIONARY_SIZE_1024 0x80
99 #define DICTIONARY_SIZE_2048 0xA0
100 #define DICTIONARY_SIZE_4096 0xC0
101 #define FILE_IS_DIRECTORY 0xE0
102 #define DICTIONARY_MASK FILE_IS_DIRECTORY
112 /* Compression Methods */
113 #define COMPRESS_METHOD_STORE 0x30
115 #define COMPRESS_METHOD_FASTEST 0x31
116 #define COMPRESS_METHOD_FAST 0x32
117 #define COMPRESS_METHOD_NORMAL 0x33
119 #define COMPRESS_METHOD_GOOD 0x34
120 #define COMPRESS_METHOD_BEST 0x35
122 #define CRC_POLYNOMIAL 0xEDB88320
124 #define NS_UNIT 10000000
126 #define DICTIONARY_MAX_SIZE 0x400000
128 #define MAINCODE_SIZE 299
129 #define OFFSETCODE_SIZE 60
130 #define LOWOFFSETCODE_SIZE 17
131 #define LENGTHCODE_SIZE 28
132 #define HUFFMAN_TABLE_SIZE \
133 MAINCODE_SIZE + OFFSETCODE_SIZE + LOWOFFSETCODE_SIZE + LENGTHCODE_SIZE
135 #define MAX_SYMBOL_LENGTH 0xF
136 #define MAX_SYMBOLS 20
139 * Considering L1,L2 cache miss and a calling of write system-call,
140 * the best size of the output buffer(uncompressed buffer) is 128K.
141 * If the structure of extracting process is changed, this value
142 * might be researched again.
144 #define UNP_BUFFER_SIZE (128 * 1024)
146 /* Define this here for non-Windows platforms */
147 #if !((defined(__WIN32__) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__))
148 #define FILE_ATTRIBUTE_DIRECTORY 0x10
151 /* Fields common to all headers */
160 /* Fields common to all file headers */
161 struct rar_file_header
174 struct huffman_tree_node
179 struct huffman_table_entry
187 struct huffman_tree_node *tree;
189 int numallocatedentries;
193 struct huffman_table_entry *table;
198 unsigned char *window;
203 struct data_block_offsets
206 int64_t start_offset;
212 /* Entries from main RAR header */
214 unsigned long file_crc;
219 /* File header entries */
220 char compression_method;
229 size_t filename_save_size;
230 size_t filename_allocated;
232 /* File header optional entries */
241 /* Fields to help with tracking decompression of files. */
242 int64_t bytes_unconsumed;
243 int64_t bytes_remaining;
244 int64_t bytes_uncopied;
246 int64_t offset_outgoing;
249 unsigned int unp_offset;
250 unsigned int unp_buffer_size;
251 unsigned char *unp_buffer;
252 unsigned int dictionary_size;
253 char start_new_block;
255 unsigned long crc_calculated;
256 int found_first_header;
257 char has_endarc_header;
258 struct data_block_offsets *dbo;
263 struct huffman_code maincode;
264 struct huffman_code offsetcode;
265 struct huffman_code lowoffsetcode;
266 struct huffman_code lengthcode;
267 unsigned char lengthtable[HUFFMAN_TABLE_SIZE];
269 char output_last_match;
270 unsigned int lastlength;
271 unsigned int lastoffset;
272 unsigned int oldoffset[4];
273 unsigned int lastlowoffset;
274 unsigned int numlowoffsetrepeats;
276 char start_new_table;
278 /* PPMd Variant H members */
283 CPpmd7 ppmd7_context;
284 CPpmd7z_RangeDec range_dec;
288 * String conversion object.
290 int init_default_conversion;
291 struct archive_string_conv *sconv_default;
292 struct archive_string_conv *opt_sconv;
293 struct archive_string_conv *sconv_utf8;
294 struct archive_string_conv *sconv_utf16be;
300 #define CACHE_TYPE uint64_t
301 #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
303 CACHE_TYPE cache_buffer;
304 /* Indicates how many bits avail in cache_buffer. */
307 const unsigned char *next_in;
311 * Custom field to denote that this archive contains encrypted entries
313 int has_encrypted_entries;
316 static int archive_read_support_format_rar_capabilities(struct archive_read *);
317 static int archive_read_format_rar_has_encrypted_entries(struct archive_read *);
318 static int archive_read_format_rar_bid(struct archive_read *, int);
319 static int archive_read_format_rar_options(struct archive_read *,
320 const char *, const char *);
321 static int archive_read_format_rar_read_header(struct archive_read *,
322 struct archive_entry *);
323 static int archive_read_format_rar_read_data(struct archive_read *,
324 const void **, size_t *, int64_t *);
325 static int archive_read_format_rar_read_data_skip(struct archive_read *a);
326 static int64_t archive_read_format_rar_seek_data(struct archive_read *, int64_t,
328 static int archive_read_format_rar_cleanup(struct archive_read *);
330 /* Support functions */
331 static int read_header(struct archive_read *, struct archive_entry *, char);
332 static time_t get_time(int);
333 static int read_exttime(const char *, struct rar *, const char *);
334 static int read_symlink_stored(struct archive_read *, struct archive_entry *,
335 struct archive_string_conv *);
336 static int read_data_stored(struct archive_read *, const void **, size_t *,
338 static int read_data_compressed(struct archive_read *, const void **, size_t *,
340 static int rar_br_preparation(struct archive_read *, struct rar_br *);
341 static int parse_codes(struct archive_read *);
342 static void free_codes(struct archive_read *);
343 static int read_next_symbol(struct archive_read *, struct huffman_code *);
344 static int create_code(struct archive_read *, struct huffman_code *,
345 unsigned char *, int, char);
346 static int add_value(struct archive_read *, struct huffman_code *, int, int,
348 static int new_node(struct huffman_code *);
349 static int make_table(struct archive_read *, struct huffman_code *);
350 static int make_table_recurse(struct archive_read *, struct huffman_code *, int,
351 struct huffman_table_entry *, int, int);
352 static int64_t expand(struct archive_read *, int64_t);
353 static int copy_from_lzss_window(struct archive_read *, const void **,
355 static const void *rar_read_ahead(struct archive_read *, size_t, ssize_t *);
360 /* Check that the cache buffer has enough bits. */
361 #define rar_br_has(br, n) ((br)->cache_avail >= n)
362 /* Get compressed data by bit. */
363 #define rar_br_bits(br, n) \
364 (((uint32_t)((br)->cache_buffer >> \
365 ((br)->cache_avail - (n)))) & cache_masks[n])
366 #define rar_br_bits_forced(br, n) \
367 (((uint32_t)((br)->cache_buffer << \
368 ((n) - (br)->cache_avail))) & cache_masks[n])
369 /* Read ahead to make sure the cache buffer has enough compressed data we
371 * True : completed, there is enough data in the cache buffer.
372 * False : there is no data in the stream. */
373 #define rar_br_read_ahead(a, br, n) \
374 ((rar_br_has(br, (n)) || rar_br_fillup(a, br)) || rar_br_has(br, (n)))
375 /* Notify how many bits we consumed. */
376 #define rar_br_consume(br, n) ((br)->cache_avail -= (n))
377 #define rar_br_consume_unalined_bits(br) ((br)->cache_avail &= ~7)
379 static const uint32_t cache_masks[] = {
380 0x00000000, 0x00000001, 0x00000003, 0x00000007,
381 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
382 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
383 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
384 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
385 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
386 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
387 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
388 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
392 * Shift away used bits in the cache data and fill it up with following bits.
393 * Call this when cache buffer does not have enough bits you need.
395 * Returns 1 if the cache buffer is full.
396 * Returns 0 if the cache buffer is not full; input buffer is empty.
399 rar_br_fillup(struct archive_read *a, struct rar_br *br)
401 struct rar *rar = (struct rar *)(a->format->data);
402 int n = CACHE_BITS - br->cache_avail;
407 if (br->avail_in >= 8) {
409 ((uint64_t)br->next_in[0]) << 56 |
410 ((uint64_t)br->next_in[1]) << 48 |
411 ((uint64_t)br->next_in[2]) << 40 |
412 ((uint64_t)br->next_in[3]) << 32 |
413 ((uint32_t)br->next_in[4]) << 24 |
414 ((uint32_t)br->next_in[5]) << 16 |
415 ((uint32_t)br->next_in[6]) << 8 |
416 (uint32_t)br->next_in[7];
419 br->cache_avail += 8 * 8;
420 rar->bytes_unconsumed += 8;
421 rar->bytes_remaining -= 8;
426 if (br->avail_in >= 7) {
428 (br->cache_buffer << 56) |
429 ((uint64_t)br->next_in[0]) << 48 |
430 ((uint64_t)br->next_in[1]) << 40 |
431 ((uint64_t)br->next_in[2]) << 32 |
432 ((uint32_t)br->next_in[3]) << 24 |
433 ((uint32_t)br->next_in[4]) << 16 |
434 ((uint32_t)br->next_in[5]) << 8 |
435 (uint32_t)br->next_in[6];
438 br->cache_avail += 7 * 8;
439 rar->bytes_unconsumed += 7;
440 rar->bytes_remaining -= 7;
445 if (br->avail_in >= 6) {
447 (br->cache_buffer << 48) |
448 ((uint64_t)br->next_in[0]) << 40 |
449 ((uint64_t)br->next_in[1]) << 32 |
450 ((uint32_t)br->next_in[2]) << 24 |
451 ((uint32_t)br->next_in[3]) << 16 |
452 ((uint32_t)br->next_in[4]) << 8 |
453 (uint32_t)br->next_in[5];
456 br->cache_avail += 6 * 8;
457 rar->bytes_unconsumed += 6;
458 rar->bytes_remaining -= 6;
463 /* We have enough compressed data in
464 * the cache buffer.*/
469 if (br->avail_in <= 0) {
471 if (rar->bytes_unconsumed > 0) {
472 /* Consume as much as the decompressor
474 __archive_read_consume(a, rar->bytes_unconsumed);
475 rar->bytes_unconsumed = 0;
477 br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
478 if (br->next_in == NULL)
480 if (br->avail_in == 0)
484 (br->cache_buffer << 8) | *br->next_in++;
486 br->cache_avail += 8;
488 rar->bytes_unconsumed++;
489 rar->bytes_remaining--;
494 rar_br_preparation(struct archive_read *a, struct rar_br *br)
496 struct rar *rar = (struct rar *)(a->format->data);
498 if (rar->bytes_remaining > 0) {
499 br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
500 if (br->next_in == NULL) {
501 archive_set_error(&a->archive,
502 ARCHIVE_ERRNO_FILE_FORMAT,
503 "Truncated RAR file data");
504 return (ARCHIVE_FATAL);
506 if (br->cache_avail == 0)
507 (void)rar_br_fillup(a, br);
512 /* Find last bit set */
514 rar_fls(unsigned int word)
520 word |= (word >> 16);
521 return word - (word >> 1);
525 static inline int64_t
526 lzss_position(struct lzss *lzss)
528 return lzss->position;
532 lzss_mask(struct lzss *lzss)
538 lzss_size(struct lzss *lzss)
540 return lzss->mask + 1;
544 lzss_offset_for_position(struct lzss *lzss, int64_t pos)
546 return (int)(pos & lzss->mask);
549 static inline unsigned char *
550 lzss_pointer_for_position(struct lzss *lzss, int64_t pos)
552 return &lzss->window[lzss_offset_for_position(lzss, pos)];
556 lzss_current_offset(struct lzss *lzss)
558 return lzss_offset_for_position(lzss, lzss->position);
561 static inline uint8_t *
562 lzss_current_pointer(struct lzss *lzss)
564 return lzss_pointer_for_position(lzss, lzss->position);
568 lzss_emit_literal(struct rar *rar, uint8_t literal)
570 *lzss_current_pointer(&rar->lzss) = literal;
571 rar->lzss.position++;
575 lzss_emit_match(struct rar *rar, int offset, int length)
577 int dstoffs = lzss_current_offset(&rar->lzss);
578 int srcoffs = (dstoffs - offset) & lzss_mask(&rar->lzss);
579 int l, li, remaining;
580 unsigned char *d, *s;
583 while (remaining > 0) {
585 if (dstoffs > srcoffs) {
586 if (l > lzss_size(&rar->lzss) - dstoffs)
587 l = lzss_size(&rar->lzss) - dstoffs;
589 if (l > lzss_size(&rar->lzss) - srcoffs)
590 l = lzss_size(&rar->lzss) - srcoffs;
592 d = &(rar->lzss.window[dstoffs]);
593 s = &(rar->lzss.window[srcoffs]);
594 if ((dstoffs + l < srcoffs) || (srcoffs + l < dstoffs))
597 for (li = 0; li < l; li++)
601 dstoffs = (dstoffs + l) & lzss_mask(&(rar->lzss));
602 srcoffs = (srcoffs + l) & lzss_mask(&(rar->lzss));
604 rar->lzss.position += length;
610 struct archive_read *a = ((IByteIn*)p)->a;
611 struct rar *rar = (struct rar *)(a->format->data);
612 struct rar_br *br = &(rar->br);
614 if (!rar_br_read_ahead(a, br, 8))
616 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
617 "Truncated RAR file data");
621 b = rar_br_bits(br, 8);
622 rar_br_consume(br, 8);
627 archive_read_support_format_rar(struct archive *_a)
629 struct archive_read *a = (struct archive_read *)_a;
633 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
634 "archive_read_support_format_rar");
636 rar = (struct rar *)calloc(sizeof(*rar), 1);
639 archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
640 return (ARCHIVE_FATAL);
644 * Until enough data has been read, we cannot tell about
645 * any encrypted entries yet.
647 rar->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
649 r = __archive_read_register_format(a,
652 archive_read_format_rar_bid,
653 archive_read_format_rar_options,
654 archive_read_format_rar_read_header,
655 archive_read_format_rar_read_data,
656 archive_read_format_rar_read_data_skip,
657 archive_read_format_rar_seek_data,
658 archive_read_format_rar_cleanup,
659 archive_read_support_format_rar_capabilities,
660 archive_read_format_rar_has_encrypted_entries);
668 archive_read_support_format_rar_capabilities(struct archive_read * a)
670 (void)a; /* UNUSED */
671 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
672 | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
676 archive_read_format_rar_has_encrypted_entries(struct archive_read *_a)
678 if (_a && _a->format) {
679 struct rar * rar = (struct rar *)_a->format->data;
681 return rar->has_encrypted_entries;
684 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
689 archive_read_format_rar_bid(struct archive_read *a, int best_bid)
693 /* If there's already a bid > 30, we'll never win. */
697 if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
700 if (memcmp(p, RAR_SIGNATURE, 7) == 0)
703 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
704 /* This is a PE file */
705 ssize_t offset = 0x10000;
706 ssize_t window = 4096;
708 while (offset + window <= (1024 * 128)) {
709 const char *buff = __archive_read_ahead(a, offset + window, &bytes_avail);
711 /* Remaining bytes are less than window. */
718 while (p + 7 < buff + bytes_avail) {
719 if (memcmp(p, RAR_SIGNATURE, 7) == 0)
730 skip_sfx(struct archive_read *a)
735 ssize_t bytes, window;
739 while (total + window <= (1024 * 128)) {
740 h = __archive_read_ahead(a, window, &bytes);
742 /* Remaining bytes are less than window. */
754 * Scan ahead until we find something that looks
755 * like the RAR header.
758 if (memcmp(p, RAR_SIGNATURE, 7) == 0) {
759 skip = p - (const char *)h;
760 __archive_read_consume(a, skip);
765 skip = p - (const char *)h;
766 __archive_read_consume(a, skip);
770 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
771 "Couldn't find out RAR header");
772 return (ARCHIVE_FATAL);
776 archive_read_format_rar_options(struct archive_read *a,
777 const char *key, const char *val)
780 int ret = ARCHIVE_FAILED;
782 rar = (struct rar *)(a->format->data);
783 if (strcmp(key, "hdrcharset") == 0) {
784 if (val == NULL || val[0] == 0)
785 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
786 "rar: hdrcharset option needs a character-set name");
789 archive_string_conversion_from_charset(
790 &a->archive, val, 0);
791 if (rar->opt_sconv != NULL)
799 /* Note: The "warn" return is just to inform the options
800 * supervisor that we didn't handle it. It will generate
801 * a suitable error if no one used this option. */
802 return (ARCHIVE_WARN);
806 archive_read_format_rar_read_header(struct archive_read *a,
807 struct archive_entry *entry)
816 unsigned long crc32_expected;
818 a->archive.archive_format = ARCHIVE_FORMAT_RAR;
819 if (a->archive.archive_format_name == NULL)
820 a->archive.archive_format_name = "RAR";
822 rar = (struct rar *)(a->format->data);
825 * It should be sufficient to call archive_read_next_header() for
826 * a reader to determine if an entry is encrypted or not. If the
827 * encryption of an entry is only detectable when calling
828 * archive_read_data(), so be it. We'll do the same check there
831 if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
832 rar->has_encrypted_entries = 0;
835 /* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
838 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
839 return (ARCHIVE_EOF);
842 if (rar->found_first_header == 0 &&
843 ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0)) {
844 /* This is an executable ? Must be self-extracting... */
846 if (ret < ARCHIVE_WARN)
849 rar->found_first_header = 1;
853 unsigned long crc32_val;
855 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
856 return (ARCHIVE_FATAL);
863 if (memcmp(p, RAR_SIGNATURE, 7) != 0) {
864 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
865 "Invalid marker header");
866 return (ARCHIVE_FATAL);
868 __archive_read_consume(a, 7);
872 rar->main_flags = archive_le16dec(p + 3);
873 skip = archive_le16dec(p + 5);
874 if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
875 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
876 "Invalid header size");
877 return (ARCHIVE_FATAL);
879 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
880 return (ARCHIVE_FATAL);
882 memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
883 memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
884 sizeof(rar->reserved2));
885 if (rar->main_flags & MHD_ENCRYPTVER) {
886 if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)+1) {
887 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
888 "Invalid header size");
889 return (ARCHIVE_FATAL);
891 rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
892 sizeof(rar->reserved2));
895 /* Main header is password encrypted, so we cannot read any
896 file names or any other info about files from the header. */
897 if (rar->main_flags & MHD_PASSWORD)
899 archive_entry_set_is_metadata_encrypted(entry, 1);
900 archive_entry_set_is_data_encrypted(entry, 1);
901 rar->has_encrypted_entries = 1;
902 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
903 "RAR encryption support unavailable.");
904 return (ARCHIVE_FATAL);
907 crc32_val = crc32(0, (const unsigned char *)p + 2, (unsigned)skip - 2);
908 if ((crc32_val & 0xffff) != archive_le16dec(p)) {
909 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
911 return (ARCHIVE_FATAL);
913 __archive_read_consume(a, skip);
917 return read_header(a, entry, head_type);
925 flags = archive_le16dec(p + 3);
926 skip = archive_le16dec(p + 5);
928 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
929 "Invalid header size too small");
930 return (ARCHIVE_FATAL);
932 if (flags & HD_ADD_SIZE_PRESENT)
935 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
936 "Invalid header size too small");
937 return (ARCHIVE_FATAL);
939 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
940 return (ARCHIVE_FATAL);
942 skip += archive_le32dec(p + 7);
945 /* Skip over the 2-byte CRC at the beginning of the header. */
946 crc32_expected = archive_le16dec(p);
947 __archive_read_consume(a, 2);
950 /* Skim the entire header and compute the CRC. */
953 size_t to_read = skip;
955 if (to_read > 32 * 1024) {
958 if ((h = __archive_read_ahead(a, to_read, &did_read)) == NULL) {
959 return (ARCHIVE_FATAL);
962 crc32_val = crc32(crc32_val, (const unsigned char *)p, (unsigned)did_read);
963 __archive_read_consume(a, did_read);
966 if ((crc32_val & 0xffff) != crc32_expected) {
967 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
969 return (ARCHIVE_FATAL);
971 if (head_type == ENDARC_HEAD)
972 return (ARCHIVE_EOF);
976 if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
981 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
983 return (ARCHIVE_FATAL);
989 archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
990 size_t *size, int64_t *offset)
992 struct rar *rar = (struct rar *)(a->format->data);
995 if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
996 rar->has_encrypted_entries = 0;
999 if (rar->bytes_unconsumed > 0) {
1000 /* Consume as much as the decompressor actually used. */
1001 __archive_read_consume(a, rar->bytes_unconsumed);
1002 rar->bytes_unconsumed = 0;
1006 if (rar->entry_eof || rar->offset_seek >= rar->unp_size) {
1008 *offset = rar->offset;
1009 if (*offset < rar->unp_size)
1010 *offset = rar->unp_size;
1011 return (ARCHIVE_EOF);
1014 switch (rar->compression_method)
1016 case COMPRESS_METHOD_STORE:
1017 ret = read_data_stored(a, buff, size, offset);
1020 case COMPRESS_METHOD_FASTEST:
1021 case COMPRESS_METHOD_FAST:
1022 case COMPRESS_METHOD_NORMAL:
1023 case COMPRESS_METHOD_GOOD:
1024 case COMPRESS_METHOD_BEST:
1025 ret = read_data_compressed(a, buff, size, offset);
1026 if (ret != ARCHIVE_OK && ret != ARCHIVE_WARN)
1027 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1031 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1032 "Unsupported compression method for RAR file.");
1033 ret = ARCHIVE_FATAL;
1040 archive_read_format_rar_read_data_skip(struct archive_read *a)
1043 int64_t bytes_skipped;
1046 rar = (struct rar *)(a->format->data);
1048 if (rar->bytes_unconsumed > 0) {
1049 /* Consume as much as the decompressor actually used. */
1050 __archive_read_consume(a, rar->bytes_unconsumed);
1051 rar->bytes_unconsumed = 0;
1054 if (rar->bytes_remaining > 0) {
1055 bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
1056 if (bytes_skipped < 0)
1057 return (ARCHIVE_FATAL);
1060 /* Compressed data to skip must be read from each header in a multivolume
1063 if (rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER)
1065 ret = archive_read_format_rar_read_header(a, a->entry);
1066 if (ret == (ARCHIVE_EOF))
1067 ret = archive_read_format_rar_read_header(a, a->entry);
1068 if (ret != (ARCHIVE_OK))
1070 return archive_read_format_rar_read_data_skip(a);
1073 return (ARCHIVE_OK);
1077 archive_read_format_rar_seek_data(struct archive_read *a, int64_t offset,
1080 int64_t client_offset, ret;
1082 struct rar *rar = (struct rar *)(a->format->data);
1084 if (rar->compression_method == COMPRESS_METHOD_STORE)
1086 /* Modify the offset for use with SEEK_SET */
1090 client_offset = rar->offset_seek;
1093 client_offset = rar->unp_size;
1099 client_offset += offset;
1100 if (client_offset < 0)
1102 /* Can't seek past beginning of data block */
1105 else if (client_offset > rar->unp_size)
1108 * Set the returned offset but only seek to the end of
1111 rar->offset_seek = client_offset;
1112 client_offset = rar->unp_size;
1115 client_offset += rar->dbo[0].start_offset;
1117 while (i < rar->cursor)
1120 client_offset += rar->dbo[i].start_offset - rar->dbo[i-1].end_offset;
1122 if (rar->main_flags & MHD_VOLUME)
1124 /* Find the appropriate offset among the multivolume archive */
1127 if (client_offset < rar->dbo[rar->cursor].start_offset &&
1128 rar->file_flags & FHD_SPLIT_BEFORE)
1130 /* Search backwards for the correct data block */
1131 if (rar->cursor == 0)
1133 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1134 "Attempt to seek past beginning of RAR data block");
1135 return (ARCHIVE_FAILED);
1138 client_offset -= rar->dbo[rar->cursor+1].start_offset -
1139 rar->dbo[rar->cursor].end_offset;
1140 if (client_offset < rar->dbo[rar->cursor].start_offset)
1142 ret = __archive_read_seek(a, rar->dbo[rar->cursor].start_offset -
1143 rar->dbo[rar->cursor].header_size, SEEK_SET);
1144 if (ret < (ARCHIVE_OK))
1146 ret = archive_read_format_rar_read_header(a, a->entry);
1147 if (ret != (ARCHIVE_OK))
1149 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1150 "Error during seek of RAR file");
1151 return (ARCHIVE_FAILED);
1156 else if (client_offset > rar->dbo[rar->cursor].end_offset &&
1157 rar->file_flags & FHD_SPLIT_AFTER)
1159 /* Search forward for the correct data block */
1161 if (rar->cursor < rar->nodes &&
1162 client_offset > rar->dbo[rar->cursor].end_offset)
1164 client_offset += rar->dbo[rar->cursor].start_offset -
1165 rar->dbo[rar->cursor-1].end_offset;
1169 ret = __archive_read_seek(a, rar->dbo[rar->cursor].end_offset,
1171 if (ret < (ARCHIVE_OK))
1173 ret = archive_read_format_rar_read_header(a, a->entry);
1174 if (ret == (ARCHIVE_EOF))
1176 rar->has_endarc_header = 1;
1177 ret = archive_read_format_rar_read_header(a, a->entry);
1179 if (ret != (ARCHIVE_OK))
1181 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1182 "Error during seek of RAR file");
1183 return (ARCHIVE_FAILED);
1185 client_offset += rar->dbo[rar->cursor].start_offset -
1186 rar->dbo[rar->cursor-1].end_offset;
1193 ret = __archive_read_seek(a, client_offset, SEEK_SET);
1194 if (ret < (ARCHIVE_OK))
1196 rar->bytes_remaining = rar->dbo[rar->cursor].end_offset - ret;
1201 ret -= rar->dbo[i+1].start_offset - rar->dbo[i].end_offset;
1203 ret -= rar->dbo[0].start_offset;
1205 /* Always restart reading the file after a seek */
1206 __archive_reset_read_data(&a->archive);
1208 rar->bytes_unconsumed = 0;
1212 * If a seek past the end of file was requested, return the requested
1215 if (ret == rar->unp_size && rar->offset_seek > rar->unp_size)
1216 return rar->offset_seek;
1218 /* Return the new offset */
1219 rar->offset_seek = ret;
1220 return rar->offset_seek;
1224 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1225 "Seeking of compressed RAR files is unsupported");
1227 return (ARCHIVE_FAILED);
1231 archive_read_format_rar_cleanup(struct archive_read *a)
1235 rar = (struct rar *)(a->format->data);
1237 free(rar->filename);
1238 free(rar->filename_save);
1240 free(rar->unp_buffer);
1241 free(rar->lzss.window);
1242 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1244 (a->format->data) = NULL;
1245 return (ARCHIVE_OK);
1249 read_header(struct archive_read *a, struct archive_entry *entry,
1253 const char *p, *endp;
1255 struct rar_header rar_header;
1256 struct rar_file_header file_header;
1257 int64_t header_size;
1258 unsigned filename_size, end;
1261 char packed_size[8];
1264 struct archive_string_conv *sconv, *fn_sconv;
1265 unsigned long crc32_val;
1266 int ret = (ARCHIVE_OK), ret2;
1268 rar = (struct rar *)(a->format->data);
1270 /* Setup a string conversion object for non-rar-unicode filenames. */
1271 sconv = rar->opt_sconv;
1272 if (sconv == NULL) {
1273 if (!rar->init_default_conversion) {
1274 rar->sconv_default =
1275 archive_string_default_conversion_for_read(
1277 rar->init_default_conversion = 1;
1279 sconv = rar->sconv_default;
1283 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
1284 return (ARCHIVE_FATAL);
1286 memcpy(&rar_header, p, sizeof(rar_header));
1287 rar->file_flags = archive_le16dec(rar_header.flags);
1288 header_size = archive_le16dec(rar_header.size);
1289 if (header_size < (int64_t)sizeof(file_header) + 7) {
1290 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1291 "Invalid header size");
1292 return (ARCHIVE_FATAL);
1294 crc32_val = crc32(0, (const unsigned char *)p + 2, 7 - 2);
1295 __archive_read_consume(a, 7);
1297 if (!(rar->file_flags & FHD_SOLID))
1299 rar->compression_method = 0;
1300 rar->packed_size = 0;
1307 memset(&rar->salt, 0, sizeof(rar->salt));
1319 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1320 "RAR solid archive support unavailable.");
1321 return (ARCHIVE_FATAL);
1324 if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1325 return (ARCHIVE_FATAL);
1327 /* File Header CRC check. */
1328 crc32_val = crc32(crc32_val, h, (unsigned)(header_size - 7));
1329 if ((crc32_val & 0xffff) != archive_le16dec(rar_header.crc)) {
1330 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1331 "Header CRC error");
1332 return (ARCHIVE_FATAL);
1334 /* If no CRC error, Go on parsing File Header. */
1336 endp = p + header_size - 7;
1337 memcpy(&file_header, p, sizeof(file_header));
1338 p += sizeof(file_header);
1340 rar->compression_method = file_header.method;
1342 ttime = archive_le32dec(file_header.file_time);
1343 rar->mtime = get_time(ttime);
1345 rar->file_crc = archive_le32dec(file_header.file_crc);
1347 if (rar->file_flags & FHD_PASSWORD)
1349 archive_entry_set_is_data_encrypted(entry, 1);
1350 rar->has_encrypted_entries = 1;
1351 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1352 "RAR encryption support unavailable.");
1353 /* Since it is only the data part itself that is encrypted we can at least
1354 extract information about the currently processed entry and don't need
1355 to return ARCHIVE_FATAL here. */
1356 /*return (ARCHIVE_FATAL);*/
1359 if (rar->file_flags & FHD_LARGE)
1361 memcpy(packed_size, file_header.pack_size, 4);
1362 memcpy(packed_size + 4, p, 4); /* High pack size */
1364 memcpy(unp_size, file_header.unp_size, 4);
1365 memcpy(unp_size + 4, p, 4); /* High unpack size */
1367 rar->packed_size = archive_le64dec(&packed_size);
1368 rar->unp_size = archive_le64dec(&unp_size);
1372 rar->packed_size = archive_le32dec(file_header.pack_size);
1373 rar->unp_size = archive_le32dec(file_header.unp_size);
1376 if (rar->packed_size < 0 || rar->unp_size < 0)
1378 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1379 "Invalid sizes specified.");
1380 return (ARCHIVE_FATAL);
1383 rar->bytes_remaining = rar->packed_size;
1385 /* TODO: RARv3 subblocks contain comments. For now the complete block is
1386 * consumed at the end.
1388 if (head_type == NEWSUB_HEAD) {
1389 size_t distance = p - (const char *)h;
1390 header_size += rar->packed_size;
1391 /* Make sure we have the extended data. */
1392 if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1393 return (ARCHIVE_FATAL);
1395 endp = p + header_size - 7;
1399 filename_size = archive_le16dec(file_header.name_size);
1400 if (p + filename_size > endp) {
1401 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1402 "Invalid filename size");
1403 return (ARCHIVE_FATAL);
1405 if (rar->filename_allocated < filename_size * 2 + 2) {
1407 size_t newsize = filename_size * 2 + 2;
1408 newptr = realloc(rar->filename, newsize);
1409 if (newptr == NULL) {
1410 archive_set_error(&a->archive, ENOMEM,
1411 "Couldn't allocate memory.");
1412 return (ARCHIVE_FATAL);
1414 rar->filename = newptr;
1415 rar->filename_allocated = newsize;
1417 filename = rar->filename;
1418 memcpy(filename, p, filename_size);
1419 filename[filename_size] = '\0';
1420 if (rar->file_flags & FHD_UNICODE)
1422 if (filename_size != strlen(filename))
1424 unsigned char highbyte, flagbits, flagbyte;
1425 unsigned fn_end, offset;
1427 end = filename_size;
1428 fn_end = filename_size * 2;
1430 offset = (unsigned)strlen(filename) + 1;
1431 highbyte = *(p + offset++);
1434 while (offset < end && filename_size < fn_end)
1438 flagbyte = *(p + offset++);
1443 switch((flagbyte >> flagbits) & 3)
1446 filename[filename_size++] = '\0';
1447 filename[filename_size++] = *(p + offset++);
1450 filename[filename_size++] = highbyte;
1451 filename[filename_size++] = *(p + offset++);
1454 filename[filename_size++] = *(p + offset + 1);
1455 filename[filename_size++] = *(p + offset);
1461 uint8_t length = *(p + offset++);
1463 if (length & 0x80) {
1464 extra = *(p + offset++);
1465 high = (char)highbyte;
1468 length = (length & 0x7f) + 2;
1469 while (length && filename_size < fn_end) {
1470 unsigned cp = filename_size >> 1;
1471 filename[filename_size++] = high;
1472 filename[filename_size++] = p[cp] + extra;
1479 if (filename_size > fn_end) {
1480 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1481 "Invalid filename");
1482 return (ARCHIVE_FATAL);
1484 filename[filename_size++] = '\0';
1486 * Do not increment filename_size here as the computations below
1487 * add the space for the terminating NUL explicitly.
1489 filename[filename_size] = '\0';
1491 /* Decoded unicode form is UTF-16BE, so we have to update a string
1492 * conversion object for it. */
1493 if (rar->sconv_utf16be == NULL) {
1494 rar->sconv_utf16be = archive_string_conversion_from_charset(
1495 &a->archive, "UTF-16BE", 1);
1496 if (rar->sconv_utf16be == NULL)
1497 return (ARCHIVE_FATAL);
1499 fn_sconv = rar->sconv_utf16be;
1502 while (memcmp(strp, "\x00\x00", 2))
1504 if (!memcmp(strp, "\x00\\", 2))
1511 * If FHD_UNICODE is set but no unicode data, this file name form
1512 * is UTF-8, so we have to update a string conversion object for
1515 if (rar->sconv_utf8 == NULL) {
1516 rar->sconv_utf8 = archive_string_conversion_from_charset(
1517 &a->archive, "UTF-8", 1);
1518 if (rar->sconv_utf8 == NULL)
1519 return (ARCHIVE_FATAL);
1521 fn_sconv = rar->sconv_utf8;
1522 while ((strp = strchr(filename, '\\')) != NULL)
1530 while ((strp = strchr(filename, '\\')) != NULL)
1535 /* Split file in multivolume RAR. No more need to process header. */
1536 if (rar->filename_save &&
1537 filename_size == rar->filename_save_size &&
1538 !memcmp(rar->filename, rar->filename_save, filename_size + 1))
1540 __archive_read_consume(a, header_size - 7);
1542 if (rar->cursor >= rar->nodes)
1546 realloc(rar->dbo, sizeof(*rar->dbo) * rar->nodes)) == NULL)
1548 archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1549 return (ARCHIVE_FATAL);
1551 rar->dbo[rar->cursor].header_size = header_size;
1552 rar->dbo[rar->cursor].start_offset = -1;
1553 rar->dbo[rar->cursor].end_offset = -1;
1555 if (rar->dbo[rar->cursor].start_offset < 0)
1557 rar->dbo[rar->cursor].start_offset = a->filter->position;
1558 rar->dbo[rar->cursor].end_offset = rar->dbo[rar->cursor].start_offset +
1564 rar->filename_save = (char*)realloc(rar->filename_save,
1566 memcpy(rar->filename_save, rar->filename, filename_size + 1);
1567 rar->filename_save_size = filename_size;
1569 /* Set info for seeking */
1571 if ((rar->dbo = calloc(1, sizeof(*rar->dbo))) == NULL)
1573 archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1574 return (ARCHIVE_FATAL);
1576 rar->dbo[0].header_size = header_size;
1577 rar->dbo[0].start_offset = -1;
1578 rar->dbo[0].end_offset = -1;
1582 if (rar->file_flags & FHD_SALT)
1585 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1586 "Invalid header size");
1587 return (ARCHIVE_FATAL);
1589 memcpy(rar->salt, p, 8);
1593 if (rar->file_flags & FHD_EXTTIME) {
1594 if (read_exttime(p, rar, endp) < 0) {
1595 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1596 "Invalid header size");
1597 return (ARCHIVE_FATAL);
1601 __archive_read_consume(a, header_size - 7);
1602 rar->dbo[0].start_offset = a->filter->position;
1603 rar->dbo[0].end_offset = rar->dbo[0].start_offset + rar->packed_size;
1605 switch(file_header.host_os)
1610 rar->mode = archive_le32dec(file_header.file_attr);
1611 if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
1612 rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
1614 rar->mode = AE_IFREG;
1615 rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1621 rar->mode = archive_le32dec(file_header.file_attr);
1625 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1626 "Unknown file attributes from RAR file's host OS");
1627 return (ARCHIVE_FATAL);
1630 rar->bytes_uncopied = rar->bytes_unconsumed = 0;
1631 rar->lzss.position = rar->offset = 0;
1632 rar->offset_seek = 0;
1633 rar->dictionary_size = 0;
1634 rar->offset_outgoing = 0;
1635 rar->br.cache_avail = 0;
1636 rar->br.avail_in = 0;
1637 rar->crc_calculated = 0;
1640 rar->is_ppmd_block = 0;
1641 rar->start_new_table = 1;
1642 free(rar->unp_buffer);
1643 rar->unp_buffer = NULL;
1644 rar->unp_offset = 0;
1645 rar->unp_buffer_size = UNP_BUFFER_SIZE;
1646 memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
1647 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1648 rar->ppmd_valid = rar->ppmd_eod = 0;
1650 /* Don't set any archive entries for non-file header types */
1651 if (head_type == NEWSUB_HEAD)
1654 archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
1655 archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
1656 archive_entry_set_atime(entry, rar->atime, rar->ansec);
1657 archive_entry_set_size(entry, rar->unp_size);
1658 archive_entry_set_mode(entry, rar->mode);
1660 if (archive_entry_copy_pathname_l(entry, filename, filename_size, fn_sconv))
1662 if (errno == ENOMEM)
1664 archive_set_error(&a->archive, ENOMEM,
1665 "Can't allocate memory for Pathname");
1666 return (ARCHIVE_FATAL);
1668 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1669 "Pathname cannot be converted from %s to current locale.",
1670 archive_string_conversion_charset_name(fn_sconv));
1671 ret = (ARCHIVE_WARN);
1674 if (((rar->mode) & AE_IFMT) == AE_IFLNK)
1676 /* Make sure a symbolic-link file does not have its body. */
1677 rar->bytes_remaining = 0;
1678 archive_entry_set_size(entry, 0);
1680 /* Read a symbolic-link name. */
1681 if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
1687 if (rar->bytes_remaining == 0)
1697 tm.tm_sec = 2 * (ttime & 0x1f);
1698 tm.tm_min = (ttime >> 5) & 0x3f;
1699 tm.tm_hour = (ttime >> 11) & 0x1f;
1700 tm.tm_mday = (ttime >> 16) & 0x1f;
1701 tm.tm_mon = ((ttime >> 21) & 0x0f) - 1;
1702 tm.tm_year = ((ttime >> 25) & 0x7f) + 80;
1708 read_exttime(const char *p, struct rar *rar, const char *endp)
1710 unsigned rmode, flags, rem, j, count;
1718 flags = archive_le16dec(p);
1721 for (i = 3; i >= 0; i--)
1726 rmode = flags >> i * 4;
1733 ttime = archive_le32dec(p);
1734 t = get_time(ttime);
1739 if (p + count > endp)
1741 for (j = 0; j < count; j++)
1743 rem = (((unsigned)(unsigned char)*p) << 16) | (rem >> 8);
1747 nsec = tm->tm_sec + rem / NS_UNIT;
1771 rar->arcnsec = nsec;
1779 read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
1780 struct archive_string_conv *sconv)
1785 int ret = (ARCHIVE_OK);
1787 rar = (struct rar *)(a->format->data);
1788 if ((h = rar_read_ahead(a, (size_t)rar->packed_size, NULL)) == NULL)
1789 return (ARCHIVE_FATAL);
1792 if (archive_entry_copy_symlink_l(entry,
1793 p, (size_t)rar->packed_size, sconv))
1795 if (errno == ENOMEM)
1797 archive_set_error(&a->archive, ENOMEM,
1798 "Can't allocate memory for link");
1799 return (ARCHIVE_FATAL);
1801 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1802 "link cannot be converted from %s to current locale.",
1803 archive_string_conversion_charset_name(sconv));
1804 ret = (ARCHIVE_WARN);
1806 __archive_read_consume(a, rar->packed_size);
1811 read_data_stored(struct archive_read *a, const void **buff, size_t *size,
1815 ssize_t bytes_avail;
1817 rar = (struct rar *)(a->format->data);
1818 if (rar->bytes_remaining == 0 &&
1819 !(rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER))
1823 *offset = rar->offset;
1824 if (rar->file_crc != rar->crc_calculated) {
1825 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1827 return (ARCHIVE_FATAL);
1830 return (ARCHIVE_EOF);
1833 *buff = rar_read_ahead(a, 1, &bytes_avail);
1834 if (bytes_avail <= 0)
1836 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1837 "Truncated RAR file data");
1838 return (ARCHIVE_FATAL);
1841 *size = bytes_avail;
1842 *offset = rar->offset;
1843 rar->offset += bytes_avail;
1844 rar->offset_seek += bytes_avail;
1845 rar->bytes_remaining -= bytes_avail;
1846 rar->bytes_unconsumed = bytes_avail;
1847 /* Calculate File CRC. */
1848 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1849 (unsigned)bytes_avail);
1850 return (ARCHIVE_OK);
1854 read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
1858 int64_t start, end, actualend;
1860 int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
1862 rar = (struct rar *)(a->format->data);
1866 return (ARCHIVE_FATAL);
1867 if (rar->ppmd_eod ||
1868 (rar->dictionary_size && rar->offset >= rar->unp_size))
1870 if (rar->unp_offset > 0) {
1872 * We have unprocessed extracted data. write it out.
1874 *buff = rar->unp_buffer;
1875 *size = rar->unp_offset;
1876 *offset = rar->offset_outgoing;
1877 rar->offset_outgoing += *size;
1878 /* Calculate File CRC. */
1879 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1881 rar->unp_offset = 0;
1882 return (ARCHIVE_OK);
1886 *offset = rar->offset;
1887 if (rar->file_crc != rar->crc_calculated) {
1888 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1890 return (ARCHIVE_FATAL);
1893 return (ARCHIVE_EOF);
1896 if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
1898 if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
1899 bs = rar->unp_buffer_size - rar->unp_offset;
1901 bs = (size_t)rar->bytes_uncopied;
1902 ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
1903 if (ret != ARCHIVE_OK)
1906 rar->bytes_uncopied -= bs;
1907 if (*buff != NULL) {
1908 rar->unp_offset = 0;
1909 *size = rar->unp_buffer_size;
1910 *offset = rar->offset_outgoing;
1911 rar->offset_outgoing += *size;
1912 /* Calculate File CRC. */
1913 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1920 if (!rar->br.next_in &&
1921 (ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
1923 if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
1926 if (rar->is_ppmd_block)
1928 if ((sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1929 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1931 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1933 return (ARCHIVE_FATAL);
1935 if(sym != rar->ppmd_escape)
1937 lzss_emit_literal(rar, sym);
1938 rar->bytes_uncopied++;
1942 if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1943 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1945 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1947 return (ARCHIVE_FATAL);
1953 rar->start_new_table = 1;
1954 return read_data_compressed(a, buff, size, offset);
1957 rar->ppmd_eod = 1;/* End Of ppmd Data. */
1961 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1962 "Parsing filters is unsupported.");
1963 return (ARCHIVE_FAILED);
1967 for (i = 2; i >= 0; i--)
1969 if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1970 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1972 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1974 return (ARCHIVE_FATAL);
1976 lzss_offset |= code << (i * 8);
1978 if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1979 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1981 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1983 return (ARCHIVE_FATAL);
1985 lzss_emit_match(rar, lzss_offset + 2, length + 32);
1986 rar->bytes_uncopied += length + 32;
1990 if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1991 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1993 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1995 return (ARCHIVE_FATAL);
1997 lzss_emit_match(rar, 1, length + 4);
1998 rar->bytes_uncopied += length + 4;
2002 lzss_emit_literal(rar, sym);
2003 rar->bytes_uncopied++;
2009 start = rar->offset;
2010 end = start + rar->dictionary_size;
2011 rar->filterstart = INT64_MAX;
2013 if ((actualend = expand(a, end)) < 0)
2014 return ((int)actualend);
2016 rar->bytes_uncopied = actualend - start;
2017 if (rar->bytes_uncopied == 0) {
2018 /* Broken RAR files cause this case.
2019 * NOTE: If this case were possible on a normal RAR file
2020 * we would find out where it was actually bad and
2021 * what we would do to solve it. */
2022 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2023 "Internal error extracting RAR file");
2024 return (ARCHIVE_FATAL);
2027 if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
2028 bs = rar->unp_buffer_size - rar->unp_offset;
2030 bs = (size_t)rar->bytes_uncopied;
2031 ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
2032 if (ret != ARCHIVE_OK)
2035 rar->bytes_uncopied -= bs;
2037 * If *buff is NULL, it means unp_buffer is not full.
2038 * So we have to continue extracting a RAR file.
2040 } while (*buff == NULL);
2042 rar->unp_offset = 0;
2043 *size = rar->unp_buffer_size;
2044 *offset = rar->offset_outgoing;
2045 rar->offset_outgoing += *size;
2046 /* Calculate File CRC. */
2047 rar->crc_calculated = crc32(rar->crc_calculated, *buff, (unsigned)*size);
2052 parse_codes(struct archive_read *a)
2054 int i, j, val, n, r;
2055 unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
2056 unsigned int maxorder;
2057 struct huffman_code precode;
2058 struct rar *rar = (struct rar *)(a->format->data);
2059 struct rar_br *br = &(rar->br);
2063 /* Skip to the next byte */
2064 rar_br_consume_unalined_bits(br);
2066 /* PPMd block flag */
2067 if (!rar_br_read_ahead(a, br, 1))
2068 goto truncated_data;
2069 if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
2071 rar_br_consume(br, 1);
2072 if (!rar_br_read_ahead(a, br, 7))
2073 goto truncated_data;
2074 ppmd_flags = rar_br_bits(br, 7);
2075 rar_br_consume(br, 7);
2077 /* Memory is allocated in MB */
2078 if (ppmd_flags & 0x20)
2080 if (!rar_br_read_ahead(a, br, 8))
2081 goto truncated_data;
2082 rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
2083 rar_br_consume(br, 8);
2086 if (ppmd_flags & 0x40)
2088 if (!rar_br_read_ahead(a, br, 8))
2089 goto truncated_data;
2090 rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
2091 rar_br_consume(br, 8);
2094 rar->ppmd_escape = 2;
2096 if (ppmd_flags & 0x20)
2098 maxorder = (ppmd_flags & 0x1F) + 1;
2100 maxorder = 16 + (maxorder - 16) * 3;
2104 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2105 "Truncated RAR file data");
2106 return (ARCHIVE_FATAL);
2109 /* Make sure ppmd7_contest is freed before Ppmd7_Construct
2110 * because reading a broken file cause this abnormal sequence. */
2111 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
2114 rar->bytein.Read = &ppmd_read;
2115 __archive_ppmd7_functions.PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
2116 rar->range_dec.Stream = &rar->bytein;
2117 __archive_ppmd7_functions.Ppmd7_Construct(&rar->ppmd7_context);
2119 if (rar->dictionary_size == 0) {
2120 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2121 "Invalid zero dictionary size");
2122 return (ARCHIVE_FATAL);
2125 if (!__archive_ppmd7_functions.Ppmd7_Alloc(&rar->ppmd7_context,
2126 rar->dictionary_size))
2128 archive_set_error(&a->archive, ENOMEM,
2130 return (ARCHIVE_FATAL);
2132 if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2134 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2135 "Unable to initialize PPMd range decoder");
2136 return (ARCHIVE_FATAL);
2138 __archive_ppmd7_functions.Ppmd7_Init(&rar->ppmd7_context, maxorder);
2139 rar->ppmd_valid = 1;
2143 if (!rar->ppmd_valid) {
2144 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2145 "Invalid PPMd sequence");
2146 return (ARCHIVE_FATAL);
2148 if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2150 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2151 "Unable to initialize PPMd range decoder");
2152 return (ARCHIVE_FATAL);
2158 rar_br_consume(br, 1);
2160 /* Keep existing table flag */
2161 if (!rar_br_read_ahead(a, br, 1))
2162 goto truncated_data;
2163 if (!rar_br_bits(br, 1))
2164 memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
2165 rar_br_consume(br, 1);
2167 memset(&bitlengths, 0, sizeof(bitlengths));
2168 for (i = 0; i < MAX_SYMBOLS;)
2170 if (!rar_br_read_ahead(a, br, 4))
2171 goto truncated_data;
2172 bitlengths[i++] = rar_br_bits(br, 4);
2173 rar_br_consume(br, 4);
2174 if (bitlengths[i-1] == 0xF)
2176 if (!rar_br_read_ahead(a, br, 4))
2177 goto truncated_data;
2178 zerocount = rar_br_bits(br, 4);
2179 rar_br_consume(br, 4);
2183 for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
2184 bitlengths[i++] = 0;
2189 memset(&precode, 0, sizeof(precode));
2190 r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
2191 if (r != ARCHIVE_OK) {
2193 free(precode.table);
2197 for (i = 0; i < HUFFMAN_TABLE_SIZE;)
2199 if ((val = read_next_symbol(a, &precode)) < 0) {
2201 free(precode.table);
2202 return (ARCHIVE_FATAL);
2206 rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
2214 free(precode.table);
2215 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2216 "Internal error extracting RAR file.");
2217 return (ARCHIVE_FATAL);
2221 if (!rar_br_read_ahead(a, br, 3)) {
2223 free(precode.table);
2224 goto truncated_data;
2226 n = rar_br_bits(br, 3) + 3;
2227 rar_br_consume(br, 3);
2229 if (!rar_br_read_ahead(a, br, 7)) {
2231 free(precode.table);
2232 goto truncated_data;
2234 n = rar_br_bits(br, 7) + 11;
2235 rar_br_consume(br, 7);
2238 for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2240 rar->lengthtable[i] = rar->lengthtable[i-1];
2247 if (!rar_br_read_ahead(a, br, 3)) {
2249 free(precode.table);
2250 goto truncated_data;
2252 n = rar_br_bits(br, 3) + 3;
2253 rar_br_consume(br, 3);
2255 if (!rar_br_read_ahead(a, br, 7)) {
2257 free(precode.table);
2258 goto truncated_data;
2260 n = rar_br_bits(br, 7) + 11;
2261 rar_br_consume(br, 7);
2264 for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2265 rar->lengthtable[i++] = 0;
2269 free(precode.table);
2271 r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
2273 if (r != ARCHIVE_OK)
2275 r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
2276 OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2277 if (r != ARCHIVE_OK)
2279 r = create_code(a, &rar->lowoffsetcode,
2280 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
2281 LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2282 if (r != ARCHIVE_OK)
2284 r = create_code(a, &rar->lengthcode,
2285 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
2286 LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
2287 if (r != ARCHIVE_OK)
2291 if (!rar->dictionary_size || !rar->lzss.window)
2293 /* Seems as though dictionary sizes are not used. Even so, minimize
2294 * memory usage as much as possible.
2297 unsigned int new_size;
2299 if (rar->unp_size >= DICTIONARY_MAX_SIZE)
2300 new_size = DICTIONARY_MAX_SIZE;
2302 new_size = rar_fls((unsigned int)rar->unp_size) << 1;
2303 new_window = realloc(rar->lzss.window, new_size);
2304 if (new_window == NULL) {
2305 archive_set_error(&a->archive, ENOMEM,
2306 "Unable to allocate memory for uncompressed data.");
2307 return (ARCHIVE_FATAL);
2309 rar->lzss.window = (unsigned char *)new_window;
2310 rar->dictionary_size = new_size;
2311 memset(rar->lzss.window, 0, rar->dictionary_size);
2312 rar->lzss.mask = rar->dictionary_size - 1;
2315 rar->start_new_table = 0;
2316 return (ARCHIVE_OK);
2318 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2319 "Truncated RAR file data");
2321 return (ARCHIVE_FATAL);
2325 free_codes(struct archive_read *a)
2327 struct rar *rar = (struct rar *)(a->format->data);
2328 free(rar->maincode.tree);
2329 free(rar->offsetcode.tree);
2330 free(rar->lowoffsetcode.tree);
2331 free(rar->lengthcode.tree);
2332 free(rar->maincode.table);
2333 free(rar->offsetcode.table);
2334 free(rar->lowoffsetcode.table);
2335 free(rar->lengthcode.table);
2336 memset(&rar->maincode, 0, sizeof(rar->maincode));
2337 memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
2338 memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
2339 memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
2344 read_next_symbol(struct archive_read *a, struct huffman_code *code)
2348 int length, value, node;
2354 if (make_table(a, code) != (ARCHIVE_OK))
2358 rar = (struct rar *)(a->format->data);
2361 /* Look ahead (peek) at bits */
2362 if (!rar_br_read_ahead(a, br, code->tablesize)) {
2363 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2364 "Truncated RAR file data");
2368 bits = rar_br_bits(br, code->tablesize);
2370 length = code->table[bits].length;
2371 value = code->table[bits].value;
2375 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2376 "Invalid prefix code in bitstream");
2380 if (length <= code->tablesize)
2382 /* Skip length bits */
2383 rar_br_consume(br, length);
2387 /* Skip tablesize bits */
2388 rar_br_consume(br, code->tablesize);
2391 while (!(code->tree[node].branches[0] ==
2392 code->tree[node].branches[1]))
2394 if (!rar_br_read_ahead(a, br, 1)) {
2395 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2396 "Truncated RAR file data");
2400 bit = rar_br_bits(br, 1);
2401 rar_br_consume(br, 1);
2403 if (code->tree[node].branches[bit] < 0)
2405 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2406 "Invalid prefix code in bitstream");
2409 node = code->tree[node].branches[bit];
2412 return code->tree[node].branches[0];
2416 create_code(struct archive_read *a, struct huffman_code *code,
2417 unsigned char *lengths, int numsymbols, char maxlength)
2419 int i, j, codebits = 0, symbolsleft = numsymbols;
2421 code->numentries = 0;
2422 code->numallocatedentries = 0;
2423 if (new_node(code) < 0) {
2424 archive_set_error(&a->archive, ENOMEM,
2425 "Unable to allocate memory for node data.");
2426 return (ARCHIVE_FATAL);
2428 code->numentries = 1;
2429 code->minlength = INT_MAX;
2430 code->maxlength = INT_MIN;
2432 for(i = 1; i <= maxlength; i++)
2434 for(j = 0; j < numsymbols; j++)
2436 if (lengths[j] != i) continue;
2437 if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
2438 return (ARCHIVE_FATAL);
2440 if (--symbolsleft <= 0) { break; break; }
2444 return (ARCHIVE_OK);
2448 add_value(struct archive_read *a, struct huffman_code *code, int value,
2449 int codebits, int length)
2451 int repeatpos, lastnode, bitpos, bit, repeatnode, nextnode;
2456 if(length > code->maxlength)
2457 code->maxlength = length;
2458 if(length < code->minlength)
2459 code->minlength = length;
2462 if (repeatpos == 0 || (repeatpos >= 0
2463 && (((codebits >> (repeatpos - 1)) & 3) == 0
2464 || ((codebits >> (repeatpos - 1)) & 3) == 3)))
2466 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2467 "Invalid repeat position");
2468 return (ARCHIVE_FATAL);
2472 for (bitpos = length - 1; bitpos >= 0; bitpos--)
2474 bit = (codebits >> bitpos) & 1;
2476 /* Leaf node check */
2477 if (code->tree[lastnode].branches[0] ==
2478 code->tree[lastnode].branches[1])
2480 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2482 return (ARCHIVE_FATAL);
2485 if (bitpos == repeatpos)
2487 /* Open branch check */
2488 if (!(code->tree[lastnode].branches[bit] < 0))
2490 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2491 "Invalid repeating code");
2492 return (ARCHIVE_FATAL);
2495 if ((repeatnode = new_node(code)) < 0) {
2496 archive_set_error(&a->archive, ENOMEM,
2497 "Unable to allocate memory for node data.");
2498 return (ARCHIVE_FATAL);
2500 if ((nextnode = new_node(code)) < 0) {
2501 archive_set_error(&a->archive, ENOMEM,
2502 "Unable to allocate memory for node data.");
2503 return (ARCHIVE_FATAL);
2507 code->tree[lastnode].branches[bit] = repeatnode;
2508 code->tree[repeatnode].branches[bit] = repeatnode;
2509 code->tree[repeatnode].branches[bit^1] = nextnode;
2510 lastnode = nextnode;
2512 bitpos++; /* terminating bit already handled, skip it */
2516 /* Open branch check */
2517 if (code->tree[lastnode].branches[bit] < 0)
2519 if (new_node(code) < 0) {
2520 archive_set_error(&a->archive, ENOMEM,
2521 "Unable to allocate memory for node data.");
2522 return (ARCHIVE_FATAL);
2524 code->tree[lastnode].branches[bit] = code->numentries++;
2528 lastnode = code->tree[lastnode].branches[bit];
2532 if (!(code->tree[lastnode].branches[0] == -1
2533 && code->tree[lastnode].branches[1] == -2))
2535 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2537 return (ARCHIVE_FATAL);
2540 /* Set leaf value */
2541 code->tree[lastnode].branches[0] = value;
2542 code->tree[lastnode].branches[1] = value;
2544 return (ARCHIVE_OK);
2548 new_node(struct huffman_code *code)
2551 if (code->numallocatedentries == code->numentries) {
2552 int new_num_entries = 256;
2553 if (code->numentries > 0) {
2554 new_num_entries = code->numentries * 2;
2556 new_tree = realloc(code->tree, new_num_entries * sizeof(*code->tree));
2557 if (new_tree == NULL)
2559 code->tree = (struct huffman_tree_node *)new_tree;
2560 code->numallocatedentries = new_num_entries;
2562 code->tree[code->numentries].branches[0] = -1;
2563 code->tree[code->numentries].branches[1] = -2;
2568 make_table(struct archive_read *a, struct huffman_code *code)
2570 if (code->maxlength < code->minlength || code->maxlength > 10)
2571 code->tablesize = 10;
2573 code->tablesize = code->maxlength;
2576 (struct huffman_table_entry *)calloc(1, sizeof(*code->table)
2577 * ((size_t)1 << code->tablesize));
2579 return make_table_recurse(a, code, 0, code->table, 0, code->tablesize);
2583 make_table_recurse(struct archive_read *a, struct huffman_code *code, int node,
2584 struct huffman_table_entry *table, int depth,
2587 int currtablesize, i, ret = (ARCHIVE_OK);
2591 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2592 "Huffman tree was not created.");
2593 return (ARCHIVE_FATAL);
2595 if (node < 0 || node >= code->numentries)
2597 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2598 "Invalid location to Huffman tree specified.");
2599 return (ARCHIVE_FATAL);
2602 currtablesize = 1 << (maxdepth - depth);
2604 if (code->tree[node].branches[0] ==
2605 code->tree[node].branches[1])
2607 for(i = 0; i < currtablesize; i++)
2609 table[i].length = depth;
2610 table[i].value = code->tree[node].branches[0];
2615 for(i = 0; i < currtablesize; i++)
2616 table[i].length = -1;
2620 if(depth == maxdepth)
2622 table[0].length = maxdepth + 1;
2623 table[0].value = node;
2627 ret |= make_table_recurse(a, code, code->tree[node].branches[0], table,
2628 depth + 1, maxdepth);
2629 ret |= make_table_recurse(a, code, code->tree[node].branches[1],
2630 table + currtablesize / 2, depth + 1, maxdepth);
2637 expand(struct archive_read *a, int64_t end)
2639 static const unsigned char lengthbases[] =
2640 { 0, 1, 2, 3, 4, 5, 6,
2641 7, 8, 10, 12, 14, 16, 20,
2642 24, 28, 32, 40, 48, 56, 64,
2643 80, 96, 112, 128, 160, 192, 224 };
2644 static const unsigned char lengthbits[] =
2645 { 0, 0, 0, 0, 0, 0, 0,
2646 0, 1, 1, 1, 1, 2, 2,
2647 2, 2, 3, 3, 3, 3, 4,
2648 4, 4, 4, 5, 5, 5, 5 };
2649 static const unsigned int offsetbases[] =
2651 8, 12, 16, 24, 32, 48,
2652 64, 96, 128, 192, 256, 384,
2653 512, 768, 1024, 1536, 2048, 3072,
2654 4096, 6144, 8192, 12288, 16384, 24576,
2655 32768, 49152, 65536, 98304, 131072, 196608,
2656 262144, 327680, 393216, 458752, 524288, 589824,
2657 655360, 720896, 786432, 851968, 917504, 983040,
2658 1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
2659 2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
2660 static const unsigned char offsetbits[] =
2661 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
2662 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
2663 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
2664 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2665 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
2666 static const unsigned char shortbases[] =
2667 { 0, 4, 8, 16, 32, 64, 128, 192 };
2668 static const unsigned char shortbits[] =
2669 { 2, 2, 3, 4, 5, 6, 6, 6 };
2671 int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
2672 unsigned char newfile;
2673 struct rar *rar = (struct rar *)(a->format->data);
2674 struct rar_br *br = &(rar->br);
2676 if (rar->filterstart < end)
2677 end = rar->filterstart;
2681 if (rar->output_last_match &&
2682 lzss_position(&rar->lzss) + rar->lastlength <= end)
2684 lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
2685 rar->output_last_match = 0;
2688 if(rar->is_ppmd_block || rar->output_last_match ||
2689 lzss_position(&rar->lzss) >= end)
2690 return lzss_position(&rar->lzss);
2692 if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
2693 return (ARCHIVE_FATAL);
2694 rar->output_last_match = 0;
2698 lzss_emit_literal(rar, symbol);
2701 else if (symbol == 256)
2703 if (!rar_br_read_ahead(a, br, 1))
2704 goto truncated_data;
2705 newfile = !rar_br_bits(br, 1);
2706 rar_br_consume(br, 1);
2710 rar->start_new_block = 1;
2711 if (!rar_br_read_ahead(a, br, 1))
2712 goto truncated_data;
2713 rar->start_new_table = rar_br_bits(br, 1);
2714 rar_br_consume(br, 1);
2715 return lzss_position(&rar->lzss);
2719 if (parse_codes(a) != ARCHIVE_OK)
2720 return (ARCHIVE_FATAL);
2724 else if(symbol==257)
2726 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2727 "Parsing filters is unsupported.");
2728 return (ARCHIVE_FAILED);
2730 else if(symbol==258)
2732 if(rar->lastlength == 0)
2735 offs = rar->lastoffset;
2736 len = rar->lastlength;
2738 else if (symbol <= 262)
2740 offsindex = symbol - 259;
2741 offs = rar->oldoffset[offsindex];
2743 if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
2745 if (lensymbol > (int)(sizeof(lengthbases)/sizeof(lengthbases[0])))
2747 if (lensymbol > (int)(sizeof(lengthbits)/sizeof(lengthbits[0])))
2749 len = lengthbases[lensymbol] + 2;
2750 if (lengthbits[lensymbol] > 0) {
2751 if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
2752 goto truncated_data;
2753 len += rar_br_bits(br, lengthbits[lensymbol]);
2754 rar_br_consume(br, lengthbits[lensymbol]);
2757 for (i = offsindex; i > 0; i--)
2758 rar->oldoffset[i] = rar->oldoffset[i-1];
2759 rar->oldoffset[0] = offs;
2761 else if(symbol<=270)
2763 offs = shortbases[symbol-263] + 1;
2764 if(shortbits[symbol-263] > 0) {
2765 if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
2766 goto truncated_data;
2767 offs += rar_br_bits(br, shortbits[symbol-263]);
2768 rar_br_consume(br, shortbits[symbol-263]);
2773 for(i = 3; i > 0; i--)
2774 rar->oldoffset[i] = rar->oldoffset[i-1];
2775 rar->oldoffset[0] = offs;
2779 if (symbol-271 > (int)(sizeof(lengthbases)/sizeof(lengthbases[0])))
2781 if (symbol-271 > (int)(sizeof(lengthbits)/sizeof(lengthbits[0])))
2783 len = lengthbases[symbol-271]+3;
2784 if(lengthbits[symbol-271] > 0) {
2785 if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
2786 goto truncated_data;
2787 len += rar_br_bits(br, lengthbits[symbol-271]);
2788 rar_br_consume(br, lengthbits[symbol-271]);
2791 if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
2793 if (offssymbol > (int)(sizeof(offsetbases)/sizeof(offsetbases[0])))
2795 if (offssymbol > (int)(sizeof(offsetbits)/sizeof(offsetbits[0])))
2797 offs = offsetbases[offssymbol]+1;
2798 if(offsetbits[offssymbol] > 0)
2802 if(offsetbits[offssymbol] > 4) {
2803 if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
2804 goto truncated_data;
2805 offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
2806 rar_br_consume(br, offsetbits[offssymbol] - 4);
2809 if(rar->numlowoffsetrepeats > 0)
2811 rar->numlowoffsetrepeats--;
2812 offs += rar->lastlowoffset;
2816 if ((lowoffsetsymbol =
2817 read_next_symbol(a, &rar->lowoffsetcode)) < 0)
2818 return (ARCHIVE_FATAL);
2819 if(lowoffsetsymbol == 16)
2821 rar->numlowoffsetrepeats = 15;
2822 offs += rar->lastlowoffset;
2826 offs += lowoffsetsymbol;
2827 rar->lastlowoffset = lowoffsetsymbol;
2832 if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
2833 goto truncated_data;
2834 offs += rar_br_bits(br, offsetbits[offssymbol]);
2835 rar_br_consume(br, offsetbits[offssymbol]);
2839 if (offs >= 0x40000)
2844 for(i = 3; i > 0; i--)
2845 rar->oldoffset[i] = rar->oldoffset[i-1];
2846 rar->oldoffset[0] = offs;
2849 rar->lastoffset = offs;
2850 rar->lastlength = len;
2851 rar->output_last_match = 1;
2854 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2855 "Truncated RAR file data");
2857 return (ARCHIVE_FATAL);
2859 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2860 "Bad RAR file data");
2861 return (ARCHIVE_FATAL);
2865 copy_from_lzss_window(struct archive_read *a, const void **buffer,
2866 int64_t startpos, int length)
2868 int windowoffs, firstpart;
2869 struct rar *rar = (struct rar *)(a->format->data);
2871 if (!rar->unp_buffer)
2873 if ((rar->unp_buffer = malloc(rar->unp_buffer_size)) == NULL)
2875 archive_set_error(&a->archive, ENOMEM,
2876 "Unable to allocate memory for uncompressed data.");
2877 return (ARCHIVE_FATAL);
2881 windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
2882 if(windowoffs + length <= lzss_size(&rar->lzss)) {
2883 memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
2885 } else if (length <= lzss_size(&rar->lzss)) {
2886 firstpart = lzss_size(&rar->lzss) - windowoffs;
2887 if (firstpart < 0) {
2888 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2889 "Bad RAR file data");
2890 return (ARCHIVE_FATAL);
2892 if (firstpart < length) {
2893 memcpy(&rar->unp_buffer[rar->unp_offset],
2894 &rar->lzss.window[windowoffs], firstpart);
2895 memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
2896 &rar->lzss.window[0], length - firstpart);
2898 memcpy(&rar->unp_buffer[rar->unp_offset],
2899 &rar->lzss.window[windowoffs], length);
2902 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2903 "Bad RAR file data");
2904 return (ARCHIVE_FATAL);
2906 rar->unp_offset += length;
2907 if (rar->unp_offset >= rar->unp_buffer_size)
2908 *buffer = rar->unp_buffer;
2911 return (ARCHIVE_OK);
2915 rar_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
2917 struct rar *rar = (struct rar *)(a->format->data);
2918 const void *h = __archive_read_ahead(a, min, avail);
2922 if (a->archive.read_data_is_posix_read && *avail > (ssize_t)a->archive.read_data_requested)
2923 *avail = a->archive.read_data_requested;
2924 if (*avail > rar->bytes_remaining)
2925 *avail = (ssize_t)rar->bytes_remaining;
2928 else if (*avail == 0 && rar->main_flags & MHD_VOLUME &&
2929 rar->file_flags & FHD_SPLIT_AFTER)
2931 ret = archive_read_format_rar_read_header(a, a->entry);
2932 if (ret == (ARCHIVE_EOF))
2934 rar->has_endarc_header = 1;
2935 ret = archive_read_format_rar_read_header(a, a->entry);
2937 if (ret != (ARCHIVE_OK))
2939 return rar_read_ahead(a, min, avail);