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 sytem-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;
192 struct huffman_table_entry *table;
197 unsigned char *window;
204 /* Entries from main RAR header */
206 unsigned long file_crc;
211 /* File header entries */
212 char compression_method;
220 size_t filename_allocated;
222 /* File header optional entries */
231 /* Fields to help with tracking decompression of files. */
232 int64_t bytes_unconsumed;
233 int64_t bytes_remaining;
234 int64_t bytes_uncopied;
236 int64_t offset_outgoing;
238 unsigned int unp_offset;
239 unsigned int unp_buffer_size;
240 unsigned char *unp_buffer;
241 unsigned int dictionary_size;
242 char start_new_block;
244 unsigned long crc_calculated;
245 int found_first_header;
248 struct huffman_code maincode;
249 struct huffman_code offsetcode;
250 struct huffman_code lowoffsetcode;
251 struct huffman_code lengthcode;
252 unsigned char lengthtable[HUFFMAN_TABLE_SIZE];
254 char output_last_match;
255 unsigned int lastlength;
256 unsigned int lastoffset;
257 unsigned int oldoffset[4];
258 unsigned int lastlowoffset;
259 unsigned int numlowoffsetrepeats;
261 char start_new_table;
263 /* PPMd Variant H members */
268 CPpmd7 ppmd7_context;
269 CPpmd7z_RangeDec range_dec;
273 * String conversion object.
275 int init_default_conversion;
276 struct archive_string_conv *sconv_default;
277 struct archive_string_conv *opt_sconv;
278 struct archive_string_conv *sconv_utf8;
279 struct archive_string_conv *sconv_utf16be;
285 #define CACHE_TYPE uint64_t
286 #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
288 CACHE_TYPE cache_buffer;
289 /* Indicates how many bits avail in cache_buffer. */
292 const unsigned char *next_in;
296 static int archive_read_format_rar_bid(struct archive_read *, int);
297 static int archive_read_format_rar_options(struct archive_read *,
298 const char *, const char *);
299 static int archive_read_format_rar_read_header(struct archive_read *,
300 struct archive_entry *);
301 static int archive_read_format_rar_read_data(struct archive_read *,
302 const void **, size_t *, int64_t *);
303 static int archive_read_format_rar_read_data_skip(struct archive_read *a);
304 static int archive_read_format_rar_cleanup(struct archive_read *);
306 /* Support functions */
307 static int read_header(struct archive_read *, struct archive_entry *, char);
308 static time_t get_time(int time);
309 static int read_exttime(const char *, struct rar *, const char *);
310 static int read_symlink_stored(struct archive_read *, struct archive_entry *,
311 struct archive_string_conv *);
312 static int read_data_stored(struct archive_read *, const void **, size_t *,
314 static int read_data_compressed(struct archive_read *, const void **, size_t *,
316 static int rar_br_preparation(struct archive_read *, struct rar_br *);
317 static int parse_codes(struct archive_read *);
318 static void free_codes(struct archive_read *);
319 static int read_next_symbol(struct archive_read *, struct huffman_code *);
320 static int create_code(struct archive_read *, struct huffman_code *,
321 unsigned char *, int, char);
322 static int add_value(struct archive_read *, struct huffman_code *, int, int,
324 static int new_node(struct huffman_code *);
325 static int make_table(struct archive_read *, struct huffman_code *);
326 static int make_table_recurse(struct archive_read *, struct huffman_code *, int,
327 struct huffman_table_entry *, int, int);
328 static int64_t expand(struct archive_read *, int64_t);
329 static int copy_from_lzss_window(struct archive_read *, const void **,
335 /* Check that the cache buffer has enough bits. */
336 #define rar_br_has(br, n) ((br)->cache_avail >= n)
337 /* Get compressed data by bit. */
338 #define rar_br_bits(br, n) \
339 (((uint32_t)((br)->cache_buffer >> \
340 ((br)->cache_avail - (n)))) & cache_masks[n])
341 #define rar_br_bits_forced(br, n) \
342 (((uint32_t)((br)->cache_buffer << \
343 ((n) - (br)->cache_avail))) & cache_masks[n])
344 /* Read ahead to make sure the cache buffer has enough compressed data we
346 * True : completed, there is enough data in the cache buffer.
347 * False : there is no data in the stream. */
348 #define rar_br_read_ahead(a, br, n) \
349 ((rar_br_has(br, (n)) || rar_br_fillup(a, br)) || rar_br_has(br, (n)))
350 /* Notify how many bits we consumed. */
351 #define rar_br_consume(br, n) ((br)->cache_avail -= (n))
352 #define rar_br_consume_unalined_bits(br) ((br)->cache_avail &= ~7)
354 static const uint32_t cache_masks[] = {
355 0x00000000, 0x00000001, 0x00000003, 0x00000007,
356 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
357 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
358 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
359 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
360 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
361 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
362 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
363 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
367 * Shift away used bits in the cache data and fill it up with following bits.
368 * Call this when cache buffer does not have enough bits you need.
370 * Returns 1 if the cache buffer is full.
371 * Returns 0 if the cache buffer is not full; input buffer is empty.
374 rar_br_fillup(struct archive_read *a, struct rar_br *br)
376 struct rar *rar = (struct rar *)(a->format->data);
377 int n = CACHE_BITS - br->cache_avail;
382 if (br->avail_in >= 8) {
384 ((uint64_t)br->next_in[0]) << 56 |
385 ((uint64_t)br->next_in[1]) << 48 |
386 ((uint64_t)br->next_in[2]) << 40 |
387 ((uint64_t)br->next_in[3]) << 32 |
388 ((uint32_t)br->next_in[4]) << 24 |
389 ((uint32_t)br->next_in[5]) << 16 |
390 ((uint32_t)br->next_in[6]) << 8 |
391 (uint32_t)br->next_in[7];
394 br->cache_avail += 8 * 8;
395 rar->bytes_unconsumed += 8;
396 rar->bytes_remaining -= 8;
401 if (br->avail_in >= 7) {
403 (br->cache_buffer << 56) |
404 ((uint64_t)br->next_in[0]) << 48 |
405 ((uint64_t)br->next_in[1]) << 40 |
406 ((uint64_t)br->next_in[2]) << 32 |
407 ((uint32_t)br->next_in[3]) << 24 |
408 ((uint32_t)br->next_in[4]) << 16 |
409 ((uint32_t)br->next_in[5]) << 8 |
410 (uint32_t)br->next_in[6];
413 br->cache_avail += 7 * 8;
414 rar->bytes_unconsumed += 7;
415 rar->bytes_remaining -= 7;
420 if (br->avail_in >= 6) {
422 (br->cache_buffer << 48) |
423 ((uint64_t)br->next_in[0]) << 40 |
424 ((uint64_t)br->next_in[1]) << 32 |
425 ((uint32_t)br->next_in[2]) << 24 |
426 ((uint32_t)br->next_in[3]) << 16 |
427 ((uint32_t)br->next_in[4]) << 8 |
428 (uint32_t)br->next_in[5];
431 br->cache_avail += 6 * 8;
432 rar->bytes_unconsumed += 6;
433 rar->bytes_remaining -= 6;
438 /* We have enough compressed data in
439 * the cache buffer.*/
444 if (br->avail_in <= 0) {
446 if (rar->bytes_unconsumed > 0) {
447 /* Consume as much as the decompressor
449 __archive_read_consume(a, rar->bytes_unconsumed);
450 rar->bytes_unconsumed = 0;
452 br->next_in = __archive_read_ahead(a, 1, &(br->avail_in));
453 if (br->next_in == NULL)
455 if (br->avail_in > rar->bytes_remaining)
456 br->avail_in = rar->bytes_remaining;
457 if (br->avail_in == 0)
461 (br->cache_buffer << 8) | *br->next_in++;
463 br->cache_avail += 8;
465 rar->bytes_unconsumed++;
466 rar->bytes_remaining--;
471 rar_br_preparation(struct archive_read *a, struct rar_br *br)
473 struct rar *rar = (struct rar *)(a->format->data);
475 if (rar->bytes_remaining > 0) {
476 br->next_in = __archive_read_ahead(a, 1, &(br->avail_in));
477 if (br->next_in == NULL) {
478 archive_set_error(&a->archive,
479 ARCHIVE_ERRNO_FILE_FORMAT,
480 "Truncated RAR file data");
481 return (ARCHIVE_FATAL);
483 if (br->avail_in > rar->bytes_remaining)
484 br->avail_in = rar->bytes_remaining;
485 if (br->cache_avail == 0)
486 (void)rar_br_fillup(a, br);
491 /* Find last bit set */
493 rar_fls(unsigned int word)
499 word |= (word >> 16);
500 return word - (word >> 1);
504 static inline int64_t
505 lzss_position(struct lzss *lzss)
507 return lzss->position;
511 lzss_mask(struct lzss *lzss)
517 lzss_size(struct lzss *lzss)
519 return lzss->mask + 1;
523 lzss_offset_for_position(struct lzss *lzss, int64_t pos)
525 return pos & lzss->mask;
528 static inline unsigned char *
529 lzss_pointer_for_position(struct lzss *lzss, int64_t pos)
531 return &lzss->window[lzss_offset_for_position(lzss, pos)];
535 lzss_current_offset(struct lzss *lzss)
537 return lzss_offset_for_position(lzss, lzss->position);
540 static inline uint8_t *
541 lzss_current_pointer(struct lzss *lzss)
543 return lzss_pointer_for_position(lzss, lzss->position);
547 lzss_emit_literal(struct rar *rar, uint8_t literal)
549 *lzss_current_pointer(&rar->lzss) = literal;
550 rar->lzss.position++;
554 lzss_emit_match(struct rar *rar, int offset, int length)
556 int dstoffs = lzss_current_offset(&rar->lzss);
557 int srcoffs = (dstoffs - offset) & lzss_mask(&rar->lzss);
558 int l, li, remaining;
559 unsigned char *d, *s;
562 while (remaining > 0) {
564 if (dstoffs > srcoffs) {
565 if (l > lzss_size(&rar->lzss) - dstoffs)
566 l = lzss_size(&rar->lzss) - dstoffs;
568 if (l > lzss_size(&rar->lzss) - srcoffs)
569 l = lzss_size(&rar->lzss) - srcoffs;
571 d = &(rar->lzss.window[dstoffs]);
572 s = &(rar->lzss.window[srcoffs]);
573 if ((dstoffs + l < srcoffs) || (srcoffs + l < dstoffs))
576 for (li = 0; li < l; li++)
580 dstoffs = (dstoffs + l) & lzss_mask(&(rar->lzss));
581 srcoffs = (srcoffs + l) & lzss_mask(&(rar->lzss));
583 rar->lzss.position += length;
587 ppmd_alloc(void *p, size_t size)
593 ppmd_free(void *p, void *address)
598 static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
603 struct archive_read *a = ((IByteIn*)p)->a;
604 struct rar *rar = (struct rar *)(a->format->data);
605 struct rar_br *br = &(rar->br);
607 if (!rar_br_read_ahead(a, br, 8))
609 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
610 "Truncated RAR file data");
614 b = rar_br_bits(br, 8);
615 rar_br_consume(br, 8);
620 archive_read_support_format_rar(struct archive *_a)
622 struct archive_read *a = (struct archive_read *)_a;
626 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
627 "archive_read_support_format_rar");
629 rar = (struct rar *)malloc(sizeof(*rar));
632 archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
633 return (ARCHIVE_FATAL);
635 memset(rar, 0, sizeof(*rar));
637 r = __archive_read_register_format(a,
640 archive_read_format_rar_bid,
641 archive_read_format_rar_options,
642 archive_read_format_rar_read_header,
643 archive_read_format_rar_read_data,
644 archive_read_format_rar_read_data_skip,
645 archive_read_format_rar_cleanup);
653 archive_read_format_rar_bid(struct archive_read *a, int best_bid)
657 /* If there's already a bid > 30, we'll never win. */
661 if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
664 if (memcmp(p, RAR_SIGNATURE, 7) == 0)
667 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
668 /* This is a PE file */
669 ssize_t offset = 0x10000;
670 ssize_t window = 4096;
672 while (offset + window <= (1024 * 128)) {
673 const char *buff = __archive_read_ahead(a, offset + window, &bytes_avail);
675 /* Remaining bytes are less than window. */
682 while (p + 7 < buff + bytes_avail) {
683 if (memcmp(p, RAR_SIGNATURE, 7) == 0)
694 skip_sfx(struct archive_read *a)
699 ssize_t bytes, window;
703 while (total + window <= (1024 * 128)) {
704 h = __archive_read_ahead(a, window, &bytes);
706 /* Remaining bytes are less than window. */
718 * Scan ahead until we find something that looks
719 * like the RAR header.
722 if (memcmp(p, RAR_SIGNATURE, 7) == 0) {
723 skip = p - (const char *)h;
724 __archive_read_consume(a, skip);
729 skip = p - (const char *)h;
730 __archive_read_consume(a, skip);
734 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
735 "Couldn't find out RAR header");
736 return (ARCHIVE_FATAL);
740 archive_read_format_rar_options(struct archive_read *a,
741 const char *key, const char *val)
744 int ret = ARCHIVE_FAILED;
746 rar = (struct rar *)(a->format->data);
747 if (strcmp(key, "hdrcharset") == 0) {
748 if (val == NULL || val[0] == 0)
749 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
750 "rar: hdrcharset option needs a character-set name");
753 archive_string_conversion_from_charset(
754 &a->archive, val, 0);
755 if (rar->opt_sconv != NULL)
761 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
762 "rar: unknown keyword ``%s''", key);
768 archive_read_format_rar_read_header(struct archive_read *a,
769 struct archive_entry *entry)
779 a->archive.archive_format = ARCHIVE_FORMAT_RAR;
780 if (a->archive.archive_format_name == NULL)
781 a->archive.archive_format_name = "RAR";
783 rar = (struct rar *)(a->format->data);
785 /* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
788 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
789 return (ARCHIVE_EOF);
792 if (rar->found_first_header == 0 &&
793 ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0)) {
794 /* This is an executable ? Must be self-extracting... */
796 if (ret < ARCHIVE_WARN)
799 rar->found_first_header = 1;
803 unsigned long crc32_val;
805 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
806 return (ARCHIVE_FATAL);
813 if (memcmp(p, RAR_SIGNATURE, 7) != 0) {
814 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
815 "Invalid marker header");
816 return (ARCHIVE_FATAL);
818 __archive_read_consume(a, 7);
822 rar->main_flags = archive_le16dec(p + 3);
823 skip = archive_le16dec(p + 5);
824 if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
825 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
826 "Invalid header size");
827 return (ARCHIVE_FATAL);
829 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
830 return (ARCHIVE_FATAL);
832 memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
833 memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
834 sizeof(rar->reserved2));
835 if (rar->main_flags & MHD_ENCRYPTVER) {
836 if (skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)+1) {
837 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
838 "Invalid header size");
839 return (ARCHIVE_FATAL);
841 rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
842 sizeof(rar->reserved2));
845 if (rar->main_flags & MHD_VOLUME ||
846 rar->main_flags & MHD_FIRSTVOLUME)
848 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
849 "RAR volume support unavailable.");
850 return (ARCHIVE_FATAL);
852 if (rar->main_flags & MHD_PASSWORD)
854 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
855 "RAR encryption support unavailable.");
856 return (ARCHIVE_FATAL);
859 crc32_val = crc32(0, (const unsigned char *)p + 2, skip - 2);
860 if ((crc32_val & 0xffff) != archive_le16dec(p)) {
861 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
863 return (ARCHIVE_FATAL);
865 __archive_read_consume(a, skip);
869 return read_header(a, entry, head_type);
876 flags = archive_le16dec(p + 3);
877 skip = archive_le16dec(p + 5);
879 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
880 "Invalid header size");
881 return (ARCHIVE_FATAL);
884 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
885 return (ARCHIVE_FATAL);
888 if (flags & HD_ADD_SIZE_PRESENT)
891 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
892 "Invalid header size");
893 return (ARCHIVE_FATAL);
895 skip += archive_le32dec(p + 7);
896 if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
897 return (ARCHIVE_FATAL);
901 crc32_val = crc32(0, (const unsigned char *)p + 2, skip - 2);
902 if ((crc32_val & 0xffff) != archive_le16dec(p)) {
903 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
905 return (ARCHIVE_FATAL);
907 __archive_read_consume(a, skip);
911 if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
916 return (ARCHIVE_EOF);
919 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
921 return (ARCHIVE_FATAL);
927 archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
928 size_t *size, int64_t *offset)
930 struct rar *rar = (struct rar *)(a->format->data);
933 if (rar->bytes_unconsumed > 0) {
934 /* Consume as much as the decompressor actually used. */
935 __archive_read_consume(a, rar->bytes_unconsumed);
936 rar->bytes_unconsumed = 0;
939 if (rar->entry_eof) {
942 *offset = rar->offset;
943 return (ARCHIVE_EOF);
946 switch (rar->compression_method)
948 case COMPRESS_METHOD_STORE:
949 ret = read_data_stored(a, buff, size, offset);
952 case COMPRESS_METHOD_FASTEST:
953 case COMPRESS_METHOD_FAST:
954 case COMPRESS_METHOD_NORMAL:
955 case COMPRESS_METHOD_GOOD:
956 case COMPRESS_METHOD_BEST:
957 ret = read_data_compressed(a, buff, size, offset);
958 if (ret != ARCHIVE_OK && ret != ARCHIVE_WARN)
959 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
963 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
964 "Unsupported compression method for RAR file.");
972 archive_read_format_rar_read_data_skip(struct archive_read *a)
975 int64_t bytes_skipped;
977 rar = (struct rar *)(a->format->data);
979 if (rar->bytes_unconsumed > 0) {
980 /* Consume as much as the decompressor actually used. */
981 __archive_read_consume(a, rar->bytes_unconsumed);
982 rar->bytes_unconsumed = 0;
985 if (rar->bytes_remaining > 0) {
986 bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
987 if (bytes_skipped < 0)
988 return (ARCHIVE_FATAL);
994 archive_read_format_rar_cleanup(struct archive_read *a)
998 rar = (struct rar *)(a->format->data);
1000 free(rar->filename);
1001 free(rar->unp_buffer);
1002 free(rar->lzss.window);
1003 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1005 (a->format->data) = NULL;
1006 return (ARCHIVE_OK);
1010 read_header(struct archive_read *a, struct archive_entry *entry,
1014 const char *p, *endp;
1016 struct rar_header rar_header;
1017 struct rar_file_header file_header;
1018 int64_t header_size;
1019 unsigned filename_size, end;
1022 char packed_size[8];
1025 struct archive_string_conv *sconv, *fn_sconv;
1026 unsigned long crc32_val;
1027 int ret = (ARCHIVE_OK), ret2;
1029 rar = (struct rar *)(a->format->data);
1031 /* Setup a string conversion object for non-rar-unicode filenames. */
1032 sconv = rar->opt_sconv;
1033 if (sconv == NULL) {
1034 if (!rar->init_default_conversion) {
1035 rar->sconv_default =
1036 archive_string_default_conversion_for_read(
1038 rar->init_default_conversion = 1;
1040 sconv = rar->sconv_default;
1044 if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
1045 return (ARCHIVE_FATAL);
1047 memcpy(&rar_header, p, sizeof(rar_header));
1048 rar->file_flags = archive_le16dec(rar_header.flags);
1049 header_size = archive_le16dec(rar_header.size);
1050 if (header_size < sizeof(file_header) + 7) {
1051 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1052 "Invalid header size");
1053 return (ARCHIVE_FATAL);
1055 crc32_val = crc32(0, (const unsigned char *)p + 2, 7 - 2);
1056 __archive_read_consume(a, 7);
1058 if (!(rar->file_flags & FHD_SOLID))
1060 rar->compression_method = 0;
1061 rar->packed_size = 0;
1068 memset(&rar->salt, 0, sizeof(rar->salt));
1080 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1081 "RAR solid archive support unavailable.");
1082 return (ARCHIVE_FATAL);
1085 if ((h = __archive_read_ahead(a, header_size - 7, NULL)) == NULL)
1086 return (ARCHIVE_FATAL);
1088 /* File Header CRC check. */
1089 crc32_val = crc32(crc32_val, h, header_size - 7);
1090 if ((crc32_val & 0xffff) != archive_le16dec(rar_header.crc)) {
1091 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1092 "Header CRC error");
1093 return (ARCHIVE_FATAL);
1095 /* If no CRC error, Go on parsing File Header. */
1097 endp = p + header_size - 7;
1098 memcpy(&file_header, p, sizeof(file_header));
1099 p += sizeof(file_header);
1101 rar->compression_method = file_header.method;
1103 time = archive_le32dec(file_header.file_time);
1104 rar->mtime = get_time(time);
1106 rar->file_crc = archive_le32dec(file_header.file_crc);
1108 if (rar->file_flags & FHD_PASSWORD)
1110 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1111 "RAR encryption support unavailable.");
1112 return (ARCHIVE_FATAL);
1115 if (rar->file_flags & FHD_LARGE)
1117 memcpy(packed_size, file_header.pack_size, 4);
1118 memcpy(packed_size + 4, p, 4); /* High pack size */
1120 memcpy(unp_size, file_header.unp_size, 4);
1121 memcpy(unp_size + 4, p, 4); /* High unpack size */
1123 rar->packed_size = archive_le64dec(&packed_size);
1124 rar->unp_size = archive_le64dec(&unp_size);
1128 rar->packed_size = archive_le32dec(file_header.pack_size);
1129 rar->unp_size = archive_le32dec(file_header.unp_size);
1132 /* TODO: Need to use CRC check for these kind of cases.
1133 * For now, check if sizes are not < 0.
1135 if (rar->packed_size < 0 || rar->unp_size < 0)
1137 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1138 "Invalid sizes specified.");
1139 return (ARCHIVE_FATAL);
1142 /* TODO: RARv3 subblocks contain comments. For now the complete block is
1143 * consumed at the end.
1145 if (head_type == NEWSUB_HEAD) {
1146 size_t distance = p - (const char *)h;
1147 header_size += rar->packed_size;
1148 /* Make sure we have the extended data. */
1149 if ((h = __archive_read_ahead(a, header_size - 7, NULL)) == NULL)
1150 return (ARCHIVE_FATAL);
1152 endp = p + header_size - 7;
1156 filename_size = archive_le16dec(file_header.name_size);
1157 if (p + filename_size > endp) {
1158 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1159 "Invalid filename size");
1160 return (ARCHIVE_FATAL);
1162 if (rar->filename_allocated < filename_size+2) {
1163 rar->filename = realloc(rar->filename, filename_size+2);
1164 if (rar->filename == NULL) {
1165 archive_set_error(&a->archive, ENOMEM,
1166 "Couldn't allocate memory.");
1167 return (ARCHIVE_FATAL);
1170 filename = rar->filename;
1171 memcpy(filename, p, filename_size);
1172 filename[filename_size] = '\0';
1173 if (rar->file_flags & FHD_UNICODE)
1175 if (filename_size != strlen(filename))
1177 unsigned char highbyte, flagbits, flagbyte, length, offset;
1179 end = filename_size;
1181 offset = strlen(filename) + 1;
1182 highbyte = *(p + offset++);
1185 while (offset < end && filename_size < end)
1189 flagbyte = *(p + offset++);
1194 switch((flagbyte >> flagbits) & 3)
1197 filename[filename_size++] = '\0';
1198 filename[filename_size++] = *(p + offset++);
1201 filename[filename_size++] = highbyte;
1202 filename[filename_size++] = *(p + offset++);
1205 filename[filename_size++] = *(p + offset + 1);
1206 filename[filename_size++] = *(p + offset);
1211 length = *(p + offset++);
1214 if (filename_size >= end)
1216 filename[filename_size++] = *(p + offset);
1223 if (filename_size >= end) {
1224 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1225 "Invalid filename");
1226 return (ARCHIVE_FATAL);
1228 filename[filename_size++] = '\0';
1229 filename[filename_size++] = '\0';
1231 /* Decoded unicode form is UTF-16BE, so we have to update a string
1232 * conversion object for it. */
1233 if (rar->sconv_utf16be == NULL) {
1234 rar->sconv_utf16be = archive_string_conversion_from_charset(
1235 &a->archive, "UTF-16BE", 1);
1236 if (rar->sconv_utf16be == NULL)
1237 return (ARCHIVE_FATAL);
1239 fn_sconv = rar->sconv_utf16be;
1242 while (memcmp(strp, "\x00\x00", 2))
1244 if (!memcmp(strp, "\x00\\", 2))
1251 * If FHD_UNICODE is set but no unicode data, this file name form
1252 * is UTF-8, so we have to update a string conversion object for
1255 if (rar->sconv_utf8 == NULL) {
1256 rar->sconv_utf8 = archive_string_conversion_from_charset(
1257 &a->archive, "UTF-8", 1);
1258 if (rar->sconv_utf8 == NULL)
1259 return (ARCHIVE_FATAL);
1261 fn_sconv = rar->sconv_utf8;
1262 while ((strp = strchr(filename, '\\')) != NULL)
1270 while ((strp = strchr(filename, '\\')) != NULL)
1275 if (rar->file_flags & FHD_SALT)
1278 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1279 "Invalid header size");
1280 return (ARCHIVE_FATAL);
1282 memcpy(rar->salt, p, 8);
1286 if (rar->file_flags & FHD_EXTTIME) {
1287 if (read_exttime(p, rar, endp) < 0) {
1288 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1289 "Invalid header size");
1290 return (ARCHIVE_FATAL);
1294 __archive_read_consume(a, header_size - 7);
1296 switch(file_header.host_os)
1301 rar->mode = archive_le32dec(file_header.file_attr);
1302 if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
1303 rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
1305 rar->mode = AE_IFREG;
1306 rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1312 rar->mode = archive_le32dec(file_header.file_attr);
1316 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1317 "Unknown file attributes from RAR file's host OS");
1318 return (ARCHIVE_FATAL);
1321 rar->bytes_remaining = rar->packed_size;
1322 rar->bytes_uncopied = rar->bytes_unconsumed = 0;
1323 rar->lzss.position = rar->dictionary_size = rar->offset = 0;
1324 rar->offset_outgoing = 0;
1325 rar->br.cache_avail = 0;
1326 rar->br.avail_in = 0;
1327 rar->crc_calculated = 0;
1330 rar->is_ppmd_block = 0;
1331 rar->start_new_table = 1;
1332 free(rar->unp_buffer);
1333 rar->unp_buffer = NULL;
1334 rar->unp_offset = 0;
1335 rar->unp_buffer_size = UNP_BUFFER_SIZE;
1336 memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
1337 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1338 rar->ppmd_valid = rar->ppmd_eod = 0;
1340 /* Don't set any archive entries for non-file header types */
1341 if (head_type == NEWSUB_HEAD)
1344 archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
1345 archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
1346 archive_entry_set_atime(entry, rar->atime, rar->ansec);
1347 archive_entry_set_size(entry, rar->unp_size);
1348 archive_entry_set_mode(entry, rar->mode);
1350 if (archive_entry_copy_pathname_l(entry, filename, filename_size, fn_sconv))
1352 if (errno == ENOMEM)
1354 archive_set_error(&a->archive, ENOMEM,
1355 "Can't allocate memory for Pathname");
1356 return (ARCHIVE_FATAL);
1358 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1359 "Pathname cannot be converted from %s to current locale.",
1360 archive_string_conversion_charset_name(fn_sconv));
1361 ret = (ARCHIVE_WARN);
1364 if (((rar->mode) & AE_IFMT) == AE_IFLNK)
1366 /* Make sure a symbolic-link file does not have its body. */
1367 rar->bytes_remaining = 0;
1368 archive_entry_set_size(entry, 0);
1370 /* Read a symbolic-link name. */
1371 if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
1377 if (rar->bytes_remaining == 0)
1387 tm.tm_sec = 2 * (time & 0x1f);
1388 tm.tm_min = (time >> 5) & 0x3f;
1389 tm.tm_hour = (time >> 11) & 0x1f;
1390 tm.tm_mday = (time >> 16) & 0x1f;
1391 tm.tm_mon = ((time >> 21) & 0x0f) - 1;
1392 tm.tm_year = ((time >> 25) & 0x7f) + 80;
1398 read_exttime(const char *p, struct rar *rar, const char *endp)
1400 unsigned rmode, flags, rem, j, count;
1408 flags = archive_le16dec(p);
1411 for (i = 3; i >= 0; i--)
1416 rmode = flags >> i * 4;
1423 time = archive_le32dec(p);
1429 if (p + count > endp)
1431 for (j = 0; j < count; j++)
1433 rem = ((*p) << 16) | (rem >> 8);
1437 nsec = tm->tm_sec + rem / NS_UNIT;
1461 rar->arcnsec = nsec;
1469 read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
1470 struct archive_string_conv *sconv)
1475 int ret = (ARCHIVE_OK);
1477 rar = (struct rar *)(a->format->data);
1478 if ((h = __archive_read_ahead(a, rar->packed_size, NULL)) == NULL)
1479 return (ARCHIVE_FATAL);
1482 if (archive_entry_copy_symlink_l(entry, p, rar->packed_size, sconv))
1484 if (errno == ENOMEM)
1486 archive_set_error(&a->archive, ENOMEM,
1487 "Can't allocate memory for link");
1488 return (ARCHIVE_FATAL);
1490 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1491 "link cannot be converted from %s to current locale.",
1492 archive_string_conversion_charset_name(sconv));
1493 ret = (ARCHIVE_WARN);
1495 __archive_read_consume(a, rar->packed_size);
1500 read_data_stored(struct archive_read *a, const void **buff, size_t *size,
1504 ssize_t bytes_avail;
1506 rar = (struct rar *)(a->format->data);
1507 if (rar->bytes_remaining == 0)
1511 *offset = rar->offset;
1512 if (rar->file_crc != rar->crc_calculated) {
1513 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1515 return (ARCHIVE_FATAL);
1518 return (ARCHIVE_EOF);
1521 *buff = __archive_read_ahead(a, 1, &bytes_avail);
1522 if (bytes_avail <= 0)
1524 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1525 "Truncated RAR file data");
1526 return (ARCHIVE_FATAL);
1528 if (bytes_avail > rar->bytes_remaining)
1529 bytes_avail = rar->bytes_remaining;
1531 *size = bytes_avail;
1532 *offset = rar->offset;
1533 rar->offset += bytes_avail;
1534 rar->bytes_remaining -= bytes_avail;
1535 rar->bytes_unconsumed = bytes_avail;
1536 /* Calculate File CRC. */
1537 rar->crc_calculated = crc32(rar->crc_calculated, *buff, bytes_avail);
1538 return (ARCHIVE_OK);
1542 read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
1546 int64_t start, end, actualend;
1548 int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
1550 rar = (struct rar *)(a->format->data);
1554 return (ARCHIVE_FATAL);
1555 if (rar->ppmd_eod ||
1556 (rar->dictionary_size && rar->offset >= rar->unp_size))
1558 if (rar->unp_offset > 0) {
1560 * We have unprocessed extracted data. write it out.
1562 *buff = rar->unp_buffer;
1563 *size = rar->unp_offset;
1564 *offset = rar->offset_outgoing;
1565 rar->offset_outgoing += *size;
1566 /* Calculate File CRC. */
1567 rar->crc_calculated = crc32(rar->crc_calculated, *buff, *size);
1568 rar->unp_offset = 0;
1569 return (ARCHIVE_OK);
1573 *offset = rar->offset;
1574 if (rar->file_crc != rar->crc_calculated) {
1575 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1577 return (ARCHIVE_FATAL);
1580 return (ARCHIVE_EOF);
1583 if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
1585 if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
1586 bs = rar->unp_buffer_size - rar->unp_offset;
1588 bs = rar->bytes_uncopied;
1589 ret = copy_from_lzss_window(a, buff, rar->offset, bs);
1590 if (ret != ARCHIVE_OK)
1593 rar->bytes_uncopied -= bs;
1594 if (*buff != NULL) {
1595 rar->unp_offset = 0;
1596 *size = rar->unp_buffer_size;
1597 *offset = rar->offset_outgoing;
1598 rar->offset_outgoing += *size;
1599 /* Calculate File CRC. */
1600 rar->crc_calculated = crc32(rar->crc_calculated, *buff, *size);
1606 if (!rar->br.next_in &&
1607 (ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
1609 if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
1612 if (rar->is_ppmd_block)
1614 if ((sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1615 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1617 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1619 return (ARCHIVE_FATAL);
1621 if(sym != rar->ppmd_escape)
1623 lzss_emit_literal(rar, sym);
1624 rar->bytes_uncopied++;
1628 if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1629 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1631 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1633 return (ARCHIVE_FATAL);
1639 rar->start_new_table = 1;
1640 return read_data_compressed(a, buff, size, offset);
1643 rar->ppmd_eod = 1;/* End Of ppmd Data. */
1647 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1648 "Parsing filters is unsupported.");
1649 return (ARCHIVE_FAILED);
1653 for (i = 2; i >= 0; i--)
1655 if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1656 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1658 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1660 return (ARCHIVE_FATAL);
1662 lzss_offset |= code << (i * 8);
1664 if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1665 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1667 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1669 return (ARCHIVE_FATAL);
1671 lzss_emit_match(rar, lzss_offset + 2, length + 32);
1672 rar->bytes_uncopied += length + 32;
1676 if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1677 &rar->ppmd7_context, &rar->range_dec.p)) < 0)
1679 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1681 return (ARCHIVE_FATAL);
1683 lzss_emit_match(rar, 1, length + 4);
1684 rar->bytes_uncopied += length + 4;
1688 lzss_emit_literal(rar, sym);
1689 rar->bytes_uncopied++;
1695 start = rar->offset;
1696 end = start + rar->dictionary_size;
1697 rar->filterstart = INT64_MAX;
1699 if ((actualend = expand(a, end)) < 0)
1700 return ((int)actualend);
1702 rar->bytes_uncopied = actualend - start;
1703 if (rar->bytes_uncopied == 0) {
1704 /* Broken RAR files cause this case.
1705 * NOTE: If this case were possible on a normal RAR file
1706 * we would find out where it was actually bad and
1707 * what we would do to solve it. */
1708 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1709 "Internal error extracting RAR file");
1710 return (ARCHIVE_FATAL);
1713 if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
1714 bs = rar->unp_buffer_size - rar->unp_offset;
1716 bs = rar->bytes_uncopied;
1717 ret = copy_from_lzss_window(a, buff, rar->offset, bs);
1718 if (ret != ARCHIVE_OK)
1721 rar->bytes_uncopied -= bs;
1723 * If *buff is NULL, it means unp_buffer is not full.
1724 * So we have to continue extracting a RAR file.
1726 } while (*buff == NULL);
1728 rar->unp_offset = 0;
1729 *size = rar->unp_buffer_size;
1730 *offset = rar->offset_outgoing;
1731 rar->offset_outgoing += *size;
1732 /* Calculate File CRC. */
1733 rar->crc_calculated = crc32(rar->crc_calculated, *buff, *size);
1738 parse_codes(struct archive_read *a)
1740 int i, j, val, n, r;
1741 unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
1742 unsigned int maxorder;
1743 struct huffman_code precode;
1744 struct rar *rar = (struct rar *)(a->format->data);
1745 struct rar_br *br = &(rar->br);
1749 /* Skip to the next byte */
1750 rar_br_consume_unalined_bits(br);
1752 /* PPMd block flag */
1753 if (!rar_br_read_ahead(a, br, 1))
1754 goto truncated_data;
1755 if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
1757 rar_br_consume(br, 1);
1758 if (!rar_br_read_ahead(a, br, 7))
1759 goto truncated_data;
1760 ppmd_flags = rar_br_bits(br, 7);
1761 rar_br_consume(br, 7);
1763 /* Memory is allocated in MB */
1764 if (ppmd_flags & 0x20)
1766 if (!rar_br_read_ahead(a, br, 8))
1767 goto truncated_data;
1768 rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
1769 rar_br_consume(br, 8);
1772 if (ppmd_flags & 0x40)
1774 if (!rar_br_read_ahead(a, br, 8))
1775 goto truncated_data;
1776 rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
1777 rar_br_consume(br, 8);
1780 rar->ppmd_escape = 2;
1782 if (ppmd_flags & 0x20)
1784 maxorder = (ppmd_flags & 0x1F) + 1;
1786 maxorder = 16 + (maxorder - 16) * 3;
1790 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1791 "Truncated RAR file data");
1792 return (ARCHIVE_FATAL);
1795 /* Make sure ppmd7_contest is freed before Ppmd7_Construct
1796 * because reading a broken file cause this abnormal sequence. */
1797 __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context, &g_szalloc);
1800 rar->bytein.Read = &ppmd_read;
1801 __archive_ppmd7_functions.PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
1802 rar->range_dec.Stream = &rar->bytein;
1803 __archive_ppmd7_functions.Ppmd7_Construct(&rar->ppmd7_context);
1805 if (!__archive_ppmd7_functions.Ppmd7_Alloc(&rar->ppmd7_context,
1806 rar->dictionary_size, &g_szalloc))
1808 archive_set_error(&a->archive, ENOMEM,
1810 return (ARCHIVE_FATAL);
1812 if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
1814 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1815 "Unable to initialize PPMd range decoder");
1816 return (ARCHIVE_FATAL);
1818 __archive_ppmd7_functions.Ppmd7_Init(&rar->ppmd7_context, maxorder);
1819 rar->ppmd_valid = 1;
1823 if (!rar->ppmd_valid) {
1824 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1825 "Invalid PPMd sequence");
1826 return (ARCHIVE_FATAL);
1828 if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
1830 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1831 "Unable to initialize PPMd range decoder");
1832 return (ARCHIVE_FATAL);
1838 rar_br_consume(br, 1);
1840 /* Keep existing table flag */
1841 if (!rar_br_read_ahead(a, br, 1))
1842 goto truncated_data;
1843 if (!rar_br_bits(br, 1))
1844 memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
1845 rar_br_consume(br, 1);
1847 memset(&bitlengths, 0, sizeof(bitlengths));
1848 for (i = 0; i < MAX_SYMBOLS;)
1850 if (!rar_br_read_ahead(a, br, 4))
1851 goto truncated_data;
1852 bitlengths[i++] = rar_br_bits(br, 4);
1853 rar_br_consume(br, 4);
1854 if (bitlengths[i-1] == 0xF)
1856 if (!rar_br_read_ahead(a, br, 4))
1857 goto truncated_data;
1858 zerocount = rar_br_bits(br, 4);
1859 rar_br_consume(br, 4);
1863 for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
1864 bitlengths[i++] = 0;
1869 memset(&precode, 0, sizeof(precode));
1870 r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
1871 if (r != ARCHIVE_OK) {
1873 free(precode.table);
1877 for (i = 0; i < HUFFMAN_TABLE_SIZE;)
1879 if ((val = read_next_symbol(a, &precode)) < 0) {
1881 free(precode.table);
1882 return (ARCHIVE_FATAL);
1886 rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
1894 free(precode.table);
1895 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1896 "Internal error extracting RAR file.");
1897 return (ARCHIVE_FATAL);
1901 if (!rar_br_read_ahead(a, br, 3)) {
1903 free(precode.table);
1904 goto truncated_data;
1906 n = rar_br_bits(br, 3) + 3;
1907 rar_br_consume(br, 3);
1909 if (!rar_br_read_ahead(a, br, 7)) {
1911 free(precode.table);
1912 goto truncated_data;
1914 n = rar_br_bits(br, 7) + 11;
1915 rar_br_consume(br, 7);
1918 for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
1920 rar->lengthtable[i] = rar->lengthtable[i-1];
1927 if (!rar_br_read_ahead(a, br, 3)) {
1929 free(precode.table);
1930 goto truncated_data;
1932 n = rar_br_bits(br, 3) + 3;
1933 rar_br_consume(br, 3);
1935 if (!rar_br_read_ahead(a, br, 7)) {
1937 free(precode.table);
1938 goto truncated_data;
1940 n = rar_br_bits(br, 7) + 11;
1941 rar_br_consume(br, 7);
1944 for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
1945 rar->lengthtable[i++] = 0;
1949 free(precode.table);
1951 r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
1953 if (r != ARCHIVE_OK)
1955 r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
1956 OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
1957 if (r != ARCHIVE_OK)
1959 r = create_code(a, &rar->lowoffsetcode,
1960 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
1961 LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
1962 if (r != ARCHIVE_OK)
1964 r = create_code(a, &rar->lengthcode,
1965 &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
1966 LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
1967 if (r != ARCHIVE_OK)
1971 if (!rar->dictionary_size || !rar->lzss.window)
1973 /* Seems as though dictionary sizes are not used. Even so, minimize
1974 * memory usage as much as possible.
1976 if (rar->unp_size >= DICTIONARY_MAX_SIZE)
1977 rar->dictionary_size = DICTIONARY_MAX_SIZE;
1979 rar->dictionary_size = rar_fls(rar->unp_size) << 1;
1980 rar->lzss.window = (unsigned char *)realloc(rar->lzss.window,
1981 rar->dictionary_size);
1982 if (rar->lzss.window == NULL) {
1983 archive_set_error(&a->archive, ENOMEM,
1984 "Unable to allocate memory for uncompressed data.");
1985 return (ARCHIVE_FATAL);
1987 memset(rar->lzss.window, 0, rar->dictionary_size);
1988 rar->lzss.mask = rar->dictionary_size - 1;
1991 rar->start_new_table = 0;
1992 return (ARCHIVE_OK);
1994 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1995 "Truncated RAR file data");
1997 return (ARCHIVE_FATAL);
2001 free_codes(struct archive_read *a)
2003 struct rar *rar = (struct rar *)(a->format->data);
2004 free(rar->maincode.tree);
2005 free(rar->offsetcode.tree);
2006 free(rar->lowoffsetcode.tree);
2007 free(rar->lengthcode.tree);
2008 free(rar->maincode.table);
2009 free(rar->offsetcode.table);
2010 free(rar->lowoffsetcode.table);
2011 free(rar->lengthcode.table);
2012 memset(&rar->maincode, 0, sizeof(rar->maincode));
2013 memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
2014 memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
2015 memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
2020 read_next_symbol(struct archive_read *a, struct huffman_code *code)
2024 int length, value, node;
2030 if (make_table(a, code) != (ARCHIVE_OK))
2034 rar = (struct rar *)(a->format->data);
2037 /* Look ahead (peek) at bits */
2038 if (!rar_br_read_ahead(a, br, code->tablesize)) {
2039 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2040 "Truncated RAR file data");
2044 bits = rar_br_bits(br, code->tablesize);
2046 length = code->table[bits].length;
2047 value = code->table[bits].value;
2051 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2052 "Invalid prefix code in bitstream");
2056 if (length <= code->tablesize)
2058 /* Skip length bits */
2059 rar_br_consume(br, length);
2063 /* Skip tablesize bits */
2064 rar_br_consume(br, code->tablesize);
2067 while (!(code->tree[node].branches[0] ==
2068 code->tree[node].branches[1]))
2070 if (!rar_br_read_ahead(a, br, 1)) {
2071 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2072 "Truncated RAR file data");
2076 bit = rar_br_bits(br, 1);
2077 rar_br_consume(br, 1);
2079 if (code->tree[node].branches[bit] < 0)
2081 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2082 "Invalid prefix code in bitstream");
2085 node = code->tree[node].branches[bit];
2088 return code->tree[node].branches[0];
2092 create_code(struct archive_read *a, struct huffman_code *code,
2093 unsigned char *lengths, int numsymbols, char maxlength)
2095 int i, j, codebits = 0, symbolsleft = numsymbols;
2097 if (new_node(code) < 0) {
2098 archive_set_error(&a->archive, ENOMEM,
2099 "Unable to allocate memory for node data.");
2100 return (ARCHIVE_FATAL);
2102 code->numentries = 1;
2103 code->minlength = INT_MAX;
2104 code->maxlength = INT_MIN;
2106 for(i = 1; i <= maxlength; i++)
2108 for(j = 0; j < numsymbols; j++)
2110 if (lengths[j] != i) continue;
2111 if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
2112 return (ARCHIVE_FATAL);
2114 if (--symbolsleft <= 0) { break; break; }
2118 return (ARCHIVE_OK);
2122 add_value(struct archive_read *a, struct huffman_code *code, int value,
2123 int codebits, int length)
2125 int repeatpos, lastnode, bitpos, bit, repeatnode, nextnode;
2130 if(length > code->maxlength)
2131 code->maxlength = length;
2132 if(length < code->minlength)
2133 code->minlength = length;
2136 if (repeatpos == 0 || (repeatpos >= 0
2137 && (((codebits >> (repeatpos - 1)) & 3) == 0
2138 || ((codebits >> (repeatpos - 1)) & 3) == 3)))
2140 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2141 "Invalid repeat position");
2142 return (ARCHIVE_FATAL);
2146 for (bitpos = length - 1; bitpos >= 0; bitpos--)
2148 bit = (codebits >> bitpos) & 1;
2150 /* Leaf node check */
2151 if (code->tree[lastnode].branches[0] ==
2152 code->tree[lastnode].branches[1])
2154 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2156 return (ARCHIVE_FATAL);
2159 if (bitpos == repeatpos)
2161 /* Open branch check */
2162 if (!(code->tree[lastnode].branches[bit] < 0))
2164 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2165 "Invalid repeating code");
2166 return (ARCHIVE_FATAL);
2169 if ((repeatnode = new_node(code)) < 0) {
2170 archive_set_error(&a->archive, ENOMEM,
2171 "Unable to allocate memory for node data.");
2172 return (ARCHIVE_FATAL);
2174 if ((nextnode = new_node(code)) < 0) {
2175 archive_set_error(&a->archive, ENOMEM,
2176 "Unable to allocate memory for node data.");
2177 return (ARCHIVE_FATAL);
2181 code->tree[lastnode].branches[bit] = repeatnode;
2182 code->tree[repeatnode].branches[bit] = repeatnode;
2183 code->tree[repeatnode].branches[bit^1] = nextnode;
2184 lastnode = nextnode;
2186 bitpos++; /* terminating bit already handled, skip it */
2190 /* Open branch check */
2191 if (code->tree[lastnode].branches[bit] < 0)
2193 if (new_node(code) < 0) {
2194 archive_set_error(&a->archive, ENOMEM,
2195 "Unable to allocate memory for node data.");
2196 return (ARCHIVE_FATAL);
2198 code->tree[lastnode].branches[bit] = code->numentries++;
2202 lastnode = code->tree[lastnode].branches[bit];
2206 if (!(code->tree[lastnode].branches[0] == -1
2207 && code->tree[lastnode].branches[1] == -2))
2209 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2211 return (ARCHIVE_FATAL);
2214 /* Set leaf value */
2215 code->tree[lastnode].branches[0] = value;
2216 code->tree[lastnode].branches[1] = value;
2218 return (ARCHIVE_OK);
2222 new_node(struct huffman_code *code)
2224 code->tree = (struct huffman_tree_node *)realloc(code->tree,
2225 (code->numentries + 1) * sizeof(*code->tree));
2226 if (code->tree == NULL)
2228 code->tree[code->numentries].branches[0] = -1;
2229 code->tree[code->numentries].branches[1] = -2;
2234 make_table(struct archive_read *a, struct huffman_code *code)
2236 if (code->maxlength < code->minlength || code->maxlength > 10)
2237 code->tablesize = 10;
2239 code->tablesize = code->maxlength;
2242 (struct huffman_table_entry *)malloc(sizeof(*code->table)
2243 * (1 << code->tablesize));
2245 return make_table_recurse(a, code, 0, code->table, 0, code->tablesize);
2249 make_table_recurse(struct archive_read *a, struct huffman_code *code, int node,
2250 struct huffman_table_entry *table, int depth,
2253 int currtablesize, i, ret = (ARCHIVE_OK);
2257 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2258 "Huffman tree was not created.");
2259 return (ARCHIVE_FATAL);
2261 if (node < 0 || node >= code->numentries)
2263 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2264 "Invalid location to Huffman tree specified.");
2265 return (ARCHIVE_FATAL);
2268 currtablesize = 1 << (maxdepth - depth);
2270 if (code->tree[node].branches[0] ==
2271 code->tree[node].branches[1])
2273 for(i = 0; i < currtablesize; i++)
2275 table[i].length = depth;
2276 table[i].value = code->tree[node].branches[0];
2281 for(i = 0; i < currtablesize; i++)
2282 table[i].length = -1;
2286 if(depth == maxdepth)
2288 table[0].length = maxdepth + 1;
2289 table[0].value = node;
2293 ret |= make_table_recurse(a, code, code->tree[node].branches[0], table,
2294 depth + 1, maxdepth);
2295 ret |= make_table_recurse(a, code, code->tree[node].branches[1],
2296 table + currtablesize / 2, depth + 1, maxdepth);
2303 expand(struct archive_read *a, int64_t end)
2305 static const unsigned char lengthbases[] =
2306 { 0, 1, 2, 3, 4, 5, 6,
2307 7, 8, 10, 12, 14, 16, 20,
2308 24, 28, 32, 40, 48, 56, 64,
2309 80, 96, 112, 128, 160, 192, 224 };
2310 static const unsigned char lengthbits[] =
2311 { 0, 0, 0, 0, 0, 0, 0,
2312 0, 1, 1, 1, 1, 2, 2,
2313 2, 2, 3, 3, 3, 3, 4,
2314 4, 4, 4, 5, 5, 5, 5 };
2315 static const unsigned int offsetbases[] =
2317 8, 12, 16, 24, 32, 48,
2318 64, 96, 128, 192, 256, 384,
2319 512, 768, 1024, 1536, 2048, 3072,
2320 4096, 6144, 8192, 12288, 16384, 24576,
2321 32768, 49152, 65536, 98304, 131072, 196608,
2322 262144, 327680, 393216, 458752, 524288, 589824,
2323 655360, 720896, 786432, 851968, 917504, 983040,
2324 1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
2325 2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
2326 static const unsigned char offsetbits[] =
2327 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
2328 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
2329 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
2330 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2331 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
2332 static const unsigned char shortbases[] =
2333 { 0, 4, 8, 16, 32, 64, 128, 192 };
2334 static const unsigned char shortbits[] =
2335 { 2, 2, 3, 4, 5, 6, 6, 6 };
2337 int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
2338 unsigned char newfile;
2339 struct rar *rar = (struct rar *)(a->format->data);
2340 struct rar_br *br = &(rar->br);
2342 if (rar->filterstart < end)
2343 end = rar->filterstart;
2347 if (rar->output_last_match &&
2348 lzss_position(&rar->lzss) + rar->lastlength <= end)
2350 lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
2351 rar->output_last_match = 0;
2354 if(rar->is_ppmd_block || rar->output_last_match ||
2355 lzss_position(&rar->lzss) >= end)
2356 return lzss_position(&rar->lzss);
2358 if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
2359 return (ARCHIVE_FATAL);
2360 rar->output_last_match = 0;
2364 lzss_emit_literal(rar, symbol);
2367 else if (symbol == 256)
2369 if (!rar_br_read_ahead(a, br, 1))
2370 goto truncated_data;
2371 newfile = !rar_br_bits(br, 1);
2372 rar_br_consume(br, 1);
2376 rar->start_new_block = 1;
2377 if (!rar_br_read_ahead(a, br, 1))
2378 goto truncated_data;
2379 rar->start_new_table = rar_br_bits(br, 1);
2380 rar_br_consume(br, 1);
2381 return lzss_position(&rar->lzss);
2385 if (parse_codes(a) != ARCHIVE_OK)
2386 return (ARCHIVE_FATAL);
2390 else if(symbol==257)
2392 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2393 "Parsing filters is unsupported.");
2394 return (ARCHIVE_FAILED);
2396 else if(symbol==258)
2398 if(rar->lastlength == 0)
2401 offs = rar->lastoffset;
2402 len = rar->lastlength;
2404 else if (symbol <= 262)
2406 offsindex = symbol - 259;
2407 offs = rar->oldoffset[offsindex];
2409 if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
2411 if (lensymbol > sizeof(lengthbases)/sizeof(lengthbases[0]))
2413 if (lensymbol > sizeof(lengthbits)/sizeof(lengthbits[0]))
2415 len = lengthbases[lensymbol] + 2;
2416 if (lengthbits[lensymbol] > 0) {
2417 if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
2418 goto truncated_data;
2419 len += rar_br_bits(br, lengthbits[lensymbol]);
2420 rar_br_consume(br, lengthbits[lensymbol]);
2423 for (i = offsindex; i > 0; i--)
2424 rar->oldoffset[i] = rar->oldoffset[i-1];
2425 rar->oldoffset[0] = offs;
2427 else if(symbol<=270)
2429 offs = shortbases[symbol-263] + 1;
2430 if(shortbits[symbol-263] > 0) {
2431 if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
2432 goto truncated_data;
2433 offs += rar_br_bits(br, shortbits[symbol-263]);
2434 rar_br_consume(br, shortbits[symbol-263]);
2439 for(i = 3; i > 0; i--)
2440 rar->oldoffset[i] = rar->oldoffset[i-1];
2441 rar->oldoffset[0] = offs;
2445 if (symbol-271 > sizeof(lengthbases)/sizeof(lengthbases[0]))
2447 if (symbol-271 > sizeof(lengthbits)/sizeof(lengthbits[0]))
2449 len = lengthbases[symbol-271]+3;
2450 if(lengthbits[symbol-271] > 0) {
2451 if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
2452 goto truncated_data;
2453 len += rar_br_bits(br, lengthbits[symbol-271]);
2454 rar_br_consume(br, lengthbits[symbol-271]);
2457 if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
2459 if (offssymbol > sizeof(offsetbases)/sizeof(offsetbases[0]))
2461 if (offssymbol > sizeof(offsetbits)/sizeof(offsetbits[0]))
2463 offs = offsetbases[offssymbol]+1;
2464 if(offsetbits[offssymbol] > 0)
2468 if(offsetbits[offssymbol] > 4) {
2469 if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
2470 goto truncated_data;
2471 offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
2472 rar_br_consume(br, offsetbits[offssymbol] - 4);
2475 if(rar->numlowoffsetrepeats > 0)
2477 rar->numlowoffsetrepeats--;
2478 offs += rar->lastlowoffset;
2482 if ((lowoffsetsymbol =
2483 read_next_symbol(a, &rar->lowoffsetcode)) < 0)
2484 return (ARCHIVE_FATAL);
2485 if(lowoffsetsymbol == 16)
2487 rar->numlowoffsetrepeats = 15;
2488 offs += rar->lastlowoffset;
2492 offs += lowoffsetsymbol;
2493 rar->lastlowoffset = lowoffsetsymbol;
2498 if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
2499 goto truncated_data;
2500 offs += rar_br_bits(br, offsetbits[offssymbol]);
2501 rar_br_consume(br, offsetbits[offssymbol]);
2505 if (offs >= 0x40000)
2510 for(i = 3; i > 0; i--)
2511 rar->oldoffset[i] = rar->oldoffset[i-1];
2512 rar->oldoffset[0] = offs;
2515 rar->lastoffset = offs;
2516 rar->lastlength = len;
2517 rar->output_last_match = 1;
2520 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2521 "Truncated RAR file data");
2523 return (ARCHIVE_FATAL);
2525 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2526 "Bad RAR file data");
2527 return (ARCHIVE_FATAL);
2531 copy_from_lzss_window(struct archive_read *a, const void **buffer,
2532 int64_t startpos, int length)
2534 int windowoffs, firstpart;
2535 struct rar *rar = (struct rar *)(a->format->data);
2537 if (!rar->unp_buffer)
2539 if ((rar->unp_buffer = malloc(rar->unp_buffer_size)) == NULL)
2541 archive_set_error(&a->archive, ENOMEM,
2542 "Unable to allocate memory for uncompressed data.");
2543 return (ARCHIVE_FATAL);
2547 windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
2548 if(windowoffs + length <= lzss_size(&rar->lzss))
2549 memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
2553 firstpart = lzss_size(&rar->lzss) - windowoffs;
2554 if (firstpart < 0) {
2555 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2556 "Bad RAR file data");
2557 return (ARCHIVE_FATAL);
2559 if (firstpart < length) {
2560 memcpy(&rar->unp_buffer[rar->unp_offset],
2561 &rar->lzss.window[windowoffs], firstpart);
2562 memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
2563 &rar->lzss.window[0], length - firstpart);
2565 memcpy(&rar->unp_buffer[rar->unp_offset],
2566 &rar->lzss.window[windowoffs], length);
2568 rar->unp_offset += length;
2569 if (rar->unp_offset >= rar->unp_buffer_size)
2570 *buffer = rar->unp_buffer;
2573 return (ARCHIVE_OK);