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;
261 char filename_must_match;
264 struct huffman_code maincode;
265 struct huffman_code offsetcode;
266 struct huffman_code lowoffsetcode;
267 struct huffman_code lengthcode;
268 unsigned char lengthtable[HUFFMAN_TABLE_SIZE];
270 char output_last_match;
271 unsigned int lastlength;
272 unsigned int lastoffset;
273 unsigned int oldoffset[4];
274 unsigned int lastlowoffset;
275 unsigned int numlowoffsetrepeats;
277 char start_new_table;
279 /* PPMd Variant H members */
284 CPpmd7 ppmd7_context;
285 CPpmd7z_RangeDec range_dec;
289 * String conversion object.
291 int init_default_conversion;
292 struct archive_string_conv *sconv_default;
293 struct archive_string_conv *opt_sconv;
294 struct archive_string_conv *sconv_utf8;
295 struct archive_string_conv *sconv_utf16be;
301 #define CACHE_TYPE uint64_t
302 #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
304 CACHE_TYPE cache_buffer;
305 /* Indicates how many bits avail in cache_buffer. */
308 const unsigned char *next_in;
312 * Custom field to denote that this archive contains encrypted entries
314 int has_encrypted_entries;
317 static int archive_read_support_format_rar_capabilities(struct archive_read *);
318 static int archive_read_format_rar_has_encrypted_entries(struct archive_read *);
319 static int archive_read_format_rar_bid(struct archive_read *, int);
320 static int archive_read_format_rar_options(struct archive_read *,
321 const char *, const char *);
322 static int archive_read_format_rar_read_header(struct archive_read *,
323 struct archive_entry *);
324 static int archive_read_format_rar_read_data(struct archive_read *,
325 const void **, size_t *, int64_t *);
326 static int archive_read_format_rar_read_data_skip(struct archive_read *a);
327 static int64_t archive_read_format_rar_seek_data(struct archive_read *, int64_t,
329 static int archive_read_format_rar_cleanup(struct archive_read *);
331 /* Support functions */
332 static int read_header(struct archive_read *, struct archive_entry *, char);
333 static time_t get_time(int);
334 static int read_exttime(const char *, struct rar *, const char *);
335 static int read_symlink_stored(struct archive_read *, struct archive_entry *,
336 struct archive_string_conv *);
337 static int read_data_stored(struct archive_read *, const void **, size_t *,
339 static int read_data_compressed(struct archive_read *, const void **, size_t *,
341 static int rar_br_preparation(struct archive_read *, struct rar_br *);
342 static int parse_codes(struct archive_read *);
343 static void free_codes(struct archive_read *);
344 static int read_next_symbol(struct archive_read *, struct huffman_code *);
345 static int create_code(struct archive_read *, struct huffman_code *,
346 unsigned char *, int, char);
347 static int add_value(struct archive_read *, struct huffman_code *, int, int,
349 static int new_node(struct huffman_code *);
350 static int make_table(struct archive_read *, struct huffman_code *);
351 static int make_table_recurse(struct archive_read *, struct huffman_code *, int,
352 struct huffman_table_entry *, int, int);
353 static int64_t expand(struct archive_read *, int64_t);
354 static int copy_from_lzss_window(struct archive_read *, const void **,
356 static const void *rar_read_ahead(struct archive_read *, size_t, ssize_t *);
361 /* Check that the cache buffer has enough bits. */
362 #define rar_br_has(br, n) ((br)->cache_avail >= n)
363 /* Get compressed data by bit. */
364 #define rar_br_bits(br, n) \
365 (((uint32_t)((br)->cache_buffer >> \
366 ((br)->cache_avail - (n)))) & cache_masks[n])
367 #define rar_br_bits_forced(br, n) \
368 (((uint32_t)((br)->cache_buffer << \
369 ((n) - (br)->cache_avail))) & cache_masks[n])
370 /* Read ahead to make sure the cache buffer has enough compressed data we
372 * True : completed, there is enough data in the cache buffer.
373 * False : there is no data in the stream. */
374 #define rar_br_read_ahead(a, br, n) \
375 ((rar_br_has(br, (n)) || rar_br_fillup(a, br)) || rar_br_has(br, (n)))
376 /* Notify how many bits we consumed. */
377 #define rar_br_consume(br, n) ((br)->cache_avail -= (n))
378 #define rar_br_consume_unalined_bits(br) ((br)->cache_avail &= ~7)
380 static const uint32_t cache_masks[] = {
381 0x00000000, 0x00000001, 0x00000003, 0x00000007,
382 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
383 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
384 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
385 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
386 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
387 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
388 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
389 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
393 * Shift away used bits in the cache data and fill it up with following bits.
394 * Call this when cache buffer does not have enough bits you need.
396 * Returns 1 if the cache buffer is full.
397 * Returns 0 if the cache buffer is not full; input buffer is empty.
400 rar_br_fillup(struct archive_read *a, struct rar_br *br)
402 struct rar *rar = (struct rar *)(a->format->data);
403 int n = CACHE_BITS - br->cache_avail;
408 if (br->avail_in >= 8) {
410 ((uint64_t)br->next_in[0]) << 56 |
411 ((uint64_t)br->next_in[1]) << 48 |
412 ((uint64_t)br->next_in[2]) << 40 |
413 ((uint64_t)br->next_in[3]) << 32 |
414 ((uint32_t)br->next_in[4]) << 24 |
415 ((uint32_t)br->next_in[5]) << 16 |
416 ((uint32_t)br->next_in[6]) << 8 |
417 (uint32_t)br->next_in[7];
420 br->cache_avail += 8 * 8;
421 rar->bytes_unconsumed += 8;
422 rar->bytes_remaining -= 8;
427 if (br->avail_in >= 7) {
429 (br->cache_buffer << 56) |
430 ((uint64_t)br->next_in[0]) << 48 |
431 ((uint64_t)br->next_in[1]) << 40 |
432 ((uint64_t)br->next_in[2]) << 32 |
433 ((uint32_t)br->next_in[3]) << 24 |
434 ((uint32_t)br->next_in[4]) << 16 |
435 ((uint32_t)br->next_in[5]) << 8 |
436 (uint32_t)br->next_in[6];
439 br->cache_avail += 7 * 8;
440 rar->bytes_unconsumed += 7;
441 rar->bytes_remaining -= 7;
446 if (br->avail_in >= 6) {
448 (br->cache_buffer << 48) |
449 ((uint64_t)br->next_in[0]) << 40 |
450 ((uint64_t)br->next_in[1]) << 32 |
451 ((uint32_t)br->next_in[2]) << 24 |
452 ((uint32_t)br->next_in[3]) << 16 |
453 ((uint32_t)br->next_in[4]) << 8 |
454 (uint32_t)br->next_in[5];
457 br->cache_avail += 6 * 8;
458 rar->bytes_unconsumed += 6;
459 rar->bytes_remaining -= 6;
464 /* We have enough compressed data in
465 * the cache buffer.*/
470 if (br->avail_in <= 0) {
472 if (rar->bytes_unconsumed > 0) {
473 /* Consume as much as the decompressor
475 __archive_read_consume(a, rar->bytes_unconsumed);
476 rar->bytes_unconsumed = 0;
478 br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
479 if (br->next_in == NULL)
481 if (br->avail_in == 0)
485 (br->cache_buffer << 8) | *br->next_in++;
487 br->cache_avail += 8;
489 rar->bytes_unconsumed++;
490 rar->bytes_remaining--;
495 rar_br_preparation(struct archive_read *a, struct rar_br *br)
497 struct rar *rar = (struct rar *)(a->format->data);
499 if (rar->bytes_remaining > 0) {
500 br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
501 if (br->next_in == NULL) {
502 archive_set_error(&a->archive,
503 ARCHIVE_ERRNO_FILE_FORMAT,
504 "Truncated RAR file data");
505 return (ARCHIVE_FATAL);
507 if (br->cache_avail == 0)
508 (void)rar_br_fillup(a, br);
513 /* Find last bit set */
515 rar_fls(unsigned int word)
521 word |= (word >> 16);
522 return word - (word >> 1);
526 static inline int64_t
527 lzss_position(struct lzss *lzss)
529 return lzss->position;
533 lzss_mask(struct lzss *lzss)
539 lzss_size(struct lzss *lzss)
541 return lzss->mask + 1;
545 lzss_offset_for_position(struct lzss *lzss, int64_t pos)
547 return (int)(pos & lzss->mask);
550 static inline unsigned char *
551 lzss_pointer_for_position(struct lzss *lzss, int64_t pos)
553 return &lzss->window[lzss_offset_for_position(lzss, pos)];
557 lzss_current_offset(struct lzss *lzss)
559 return lzss_offset_for_position(lzss, lzss->position);
562 static inline uint8_t *
563 lzss_current_pointer(struct lzss *lzss)
565 return lzss_pointer_for_position(lzss, lzss->position);
569 lzss_emit_literal(struct rar *rar, uint8_t literal)
571 *lzss_current_pointer(&rar->lzss) = literal;
572 rar->lzss.position++;
576 lzss_emit_match(struct rar *rar, int offset, int length)
578 int dstoffs = lzss_current_offset(&rar->lzss);
579 int srcoffs = (dstoffs - offset) & lzss_mask(&rar->lzss);
580 int l, li, remaining;
581 unsigned char *d, *s;
584 while (remaining > 0) {
586 if (dstoffs > srcoffs) {
587 if (l > lzss_size(&rar->lzss) - dstoffs)
588 l = lzss_size(&rar->lzss) - dstoffs;
590 if (l > lzss_size(&rar->lzss) - srcoffs)
591 l = lzss_size(&rar->lzss) - srcoffs;
593 d = &(rar->lzss.window[dstoffs]);
594 s = &(rar->lzss.window[srcoffs]);
595 if ((dstoffs + l < srcoffs) || (srcoffs + l < dstoffs))
598 for (li = 0; li < l; li++)
602 dstoffs = (dstoffs + l) & lzss_mask(&(rar->lzss));
603 srcoffs = (srcoffs + l) & lzss_mask(&(rar->lzss));
605 rar->lzss.position += length;
611 struct archive_read *a = ((IByteIn*)p)->a;
612 struct rar *rar = (struct rar *)(a->format->data);
613 struct rar_br *br = &(rar->br);
615 if (!rar_br_read_ahead(a, br, 8))
617 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
618 "Truncated RAR file data");
622 b = rar_br_bits(br, 8);
623 rar_br_consume(br, 8);
628 archive_read_support_format_rar(struct archive *_a)
630 struct archive_read *a = (struct archive_read *)_a;
634 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
635 "archive_read_support_format_rar");
637 rar = (struct rar *)calloc(sizeof(*rar), 1);
640 archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
641 return (ARCHIVE_FATAL);
645 * Until enough data has been read, we cannot tell about
646 * any encrypted entries yet.
648 rar->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
650 r = __archive_read_register_format(a,
653 archive_read_format_rar_bid,
654 archive_read_format_rar_options,
655 archive_read_format_rar_read_header,
656 archive_read_format_rar_read_data,
657 archive_read_format_rar_read_data_skip,
658 archive_read_format_rar_seek_data,
659 archive_read_format_rar_cleanup,
660 archive_read_support_format_rar_capabilities,
661 archive_read_format_rar_has_encrypted_entries);
669 archive_read_support_format_rar_capabilities(struct archive_read * a)
671 (void)a; /* UNUSED */
672 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
673 | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
677 archive_read_format_rar_has_encrypted_entries(struct archive_read *_a)
679 if (_a && _a->format) {
680 struct rar * rar = (struct rar *)_a->format->data;
682 return rar->has_encrypted_entries;
685 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
690 archive_read_format_rar_bid(struct archive_read *a, int best_bid)
694 /* If there's already a bid > 30, we'll never win. */
698 if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
701 if (memcmp(p, RAR_SIGNATURE, 7) == 0)
704 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
705 /* This is a PE file */
706 ssize_t offset = 0x10000;
707 ssize_t window = 4096;
709 while (offset + window <= (1024 * 128)) {
710 const char *buff = __archive_read_ahead(a, offset + window, &bytes_avail);
712 /* Remaining bytes are less than window. */
719 while (p + 7 < buff + bytes_avail) {
720 if (memcmp(p, RAR_SIGNATURE, 7) == 0)
731 skip_sfx(struct archive_read *a)
736 ssize_t bytes, window;
740 while (total + window <= (1024 * 128)) {
741 h = __archive_read_ahead(a, window, &bytes);
743 /* Remaining bytes are less than window. */
755 * Scan ahead until we find something that looks
756 * like the RAR header.
759 if (memcmp(p, RAR_SIGNATURE, 7) == 0) {
760 skip = p - (const char *)h;
761 __archive_read_consume(a, skip);
766 skip = p - (const char *)h;
767 __archive_read_consume(a, skip);
771 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
772 "Couldn't find out RAR header");
773 return (ARCHIVE_FATAL);
777 archive_read_format_rar_options(struct archive_read *a,
778 const char *key, const char *val)
781 int ret = ARCHIVE_FAILED;
783 rar = (struct rar *)(a->format->data);
784 if (strcmp(key, "hdrcharset") == 0) {
785 if (val == NULL || val[0] == 0)
786 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
787 "rar: hdrcharset option needs a character-set name");
790 archive_string_conversion_from_charset(
791 &a->archive, val, 0);
792 if (rar->opt_sconv != NULL)
800 /* Note: The "warn" return is just to inform the options
801 * supervisor that we didn't handle it. It will generate
802 * a suitable error if no one used this option. */
803 return (ARCHIVE_WARN);
807 archive_read_format_rar_read_header(struct archive_read *a,
808 struct archive_entry *entry)
817 unsigned long crc32_expected;
819 a->archive.archive_format = ARCHIVE_FORMAT_RAR;
820 if (a->archive.archive_format_name == NULL)
821 a->archive.archive_format_name = "RAR";
823 rar = (struct rar *)(a->format->data);
826 * It should be sufficient to call archive_read_next_header() for
827 * a reader to determine if an entry is encrypted or not. If the
828 * encryption of an entry is only detectable when calling
829 * archive_read_data(), so be it. We'll do the same check there
832 if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
833 rar->has_encrypted_entries = 0;
836 /* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
839 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
840 return (ARCHIVE_EOF);
843 if (rar->found_first_header == 0 &&
844 ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0)) {
845 /* This is an executable ? Must be self-extracting... */
847 if (ret < ARCHIVE_WARN)
850 rar->found_first_header = 1;
854 unsigned long crc32_val;
856 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
857 return (ARCHIVE_FATAL);
864 if (memcmp(p, RAR_SIGNATURE, 7) != 0) {
865 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
866 "Invalid marker header");
867 return (ARCHIVE_FATAL);
869 __archive_read_consume(a, 7);
873 rar->main_flags = archive_le16dec(p + 3);
874 skip = archive_le16dec(p + 5);
875 if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
876 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
877 "Invalid header size");
878 return (ARCHIVE_FATAL);
880 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
881 return (ARCHIVE_FATAL);
883 memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
884 memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
885 sizeof(rar->reserved2));
886 if (rar->main_flags & MHD_ENCRYPTVER) {
887 if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)+1) {
888 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
889 "Invalid header size");
890 return (ARCHIVE_FATAL);
892 rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
893 sizeof(rar->reserved2));
896 /* Main header is password encrypted, so we cannot read any
897 file names or any other info about files from the header. */
898 if (rar->main_flags & MHD_PASSWORD)
900 archive_entry_set_is_metadata_encrypted(entry, 1);
901 archive_entry_set_is_data_encrypted(entry, 1);
902 rar->has_encrypted_entries = 1;
903 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
904 "RAR encryption support unavailable.");
905 return (ARCHIVE_FATAL);
908 crc32_val = crc32(0, (const unsigned char *)p + 2, (unsigned)skip - 2);
909 if ((crc32_val & 0xffff) != archive_le16dec(p)) {
910 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
912 return (ARCHIVE_FATAL);
914 __archive_read_consume(a, skip);
918 return read_header(a, entry, head_type);
926 flags = archive_le16dec(p + 3);
927 skip = archive_le16dec(p + 5);
929 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
930 "Invalid header size too small");
931 return (ARCHIVE_FATAL);
933 if (flags & HD_ADD_SIZE_PRESENT)
936 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
937 "Invalid header size too small");
938 return (ARCHIVE_FATAL);
940 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
941 return (ARCHIVE_FATAL);
943 skip += archive_le32dec(p + 7);
946 /* Skip over the 2-byte CRC at the beginning of the header. */
947 crc32_expected = archive_le16dec(p);
948 __archive_read_consume(a, 2);
951 /* Skim the entire header and compute the CRC. */
954 size_t to_read = skip;
956 if (to_read > 32 * 1024) {
959 if ((h = __archive_read_ahead(a, to_read, &did_read)) == NULL) {
960 return (ARCHIVE_FATAL);
963 crc32_val = crc32(crc32_val, (const unsigned char *)p, (unsigned)did_read);
964 __archive_read_consume(a, did_read);
967 if ((crc32_val & 0xffff) != crc32_expected) {
968 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
970 return (ARCHIVE_FATAL);
972 if (head_type == ENDARC_HEAD)
973 return (ARCHIVE_EOF);
977 if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
982 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
984 return (ARCHIVE_FATAL);
990 archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
991 size_t *size, int64_t *offset)
993 struct rar *rar = (struct rar *)(a->format->data);
996 if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
997 rar->has_encrypted_entries = 0;
1000 if (rar->bytes_unconsumed > 0) {
1001 /* Consume as much as the decompressor actually used. */
1002 __archive_read_consume(a, rar->bytes_unconsumed);
1003 rar->bytes_unconsumed = 0;
1007 if (rar->entry_eof || rar->offset_seek >= rar->unp_size) {
1009 *offset = rar->offset;
1010 if (*offset < rar->unp_size)
1011 *offset = rar->unp_size;
1012 return (ARCHIVE_EOF);
1015 switch (rar->compression_method)
1017 case COMPRESS_METHOD_STORE:
1018 ret = read_data_stored(a, buff, size, offset);
1021 case COMPRESS_METHOD_FASTEST:
1022 case COMPRESS_METHOD_FAST:
1023 case COMPRESS_METHOD_NORMAL:
1024 case COMPRESS_METHOD_GOOD:
1025 case COMPRESS_METHOD_BEST:
1026 ret = read_data_compressed(a, buff, size, offset);
1027 if (ret != ARCHIVE_OK && ret != ARCHIVE_WARN) {
1028 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1029 rar->start_new_table = 1;
1030 rar->ppmd_valid = 0;
1035 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1036 "Unsupported compression method for RAR file.");
1037 ret = ARCHIVE_FATAL;
1044 archive_read_format_rar_read_data_skip(struct archive_read *a)
1047 int64_t bytes_skipped;
1050 rar = (struct rar *)(a->format->data);
1052 if (rar->bytes_unconsumed > 0) {
1053 /* Consume as much as the decompressor actually used. */
1054 __archive_read_consume(a, rar->bytes_unconsumed);
1055 rar->bytes_unconsumed = 0;
1058 if (rar->bytes_remaining > 0) {
1059 bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
1060 if (bytes_skipped < 0)
1061 return (ARCHIVE_FATAL);
1064 /* Compressed data to skip must be read from each header in a multivolume
1067 if (rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER)
1069 ret = archive_read_format_rar_read_header(a, a->entry);
1070 if (ret == (ARCHIVE_EOF))
1071 ret = archive_read_format_rar_read_header(a, a->entry);
1072 if (ret != (ARCHIVE_OK))
1074 return archive_read_format_rar_read_data_skip(a);
1077 return (ARCHIVE_OK);
1081 archive_read_format_rar_seek_data(struct archive_read *a, int64_t offset,
1084 int64_t client_offset, ret;
1086 struct rar *rar = (struct rar *)(a->format->data);
1088 if (rar->compression_method == COMPRESS_METHOD_STORE)
1090 /* Modify the offset for use with SEEK_SET */
1094 client_offset = rar->offset_seek;
1097 client_offset = rar->unp_size;
1103 client_offset += offset;
1104 if (client_offset < 0)
1106 /* Can't seek past beginning of data block */
1109 else if (client_offset > rar->unp_size)
1112 * Set the returned offset but only seek to the end of
1115 rar->offset_seek = client_offset;
1116 client_offset = rar->unp_size;
1119 client_offset += rar->dbo[0].start_offset;
1121 while (i < rar->cursor)
1124 client_offset += rar->dbo[i].start_offset - rar->dbo[i-1].end_offset;
1126 if (rar->main_flags & MHD_VOLUME)
1128 /* Find the appropriate offset among the multivolume archive */
1131 if (client_offset < rar->dbo[rar->cursor].start_offset &&
1132 rar->file_flags & FHD_SPLIT_BEFORE)
1134 /* Search backwards for the correct data block */
1135 if (rar->cursor == 0)
1137 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1138 "Attempt to seek past beginning of RAR data block");
1139 return (ARCHIVE_FAILED);
1142 client_offset -= rar->dbo[rar->cursor+1].start_offset -
1143 rar->dbo[rar->cursor].end_offset;
1144 if (client_offset < rar->dbo[rar->cursor].start_offset)
1146 ret = __archive_read_seek(a, rar->dbo[rar->cursor].start_offset -
1147 rar->dbo[rar->cursor].header_size, SEEK_SET);
1148 if (ret < (ARCHIVE_OK))
1150 ret = archive_read_format_rar_read_header(a, a->entry);
1151 if (ret != (ARCHIVE_OK))
1153 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1154 "Error during seek of RAR file");
1155 return (ARCHIVE_FAILED);
1160 else if (client_offset > rar->dbo[rar->cursor].end_offset &&
1161 rar->file_flags & FHD_SPLIT_AFTER)
1163 /* Search forward for the correct data block */
1165 if (rar->cursor < rar->nodes &&
1166 client_offset > rar->dbo[rar->cursor].end_offset)
1168 client_offset += rar->dbo[rar->cursor].start_offset -
1169 rar->dbo[rar->cursor-1].end_offset;
1173 ret = __archive_read_seek(a, rar->dbo[rar->cursor].end_offset,
1175 if (ret < (ARCHIVE_OK))
1177 ret = archive_read_format_rar_read_header(a, a->entry);
1178 if (ret == (ARCHIVE_EOF))
1180 rar->has_endarc_header = 1;
1181 ret = archive_read_format_rar_read_header(a, a->entry);
1183 if (ret != (ARCHIVE_OK))
1185 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1186 "Error during seek of RAR file");
1187 return (ARCHIVE_FAILED);
1189 client_offset += rar->dbo[rar->cursor].start_offset -
1190 rar->dbo[rar->cursor-1].end_offset;
1197 ret = __archive_read_seek(a, client_offset, SEEK_SET);
1198 if (ret < (ARCHIVE_OK))
1200 rar->bytes_remaining = rar->dbo[rar->cursor].end_offset - ret;
1205 ret -= rar->dbo[i+1].start_offset - rar->dbo[i].end_offset;
1207 ret -= rar->dbo[0].start_offset;
1209 /* Always restart reading the file after a seek */
1210 __archive_reset_read_data(&a->archive);
1212 rar->bytes_unconsumed = 0;
1216 * If a seek past the end of file was requested, return the requested
1219 if (ret == rar->unp_size && rar->offset_seek > rar->unp_size)
1220 return rar->offset_seek;
1222 /* Return the new offset */
1223 rar->offset_seek = ret;
1224 return rar->offset_seek;
1228 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1229 "Seeking of compressed RAR files is unsupported");
1231 return (ARCHIVE_FAILED);
1235 archive_read_format_rar_cleanup(struct archive_read *a)
1239 rar = (struct rar *)(a->format->data);
1241 free(rar->filename);
1242 free(rar->filename_save);
1244 free(rar->unp_buffer);
1245 free(rar->lzss.window);
1246 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1248 (a->format->data) = NULL;
1249 return (ARCHIVE_OK);
1253 read_header(struct archive_read *a, struct archive_entry *entry,
1257 const char *p, *endp;
1259 struct rar_header rar_header;
1260 struct rar_file_header file_header;
1261 int64_t header_size;
1262 unsigned filename_size, end;
1265 char packed_size[8];
1268 struct archive_string_conv *sconv, *fn_sconv;
1269 unsigned long crc32_val;
1270 int ret = (ARCHIVE_OK), ret2;
1272 rar = (struct rar *)(a->format->data);
1274 /* Setup a string conversion object for non-rar-unicode filenames. */
1275 sconv = rar->opt_sconv;
1276 if (sconv == NULL) {
1277 if (!rar->init_default_conversion) {
1278 rar->sconv_default =
1279 archive_string_default_conversion_for_read(
1281 rar->init_default_conversion = 1;
1283 sconv = rar->sconv_default;
1287 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
1288 return (ARCHIVE_FATAL);
1290 memcpy(&rar_header, p, sizeof(rar_header));
1291 rar->file_flags = archive_le16dec(rar_header.flags);
1292 header_size = archive_le16dec(rar_header.size);
1293 if (header_size < (int64_t)sizeof(file_header) + 7) {
1294 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1295 "Invalid header size");
1296 return (ARCHIVE_FATAL);
1298 crc32_val = crc32(0, (const unsigned char *)p + 2, 7 - 2);
1299 __archive_read_consume(a, 7);
1301 if (!(rar->file_flags & FHD_SOLID))
1303 rar->compression_method = 0;
1304 rar->packed_size = 0;
1311 memset(&rar->salt, 0, sizeof(rar->salt));
1323 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1324 "RAR solid archive support unavailable.");
1325 return (ARCHIVE_FATAL);
1328 if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1329 return (ARCHIVE_FATAL);
1331 /* File Header CRC check. */
1332 crc32_val = crc32(crc32_val, h, (unsigned)(header_size - 7));
1333 if ((crc32_val & 0xffff) != archive_le16dec(rar_header.crc)) {
1334 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1335 "Header CRC error");
1336 return (ARCHIVE_FATAL);
1338 /* If no CRC error, Go on parsing File Header. */
1340 endp = p + header_size - 7;
1341 memcpy(&file_header, p, sizeof(file_header));
1342 p += sizeof(file_header);
1344 rar->compression_method = file_header.method;
1346 ttime = archive_le32dec(file_header.file_time);
1347 rar->mtime = get_time(ttime);
1349 rar->file_crc = archive_le32dec(file_header.file_crc);
1351 if (rar->file_flags & FHD_PASSWORD)
1353 archive_entry_set_is_data_encrypted(entry, 1);
1354 rar->has_encrypted_entries = 1;
1355 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1356 "RAR encryption support unavailable.");
1357 /* Since it is only the data part itself that is encrypted we can at least
1358 extract information about the currently processed entry and don't need
1359 to return ARCHIVE_FATAL here. */
1360 /*return (ARCHIVE_FATAL);*/
1363 if (rar->file_flags & FHD_LARGE)
1365 memcpy(packed_size, file_header.pack_size, 4);
1366 memcpy(packed_size + 4, p, 4); /* High pack size */
1368 memcpy(unp_size, file_header.unp_size, 4);
1369 memcpy(unp_size + 4, p, 4); /* High unpack size */
1371 rar->packed_size = archive_le64dec(&packed_size);
1372 rar->unp_size = archive_le64dec(&unp_size);
1376 rar->packed_size = archive_le32dec(file_header.pack_size);
1377 rar->unp_size = archive_le32dec(file_header.unp_size);
1380 if (rar->packed_size < 0 || rar->unp_size < 0)
1382 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1383 "Invalid sizes specified.");
1384 return (ARCHIVE_FATAL);
1387 rar->bytes_remaining = rar->packed_size;
1389 /* TODO: RARv3 subblocks contain comments. For now the complete block is
1390 * consumed at the end.
1392 if (head_type == NEWSUB_HEAD) {
1393 size_t distance = p - (const char *)h;
1394 header_size += rar->packed_size;
1395 /* Make sure we have the extended data. */
1396 if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1397 return (ARCHIVE_FATAL);
1399 endp = p + header_size - 7;
1403 filename_size = archive_le16dec(file_header.name_size);
1404 if (p + filename_size > endp) {
1405 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1406 "Invalid filename size");
1407 return (ARCHIVE_FATAL);
1409 if (rar->filename_allocated < filename_size * 2 + 2) {
1411 size_t newsize = filename_size * 2 + 2;
1412 newptr = realloc(rar->filename, newsize);
1413 if (newptr == NULL) {
1414 archive_set_error(&a->archive, ENOMEM,
1415 "Couldn't allocate memory.");
1416 return (ARCHIVE_FATAL);
1418 rar->filename = newptr;
1419 rar->filename_allocated = newsize;
1421 filename = rar->filename;
1422 memcpy(filename, p, filename_size);
1423 filename[filename_size] = '\0';
1424 if (rar->file_flags & FHD_UNICODE)
1426 if (filename_size != strlen(filename))
1428 unsigned char highbyte, flagbits, flagbyte;
1429 unsigned fn_end, offset;
1431 end = filename_size;
1432 fn_end = filename_size * 2;
1434 offset = (unsigned)strlen(filename) + 1;
1435 highbyte = *(p + offset++);
1438 while (offset < end && filename_size < fn_end)
1442 flagbyte = *(p + offset++);
1447 switch((flagbyte >> flagbits) & 3)
1450 filename[filename_size++] = '\0';
1451 filename[filename_size++] = *(p + offset++);
1454 filename[filename_size++] = highbyte;
1455 filename[filename_size++] = *(p + offset++);
1458 filename[filename_size++] = *(p + offset + 1);
1459 filename[filename_size++] = *(p + offset);
1465 uint8_t length = *(p + offset++);
1467 if (length & 0x80) {
1468 extra = *(p + offset++);
1469 high = (char)highbyte;
1472 length = (length & 0x7f) + 2;
1473 while (length && filename_size < fn_end) {
1474 unsigned cp = filename_size >> 1;
1475 filename[filename_size++] = high;
1476 filename[filename_size++] = p[cp] + extra;
1483 if (filename_size > fn_end) {
1484 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1485 "Invalid filename");
1486 return (ARCHIVE_FATAL);
1488 filename[filename_size++] = '\0';
1490 * Do not increment filename_size here as the computations below
1491 * add the space for the terminating NUL explicitly.
1493 filename[filename_size] = '\0';
1495 /* Decoded unicode form is UTF-16BE, so we have to update a string
1496 * conversion object for it. */
1497 if (rar->sconv_utf16be == NULL) {
1498 rar->sconv_utf16be = archive_string_conversion_from_charset(
1499 &a->archive, "UTF-16BE", 1);
1500 if (rar->sconv_utf16be == NULL)
1501 return (ARCHIVE_FATAL);
1503 fn_sconv = rar->sconv_utf16be;
1506 while (memcmp(strp, "\x00\x00", 2))
1508 if (!memcmp(strp, "\x00\\", 2))
1515 * If FHD_UNICODE is set but no unicode data, this file name form
1516 * is UTF-8, so we have to update a string conversion object for
1519 if (rar->sconv_utf8 == NULL) {
1520 rar->sconv_utf8 = archive_string_conversion_from_charset(
1521 &a->archive, "UTF-8", 1);
1522 if (rar->sconv_utf8 == NULL)
1523 return (ARCHIVE_FATAL);
1525 fn_sconv = rar->sconv_utf8;
1526 while ((strp = strchr(filename, '\\')) != NULL)
1534 while ((strp = strchr(filename, '\\')) != NULL)
1539 /* Split file in multivolume RAR. No more need to process header. */
1540 if (rar->filename_save &&
1541 filename_size == rar->filename_save_size &&
1542 !memcmp(rar->filename, rar->filename_save, filename_size + 1))
1544 __archive_read_consume(a, header_size - 7);
1546 if (rar->cursor >= rar->nodes)
1550 realloc(rar->dbo, sizeof(*rar->dbo) * rar->nodes)) == NULL)
1552 archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1553 return (ARCHIVE_FATAL);
1555 rar->dbo[rar->cursor].header_size = header_size;
1556 rar->dbo[rar->cursor].start_offset = -1;
1557 rar->dbo[rar->cursor].end_offset = -1;
1559 if (rar->dbo[rar->cursor].start_offset < 0)
1561 rar->dbo[rar->cursor].start_offset = a->filter->position;
1562 rar->dbo[rar->cursor].end_offset = rar->dbo[rar->cursor].start_offset +
1567 else if (rar->filename_must_match)
1569 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1570 "Mismatch of file parts split across multi-volume archive");
1571 return (ARCHIVE_FATAL);
1574 rar->filename_save = (char*)realloc(rar->filename_save,
1576 memcpy(rar->filename_save, rar->filename, filename_size + 1);
1577 rar->filename_save_size = filename_size;
1579 /* Set info for seeking */
1581 if ((rar->dbo = calloc(1, sizeof(*rar->dbo))) == NULL)
1583 archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1584 return (ARCHIVE_FATAL);
1586 rar->dbo[0].header_size = header_size;
1587 rar->dbo[0].start_offset = -1;
1588 rar->dbo[0].end_offset = -1;
1592 if (rar->file_flags & FHD_SALT)
1595 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1596 "Invalid header size");
1597 return (ARCHIVE_FATAL);
1599 memcpy(rar->salt, p, 8);
1603 if (rar->file_flags & FHD_EXTTIME) {
1604 if (read_exttime(p, rar, endp) < 0) {
1605 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1606 "Invalid header size");
1607 return (ARCHIVE_FATAL);
1611 __archive_read_consume(a, header_size - 7);
1612 rar->dbo[0].start_offset = a->filter->position;
1613 rar->dbo[0].end_offset = rar->dbo[0].start_offset + rar->packed_size;
1615 switch(file_header.host_os)
1620 rar->mode = archive_le32dec(file_header.file_attr);
1621 if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
1622 rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
1624 rar->mode = AE_IFREG;
1625 rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1631 rar->mode = archive_le32dec(file_header.file_attr);
1635 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1636 "Unknown file attributes from RAR file's host OS");
1637 return (ARCHIVE_FATAL);
1640 rar->bytes_uncopied = rar->bytes_unconsumed = 0;
1641 rar->lzss.position = rar->offset = 0;
1642 rar->offset_seek = 0;
1643 rar->dictionary_size = 0;
1644 rar->offset_outgoing = 0;
1645 rar->br.cache_avail = 0;
1646 rar->br.avail_in = 0;
1647 rar->crc_calculated = 0;
1650 rar->is_ppmd_block = 0;
1651 rar->start_new_table = 1;
1652 free(rar->unp_buffer);
1653 rar->unp_buffer = NULL;
1654 rar->unp_offset = 0;
1655 rar->unp_buffer_size = UNP_BUFFER_SIZE;
1656 memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
1657 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1658 rar->ppmd_valid = rar->ppmd_eod = 0;
1660 /* Don't set any archive entries for non-file header types */
1661 if (head_type == NEWSUB_HEAD)
1664 archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
1665 archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
1666 archive_entry_set_atime(entry, rar->atime, rar->ansec);
1667 archive_entry_set_size(entry, rar->unp_size);
1668 archive_entry_set_mode(entry, rar->mode);
1670 if (archive_entry_copy_pathname_l(entry, filename, filename_size, fn_sconv))
1672 if (errno == ENOMEM)
1674 archive_set_error(&a->archive, ENOMEM,
1675 "Can't allocate memory for Pathname");
1676 return (ARCHIVE_FATAL);
1678 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1679 "Pathname cannot be converted from %s to current locale.",
1680 archive_string_conversion_charset_name(fn_sconv));
1681 ret = (ARCHIVE_WARN);
1684 if (((rar->mode) & AE_IFMT) == AE_IFLNK)
1686 /* Make sure a symbolic-link file does not have its body. */
1687 rar->bytes_remaining = 0;
1688 archive_entry_set_size(entry, 0);
1690 /* Read a symbolic-link name. */
1691 if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
1697 if (rar->bytes_remaining == 0)
1707 tm.tm_sec = 2 * (ttime & 0x1f);
1708 tm.tm_min = (ttime >> 5) & 0x3f;
1709 tm.tm_hour = (ttime >> 11) & 0x1f;
1710 tm.tm_mday = (ttime >> 16) & 0x1f;
1711 tm.tm_mon = ((ttime >> 21) & 0x0f) - 1;
1712 tm.tm_year = ((ttime >> 25) & 0x7f) + 80;
1718 read_exttime(const char *p, struct rar *rar, const char *endp)
1720 unsigned rmode, flags, rem, j, count;
1728 flags = archive_le16dec(p);
1731 for (i = 3; i >= 0; i--)
1736 rmode = flags >> i * 4;
1743 ttime = archive_le32dec(p);
1744 t = get_time(ttime);
1749 if (p + count > endp)
1751 for (j = 0; j < count; j++)
1753 rem = (((unsigned)(unsigned char)*p) << 16) | (rem >> 8);
1757 nsec = tm->tm_sec + rem / NS_UNIT;
1781 rar->arcnsec = nsec;
1789 read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
1790 struct archive_string_conv *sconv)
1795 int ret = (ARCHIVE_OK);
1797 rar = (struct rar *)(a->format->data);
1798 if ((h = rar_read_ahead(a, (size_t)rar->packed_size, NULL)) == NULL)
1799 return (ARCHIVE_FATAL);
1802 if (archive_entry_copy_symlink_l(entry,
1803 p, (size_t)rar->packed_size, sconv))
1805 if (errno == ENOMEM)
1807 archive_set_error(&a->archive, ENOMEM,
1808 "Can't allocate memory for link");
1809 return (ARCHIVE_FATAL);
1811 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1812 "link cannot be converted from %s to current locale.",
1813 archive_string_conversion_charset_name(sconv));
1814 ret = (ARCHIVE_WARN);
1816 __archive_read_consume(a, rar->packed_size);
1821 read_data_stored(struct archive_read *a, const void **buff, size_t *size,
1825 ssize_t bytes_avail;
1827 rar = (struct rar *)(a->format->data);
1828 if (rar->bytes_remaining == 0 &&
1829 !(rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER))
1833 *offset = rar->offset;
1834 if (rar->file_crc != rar->crc_calculated) {
1835 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1837 return (ARCHIVE_FATAL);
1840 return (ARCHIVE_EOF);
1843 *buff = rar_read_ahead(a, 1, &bytes_avail);
1844 if (bytes_avail <= 0)
1846 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1847 "Truncated RAR file data");
1848 return (ARCHIVE_FATAL);
1851 *size = bytes_avail;
1852 *offset = rar->offset;
1853 rar->offset += bytes_avail;
1854 rar->offset_seek += bytes_avail;
1855 rar->bytes_remaining -= bytes_avail;
1856 rar->bytes_unconsumed = bytes_avail;
1857 /* Calculate File CRC. */
1858 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1859 (unsigned)bytes_avail);
1860 return (ARCHIVE_OK);
1864 read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
1868 int64_t start, end, actualend;
1870 int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
1872 rar = (struct rar *)(a->format->data);
1876 return (ARCHIVE_FATAL);
1877 if (rar->ppmd_eod ||
1878 (rar->dictionary_size && rar->offset >= rar->unp_size))
1880 if (rar->unp_offset > 0) {
1882 * We have unprocessed extracted data. write it out.
1884 *buff = rar->unp_buffer;
1885 *size = rar->unp_offset;
1886 *offset = rar->offset_outgoing;
1887 rar->offset_outgoing += *size;
1888 /* Calculate File CRC. */
1889 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1891 rar->unp_offset = 0;
1892 return (ARCHIVE_OK);
1896 *offset = rar->offset;
1897 if (rar->file_crc != rar->crc_calculated) {
1898 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1900 return (ARCHIVE_FATAL);
1903 return (ARCHIVE_EOF);
1906 if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
1908 if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
1909 bs = rar->unp_buffer_size - rar->unp_offset;
1911 bs = (size_t)rar->bytes_uncopied;
1912 ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
1913 if (ret != ARCHIVE_OK)
1916 rar->bytes_uncopied -= bs;
1917 if (*buff != NULL) {
1918 rar->unp_offset = 0;
1919 *size = rar->unp_buffer_size;
1920 *offset = rar->offset_outgoing;
1921 rar->offset_outgoing += *size;
1922 /* Calculate File CRC. */
1923 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1930 if (!rar->br.next_in &&
1931 (ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
1933 if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
1936 if (rar->is_ppmd_block)
1938 if ((sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1939 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1941 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1943 return (ARCHIVE_FATAL);
1945 if(sym != rar->ppmd_escape)
1947 lzss_emit_literal(rar, sym);
1948 rar->bytes_uncopied++;
1952 if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1953 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1955 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1957 return (ARCHIVE_FATAL);
1963 rar->start_new_table = 1;
1964 return read_data_compressed(a, buff, size, offset);
1967 rar->ppmd_eod = 1;/* End Of ppmd Data. */
1971 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1972 "Parsing filters is unsupported.");
1973 return (ARCHIVE_FAILED);
1977 for (i = 2; i >= 0; i--)
1979 if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1980 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1982 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1984 return (ARCHIVE_FATAL);
1986 lzss_offset |= code << (i * 8);
1988 if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1989 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1991 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1993 return (ARCHIVE_FATAL);
1995 lzss_emit_match(rar, lzss_offset + 2, length + 32);
1996 rar->bytes_uncopied += length + 32;
2000 if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2001 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2003 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2005 return (ARCHIVE_FATAL);
2007 lzss_emit_match(rar, 1, length + 4);
2008 rar->bytes_uncopied += length + 4;
2012 lzss_emit_literal(rar, sym);
2013 rar->bytes_uncopied++;
2019 start = rar->offset;
2020 end = start + rar->dictionary_size;
2021 rar->filterstart = INT64_MAX;
2023 if ((actualend = expand(a, end)) < 0)
2024 return ((int)actualend);
2026 rar->bytes_uncopied = actualend - start;
2027 if (rar->bytes_uncopied == 0) {
2028 /* Broken RAR files cause this case.
2029 * NOTE: If this case were possible on a normal RAR file
2030 * we would find out where it was actually bad and
2031 * what we would do to solve it. */
2032 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2033 "Internal error extracting RAR file");
2034 return (ARCHIVE_FATAL);
2037 if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
2038 bs = rar->unp_buffer_size - rar->unp_offset;
2040 bs = (size_t)rar->bytes_uncopied;
2041 ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
2042 if (ret != ARCHIVE_OK)
2045 rar->bytes_uncopied -= bs;
2047 * If *buff is NULL, it means unp_buffer is not full.
2048 * So we have to continue extracting a RAR file.
2050 } while (*buff == NULL);
2052 rar->unp_offset = 0;
2053 *size = rar->unp_buffer_size;
2054 *offset = rar->offset_outgoing;
2055 rar->offset_outgoing += *size;
2056 /* Calculate File CRC. */
2057 rar->crc_calculated = crc32(rar->crc_calculated, *buff, (unsigned)*size);
2062 parse_codes(struct archive_read *a)
2064 int i, j, val, n, r;
2065 unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
2066 unsigned int maxorder;
2067 struct huffman_code precode;
2068 struct rar *rar = (struct rar *)(a->format->data);
2069 struct rar_br *br = &(rar->br);
2073 /* Skip to the next byte */
2074 rar_br_consume_unalined_bits(br);
2076 /* PPMd block flag */
2077 if (!rar_br_read_ahead(a, br, 1))
2078 goto truncated_data;
2079 if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
2081 rar_br_consume(br, 1);
2082 if (!rar_br_read_ahead(a, br, 7))
2083 goto truncated_data;
2084 ppmd_flags = rar_br_bits(br, 7);
2085 rar_br_consume(br, 7);
2087 /* Memory is allocated in MB */
2088 if (ppmd_flags & 0x20)
2090 if (!rar_br_read_ahead(a, br, 8))
2091 goto truncated_data;
2092 rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
2093 rar_br_consume(br, 8);
2096 if (ppmd_flags & 0x40)
2098 if (!rar_br_read_ahead(a, br, 8))
2099 goto truncated_data;
2100 rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
2101 rar_br_consume(br, 8);
2104 rar->ppmd_escape = 2;
2106 if (ppmd_flags & 0x20)
2108 maxorder = (ppmd_flags & 0x1F) + 1;
2110 maxorder = 16 + (maxorder - 16) * 3;
2114 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2115 "Truncated RAR file data");
2116 return (ARCHIVE_FATAL);
2119 /* Make sure ppmd7_contest is freed before Ppmd7_Construct
2120 * because reading a broken file cause this abnormal sequence. */
2121 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
2124 rar->bytein.Read = &ppmd_read;
2125 __archive_ppmd7_functions.PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
2126 rar->range_dec.Stream = &rar->bytein;
2127 __archive_ppmd7_functions.Ppmd7_Construct(&rar->ppmd7_context);
2129 if (rar->dictionary_size == 0) {
2130 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2131 "Invalid zero dictionary size");
2132 return (ARCHIVE_FATAL);
2135 if (!__archive_ppmd7_functions.Ppmd7_Alloc(&rar->ppmd7_context,
2136 rar->dictionary_size))
2138 archive_set_error(&a->archive, ENOMEM,
2140 return (ARCHIVE_FATAL);
2142 if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2144 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2145 "Unable to initialize PPMd range decoder");
2146 return (ARCHIVE_FATAL);
2148 __archive_ppmd7_functions.Ppmd7_Init(&rar->ppmd7_context, maxorder);
2149 rar->ppmd_valid = 1;
2153 if (!rar->ppmd_valid) {
2154 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2155 "Invalid PPMd sequence");
2156 return (ARCHIVE_FATAL);
2158 if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2160 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2161 "Unable to initialize PPMd range decoder");
2162 return (ARCHIVE_FATAL);
2168 rar_br_consume(br, 1);
2170 /* Keep existing table flag */
2171 if (!rar_br_read_ahead(a, br, 1))
2172 goto truncated_data;
2173 if (!rar_br_bits(br, 1))
2174 memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
2175 rar_br_consume(br, 1);
2177 memset(&bitlengths, 0, sizeof(bitlengths));
2178 for (i = 0; i < MAX_SYMBOLS;)
2180 if (!rar_br_read_ahead(a, br, 4))
2181 goto truncated_data;
2182 bitlengths[i++] = rar_br_bits(br, 4);
2183 rar_br_consume(br, 4);
2184 if (bitlengths[i-1] == 0xF)
2186 if (!rar_br_read_ahead(a, br, 4))
2187 goto truncated_data;
2188 zerocount = rar_br_bits(br, 4);
2189 rar_br_consume(br, 4);
2193 for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
2194 bitlengths[i++] = 0;
2199 memset(&precode, 0, sizeof(precode));
2200 r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
2201 if (r != ARCHIVE_OK) {
2203 free(precode.table);
2207 for (i = 0; i < HUFFMAN_TABLE_SIZE;)
2209 if ((val = read_next_symbol(a, &precode)) < 0) {
2211 free(precode.table);
2212 return (ARCHIVE_FATAL);
2216 rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
2224 free(precode.table);
2225 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2226 "Internal error extracting RAR file.");
2227 return (ARCHIVE_FATAL);
2231 if (!rar_br_read_ahead(a, br, 3)) {
2233 free(precode.table);
2234 goto truncated_data;
2236 n = rar_br_bits(br, 3) + 3;
2237 rar_br_consume(br, 3);
2239 if (!rar_br_read_ahead(a, br, 7)) {
2241 free(precode.table);
2242 goto truncated_data;
2244 n = rar_br_bits(br, 7) + 11;
2245 rar_br_consume(br, 7);
2248 for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2250 rar->lengthtable[i] = rar->lengthtable[i-1];
2257 if (!rar_br_read_ahead(a, br, 3)) {
2259 free(precode.table);
2260 goto truncated_data;
2262 n = rar_br_bits(br, 3) + 3;
2263 rar_br_consume(br, 3);
2265 if (!rar_br_read_ahead(a, br, 7)) {
2267 free(precode.table);
2268 goto truncated_data;
2270 n = rar_br_bits(br, 7) + 11;
2271 rar_br_consume(br, 7);
2274 for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2275 rar->lengthtable[i++] = 0;
2279 free(precode.table);
2281 r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
2283 if (r != ARCHIVE_OK)
2285 r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
2286 OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2287 if (r != ARCHIVE_OK)
2289 r = create_code(a, &rar->lowoffsetcode,
2290 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
2291 LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2292 if (r != ARCHIVE_OK)
2294 r = create_code(a, &rar->lengthcode,
2295 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
2296 LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
2297 if (r != ARCHIVE_OK)
2301 if (!rar->dictionary_size || !rar->lzss.window)
2303 /* Seems as though dictionary sizes are not used. Even so, minimize
2304 * memory usage as much as possible.
2307 unsigned int new_size;
2309 if (rar->unp_size >= DICTIONARY_MAX_SIZE)
2310 new_size = DICTIONARY_MAX_SIZE;
2312 new_size = rar_fls((unsigned int)rar->unp_size) << 1;
2313 if (new_size == 0) {
2314 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2315 "Zero window size is invalid.");
2316 return (ARCHIVE_FATAL);
2318 new_window = realloc(rar->lzss.window, new_size);
2319 if (new_window == NULL) {
2320 archive_set_error(&a->archive, ENOMEM,
2321 "Unable to allocate memory for uncompressed data.");
2322 return (ARCHIVE_FATAL);
2324 rar->lzss.window = (unsigned char *)new_window;
2325 rar->dictionary_size = new_size;
2326 memset(rar->lzss.window, 0, rar->dictionary_size);
2327 rar->lzss.mask = rar->dictionary_size - 1;
2330 rar->start_new_table = 0;
2331 return (ARCHIVE_OK);
2333 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2334 "Truncated RAR file data");
2336 return (ARCHIVE_FATAL);
2340 free_codes(struct archive_read *a)
2342 struct rar *rar = (struct rar *)(a->format->data);
2343 free(rar->maincode.tree);
2344 free(rar->offsetcode.tree);
2345 free(rar->lowoffsetcode.tree);
2346 free(rar->lengthcode.tree);
2347 free(rar->maincode.table);
2348 free(rar->offsetcode.table);
2349 free(rar->lowoffsetcode.table);
2350 free(rar->lengthcode.table);
2351 memset(&rar->maincode, 0, sizeof(rar->maincode));
2352 memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
2353 memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
2354 memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
2359 read_next_symbol(struct archive_read *a, struct huffman_code *code)
2363 int length, value, node;
2369 if (make_table(a, code) != (ARCHIVE_OK))
2373 rar = (struct rar *)(a->format->data);
2376 /* Look ahead (peek) at bits */
2377 if (!rar_br_read_ahead(a, br, code->tablesize)) {
2378 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2379 "Truncated RAR file data");
2383 bits = rar_br_bits(br, code->tablesize);
2385 length = code->table[bits].length;
2386 value = code->table[bits].value;
2390 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2391 "Invalid prefix code in bitstream");
2395 if (length <= code->tablesize)
2397 /* Skip length bits */
2398 rar_br_consume(br, length);
2402 /* Skip tablesize bits */
2403 rar_br_consume(br, code->tablesize);
2406 while (!(code->tree[node].branches[0] ==
2407 code->tree[node].branches[1]))
2409 if (!rar_br_read_ahead(a, br, 1)) {
2410 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2411 "Truncated RAR file data");
2415 bit = rar_br_bits(br, 1);
2416 rar_br_consume(br, 1);
2418 if (code->tree[node].branches[bit] < 0)
2420 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2421 "Invalid prefix code in bitstream");
2424 node = code->tree[node].branches[bit];
2427 return code->tree[node].branches[0];
2431 create_code(struct archive_read *a, struct huffman_code *code,
2432 unsigned char *lengths, int numsymbols, char maxlength)
2434 int i, j, codebits = 0, symbolsleft = numsymbols;
2436 code->numentries = 0;
2437 code->numallocatedentries = 0;
2438 if (new_node(code) < 0) {
2439 archive_set_error(&a->archive, ENOMEM,
2440 "Unable to allocate memory for node data.");
2441 return (ARCHIVE_FATAL);
2443 code->numentries = 1;
2444 code->minlength = INT_MAX;
2445 code->maxlength = INT_MIN;
2447 for(i = 1; i <= maxlength; i++)
2449 for(j = 0; j < numsymbols; j++)
2451 if (lengths[j] != i) continue;
2452 if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
2453 return (ARCHIVE_FATAL);
2455 if (--symbolsleft <= 0) { break; break; }
2459 return (ARCHIVE_OK);
2463 add_value(struct archive_read *a, struct huffman_code *code, int value,
2464 int codebits, int length)
2466 int repeatpos, lastnode, bitpos, bit, repeatnode, nextnode;
2471 if(length > code->maxlength)
2472 code->maxlength = length;
2473 if(length < code->minlength)
2474 code->minlength = length;
2477 if (repeatpos == 0 || (repeatpos >= 0
2478 && (((codebits >> (repeatpos - 1)) & 3) == 0
2479 || ((codebits >> (repeatpos - 1)) & 3) == 3)))
2481 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2482 "Invalid repeat position");
2483 return (ARCHIVE_FATAL);
2487 for (bitpos = length - 1; bitpos >= 0; bitpos--)
2489 bit = (codebits >> bitpos) & 1;
2491 /* Leaf node check */
2492 if (code->tree[lastnode].branches[0] ==
2493 code->tree[lastnode].branches[1])
2495 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2497 return (ARCHIVE_FATAL);
2500 if (bitpos == repeatpos)
2502 /* Open branch check */
2503 if (!(code->tree[lastnode].branches[bit] < 0))
2505 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2506 "Invalid repeating code");
2507 return (ARCHIVE_FATAL);
2510 if ((repeatnode = new_node(code)) < 0) {
2511 archive_set_error(&a->archive, ENOMEM,
2512 "Unable to allocate memory for node data.");
2513 return (ARCHIVE_FATAL);
2515 if ((nextnode = new_node(code)) < 0) {
2516 archive_set_error(&a->archive, ENOMEM,
2517 "Unable to allocate memory for node data.");
2518 return (ARCHIVE_FATAL);
2522 code->tree[lastnode].branches[bit] = repeatnode;
2523 code->tree[repeatnode].branches[bit] = repeatnode;
2524 code->tree[repeatnode].branches[bit^1] = nextnode;
2525 lastnode = nextnode;
2527 bitpos++; /* terminating bit already handled, skip it */
2531 /* Open branch check */
2532 if (code->tree[lastnode].branches[bit] < 0)
2534 if (new_node(code) < 0) {
2535 archive_set_error(&a->archive, ENOMEM,
2536 "Unable to allocate memory for node data.");
2537 return (ARCHIVE_FATAL);
2539 code->tree[lastnode].branches[bit] = code->numentries++;
2543 lastnode = code->tree[lastnode].branches[bit];
2547 if (!(code->tree[lastnode].branches[0] == -1
2548 && code->tree[lastnode].branches[1] == -2))
2550 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2552 return (ARCHIVE_FATAL);
2555 /* Set leaf value */
2556 code->tree[lastnode].branches[0] = value;
2557 code->tree[lastnode].branches[1] = value;
2559 return (ARCHIVE_OK);
2563 new_node(struct huffman_code *code)
2566 if (code->numallocatedentries == code->numentries) {
2567 int new_num_entries = 256;
2568 if (code->numentries > 0) {
2569 new_num_entries = code->numentries * 2;
2571 new_tree = realloc(code->tree, new_num_entries * sizeof(*code->tree));
2572 if (new_tree == NULL)
2574 code->tree = (struct huffman_tree_node *)new_tree;
2575 code->numallocatedentries = new_num_entries;
2577 code->tree[code->numentries].branches[0] = -1;
2578 code->tree[code->numentries].branches[1] = -2;
2583 make_table(struct archive_read *a, struct huffman_code *code)
2585 if (code->maxlength < code->minlength || code->maxlength > 10)
2586 code->tablesize = 10;
2588 code->tablesize = code->maxlength;
2591 (struct huffman_table_entry *)calloc(1, sizeof(*code->table)
2592 * ((size_t)1 << code->tablesize));
2594 return make_table_recurse(a, code, 0, code->table, 0, code->tablesize);
2598 make_table_recurse(struct archive_read *a, struct huffman_code *code, int node,
2599 struct huffman_table_entry *table, int depth,
2602 int currtablesize, i, ret = (ARCHIVE_OK);
2606 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2607 "Huffman tree was not created.");
2608 return (ARCHIVE_FATAL);
2610 if (node < 0 || node >= code->numentries)
2612 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2613 "Invalid location to Huffman tree specified.");
2614 return (ARCHIVE_FATAL);
2617 currtablesize = 1 << (maxdepth - depth);
2619 if (code->tree[node].branches[0] ==
2620 code->tree[node].branches[1])
2622 for(i = 0; i < currtablesize; i++)
2624 table[i].length = depth;
2625 table[i].value = code->tree[node].branches[0];
2630 for(i = 0; i < currtablesize; i++)
2631 table[i].length = -1;
2635 if(depth == maxdepth)
2637 table[0].length = maxdepth + 1;
2638 table[0].value = node;
2642 ret |= make_table_recurse(a, code, code->tree[node].branches[0], table,
2643 depth + 1, maxdepth);
2644 ret |= make_table_recurse(a, code, code->tree[node].branches[1],
2645 table + currtablesize / 2, depth + 1, maxdepth);
2652 expand(struct archive_read *a, int64_t end)
2654 static const unsigned char lengthbases[] =
2655 { 0, 1, 2, 3, 4, 5, 6,
2656 7, 8, 10, 12, 14, 16, 20,
2657 24, 28, 32, 40, 48, 56, 64,
2658 80, 96, 112, 128, 160, 192, 224 };
2659 static const unsigned char lengthbits[] =
2660 { 0, 0, 0, 0, 0, 0, 0,
2661 0, 1, 1, 1, 1, 2, 2,
2662 2, 2, 3, 3, 3, 3, 4,
2663 4, 4, 4, 5, 5, 5, 5 };
2664 static const unsigned int offsetbases[] =
2666 8, 12, 16, 24, 32, 48,
2667 64, 96, 128, 192, 256, 384,
2668 512, 768, 1024, 1536, 2048, 3072,
2669 4096, 6144, 8192, 12288, 16384, 24576,
2670 32768, 49152, 65536, 98304, 131072, 196608,
2671 262144, 327680, 393216, 458752, 524288, 589824,
2672 655360, 720896, 786432, 851968, 917504, 983040,
2673 1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
2674 2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
2675 static const unsigned char offsetbits[] =
2676 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
2677 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
2678 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
2679 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2680 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
2681 static const unsigned char shortbases[] =
2682 { 0, 4, 8, 16, 32, 64, 128, 192 };
2683 static const unsigned char shortbits[] =
2684 { 2, 2, 3, 4, 5, 6, 6, 6 };
2686 int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
2687 unsigned char newfile;
2688 struct rar *rar = (struct rar *)(a->format->data);
2689 struct rar_br *br = &(rar->br);
2691 if (rar->filterstart < end)
2692 end = rar->filterstart;
2696 if (rar->output_last_match &&
2697 lzss_position(&rar->lzss) + rar->lastlength <= end)
2699 lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
2700 rar->output_last_match = 0;
2703 if(rar->is_ppmd_block || rar->output_last_match ||
2704 lzss_position(&rar->lzss) >= end)
2705 return lzss_position(&rar->lzss);
2707 if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
2708 return (ARCHIVE_FATAL);
2709 rar->output_last_match = 0;
2713 lzss_emit_literal(rar, symbol);
2716 else if (symbol == 256)
2718 if (!rar_br_read_ahead(a, br, 1))
2719 goto truncated_data;
2720 newfile = !rar_br_bits(br, 1);
2721 rar_br_consume(br, 1);
2725 rar->start_new_block = 1;
2726 if (!rar_br_read_ahead(a, br, 1))
2727 goto truncated_data;
2728 rar->start_new_table = rar_br_bits(br, 1);
2729 rar_br_consume(br, 1);
2730 return lzss_position(&rar->lzss);
2734 if (parse_codes(a) != ARCHIVE_OK)
2735 return (ARCHIVE_FATAL);
2739 else if(symbol==257)
2741 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2742 "Parsing filters is unsupported.");
2743 return (ARCHIVE_FAILED);
2745 else if(symbol==258)
2747 if(rar->lastlength == 0)
2750 offs = rar->lastoffset;
2751 len = rar->lastlength;
2753 else if (symbol <= 262)
2755 offsindex = symbol - 259;
2756 offs = rar->oldoffset[offsindex];
2758 if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
2760 if (lensymbol > (int)(sizeof(lengthbases)/sizeof(lengthbases[0])))
2762 if (lensymbol > (int)(sizeof(lengthbits)/sizeof(lengthbits[0])))
2764 len = lengthbases[lensymbol] + 2;
2765 if (lengthbits[lensymbol] > 0) {
2766 if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
2767 goto truncated_data;
2768 len += rar_br_bits(br, lengthbits[lensymbol]);
2769 rar_br_consume(br, lengthbits[lensymbol]);
2772 for (i = offsindex; i > 0; i--)
2773 rar->oldoffset[i] = rar->oldoffset[i-1];
2774 rar->oldoffset[0] = offs;
2776 else if(symbol<=270)
2778 offs = shortbases[symbol-263] + 1;
2779 if(shortbits[symbol-263] > 0) {
2780 if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
2781 goto truncated_data;
2782 offs += rar_br_bits(br, shortbits[symbol-263]);
2783 rar_br_consume(br, shortbits[symbol-263]);
2788 for(i = 3; i > 0; i--)
2789 rar->oldoffset[i] = rar->oldoffset[i-1];
2790 rar->oldoffset[0] = offs;
2794 if (symbol-271 > (int)(sizeof(lengthbases)/sizeof(lengthbases[0])))
2796 if (symbol-271 > (int)(sizeof(lengthbits)/sizeof(lengthbits[0])))
2798 len = lengthbases[symbol-271]+3;
2799 if(lengthbits[symbol-271] > 0) {
2800 if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
2801 goto truncated_data;
2802 len += rar_br_bits(br, lengthbits[symbol-271]);
2803 rar_br_consume(br, lengthbits[symbol-271]);
2806 if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
2808 if (offssymbol > (int)(sizeof(offsetbases)/sizeof(offsetbases[0])))
2810 if (offssymbol > (int)(sizeof(offsetbits)/sizeof(offsetbits[0])))
2812 offs = offsetbases[offssymbol]+1;
2813 if(offsetbits[offssymbol] > 0)
2817 if(offsetbits[offssymbol] > 4) {
2818 if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
2819 goto truncated_data;
2820 offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
2821 rar_br_consume(br, offsetbits[offssymbol] - 4);
2824 if(rar->numlowoffsetrepeats > 0)
2826 rar->numlowoffsetrepeats--;
2827 offs += rar->lastlowoffset;
2831 if ((lowoffsetsymbol =
2832 read_next_symbol(a, &rar->lowoffsetcode)) < 0)
2833 return (ARCHIVE_FATAL);
2834 if(lowoffsetsymbol == 16)
2836 rar->numlowoffsetrepeats = 15;
2837 offs += rar->lastlowoffset;
2841 offs += lowoffsetsymbol;
2842 rar->lastlowoffset = lowoffsetsymbol;
2847 if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
2848 goto truncated_data;
2849 offs += rar_br_bits(br, offsetbits[offssymbol]);
2850 rar_br_consume(br, offsetbits[offssymbol]);
2854 if (offs >= 0x40000)
2859 for(i = 3; i > 0; i--)
2860 rar->oldoffset[i] = rar->oldoffset[i-1];
2861 rar->oldoffset[0] = offs;
2864 rar->lastoffset = offs;
2865 rar->lastlength = len;
2866 rar->output_last_match = 1;
2869 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2870 "Truncated RAR file data");
2872 return (ARCHIVE_FATAL);
2874 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2875 "Bad RAR file data");
2876 return (ARCHIVE_FATAL);
2880 copy_from_lzss_window(struct archive_read *a, const void **buffer,
2881 int64_t startpos, int length)
2883 int windowoffs, firstpart;
2884 struct rar *rar = (struct rar *)(a->format->data);
2886 if (!rar->unp_buffer)
2888 if ((rar->unp_buffer = malloc(rar->unp_buffer_size)) == NULL)
2890 archive_set_error(&a->archive, ENOMEM,
2891 "Unable to allocate memory for uncompressed data.");
2892 return (ARCHIVE_FATAL);
2896 windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
2897 if(windowoffs + length <= lzss_size(&rar->lzss)) {
2898 memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
2900 } else if (length <= lzss_size(&rar->lzss)) {
2901 firstpart = lzss_size(&rar->lzss) - windowoffs;
2902 if (firstpart < 0) {
2903 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2904 "Bad RAR file data");
2905 return (ARCHIVE_FATAL);
2907 if (firstpart < length) {
2908 memcpy(&rar->unp_buffer[rar->unp_offset],
2909 &rar->lzss.window[windowoffs], firstpart);
2910 memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
2911 &rar->lzss.window[0], length - firstpart);
2913 memcpy(&rar->unp_buffer[rar->unp_offset],
2914 &rar->lzss.window[windowoffs], length);
2917 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2918 "Bad RAR file data");
2919 return (ARCHIVE_FATAL);
2921 rar->unp_offset += length;
2922 if (rar->unp_offset >= rar->unp_buffer_size)
2923 *buffer = rar->unp_buffer;
2926 return (ARCHIVE_OK);
2930 rar_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
2932 struct rar *rar = (struct rar *)(a->format->data);
2933 const void *h = __archive_read_ahead(a, min, avail);
2937 if (a->archive.read_data_is_posix_read && *avail > (ssize_t)a->archive.read_data_requested)
2938 *avail = a->archive.read_data_requested;
2939 if (*avail > rar->bytes_remaining)
2940 *avail = (ssize_t)rar->bytes_remaining;
2943 else if (*avail == 0 && rar->main_flags & MHD_VOLUME &&
2944 rar->file_flags & FHD_SPLIT_AFTER)
2946 rar->filename_must_match = 1;
2947 ret = archive_read_format_rar_read_header(a, a->entry);
2948 if (ret == (ARCHIVE_EOF))
2950 rar->has_endarc_header = 1;
2951 ret = archive_read_format_rar_read_header(a, a->entry);
2953 rar->filename_must_match = 0;
2954 if (ret != (ARCHIVE_OK))
2956 return rar_read_ahead(a, min, avail);