2 * Copyright (c) 2018 Grzegorz Antoniak (http://antoniak.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 #include "archive_endian.h"
34 #include <zlib.h> /* crc32 */
42 #include "archive_crc32.h"
45 #include "archive_entry.h"
46 #include "archive_entry_locale.h"
47 #include "archive_ppmd7_private.h"
48 #include "archive_entry_private.h"
53 #include "archive_blake2.h"
56 /*#define CHECK_CRC_ON_SOLID_SKIP*/
57 /*#define DONT_FAIL_ON_CRC_ERROR*/
60 #define rar5_min(a, b) (((a) > (b)) ? (b) : (a))
61 #define rar5_max(a, b) (((a) > (b)) ? (a) : (b))
62 #define rar5_countof(X) ((const ssize_t) (sizeof(X) / sizeof(*X)))
65 #define DEBUG_CODE if(1)
67 #define DEBUG_CODE if(0)
70 /* Real RAR5 magic number is:
72 * 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00
75 * It's stored in `rar5_signature` after XOR'ing it with 0xA1, because I don't
76 * want to put this magic sequence in each binary that uses libarchive, so
77 * applications that scan through the file for this marker won't trigger on
80 * The array itself is decrypted in `rar5_init` function. */
82 static unsigned char rar5_signature[] = { 243, 192, 211, 128, 187, 166, 160, 161 };
83 static const ssize_t rar5_signature_size = sizeof(rar5_signature);
84 static const size_t g_unpack_window_size = 0x20000;
86 /* These could have been static const's, but they aren't, because of
88 #define MAX_NAME_IN_CHARS 2048
89 #define MAX_NAME_IN_BYTES (4 * MAX_NAME_IN_CHARS)
92 ssize_t bytes_remaining;
93 ssize_t unpacked_size;
94 int64_t last_offset; /* Used in sanity checks. */
95 int64_t last_size; /* Used in sanity checks. */
97 uint8_t solid : 1; /* Is this a solid stream? */
98 uint8_t service : 1; /* Is this file a service data? */
99 uint8_t eof : 1; /* Did we finish unpacking the file? */
100 uint8_t dir : 1; /* Is this file entry a directory? */
102 /* Optional time fields. */
108 /* Optional hash fields. */
109 uint32_t stored_crc32;
110 uint32_t calculated_crc32;
111 uint8_t blake2sp[32];
112 blake2sp_state b2state;
115 /* Optional redir fields */
117 uint64_t redir_flags;
130 #define REDIR_SYMLINK_IS_DIR 1
134 REDIR_TYPE_UNIXSYMLINK = 1,
135 REDIR_TYPE_WINSYMLINK = 2,
136 REDIR_TYPE_JUNCTION = 3,
137 REDIR_TYPE_HARDLINK = 4,
138 REDIR_TYPE_FILECOPY = 5,
141 #define OWNER_USER_NAME 0x01
142 #define OWNER_GROUP_NAME 0x02
143 #define OWNER_USER_UID 0x04
144 #define OWNER_GROUP_GID 0x08
145 #define OWNER_MAXNAMELEN 256
148 FILTER_DELTA = 0, /* Generic pattern. */
149 FILTER_E8 = 1, /* Intel x86 code. */
150 FILTER_E8E9 = 2, /* Intel x86 code. */
151 FILTER_ARM = 3, /* ARM code. */
152 FILTER_AUDIO = 4, /* Audio filter, not used in RARv5. */
153 FILTER_RGB = 5, /* Color palette, not used in RARv5. */
154 FILTER_ITANIUM = 6, /* Intel's Itanium, not used in RARv5. */
155 FILTER_PPM = 7, /* Predictive pattern matching, not used in
166 ssize_t block_length;
185 struct decode_table {
187 int32_t decode_len[16];
188 uint32_t decode_pos[16];
190 uint8_t quick_len[1 << 10];
191 uint16_t quick_num[1 << 10];
192 uint16_t decode_num[306];
196 /* Flag used to specify if unpacker needs to reinitialize the
197 uncompression context. */
198 uint8_t initialized : 1;
200 /* Flag used when applying filters. */
201 uint8_t all_filters_applied : 1;
203 /* Flag used to skip file context reinitialization, used when unpacker
204 is skipping through different multivolume archives. */
205 uint8_t switch_multivolume : 1;
207 /* Flag used to specify if unpacker has processed the whole data block
208 or just a part of it. */
209 uint8_t block_parsing_finished : 1;
213 int flags; /* Uncompression flags. */
214 int method; /* Uncompression algorithm method. */
215 int version; /* Uncompression algorithm version. */
216 ssize_t window_size; /* Size of window_buf. */
217 uint8_t* window_buf; /* Circular buffer used during
219 uint8_t* filtered_buf; /* Buffer used when applying filters. */
220 const uint8_t* block_buf; /* Buffer used when merging blocks. */
221 size_t window_mask; /* Convenience field; window_size - 1. */
222 int64_t write_ptr; /* This amount of data has been unpacked
223 in the window buffer. */
224 int64_t last_write_ptr; /* This amount of data has been stored in
226 int64_t last_unstore_ptr; /* Counter of bytes extracted during
227 unstoring. This is separate from
228 last_write_ptr because of how SERVICE
229 base blocks are handled during skipping
230 in solid multiarchive archives. */
231 int64_t solid_offset; /* Additional offset inside the window
232 buffer, used in unpacking solid
234 ssize_t cur_block_size; /* Size of current data block. */
235 int last_len; /* Flag used in lzss decompression. */
237 /* Decode tables used during lzss uncompression. */
240 struct decode_table bd; /* huffman bit lengths */
242 struct decode_table ld; /* literals */
244 struct decode_table dd; /* distances */
246 struct decode_table ldd; /* lower bits of distances */
248 struct decode_table rd; /* repeating distances */
249 #define HUFF_TABLE_SIZE (HUFF_NC + HUFF_DC + HUFF_RC + HUFF_LDC)
251 /* Circular deque for storing filters. */
252 struct cdeque filters;
253 int64_t last_block_start; /* Used for sanity checking. */
254 ssize_t last_block_length; /* Used for sanity checking. */
256 /* Distance cache used during lzss uncompression. */
259 /* Data buffer stack. */
260 struct data_ready dready[2];
263 /* Bit reader state. */
265 int8_t bit_addr; /* Current bit pointer inside current byte. */
266 int in_addr; /* Current byte pointer. */
269 /* RARv5 block header structure. Use bf_* functions to get values from
270 * block_flags_u8 field. I.e. bf_byte_count, etc. */
271 struct compressed_block_header {
272 /* block_flags_u8 contain fields encoded in little-endian bitfield:
274 * - table present flag (shr 7, and 1),
275 * - last block flag (shr 6, and 1),
276 * - byte_count (shr 3, and 7),
277 * - bit_size (shr 0, and 7).
279 uint8_t block_flags_u8;
283 /* RARv5 main header structure. */
285 /* Does the archive contain solid streams? */
288 /* If this a multi-file archive? */
296 struct generic_header {
297 uint8_t split_after : 1;
298 uint8_t split_before : 1;
305 unsigned int expected_vol_no;
309 /* Main context structure. */
311 int header_initialized;
313 /* Set to 1 if current file is positioned AFTER the magic value
314 * of the archive file. This is used in header reading functions. */
317 /* Set to not zero if we're in skip mode (either by calling
318 * rar5_data_skip function or when skipping over solid streams).
319 * Set to 0 when in * extraction mode. This is used during checksum
320 * calculation functions. */
323 /* Set to not zero if we're in block merging mode (i.e. when switching
324 * to another file in multivolume archive, last block from 1st archive
325 * needs to be merged with 1st block from 2nd archive). This flag
326 * guards against recursive use of the merging function, which doesn't
327 * support recursive calls. */
330 /* An offset to QuickOpen list. This is not supported by this unpacker,
331 * because we're focusing on streaming interface. QuickOpen is designed
332 * to make things quicker for non-stream interfaces, so it's not our
334 uint64_t qlist_offset;
336 /* An offset to additional Recovery data. This is not supported by this
337 * unpacker. Recovery data are additional Reed-Solomon codes that could
338 * be used to calculate bytes that are missing in archive or are
342 /* Various context variables grouped to different structures. */
343 struct generic_header generic;
344 struct main_header main;
345 struct comp_state cstate;
346 struct file_header file;
347 struct bit_reader bits;
348 struct multivolume vol;
350 /* The header of currently processed RARv5 block. Used in main
351 * decompression logic loop. */
352 struct compressed_block_header last_block_hdr;
355 /* Forward function declarations. */
357 static int verify_global_checksums(struct archive_read* a);
358 static int rar5_read_data_skip(struct archive_read *a);
359 static int push_data_ready(struct archive_read* a, struct rar5* rar,
360 const uint8_t* buf, size_t size, int64_t offset);
362 /* CDE_xxx = Circular Double Ended (Queue) return values. */
363 enum CDE_RETURN_VALUES {
364 CDE_OK, CDE_ALLOC, CDE_PARAM, CDE_OUT_OF_BOUNDS,
367 /* Clears the contents of this circular deque. */
368 static void cdeque_clear(struct cdeque* d) {
374 /* Creates a new circular deque object. Capacity must be power of 2: 8, 16, 32,
375 * 64, 256, etc. When the user will add another item above current capacity,
376 * the circular deque will overwrite the oldest entry. */
377 static int cdeque_init(struct cdeque* d, int max_capacity_power_of_2) {
378 if(d == NULL || max_capacity_power_of_2 == 0)
381 d->cap_mask = max_capacity_power_of_2 - 1;
384 if((max_capacity_power_of_2 & d->cap_mask) > 0)
388 d->arr = malloc(sizeof(void*) * max_capacity_power_of_2);
390 return d->arr ? CDE_OK : CDE_ALLOC;
393 /* Return the current size (not capacity) of circular deque `d`. */
394 static size_t cdeque_size(struct cdeque* d) {
398 /* Returns the first element of current circular deque. Note that this function
399 * doesn't perform any bounds checking. If you need bounds checking, use
400 * `cdeque_front()` function instead. */
401 static void cdeque_front_fast(struct cdeque* d, void** value) {
402 *value = (void*) d->arr[d->beg_pos];
405 /* Returns the first element of current circular deque. This function
406 * performs bounds checking. */
407 static int cdeque_front(struct cdeque* d, void** value) {
409 cdeque_front_fast(d, value);
412 return CDE_OUT_OF_BOUNDS;
415 /* Pushes a new element into the end of this circular deque object. If current
416 * size will exceed capacity, the oldest element will be overwritten. */
417 static int cdeque_push_back(struct cdeque* d, void* item) {
421 if(d->size == d->cap_mask + 1)
422 return CDE_OUT_OF_BOUNDS;
424 d->arr[d->end_pos] = (size_t) item;
425 d->end_pos = (d->end_pos + 1) & d->cap_mask;
431 /* Pops a front element of this circular deque object and returns its value.
432 * This function doesn't perform any bounds checking. */
433 static void cdeque_pop_front_fast(struct cdeque* d, void** value) {
434 *value = (void*) d->arr[d->beg_pos];
435 d->beg_pos = (d->beg_pos + 1) & d->cap_mask;
439 /* Pops a front element of this circular deque object and returns its value.
440 * This function performs bounds checking. */
441 static int cdeque_pop_front(struct cdeque* d, void** value) {
446 return CDE_OUT_OF_BOUNDS;
448 cdeque_pop_front_fast(d, value);
452 /* Convenience function to cast filter_info** to void **. */
453 static void** cdeque_filter_p(struct filter_info** f) {
454 return (void**) (size_t) f;
457 /* Convenience function to cast filter_info* to void *. */
458 static void* cdeque_filter(struct filter_info* f) {
459 return (void**) (size_t) f;
462 /* Destroys this circular deque object. Deallocates the memory of the
463 * collection buffer, but doesn't deallocate the memory of any pointer passed
464 * to this deque as a value. */
465 static void cdeque_free(struct cdeque* d) {
481 uint8_t bf_bit_size(const struct compressed_block_header* hdr) {
482 return hdr->block_flags_u8 & 7;
486 uint8_t bf_byte_count(const struct compressed_block_header* hdr) {
487 return (hdr->block_flags_u8 >> 3) & 7;
491 uint8_t bf_is_table_present(const struct compressed_block_header* hdr) {
492 return (hdr->block_flags_u8 >> 7) & 1;
495 static inline struct rar5* get_context(struct archive_read* a) {
496 return (struct rar5*) a->format->data;
499 /* Convenience functions used by filter implementations. */
500 static void circular_memcpy(uint8_t* dst, uint8_t* window, const uint64_t mask,
501 int64_t start, int64_t end)
503 if((start & mask) > (end & mask)) {
504 ssize_t len1 = mask + 1 - (start & mask);
505 ssize_t len2 = end & mask;
507 memcpy(dst, &window[start & mask], len1);
508 memcpy(dst + len1, window, len2);
510 memcpy(dst, &window[start & mask], (size_t) (end - start));
514 static uint32_t read_filter_data(struct rar5* rar, uint32_t offset) {
515 uint8_t linear_buf[4];
516 circular_memcpy(linear_buf, rar->cstate.window_buf,
517 rar->cstate.window_mask, offset, offset + 4);
518 return archive_le32dec(linear_buf);
521 static void write_filter_data(struct rar5* rar, uint32_t offset,
524 archive_le32enc(&rar->cstate.filtered_buf[offset], value);
527 /* Allocates a new filter descriptor and adds it to the filter array. */
528 static struct filter_info* add_new_filter(struct rar5* rar) {
529 struct filter_info* f =
530 (struct filter_info*) calloc(1, sizeof(struct filter_info));
536 cdeque_push_back(&rar->cstate.filters, cdeque_filter(f));
540 static int run_delta_filter(struct rar5* rar, struct filter_info* flt) {
542 ssize_t dest_pos, src_pos = 0;
544 for(i = 0; i < flt->channels; i++) {
545 uint8_t prev_byte = 0;
547 dest_pos < flt->block_length;
548 dest_pos += flt->channels)
552 byte = rar->cstate.window_buf[
553 (rar->cstate.solid_offset + flt->block_start +
554 src_pos) & rar->cstate.window_mask];
557 rar->cstate.filtered_buf[dest_pos] = prev_byte;
565 static int run_e8e9_filter(struct rar5* rar, struct filter_info* flt,
568 const uint32_t file_size = 0x1000000;
571 circular_memcpy(rar->cstate.filtered_buf,
572 rar->cstate.window_buf, rar->cstate.window_mask,
573 rar->cstate.solid_offset + flt->block_start,
574 rar->cstate.solid_offset + flt->block_start + flt->block_length);
576 for(i = 0; i < flt->block_length - 4;) {
577 uint8_t b = rar->cstate.window_buf[
578 (rar->cstate.solid_offset + flt->block_start +
579 i++) & rar->cstate.window_mask];
582 * 0xE8 = x86's call <relative_addr_uint32> (function call)
583 * 0xE9 = x86's jmp <relative_addr_uint32> (unconditional jump)
585 if(b == 0xE8 || (extended && b == 0xE9)) {
588 uint32_t offset = (i + flt->block_start) % file_size;
590 addr = read_filter_data(rar,
591 (uint32_t)(rar->cstate.solid_offset +
592 flt->block_start + i) & rar->cstate.window_mask);
594 if(addr & 0x80000000) {
595 if(((addr + offset) & 0x80000000) == 0) {
596 write_filter_data(rar, (uint32_t)i,
600 if((addr - file_size) & 0x80000000) {
601 uint32_t naddr = addr - offset;
602 write_filter_data(rar, (uint32_t)i,
614 static int run_arm_filter(struct rar5* rar, struct filter_info* flt) {
618 circular_memcpy(rar->cstate.filtered_buf,
619 rar->cstate.window_buf, rar->cstate.window_mask,
620 rar->cstate.solid_offset + flt->block_start,
621 rar->cstate.solid_offset + flt->block_start + flt->block_length);
623 for(i = 0; i < flt->block_length - 3; i += 4) {
624 uint8_t* b = &rar->cstate.window_buf[
625 (rar->cstate.solid_offset +
626 flt->block_start + i) & rar->cstate.window_mask];
629 /* 0xEB = ARM's BL (branch + link) instruction. */
630 offset = read_filter_data(rar,
631 (rar->cstate.solid_offset + flt->block_start + i) &
632 rar->cstate.window_mask) & 0x00ffffff;
634 offset -= (uint32_t) ((i + flt->block_start) / 4);
635 offset = (offset & 0x00ffffff) | 0xeb000000;
636 write_filter_data(rar, (uint32_t)i, offset);
643 static int run_filter(struct archive_read* a, struct filter_info* flt) {
645 struct rar5* rar = get_context(a);
647 free(rar->cstate.filtered_buf);
649 rar->cstate.filtered_buf = malloc(flt->block_length);
650 if(!rar->cstate.filtered_buf) {
651 archive_set_error(&a->archive, ENOMEM,
652 "Can't allocate memory for filter data.");
653 return ARCHIVE_FATAL;
658 ret = run_delta_filter(rar, flt);
664 ret = run_e8e9_filter(rar, flt,
665 flt->type == FILTER_E8E9);
669 ret = run_arm_filter(rar, flt);
673 archive_set_error(&a->archive,
674 ARCHIVE_ERRNO_FILE_FORMAT,
675 "Unsupported filter type: 0x%x", flt->type);
676 return ARCHIVE_FATAL;
679 if(ret != ARCHIVE_OK) {
680 /* Filter has failed. */
684 if(ARCHIVE_OK != push_data_ready(a, rar, rar->cstate.filtered_buf,
685 flt->block_length, rar->cstate.last_write_ptr))
687 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
688 "Stack overflow when submitting unpacked data");
690 return ARCHIVE_FATAL;
693 rar->cstate.last_write_ptr += flt->block_length;
697 /* The `push_data` function submits the selected data range to the user.
698 * Next call of `use_data` will use the pointer, size and offset arguments
699 * that are specified here. These arguments are pushed to the FIFO stack here,
700 * and popped from the stack by the `use_data` function. */
701 static void push_data(struct archive_read* a, struct rar5* rar,
702 const uint8_t* buf, int64_t idx_begin, int64_t idx_end)
704 const uint64_t wmask = rar->cstate.window_mask;
705 const ssize_t solid_write_ptr = (rar->cstate.solid_offset +
706 rar->cstate.last_write_ptr) & wmask;
708 idx_begin += rar->cstate.solid_offset;
709 idx_end += rar->cstate.solid_offset;
711 /* Check if our unpacked data is wrapped inside the window circular
712 * buffer. If it's not wrapped, it can be copied out by using
713 * a single memcpy, but when it's wrapped, we need to copy the first
714 * part with one memcpy, and the second part with another memcpy. */
716 if((idx_begin & wmask) > (idx_end & wmask)) {
717 /* The data is wrapped (begin offset sis bigger than end
719 const ssize_t frag1_size = rar->cstate.window_size -
721 const ssize_t frag2_size = idx_end & wmask;
723 /* Copy the first part of the buffer first. */
724 push_data_ready(a, rar, buf + solid_write_ptr, frag1_size,
725 rar->cstate.last_write_ptr);
727 /* Copy the second part of the buffer. */
728 push_data_ready(a, rar, buf, frag2_size,
729 rar->cstate.last_write_ptr + frag1_size);
731 rar->cstate.last_write_ptr += frag1_size + frag2_size;
733 /* Data is not wrapped, so we can just use one call to copy the
735 push_data_ready(a, rar,
736 buf + solid_write_ptr, (idx_end - idx_begin) & wmask,
737 rar->cstate.last_write_ptr);
739 rar->cstate.last_write_ptr += idx_end - idx_begin;
743 /* Convenience function that submits the data to the user. It uses the
744 * unpack window buffer as a source location. */
745 static void push_window_data(struct archive_read* a, struct rar5* rar,
746 int64_t idx_begin, int64_t idx_end)
748 push_data(a, rar, rar->cstate.window_buf, idx_begin, idx_end);
751 static int apply_filters(struct archive_read* a) {
752 struct filter_info* flt;
753 struct rar5* rar = get_context(a);
756 rar->cstate.all_filters_applied = 0;
758 /* Get the first filter that can be applied to our data. The data
759 * needs to be fully unpacked before the filter can be run. */
760 if(CDE_OK == cdeque_front(&rar->cstate.filters,
761 cdeque_filter_p(&flt))) {
762 /* Check if our unpacked data fully covers this filter's
764 if(rar->cstate.write_ptr > flt->block_start &&
765 rar->cstate.write_ptr >= flt->block_start +
767 /* Check if we have some data pending to be written
768 * right before the filter's start offset. */
769 if(rar->cstate.last_write_ptr == flt->block_start) {
770 /* Run the filter specified by descriptor
772 ret = run_filter(a, flt);
773 if(ret != ARCHIVE_OK) {
774 /* Filter failure, return error. */
778 /* Filter descriptor won't be needed anymore
779 * after it's used, * so remove it from the
780 * filter list and free its memory. */
781 (void) cdeque_pop_front(&rar->cstate.filters,
782 cdeque_filter_p(&flt));
786 /* We can't run filters yet, dump the memory
787 * right before the filter. */
788 push_window_data(a, rar,
789 rar->cstate.last_write_ptr,
793 /* Return 'filter applied or not needed' state to the
795 return ARCHIVE_RETRY;
799 rar->cstate.all_filters_applied = 1;
803 static void dist_cache_push(struct rar5* rar, int value) {
804 int* q = rar->cstate.dist_cache;
812 static int dist_cache_touch(struct rar5* rar, int idx) {
813 int* q = rar->cstate.dist_cache;
814 int i, dist = q[idx];
816 for(i = idx; i > 0; i--)
823 static void free_filters(struct rar5* rar) {
824 struct cdeque* d = &rar->cstate.filters;
826 /* Free any remaining filters. All filters should be naturally
827 * consumed by the unpacking function, so remaining filters after
828 * unpacking normally mean that unpacking wasn't successful.
829 * But still of course we shouldn't leak memory in such case. */
831 /* cdeque_size() is a fast operation, so we can use it as a loop
833 while(cdeque_size(d) > 0) {
834 struct filter_info* f = NULL;
836 /* Pop_front will also decrease the collection's size. */
837 if (CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)))
843 /* Also clear out the variables needed for sanity checking. */
844 rar->cstate.last_block_start = 0;
845 rar->cstate.last_block_length = 0;
848 static void reset_file_context(struct rar5* rar) {
849 memset(&rar->file, 0, sizeof(rar->file));
850 blake2sp_init(&rar->file.b2state, 32);
852 if(rar->main.solid) {
853 rar->cstate.solid_offset += rar->cstate.write_ptr;
855 rar->cstate.solid_offset = 0;
858 rar->cstate.write_ptr = 0;
859 rar->cstate.last_write_ptr = 0;
860 rar->cstate.last_unstore_ptr = 0;
862 rar->file.redir_type = REDIR_TYPE_NONE;
863 rar->file.redir_flags = 0;
868 static inline int get_archive_read(struct archive* a,
869 struct archive_read** ar)
871 *ar = (struct archive_read*) a;
872 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
873 "archive_read_support_format_rar5");
878 static int read_ahead(struct archive_read* a, size_t how_many,
885 *ptr = __archive_read_ahead(a, how_many, &avail);
893 static int consume(struct archive_read* a, int64_t how_many) {
896 ret = how_many == __archive_read_consume(a, how_many)
904 * Read a RAR5 variable sized numeric value. This value will be stored in
905 * `pvalue`. The `pvalue_len` argument points to a variable that will receive
906 * the byte count that was consumed in order to decode the `pvalue` value, plus
909 * pvalue_len is optional and can be NULL.
911 * NOTE: if `pvalue_len` is NOT NULL, the caller needs to manually consume
912 * the number of bytes that `pvalue_len` value contains. If the `pvalue_len`
913 * is NULL, this consuming operation is done automatically.
915 * Returns 1 if *pvalue was successfully read.
916 * Returns 0 if there was an error. In this case, *pvalue contains an
920 static int read_var(struct archive_read* a, uint64_t* pvalue,
921 uint64_t* pvalue_len)
928 /* We will read maximum of 8 bytes. We don't have to handle the
929 * situation to read the RAR5 variable-sized value stored at the end of
930 * the file, because such situation will never happen. */
931 if(!read_ahead(a, 8, &p))
934 for(shift = 0, i = 0; i < 8; i++, shift += 7) {
937 /* Strip the MSB from the input byte and add the resulting
938 * number to the `result`. */
939 result += (b & (uint64_t)0x7F) << shift;
941 /* MSB set to 1 means we need to continue decoding process.
942 * MSB set to 0 means we're done.
944 * This conditional checks for the second case. */
945 if((b & 0x80) == 0) {
950 /* If the caller has passed the `pvalue_len` pointer,
951 * store the number of consumed bytes in it and do NOT
952 * consume those bytes, since the caller has all the
953 * information it needs to perform */
957 /* If the caller did not provide the
958 * `pvalue_len` pointer, it will not have the
959 * possibility to advance the file pointer,
960 * because it will not know how many bytes it
961 * needs to consume. This is why we handle
962 * such situation here automatically. */
963 if(ARCHIVE_OK != consume(a, 1 + i)) {
968 /* End of decoding process, return success. */
973 /* The decoded value takes the maximum number of 8 bytes.
974 * It's a maximum number of bytes, so end decoding process here
975 * even if the first bit of last byte is 1. */
983 if(ARCHIVE_OK != consume(a, 9)) {
991 static int read_var_sized(struct archive_read* a, size_t* pvalue,
997 const int ret = pvalue_len ? read_var(a, &v, &v_size)
998 : read_var(a, &v, NULL);
1000 if(ret == 1 && pvalue) {
1001 *pvalue = (size_t) v;
1005 /* Possible data truncation should be safe. */
1006 *pvalue_len = (size_t) v_size;
1012 static int read_bits_32(struct rar5* rar, const uint8_t* p, uint32_t* value) {
1013 uint32_t bits = ((uint32_t) p[rar->bits.in_addr]) << 24;
1014 bits |= p[rar->bits.in_addr + 1] << 16;
1015 bits |= p[rar->bits.in_addr + 2] << 8;
1016 bits |= p[rar->bits.in_addr + 3];
1017 bits <<= rar->bits.bit_addr;
1018 bits |= p[rar->bits.in_addr + 4] >> (8 - rar->bits.bit_addr);
1023 static int read_bits_16(struct rar5* rar, const uint8_t* p, uint16_t* value) {
1024 int bits = (int) ((uint32_t) p[rar->bits.in_addr]) << 16;
1025 bits |= (int) p[rar->bits.in_addr + 1] << 8;
1026 bits |= (int) p[rar->bits.in_addr + 2];
1027 bits >>= (8 - rar->bits.bit_addr);
1028 *value = bits & 0xffff;
1032 static void skip_bits(struct rar5* rar, int bits) {
1033 const int new_bits = rar->bits.bit_addr + bits;
1034 rar->bits.in_addr += new_bits >> 3;
1035 rar->bits.bit_addr = new_bits & 7;
1039 static int read_consume_bits(struct rar5* rar, const uint8_t* p, int n,
1045 if(n == 0 || n > 16) {
1046 /* This is a programmer error and should never happen
1048 return ARCHIVE_FATAL;
1051 ret = read_bits_16(rar, p, &v);
1052 if(ret != ARCHIVE_OK)
1066 static int read_u32(struct archive_read* a, uint32_t* pvalue) {
1068 if(!read_ahead(a, 4, &p))
1071 *pvalue = archive_le32dec(p);
1072 return ARCHIVE_OK == consume(a, 4) ? 1 : 0;
1075 static int read_u64(struct archive_read* a, uint64_t* pvalue) {
1077 if(!read_ahead(a, 8, &p))
1080 *pvalue = archive_le64dec(p);
1081 return ARCHIVE_OK == consume(a, 8) ? 1 : 0;
1084 static int bid_standard(struct archive_read* a) {
1087 if(!read_ahead(a, rar5_signature_size, &p))
1090 if(!memcmp(rar5_signature, p, rar5_signature_size))
1096 static int rar5_bid(struct archive_read* a, int best_bid) {
1102 my_bid = bid_standard(a);
1110 static int rar5_options(struct archive_read *a, const char *key,
1116 /* No options supported in this version. Return the ARCHIVE_WARN code
1117 * to signal the options supervisor that the unpacker didn't handle
1118 * setting this option. */
1120 return ARCHIVE_WARN;
1123 static void init_header(struct archive_read* a) {
1124 a->archive.archive_format = ARCHIVE_FORMAT_RAR_V5;
1125 a->archive.archive_format_name = "RAR5";
1129 HFL_EXTRA_DATA = 0x0001,
1131 HFL_SKIP_IF_UNKNOWN = 0x0004,
1132 HFL_SPLIT_BEFORE = 0x0008,
1133 HFL_SPLIT_AFTER = 0x0010,
1135 HFL_INHERITED = 0x0040
1138 static int process_main_locator_extra_block(struct archive_read* a,
1141 uint64_t locator_flags;
1143 if(!read_var(a, &locator_flags, NULL)) {
1147 enum LOCATOR_FLAGS {
1148 QLIST = 0x01, RECOVERY = 0x02,
1151 if(locator_flags & QLIST) {
1152 if(!read_var(a, &rar->qlist_offset, NULL)) {
1156 /* qlist is not used */
1159 if(locator_flags & RECOVERY) {
1160 if(!read_var(a, &rar->rr_offset, NULL)) {
1164 /* rr is not used */
1170 static int parse_file_extra_hash(struct archive_read* a, struct rar5* rar,
1171 ssize_t* extra_data_size)
1176 if(!read_var_sized(a, &hash_type, &value_len))
1179 *extra_data_size -= value_len;
1180 if(ARCHIVE_OK != consume(a, value_len)) {
1188 /* The file uses BLAKE2sp checksum algorithm instead of plain old
1190 if(hash_type == BLAKE2sp) {
1192 const int hash_size = sizeof(rar->file.blake2sp);
1194 if(!read_ahead(a, hash_size, &p))
1197 rar->file.has_blake2 = 1;
1198 memcpy(&rar->file.blake2sp, p, hash_size);
1200 if(ARCHIVE_OK != consume(a, hash_size)) {
1204 *extra_data_size -= hash_size;
1206 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1207 "Unsupported hash type (0x%x)", (int) hash_type);
1208 return ARCHIVE_FATAL;
1214 static uint64_t time_win_to_unix(uint64_t win_time) {
1215 const size_t ns_in_sec = 10000000;
1216 const uint64_t sec_to_unix = 11644473600LL;
1217 return win_time / ns_in_sec - sec_to_unix;
1220 static int parse_htime_item(struct archive_read* a, char unix_time,
1221 uint64_t* where, ssize_t* extra_data_size)
1225 if(!read_u32(a, &time_val))
1228 *extra_data_size -= 4;
1229 *where = (uint64_t) time_val;
1231 uint64_t windows_time;
1232 if(!read_u64(a, &windows_time))
1235 *where = time_win_to_unix(windows_time);
1236 *extra_data_size -= 8;
1242 static int parse_file_extra_version(struct archive_read* a,
1243 struct archive_entry* e, ssize_t* extra_data_size)
1247 size_t value_len = 0;
1248 struct archive_string version_string;
1249 struct archive_string name_utf8_string;
1251 /* Flags are ignored. */
1252 if(!read_var_sized(a, &flags, &value_len))
1255 *extra_data_size -= value_len;
1256 if(ARCHIVE_OK != consume(a, value_len))
1259 if(!read_var_sized(a, &version, &value_len))
1262 *extra_data_size -= value_len;
1263 if(ARCHIVE_OK != consume(a, value_len))
1266 /* extra_data_size should be zero here. */
1268 const char* cur_filename = archive_entry_pathname_utf8(e);
1269 if(cur_filename == NULL) {
1270 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1271 "Version entry without file name");
1272 return ARCHIVE_FATAL;
1275 archive_string_init(&version_string);
1276 archive_string_init(&name_utf8_string);
1278 /* Prepare a ;123 suffix for the filename, where '123' is the version
1279 * value of this file. */
1280 archive_string_sprintf(&version_string, ";%zu", version);
1282 /* Build the new filename. */
1283 archive_strcat(&name_utf8_string, cur_filename);
1284 archive_strcat(&name_utf8_string, version_string.s);
1286 /* Apply the new filename into this file's context. */
1287 archive_entry_update_pathname_utf8(e, name_utf8_string.s);
1290 archive_string_free(&version_string);
1291 archive_string_free(&name_utf8_string);
1295 static int parse_file_extra_htime(struct archive_read* a,
1296 struct archive_entry* e, struct rar5* rar, ssize_t* extra_data_size)
1310 if(!read_var_sized(a, &flags, &value_len))
1313 *extra_data_size -= value_len;
1314 if(ARCHIVE_OK != consume(a, value_len)) {
1318 unix_time = flags & IS_UNIX;
1320 if(flags & HAS_MTIME) {
1321 parse_htime_item(a, unix_time, &rar->file.e_mtime,
1323 archive_entry_set_mtime(e, rar->file.e_mtime, 0);
1326 if(flags & HAS_CTIME) {
1327 parse_htime_item(a, unix_time, &rar->file.e_ctime,
1329 archive_entry_set_ctime(e, rar->file.e_ctime, 0);
1332 if(flags & HAS_ATIME) {
1333 parse_htime_item(a, unix_time, &rar->file.e_atime,
1335 archive_entry_set_atime(e, rar->file.e_atime, 0);
1338 if(flags & HAS_UNIX_NS) {
1339 if(!read_u32(a, &rar->file.e_unix_ns))
1342 *extra_data_size -= 4;
1348 static int parse_file_extra_redir(struct archive_read* a,
1349 struct archive_entry* e, struct rar5* rar, ssize_t* extra_data_size)
1351 uint64_t value_size = 0;
1352 size_t target_size = 0;
1353 char target_utf8_buf[MAX_NAME_IN_BYTES];
1356 if(!read_var(a, &rar->file.redir_type, &value_size))
1358 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1360 *extra_data_size -= value_size;
1362 if(!read_var(a, &rar->file.redir_flags, &value_size))
1364 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1366 *extra_data_size -= value_size;
1368 if(!read_var_sized(a, &target_size, NULL))
1370 *extra_data_size -= target_size + 1;
1372 if(!read_ahead(a, target_size, &p))
1375 if(target_size > (MAX_NAME_IN_CHARS - 1)) {
1376 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1377 "Link target is too long");
1378 return ARCHIVE_FATAL;
1381 if(target_size == 0) {
1382 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1383 "No link target specified");
1384 return ARCHIVE_FATAL;
1387 memcpy(target_utf8_buf, p, target_size);
1388 target_utf8_buf[target_size] = 0;
1390 if(ARCHIVE_OK != consume(a, (int64_t)target_size))
1393 switch(rar->file.redir_type) {
1394 case REDIR_TYPE_UNIXSYMLINK:
1395 case REDIR_TYPE_WINSYMLINK:
1396 archive_entry_set_filetype(e, AE_IFLNK);
1397 archive_entry_update_symlink_utf8(e, target_utf8_buf);
1398 if (rar->file.redir_flags & REDIR_SYMLINK_IS_DIR) {
1399 archive_entry_set_symlink_type(e,
1400 AE_SYMLINK_TYPE_DIRECTORY);
1402 archive_entry_set_symlink_type(e,
1403 AE_SYMLINK_TYPE_FILE);
1407 case REDIR_TYPE_HARDLINK:
1408 archive_entry_set_filetype(e, AE_IFREG);
1409 archive_entry_update_hardlink_utf8(e, target_utf8_buf);
1413 /* Unknown redir type, skip it. */
1419 static int parse_file_extra_owner(struct archive_read* a,
1420 struct archive_entry* e, ssize_t* extra_data_size)
1423 uint64_t value_size = 0;
1425 size_t name_len = 0;
1426 size_t name_size = 0;
1427 char namebuf[OWNER_MAXNAMELEN];
1430 if(!read_var(a, &flags, &value_size))
1432 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1434 *extra_data_size -= value_size;
1436 if ((flags & OWNER_USER_NAME) != 0) {
1437 if(!read_var_sized(a, &name_size, NULL))
1439 *extra_data_size -= name_size + 1;
1441 if(!read_ahead(a, name_size, &p))
1444 if (name_size >= OWNER_MAXNAMELEN) {
1445 name_len = OWNER_MAXNAMELEN - 1;
1447 name_len = name_size;
1450 memcpy(namebuf, p, name_len);
1451 namebuf[name_len] = 0;
1452 if(ARCHIVE_OK != consume(a, (int64_t)name_size))
1455 archive_entry_set_uname(e, namebuf);
1457 if ((flags & OWNER_GROUP_NAME) != 0) {
1458 if(!read_var_sized(a, &name_size, NULL))
1460 *extra_data_size -= name_size + 1;
1462 if(!read_ahead(a, name_size, &p))
1465 if (name_size >= OWNER_MAXNAMELEN) {
1466 name_len = OWNER_MAXNAMELEN - 1;
1468 name_len = name_size;
1471 memcpy(namebuf, p, name_len);
1472 namebuf[name_len] = 0;
1473 if(ARCHIVE_OK != consume(a, (int64_t)name_size))
1476 archive_entry_set_gname(e, namebuf);
1478 if ((flags & OWNER_USER_UID) != 0) {
1479 if(!read_var(a, &id, &value_size))
1481 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1483 *extra_data_size -= value_size;
1485 archive_entry_set_uid(e, (la_int64_t)id);
1487 if ((flags & OWNER_GROUP_GID) != 0) {
1488 if(!read_var(a, &id, &value_size))
1490 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1492 *extra_data_size -= value_size;
1494 archive_entry_set_gid(e, (la_int64_t)id);
1499 static int process_head_file_extra(struct archive_read* a,
1500 struct archive_entry* e, struct rar5* rar, ssize_t extra_data_size)
1502 size_t extra_field_size;
1503 size_t extra_field_id = 0;
1504 int ret = ARCHIVE_FATAL;
1507 while(extra_data_size > 0) {
1508 if(!read_var_sized(a, &extra_field_size, &var_size))
1511 extra_data_size -= var_size;
1512 if(ARCHIVE_OK != consume(a, var_size)) {
1516 if(!read_var_sized(a, &extra_field_id, &var_size))
1519 extra_data_size -= var_size;
1520 if(ARCHIVE_OK != consume(a, var_size)) {
1524 switch(extra_field_id) {
1526 ret = parse_file_extra_hash(a, rar,
1530 ret = parse_file_extra_htime(a, e, rar,
1534 ret = parse_file_extra_redir(a, e, rar,
1538 ret = parse_file_extra_owner(a, e,
1542 ret = parse_file_extra_version(a, e,
1550 /* Skip unsupported entry. */
1551 return consume(a, extra_data_size);
1555 if(ret != ARCHIVE_OK) {
1556 /* Attribute not implemented. */
1563 static int process_head_file(struct archive_read* a, struct rar5* rar,
1564 struct archive_entry* entry, size_t block_flags)
1566 ssize_t extra_data_size = 0;
1567 size_t data_size = 0;
1568 size_t file_flags = 0;
1569 size_t file_attr = 0;
1570 size_t compression_info = 0;
1572 size_t name_size = 0;
1573 uint64_t unpacked_size, window_size;
1574 uint32_t mtime = 0, crc = 0;
1575 int c_method = 0, c_version = 0;
1576 char name_utf8_buf[MAX_NAME_IN_BYTES];
1579 archive_entry_clear(entry);
1581 /* Do not reset file context if we're switching archives. */
1582 if(!rar->cstate.switch_multivolume) {
1583 reset_file_context(rar);
1586 if(block_flags & HFL_EXTRA_DATA) {
1587 size_t edata_size = 0;
1588 if(!read_var_sized(a, &edata_size, NULL))
1591 /* Intentional type cast from unsigned to signed. */
1592 extra_data_size = (ssize_t) edata_size;
1595 if(block_flags & HFL_DATA) {
1596 if(!read_var_sized(a, &data_size, NULL))
1599 rar->file.bytes_remaining = data_size;
1601 rar->file.bytes_remaining = 0;
1603 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1604 "no data found in file/service block");
1605 return ARCHIVE_FATAL;
1609 DIRECTORY = 0x0001, UTIME = 0x0002, CRC32 = 0x0004,
1610 UNKNOWN_UNPACKED_SIZE = 0x0008,
1614 ATTR_READONLY = 0x1, ATTR_HIDDEN = 0x2, ATTR_SYSTEM = 0x4,
1615 ATTR_DIRECTORY = 0x10,
1618 enum COMP_INFO_FLAGS {
1622 if(!read_var_sized(a, &file_flags, NULL))
1625 if(!read_var(a, &unpacked_size, NULL))
1628 if(file_flags & UNKNOWN_UNPACKED_SIZE) {
1629 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1630 "Files with unknown unpacked size are not supported");
1631 return ARCHIVE_FATAL;
1634 rar->file.dir = (uint8_t) ((file_flags & DIRECTORY) > 0);
1636 if(!read_var_sized(a, &file_attr, NULL))
1639 if(file_flags & UTIME) {
1640 if(!read_u32(a, &mtime))
1644 if(file_flags & CRC32) {
1645 if(!read_u32(a, &crc))
1649 if(!read_var_sized(a, &compression_info, NULL))
1652 c_method = (int) (compression_info >> 7) & 0x7;
1653 c_version = (int) (compression_info & 0x3f);
1655 /* RAR5 seems to limit the dictionary size to 64MB. */
1656 window_size = (rar->file.dir > 0) ?
1658 g_unpack_window_size << ((compression_info >> 10) & 15);
1659 rar->cstate.method = c_method;
1660 rar->cstate.version = c_version + 50;
1662 /* Check if window_size is a sane value. Also, if the file is not
1663 * declared as a directory, disallow window_size == 0. */
1664 if(window_size > (64 * 1024 * 1024) ||
1665 (rar->file.dir == 0 && window_size == 0))
1667 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1668 "Declared dictionary size is not supported.");
1669 return ARCHIVE_FATAL;
1672 /* Values up to 64M should fit into ssize_t on every
1674 rar->cstate.window_size = (ssize_t) window_size;
1676 rar->file.solid = (compression_info & SOLID) > 0;
1677 rar->file.service = 0;
1679 if(!read_var_sized(a, &host_os, NULL))
1687 if(host_os == HOST_WINDOWS) {
1688 /* Host OS is Windows */
1692 if(file_attr & ATTR_DIRECTORY) {
1693 if (file_attr & ATTR_READONLY) {
1694 mode = 0555 | AE_IFDIR;
1696 mode = 0755 | AE_IFDIR;
1699 if (file_attr & ATTR_READONLY) {
1700 mode = 0444 | AE_IFREG;
1702 mode = 0644 | AE_IFREG;
1706 archive_entry_set_mode(entry, mode);
1708 if (file_attr & (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM)) {
1709 char *fflags_text, *ptr;
1710 /* allocate for "rdonly,hidden,system," */
1711 fflags_text = malloc(22 * sizeof(char));
1712 if (fflags_text != NULL) {
1714 if (file_attr & ATTR_READONLY) {
1715 strcpy(ptr, "rdonly,");
1718 if (file_attr & ATTR_HIDDEN) {
1719 strcpy(ptr, "hidden,");
1722 if (file_attr & ATTR_SYSTEM) {
1723 strcpy(ptr, "system,");
1726 if (ptr > fflags_text) {
1727 /* Delete trailing comma */
1729 archive_entry_copy_fflags_text(entry,
1735 } else if(host_os == HOST_UNIX) {
1736 /* Host OS is Unix */
1737 archive_entry_set_mode(entry, (__LA_MODE_T) file_attr);
1739 /* Unknown host OS */
1740 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1741 "Unsupported Host OS: 0x%x", (int) host_os);
1743 return ARCHIVE_FATAL;
1746 if(!read_var_sized(a, &name_size, NULL))
1749 if(!read_ahead(a, name_size, &p))
1752 if(name_size > (MAX_NAME_IN_CHARS - 1)) {
1753 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1754 "Filename is too long");
1756 return ARCHIVE_FATAL;
1759 if(name_size == 0) {
1760 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1761 "No filename specified");
1763 return ARCHIVE_FATAL;
1766 memcpy(name_utf8_buf, p, name_size);
1767 name_utf8_buf[name_size] = 0;
1768 if(ARCHIVE_OK != consume(a, name_size)) {
1772 archive_entry_update_pathname_utf8(entry, name_utf8_buf);
1774 if(extra_data_size > 0) {
1775 int ret = process_head_file_extra(a, entry, rar,
1779 if(extra_data_size < 0) {
1780 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1781 "File extra data size is not zero");
1782 return ARCHIVE_FATAL;
1785 if(ret != ARCHIVE_OK)
1789 if((file_flags & UNKNOWN_UNPACKED_SIZE) == 0) {
1790 rar->file.unpacked_size = (ssize_t) unpacked_size;
1791 if(rar->file.redir_type == REDIR_TYPE_NONE)
1792 archive_entry_set_size(entry, unpacked_size);
1795 if(file_flags & UTIME) {
1796 archive_entry_set_mtime(entry, (time_t) mtime, 0);
1799 if(file_flags & CRC32) {
1800 rar->file.stored_crc32 = crc;
1803 if(!rar->cstate.switch_multivolume) {
1804 /* Do not reinitialize unpacking state if we're switching
1806 rar->cstate.block_parsing_finished = 1;
1807 rar->cstate.all_filters_applied = 1;
1808 rar->cstate.initialized = 0;
1811 if(rar->generic.split_before > 0) {
1812 /* If now we're standing on a header that has a 'split before'
1813 * mark, it means we're standing on a 'continuation' file
1814 * header. Signal the caller that if it wants to move to
1815 * another file, it must call rar5_read_header() function
1818 return ARCHIVE_RETRY;
1824 static int process_head_service(struct archive_read* a, struct rar5* rar,
1825 struct archive_entry* entry, size_t block_flags)
1827 /* Process this SERVICE block the same way as FILE blocks. */
1828 int ret = process_head_file(a, rar, entry, block_flags);
1829 if(ret != ARCHIVE_OK)
1832 rar->file.service = 1;
1834 /* But skip the data part automatically. It's no use for the user
1835 * anyway. It contains only service data, not even needed to
1836 * properly unpack the file. */
1837 ret = rar5_read_data_skip(a);
1838 if(ret != ARCHIVE_OK)
1841 /* After skipping, try parsing another block automatically. */
1842 return ARCHIVE_RETRY;
1845 static int process_head_main(struct archive_read* a, struct rar5* rar,
1846 struct archive_entry* entry, size_t block_flags)
1851 size_t extra_data_size = 0;
1852 size_t extra_field_size = 0;
1853 size_t extra_field_id = 0;
1854 size_t archive_flags = 0;
1856 if(block_flags & HFL_EXTRA_DATA) {
1857 if(!read_var_sized(a, &extra_data_size, NULL))
1860 extra_data_size = 0;
1863 if(!read_var_sized(a, &archive_flags, NULL)) {
1868 VOLUME = 0x0001, /* multi-volume archive */
1869 VOLUME_NUMBER = 0x0002, /* volume number, first vol doesn't
1871 SOLID = 0x0004, /* solid archive */
1872 PROTECT = 0x0008, /* contains Recovery info */
1873 LOCK = 0x0010, /* readonly flag, not used */
1876 rar->main.volume = (archive_flags & VOLUME) > 0;
1877 rar->main.solid = (archive_flags & SOLID) > 0;
1879 if(archive_flags & VOLUME_NUMBER) {
1881 if(!read_var_sized(a, &v, NULL)) {
1886 archive_set_error(&a->archive,
1887 ARCHIVE_ERRNO_FILE_FORMAT,
1888 "Invalid volume number");
1889 return ARCHIVE_FATAL;
1892 rar->main.vol_no = (unsigned int) v;
1894 rar->main.vol_no = 0;
1897 if(rar->vol.expected_vol_no > 0 &&
1898 rar->main.vol_no != rar->vol.expected_vol_no)
1900 /* Returning EOF instead of FATAL because of strange
1901 * libarchive behavior. When opening multiple files via
1902 * archive_read_open_filenames(), after reading up the whole
1903 * last file, the __archive_read_ahead function wraps up to
1904 * the first archive instead of returning EOF. */
1908 if(extra_data_size == 0) {
1913 if(!read_var_sized(a, &extra_field_size, NULL)) {
1917 if(!read_var_sized(a, &extra_field_id, NULL)) {
1921 if(extra_field_size == 0) {
1922 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1923 "Invalid extra field size");
1924 return ARCHIVE_FATAL;
1928 // Just one attribute here.
1932 switch(extra_field_id) {
1934 ret = process_main_locator_extra_block(a, rar);
1935 if(ret != ARCHIVE_OK) {
1936 /* Error while parsing main locator extra
1943 archive_set_error(&a->archive,
1944 ARCHIVE_ERRNO_FILE_FORMAT,
1945 "Unsupported extra type (0x%x)",
1946 (int) extra_field_id);
1947 return ARCHIVE_FATAL;
1953 static int skip_unprocessed_bytes(struct archive_read* a) {
1954 struct rar5* rar = get_context(a);
1957 if(rar->file.bytes_remaining) {
1958 /* Use different skipping method in block merging mode than in
1959 * normal mode. If merge mode is active, rar5_read_data_skip
1960 * can't be used, because it could allow recursive use of
1961 * merge_block() * function, and this function doesn't support
1963 if(rar->merge_mode) {
1964 /* Discard whole merged block. This is valid in solid
1965 * mode as well, because the code will discard blocks
1966 * only if those blocks are safe to discard (i.e.
1967 * they're not FILE blocks). */
1968 ret = consume(a, rar->file.bytes_remaining);
1969 if(ret != ARCHIVE_OK) {
1972 rar->file.bytes_remaining = 0;
1974 /* If we're not in merge mode, use safe skipping code.
1975 * This will ensure we'll handle solid archives
1977 ret = rar5_read_data_skip(a);
1978 if(ret != ARCHIVE_OK) {
1987 static int scan_for_signature(struct archive_read* a);
1989 /* Base block processing function. A 'base block' is a RARv5 header block
1990 * that tells the reader what kind of data is stored inside the block.
1992 * From the birds-eye view a RAR file looks file this:
1994 * <magic><base_block_1><base_block_2>...<base_block_n>
1996 * There are a few types of base blocks. Those types are specified inside
1997 * the 'switch' statement in this function. For example purposes, I'll write
1998 * how a standard RARv5 file could look like here:
2000 * <magic><MAIN><FILE><FILE><FILE><SERVICE><ENDARC>
2002 * The structure above could describe an archive file with 3 files in it,
2003 * one service "QuickOpen" block (that is ignored by this parser), and an
2004 * end of file base block marker.
2006 * If the file is stored in multiple archive files ("multiarchive"), it might
2009 * .part01.rar: <magic><MAIN><FILE><ENDARC>
2010 * .part02.rar: <magic><MAIN><FILE><ENDARC>
2011 * .part03.rar: <magic><MAIN><FILE><ENDARC>
2013 * This example could describe 3 RAR files that contain ONE archived file.
2014 * Or it could describe 3 RAR files that contain 3 different files. Or 3
2015 * RAR files than contain 2 files. It all depends what metadata is stored in
2016 * the headers of <FILE> blocks.
2018 * Each <FILE> block contains info about its size, the name of the file it's
2019 * storing inside, and whether this FILE block is a continuation block of
2020 * previous archive ('split before'), and is this FILE block should be
2021 * continued in another archive ('split after'). By parsing the 'split before'
2022 * and 'split after' flags, we're able to tell if multiple <FILE> base blocks
2023 * are describing one file, or multiple files (with the same filename, for
2026 * One thing to note is that if we're parsing the first <FILE> block, and
2027 * we see 'split after' flag, then we need to jump over to another <FILE>
2028 * block to be able to decompress rest of the data. To do this, we need
2029 * to skip the <ENDARC> block, then switch to another file, then skip the
2030 * <magic> block, <MAIN> block, and then we're standing on the proper
2034 static int process_base_block(struct archive_read* a,
2035 struct archive_entry* entry)
2037 struct rar5* rar = get_context(a);
2038 uint32_t hdr_crc, computed_crc;
2039 size_t raw_hdr_size = 0, hdr_size_len, hdr_size;
2040 size_t header_id = 0;
2041 size_t header_flags = 0;
2045 /* Skip any unprocessed data for this file. */
2046 ret = skip_unprocessed_bytes(a);
2047 if(ret != ARCHIVE_OK)
2050 /* Read the expected CRC32 checksum. */
2051 if(!read_u32(a, &hdr_crc)) {
2055 /* Read header size. */
2056 if(!read_var_sized(a, &raw_hdr_size, &hdr_size_len)) {
2060 /* Sanity check, maximum header size for RAR5 is 2MB. */
2061 if(raw_hdr_size > (2 * 1024 * 1024)) {
2062 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2063 "Base block header is too large");
2065 return ARCHIVE_FATAL;
2068 hdr_size = raw_hdr_size + hdr_size_len;
2070 /* Read the whole header data into memory, maximum memory use here is
2072 if(!read_ahead(a, hdr_size, &p)) {
2076 /* Verify the CRC32 of the header data. */
2077 computed_crc = (uint32_t) crc32(0, p, (int) hdr_size);
2078 if(computed_crc != hdr_crc) {
2079 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2080 "Header CRC error");
2082 return ARCHIVE_FATAL;
2085 /* If the checksum is OK, we proceed with parsing. */
2086 if(ARCHIVE_OK != consume(a, hdr_size_len)) {
2090 if(!read_var_sized(a, &header_id, NULL))
2093 if(!read_var_sized(a, &header_flags, NULL))
2096 rar->generic.split_after = (header_flags & HFL_SPLIT_AFTER) > 0;
2097 rar->generic.split_before = (header_flags & HFL_SPLIT_BEFORE) > 0;
2098 rar->generic.size = (int)hdr_size;
2099 rar->generic.last_header_id = (int)header_id;
2100 rar->main.endarc = 0;
2102 /* Those are possible header ids in RARv5. */
2104 HEAD_MARK = 0x00, HEAD_MAIN = 0x01, HEAD_FILE = 0x02,
2105 HEAD_SERVICE = 0x03, HEAD_CRYPT = 0x04, HEAD_ENDARC = 0x05,
2106 HEAD_UNKNOWN = 0xff,
2111 ret = process_head_main(a, rar, entry, header_flags);
2113 /* Main header doesn't have any files in it, so it's
2114 * pointless to return to the caller. Retry to next
2115 * header, which should be HEAD_FILE/HEAD_SERVICE. */
2116 if(ret == ARCHIVE_OK)
2117 return ARCHIVE_RETRY;
2121 ret = process_head_service(a, rar, entry, header_flags);
2124 ret = process_head_file(a, rar, entry, header_flags);
2127 archive_set_error(&a->archive,
2128 ARCHIVE_ERRNO_FILE_FORMAT,
2129 "Encryption is not supported");
2130 return ARCHIVE_FATAL;
2132 rar->main.endarc = 1;
2134 /* After encountering an end of file marker, we need
2135 * to take into consideration if this archive is
2136 * continued in another file (i.e. is it part01.rar:
2137 * is there a part02.rar?) */
2138 if(rar->main.volume) {
2139 /* In case there is part02.rar, position the
2140 * read pointer in a proper place, so we can
2141 * resume parsing. */
2142 ret = scan_for_signature(a);
2143 if(ret == ARCHIVE_FATAL) {
2146 if(rar->vol.expected_vol_no ==
2148 archive_set_error(&a->archive,
2149 ARCHIVE_ERRNO_FILE_FORMAT,
2151 return ARCHIVE_FATAL;
2154 rar->vol.expected_vol_no =
2155 rar->main.vol_no + 1;
2164 if((header_flags & HFL_SKIP_IF_UNKNOWN) == 0) {
2165 archive_set_error(&a->archive,
2166 ARCHIVE_ERRNO_FILE_FORMAT,
2167 "Header type error");
2168 return ARCHIVE_FATAL;
2170 /* If the block is marked as 'skip if unknown',
2171 * do as the flag says: skip the block
2172 * instead on failing on it. */
2173 return ARCHIVE_RETRY;
2179 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2180 "Internal unpacker error");
2181 return ARCHIVE_FATAL;
2185 static int skip_base_block(struct archive_read* a) {
2187 struct rar5* rar = get_context(a);
2189 /* Create a new local archive_entry structure that will be operated on
2190 * by header reader; operations on this archive_entry will be discarded.
2192 struct archive_entry* entry = archive_entry_new();
2193 ret = process_base_block(a, entry);
2195 /* Discard operations on this archive_entry structure. */
2196 archive_entry_free(entry);
2197 if(ret == ARCHIVE_FATAL)
2200 if(rar->generic.last_header_id == 2 && rar->generic.split_before > 0)
2203 if(ret == ARCHIVE_OK)
2204 return ARCHIVE_RETRY;
2209 static int rar5_read_header(struct archive_read *a,
2210 struct archive_entry *entry)
2212 struct rar5* rar = get_context(a);
2215 if(rar->header_initialized == 0) {
2217 rar->header_initialized = 1;
2220 if(rar->skipped_magic == 0) {
2221 if(ARCHIVE_OK != consume(a, rar5_signature_size)) {
2225 rar->skipped_magic = 1;
2229 ret = process_base_block(a, entry);
2230 } while(ret == ARCHIVE_RETRY ||
2231 (rar->main.endarc > 0 && ret == ARCHIVE_OK));
2236 static void init_unpack(struct rar5* rar) {
2237 rar->file.calculated_crc32 = 0;
2238 if (rar->cstate.window_size)
2239 rar->cstate.window_mask = rar->cstate.window_size - 1;
2241 rar->cstate.window_mask = 0;
2243 free(rar->cstate.window_buf);
2244 free(rar->cstate.filtered_buf);
2246 if(rar->cstate.window_size > 0) {
2247 rar->cstate.window_buf = calloc(1, rar->cstate.window_size);
2248 rar->cstate.filtered_buf = calloc(1, rar->cstate.window_size);
2250 rar->cstate.window_buf = NULL;
2251 rar->cstate.filtered_buf = NULL;
2254 rar->cstate.write_ptr = 0;
2255 rar->cstate.last_write_ptr = 0;
2257 memset(&rar->cstate.bd, 0, sizeof(rar->cstate.bd));
2258 memset(&rar->cstate.ld, 0, sizeof(rar->cstate.ld));
2259 memset(&rar->cstate.dd, 0, sizeof(rar->cstate.dd));
2260 memset(&rar->cstate.ldd, 0, sizeof(rar->cstate.ldd));
2261 memset(&rar->cstate.rd, 0, sizeof(rar->cstate.rd));
2264 static void update_crc(struct rar5* rar, const uint8_t* p, size_t to_read) {
2267 if(rar->skip_mode) {
2268 #if defined CHECK_CRC_ON_SOLID_SKIP
2277 /* Don't update CRC32 if the file doesn't have the
2278 * `stored_crc32` info filled in. */
2279 if(rar->file.stored_crc32 > 0) {
2280 rar->file.calculated_crc32 =
2281 crc32(rar->file.calculated_crc32, p, to_read);
2284 /* Check if the file uses an optional BLAKE2sp checksum
2286 if(rar->file.has_blake2 > 0) {
2287 /* Return value of the `update` function is always 0,
2288 * so we can explicitly ignore it here. */
2289 (void) blake2sp_update(&rar->file.b2state, p, to_read);
2294 static int create_decode_tables(uint8_t* bit_length,
2295 struct decode_table* table, int size)
2297 int code, upper_limit = 0, i, lc[16];
2298 uint32_t decode_pos_clone[rar5_countof(table->decode_pos)];
2299 ssize_t cur_len, quick_data_size;
2301 memset(&lc, 0, sizeof(lc));
2302 memset(table->decode_num, 0, sizeof(table->decode_num));
2304 table->quick_bits = size == HUFF_NC ? 10 : 7;
2306 for(i = 0; i < size; i++) {
2307 lc[bit_length[i] & 15]++;
2311 table->decode_pos[0] = 0;
2312 table->decode_len[0] = 0;
2314 for(i = 1; i < 16; i++) {
2315 upper_limit += lc[i];
2317 table->decode_len[i] = upper_limit << (16 - i);
2318 table->decode_pos[i] = table->decode_pos[i - 1] + lc[i - 1];
2323 memcpy(decode_pos_clone, table->decode_pos, sizeof(decode_pos_clone));
2325 for(i = 0; i < size; i++) {
2326 uint8_t clen = bit_length[i] & 15;
2328 int last_pos = decode_pos_clone[clen];
2329 table->decode_num[last_pos] = i;
2330 decode_pos_clone[clen]++;
2334 quick_data_size = (int64_t)1 << table->quick_bits;
2336 for(code = 0; code < quick_data_size; code++) {
2337 int bit_field = code << (16 - table->quick_bits);
2340 while(cur_len < rar5_countof(table->decode_len) &&
2341 bit_field >= table->decode_len[cur_len]) {
2345 table->quick_len[code] = (uint8_t) cur_len;
2347 dist = bit_field - table->decode_len[cur_len - 1];
2348 dist >>= (16 - cur_len);
2350 pos = table->decode_pos[cur_len & 15] + dist;
2351 if(cur_len < rar5_countof(table->decode_pos) && pos < size) {
2352 table->quick_num[code] = table->decode_num[pos];
2354 table->quick_num[code] = 0;
2361 static int decode_number(struct archive_read* a, struct decode_table* table,
2362 const uint8_t* p, uint16_t* num)
2367 struct rar5* rar = get_context(a);
2369 if(ARCHIVE_OK != read_bits_16(rar, p, &bitfield)) {
2375 if(bitfield < table->decode_len[table->quick_bits]) {
2376 int code = bitfield >> (16 - table->quick_bits);
2377 skip_bits(rar, table->quick_len[code]);
2378 *num = table->quick_num[code];
2384 for(i = table->quick_bits + 1; i < 15; i++) {
2385 if(bitfield < table->decode_len[i]) {
2391 skip_bits(rar, bits);
2393 dist = bitfield - table->decode_len[bits - 1];
2394 dist >>= (16 - bits);
2395 pos = table->decode_pos[bits] + dist;
2397 if(pos >= table->size)
2400 *num = table->decode_num[pos];
2404 /* Reads and parses Huffman tables from the beginning of the block. */
2405 static int parse_tables(struct archive_read* a, struct rar5* rar,
2408 int ret, value, i, w, idx = 0;
2409 uint8_t bit_length[HUFF_BC],
2410 table[HUFF_TABLE_SIZE],
2414 enum { ESCAPE = 15 };
2416 /* The data for table generation is compressed using a simple RLE-like
2417 * algorithm when storing zeroes, so we need to unpack it first. */
2418 for(w = 0, i = 0; w < HUFF_BC;) {
2419 if(i >= rar->cstate.cur_block_size) {
2420 /* Truncated data, can't continue. */
2421 archive_set_error(&a->archive,
2422 ARCHIVE_ERRNO_FILE_FORMAT,
2423 "Truncated data in huffman tables");
2424 return ARCHIVE_FATAL;
2427 value = (p[i] & nibble_mask) >> nibble_shift;
2429 if(nibble_mask == 0x0F)
2432 nibble_mask ^= 0xFF;
2435 /* Values smaller than 15 is data, so we write it directly.
2436 * Value 15 is a flag telling us that we need to unpack more
2438 if(value == ESCAPE) {
2439 value = (p[i] & nibble_mask) >> nibble_shift;
2440 if(nibble_mask == 0x0F)
2442 nibble_mask ^= 0xFF;
2446 /* We sometimes need to write the actual value
2447 * of 15, so this case handles that. */
2448 bit_length[w++] = ESCAPE;
2453 for(k = 0; (k < value + 2) && (w < HUFF_BC);
2455 bit_length[w++] = 0;
2459 bit_length[w++] = value;
2463 rar->bits.in_addr = i;
2464 rar->bits.bit_addr = nibble_shift ^ 4;
2466 ret = create_decode_tables(bit_length, &rar->cstate.bd, HUFF_BC);
2467 if(ret != ARCHIVE_OK) {
2468 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2469 "Decoding huffman tables failed");
2470 return ARCHIVE_FATAL;
2473 for(i = 0; i < HUFF_TABLE_SIZE;) {
2476 if((rar->bits.in_addr + 6) >= rar->cstate.cur_block_size) {
2477 /* Truncated data, can't continue. */
2478 archive_set_error(&a->archive,
2479 ARCHIVE_ERRNO_FILE_FORMAT,
2480 "Truncated data in huffman tables (#2)");
2481 return ARCHIVE_FATAL;
2484 ret = decode_number(a, &rar->cstate.bd, p, &num);
2485 if(ret != ARCHIVE_OK) {
2486 archive_set_error(&a->archive,
2487 ARCHIVE_ERRNO_FILE_FORMAT,
2488 "Decoding huffman tables failed");
2489 return ARCHIVE_FATAL;
2493 /* 0..15: store directly */
2494 table[i] = (uint8_t) num;
2500 /* 16..17: repeat previous code */
2502 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2516 while(n-- > 0 && i < HUFF_TABLE_SIZE) {
2517 table[i] = table[i - 1];
2521 archive_set_error(&a->archive,
2522 ARCHIVE_ERRNO_FILE_FORMAT,
2523 "Unexpected error when decoding "
2525 return ARCHIVE_FATAL;
2531 /* other codes: fill with zeroes `n` times */
2533 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2546 while(n-- > 0 && i < HUFF_TABLE_SIZE)
2550 ret = create_decode_tables(&table[idx], &rar->cstate.ld, HUFF_NC);
2551 if(ret != ARCHIVE_OK) {
2552 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2553 "Failed to create literal table");
2554 return ARCHIVE_FATAL;
2559 ret = create_decode_tables(&table[idx], &rar->cstate.dd, HUFF_DC);
2560 if(ret != ARCHIVE_OK) {
2561 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2562 "Failed to create distance table");
2563 return ARCHIVE_FATAL;
2568 ret = create_decode_tables(&table[idx], &rar->cstate.ldd, HUFF_LDC);
2569 if(ret != ARCHIVE_OK) {
2570 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2571 "Failed to create lower bits of distances table");
2572 return ARCHIVE_FATAL;
2577 ret = create_decode_tables(&table[idx], &rar->cstate.rd, HUFF_RC);
2578 if(ret != ARCHIVE_OK) {
2579 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2580 "Failed to create repeating distances table");
2581 return ARCHIVE_FATAL;
2587 /* Parses the block header, verifies its CRC byte, and saves the header
2588 * fields inside the `hdr` pointer. */
2589 static int parse_block_header(struct archive_read* a, const uint8_t* p,
2590 ssize_t* block_size, struct compressed_block_header* hdr)
2592 memcpy(hdr, p, sizeof(struct compressed_block_header));
2594 if(bf_byte_count(hdr) > 2) {
2595 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2596 "Unsupported block header size (was %d, max is 2)",
2597 bf_byte_count(hdr));
2598 return ARCHIVE_FATAL;
2601 /* This should probably use bit reader interface in order to be more
2604 switch(bf_byte_count(hdr)) {
2605 /* 1-byte block size */
2607 *block_size = *(const uint8_t*) &p[2];
2610 /* 2-byte block size */
2612 *block_size = archive_le16dec(&p[2]);
2615 /* 3-byte block size */
2617 *block_size = archive_le32dec(&p[2]);
2618 *block_size &= 0x00FFFFFF;
2621 /* Other block sizes are not supported. This case is not
2622 * reached, because we have an 'if' guard before the switch
2623 * that makes sure of it. */
2625 return ARCHIVE_FATAL;
2628 /* Verify the block header checksum. 0x5A is a magic value and is
2629 * always * constant. */
2630 uint8_t calculated_cksum = 0x5A
2631 ^ (uint8_t) hdr->block_flags_u8
2632 ^ (uint8_t) *block_size
2633 ^ (uint8_t) (*block_size >> 8)
2634 ^ (uint8_t) (*block_size >> 16);
2636 if(calculated_cksum != hdr->block_cksum) {
2637 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2638 "Block checksum error: got 0x%x, expected 0x%x",
2639 hdr->block_cksum, calculated_cksum);
2641 return ARCHIVE_FATAL;
2647 /* Convenience function used during filter processing. */
2648 static int parse_filter_data(struct rar5* rar, const uint8_t* p,
2649 uint32_t* filter_data)
2654 if(ARCHIVE_OK != read_consume_bits(rar, p, 2, &bytes))
2659 for(i = 0; i < bytes; i++) {
2662 if(ARCHIVE_OK != read_bits_16(rar, p, &byte)) {
2666 /* Cast to uint32_t will ensure the shift operation will not
2667 * produce undefined result. */
2668 data += ((uint32_t) byte >> 8) << (i * 8);
2672 *filter_data = data;
2676 /* Function is used during sanity checking. */
2677 static int is_valid_filter_block_start(struct rar5* rar,
2680 const int64_t block_start = (ssize_t) start + rar->cstate.write_ptr;
2681 const int64_t last_bs = rar->cstate.last_block_start;
2682 const ssize_t last_bl = rar->cstate.last_block_length;
2684 if(last_bs == 0 || last_bl == 0) {
2685 /* We didn't have any filters yet, so accept this offset. */
2689 if(block_start >= last_bs + last_bl) {
2690 /* Current offset is bigger than last block's end offset, so
2691 * accept current offset. */
2695 /* Any other case is not a normal situation and we should fail. */
2699 /* The function will create a new filter, read its parameters from the input
2700 * stream and add it to the filter collection. */
2701 static int parse_filter(struct archive_read* ar, const uint8_t* p) {
2702 uint32_t block_start, block_length;
2703 uint16_t filter_type;
2704 struct rar5* rar = get_context(ar);
2706 /* Read the parameters from the input stream. */
2707 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_start))
2710 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_length))
2713 if(ARCHIVE_OK != read_bits_16(rar, p, &filter_type))
2719 /* Perform some sanity checks on this filter parameters. Note that we
2720 * allow only DELTA, E8/E9 and ARM filters here, because rest of
2721 * filters are not used in RARv5. */
2723 if(block_length < 4 ||
2724 block_length > 0x400000 ||
2725 filter_type > FILTER_ARM ||
2726 !is_valid_filter_block_start(rar, block_start))
2728 archive_set_error(&ar->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2729 "Invalid filter encountered");
2730 return ARCHIVE_FATAL;
2733 /* Allocate a new filter. */
2734 struct filter_info* filt = add_new_filter(rar);
2736 archive_set_error(&ar->archive, ENOMEM,
2737 "Can't allocate memory for a filter descriptor.");
2738 return ARCHIVE_FATAL;
2741 filt->type = filter_type;
2742 filt->block_start = rar->cstate.write_ptr + block_start;
2743 filt->block_length = block_length;
2745 rar->cstate.last_block_start = filt->block_start;
2746 rar->cstate.last_block_length = filt->block_length;
2748 /* Read some more data in case this is a DELTA filter. Other filter
2749 * types don't require any additional data over what was already
2751 if(filter_type == FILTER_DELTA) {
2754 if(ARCHIVE_OK != read_consume_bits(rar, p, 5, &channels))
2757 filt->channels = channels + 1;
2763 static int decode_code_length(struct rar5* rar, const uint8_t* p,
2766 int lbits, length = 2;
2771 lbits = code / 4 - 1;
2772 length += (4 | (code & 3)) << lbits;
2778 if(ARCHIVE_OK != read_consume_bits(rar, p, lbits, &add))
2787 static int copy_string(struct archive_read* a, int len, int dist) {
2788 struct rar5* rar = get_context(a);
2789 const uint64_t cmask = rar->cstate.window_mask;
2790 const uint64_t write_ptr = rar->cstate.write_ptr +
2791 rar->cstate.solid_offset;
2794 if (rar->cstate.window_buf == NULL)
2795 return ARCHIVE_FATAL;
2797 /* The unpacker spends most of the time in this function. It would be
2798 * a good idea to introduce some optimizations here.
2800 * Just remember that this loop treats buffers that overlap differently
2801 * than buffers that do not overlap. This is why a simple memcpy(3)
2802 * call will not be enough. */
2804 for(i = 0; i < len; i++) {
2805 const ssize_t write_idx = (write_ptr + i) & cmask;
2806 const ssize_t read_idx = (write_ptr + i - dist) & cmask;
2807 rar->cstate.window_buf[write_idx] =
2808 rar->cstate.window_buf[read_idx];
2811 rar->cstate.write_ptr += len;
2815 static int do_uncompress_block(struct archive_read* a, const uint8_t* p) {
2816 struct rar5* rar = get_context(a);
2820 const uint64_t cmask = rar->cstate.window_mask;
2821 const struct compressed_block_header* hdr = &rar->last_block_hdr;
2822 const uint8_t bit_size = 1 + bf_bit_size(hdr);
2825 if(rar->cstate.write_ptr - rar->cstate.last_write_ptr >
2826 (rar->cstate.window_size >> 1)) {
2827 /* Don't allow growing data by more than half of the
2828 * window size at a time. In such case, break the loop;
2829 * next call to this function will continue processing
2830 * from this moment. */
2834 if(rar->bits.in_addr > rar->cstate.cur_block_size - 1 ||
2835 (rar->bits.in_addr == rar->cstate.cur_block_size - 1 &&
2836 rar->bits.bit_addr >= bit_size))
2838 /* If the program counter is here, it means the
2839 * function has finished processing the block. */
2840 rar->cstate.block_parsing_finished = 1;
2844 /* Decode the next literal. */
2845 if(ARCHIVE_OK != decode_number(a, &rar->cstate.ld, p, &num)) {
2849 /* Num holds a decompression literal, or 'command code'.
2851 * - Values lower than 256 are just bytes. Those codes
2852 * can be stored in the output buffer directly.
2854 * - Code 256 defines a new filter, which is later used to
2855 * ransform the data block accordingly to the filter type.
2856 * The data block needs to be fully uncompressed first.
2858 * - Code bigger than 257 and smaller than 262 define
2859 * a repetition pattern that should be copied from
2860 * an already uncompressed chunk of data.
2864 /* Directly store the byte. */
2865 int64_t write_idx = rar->cstate.solid_offset +
2866 rar->cstate.write_ptr++;
2868 rar->cstate.window_buf[write_idx & cmask] =
2871 } else if(num >= 262) {
2873 int len = decode_code_length(rar, p, num - 262),
2878 archive_set_error(&a->archive,
2879 ARCHIVE_ERRNO_PROGRAMMER,
2880 "Failed to decode the code length");
2882 return ARCHIVE_FATAL;
2885 if(ARCHIVE_OK != decode_number(a, &rar->cstate.dd, p,
2888 archive_set_error(&a->archive,
2889 ARCHIVE_ERRNO_PROGRAMMER,
2890 "Failed to decode the distance slot");
2892 return ARCHIVE_FATAL;
2899 dbits = dist_slot / 2 - 1;
2901 /* Cast to uint32_t will make sure the shift
2902 * left operation won't produce undefined
2903 * result. Then, the uint32_t type will
2904 * be implicitly casted to int. */
2905 dist += (uint32_t) (2 |
2906 (dist_slot & 1)) << dbits;
2915 if(ARCHIVE_OK != read_bits_32(
2923 skip_bits(rar, dbits - 4);
2929 if(ARCHIVE_OK != decode_number(a,
2930 &rar->cstate.ldd, p, &low_dist))
2932 archive_set_error(&a->archive,
2933 ARCHIVE_ERRNO_PROGRAMMER,
2934 "Failed to decode the "
2937 return ARCHIVE_FATAL;
2940 if(dist >= INT_MAX - low_dist - 1) {
2941 /* This only happens in
2942 * invalid archives. */
2943 archive_set_error(&a->archive,
2944 ARCHIVE_ERRNO_FILE_FORMAT,
2947 return ARCHIVE_FATAL;
2952 /* dbits is one of [0,1,2,3] */
2955 if(ARCHIVE_OK != read_consume_bits(rar,
2957 /* Return EOF if we can't read
2972 if(dist > 0x40000) {
2978 dist_cache_push(rar, dist);
2979 rar->cstate.last_len = len;
2981 if(ARCHIVE_OK != copy_string(a, len, dist))
2982 return ARCHIVE_FATAL;
2985 } else if(num == 256) {
2986 /* Create a filter. */
2987 ret = parse_filter(a, p);
2988 if(ret != ARCHIVE_OK)
2992 } else if(num == 257) {
2993 if(rar->cstate.last_len != 0) {
2994 if(ARCHIVE_OK != copy_string(a,
2995 rar->cstate.last_len,
2996 rar->cstate.dist_cache[0]))
2998 return ARCHIVE_FATAL;
3003 } else if(num < 262) {
3004 const int idx = num - 258;
3005 const int dist = dist_cache_touch(rar, idx);
3010 if(ARCHIVE_OK != decode_number(a, &rar->cstate.rd, p,
3012 return ARCHIVE_FATAL;
3015 len = decode_code_length(rar, p, len_slot);
3016 rar->cstate.last_len = len;
3018 if(ARCHIVE_OK != copy_string(a, len, dist))
3019 return ARCHIVE_FATAL;
3024 /* The program counter shouldn't reach here. */
3025 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3026 "Unsupported block code: 0x%x", num);
3028 return ARCHIVE_FATAL;
3034 /* Binary search for the RARv5 signature. */
3035 static int scan_for_signature(struct archive_read* a) {
3037 const int chunk_size = 512;
3040 /* If we're here, it means we're on an 'unknown territory' data.
3041 * There's no indication what kind of data we're reading here.
3042 * It could be some text comment, any kind of binary data,
3043 * digital sign, dragons, etc.
3045 * We want to find a valid RARv5 magic header inside this unknown
3048 /* Is it possible in libarchive to just skip everything until the
3049 * end of the file? If so, it would be a better approach than the
3050 * current implementation of this function. */
3053 if(!read_ahead(a, chunk_size, &p))
3056 for(i = 0; i < chunk_size - rar5_signature_size; i++) {
3057 if(memcmp(&p[i], rar5_signature,
3058 rar5_signature_size) == 0) {
3059 /* Consume the number of bytes we've used to
3060 * search for the signature, as well as the
3061 * number of bytes used by the signature
3062 * itself. After this we should be standing
3063 * on a valid base block header. */
3064 (void) consume(a, i + rar5_signature_size);
3069 consume(a, chunk_size);
3072 return ARCHIVE_FATAL;
3075 /* This function will switch the multivolume archive file to another file,
3076 * i.e. from part03 to part 04. */
3077 static int advance_multivolume(struct archive_read* a) {
3079 struct rar5* rar = get_context(a);
3081 /* A small state machine that will skip unnecessary data, needed to
3082 * switch from one multivolume to another. Such skipping is needed if
3083 * we want to be an stream-oriented (instead of file-oriented)
3086 * The state machine starts with `rar->main.endarc` == 0. It also
3087 * assumes that current stream pointer points to some base block
3090 * The `endarc` field is being set when the base block parsing
3091 * function encounters the 'end of archive' marker.
3095 if(rar->main.endarc == 1) {
3098 rar->main.endarc = 0;
3101 lret = skip_base_block(a);
3104 /* Continue looping. */
3111 /* Forward any errors to the
3119 /* Skip current base block. In order to properly skip
3120 * it, we really need to simply parse it and discard
3123 lret = skip_base_block(a);
3124 if(lret == ARCHIVE_FATAL || lret == ARCHIVE_FAILED)
3127 /* The `skip_base_block` function tells us if we
3128 * should continue with skipping, or we should stop
3129 * skipping. We're trying to skip everything up to
3130 * a base FILE block. */
3132 if(lret != ARCHIVE_RETRY) {
3133 /* If there was an error during skipping, or we
3134 * have just skipped a FILE base block... */
3136 if(rar->main.endarc == 0) {
3148 /* Merges the partial block from the first multivolume archive file, and
3149 * partial block from the second multivolume archive file. The result is
3150 * a chunk of memory containing the whole block, and the stream pointer
3151 * is advanced to the next block in the second multivolume archive file. */
3152 static int merge_block(struct archive_read* a, ssize_t block_size,
3155 struct rar5* rar = get_context(a);
3156 ssize_t cur_block_size, partial_offset = 0;
3160 if(rar->merge_mode) {
3161 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3162 "Recursive merge is not allowed");
3164 return ARCHIVE_FATAL;
3167 /* Set a flag that we're in the switching mode. */
3168 rar->cstate.switch_multivolume = 1;
3170 /* Reallocate the memory which will hold the whole block. */
3171 if(rar->vol.push_buf)
3172 free((void*) rar->vol.push_buf);
3174 /* Increasing the allocation block by 8 is due to bit reading functions,
3175 * which are using additional 2 or 4 bytes. Allocating the block size
3176 * by exact value would make bit reader perform reads from invalid
3177 * memory block when reading the last byte from the buffer. */
3178 rar->vol.push_buf = malloc(block_size + 8);
3179 if(!rar->vol.push_buf) {
3180 archive_set_error(&a->archive, ENOMEM,
3181 "Can't allocate memory for a merge block buffer.");
3182 return ARCHIVE_FATAL;
3185 /* Valgrind complains if the extension block for bit reader is not
3186 * initialized, so initialize it. */
3187 memset(&rar->vol.push_buf[block_size], 0, 8);
3189 /* A single block can span across multiple multivolume archive files,
3190 * so we use a loop here. This loop will consume enough multivolume
3191 * archive files until the whole block is read. */
3194 /* Get the size of current block chunk in this multivolume
3195 * archive file and read it. */
3196 cur_block_size = rar5_min(rar->file.bytes_remaining,
3197 block_size - partial_offset);
3199 if(cur_block_size == 0) {
3200 archive_set_error(&a->archive,
3201 ARCHIVE_ERRNO_FILE_FORMAT,
3202 "Encountered block size == 0 during block merge");
3203 return ARCHIVE_FATAL;
3206 if(!read_ahead(a, cur_block_size, &lp))
3209 /* Sanity check; there should never be a situation where this
3210 * function reads more data than the block's size. */
3211 if(partial_offset + cur_block_size > block_size) {
3212 archive_set_error(&a->archive,
3213 ARCHIVE_ERRNO_PROGRAMMER,
3214 "Consumed too much data when merging blocks.");
3215 return ARCHIVE_FATAL;
3218 /* Merge previous block chunk with current block chunk,
3219 * or create first block chunk if this is our first
3221 memcpy(&rar->vol.push_buf[partial_offset], lp, cur_block_size);
3223 /* Advance the stream read pointer by this block chunk size. */
3224 if(ARCHIVE_OK != consume(a, cur_block_size))
3227 /* Update the pointers. `partial_offset` contains information
3228 * about the sum of merged block chunks. */
3229 partial_offset += cur_block_size;
3230 rar->file.bytes_remaining -= cur_block_size;
3232 /* If `partial_offset` is the same as `block_size`, this means
3233 * we've merged all block chunks and we have a valid full
3235 if(partial_offset == block_size) {
3239 /* If we don't have any bytes to read, this means we should
3240 * switch to another multivolume archive file. */
3241 if(rar->file.bytes_remaining == 0) {
3243 ret = advance_multivolume(a);
3245 if(ret != ARCHIVE_OK) {
3251 *p = rar->vol.push_buf;
3253 /* If we're here, we can resume unpacking by processing the block
3254 * pointed to by the `*p` memory pointer. */
3259 static int process_block(struct archive_read* a) {
3261 struct rar5* rar = get_context(a);
3264 /* If we don't have any data to be processed, this most probably means
3265 * we need to switch to the next volume. */
3266 if(rar->main.volume && rar->file.bytes_remaining == 0) {
3267 ret = advance_multivolume(a);
3268 if(ret != ARCHIVE_OK)
3272 if(rar->cstate.block_parsing_finished) {
3275 /* The header size won't be bigger than 6 bytes. */
3276 if(!read_ahead(a, 6, &p)) {
3277 /* Failed to prefetch data block header. */
3282 * Read block_size by parsing block header. Validate the header
3283 * by calculating CRC byte stored inside the header. Size of
3284 * the header is not constant (block size can be stored either
3285 * in 1 or 2 bytes), that's why block size is left out from the
3286 * `compressed_block_header` structure and returned by
3287 * `parse_block_header` as the second argument. */
3289 ret = parse_block_header(a, p, &block_size,
3290 &rar->last_block_hdr);
3291 if(ret != ARCHIVE_OK) {
3295 /* Skip block header. Next data is huffman tables,
3297 ssize_t to_skip = sizeof(struct compressed_block_header) +
3298 bf_byte_count(&rar->last_block_hdr) + 1;
3300 if(ARCHIVE_OK != consume(a, to_skip))
3303 rar->file.bytes_remaining -= to_skip;
3305 /* The block size gives information about the whole block size,
3306 * but the block could be stored in split form when using
3307 * multi-volume archives. In this case, the block size will be
3308 * bigger than the actual data stored in this file. Remaining
3309 * part of the data will be in another file. */
3311 ssize_t cur_block_size =
3312 rar5_min(rar->file.bytes_remaining, block_size);
3314 if(block_size > rar->file.bytes_remaining) {
3315 /* If current blocks' size is bigger than our data
3316 * size, this means we have a multivolume archive.
3317 * In this case, skip all base headers until the end
3318 * of the file, proceed to next "partXXX.rar" volume,
3319 * find its signature, skip all headers up to the first
3320 * FILE base header, and continue from there.
3322 * Note that `merge_block` will update the `rar`
3323 * context structure quite extensively. */
3325 ret = merge_block(a, block_size, &p);
3326 if(ret != ARCHIVE_OK) {
3330 cur_block_size = block_size;
3332 /* Current stream pointer should be now directly
3333 * *after* the block that spanned through multiple
3334 * archive files. `p` pointer should have the data of
3335 * the *whole* block (merged from partial blocks
3336 * stored in multiple archives files). */
3338 rar->cstate.switch_multivolume = 0;
3340 /* Read the whole block size into memory. This can take
3341 * up to 8 megabytes of memory in theoretical cases.
3342 * Might be worth to optimize this and use a standard
3343 * chunk of 4kb's. */
3344 if(!read_ahead(a, 4 + cur_block_size, &p)) {
3345 /* Failed to prefetch block data. */
3350 rar->cstate.block_buf = p;
3351 rar->cstate.cur_block_size = cur_block_size;
3352 rar->cstate.block_parsing_finished = 0;
3354 rar->bits.in_addr = 0;
3355 rar->bits.bit_addr = 0;
3357 if(bf_is_table_present(&rar->last_block_hdr)) {
3358 /* Load Huffman tables. */
3359 ret = parse_tables(a, rar, p);
3360 if(ret != ARCHIVE_OK) {
3361 /* Error during decompression of Huffman
3367 /* Block parsing not finished, reuse previous memory buffer. */
3368 p = rar->cstate.block_buf;
3371 /* Uncompress the block, or a part of it, depending on how many bytes
3372 * will be generated by uncompressing the block.
3374 * In case too many bytes will be generated, calling this function
3375 * again will resume the uncompression operation. */
3376 ret = do_uncompress_block(a, p);
3377 if(ret != ARCHIVE_OK) {
3381 if(rar->cstate.block_parsing_finished &&
3382 rar->cstate.switch_multivolume == 0 &&
3383 rar->cstate.cur_block_size > 0)
3385 /* If we're processing a normal block, consume the whole
3386 * block. We can do this because we've already read the whole
3387 * block to memory. */
3388 if(ARCHIVE_OK != consume(a, rar->cstate.cur_block_size))
3389 return ARCHIVE_FATAL;
3391 rar->file.bytes_remaining -= rar->cstate.cur_block_size;
3392 } else if(rar->cstate.switch_multivolume) {
3393 /* Don't consume the block if we're doing multivolume
3394 * processing. The volume switching function will consume
3395 * the proper count of bytes instead. */
3396 rar->cstate.switch_multivolume = 0;
3402 /* Pops the `buf`, `size` and `offset` from the "data ready" stack.
3404 * Returns ARCHIVE_OK when those arguments can be used, ARCHIVE_RETRY
3405 * when there is no data on the stack. */
3406 static int use_data(struct rar5* rar, const void** buf, size_t* size,
3411 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
3412 struct data_ready *d = &rar->cstate.dready[i];
3415 if(buf) *buf = d->buf;
3416 if(size) *size = d->size;
3417 if(offset) *offset = d->offset;
3424 return ARCHIVE_RETRY;
3427 /* Pushes the `buf`, `size` and `offset` arguments to the rar->cstate.dready
3428 * FIFO stack. Those values will be popped from this stack by the `use_data`
3430 static int push_data_ready(struct archive_read* a, struct rar5* rar,
3431 const uint8_t* buf, size_t size, int64_t offset)
3435 /* Don't push if we're in skip mode. This is needed because solid
3436 * streams need full processing even if we're skipping data. After
3437 * fully processing the stream, we need to discard the generated bytes,
3438 * because we're interested only in the side effect: building up the
3439 * internal window circular buffer. This window buffer will be used
3440 * later during unpacking of requested data. */
3445 if(offset != rar->file.last_offset + rar->file.last_size) {
3446 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3447 "Sanity check error: output stream is not continuous");
3448 return ARCHIVE_FATAL;
3451 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
3452 struct data_ready* d = &rar->cstate.dready[i];
3459 /* These fields are used only in sanity checking. */
3460 rar->file.last_offset = offset;
3461 rar->file.last_size = size;
3463 /* Calculate the checksum of this new block before
3464 * submitting data to libarchive's engine. */
3465 update_crc(rar, d->buf, d->size);
3471 /* Program counter will reach this code if the `rar->cstate.data_ready`
3472 * stack will be filled up so that no new entries will be allowed. The
3473 * code shouldn't allow such situation to occur. So we treat this case
3474 * as an internal error. */
3476 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3477 "Error: premature end of data_ready stack");
3478 return ARCHIVE_FATAL;
3481 /* This function uncompresses the data that is stored in the <FILE> base
3484 * The FILE base block looks like this:
3486 * <header><huffman tables><block_1><block_2>...<block_n>
3488 * The <header> is a block header, that is parsed in parse_block_header().
3489 * It's a "compressed_block_header" structure, containing metadata needed
3490 * to know when we should stop looking for more <block_n> blocks.
3492 * <huffman tables> contain data needed to set up the huffman tables, needed
3493 * for the actual decompression.
3495 * Each <block_n> consists of series of literals:
3497 * <literal><literal><literal>...<literal>
3499 * Those literals generate the uncompression data. They operate on a circular
3500 * buffer, sometimes writing raw data into it, sometimes referencing
3501 * some previous data inside this buffer, and sometimes declaring a filter
3502 * that will need to be executed on the data stored in the circular buffer.
3503 * It all depends on the literal that is used.
3505 * Sometimes blocks produce output data, sometimes they don't. For example, for
3506 * some huge files that use lots of filters, sometimes a block is filled with
3507 * only filter declaration literals. Such blocks won't produce any data in the
3510 * Sometimes blocks will produce 4 bytes of data, and sometimes 1 megabyte,
3511 * because a literal can reference previously decompressed data. For example,
3512 * there can be a literal that says: 'append a byte 0xFE here', and after
3513 * it another literal can say 'append 1 megabyte of data from circular buffer
3514 * offset 0x12345'. This is how RAR format handles compressing repeated
3517 * The RAR compressor creates those literals and the actual efficiency of
3518 * compression depends on what those literals are. The literals can also
3519 * be seen as a kind of a non-turing-complete virtual machine that simply
3520 * tells the decompressor what it should do.
3523 static int do_uncompress_file(struct archive_read* a) {
3524 struct rar5* rar = get_context(a);
3526 int64_t max_end_pos;
3528 if(!rar->cstate.initialized) {
3529 /* Don't perform full context reinitialization if we're
3530 * processing a solid archive. */
3531 if(!rar->main.solid || !rar->cstate.window_buf) {
3535 rar->cstate.initialized = 1;
3538 if(rar->cstate.all_filters_applied == 1) {
3539 /* We use while(1) here, but standard case allows for just 1
3540 * iteration. The loop will iterate if process_block() didn't
3541 * generate any data at all. This can happen if the block
3542 * contains only filter definitions (this is common in big
3545 ret = process_block(a);
3546 if(ret == ARCHIVE_EOF || ret == ARCHIVE_FATAL)
3549 if(rar->cstate.last_write_ptr ==
3550 rar->cstate.write_ptr) {
3551 /* The block didn't generate any new data,
3552 * so just process a new block. */
3556 /* The block has generated some new data, so break
3562 /* Try to run filters. If filters won't be applied, it means that
3563 * insufficient data was generated. */
3564 ret = apply_filters(a);
3565 if(ret == ARCHIVE_RETRY) {
3567 } else if(ret == ARCHIVE_FATAL) {
3568 return ARCHIVE_FATAL;
3571 /* If apply_filters() will return ARCHIVE_OK, we can continue here. */
3573 if(cdeque_size(&rar->cstate.filters) > 0) {
3574 /* Check if we can write something before hitting first
3576 struct filter_info* flt;
3578 /* Get the block_start offset from the first filter. */
3579 if(CDE_OK != cdeque_front(&rar->cstate.filters,
3580 cdeque_filter_p(&flt)))
3582 archive_set_error(&a->archive,
3583 ARCHIVE_ERRNO_PROGRAMMER,
3584 "Can't read first filter");
3585 return ARCHIVE_FATAL;
3588 max_end_pos = rar5_min(flt->block_start,
3589 rar->cstate.write_ptr);
3591 /* There are no filters defined, or all filters were applied.
3592 * This means we can just store the data without any
3593 * postprocessing. */
3594 max_end_pos = rar->cstate.write_ptr;
3597 if(max_end_pos == rar->cstate.last_write_ptr) {
3598 /* We can't write anything yet. The block uncompression
3599 * function did not generate enough data, and no filter can be
3600 * applied. At the same time we don't have any data that can be
3601 * stored without filter postprocessing. This means we need to
3602 * wait for more data to be generated, so we can apply the
3605 * Signal the caller that we need more data to be able to do
3608 return ARCHIVE_RETRY;
3610 /* We can write the data before hitting the first filter.
3611 * So let's do it. The push_window_data() function will
3612 * effectively return the selected data block to the user
3614 push_window_data(a, rar, rar->cstate.last_write_ptr,
3616 rar->cstate.last_write_ptr = max_end_pos;
3622 static int uncompress_file(struct archive_read* a) {
3626 /* Sometimes the uncompression function will return a
3627 * 'retry' signal. If this will happen, we have to retry
3629 ret = do_uncompress_file(a);
3630 if(ret != ARCHIVE_RETRY)
3636 static int do_unstore_file(struct archive_read* a,
3637 struct rar5* rar, const void** buf, size_t* size, int64_t* offset)
3641 if(rar->file.bytes_remaining == 0 && rar->main.volume > 0 &&
3642 rar->generic.split_after > 0)
3646 rar->cstate.switch_multivolume = 1;
3647 ret = advance_multivolume(a);
3648 rar->cstate.switch_multivolume = 0;
3650 if(ret != ARCHIVE_OK) {
3651 /* Failed to advance to next multivolume archive
3657 size_t to_read = rar5_min(rar->file.bytes_remaining, 64 * 1024);
3662 if(!read_ahead(a, to_read, &p)) {
3663 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3664 "I/O error when unstoring file");
3665 return ARCHIVE_FATAL;
3668 if(ARCHIVE_OK != consume(a, to_read)) {
3673 if(size) *size = to_read;
3674 if(offset) *offset = rar->cstate.last_unstore_ptr;
3676 rar->file.bytes_remaining -= to_read;
3677 rar->cstate.last_unstore_ptr += to_read;
3679 update_crc(rar, p, to_read);
3683 static int do_unpack(struct archive_read* a, struct rar5* rar,
3684 const void** buf, size_t* size, int64_t* offset)
3686 enum COMPRESSION_METHOD {
3687 STORE = 0, FASTEST = 1, FAST = 2, NORMAL = 3, GOOD = 4,
3691 if(rar->file.service > 0) {
3692 return do_unstore_file(a, rar, buf, size, offset);
3694 switch(rar->cstate.method) {
3696 return do_unstore_file(a, rar, buf, size,
3707 return uncompress_file(a);
3709 archive_set_error(&a->archive,
3710 ARCHIVE_ERRNO_FILE_FORMAT,
3711 "Compression method not supported: 0x%x",
3712 rar->cstate.method);
3714 return ARCHIVE_FATAL;
3724 static int verify_checksums(struct archive_read* a) {
3726 struct rar5* rar = get_context(a);
3728 /* Check checksums only when actually unpacking the data. There's no
3729 * need to calculate checksum when we're skipping data in solid archives
3730 * (skipping in solid archives is the same thing as unpacking compressed
3731 * data and discarding the result). */
3733 if(!rar->skip_mode) {
3734 /* Always check checksums if we're not in skip mode */
3737 /* We can override the logic above with a compile-time option
3738 * NO_CRC_ON_SOLID_SKIP. This option is used during debugging,
3739 * and it will check checksums of unpacked data even when
3740 * we're skipping it. */
3742 #if defined CHECK_CRC_ON_SOLID_SKIP
3752 /* During unpacking, on each unpacked block we're calling the
3753 * update_crc() function. Since we are here, the unpacking
3754 * process is already over and we can check if calculated
3755 * checksum (CRC32 or BLAKE2sp) is the same as what is stored
3756 * in the archive. */
3757 if(rar->file.stored_crc32 > 0) {
3758 /* Check CRC32 only when the file contains a CRC32
3759 * value for this file. */
3761 if(rar->file.calculated_crc32 !=
3762 rar->file.stored_crc32) {
3763 /* Checksums do not match; the unpacked file
3767 printf("Checksum error: CRC32 "
3768 "(was: %08x, expected: %08x)\n",
3769 rar->file.calculated_crc32,
3770 rar->file.stored_crc32);
3773 #ifndef DONT_FAIL_ON_CRC_ERROR
3774 archive_set_error(&a->archive,
3775 ARCHIVE_ERRNO_FILE_FORMAT,
3776 "Checksum error: CRC32");
3777 return ARCHIVE_FATAL;
3781 printf("Checksum OK: CRC32 "
3783 rar->file.stored_crc32,
3784 rar->file.calculated_crc32);
3789 if(rar->file.has_blake2 > 0) {
3790 /* BLAKE2sp is an optional checksum algorithm that is
3791 * added to RARv5 archives when using the `-htb` switch
3792 * during creation of archive.
3794 * We now finalize the hash calculation by calling the
3795 * `final` function. This will generate the final hash
3796 * value we can use to compare it with the BLAKE2sp
3797 * checksum that is stored in the archive.
3799 * The return value of this `final` function is not
3800 * very helpful, as it guards only against improper use.
3801 * This is why we're explicitly ignoring it. */
3804 (void) blake2sp_final(&rar->file.b2state, b2_buf, 32);
3806 if(memcmp(&rar->file.blake2sp, b2_buf, 32) != 0) {
3807 #ifndef DONT_FAIL_ON_CRC_ERROR
3808 archive_set_error(&a->archive,
3809 ARCHIVE_ERRNO_FILE_FORMAT,
3810 "Checksum error: BLAKE2");
3812 return ARCHIVE_FATAL;
3818 /* Finalization for this file has been successfully completed. */
3822 static int verify_global_checksums(struct archive_read* a) {
3823 return verify_checksums(a);
3826 static int rar5_read_data(struct archive_read *a, const void **buff,
3827 size_t *size, int64_t *offset) {
3829 struct rar5* rar = get_context(a);
3831 if(rar->file.dir > 0) {
3832 /* Don't process any data if this file entry was declared
3833 * as a directory. This is needed, because entries marked as
3834 * directory doesn't have any dictionary buffer allocated, so
3835 * it's impossible to perform any decompression. */
3836 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3837 "Can't decompress an entry marked as a directory");
3838 return ARCHIVE_FAILED;
3841 if(!rar->skip_mode && (rar->cstate.last_write_ptr > rar->file.unpacked_size)) {
3842 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3843 "Unpacker has written too many bytes");
3844 return ARCHIVE_FATAL;
3847 ret = use_data(rar, buff, size, offset);
3848 if(ret == ARCHIVE_OK) {
3852 if(rar->file.eof == 1) {
3856 ret = do_unpack(a, rar, buff, size, offset);
3857 if(ret != ARCHIVE_OK) {
3861 if(rar->file.bytes_remaining == 0 &&
3862 rar->cstate.last_write_ptr == rar->file.unpacked_size)
3864 /* If all bytes of current file were processed, run
3867 * Finalization will check checksum against proper values. If
3868 * some of the checksums will not match, we'll return an error
3869 * value in the last `archive_read_data` call to signal an error
3873 return verify_global_checksums(a);
3879 static int rar5_read_data_skip(struct archive_read *a) {
3880 struct rar5* rar = get_context(a);
3882 if(rar->main.solid) {
3883 /* In solid archives, instead of skipping the data, we need to
3884 * extract it, and dispose the result. The side effect of this
3885 * operation will be setting up the initial window buffer state
3886 * needed to be able to extract the selected file. */
3890 /* Make sure to process all blocks in the compressed stream. */
3891 while(rar->file.bytes_remaining > 0) {
3892 /* Setting the "skip mode" will allow us to skip
3893 * checksum checks during data skipping. Checking the
3894 * checksum of skipped data isn't really necessary and
3895 * it's only slowing things down.
3897 * This is incremented instead of setting to 1 because
3898 * this data skipping function can be called
3902 /* We're disposing 1 block of data, so we use triple
3903 * NULLs in arguments. */
3904 ret = rar5_read_data(a, NULL, NULL, NULL);
3906 /* Turn off "skip mode". */
3910 /* Propagate any potential error conditions
3916 /* In standard archives, we can just jump over the compressed
3917 * stream. Each file in non-solid archives starts from an empty
3920 if(ARCHIVE_OK != consume(a, rar->file.bytes_remaining)) {
3921 return ARCHIVE_FATAL;
3924 rar->file.bytes_remaining = 0;
3930 static int64_t rar5_seek_data(struct archive_read *a, int64_t offset,
3937 /* We're a streaming unpacker, and we don't support seeking. */
3939 return ARCHIVE_FATAL;
3942 static int rar5_cleanup(struct archive_read *a) {
3943 struct rar5* rar = get_context(a);
3945 free(rar->cstate.window_buf);
3946 free(rar->cstate.filtered_buf);
3948 free(rar->vol.push_buf);
3951 cdeque_free(&rar->cstate.filters);
3954 a->format->data = NULL;
3959 static int rar5_capabilities(struct archive_read * a) {
3964 static int rar5_has_encrypted_entries(struct archive_read *_a) {
3967 /* Unsupported for now. */
3968 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
3971 static int rar5_init(struct rar5* rar) {
3974 memset(rar, 0, sizeof(struct rar5));
3976 /* Decrypt the magic signature pattern. Check the comment near the
3977 * `rar5_signature` symbol to read the rationale behind this. */
3979 if(rar5_signature[0] == 243) {
3980 for(i = 0; i < rar5_signature_size; i++) {
3981 rar5_signature[i] ^= 0xA1;
3985 if(CDE_OK != cdeque_init(&rar->cstate.filters, 8192))
3986 return ARCHIVE_FATAL;
3991 int archive_read_support_format_rar5(struct archive *_a) {
3992 struct archive_read* ar;
3996 if(ARCHIVE_OK != (ret = get_archive_read(_a, &ar)))
3999 rar = malloc(sizeof(*rar));
4001 archive_set_error(&ar->archive, ENOMEM,
4002 "Can't allocate rar5 data");
4003 return ARCHIVE_FATAL;
4006 if(ARCHIVE_OK != rar5_init(rar)) {
4007 archive_set_error(&ar->archive, ENOMEM,
4008 "Can't allocate rar5 filter buffer");
4009 return ARCHIVE_FATAL;
4012 ret = __archive_read_register_format(ar,
4019 rar5_read_data_skip,
4023 rar5_has_encrypted_entries);
4025 if(ret != ARCHIVE_OK) {
4026 (void) rar5_cleanup(ar);