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
152 #define minimum(a, b) ((a)<(b)?(a):(b))
154 /* Fields common to all headers */
163 /* Fields common to all file headers */
164 struct rar_file_header
177 struct huffman_tree_node
182 struct huffman_table_entry
190 struct huffman_tree_node *tree;
192 int numallocatedentries;
196 struct huffman_table_entry *table;
201 unsigned char *window;
206 struct data_block_offsets
209 int64_t start_offset;
215 /* Entries from main RAR header */
217 unsigned long file_crc;
222 /* File header entries */
223 char compression_method;
232 size_t filename_save_size;
233 size_t filename_allocated;
235 /* File header optional entries */
244 /* Fields to help with tracking decompression of files. */
245 int64_t bytes_unconsumed;
246 int64_t bytes_remaining;
247 int64_t bytes_uncopied;
249 int64_t offset_outgoing;
252 unsigned int unp_offset;
253 unsigned int unp_buffer_size;
254 unsigned char *unp_buffer;
255 unsigned int dictionary_size;
256 char start_new_block;
258 unsigned long crc_calculated;
259 int found_first_header;
260 char has_endarc_header;
261 struct data_block_offsets *dbo;
266 struct huffman_code maincode;
267 struct huffman_code offsetcode;
268 struct huffman_code lowoffsetcode;
269 struct huffman_code lengthcode;
270 unsigned char lengthtable[HUFFMAN_TABLE_SIZE];
272 char output_last_match;
273 unsigned int lastlength;
274 unsigned int lastoffset;
275 unsigned int oldoffset[4];
276 unsigned int lastlowoffset;
277 unsigned int numlowoffsetrepeats;
279 char start_new_table;
281 /* PPMd Variant H members */
286 CPpmd7 ppmd7_context;
287 CPpmd7z_RangeDec range_dec;
291 * String conversion object.
293 int init_default_conversion;
294 struct archive_string_conv *sconv_default;
295 struct archive_string_conv *opt_sconv;
296 struct archive_string_conv *sconv_utf8;
297 struct archive_string_conv *sconv_utf16be;
303 #define CACHE_TYPE uint64_t
304 #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
306 CACHE_TYPE cache_buffer;
307 /* Indicates how many bits avail in cache_buffer. */
310 const unsigned char *next_in;
314 * Custom field to denote that this archive contains encrypted entries
316 int has_encrypted_entries;
319 static int archive_read_support_format_rar_capabilities(struct archive_read *);
320 static int archive_read_format_rar_has_encrypted_entries(struct archive_read *);
321 static int archive_read_format_rar_bid(struct archive_read *, int);
322 static int archive_read_format_rar_options(struct archive_read *,
323 const char *, const char *);
324 static int archive_read_format_rar_read_header(struct archive_read *,
325 struct archive_entry *);
326 static int archive_read_format_rar_read_data(struct archive_read *,
327 const void **, size_t *, int64_t *);
328 static int archive_read_format_rar_read_data_skip(struct archive_read *a);
329 static int64_t archive_read_format_rar_seek_data(struct archive_read *, int64_t,
331 static int archive_read_format_rar_cleanup(struct archive_read *);
333 /* Support functions */
334 static int read_header(struct archive_read *, struct archive_entry *, char);
335 static time_t get_time(int);
336 static int read_exttime(const char *, struct rar *, const char *);
337 static int read_symlink_stored(struct archive_read *, struct archive_entry *,
338 struct archive_string_conv *);
339 static int read_data_stored(struct archive_read *, const void **, size_t *,
341 static int read_data_compressed(struct archive_read *, const void **, size_t *,
343 static int rar_br_preparation(struct archive_read *, struct rar_br *);
344 static int parse_codes(struct archive_read *);
345 static void free_codes(struct archive_read *);
346 static int read_next_symbol(struct archive_read *, struct huffman_code *);
347 static int create_code(struct archive_read *, struct huffman_code *,
348 unsigned char *, int, char);
349 static int add_value(struct archive_read *, struct huffman_code *, int, int,
351 static int new_node(struct huffman_code *);
352 static int make_table(struct archive_read *, struct huffman_code *);
353 static int make_table_recurse(struct archive_read *, struct huffman_code *, int,
354 struct huffman_table_entry *, int, int);
355 static int64_t expand(struct archive_read *, int64_t);
356 static int copy_from_lzss_window(struct archive_read *, const void **,
358 static const void *rar_read_ahead(struct archive_read *, size_t, ssize_t *);
363 /* Check that the cache buffer has enough bits. */
364 #define rar_br_has(br, n) ((br)->cache_avail >= n)
365 /* Get compressed data by bit. */
366 #define rar_br_bits(br, n) \
367 (((uint32_t)((br)->cache_buffer >> \
368 ((br)->cache_avail - (n)))) & cache_masks[n])
369 #define rar_br_bits_forced(br, n) \
370 (((uint32_t)((br)->cache_buffer << \
371 ((n) - (br)->cache_avail))) & cache_masks[n])
372 /* Read ahead to make sure the cache buffer has enough compressed data we
374 * True : completed, there is enough data in the cache buffer.
375 * False : there is no data in the stream. */
376 #define rar_br_read_ahead(a, br, n) \
377 ((rar_br_has(br, (n)) || rar_br_fillup(a, br)) || rar_br_has(br, (n)))
378 /* Notify how many bits we consumed. */
379 #define rar_br_consume(br, n) ((br)->cache_avail -= (n))
380 #define rar_br_consume_unalined_bits(br) ((br)->cache_avail &= ~7)
382 static const uint32_t cache_masks[] = {
383 0x00000000, 0x00000001, 0x00000003, 0x00000007,
384 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
385 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
386 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
387 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
388 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
389 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
390 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
391 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
395 * Shift away used bits in the cache data and fill it up with following bits.
396 * Call this when cache buffer does not have enough bits you need.
398 * Returns 1 if the cache buffer is full.
399 * Returns 0 if the cache buffer is not full; input buffer is empty.
402 rar_br_fillup(struct archive_read *a, struct rar_br *br)
404 struct rar *rar = (struct rar *)(a->format->data);
405 int n = CACHE_BITS - br->cache_avail;
410 if (br->avail_in >= 8) {
412 ((uint64_t)br->next_in[0]) << 56 |
413 ((uint64_t)br->next_in[1]) << 48 |
414 ((uint64_t)br->next_in[2]) << 40 |
415 ((uint64_t)br->next_in[3]) << 32 |
416 ((uint32_t)br->next_in[4]) << 24 |
417 ((uint32_t)br->next_in[5]) << 16 |
418 ((uint32_t)br->next_in[6]) << 8 |
419 (uint32_t)br->next_in[7];
422 br->cache_avail += 8 * 8;
423 rar->bytes_unconsumed += 8;
424 rar->bytes_remaining -= 8;
429 if (br->avail_in >= 7) {
431 (br->cache_buffer << 56) |
432 ((uint64_t)br->next_in[0]) << 48 |
433 ((uint64_t)br->next_in[1]) << 40 |
434 ((uint64_t)br->next_in[2]) << 32 |
435 ((uint32_t)br->next_in[3]) << 24 |
436 ((uint32_t)br->next_in[4]) << 16 |
437 ((uint32_t)br->next_in[5]) << 8 |
438 (uint32_t)br->next_in[6];
441 br->cache_avail += 7 * 8;
442 rar->bytes_unconsumed += 7;
443 rar->bytes_remaining -= 7;
448 if (br->avail_in >= 6) {
450 (br->cache_buffer << 48) |
451 ((uint64_t)br->next_in[0]) << 40 |
452 ((uint64_t)br->next_in[1]) << 32 |
453 ((uint32_t)br->next_in[2]) << 24 |
454 ((uint32_t)br->next_in[3]) << 16 |
455 ((uint32_t)br->next_in[4]) << 8 |
456 (uint32_t)br->next_in[5];
459 br->cache_avail += 6 * 8;
460 rar->bytes_unconsumed += 6;
461 rar->bytes_remaining -= 6;
466 /* We have enough compressed data in
467 * the cache buffer.*/
472 if (br->avail_in <= 0) {
474 if (rar->bytes_unconsumed > 0) {
475 /* Consume as much as the decompressor
477 __archive_read_consume(a, rar->bytes_unconsumed);
478 rar->bytes_unconsumed = 0;
480 br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
481 if (br->next_in == NULL)
483 if (br->avail_in == 0)
487 (br->cache_buffer << 8) | *br->next_in++;
489 br->cache_avail += 8;
491 rar->bytes_unconsumed++;
492 rar->bytes_remaining--;
497 rar_br_preparation(struct archive_read *a, struct rar_br *br)
499 struct rar *rar = (struct rar *)(a->format->data);
501 if (rar->bytes_remaining > 0) {
502 br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
503 if (br->next_in == NULL) {
504 archive_set_error(&a->archive,
505 ARCHIVE_ERRNO_FILE_FORMAT,
506 "Truncated RAR file data");
507 return (ARCHIVE_FATAL);
509 if (br->cache_avail == 0)
510 (void)rar_br_fillup(a, br);
515 /* Find last bit set */
517 rar_fls(unsigned int word)
523 word |= (word >> 16);
524 return word - (word >> 1);
528 static inline int64_t
529 lzss_position(struct lzss *lzss)
531 return lzss->position;
535 lzss_mask(struct lzss *lzss)
541 lzss_size(struct lzss *lzss)
543 return lzss->mask + 1;
547 lzss_offset_for_position(struct lzss *lzss, int64_t pos)
549 return (int)(pos & lzss->mask);
552 static inline unsigned char *
553 lzss_pointer_for_position(struct lzss *lzss, int64_t pos)
555 return &lzss->window[lzss_offset_for_position(lzss, pos)];
559 lzss_current_offset(struct lzss *lzss)
561 return lzss_offset_for_position(lzss, lzss->position);
564 static inline uint8_t *
565 lzss_current_pointer(struct lzss *lzss)
567 return lzss_pointer_for_position(lzss, lzss->position);
571 lzss_emit_literal(struct rar *rar, uint8_t literal)
573 *lzss_current_pointer(&rar->lzss) = literal;
574 rar->lzss.position++;
578 lzss_emit_match(struct rar *rar, int offset, int length)
580 int dstoffs = lzss_current_offset(&rar->lzss);
581 int srcoffs = (dstoffs - offset) & lzss_mask(&rar->lzss);
582 int l, li, remaining;
583 unsigned char *d, *s;
586 while (remaining > 0) {
588 if (dstoffs > srcoffs) {
589 if (l > lzss_size(&rar->lzss) - dstoffs)
590 l = lzss_size(&rar->lzss) - dstoffs;
592 if (l > lzss_size(&rar->lzss) - srcoffs)
593 l = lzss_size(&rar->lzss) - srcoffs;
595 d = &(rar->lzss.window[dstoffs]);
596 s = &(rar->lzss.window[srcoffs]);
597 if ((dstoffs + l < srcoffs) || (srcoffs + l < dstoffs))
600 for (li = 0; li < l; li++)
604 dstoffs = (dstoffs + l) & lzss_mask(&(rar->lzss));
605 srcoffs = (srcoffs + l) & lzss_mask(&(rar->lzss));
607 rar->lzss.position += length;
613 struct archive_read *a = ((IByteIn*)p)->a;
614 struct rar *rar = (struct rar *)(a->format->data);
615 struct rar_br *br = &(rar->br);
617 if (!rar_br_read_ahead(a, br, 8))
619 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
620 "Truncated RAR file data");
624 b = rar_br_bits(br, 8);
625 rar_br_consume(br, 8);
630 archive_read_support_format_rar(struct archive *_a)
632 struct archive_read *a = (struct archive_read *)_a;
636 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
637 "archive_read_support_format_rar");
639 rar = (struct rar *)calloc(sizeof(*rar), 1);
642 archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
643 return (ARCHIVE_FATAL);
647 * Until enough data has been read, we cannot tell about
648 * any encrypted entries yet.
650 rar->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
652 r = __archive_read_register_format(a,
655 archive_read_format_rar_bid,
656 archive_read_format_rar_options,
657 archive_read_format_rar_read_header,
658 archive_read_format_rar_read_data,
659 archive_read_format_rar_read_data_skip,
660 archive_read_format_rar_seek_data,
661 archive_read_format_rar_cleanup,
662 archive_read_support_format_rar_capabilities,
663 archive_read_format_rar_has_encrypted_entries);
671 archive_read_support_format_rar_capabilities(struct archive_read * a)
673 (void)a; /* UNUSED */
674 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
675 | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
679 archive_read_format_rar_has_encrypted_entries(struct archive_read *_a)
681 if (_a && _a->format) {
682 struct rar * rar = (struct rar *)_a->format->data;
684 return rar->has_encrypted_entries;
687 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
692 archive_read_format_rar_bid(struct archive_read *a, int best_bid)
696 /* If there's already a bid > 30, we'll never win. */
700 if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
703 if (memcmp(p, RAR_SIGNATURE, 7) == 0)
706 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
707 /* This is a PE file */
708 ssize_t offset = 0x10000;
709 ssize_t window = 4096;
711 while (offset + window <= (1024 * 128)) {
712 const char *buff = __archive_read_ahead(a, offset + window, &bytes_avail);
714 /* Remaining bytes are less than window. */
721 while (p + 7 < buff + bytes_avail) {
722 if (memcmp(p, RAR_SIGNATURE, 7) == 0)
733 skip_sfx(struct archive_read *a)
738 ssize_t bytes, window;
742 while (total + window <= (1024 * 128)) {
743 h = __archive_read_ahead(a, window, &bytes);
745 /* Remaining bytes are less than window. */
757 * Scan ahead until we find something that looks
758 * like the RAR header.
761 if (memcmp(p, RAR_SIGNATURE, 7) == 0) {
762 skip = p - (const char *)h;
763 __archive_read_consume(a, skip);
768 skip = p - (const char *)h;
769 __archive_read_consume(a, skip);
773 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
774 "Couldn't find out RAR header");
775 return (ARCHIVE_FATAL);
779 archive_read_format_rar_options(struct archive_read *a,
780 const char *key, const char *val)
783 int ret = ARCHIVE_FAILED;
785 rar = (struct rar *)(a->format->data);
786 if (strcmp(key, "hdrcharset") == 0) {
787 if (val == NULL || val[0] == 0)
788 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
789 "rar: hdrcharset option needs a character-set name");
792 archive_string_conversion_from_charset(
793 &a->archive, val, 0);
794 if (rar->opt_sconv != NULL)
802 /* Note: The "warn" return is just to inform the options
803 * supervisor that we didn't handle it. It will generate
804 * a suitable error if no one used this option. */
805 return (ARCHIVE_WARN);
809 archive_read_format_rar_read_header(struct archive_read *a,
810 struct archive_entry *entry)
819 unsigned long crc32_expected;
821 a->archive.archive_format = ARCHIVE_FORMAT_RAR;
822 if (a->archive.archive_format_name == NULL)
823 a->archive.archive_format_name = "RAR";
825 rar = (struct rar *)(a->format->data);
828 * It should be sufficient to call archive_read_next_header() for
829 * a reader to determine if an entry is encrypted or not. If the
830 * encryption of an entry is only detectable when calling
831 * archive_read_data(), so be it. We'll do the same check there
834 if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
835 rar->has_encrypted_entries = 0;
838 /* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
841 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
842 return (ARCHIVE_EOF);
845 if (rar->found_first_header == 0 &&
846 ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0)) {
847 /* This is an executable ? Must be self-extracting... */
849 if (ret < ARCHIVE_WARN)
852 rar->found_first_header = 1;
856 unsigned long crc32_val;
858 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
859 return (ARCHIVE_FATAL);
866 if (memcmp(p, RAR_SIGNATURE, 7) != 0) {
867 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
868 "Invalid marker header");
869 return (ARCHIVE_FATAL);
871 __archive_read_consume(a, 7);
875 rar->main_flags = archive_le16dec(p + 3);
876 skip = archive_le16dec(p + 5);
877 if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
878 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
879 "Invalid header size");
880 return (ARCHIVE_FATAL);
882 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
883 return (ARCHIVE_FATAL);
885 memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
886 memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
887 sizeof(rar->reserved2));
888 if (rar->main_flags & MHD_ENCRYPTVER) {
889 if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)+1) {
890 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
891 "Invalid header size");
892 return (ARCHIVE_FATAL);
894 rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
895 sizeof(rar->reserved2));
898 /* Main header is password encrypted, so we cannot read any
899 file names or any other info about files from the header. */
900 if (rar->main_flags & MHD_PASSWORD)
902 archive_entry_set_is_metadata_encrypted(entry, 1);
903 archive_entry_set_is_data_encrypted(entry, 1);
904 rar->has_encrypted_entries = 1;
905 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
906 "RAR encryption support unavailable.");
907 return (ARCHIVE_FATAL);
910 crc32_val = crc32(0, (const unsigned char *)p + 2, (unsigned)skip - 2);
911 if ((crc32_val & 0xffff) != archive_le16dec(p)) {
912 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
914 return (ARCHIVE_FATAL);
916 __archive_read_consume(a, skip);
920 return read_header(a, entry, head_type);
928 flags = archive_le16dec(p + 3);
929 skip = archive_le16dec(p + 5);
931 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
932 "Invalid header size too small");
933 return (ARCHIVE_FATAL);
935 if (flags & HD_ADD_SIZE_PRESENT)
938 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
939 "Invalid header size too small");
940 return (ARCHIVE_FATAL);
942 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
943 return (ARCHIVE_FATAL);
945 skip += archive_le32dec(p + 7);
948 /* Skip over the 2-byte CRC at the beginning of the header. */
949 crc32_expected = archive_le16dec(p);
950 __archive_read_consume(a, 2);
953 /* Skim the entire header and compute the CRC. */
956 size_t to_read = skip;
958 if (to_read > 32 * 1024) {
961 if ((h = __archive_read_ahead(a, to_read, &did_read)) == NULL) {
962 return (ARCHIVE_FATAL);
965 crc32_val = crc32(crc32_val, (const unsigned char *)p, (unsigned)did_read);
966 __archive_read_consume(a, did_read);
969 if ((crc32_val & 0xffff) != crc32_expected) {
970 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
972 return (ARCHIVE_FATAL);
974 if (head_type == ENDARC_HEAD)
975 return (ARCHIVE_EOF);
979 if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
984 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
986 return (ARCHIVE_FATAL);
992 archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
993 size_t *size, int64_t *offset)
995 struct rar *rar = (struct rar *)(a->format->data);
998 if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
999 rar->has_encrypted_entries = 0;
1002 if (rar->bytes_unconsumed > 0) {
1003 /* Consume as much as the decompressor actually used. */
1004 __archive_read_consume(a, rar->bytes_unconsumed);
1005 rar->bytes_unconsumed = 0;
1009 if (rar->entry_eof || rar->offset_seek >= rar->unp_size) {
1011 *offset = rar->offset;
1012 if (*offset < rar->unp_size)
1013 *offset = rar->unp_size;
1014 return (ARCHIVE_EOF);
1017 switch (rar->compression_method)
1019 case COMPRESS_METHOD_STORE:
1020 ret = read_data_stored(a, buff, size, offset);
1023 case COMPRESS_METHOD_FASTEST:
1024 case COMPRESS_METHOD_FAST:
1025 case COMPRESS_METHOD_NORMAL:
1026 case COMPRESS_METHOD_GOOD:
1027 case COMPRESS_METHOD_BEST:
1028 ret = read_data_compressed(a, buff, size, offset);
1029 if (ret != ARCHIVE_OK && ret != ARCHIVE_WARN) {
1030 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1031 rar->start_new_table = 1;
1032 rar->ppmd_valid = 0;
1037 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1038 "Unsupported compression method for RAR file.");
1039 ret = ARCHIVE_FATAL;
1046 archive_read_format_rar_read_data_skip(struct archive_read *a)
1049 int64_t bytes_skipped;
1052 rar = (struct rar *)(a->format->data);
1054 if (rar->bytes_unconsumed > 0) {
1055 /* Consume as much as the decompressor actually used. */
1056 __archive_read_consume(a, rar->bytes_unconsumed);
1057 rar->bytes_unconsumed = 0;
1060 if (rar->bytes_remaining > 0) {
1061 bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
1062 if (bytes_skipped < 0)
1063 return (ARCHIVE_FATAL);
1066 /* Compressed data to skip must be read from each header in a multivolume
1069 if (rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER)
1071 ret = archive_read_format_rar_read_header(a, a->entry);
1072 if (ret == (ARCHIVE_EOF))
1073 ret = archive_read_format_rar_read_header(a, a->entry);
1074 if (ret != (ARCHIVE_OK))
1076 return archive_read_format_rar_read_data_skip(a);
1079 return (ARCHIVE_OK);
1083 archive_read_format_rar_seek_data(struct archive_read *a, int64_t offset,
1086 int64_t client_offset, ret;
1088 struct rar *rar = (struct rar *)(a->format->data);
1090 if (rar->compression_method == COMPRESS_METHOD_STORE)
1092 /* Modify the offset for use with SEEK_SET */
1096 client_offset = rar->offset_seek;
1099 client_offset = rar->unp_size;
1105 client_offset += offset;
1106 if (client_offset < 0)
1108 /* Can't seek past beginning of data block */
1111 else if (client_offset > rar->unp_size)
1114 * Set the returned offset but only seek to the end of
1117 rar->offset_seek = client_offset;
1118 client_offset = rar->unp_size;
1121 client_offset += rar->dbo[0].start_offset;
1123 while (i < rar->cursor)
1126 client_offset += rar->dbo[i].start_offset - rar->dbo[i-1].end_offset;
1128 if (rar->main_flags & MHD_VOLUME)
1130 /* Find the appropriate offset among the multivolume archive */
1133 if (client_offset < rar->dbo[rar->cursor].start_offset &&
1134 rar->file_flags & FHD_SPLIT_BEFORE)
1136 /* Search backwards for the correct data block */
1137 if (rar->cursor == 0)
1139 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1140 "Attempt to seek past beginning of RAR data block");
1141 return (ARCHIVE_FAILED);
1144 client_offset -= rar->dbo[rar->cursor+1].start_offset -
1145 rar->dbo[rar->cursor].end_offset;
1146 if (client_offset < rar->dbo[rar->cursor].start_offset)
1148 ret = __archive_read_seek(a, rar->dbo[rar->cursor].start_offset -
1149 rar->dbo[rar->cursor].header_size, SEEK_SET);
1150 if (ret < (ARCHIVE_OK))
1152 ret = archive_read_format_rar_read_header(a, a->entry);
1153 if (ret != (ARCHIVE_OK))
1155 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1156 "Error during seek of RAR file");
1157 return (ARCHIVE_FAILED);
1162 else if (client_offset > rar->dbo[rar->cursor].end_offset &&
1163 rar->file_flags & FHD_SPLIT_AFTER)
1165 /* Search forward for the correct data block */
1167 if (rar->cursor < rar->nodes &&
1168 client_offset > rar->dbo[rar->cursor].end_offset)
1170 client_offset += rar->dbo[rar->cursor].start_offset -
1171 rar->dbo[rar->cursor-1].end_offset;
1175 ret = __archive_read_seek(a, rar->dbo[rar->cursor].end_offset,
1177 if (ret < (ARCHIVE_OK))
1179 ret = archive_read_format_rar_read_header(a, a->entry);
1180 if (ret == (ARCHIVE_EOF))
1182 rar->has_endarc_header = 1;
1183 ret = archive_read_format_rar_read_header(a, a->entry);
1185 if (ret != (ARCHIVE_OK))
1187 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1188 "Error during seek of RAR file");
1189 return (ARCHIVE_FAILED);
1191 client_offset += rar->dbo[rar->cursor].start_offset -
1192 rar->dbo[rar->cursor-1].end_offset;
1199 ret = __archive_read_seek(a, client_offset, SEEK_SET);
1200 if (ret < (ARCHIVE_OK))
1202 rar->bytes_remaining = rar->dbo[rar->cursor].end_offset - ret;
1207 ret -= rar->dbo[i+1].start_offset - rar->dbo[i].end_offset;
1209 ret -= rar->dbo[0].start_offset;
1211 /* Always restart reading the file after a seek */
1212 __archive_reset_read_data(&a->archive);
1214 rar->bytes_unconsumed = 0;
1218 * If a seek past the end of file was requested, return the requested
1221 if (ret == rar->unp_size && rar->offset_seek > rar->unp_size)
1222 return rar->offset_seek;
1224 /* Return the new offset */
1225 rar->offset_seek = ret;
1226 return rar->offset_seek;
1230 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1231 "Seeking of compressed RAR files is unsupported");
1233 return (ARCHIVE_FAILED);
1237 archive_read_format_rar_cleanup(struct archive_read *a)
1241 rar = (struct rar *)(a->format->data);
1243 free(rar->filename);
1244 free(rar->filename_save);
1246 free(rar->unp_buffer);
1247 free(rar->lzss.window);
1248 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1250 (a->format->data) = NULL;
1251 return (ARCHIVE_OK);
1255 read_header(struct archive_read *a, struct archive_entry *entry,
1259 const char *p, *endp;
1261 struct rar_header rar_header;
1262 struct rar_file_header file_header;
1263 int64_t header_size;
1264 unsigned filename_size, end;
1267 char packed_size[8];
1270 struct archive_string_conv *sconv, *fn_sconv;
1271 unsigned long crc32_val;
1272 int ret = (ARCHIVE_OK), ret2;
1274 rar = (struct rar *)(a->format->data);
1276 /* Setup a string conversion object for non-rar-unicode filenames. */
1277 sconv = rar->opt_sconv;
1278 if (sconv == NULL) {
1279 if (!rar->init_default_conversion) {
1280 rar->sconv_default =
1281 archive_string_default_conversion_for_read(
1283 rar->init_default_conversion = 1;
1285 sconv = rar->sconv_default;
1289 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
1290 return (ARCHIVE_FATAL);
1292 memcpy(&rar_header, p, sizeof(rar_header));
1293 rar->file_flags = archive_le16dec(rar_header.flags);
1294 header_size = archive_le16dec(rar_header.size);
1295 if (header_size < (int64_t)sizeof(file_header) + 7) {
1296 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1297 "Invalid header size");
1298 return (ARCHIVE_FATAL);
1300 crc32_val = crc32(0, (const unsigned char *)p + 2, 7 - 2);
1301 __archive_read_consume(a, 7);
1303 if (!(rar->file_flags & FHD_SOLID))
1305 rar->compression_method = 0;
1306 rar->packed_size = 0;
1313 memset(&rar->salt, 0, sizeof(rar->salt));
1325 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1326 "RAR solid archive support unavailable.");
1327 return (ARCHIVE_FATAL);
1330 if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1331 return (ARCHIVE_FATAL);
1333 /* File Header CRC check. */
1334 crc32_val = crc32(crc32_val, h, (unsigned)(header_size - 7));
1335 if ((crc32_val & 0xffff) != archive_le16dec(rar_header.crc)) {
1336 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1337 "Header CRC error");
1338 return (ARCHIVE_FATAL);
1340 /* If no CRC error, Go on parsing File Header. */
1342 endp = p + header_size - 7;
1343 memcpy(&file_header, p, sizeof(file_header));
1344 p += sizeof(file_header);
1346 rar->compression_method = file_header.method;
1348 ttime = archive_le32dec(file_header.file_time);
1349 rar->mtime = get_time(ttime);
1351 rar->file_crc = archive_le32dec(file_header.file_crc);
1353 if (rar->file_flags & FHD_PASSWORD)
1355 archive_entry_set_is_data_encrypted(entry, 1);
1356 rar->has_encrypted_entries = 1;
1357 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1358 "RAR encryption support unavailable.");
1359 /* Since it is only the data part itself that is encrypted we can at least
1360 extract information about the currently processed entry and don't need
1361 to return ARCHIVE_FATAL here. */
1362 /*return (ARCHIVE_FATAL);*/
1365 if (rar->file_flags & FHD_LARGE)
1367 memcpy(packed_size, file_header.pack_size, 4);
1368 memcpy(packed_size + 4, p, 4); /* High pack size */
1370 memcpy(unp_size, file_header.unp_size, 4);
1371 memcpy(unp_size + 4, p, 4); /* High unpack size */
1373 rar->packed_size = archive_le64dec(&packed_size);
1374 rar->unp_size = archive_le64dec(&unp_size);
1378 rar->packed_size = archive_le32dec(file_header.pack_size);
1379 rar->unp_size = archive_le32dec(file_header.unp_size);
1382 if (rar->packed_size < 0 || rar->unp_size < 0)
1384 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1385 "Invalid sizes specified.");
1386 return (ARCHIVE_FATAL);
1389 rar->bytes_remaining = rar->packed_size;
1391 /* TODO: RARv3 subblocks contain comments. For now the complete block is
1392 * consumed at the end.
1394 if (head_type == NEWSUB_HEAD) {
1395 size_t distance = p - (const char *)h;
1396 header_size += rar->packed_size;
1397 /* Make sure we have the extended data. */
1398 if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1399 return (ARCHIVE_FATAL);
1401 endp = p + header_size - 7;
1405 filename_size = archive_le16dec(file_header.name_size);
1406 if (p + filename_size > endp) {
1407 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1408 "Invalid filename size");
1409 return (ARCHIVE_FATAL);
1411 if (rar->filename_allocated < filename_size * 2 + 2) {
1413 size_t newsize = filename_size * 2 + 2;
1414 newptr = realloc(rar->filename, newsize);
1415 if (newptr == NULL) {
1416 archive_set_error(&a->archive, ENOMEM,
1417 "Couldn't allocate memory.");
1418 return (ARCHIVE_FATAL);
1420 rar->filename = newptr;
1421 rar->filename_allocated = newsize;
1423 filename = rar->filename;
1424 memcpy(filename, p, filename_size);
1425 filename[filename_size] = '\0';
1426 if (rar->file_flags & FHD_UNICODE)
1428 if (filename_size != strlen(filename))
1430 unsigned char highbyte, flagbits, flagbyte;
1431 unsigned fn_end, offset;
1433 end = filename_size;
1434 fn_end = filename_size * 2;
1436 offset = (unsigned)strlen(filename) + 1;
1437 highbyte = *(p + offset++);
1440 while (offset < end && filename_size < fn_end)
1444 flagbyte = *(p + offset++);
1449 switch((flagbyte >> flagbits) & 3)
1452 filename[filename_size++] = '\0';
1453 filename[filename_size++] = *(p + offset++);
1456 filename[filename_size++] = highbyte;
1457 filename[filename_size++] = *(p + offset++);
1460 filename[filename_size++] = *(p + offset + 1);
1461 filename[filename_size++] = *(p + offset);
1467 uint8_t length = *(p + offset++);
1469 if (length & 0x80) {
1470 extra = *(p + offset++);
1471 high = (char)highbyte;
1474 length = (length & 0x7f) + 2;
1475 while (length && filename_size < fn_end) {
1476 unsigned cp = filename_size >> 1;
1477 filename[filename_size++] = high;
1478 filename[filename_size++] = p[cp] + extra;
1485 if (filename_size > fn_end) {
1486 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1487 "Invalid filename");
1488 return (ARCHIVE_FATAL);
1490 filename[filename_size++] = '\0';
1492 * Do not increment filename_size here as the computations below
1493 * add the space for the terminating NUL explicitly.
1495 filename[filename_size] = '\0';
1497 /* Decoded unicode form is UTF-16BE, so we have to update a string
1498 * conversion object for it. */
1499 if (rar->sconv_utf16be == NULL) {
1500 rar->sconv_utf16be = archive_string_conversion_from_charset(
1501 &a->archive, "UTF-16BE", 1);
1502 if (rar->sconv_utf16be == NULL)
1503 return (ARCHIVE_FATAL);
1505 fn_sconv = rar->sconv_utf16be;
1508 while (memcmp(strp, "\x00\x00", 2))
1510 if (!memcmp(strp, "\x00\\", 2))
1517 * If FHD_UNICODE is set but no unicode data, this file name form
1518 * is UTF-8, so we have to update a string conversion object for
1521 if (rar->sconv_utf8 == NULL) {
1522 rar->sconv_utf8 = archive_string_conversion_from_charset(
1523 &a->archive, "UTF-8", 1);
1524 if (rar->sconv_utf8 == NULL)
1525 return (ARCHIVE_FATAL);
1527 fn_sconv = rar->sconv_utf8;
1528 while ((strp = strchr(filename, '\\')) != NULL)
1536 while ((strp = strchr(filename, '\\')) != NULL)
1541 /* Split file in multivolume RAR. No more need to process header. */
1542 if (rar->filename_save &&
1543 filename_size == rar->filename_save_size &&
1544 !memcmp(rar->filename, rar->filename_save, filename_size + 1))
1546 __archive_read_consume(a, header_size - 7);
1548 if (rar->cursor >= rar->nodes)
1552 realloc(rar->dbo, sizeof(*rar->dbo) * rar->nodes)) == NULL)
1554 archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1555 return (ARCHIVE_FATAL);
1557 rar->dbo[rar->cursor].header_size = header_size;
1558 rar->dbo[rar->cursor].start_offset = -1;
1559 rar->dbo[rar->cursor].end_offset = -1;
1561 if (rar->dbo[rar->cursor].start_offset < 0)
1563 rar->dbo[rar->cursor].start_offset = a->filter->position;
1564 rar->dbo[rar->cursor].end_offset = rar->dbo[rar->cursor].start_offset +
1570 rar->filename_save = (char*)realloc(rar->filename_save,
1572 memcpy(rar->filename_save, rar->filename, filename_size + 1);
1573 rar->filename_save_size = filename_size;
1575 /* Set info for seeking */
1577 if ((rar->dbo = calloc(1, sizeof(*rar->dbo))) == NULL)
1579 archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory.");
1580 return (ARCHIVE_FATAL);
1582 rar->dbo[0].header_size = header_size;
1583 rar->dbo[0].start_offset = -1;
1584 rar->dbo[0].end_offset = -1;
1588 if (rar->file_flags & FHD_SALT)
1591 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1592 "Invalid header size");
1593 return (ARCHIVE_FATAL);
1595 memcpy(rar->salt, p, 8);
1599 if (rar->file_flags & FHD_EXTTIME) {
1600 if (read_exttime(p, rar, endp) < 0) {
1601 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1602 "Invalid header size");
1603 return (ARCHIVE_FATAL);
1607 __archive_read_consume(a, header_size - 7);
1608 rar->dbo[0].start_offset = a->filter->position;
1609 rar->dbo[0].end_offset = rar->dbo[0].start_offset + rar->packed_size;
1611 switch(file_header.host_os)
1616 rar->mode = archive_le32dec(file_header.file_attr);
1617 if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
1618 rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
1620 rar->mode = AE_IFREG;
1621 rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1627 rar->mode = archive_le32dec(file_header.file_attr);
1631 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1632 "Unknown file attributes from RAR file's host OS");
1633 return (ARCHIVE_FATAL);
1636 rar->bytes_uncopied = rar->bytes_unconsumed = 0;
1637 rar->lzss.position = rar->offset = 0;
1638 rar->offset_seek = 0;
1639 rar->dictionary_size = 0;
1640 rar->offset_outgoing = 0;
1641 rar->br.cache_avail = 0;
1642 rar->br.avail_in = 0;
1643 rar->crc_calculated = 0;
1646 rar->is_ppmd_block = 0;
1647 rar->start_new_table = 1;
1648 free(rar->unp_buffer);
1649 rar->unp_buffer = NULL;
1650 rar->unp_offset = 0;
1651 rar->unp_buffer_size = UNP_BUFFER_SIZE;
1652 memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
1653 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1654 rar->ppmd_valid = rar->ppmd_eod = 0;
1656 /* Don't set any archive entries for non-file header types */
1657 if (head_type == NEWSUB_HEAD)
1660 archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
1661 archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
1662 archive_entry_set_atime(entry, rar->atime, rar->ansec);
1663 archive_entry_set_size(entry, rar->unp_size);
1664 archive_entry_set_mode(entry, rar->mode);
1666 if (archive_entry_copy_pathname_l(entry, filename, filename_size, fn_sconv))
1668 if (errno == ENOMEM)
1670 archive_set_error(&a->archive, ENOMEM,
1671 "Can't allocate memory for Pathname");
1672 return (ARCHIVE_FATAL);
1674 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1675 "Pathname cannot be converted from %s to current locale.",
1676 archive_string_conversion_charset_name(fn_sconv));
1677 ret = (ARCHIVE_WARN);
1680 if (((rar->mode) & AE_IFMT) == AE_IFLNK)
1682 /* Make sure a symbolic-link file does not have its body. */
1683 rar->bytes_remaining = 0;
1684 archive_entry_set_size(entry, 0);
1686 /* Read a symbolic-link name. */
1687 if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
1693 if (rar->bytes_remaining == 0)
1703 tm.tm_sec = 2 * (ttime & 0x1f);
1704 tm.tm_min = (ttime >> 5) & 0x3f;
1705 tm.tm_hour = (ttime >> 11) & 0x1f;
1706 tm.tm_mday = (ttime >> 16) & 0x1f;
1707 tm.tm_mon = ((ttime >> 21) & 0x0f) - 1;
1708 tm.tm_year = ((ttime >> 25) & 0x7f) + 80;
1714 read_exttime(const char *p, struct rar *rar, const char *endp)
1716 unsigned rmode, flags, rem, j, count;
1721 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
1724 #if defined(HAVE__LOCALTIME64_S)
1731 flags = archive_le16dec(p);
1734 for (i = 3; i >= 0; i--)
1739 rmode = flags >> i * 4;
1746 ttime = archive_le32dec(p);
1747 t = get_time(ttime);
1752 if (p + count > endp)
1754 for (j = 0; j < count; j++)
1756 rem = (((unsigned)(unsigned char)*p) << 16) | (rem >> 8);
1759 #if defined(HAVE_LOCALTIME_R)
1760 tm = localtime_r(&t, &tmbuf);
1761 #elif defined(HAVE__LOCALTIME64_S)
1763 terr = _localtime64_s(&tmbuf, &tmptime);
1771 nsec = tm->tm_sec + rem / NS_UNIT;
1795 rar->arcnsec = nsec;
1803 read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
1804 struct archive_string_conv *sconv)
1809 int ret = (ARCHIVE_OK);
1811 rar = (struct rar *)(a->format->data);
1812 if ((h = rar_read_ahead(a, (size_t)rar->packed_size, NULL)) == NULL)
1813 return (ARCHIVE_FATAL);
1816 if (archive_entry_copy_symlink_l(entry,
1817 p, (size_t)rar->packed_size, sconv))
1819 if (errno == ENOMEM)
1821 archive_set_error(&a->archive, ENOMEM,
1822 "Can't allocate memory for link");
1823 return (ARCHIVE_FATAL);
1825 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1826 "link cannot be converted from %s to current locale.",
1827 archive_string_conversion_charset_name(sconv));
1828 ret = (ARCHIVE_WARN);
1830 __archive_read_consume(a, rar->packed_size);
1835 read_data_stored(struct archive_read *a, const void **buff, size_t *size,
1839 ssize_t bytes_avail;
1841 rar = (struct rar *)(a->format->data);
1842 if (rar->bytes_remaining == 0 &&
1843 !(rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER))
1847 *offset = rar->offset;
1848 if (rar->file_crc != rar->crc_calculated) {
1849 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1851 return (ARCHIVE_FATAL);
1854 return (ARCHIVE_EOF);
1857 *buff = rar_read_ahead(a, 1, &bytes_avail);
1858 if (bytes_avail <= 0)
1860 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1861 "Truncated RAR file data");
1862 return (ARCHIVE_FATAL);
1865 *size = bytes_avail;
1866 *offset = rar->offset;
1867 rar->offset += bytes_avail;
1868 rar->offset_seek += bytes_avail;
1869 rar->bytes_remaining -= bytes_avail;
1870 rar->bytes_unconsumed = bytes_avail;
1871 /* Calculate File CRC. */
1872 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1873 (unsigned)bytes_avail);
1874 return (ARCHIVE_OK);
1878 read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
1882 int64_t start, end, actualend;
1884 int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
1886 rar = (struct rar *)(a->format->data);
1890 return (ARCHIVE_FATAL);
1891 if (rar->ppmd_eod ||
1892 (rar->dictionary_size && rar->offset >= rar->unp_size))
1894 if (rar->unp_offset > 0) {
1896 * We have unprocessed extracted data. write it out.
1898 *buff = rar->unp_buffer;
1899 *size = rar->unp_offset;
1900 *offset = rar->offset_outgoing;
1901 rar->offset_outgoing += *size;
1902 /* Calculate File CRC. */
1903 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1905 rar->unp_offset = 0;
1906 return (ARCHIVE_OK);
1910 *offset = rar->offset;
1911 if (rar->file_crc != rar->crc_calculated) {
1912 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1914 return (ARCHIVE_FATAL);
1917 return (ARCHIVE_EOF);
1920 if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
1922 if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
1923 bs = rar->unp_buffer_size - rar->unp_offset;
1925 bs = (size_t)rar->bytes_uncopied;
1926 ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
1927 if (ret != ARCHIVE_OK)
1930 rar->bytes_uncopied -= bs;
1931 if (*buff != NULL) {
1932 rar->unp_offset = 0;
1933 *size = rar->unp_buffer_size;
1934 *offset = rar->offset_outgoing;
1935 rar->offset_outgoing += *size;
1936 /* Calculate File CRC. */
1937 rar->crc_calculated = crc32(rar->crc_calculated, *buff,
1944 if (!rar->br.next_in &&
1945 (ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
1947 if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
1950 if (rar->is_ppmd_block)
1952 if ((sym = __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);
1959 if(sym != rar->ppmd_escape)
1961 lzss_emit_literal(rar, sym);
1962 rar->bytes_uncopied++;
1966 if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1967 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1969 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1971 return (ARCHIVE_FATAL);
1977 rar->start_new_table = 1;
1978 return read_data_compressed(a, buff, size, offset);
1981 rar->ppmd_eod = 1;/* End Of ppmd Data. */
1985 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1986 "Parsing filters is unsupported.");
1987 return (ARCHIVE_FAILED);
1991 for (i = 2; i >= 0; i--)
1993 if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1994 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1996 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1998 return (ARCHIVE_FATAL);
2000 lzss_offset |= code << (i * 8);
2002 if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2003 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2005 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2007 return (ARCHIVE_FATAL);
2009 lzss_emit_match(rar, lzss_offset + 2, length + 32);
2010 rar->bytes_uncopied += length + 32;
2014 if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2015 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2017 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2019 return (ARCHIVE_FATAL);
2021 lzss_emit_match(rar, 1, length + 4);
2022 rar->bytes_uncopied += length + 4;
2026 lzss_emit_literal(rar, sym);
2027 rar->bytes_uncopied++;
2033 start = rar->offset;
2034 end = start + rar->dictionary_size;
2035 rar->filterstart = INT64_MAX;
2037 if ((actualend = expand(a, end)) < 0)
2038 return ((int)actualend);
2040 rar->bytes_uncopied = actualend - start;
2041 if (rar->bytes_uncopied == 0) {
2042 /* Broken RAR files cause this case.
2043 * NOTE: If this case were possible on a normal RAR file
2044 * we would find out where it was actually bad and
2045 * what we would do to solve it. */
2046 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2047 "Internal error extracting RAR file");
2048 return (ARCHIVE_FATAL);
2051 if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
2052 bs = rar->unp_buffer_size - rar->unp_offset;
2054 bs = (size_t)rar->bytes_uncopied;
2055 ret = copy_from_lzss_window(a, buff, rar->offset, (int)bs);
2056 if (ret != ARCHIVE_OK)
2059 rar->bytes_uncopied -= bs;
2061 * If *buff is NULL, it means unp_buffer is not full.
2062 * So we have to continue extracting a RAR file.
2064 } while (*buff == NULL);
2066 rar->unp_offset = 0;
2067 *size = rar->unp_buffer_size;
2068 *offset = rar->offset_outgoing;
2069 rar->offset_outgoing += *size;
2070 /* Calculate File CRC. */
2071 rar->crc_calculated = crc32(rar->crc_calculated, *buff, (unsigned)*size);
2076 parse_codes(struct archive_read *a)
2078 int i, j, val, n, r;
2079 unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
2080 unsigned int maxorder;
2081 struct huffman_code precode;
2082 struct rar *rar = (struct rar *)(a->format->data);
2083 struct rar_br *br = &(rar->br);
2087 /* Skip to the next byte */
2088 rar_br_consume_unalined_bits(br);
2090 /* PPMd block flag */
2091 if (!rar_br_read_ahead(a, br, 1))
2092 goto truncated_data;
2093 if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
2095 rar_br_consume(br, 1);
2096 if (!rar_br_read_ahead(a, br, 7))
2097 goto truncated_data;
2098 ppmd_flags = rar_br_bits(br, 7);
2099 rar_br_consume(br, 7);
2101 /* Memory is allocated in MB */
2102 if (ppmd_flags & 0x20)
2104 if (!rar_br_read_ahead(a, br, 8))
2105 goto truncated_data;
2106 rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
2107 rar_br_consume(br, 8);
2110 if (ppmd_flags & 0x40)
2112 if (!rar_br_read_ahead(a, br, 8))
2113 goto truncated_data;
2114 rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
2115 rar_br_consume(br, 8);
2118 rar->ppmd_escape = 2;
2120 if (ppmd_flags & 0x20)
2122 maxorder = (ppmd_flags & 0x1F) + 1;
2124 maxorder = 16 + (maxorder - 16) * 3;
2128 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2129 "Truncated RAR file data");
2130 return (ARCHIVE_FATAL);
2133 /* Make sure ppmd7_contest is freed before Ppmd7_Construct
2134 * because reading a broken file cause this abnormal sequence. */
2135 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
2138 rar->bytein.Read = &ppmd_read;
2139 __archive_ppmd7_functions.PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
2140 rar->range_dec.Stream = &rar->bytein;
2141 __archive_ppmd7_functions.Ppmd7_Construct(&rar->ppmd7_context);
2143 if (rar->dictionary_size == 0) {
2144 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2145 "Invalid zero dictionary size");
2146 return (ARCHIVE_FATAL);
2149 if (!__archive_ppmd7_functions.Ppmd7_Alloc(&rar->ppmd7_context,
2150 rar->dictionary_size))
2152 archive_set_error(&a->archive, ENOMEM,
2154 return (ARCHIVE_FATAL);
2156 if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2158 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2159 "Unable to initialize PPMd range decoder");
2160 return (ARCHIVE_FATAL);
2162 __archive_ppmd7_functions.Ppmd7_Init(&rar->ppmd7_context, maxorder);
2163 rar->ppmd_valid = 1;
2167 if (!rar->ppmd_valid) {
2168 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2169 "Invalid PPMd sequence");
2170 return (ARCHIVE_FATAL);
2172 if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2174 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2175 "Unable to initialize PPMd range decoder");
2176 return (ARCHIVE_FATAL);
2182 rar_br_consume(br, 1);
2184 /* Keep existing table flag */
2185 if (!rar_br_read_ahead(a, br, 1))
2186 goto truncated_data;
2187 if (!rar_br_bits(br, 1))
2188 memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
2189 rar_br_consume(br, 1);
2191 memset(&bitlengths, 0, sizeof(bitlengths));
2192 for (i = 0; i < MAX_SYMBOLS;)
2194 if (!rar_br_read_ahead(a, br, 4))
2195 goto truncated_data;
2196 bitlengths[i++] = rar_br_bits(br, 4);
2197 rar_br_consume(br, 4);
2198 if (bitlengths[i-1] == 0xF)
2200 if (!rar_br_read_ahead(a, br, 4))
2201 goto truncated_data;
2202 zerocount = rar_br_bits(br, 4);
2203 rar_br_consume(br, 4);
2207 for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
2208 bitlengths[i++] = 0;
2213 memset(&precode, 0, sizeof(precode));
2214 r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
2215 if (r != ARCHIVE_OK) {
2217 free(precode.table);
2221 for (i = 0; i < HUFFMAN_TABLE_SIZE;)
2223 if ((val = read_next_symbol(a, &precode)) < 0) {
2225 free(precode.table);
2226 return (ARCHIVE_FATAL);
2230 rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
2238 free(precode.table);
2239 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2240 "Internal error extracting RAR file.");
2241 return (ARCHIVE_FATAL);
2245 if (!rar_br_read_ahead(a, br, 3)) {
2247 free(precode.table);
2248 goto truncated_data;
2250 n = rar_br_bits(br, 3) + 3;
2251 rar_br_consume(br, 3);
2253 if (!rar_br_read_ahead(a, br, 7)) {
2255 free(precode.table);
2256 goto truncated_data;
2258 n = rar_br_bits(br, 7) + 11;
2259 rar_br_consume(br, 7);
2262 for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2264 rar->lengthtable[i] = rar->lengthtable[i-1];
2271 if (!rar_br_read_ahead(a, br, 3)) {
2273 free(precode.table);
2274 goto truncated_data;
2276 n = rar_br_bits(br, 3) + 3;
2277 rar_br_consume(br, 3);
2279 if (!rar_br_read_ahead(a, br, 7)) {
2281 free(precode.table);
2282 goto truncated_data;
2284 n = rar_br_bits(br, 7) + 11;
2285 rar_br_consume(br, 7);
2288 for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2289 rar->lengthtable[i++] = 0;
2293 free(precode.table);
2295 r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
2297 if (r != ARCHIVE_OK)
2299 r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
2300 OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2301 if (r != ARCHIVE_OK)
2303 r = create_code(a, &rar->lowoffsetcode,
2304 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
2305 LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2306 if (r != ARCHIVE_OK)
2308 r = create_code(a, &rar->lengthcode,
2309 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
2310 LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
2311 if (r != ARCHIVE_OK)
2315 if (!rar->dictionary_size || !rar->lzss.window)
2317 /* Seems as though dictionary sizes are not used. Even so, minimize
2318 * memory usage as much as possible.
2321 unsigned int new_size;
2323 if (rar->unp_size >= DICTIONARY_MAX_SIZE)
2324 new_size = DICTIONARY_MAX_SIZE;
2326 new_size = rar_fls((unsigned int)rar->unp_size) << 1;
2327 new_window = realloc(rar->lzss.window, new_size);
2328 if (new_window == NULL) {
2329 archive_set_error(&a->archive, ENOMEM,
2330 "Unable to allocate memory for uncompressed data.");
2331 return (ARCHIVE_FATAL);
2333 rar->lzss.window = (unsigned char *)new_window;
2334 rar->dictionary_size = new_size;
2335 memset(rar->lzss.window, 0, rar->dictionary_size);
2336 rar->lzss.mask = rar->dictionary_size - 1;
2339 rar->start_new_table = 0;
2340 return (ARCHIVE_OK);
2342 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2343 "Truncated RAR file data");
2345 return (ARCHIVE_FATAL);
2349 free_codes(struct archive_read *a)
2351 struct rar *rar = (struct rar *)(a->format->data);
2352 free(rar->maincode.tree);
2353 free(rar->offsetcode.tree);
2354 free(rar->lowoffsetcode.tree);
2355 free(rar->lengthcode.tree);
2356 free(rar->maincode.table);
2357 free(rar->offsetcode.table);
2358 free(rar->lowoffsetcode.table);
2359 free(rar->lengthcode.table);
2360 memset(&rar->maincode, 0, sizeof(rar->maincode));
2361 memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
2362 memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
2363 memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
2368 read_next_symbol(struct archive_read *a, struct huffman_code *code)
2372 int length, value, node;
2378 if (make_table(a, code) != (ARCHIVE_OK))
2382 rar = (struct rar *)(a->format->data);
2385 /* Look ahead (peek) at bits */
2386 if (!rar_br_read_ahead(a, br, code->tablesize)) {
2387 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2388 "Truncated RAR file data");
2392 bits = rar_br_bits(br, code->tablesize);
2394 length = code->table[bits].length;
2395 value = code->table[bits].value;
2399 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2400 "Invalid prefix code in bitstream");
2404 if (length <= code->tablesize)
2406 /* Skip length bits */
2407 rar_br_consume(br, length);
2411 /* Skip tablesize bits */
2412 rar_br_consume(br, code->tablesize);
2415 while (!(code->tree[node].branches[0] ==
2416 code->tree[node].branches[1]))
2418 if (!rar_br_read_ahead(a, br, 1)) {
2419 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2420 "Truncated RAR file data");
2424 bit = rar_br_bits(br, 1);
2425 rar_br_consume(br, 1);
2427 if (code->tree[node].branches[bit] < 0)
2429 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2430 "Invalid prefix code in bitstream");
2433 node = code->tree[node].branches[bit];
2436 return code->tree[node].branches[0];
2440 create_code(struct archive_read *a, struct huffman_code *code,
2441 unsigned char *lengths, int numsymbols, char maxlength)
2443 int i, j, codebits = 0, symbolsleft = numsymbols;
2445 code->numentries = 0;
2446 code->numallocatedentries = 0;
2447 if (new_node(code) < 0) {
2448 archive_set_error(&a->archive, ENOMEM,
2449 "Unable to allocate memory for node data.");
2450 return (ARCHIVE_FATAL);
2452 code->numentries = 1;
2453 code->minlength = INT_MAX;
2454 code->maxlength = INT_MIN;
2456 for(i = 1; i <= maxlength; i++)
2458 for(j = 0; j < numsymbols; j++)
2460 if (lengths[j] != i) continue;
2461 if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
2462 return (ARCHIVE_FATAL);
2464 if (--symbolsleft <= 0)
2467 if (symbolsleft <= 0)
2471 return (ARCHIVE_OK);
2475 add_value(struct archive_read *a, struct huffman_code *code, int value,
2476 int codebits, int length)
2478 int lastnode, bitpos, bit;
2479 /* int repeatpos, repeatnode, nextnode; */
2484 if(length > code->maxlength)
2485 code->maxlength = length;
2486 if(length < code->minlength)
2487 code->minlength = length;
2490 * Dead code, repeatpos was is -1
2493 if (repeatpos == 0 || (repeatpos >= 0
2494 && (((codebits >> (repeatpos - 1)) & 3) == 0
2495 || ((codebits >> (repeatpos - 1)) & 3) == 3)))
2497 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2498 "Invalid repeat position");
2499 return (ARCHIVE_FATAL);
2504 for (bitpos = length - 1; bitpos >= 0; bitpos--)
2506 bit = (codebits >> bitpos) & 1;
2508 /* Leaf node check */
2509 if (code->tree[lastnode].branches[0] ==
2510 code->tree[lastnode].branches[1])
2512 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2514 return (ARCHIVE_FATAL);
2518 * Dead code, repeatpos was -1, bitpos >=0
2520 if (bitpos == repeatpos)
2522 * Open branch check *
2523 if (!(code->tree[lastnode].branches[bit] < 0))
2525 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2526 "Invalid repeating code");
2527 return (ARCHIVE_FATAL);
2530 if ((repeatnode = new_node(code)) < 0) {
2531 archive_set_error(&a->archive, ENOMEM,
2532 "Unable to allocate memory for node data.");
2533 return (ARCHIVE_FATAL);
2535 if ((nextnode = new_node(code)) < 0) {
2536 archive_set_error(&a->archive, ENOMEM,
2537 "Unable to allocate memory for node data.");
2538 return (ARCHIVE_FATAL);
2542 code->tree[lastnode].branches[bit] = repeatnode;
2543 code->tree[repeatnode].branches[bit] = repeatnode;
2544 code->tree[repeatnode].branches[bit^1] = nextnode;
2545 lastnode = nextnode;
2547 bitpos++; * terminating bit already handled, skip it *
2552 /* Open branch check */
2553 if (code->tree[lastnode].branches[bit] < 0)
2555 if (new_node(code) < 0) {
2556 archive_set_error(&a->archive, ENOMEM,
2557 "Unable to allocate memory for node data.");
2558 return (ARCHIVE_FATAL);
2560 code->tree[lastnode].branches[bit] = code->numentries++;
2564 lastnode = code->tree[lastnode].branches[bit];
2568 if (!(code->tree[lastnode].branches[0] == -1
2569 && code->tree[lastnode].branches[1] == -2))
2571 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2573 return (ARCHIVE_FATAL);
2576 /* Set leaf value */
2577 code->tree[lastnode].branches[0] = value;
2578 code->tree[lastnode].branches[1] = value;
2580 return (ARCHIVE_OK);
2584 new_node(struct huffman_code *code)
2587 if (code->numallocatedentries == code->numentries) {
2588 int new_num_entries = 256;
2589 if (code->numentries > 0) {
2590 new_num_entries = code->numentries * 2;
2592 new_tree = realloc(code->tree, new_num_entries * sizeof(*code->tree));
2593 if (new_tree == NULL)
2595 code->tree = (struct huffman_tree_node *)new_tree;
2596 code->numallocatedentries = new_num_entries;
2598 code->tree[code->numentries].branches[0] = -1;
2599 code->tree[code->numentries].branches[1] = -2;
2604 make_table(struct archive_read *a, struct huffman_code *code)
2606 if (code->maxlength < code->minlength || code->maxlength > 10)
2607 code->tablesize = 10;
2609 code->tablesize = code->maxlength;
2612 (struct huffman_table_entry *)calloc(1, sizeof(*code->table)
2613 * ((size_t)1 << code->tablesize));
2615 return make_table_recurse(a, code, 0, code->table, 0, code->tablesize);
2619 make_table_recurse(struct archive_read *a, struct huffman_code *code, int node,
2620 struct huffman_table_entry *table, int depth,
2623 int currtablesize, i, ret = (ARCHIVE_OK);
2627 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2628 "Huffman tree was not created.");
2629 return (ARCHIVE_FATAL);
2631 if (node < 0 || node >= code->numentries)
2633 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2634 "Invalid location to Huffman tree specified.");
2635 return (ARCHIVE_FATAL);
2638 currtablesize = 1 << (maxdepth - depth);
2640 if (code->tree[node].branches[0] ==
2641 code->tree[node].branches[1])
2643 for(i = 0; i < currtablesize; i++)
2645 table[i].length = depth;
2646 table[i].value = code->tree[node].branches[0];
2650 * Dead code, node >= 0
2654 for(i = 0; i < currtablesize; i++)
2655 table[i].length = -1;
2660 if(depth == maxdepth)
2662 table[0].length = maxdepth + 1;
2663 table[0].value = node;
2667 ret |= make_table_recurse(a, code, code->tree[node].branches[0], table,
2668 depth + 1, maxdepth);
2669 ret |= make_table_recurse(a, code, code->tree[node].branches[1],
2670 table + currtablesize / 2, depth + 1, maxdepth);
2677 expand(struct archive_read *a, int64_t end)
2679 static const unsigned char lengthbases[] =
2680 { 0, 1, 2, 3, 4, 5, 6,
2681 7, 8, 10, 12, 14, 16, 20,
2682 24, 28, 32, 40, 48, 56, 64,
2683 80, 96, 112, 128, 160, 192, 224 };
2684 static const unsigned char lengthbits[] =
2685 { 0, 0, 0, 0, 0, 0, 0,
2686 0, 1, 1, 1, 1, 2, 2,
2687 2, 2, 3, 3, 3, 3, 4,
2688 4, 4, 4, 5, 5, 5, 5 };
2689 static const int lengthb_min = minimum(
2690 (int)(sizeof(lengthbases)/sizeof(lengthbases[0])),
2691 (int)(sizeof(lengthbits)/sizeof(lengthbits[0]))
2693 static const unsigned int offsetbases[] =
2695 8, 12, 16, 24, 32, 48,
2696 64, 96, 128, 192, 256, 384,
2697 512, 768, 1024, 1536, 2048, 3072,
2698 4096, 6144, 8192, 12288, 16384, 24576,
2699 32768, 49152, 65536, 98304, 131072, 196608,
2700 262144, 327680, 393216, 458752, 524288, 589824,
2701 655360, 720896, 786432, 851968, 917504, 983040,
2702 1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
2703 2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
2704 static const unsigned char offsetbits[] =
2705 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
2706 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
2707 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
2708 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2709 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
2710 static const int offsetb_min = minimum(
2711 (int)(sizeof(offsetbases)/sizeof(offsetbases[0])),
2712 (int)(sizeof(offsetbits)/sizeof(offsetbits[0]))
2714 static const unsigned char shortbases[] =
2715 { 0, 4, 8, 16, 32, 64, 128, 192 };
2716 static const unsigned char shortbits[] =
2717 { 2, 2, 3, 4, 5, 6, 6, 6 };
2719 int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
2720 unsigned char newfile;
2721 struct rar *rar = (struct rar *)(a->format->data);
2722 struct rar_br *br = &(rar->br);
2724 if (rar->filterstart < end)
2725 end = rar->filterstart;
2729 if (rar->output_last_match &&
2730 lzss_position(&rar->lzss) + rar->lastlength <= end)
2732 lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
2733 rar->output_last_match = 0;
2736 if(rar->is_ppmd_block || rar->output_last_match ||
2737 lzss_position(&rar->lzss) >= end)
2738 return lzss_position(&rar->lzss);
2740 if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
2741 return (ARCHIVE_FATAL);
2742 rar->output_last_match = 0;
2746 lzss_emit_literal(rar, symbol);
2749 else if (symbol == 256)
2751 if (!rar_br_read_ahead(a, br, 1))
2752 goto truncated_data;
2753 newfile = !rar_br_bits(br, 1);
2754 rar_br_consume(br, 1);
2758 rar->start_new_block = 1;
2759 if (!rar_br_read_ahead(a, br, 1))
2760 goto truncated_data;
2761 rar->start_new_table = rar_br_bits(br, 1);
2762 rar_br_consume(br, 1);
2763 return lzss_position(&rar->lzss);
2767 if (parse_codes(a) != ARCHIVE_OK)
2768 return (ARCHIVE_FATAL);
2772 else if(symbol==257)
2774 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2775 "Parsing filters is unsupported.");
2776 return (ARCHIVE_FAILED);
2778 else if(symbol==258)
2780 if(rar->lastlength == 0)
2783 offs = rar->lastoffset;
2784 len = rar->lastlength;
2786 else if (symbol <= 262)
2788 offsindex = symbol - 259;
2789 offs = rar->oldoffset[offsindex];
2791 if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
2793 if (lensymbol > lengthb_min)
2795 len = lengthbases[lensymbol] + 2;
2796 if (lengthbits[lensymbol] > 0) {
2797 if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
2798 goto truncated_data;
2799 len += rar_br_bits(br, lengthbits[lensymbol]);
2800 rar_br_consume(br, lengthbits[lensymbol]);
2803 for (i = offsindex; i > 0; i--)
2804 rar->oldoffset[i] = rar->oldoffset[i-1];
2805 rar->oldoffset[0] = offs;
2807 else if(symbol<=270)
2809 offs = shortbases[symbol-263] + 1;
2810 if(shortbits[symbol-263] > 0) {
2811 if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
2812 goto truncated_data;
2813 offs += rar_br_bits(br, shortbits[symbol-263]);
2814 rar_br_consume(br, shortbits[symbol-263]);
2819 for(i = 3; i > 0; i--)
2820 rar->oldoffset[i] = rar->oldoffset[i-1];
2821 rar->oldoffset[0] = offs;
2825 if (symbol-271 > lengthb_min)
2827 len = lengthbases[symbol-271]+3;
2828 if(lengthbits[symbol-271] > 0) {
2829 if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
2830 goto truncated_data;
2831 len += rar_br_bits(br, lengthbits[symbol-271]);
2832 rar_br_consume(br, lengthbits[symbol-271]);
2835 if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
2837 if (offssymbol > offsetb_min)
2839 offs = offsetbases[offssymbol]+1;
2840 if(offsetbits[offssymbol] > 0)
2844 if(offsetbits[offssymbol] > 4) {
2845 if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
2846 goto truncated_data;
2847 offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
2848 rar_br_consume(br, offsetbits[offssymbol] - 4);
2851 if(rar->numlowoffsetrepeats > 0)
2853 rar->numlowoffsetrepeats--;
2854 offs += rar->lastlowoffset;
2858 if ((lowoffsetsymbol =
2859 read_next_symbol(a, &rar->lowoffsetcode)) < 0)
2860 return (ARCHIVE_FATAL);
2861 if(lowoffsetsymbol == 16)
2863 rar->numlowoffsetrepeats = 15;
2864 offs += rar->lastlowoffset;
2868 offs += lowoffsetsymbol;
2869 rar->lastlowoffset = lowoffsetsymbol;
2874 if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
2875 goto truncated_data;
2876 offs += rar_br_bits(br, offsetbits[offssymbol]);
2877 rar_br_consume(br, offsetbits[offssymbol]);
2881 if (offs >= 0x40000)
2886 for(i = 3; i > 0; i--)
2887 rar->oldoffset[i] = rar->oldoffset[i-1];
2888 rar->oldoffset[0] = offs;
2891 rar->lastoffset = offs;
2892 rar->lastlength = len;
2893 rar->output_last_match = 1;
2896 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2897 "Truncated RAR file data");
2899 return (ARCHIVE_FATAL);
2901 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2902 "Bad RAR file data");
2903 return (ARCHIVE_FATAL);
2907 copy_from_lzss_window(struct archive_read *a, const void **buffer,
2908 int64_t startpos, int length)
2910 int windowoffs, firstpart;
2911 struct rar *rar = (struct rar *)(a->format->data);
2913 if (!rar->unp_buffer)
2915 if ((rar->unp_buffer = malloc(rar->unp_buffer_size)) == NULL)
2917 archive_set_error(&a->archive, ENOMEM,
2918 "Unable to allocate memory for uncompressed data.");
2919 return (ARCHIVE_FATAL);
2923 windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
2924 if(windowoffs + length <= lzss_size(&rar->lzss)) {
2925 memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
2927 } else if (length <= lzss_size(&rar->lzss)) {
2928 firstpart = lzss_size(&rar->lzss) - windowoffs;
2929 if (firstpart < 0) {
2930 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2931 "Bad RAR file data");
2932 return (ARCHIVE_FATAL);
2934 if (firstpart < length) {
2935 memcpy(&rar->unp_buffer[rar->unp_offset],
2936 &rar->lzss.window[windowoffs], firstpart);
2937 memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
2938 &rar->lzss.window[0], length - firstpart);
2940 memcpy(&rar->unp_buffer[rar->unp_offset],
2941 &rar->lzss.window[windowoffs], length);
2944 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2945 "Bad RAR file data");
2946 return (ARCHIVE_FATAL);
2948 rar->unp_offset += length;
2949 if (rar->unp_offset >= rar->unp_buffer_size)
2950 *buffer = rar->unp_buffer;
2953 return (ARCHIVE_OK);
2957 rar_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
2959 struct rar *rar = (struct rar *)(a->format->data);
2960 const void *h = __archive_read_ahead(a, min, avail);
2964 if (a->archive.read_data_is_posix_read && *avail > (ssize_t)a->archive.read_data_requested)
2965 *avail = a->archive.read_data_requested;
2966 if (*avail > rar->bytes_remaining)
2967 *avail = (ssize_t)rar->bytes_remaining;
2970 else if (*avail == 0 && rar->main_flags & MHD_VOLUME &&
2971 rar->file_flags & FHD_SPLIT_AFTER)
2973 ret = archive_read_format_rar_read_header(a, a->entry);
2974 if (ret == (ARCHIVE_EOF))
2976 rar->has_endarc_header = 1;
2977 ret = archive_read_format_rar_read_header(a, a->entry);
2979 if (ret != (ARCHIVE_OK))
2981 return rar_read_ahead(a, min, avail);