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)
66 #define LOG(...) do { printf("rar5: " __VA_ARGS__); puts(""); } while(0)
68 #define DEBUG_CODE if(0)
71 /* Real RAR5 magic number is:
73 * 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00
76 * Retrieved with `rar5_signature()` by XOR'ing it with 0xA1, because I don't
77 * want to put this magic sequence in each binary that uses libarchive, so
78 * applications that scan through the file for this marker won't trigger on
81 * The array itself is decrypted in `rar5_init` function. */
83 static unsigned char rar5_signature_xor[] = { 243, 192, 211, 128, 187, 166, 160, 161 };
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;
119 ssize_t solid_window_size; /* Used in file format check. */
132 #define REDIR_SYMLINK_IS_DIR 1
136 REDIR_TYPE_UNIXSYMLINK = 1,
137 REDIR_TYPE_WINSYMLINK = 2,
138 REDIR_TYPE_JUNCTION = 3,
139 REDIR_TYPE_HARDLINK = 4,
140 REDIR_TYPE_FILECOPY = 5,
143 #define OWNER_USER_NAME 0x01
144 #define OWNER_GROUP_NAME 0x02
145 #define OWNER_USER_UID 0x04
146 #define OWNER_GROUP_GID 0x08
147 #define OWNER_MAXNAMELEN 256
150 FILTER_DELTA = 0, /* Generic pattern. */
151 FILTER_E8 = 1, /* Intel x86 code. */
152 FILTER_E8E9 = 2, /* Intel x86 code. */
153 FILTER_ARM = 3, /* ARM code. */
154 FILTER_AUDIO = 4, /* Audio filter, not used in RARv5. */
155 FILTER_RGB = 5, /* Color palette, not used in RARv5. */
156 FILTER_ITANIUM = 6, /* Intel's Itanium, not used in RARv5. */
157 FILTER_PPM = 7, /* Predictive pattern matching, not used in
168 ssize_t block_length;
187 struct decode_table {
189 int32_t decode_len[16];
190 uint32_t decode_pos[16];
192 uint8_t quick_len[1 << 10];
193 uint16_t quick_num[1 << 10];
194 uint16_t decode_num[306];
198 /* Flag used to specify if unpacker needs to reinitialize the
199 uncompression context. */
200 uint8_t initialized : 1;
202 /* Flag used when applying filters. */
203 uint8_t all_filters_applied : 1;
205 /* Flag used to skip file context reinitialization, used when unpacker
206 is skipping through different multivolume archives. */
207 uint8_t switch_multivolume : 1;
209 /* Flag used to specify if unpacker has processed the whole data block
210 or just a part of it. */
211 uint8_t block_parsing_finished : 1;
213 signed int notused : 4;
215 int flags; /* Uncompression flags. */
216 int method; /* Uncompression algorithm method. */
217 int version; /* Uncompression algorithm version. */
218 ssize_t window_size; /* Size of window_buf. */
219 uint8_t* window_buf; /* Circular buffer used during
221 uint8_t* filtered_buf; /* Buffer used when applying filters. */
222 const uint8_t* block_buf; /* Buffer used when merging blocks. */
223 size_t window_mask; /* Convenience field; window_size - 1. */
224 int64_t write_ptr; /* This amount of data has been unpacked
225 in the window buffer. */
226 int64_t last_write_ptr; /* This amount of data has been stored in
228 int64_t last_unstore_ptr; /* Counter of bytes extracted during
229 unstoring. This is separate from
230 last_write_ptr because of how SERVICE
231 base blocks are handled during skipping
232 in solid multiarchive archives. */
233 int64_t solid_offset; /* Additional offset inside the window
234 buffer, used in unpacking solid
236 ssize_t cur_block_size; /* Size of current data block. */
237 int last_len; /* Flag used in lzss decompression. */
239 /* Decode tables used during lzss uncompression. */
242 struct decode_table bd; /* huffman bit lengths */
244 struct decode_table ld; /* literals */
246 struct decode_table dd; /* distances */
248 struct decode_table ldd; /* lower bits of distances */
250 struct decode_table rd; /* repeating distances */
251 #define HUFF_TABLE_SIZE (HUFF_NC + HUFF_DC + HUFF_RC + HUFF_LDC)
253 /* Circular deque for storing filters. */
254 struct cdeque filters;
255 int64_t last_block_start; /* Used for sanity checking. */
256 ssize_t last_block_length; /* Used for sanity checking. */
258 /* Distance cache used during lzss uncompression. */
261 /* Data buffer stack. */
262 struct data_ready dready[2];
265 /* Bit reader state. */
267 int8_t bit_addr; /* Current bit pointer inside current byte. */
268 int in_addr; /* Current byte pointer. */
271 /* RARv5 block header structure. Use bf_* functions to get values from
272 * block_flags_u8 field. I.e. bf_byte_count, etc. */
273 struct compressed_block_header {
274 /* block_flags_u8 contain fields encoded in little-endian bitfield:
276 * - table present flag (shr 7, and 1),
277 * - last block flag (shr 6, and 1),
278 * - byte_count (shr 3, and 7),
279 * - bit_size (shr 0, and 7).
281 uint8_t block_flags_u8;
285 /* RARv5 main header structure. */
287 /* Does the archive contain solid streams? */
290 /* If this a multi-file archive? */
298 struct generic_header {
299 uint8_t split_after : 1;
300 uint8_t split_before : 1;
307 unsigned int expected_vol_no;
311 /* Main context structure. */
313 int header_initialized;
315 /* Set to 1 if current file is positioned AFTER the magic value
316 * of the archive file. This is used in header reading functions. */
319 /* Set to not zero if we're in skip mode (either by calling
320 * rar5_data_skip function or when skipping over solid streams).
321 * Set to 0 when in * extraction mode. This is used during checksum
322 * calculation functions. */
325 /* Set to not zero if we're in block merging mode (i.e. when switching
326 * to another file in multivolume archive, last block from 1st archive
327 * needs to be merged with 1st block from 2nd archive). This flag
328 * guards against recursive use of the merging function, which doesn't
329 * support recursive calls. */
332 /* An offset to QuickOpen list. This is not supported by this unpacker,
333 * because we're focusing on streaming interface. QuickOpen is designed
334 * to make things quicker for non-stream interfaces, so it's not our
336 uint64_t qlist_offset;
338 /* An offset to additional Recovery data. This is not supported by this
339 * unpacker. Recovery data are additional Reed-Solomon codes that could
340 * be used to calculate bytes that are missing in archive or are
344 /* Various context variables grouped to different structures. */
345 struct generic_header generic;
346 struct main_header main;
347 struct comp_state cstate;
348 struct file_header file;
349 struct bit_reader bits;
350 struct multivolume vol;
352 /* The header of currently processed RARv5 block. Used in main
353 * decompression logic loop. */
354 struct compressed_block_header last_block_hdr;
357 /* Forward function declarations. */
359 static void rar5_signature(char *buf);
360 static int verify_global_checksums(struct archive_read* a);
361 static int rar5_read_data_skip(struct archive_read *a);
362 static int push_data_ready(struct archive_read* a, struct rar5* rar,
363 const uint8_t* buf, size_t size, int64_t offset);
365 /* CDE_xxx = Circular Double Ended (Queue) return values. */
366 enum CDE_RETURN_VALUES {
367 CDE_OK, CDE_ALLOC, CDE_PARAM, CDE_OUT_OF_BOUNDS,
370 /* Clears the contents of this circular deque. */
371 static void cdeque_clear(struct cdeque* d) {
377 /* Creates a new circular deque object. Capacity must be power of 2: 8, 16, 32,
378 * 64, 256, etc. When the user will add another item above current capacity,
379 * the circular deque will overwrite the oldest entry. */
380 static int cdeque_init(struct cdeque* d, int max_capacity_power_of_2) {
381 if(d == NULL || max_capacity_power_of_2 == 0)
384 d->cap_mask = max_capacity_power_of_2 - 1;
387 if((max_capacity_power_of_2 & d->cap_mask) != 0)
391 d->arr = malloc(sizeof(void*) * max_capacity_power_of_2);
393 return d->arr ? CDE_OK : CDE_ALLOC;
396 /* Return the current size (not capacity) of circular deque `d`. */
397 static size_t cdeque_size(struct cdeque* d) {
401 /* Returns the first element of current circular deque. Note that this function
402 * doesn't perform any bounds checking. If you need bounds checking, use
403 * `cdeque_front()` function instead. */
404 static void cdeque_front_fast(struct cdeque* d, void** value) {
405 *value = (void*) d->arr[d->beg_pos];
408 /* Returns the first element of current circular deque. This function
409 * performs bounds checking. */
410 static int cdeque_front(struct cdeque* d, void** value) {
412 cdeque_front_fast(d, value);
415 return CDE_OUT_OF_BOUNDS;
418 /* Pushes a new element into the end of this circular deque object. If current
419 * size will exceed capacity, the oldest element will be overwritten. */
420 static int cdeque_push_back(struct cdeque* d, void* item) {
424 if(d->size == d->cap_mask + 1)
425 return CDE_OUT_OF_BOUNDS;
427 d->arr[d->end_pos] = (size_t) item;
428 d->end_pos = (d->end_pos + 1) & d->cap_mask;
434 /* Pops a front element of this circular deque object and returns its value.
435 * This function doesn't perform any bounds checking. */
436 static void cdeque_pop_front_fast(struct cdeque* d, void** value) {
437 *value = (void*) d->arr[d->beg_pos];
438 d->beg_pos = (d->beg_pos + 1) & d->cap_mask;
442 /* Pops a front element of this circular deque object and returns its value.
443 * This function performs bounds checking. */
444 static int cdeque_pop_front(struct cdeque* d, void** value) {
449 return CDE_OUT_OF_BOUNDS;
451 cdeque_pop_front_fast(d, value);
455 /* Convenience function to cast filter_info** to void **. */
456 static void** cdeque_filter_p(struct filter_info** f) {
457 return (void**) (size_t) f;
460 /* Convenience function to cast filter_info* to void *. */
461 static void* cdeque_filter(struct filter_info* f) {
462 return (void**) (size_t) f;
465 /* Destroys this circular deque object. Deallocates the memory of the
466 * collection buffer, but doesn't deallocate the memory of any pointer passed
467 * to this deque as a value. */
468 static void cdeque_free(struct cdeque* d) {
484 uint8_t bf_bit_size(const struct compressed_block_header* hdr) {
485 return hdr->block_flags_u8 & 7;
489 uint8_t bf_byte_count(const struct compressed_block_header* hdr) {
490 return (hdr->block_flags_u8 >> 3) & 7;
494 uint8_t bf_is_table_present(const struct compressed_block_header* hdr) {
495 return (hdr->block_flags_u8 >> 7) & 1;
498 static inline struct rar5* get_context(struct archive_read* a) {
499 return (struct rar5*) a->format->data;
502 /* Convenience functions used by filter implementations. */
503 static void circular_memcpy(uint8_t* dst, uint8_t* window, const uint64_t mask,
504 int64_t start, int64_t end)
506 if((start & mask) > (end & mask)) {
507 ssize_t len1 = mask + 1 - (start & mask);
508 ssize_t len2 = end & mask;
510 memcpy(dst, &window[start & mask], len1);
511 memcpy(dst + len1, window, len2);
513 memcpy(dst, &window[start & mask], (size_t) (end - start));
517 static uint32_t read_filter_data(struct rar5* rar, uint32_t offset) {
518 uint8_t linear_buf[4];
519 circular_memcpy(linear_buf, rar->cstate.window_buf,
520 rar->cstate.window_mask, offset, offset + 4);
521 return archive_le32dec(linear_buf);
524 static void write_filter_data(struct rar5* rar, uint32_t offset,
527 archive_le32enc(&rar->cstate.filtered_buf[offset], value);
530 /* Allocates a new filter descriptor and adds it to the filter array. */
531 static struct filter_info* add_new_filter(struct rar5* rar) {
532 struct filter_info* f =
533 (struct filter_info*) calloc(1, sizeof(struct filter_info));
539 cdeque_push_back(&rar->cstate.filters, cdeque_filter(f));
543 static int run_delta_filter(struct rar5* rar, struct filter_info* flt) {
545 ssize_t dest_pos, src_pos = 0;
547 for(i = 0; i < flt->channels; i++) {
548 uint8_t prev_byte = 0;
550 dest_pos < flt->block_length;
551 dest_pos += flt->channels)
555 byte = rar->cstate.window_buf[
556 (rar->cstate.solid_offset + flt->block_start +
557 src_pos) & rar->cstate.window_mask];
560 rar->cstate.filtered_buf[dest_pos] = prev_byte;
568 static int run_e8e9_filter(struct rar5* rar, struct filter_info* flt,
571 const uint32_t file_size = 0x1000000;
574 circular_memcpy(rar->cstate.filtered_buf,
575 rar->cstate.window_buf, rar->cstate.window_mask,
576 rar->cstate.solid_offset + flt->block_start,
577 rar->cstate.solid_offset + flt->block_start + flt->block_length);
579 for(i = 0; i < flt->block_length - 4;) {
580 uint8_t b = rar->cstate.window_buf[
581 (rar->cstate.solid_offset + flt->block_start +
582 i++) & rar->cstate.window_mask];
585 * 0xE8 = x86's call <relative_addr_uint32> (function call)
586 * 0xE9 = x86's jmp <relative_addr_uint32> (unconditional jump)
588 if(b == 0xE8 || (extended && b == 0xE9)) {
591 uint32_t offset = (i + flt->block_start) % file_size;
593 addr = read_filter_data(rar,
594 (uint32_t)(rar->cstate.solid_offset +
595 flt->block_start + i) & rar->cstate.window_mask);
597 if(addr & 0x80000000) {
598 if(((addr + offset) & 0x80000000) == 0) {
599 write_filter_data(rar, (uint32_t)i,
603 if((addr - file_size) & 0x80000000) {
604 uint32_t naddr = addr - offset;
605 write_filter_data(rar, (uint32_t)i,
617 static int run_arm_filter(struct rar5* rar, struct filter_info* flt) {
621 circular_memcpy(rar->cstate.filtered_buf,
622 rar->cstate.window_buf, rar->cstate.window_mask,
623 rar->cstate.solid_offset + flt->block_start,
624 rar->cstate.solid_offset + flt->block_start + flt->block_length);
626 for(i = 0; i < flt->block_length - 3; i += 4) {
627 uint8_t* b = &rar->cstate.window_buf[
628 (rar->cstate.solid_offset +
629 flt->block_start + i + 3) & rar->cstate.window_mask];
632 /* 0xEB = ARM's BL (branch + link) instruction. */
633 offset = read_filter_data(rar,
634 (rar->cstate.solid_offset + flt->block_start + i) &
635 rar->cstate.window_mask) & 0x00ffffff;
637 offset -= (uint32_t) ((i + flt->block_start) / 4);
638 offset = (offset & 0x00ffffff) | 0xeb000000;
639 write_filter_data(rar, (uint32_t)i, offset);
646 static int run_filter(struct archive_read* a, struct filter_info* flt) {
648 struct rar5* rar = get_context(a);
650 free(rar->cstate.filtered_buf);
652 rar->cstate.filtered_buf = malloc(flt->block_length);
653 if(!rar->cstate.filtered_buf) {
654 archive_set_error(&a->archive, ENOMEM,
655 "Can't allocate memory for filter data.");
656 return ARCHIVE_FATAL;
661 ret = run_delta_filter(rar, flt);
667 ret = run_e8e9_filter(rar, flt,
668 flt->type == FILTER_E8E9);
672 ret = run_arm_filter(rar, flt);
676 archive_set_error(&a->archive,
677 ARCHIVE_ERRNO_FILE_FORMAT,
678 "Unsupported filter type: 0x%x", flt->type);
679 return ARCHIVE_FATAL;
682 if(ret != ARCHIVE_OK) {
683 /* Filter has failed. */
687 if(ARCHIVE_OK != push_data_ready(a, rar, rar->cstate.filtered_buf,
688 flt->block_length, rar->cstate.last_write_ptr))
690 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
691 "Stack overflow when submitting unpacked data");
693 return ARCHIVE_FATAL;
696 rar->cstate.last_write_ptr += flt->block_length;
700 /* The `push_data` function submits the selected data range to the user.
701 * Next call of `use_data` will use the pointer, size and offset arguments
702 * that are specified here. These arguments are pushed to the FIFO stack here,
703 * and popped from the stack by the `use_data` function. */
704 static void push_data(struct archive_read* a, struct rar5* rar,
705 const uint8_t* buf, int64_t idx_begin, int64_t idx_end)
707 const uint64_t wmask = rar->cstate.window_mask;
708 const ssize_t solid_write_ptr = (rar->cstate.solid_offset +
709 rar->cstate.last_write_ptr) & wmask;
711 idx_begin += rar->cstate.solid_offset;
712 idx_end += rar->cstate.solid_offset;
714 /* Check if our unpacked data is wrapped inside the window circular
715 * buffer. If it's not wrapped, it can be copied out by using
716 * a single memcpy, but when it's wrapped, we need to copy the first
717 * part with one memcpy, and the second part with another memcpy. */
719 if((idx_begin & wmask) > (idx_end & wmask)) {
720 /* The data is wrapped (begin offset sis bigger than end
722 const ssize_t frag1_size = rar->cstate.window_size -
724 const ssize_t frag2_size = idx_end & wmask;
726 /* Copy the first part of the buffer first. */
727 push_data_ready(a, rar, buf + solid_write_ptr, frag1_size,
728 rar->cstate.last_write_ptr);
730 /* Copy the second part of the buffer. */
731 push_data_ready(a, rar, buf, frag2_size,
732 rar->cstate.last_write_ptr + frag1_size);
734 rar->cstate.last_write_ptr += frag1_size + frag2_size;
736 /* Data is not wrapped, so we can just use one call to copy the
738 push_data_ready(a, rar,
739 buf + solid_write_ptr, (idx_end - idx_begin) & wmask,
740 rar->cstate.last_write_ptr);
742 rar->cstate.last_write_ptr += idx_end - idx_begin;
746 /* Convenience function that submits the data to the user. It uses the
747 * unpack window buffer as a source location. */
748 static void push_window_data(struct archive_read* a, struct rar5* rar,
749 int64_t idx_begin, int64_t idx_end)
751 push_data(a, rar, rar->cstate.window_buf, idx_begin, idx_end);
754 static int apply_filters(struct archive_read* a) {
755 struct filter_info* flt;
756 struct rar5* rar = get_context(a);
759 rar->cstate.all_filters_applied = 0;
761 /* Get the first filter that can be applied to our data. The data
762 * needs to be fully unpacked before the filter can be run. */
763 if(CDE_OK == cdeque_front(&rar->cstate.filters,
764 cdeque_filter_p(&flt))) {
765 /* Check if our unpacked data fully covers this filter's
767 if(rar->cstate.write_ptr > flt->block_start &&
768 rar->cstate.write_ptr >= flt->block_start +
770 /* Check if we have some data pending to be written
771 * right before the filter's start offset. */
772 if(rar->cstate.last_write_ptr == flt->block_start) {
773 /* Run the filter specified by descriptor
775 ret = run_filter(a, flt);
776 if(ret != ARCHIVE_OK) {
777 /* Filter failure, return error. */
781 /* Filter descriptor won't be needed anymore
782 * after it's used, * so remove it from the
783 * filter list and free its memory. */
784 (void) cdeque_pop_front(&rar->cstate.filters,
785 cdeque_filter_p(&flt));
789 /* We can't run filters yet, dump the memory
790 * right before the filter. */
791 push_window_data(a, rar,
792 rar->cstate.last_write_ptr,
796 /* Return 'filter applied or not needed' state to the
798 return ARCHIVE_RETRY;
802 rar->cstate.all_filters_applied = 1;
806 static void dist_cache_push(struct rar5* rar, int value) {
807 int* q = rar->cstate.dist_cache;
815 static int dist_cache_touch(struct rar5* rar, int idx) {
816 int* q = rar->cstate.dist_cache;
817 int i, dist = q[idx];
819 for(i = idx; i > 0; i--)
826 static void free_filters(struct rar5* rar) {
827 struct cdeque* d = &rar->cstate.filters;
829 /* Free any remaining filters. All filters should be naturally
830 * consumed by the unpacking function, so remaining filters after
831 * unpacking normally mean that unpacking wasn't successful.
832 * But still of course we shouldn't leak memory in such case. */
834 /* cdeque_size() is a fast operation, so we can use it as a loop
836 while(cdeque_size(d) > 0) {
837 struct filter_info* f = NULL;
839 /* Pop_front will also decrease the collection's size. */
840 if (CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)))
846 /* Also clear out the variables needed for sanity checking. */
847 rar->cstate.last_block_start = 0;
848 rar->cstate.last_block_length = 0;
851 static void reset_file_context(struct rar5* rar) {
852 memset(&rar->file, 0, sizeof(rar->file));
853 blake2sp_init(&rar->file.b2state, 32);
855 if(rar->main.solid) {
856 rar->cstate.solid_offset += rar->cstate.write_ptr;
858 rar->cstate.solid_offset = 0;
861 rar->cstate.write_ptr = 0;
862 rar->cstate.last_write_ptr = 0;
863 rar->cstate.last_unstore_ptr = 0;
865 rar->file.redir_type = REDIR_TYPE_NONE;
866 rar->file.redir_flags = 0;
871 static inline int get_archive_read(struct archive* a,
872 struct archive_read** ar)
874 *ar = (struct archive_read*) a;
875 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
876 "archive_read_support_format_rar5");
881 static int read_ahead(struct archive_read* a, size_t how_many,
888 *ptr = __archive_read_ahead(a, how_many, &avail);
896 static int consume(struct archive_read* a, int64_t how_many) {
899 ret = how_many == __archive_read_consume(a, how_many)
907 * Read a RAR5 variable sized numeric value. This value will be stored in
908 * `pvalue`. The `pvalue_len` argument points to a variable that will receive
909 * the byte count that was consumed in order to decode the `pvalue` value, plus
912 * pvalue_len is optional and can be NULL.
914 * NOTE: if `pvalue_len` is NOT NULL, the caller needs to manually consume
915 * the number of bytes that `pvalue_len` value contains. If the `pvalue_len`
916 * is NULL, this consuming operation is done automatically.
918 * Returns 1 if *pvalue was successfully read.
919 * Returns 0 if there was an error. In this case, *pvalue contains an
923 static int read_var(struct archive_read* a, uint64_t* pvalue,
924 uint64_t* pvalue_len)
931 /* We will read maximum of 8 bytes. We don't have to handle the
932 * situation to read the RAR5 variable-sized value stored at the end of
933 * the file, because such situation will never happen. */
934 if(!read_ahead(a, 8, &p))
937 for(shift = 0, i = 0; i < 8; i++, shift += 7) {
940 /* Strip the MSB from the input byte and add the resulting
941 * number to the `result`. */
942 result += (b & (uint64_t)0x7F) << shift;
944 /* MSB set to 1 means we need to continue decoding process.
945 * MSB set to 0 means we're done.
947 * This conditional checks for the second case. */
948 if((b & 0x80) == 0) {
953 /* If the caller has passed the `pvalue_len` pointer,
954 * store the number of consumed bytes in it and do NOT
955 * consume those bytes, since the caller has all the
956 * information it needs to perform */
960 /* If the caller did not provide the
961 * `pvalue_len` pointer, it will not have the
962 * possibility to advance the file pointer,
963 * because it will not know how many bytes it
964 * needs to consume. This is why we handle
965 * such situation here automatically. */
966 if(ARCHIVE_OK != consume(a, 1 + i)) {
971 /* End of decoding process, return success. */
976 /* The decoded value takes the maximum number of 8 bytes.
977 * It's a maximum number of bytes, so end decoding process here
978 * even if the first bit of last byte is 1. */
986 if(ARCHIVE_OK != consume(a, 9)) {
994 static int read_var_sized(struct archive_read* a, size_t* pvalue,
1000 const int ret = pvalue_len ? read_var(a, &v, &v_size)
1001 : read_var(a, &v, NULL);
1003 if(ret == 1 && pvalue) {
1004 *pvalue = (size_t) v;
1008 /* Possible data truncation should be safe. */
1009 *pvalue_len = (size_t) v_size;
1015 static int read_bits_32(struct rar5* rar, const uint8_t* p, uint32_t* value) {
1016 uint32_t bits = ((uint32_t) p[rar->bits.in_addr]) << 24;
1017 bits |= p[rar->bits.in_addr + 1] << 16;
1018 bits |= p[rar->bits.in_addr + 2] << 8;
1019 bits |= p[rar->bits.in_addr + 3];
1020 bits <<= rar->bits.bit_addr;
1021 bits |= p[rar->bits.in_addr + 4] >> (8 - rar->bits.bit_addr);
1026 static int read_bits_16(struct rar5* rar, const uint8_t* p, uint16_t* value) {
1027 int bits = (int) ((uint32_t) p[rar->bits.in_addr]) << 16;
1028 bits |= (int) p[rar->bits.in_addr + 1] << 8;
1029 bits |= (int) p[rar->bits.in_addr + 2];
1030 bits >>= (8 - rar->bits.bit_addr);
1031 *value = bits & 0xffff;
1035 static void skip_bits(struct rar5* rar, int bits) {
1036 const int new_bits = rar->bits.bit_addr + bits;
1037 rar->bits.in_addr += new_bits >> 3;
1038 rar->bits.bit_addr = new_bits & 7;
1042 static int read_consume_bits(struct rar5* rar, const uint8_t* p, int n,
1048 if(n == 0 || n > 16) {
1049 /* This is a programmer error and should never happen
1051 return ARCHIVE_FATAL;
1054 ret = read_bits_16(rar, p, &v);
1055 if(ret != ARCHIVE_OK)
1069 static int read_u32(struct archive_read* a, uint32_t* pvalue) {
1071 if(!read_ahead(a, 4, &p))
1074 *pvalue = archive_le32dec(p);
1075 return ARCHIVE_OK == consume(a, 4) ? 1 : 0;
1078 static int read_u64(struct archive_read* a, uint64_t* pvalue) {
1080 if(!read_ahead(a, 8, &p))
1083 *pvalue = archive_le64dec(p);
1084 return ARCHIVE_OK == consume(a, 8) ? 1 : 0;
1087 static int bid_standard(struct archive_read* a) {
1089 char signature[sizeof(rar5_signature_xor)];
1091 rar5_signature(signature);
1093 if(!read_ahead(a, sizeof(rar5_signature_xor), &p))
1096 if(!memcmp(signature, p, sizeof(rar5_signature_xor)))
1102 static int rar5_bid(struct archive_read* a, int best_bid) {
1108 my_bid = bid_standard(a);
1116 static int rar5_options(struct archive_read *a, const char *key,
1122 /* No options supported in this version. Return the ARCHIVE_WARN code
1123 * to signal the options supervisor that the unpacker didn't handle
1124 * setting this option. */
1126 return ARCHIVE_WARN;
1129 static void init_header(struct archive_read* a) {
1130 a->archive.archive_format = ARCHIVE_FORMAT_RAR_V5;
1131 a->archive.archive_format_name = "RAR5";
1134 static void init_window_mask(struct rar5* rar) {
1135 if (rar->cstate.window_size)
1136 rar->cstate.window_mask = rar->cstate.window_size - 1;
1138 rar->cstate.window_mask = 0;
1142 HFL_EXTRA_DATA = 0x0001,
1144 HFL_SKIP_IF_UNKNOWN = 0x0004,
1145 HFL_SPLIT_BEFORE = 0x0008,
1146 HFL_SPLIT_AFTER = 0x0010,
1148 HFL_INHERITED = 0x0040
1151 static int process_main_locator_extra_block(struct archive_read* a,
1154 uint64_t locator_flags;
1156 enum LOCATOR_FLAGS {
1157 QLIST = 0x01, RECOVERY = 0x02,
1160 if(!read_var(a, &locator_flags, NULL)) {
1164 if(locator_flags & QLIST) {
1165 if(!read_var(a, &rar->qlist_offset, NULL)) {
1169 /* qlist is not used */
1172 if(locator_flags & RECOVERY) {
1173 if(!read_var(a, &rar->rr_offset, NULL)) {
1177 /* rr is not used */
1183 static int parse_file_extra_hash(struct archive_read* a, struct rar5* rar,
1184 ssize_t* extra_data_size)
1186 size_t hash_type = 0;
1193 if(!read_var_sized(a, &hash_type, &value_len))
1196 *extra_data_size -= value_len;
1197 if(ARCHIVE_OK != consume(a, value_len)) {
1201 /* The file uses BLAKE2sp checksum algorithm instead of plain old
1203 if(hash_type == BLAKE2sp) {
1205 const int hash_size = sizeof(rar->file.blake2sp);
1207 if(!read_ahead(a, hash_size, &p))
1210 rar->file.has_blake2 = 1;
1211 memcpy(&rar->file.blake2sp, p, hash_size);
1213 if(ARCHIVE_OK != consume(a, hash_size)) {
1217 *extra_data_size -= hash_size;
1219 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1220 "Unsupported hash type (0x%x)", (int) hash_type);
1221 return ARCHIVE_FATAL;
1227 static uint64_t time_win_to_unix(uint64_t win_time) {
1228 const size_t ns_in_sec = 10000000;
1229 const uint64_t sec_to_unix = 11644473600LL;
1230 return win_time / ns_in_sec - sec_to_unix;
1233 static int parse_htime_item(struct archive_read* a, char unix_time,
1234 uint64_t* where, ssize_t* extra_data_size)
1238 if(!read_u32(a, &time_val))
1241 *extra_data_size -= 4;
1242 *where = (uint64_t) time_val;
1244 uint64_t windows_time;
1245 if(!read_u64(a, &windows_time))
1248 *where = time_win_to_unix(windows_time);
1249 *extra_data_size -= 8;
1255 static int parse_file_extra_version(struct archive_read* a,
1256 struct archive_entry* e, ssize_t* extra_data_size)
1260 size_t value_len = 0;
1261 struct archive_string version_string;
1262 struct archive_string name_utf8_string;
1263 const char* cur_filename;
1265 /* Flags are ignored. */
1266 if(!read_var_sized(a, &flags, &value_len))
1269 *extra_data_size -= value_len;
1270 if(ARCHIVE_OK != consume(a, value_len))
1273 if(!read_var_sized(a, &version, &value_len))
1276 *extra_data_size -= value_len;
1277 if(ARCHIVE_OK != consume(a, value_len))
1280 /* extra_data_size should be zero here. */
1282 cur_filename = archive_entry_pathname_utf8(e);
1283 if(cur_filename == NULL) {
1284 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1285 "Version entry without file name");
1286 return ARCHIVE_FATAL;
1289 archive_string_init(&version_string);
1290 archive_string_init(&name_utf8_string);
1292 /* Prepare a ;123 suffix for the filename, where '123' is the version
1293 * value of this file. */
1294 archive_string_sprintf(&version_string, ";%zu", version);
1296 /* Build the new filename. */
1297 archive_strcat(&name_utf8_string, cur_filename);
1298 archive_strcat(&name_utf8_string, version_string.s);
1300 /* Apply the new filename into this file's context. */
1301 archive_entry_update_pathname_utf8(e, name_utf8_string.s);
1304 archive_string_free(&version_string);
1305 archive_string_free(&name_utf8_string);
1309 static int parse_file_extra_htime(struct archive_read* a,
1310 struct archive_entry* e, struct rar5* rar, ssize_t* extra_data_size)
1324 if(!read_var_sized(a, &flags, &value_len))
1327 *extra_data_size -= value_len;
1328 if(ARCHIVE_OK != consume(a, value_len)) {
1332 unix_time = flags & IS_UNIX;
1334 if(flags & HAS_MTIME) {
1335 parse_htime_item(a, unix_time, &rar->file.e_mtime,
1337 archive_entry_set_mtime(e, rar->file.e_mtime, 0);
1340 if(flags & HAS_CTIME) {
1341 parse_htime_item(a, unix_time, &rar->file.e_ctime,
1343 archive_entry_set_ctime(e, rar->file.e_ctime, 0);
1346 if(flags & HAS_ATIME) {
1347 parse_htime_item(a, unix_time, &rar->file.e_atime,
1349 archive_entry_set_atime(e, rar->file.e_atime, 0);
1352 if(flags & HAS_UNIX_NS) {
1353 if(!read_u32(a, &rar->file.e_unix_ns))
1356 *extra_data_size -= 4;
1362 static int parse_file_extra_redir(struct archive_read* a,
1363 struct archive_entry* e, struct rar5* rar, ssize_t* extra_data_size)
1365 uint64_t value_size = 0;
1366 size_t target_size = 0;
1367 char target_utf8_buf[MAX_NAME_IN_BYTES];
1370 if(!read_var(a, &rar->file.redir_type, &value_size))
1372 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1374 *extra_data_size -= value_size;
1376 if(!read_var(a, &rar->file.redir_flags, &value_size))
1378 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1380 *extra_data_size -= value_size;
1382 if(!read_var_sized(a, &target_size, NULL))
1384 *extra_data_size -= target_size + 1;
1386 if(!read_ahead(a, target_size, &p))
1389 if(target_size > (MAX_NAME_IN_CHARS - 1)) {
1390 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1391 "Link target is too long");
1392 return ARCHIVE_FATAL;
1395 if(target_size == 0) {
1396 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1397 "No link target specified");
1398 return ARCHIVE_FATAL;
1401 memcpy(target_utf8_buf, p, target_size);
1402 target_utf8_buf[target_size] = 0;
1404 if(ARCHIVE_OK != consume(a, (int64_t)target_size))
1407 switch(rar->file.redir_type) {
1408 case REDIR_TYPE_UNIXSYMLINK:
1409 case REDIR_TYPE_WINSYMLINK:
1410 archive_entry_set_filetype(e, AE_IFLNK);
1411 archive_entry_update_symlink_utf8(e, target_utf8_buf);
1412 if (rar->file.redir_flags & REDIR_SYMLINK_IS_DIR) {
1413 archive_entry_set_symlink_type(e,
1414 AE_SYMLINK_TYPE_DIRECTORY);
1416 archive_entry_set_symlink_type(e,
1417 AE_SYMLINK_TYPE_FILE);
1421 case REDIR_TYPE_HARDLINK:
1422 archive_entry_set_filetype(e, AE_IFREG);
1423 archive_entry_update_hardlink_utf8(e, target_utf8_buf);
1427 /* Unknown redir type, skip it. */
1433 static int parse_file_extra_owner(struct archive_read* a,
1434 struct archive_entry* e, ssize_t* extra_data_size)
1437 uint64_t value_size = 0;
1439 size_t name_len = 0;
1440 size_t name_size = 0;
1441 char namebuf[OWNER_MAXNAMELEN];
1444 if(!read_var(a, &flags, &value_size))
1446 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1448 *extra_data_size -= value_size;
1450 if ((flags & OWNER_USER_NAME) != 0) {
1451 if(!read_var_sized(a, &name_size, NULL))
1453 *extra_data_size -= name_size + 1;
1455 if(!read_ahead(a, name_size, &p))
1458 if (name_size >= OWNER_MAXNAMELEN) {
1459 name_len = OWNER_MAXNAMELEN - 1;
1461 name_len = name_size;
1464 memcpy(namebuf, p, name_len);
1465 namebuf[name_len] = 0;
1466 if(ARCHIVE_OK != consume(a, (int64_t)name_size))
1469 archive_entry_set_uname(e, namebuf);
1471 if ((flags & OWNER_GROUP_NAME) != 0) {
1472 if(!read_var_sized(a, &name_size, NULL))
1474 *extra_data_size -= name_size + 1;
1476 if(!read_ahead(a, name_size, &p))
1479 if (name_size >= OWNER_MAXNAMELEN) {
1480 name_len = OWNER_MAXNAMELEN - 1;
1482 name_len = name_size;
1485 memcpy(namebuf, p, name_len);
1486 namebuf[name_len] = 0;
1487 if(ARCHIVE_OK != consume(a, (int64_t)name_size))
1490 archive_entry_set_gname(e, namebuf);
1492 if ((flags & OWNER_USER_UID) != 0) {
1493 if(!read_var(a, &id, &value_size))
1495 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1497 *extra_data_size -= value_size;
1499 archive_entry_set_uid(e, (la_int64_t)id);
1501 if ((flags & OWNER_GROUP_GID) != 0) {
1502 if(!read_var(a, &id, &value_size))
1504 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1506 *extra_data_size -= value_size;
1508 archive_entry_set_gid(e, (la_int64_t)id);
1513 static int process_head_file_extra(struct archive_read* a,
1514 struct archive_entry* e, struct rar5* rar, ssize_t extra_data_size)
1516 size_t extra_field_size;
1517 size_t extra_field_id = 0;
1518 int ret = ARCHIVE_FATAL;
1521 while(extra_data_size > 0) {
1522 if(!read_var_sized(a, &extra_field_size, &var_size))
1525 extra_data_size -= var_size;
1526 if(ARCHIVE_OK != consume(a, var_size)) {
1530 if(!read_var_sized(a, &extra_field_id, &var_size))
1533 extra_data_size -= var_size;
1534 if(ARCHIVE_OK != consume(a, var_size)) {
1538 switch(extra_field_id) {
1540 ret = parse_file_extra_hash(a, rar,
1544 ret = parse_file_extra_htime(a, e, rar,
1548 ret = parse_file_extra_redir(a, e, rar,
1552 ret = parse_file_extra_owner(a, e,
1556 ret = parse_file_extra_version(a, e,
1564 /* Skip unsupported entry. */
1565 return consume(a, extra_data_size);
1569 if(ret != ARCHIVE_OK) {
1570 /* Attribute not implemented. */
1577 static int process_head_file(struct archive_read* a, struct rar5* rar,
1578 struct archive_entry* entry, size_t block_flags)
1580 ssize_t extra_data_size = 0;
1581 size_t data_size = 0;
1582 size_t file_flags = 0;
1583 size_t file_attr = 0;
1584 size_t compression_info = 0;
1586 size_t name_size = 0;
1587 uint64_t unpacked_size, window_size;
1588 uint32_t mtime = 0, crc = 0;
1589 int c_method = 0, c_version = 0;
1590 char name_utf8_buf[MAX_NAME_IN_BYTES];
1594 DIRECTORY = 0x0001, UTIME = 0x0002, CRC32 = 0x0004,
1595 UNKNOWN_UNPACKED_SIZE = 0x0008,
1599 ATTR_READONLY = 0x1, ATTR_HIDDEN = 0x2, ATTR_SYSTEM = 0x4,
1600 ATTR_DIRECTORY = 0x10,
1603 enum COMP_INFO_FLAGS {
1612 archive_entry_clear(entry);
1614 /* Do not reset file context if we're switching archives. */
1615 if(!rar->cstate.switch_multivolume) {
1616 reset_file_context(rar);
1619 if(block_flags & HFL_EXTRA_DATA) {
1620 size_t edata_size = 0;
1621 if(!read_var_sized(a, &edata_size, NULL))
1624 /* Intentional type cast from unsigned to signed. */
1625 extra_data_size = (ssize_t) edata_size;
1628 if(block_flags & HFL_DATA) {
1629 if(!read_var_sized(a, &data_size, NULL))
1632 rar->file.bytes_remaining = data_size;
1634 rar->file.bytes_remaining = 0;
1636 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1637 "no data found in file/service block");
1638 return ARCHIVE_FATAL;
1641 if(!read_var_sized(a, &file_flags, NULL))
1644 if(!read_var(a, &unpacked_size, NULL))
1647 if(file_flags & UNKNOWN_UNPACKED_SIZE) {
1648 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1649 "Files with unknown unpacked size are not supported");
1650 return ARCHIVE_FATAL;
1653 rar->file.dir = (uint8_t) ((file_flags & DIRECTORY) > 0);
1655 if(!read_var_sized(a, &file_attr, NULL))
1658 if(file_flags & UTIME) {
1659 if(!read_u32(a, &mtime))
1663 if(file_flags & CRC32) {
1664 if(!read_u32(a, &crc))
1668 if(!read_var_sized(a, &compression_info, NULL))
1671 c_method = (int) (compression_info >> 7) & 0x7;
1672 c_version = (int) (compression_info & 0x3f);
1674 /* RAR5 seems to limit the dictionary size to 64MB. */
1675 window_size = (rar->file.dir > 0) ?
1677 g_unpack_window_size << ((compression_info >> 10) & 15);
1678 rar->cstate.method = c_method;
1679 rar->cstate.version = c_version + 50;
1680 rar->file.solid = (compression_info & SOLID) > 0;
1682 /* Archives which declare solid files without initializing the window
1683 * buffer first are invalid. */
1685 if(rar->file.solid > 0 && rar->cstate.window_buf == NULL) {
1686 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1687 "Declared solid file, but no window buffer "
1688 "initialized yet.");
1689 return ARCHIVE_FATAL;
1692 /* Check if window_size is a sane value. Also, if the file is not
1693 * declared as a directory, disallow window_size == 0. */
1694 if(window_size > (64 * 1024 * 1024) ||
1695 (rar->file.dir == 0 && window_size == 0))
1697 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1698 "Declared dictionary size is not supported.");
1699 return ARCHIVE_FATAL;
1702 if(rar->file.solid > 0) {
1703 /* Re-check if current window size is the same as previous
1704 * window size (for solid files only). */
1705 if(rar->file.solid_window_size > 0 &&
1706 rar->file.solid_window_size != (ssize_t) window_size)
1708 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1709 "Window size for this solid file doesn't match "
1710 "the window size used in previous solid file. ");
1711 return ARCHIVE_FATAL;
1715 /* If we're currently switching volumes, ignore the new definition of
1717 if(rar->cstate.switch_multivolume == 0) {
1718 /* Values up to 64M should fit into ssize_t on every
1720 rar->cstate.window_size = (ssize_t) window_size;
1723 if(rar->file.solid > 0 && rar->file.solid_window_size == 0) {
1724 /* Solid files have to have the same window_size across
1725 whole archive. Remember the window_size parameter
1726 for first solid file found. */
1727 rar->file.solid_window_size = rar->cstate.window_size;
1730 init_window_mask(rar);
1732 rar->file.service = 0;
1734 if(!read_var_sized(a, &host_os, NULL))
1737 if(host_os == HOST_WINDOWS) {
1738 /* Host OS is Windows */
1742 if(file_attr & ATTR_DIRECTORY) {
1743 if (file_attr & ATTR_READONLY) {
1744 mode = 0555 | AE_IFDIR;
1746 mode = 0755 | AE_IFDIR;
1749 if (file_attr & ATTR_READONLY) {
1750 mode = 0444 | AE_IFREG;
1752 mode = 0644 | AE_IFREG;
1756 archive_entry_set_mode(entry, mode);
1758 if (file_attr & (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM)) {
1759 char *fflags_text, *ptr;
1760 /* allocate for "rdonly,hidden,system," */
1761 fflags_text = malloc(22 * sizeof(char));
1762 if (fflags_text != NULL) {
1764 if (file_attr & ATTR_READONLY) {
1765 strcpy(ptr, "rdonly,");
1768 if (file_attr & ATTR_HIDDEN) {
1769 strcpy(ptr, "hidden,");
1772 if (file_attr & ATTR_SYSTEM) {
1773 strcpy(ptr, "system,");
1776 if (ptr > fflags_text) {
1777 /* Delete trailing comma */
1779 archive_entry_copy_fflags_text(entry,
1785 } else if(host_os == HOST_UNIX) {
1786 /* Host OS is Unix */
1787 archive_entry_set_mode(entry, (__LA_MODE_T) file_attr);
1789 /* Unknown host OS */
1790 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1791 "Unsupported Host OS: 0x%x", (int) host_os);
1793 return ARCHIVE_FATAL;
1796 if(!read_var_sized(a, &name_size, NULL))
1799 if(!read_ahead(a, name_size, &p))
1802 if(name_size > (MAX_NAME_IN_CHARS - 1)) {
1803 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1804 "Filename is too long");
1806 return ARCHIVE_FATAL;
1809 if(name_size == 0) {
1810 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1811 "No filename specified");
1813 return ARCHIVE_FATAL;
1816 memcpy(name_utf8_buf, p, name_size);
1817 name_utf8_buf[name_size] = 0;
1818 if(ARCHIVE_OK != consume(a, name_size)) {
1822 archive_entry_update_pathname_utf8(entry, name_utf8_buf);
1824 if(extra_data_size > 0) {
1825 int ret = process_head_file_extra(a, entry, rar,
1829 * TODO: rewrite or remove useless sanity check
1830 * as extra_data_size is not passed as a pointer
1832 if(extra_data_size < 0) {
1833 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1834 "File extra data size is not zero");
1835 return ARCHIVE_FATAL;
1839 if(ret != ARCHIVE_OK)
1843 if((file_flags & UNKNOWN_UNPACKED_SIZE) == 0) {
1844 rar->file.unpacked_size = (ssize_t) unpacked_size;
1845 if(rar->file.redir_type == REDIR_TYPE_NONE)
1846 archive_entry_set_size(entry, unpacked_size);
1849 if(file_flags & UTIME) {
1850 archive_entry_set_mtime(entry, (time_t) mtime, 0);
1853 if(file_flags & CRC32) {
1854 rar->file.stored_crc32 = crc;
1857 if(!rar->cstate.switch_multivolume) {
1858 /* Do not reinitialize unpacking state if we're switching
1860 rar->cstate.block_parsing_finished = 1;
1861 rar->cstate.all_filters_applied = 1;
1862 rar->cstate.initialized = 0;
1865 if(rar->generic.split_before > 0) {
1866 /* If now we're standing on a header that has a 'split before'
1867 * mark, it means we're standing on a 'continuation' file
1868 * header. Signal the caller that if it wants to move to
1869 * another file, it must call rar5_read_header() function
1872 return ARCHIVE_RETRY;
1878 static int process_head_service(struct archive_read* a, struct rar5* rar,
1879 struct archive_entry* entry, size_t block_flags)
1881 /* Process this SERVICE block the same way as FILE blocks. */
1882 int ret = process_head_file(a, rar, entry, block_flags);
1883 if(ret != ARCHIVE_OK)
1886 rar->file.service = 1;
1888 /* But skip the data part automatically. It's no use for the user
1889 * anyway. It contains only service data, not even needed to
1890 * properly unpack the file. */
1891 ret = rar5_read_data_skip(a);
1892 if(ret != ARCHIVE_OK)
1895 /* After skipping, try parsing another block automatically. */
1896 return ARCHIVE_RETRY;
1899 static int process_head_main(struct archive_read* a, struct rar5* rar,
1900 struct archive_entry* entry, size_t block_flags)
1903 size_t extra_data_size = 0;
1904 size_t extra_field_size = 0;
1905 size_t extra_field_id = 0;
1906 size_t archive_flags = 0;
1909 VOLUME = 0x0001, /* multi-volume archive */
1910 VOLUME_NUMBER = 0x0002, /* volume number, first vol doesn't
1912 SOLID = 0x0004, /* solid archive */
1913 PROTECT = 0x0008, /* contains Recovery info */
1914 LOCK = 0x0010, /* readonly flag, not used */
1918 // Just one attribute here.
1924 if(block_flags & HFL_EXTRA_DATA) {
1925 if(!read_var_sized(a, &extra_data_size, NULL))
1928 extra_data_size = 0;
1931 if(!read_var_sized(a, &archive_flags, NULL)) {
1935 rar->main.volume = (archive_flags & VOLUME) > 0;
1936 rar->main.solid = (archive_flags & SOLID) > 0;
1938 if(archive_flags & VOLUME_NUMBER) {
1940 if(!read_var_sized(a, &v, NULL)) {
1945 archive_set_error(&a->archive,
1946 ARCHIVE_ERRNO_FILE_FORMAT,
1947 "Invalid volume number");
1948 return ARCHIVE_FATAL;
1951 rar->main.vol_no = (unsigned int) v;
1953 rar->main.vol_no = 0;
1956 if(rar->vol.expected_vol_no > 0 &&
1957 rar->main.vol_no != rar->vol.expected_vol_no)
1959 /* Returning EOF instead of FATAL because of strange
1960 * libarchive behavior. When opening multiple files via
1961 * archive_read_open_filenames(), after reading up the whole
1962 * last file, the __archive_read_ahead function wraps up to
1963 * the first archive instead of returning EOF. */
1967 if(extra_data_size == 0) {
1972 if(!read_var_sized(a, &extra_field_size, NULL)) {
1976 if(!read_var_sized(a, &extra_field_id, NULL)) {
1980 if(extra_field_size == 0) {
1981 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1982 "Invalid extra field size");
1983 return ARCHIVE_FATAL;
1986 switch(extra_field_id) {
1988 ret = process_main_locator_extra_block(a, rar);
1989 if(ret != ARCHIVE_OK) {
1990 /* Error while parsing main locator extra
1997 archive_set_error(&a->archive,
1998 ARCHIVE_ERRNO_FILE_FORMAT,
1999 "Unsupported extra type (0x%x)",
2000 (int) extra_field_id);
2001 return ARCHIVE_FATAL;
2007 static int skip_unprocessed_bytes(struct archive_read* a) {
2008 struct rar5* rar = get_context(a);
2011 if(rar->file.bytes_remaining) {
2012 /* Use different skipping method in block merging mode than in
2013 * normal mode. If merge mode is active, rar5_read_data_skip
2014 * can't be used, because it could allow recursive use of
2015 * merge_block() * function, and this function doesn't support
2017 if(rar->merge_mode) {
2018 /* Discard whole merged block. This is valid in solid
2019 * mode as well, because the code will discard blocks
2020 * only if those blocks are safe to discard (i.e.
2021 * they're not FILE blocks). */
2022 ret = consume(a, rar->file.bytes_remaining);
2023 if(ret != ARCHIVE_OK) {
2026 rar->file.bytes_remaining = 0;
2028 /* If we're not in merge mode, use safe skipping code.
2029 * This will ensure we'll handle solid archives
2031 ret = rar5_read_data_skip(a);
2032 if(ret != ARCHIVE_OK) {
2041 static int scan_for_signature(struct archive_read* a);
2043 /* Base block processing function. A 'base block' is a RARv5 header block
2044 * that tells the reader what kind of data is stored inside the block.
2046 * From the birds-eye view a RAR file looks file this:
2048 * <magic><base_block_1><base_block_2>...<base_block_n>
2050 * There are a few types of base blocks. Those types are specified inside
2051 * the 'switch' statement in this function. For example purposes, I'll write
2052 * how a standard RARv5 file could look like here:
2054 * <magic><MAIN><FILE><FILE><FILE><SERVICE><ENDARC>
2056 * The structure above could describe an archive file with 3 files in it,
2057 * one service "QuickOpen" block (that is ignored by this parser), and an
2058 * end of file base block marker.
2060 * If the file is stored in multiple archive files ("multiarchive"), it might
2063 * .part01.rar: <magic><MAIN><FILE><ENDARC>
2064 * .part02.rar: <magic><MAIN><FILE><ENDARC>
2065 * .part03.rar: <magic><MAIN><FILE><ENDARC>
2067 * This example could describe 3 RAR files that contain ONE archived file.
2068 * Or it could describe 3 RAR files that contain 3 different files. Or 3
2069 * RAR files than contain 2 files. It all depends what metadata is stored in
2070 * the headers of <FILE> blocks.
2072 * Each <FILE> block contains info about its size, the name of the file it's
2073 * storing inside, and whether this FILE block is a continuation block of
2074 * previous archive ('split before'), and is this FILE block should be
2075 * continued in another archive ('split after'). By parsing the 'split before'
2076 * and 'split after' flags, we're able to tell if multiple <FILE> base blocks
2077 * are describing one file, or multiple files (with the same filename, for
2080 * One thing to note is that if we're parsing the first <FILE> block, and
2081 * we see 'split after' flag, then we need to jump over to another <FILE>
2082 * block to be able to decompress rest of the data. To do this, we need
2083 * to skip the <ENDARC> block, then switch to another file, then skip the
2084 * <magic> block, <MAIN> block, and then we're standing on the proper
2088 static int process_base_block(struct archive_read* a,
2089 struct archive_entry* entry)
2091 const size_t SMALLEST_RAR5_BLOCK_SIZE = 3;
2093 struct rar5* rar = get_context(a);
2094 uint32_t hdr_crc, computed_crc;
2095 size_t raw_hdr_size = 0, hdr_size_len, hdr_size;
2096 size_t header_id = 0;
2097 size_t header_flags = 0;
2102 HEAD_MARK = 0x00, HEAD_MAIN = 0x01, HEAD_FILE = 0x02,
2103 HEAD_SERVICE = 0x03, HEAD_CRYPT = 0x04, HEAD_ENDARC = 0x05,
2104 HEAD_UNKNOWN = 0xff,
2107 /* Skip any unprocessed data for this file. */
2108 ret = skip_unprocessed_bytes(a);
2109 if(ret != ARCHIVE_OK)
2112 /* Read the expected CRC32 checksum. */
2113 if(!read_u32(a, &hdr_crc)) {
2117 /* Read header size. */
2118 if(!read_var_sized(a, &raw_hdr_size, &hdr_size_len)) {
2122 hdr_size = raw_hdr_size + hdr_size_len;
2124 /* Sanity check, maximum header size for RAR5 is 2MB. */
2125 if(hdr_size > (2 * 1024 * 1024)) {
2126 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2127 "Base block header is too large");
2129 return ARCHIVE_FATAL;
2132 /* Additional sanity checks to weed out invalid files. */
2133 if(raw_hdr_size == 0 || hdr_size_len == 0 ||
2134 hdr_size < SMALLEST_RAR5_BLOCK_SIZE)
2136 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2137 "Too small block encountered (%zu bytes)",
2140 return ARCHIVE_FATAL;
2143 /* Read the whole header data into memory, maximum memory use here is
2145 if(!read_ahead(a, hdr_size, &p)) {
2149 /* Verify the CRC32 of the header data. */
2150 computed_crc = (uint32_t) crc32(0, p, (int) hdr_size);
2151 if(computed_crc != hdr_crc) {
2152 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2153 "Header CRC error");
2155 return ARCHIVE_FATAL;
2158 /* If the checksum is OK, we proceed with parsing. */
2159 if(ARCHIVE_OK != consume(a, hdr_size_len)) {
2163 if(!read_var_sized(a, &header_id, NULL))
2166 if(!read_var_sized(a, &header_flags, NULL))
2169 rar->generic.split_after = (header_flags & HFL_SPLIT_AFTER) > 0;
2170 rar->generic.split_before = (header_flags & HFL_SPLIT_BEFORE) > 0;
2171 rar->generic.size = (int)hdr_size;
2172 rar->generic.last_header_id = (int)header_id;
2173 rar->main.endarc = 0;
2175 /* Those are possible header ids in RARv5. */
2178 ret = process_head_main(a, rar, entry, header_flags);
2180 /* Main header doesn't have any files in it, so it's
2181 * pointless to return to the caller. Retry to next
2182 * header, which should be HEAD_FILE/HEAD_SERVICE. */
2183 if(ret == ARCHIVE_OK)
2184 return ARCHIVE_RETRY;
2188 ret = process_head_service(a, rar, entry, header_flags);
2191 ret = process_head_file(a, rar, entry, header_flags);
2194 archive_set_error(&a->archive,
2195 ARCHIVE_ERRNO_FILE_FORMAT,
2196 "Encryption is not supported");
2197 return ARCHIVE_FATAL;
2199 rar->main.endarc = 1;
2201 /* After encountering an end of file marker, we need
2202 * to take into consideration if this archive is
2203 * continued in another file (i.e. is it part01.rar:
2204 * is there a part02.rar?) */
2205 if(rar->main.volume) {
2206 /* In case there is part02.rar, position the
2207 * read pointer in a proper place, so we can
2208 * resume parsing. */
2209 ret = scan_for_signature(a);
2210 if(ret == ARCHIVE_FATAL) {
2213 if(rar->vol.expected_vol_no ==
2215 archive_set_error(&a->archive,
2216 ARCHIVE_ERRNO_FILE_FORMAT,
2218 return ARCHIVE_FATAL;
2221 rar->vol.expected_vol_no =
2222 rar->main.vol_no + 1;
2231 if((header_flags & HFL_SKIP_IF_UNKNOWN) == 0) {
2232 archive_set_error(&a->archive,
2233 ARCHIVE_ERRNO_FILE_FORMAT,
2234 "Header type error");
2235 return ARCHIVE_FATAL;
2237 /* If the block is marked as 'skip if unknown',
2238 * do as the flag says: skip the block
2239 * instead on failing on it. */
2240 return ARCHIVE_RETRY;
2246 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2247 "Internal unpacker error");
2248 return ARCHIVE_FATAL;
2252 static int skip_base_block(struct archive_read* a) {
2254 struct rar5* rar = get_context(a);
2256 /* Create a new local archive_entry structure that will be operated on
2257 * by header reader; operations on this archive_entry will be discarded.
2259 struct archive_entry* entry = archive_entry_new();
2260 ret = process_base_block(a, entry);
2262 /* Discard operations on this archive_entry structure. */
2263 archive_entry_free(entry);
2264 if(ret == ARCHIVE_FATAL)
2267 if(rar->generic.last_header_id == 2 && rar->generic.split_before > 0)
2270 if(ret == ARCHIVE_OK)
2271 return ARCHIVE_RETRY;
2276 static int rar5_read_header(struct archive_read *a,
2277 struct archive_entry *entry)
2279 struct rar5* rar = get_context(a);
2282 if(rar->header_initialized == 0) {
2284 rar->header_initialized = 1;
2287 if(rar->skipped_magic == 0) {
2288 if(ARCHIVE_OK != consume(a, sizeof(rar5_signature_xor))) {
2292 rar->skipped_magic = 1;
2296 ret = process_base_block(a, entry);
2297 } while(ret == ARCHIVE_RETRY ||
2298 (rar->main.endarc > 0 && ret == ARCHIVE_OK));
2303 static void init_unpack(struct rar5* rar) {
2304 rar->file.calculated_crc32 = 0;
2305 init_window_mask(rar);
2307 free(rar->cstate.window_buf);
2308 free(rar->cstate.filtered_buf);
2310 if(rar->cstate.window_size > 0) {
2311 rar->cstate.window_buf = calloc(1, rar->cstate.window_size);
2312 rar->cstate.filtered_buf = calloc(1, rar->cstate.window_size);
2314 rar->cstate.window_buf = NULL;
2315 rar->cstate.filtered_buf = NULL;
2318 rar->cstate.write_ptr = 0;
2319 rar->cstate.last_write_ptr = 0;
2321 memset(&rar->cstate.bd, 0, sizeof(rar->cstate.bd));
2322 memset(&rar->cstate.ld, 0, sizeof(rar->cstate.ld));
2323 memset(&rar->cstate.dd, 0, sizeof(rar->cstate.dd));
2324 memset(&rar->cstate.ldd, 0, sizeof(rar->cstate.ldd));
2325 memset(&rar->cstate.rd, 0, sizeof(rar->cstate.rd));
2328 static void update_crc(struct rar5* rar, const uint8_t* p, size_t to_read) {
2331 if(rar->skip_mode) {
2332 #if defined CHECK_CRC_ON_SOLID_SKIP
2341 /* Don't update CRC32 if the file doesn't have the
2342 * `stored_crc32` info filled in. */
2343 if(rar->file.stored_crc32 > 0) {
2344 rar->file.calculated_crc32 =
2345 crc32(rar->file.calculated_crc32, p, to_read);
2348 /* Check if the file uses an optional BLAKE2sp checksum
2350 if(rar->file.has_blake2 > 0) {
2351 /* Return value of the `update` function is always 0,
2352 * so we can explicitly ignore it here. */
2353 (void) blake2sp_update(&rar->file.b2state, p, to_read);
2358 static int create_decode_tables(uint8_t* bit_length,
2359 struct decode_table* table, int size)
2361 int code, upper_limit = 0, i, lc[16];
2362 uint32_t decode_pos_clone[rar5_countof(table->decode_pos)];
2363 ssize_t cur_len, quick_data_size;
2365 memset(&lc, 0, sizeof(lc));
2366 memset(table->decode_num, 0, sizeof(table->decode_num));
2368 table->quick_bits = size == HUFF_NC ? 10 : 7;
2370 for(i = 0; i < size; i++) {
2371 lc[bit_length[i] & 15]++;
2375 table->decode_pos[0] = 0;
2376 table->decode_len[0] = 0;
2378 for(i = 1; i < 16; i++) {
2379 upper_limit += lc[i];
2381 table->decode_len[i] = upper_limit << (16 - i);
2382 table->decode_pos[i] = table->decode_pos[i - 1] + lc[i - 1];
2387 memcpy(decode_pos_clone, table->decode_pos, sizeof(decode_pos_clone));
2389 for(i = 0; i < size; i++) {
2390 uint8_t clen = bit_length[i] & 15;
2392 int last_pos = decode_pos_clone[clen];
2393 table->decode_num[last_pos] = i;
2394 decode_pos_clone[clen]++;
2398 quick_data_size = (int64_t)1 << table->quick_bits;
2400 for(code = 0; code < quick_data_size; code++) {
2401 int bit_field = code << (16 - table->quick_bits);
2404 while(cur_len < rar5_countof(table->decode_len) &&
2405 bit_field >= table->decode_len[cur_len]) {
2409 table->quick_len[code] = (uint8_t) cur_len;
2411 dist = bit_field - table->decode_len[cur_len - 1];
2412 dist >>= (16 - cur_len);
2414 pos = table->decode_pos[cur_len & 15] + dist;
2415 if(cur_len < rar5_countof(table->decode_pos) && pos < size) {
2416 table->quick_num[code] = table->decode_num[pos];
2418 table->quick_num[code] = 0;
2425 static int decode_number(struct archive_read* a, struct decode_table* table,
2426 const uint8_t* p, uint16_t* num)
2431 struct rar5* rar = get_context(a);
2433 if(ARCHIVE_OK != read_bits_16(rar, p, &bitfield)) {
2439 if(bitfield < table->decode_len[table->quick_bits]) {
2440 int code = bitfield >> (16 - table->quick_bits);
2441 skip_bits(rar, table->quick_len[code]);
2442 *num = table->quick_num[code];
2448 for(i = table->quick_bits + 1; i < 15; i++) {
2449 if(bitfield < table->decode_len[i]) {
2455 skip_bits(rar, bits);
2457 dist = bitfield - table->decode_len[bits - 1];
2458 dist >>= (16 - bits);
2459 pos = table->decode_pos[bits] + dist;
2461 if(pos >= table->size)
2464 *num = table->decode_num[pos];
2468 /* Reads and parses Huffman tables from the beginning of the block. */
2469 static int parse_tables(struct archive_read* a, struct rar5* rar,
2472 int ret, value, i, w, idx = 0;
2473 uint8_t bit_length[HUFF_BC],
2474 table[HUFF_TABLE_SIZE],
2478 enum { ESCAPE = 15 };
2480 /* The data for table generation is compressed using a simple RLE-like
2481 * algorithm when storing zeroes, so we need to unpack it first. */
2482 for(w = 0, i = 0; w < HUFF_BC;) {
2483 if(i >= rar->cstate.cur_block_size) {
2484 /* Truncated data, can't continue. */
2485 archive_set_error(&a->archive,
2486 ARCHIVE_ERRNO_FILE_FORMAT,
2487 "Truncated data in huffman tables");
2488 return ARCHIVE_FATAL;
2491 value = (p[i] & nibble_mask) >> nibble_shift;
2493 if(nibble_mask == 0x0F)
2496 nibble_mask ^= 0xFF;
2499 /* Values smaller than 15 is data, so we write it directly.
2500 * Value 15 is a flag telling us that we need to unpack more
2502 if(value == ESCAPE) {
2503 value = (p[i] & nibble_mask) >> nibble_shift;
2504 if(nibble_mask == 0x0F)
2506 nibble_mask ^= 0xFF;
2510 /* We sometimes need to write the actual value
2511 * of 15, so this case handles that. */
2512 bit_length[w++] = ESCAPE;
2517 for(k = 0; (k < value + 2) && (w < HUFF_BC);
2519 bit_length[w++] = 0;
2523 bit_length[w++] = value;
2527 rar->bits.in_addr = i;
2528 rar->bits.bit_addr = nibble_shift ^ 4;
2530 ret = create_decode_tables(bit_length, &rar->cstate.bd, HUFF_BC);
2531 if(ret != ARCHIVE_OK) {
2532 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2533 "Decoding huffman tables failed");
2534 return ARCHIVE_FATAL;
2537 for(i = 0; i < HUFF_TABLE_SIZE;) {
2540 if((rar->bits.in_addr + 6) >= rar->cstate.cur_block_size) {
2541 /* Truncated data, can't continue. */
2542 archive_set_error(&a->archive,
2543 ARCHIVE_ERRNO_FILE_FORMAT,
2544 "Truncated data in huffman tables (#2)");
2545 return ARCHIVE_FATAL;
2548 ret = decode_number(a, &rar->cstate.bd, p, &num);
2549 if(ret != ARCHIVE_OK) {
2550 archive_set_error(&a->archive,
2551 ARCHIVE_ERRNO_FILE_FORMAT,
2552 "Decoding huffman tables failed");
2553 return ARCHIVE_FATAL;
2557 /* 0..15: store directly */
2558 table[i] = (uint8_t) num;
2560 } else if(num < 18) {
2561 /* 16..17: repeat previous code */
2564 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2578 while(n-- > 0 && i < HUFF_TABLE_SIZE) {
2579 table[i] = table[i - 1];
2583 archive_set_error(&a->archive,
2584 ARCHIVE_ERRNO_FILE_FORMAT,
2585 "Unexpected error when decoding "
2587 return ARCHIVE_FATAL;
2590 /* other codes: fill with zeroes `n` times */
2593 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2606 while(n-- > 0 && i < HUFF_TABLE_SIZE)
2611 ret = create_decode_tables(&table[idx], &rar->cstate.ld, HUFF_NC);
2612 if(ret != ARCHIVE_OK) {
2613 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2614 "Failed to create literal table");
2615 return ARCHIVE_FATAL;
2620 ret = create_decode_tables(&table[idx], &rar->cstate.dd, HUFF_DC);
2621 if(ret != ARCHIVE_OK) {
2622 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2623 "Failed to create distance table");
2624 return ARCHIVE_FATAL;
2629 ret = create_decode_tables(&table[idx], &rar->cstate.ldd, HUFF_LDC);
2630 if(ret != ARCHIVE_OK) {
2631 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2632 "Failed to create lower bits of distances table");
2633 return ARCHIVE_FATAL;
2638 ret = create_decode_tables(&table[idx], &rar->cstate.rd, HUFF_RC);
2639 if(ret != ARCHIVE_OK) {
2640 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2641 "Failed to create repeating distances table");
2642 return ARCHIVE_FATAL;
2648 /* Parses the block header, verifies its CRC byte, and saves the header
2649 * fields inside the `hdr` pointer. */
2650 static int parse_block_header(struct archive_read* a, const uint8_t* p,
2651 ssize_t* block_size, struct compressed_block_header* hdr)
2653 uint8_t calculated_cksum;
2654 memcpy(hdr, p, sizeof(struct compressed_block_header));
2656 if(bf_byte_count(hdr) > 2) {
2657 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2658 "Unsupported block header size (was %d, max is 2)",
2659 bf_byte_count(hdr));
2660 return ARCHIVE_FATAL;
2663 /* This should probably use bit reader interface in order to be more
2666 switch(bf_byte_count(hdr)) {
2667 /* 1-byte block size */
2669 *block_size = *(const uint8_t*) &p[2];
2672 /* 2-byte block size */
2674 *block_size = archive_le16dec(&p[2]);
2677 /* 3-byte block size */
2679 *block_size = archive_le32dec(&p[2]);
2680 *block_size &= 0x00FFFFFF;
2683 /* Other block sizes are not supported. This case is not
2684 * reached, because we have an 'if' guard before the switch
2685 * that makes sure of it. */
2687 return ARCHIVE_FATAL;
2690 /* Verify the block header checksum. 0x5A is a magic value and is
2691 * always * constant. */
2692 calculated_cksum = 0x5A
2693 ^ (uint8_t) hdr->block_flags_u8
2694 ^ (uint8_t) *block_size
2695 ^ (uint8_t) (*block_size >> 8)
2696 ^ (uint8_t) (*block_size >> 16);
2698 if(calculated_cksum != hdr->block_cksum) {
2699 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2700 "Block checksum error: got 0x%x, expected 0x%x",
2701 hdr->block_cksum, calculated_cksum);
2703 return ARCHIVE_FATAL;
2709 /* Convenience function used during filter processing. */
2710 static int parse_filter_data(struct rar5* rar, const uint8_t* p,
2711 uint32_t* filter_data)
2716 if(ARCHIVE_OK != read_consume_bits(rar, p, 2, &bytes))
2721 for(i = 0; i < bytes; i++) {
2724 if(ARCHIVE_OK != read_bits_16(rar, p, &byte)) {
2728 /* Cast to uint32_t will ensure the shift operation will not
2729 * produce undefined result. */
2730 data += ((uint32_t) byte >> 8) << (i * 8);
2734 *filter_data = data;
2738 /* Function is used during sanity checking. */
2739 static int is_valid_filter_block_start(struct rar5* rar,
2742 const int64_t block_start = (ssize_t) start + rar->cstate.write_ptr;
2743 const int64_t last_bs = rar->cstate.last_block_start;
2744 const ssize_t last_bl = rar->cstate.last_block_length;
2746 if(last_bs == 0 || last_bl == 0) {
2747 /* We didn't have any filters yet, so accept this offset. */
2751 if(block_start >= last_bs + last_bl) {
2752 /* Current offset is bigger than last block's end offset, so
2753 * accept current offset. */
2757 /* Any other case is not a normal situation and we should fail. */
2761 /* The function will create a new filter, read its parameters from the input
2762 * stream and add it to the filter collection. */
2763 static int parse_filter(struct archive_read* ar, const uint8_t* p) {
2764 uint32_t block_start, block_length;
2765 uint16_t filter_type;
2766 struct filter_info* filt = NULL;
2767 struct rar5* rar = get_context(ar);
2769 /* Read the parameters from the input stream. */
2770 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_start))
2773 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_length))
2776 if(ARCHIVE_OK != read_bits_16(rar, p, &filter_type))
2782 /* Perform some sanity checks on this filter parameters. Note that we
2783 * allow only DELTA, E8/E9 and ARM filters here, because rest of
2784 * filters are not used in RARv5. */
2786 if(block_length < 4 ||
2787 block_length > 0x400000 ||
2788 filter_type > FILTER_ARM ||
2789 !is_valid_filter_block_start(rar, block_start))
2791 archive_set_error(&ar->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2792 "Invalid filter encountered");
2793 return ARCHIVE_FATAL;
2796 /* Allocate a new filter. */
2797 filt = add_new_filter(rar);
2799 archive_set_error(&ar->archive, ENOMEM,
2800 "Can't allocate memory for a filter descriptor.");
2801 return ARCHIVE_FATAL;
2804 filt->type = filter_type;
2805 filt->block_start = rar->cstate.write_ptr + block_start;
2806 filt->block_length = block_length;
2808 rar->cstate.last_block_start = filt->block_start;
2809 rar->cstate.last_block_length = filt->block_length;
2811 /* Read some more data in case this is a DELTA filter. Other filter
2812 * types don't require any additional data over what was already
2814 if(filter_type == FILTER_DELTA) {
2817 if(ARCHIVE_OK != read_consume_bits(rar, p, 5, &channels))
2820 filt->channels = channels + 1;
2826 static int decode_code_length(struct rar5* rar, const uint8_t* p,
2829 int lbits, length = 2;
2834 lbits = code / 4 - 1;
2835 length += (4 | (code & 3)) << lbits;
2841 if(ARCHIVE_OK != read_consume_bits(rar, p, lbits, &add))
2850 static int copy_string(struct archive_read* a, int len, int dist) {
2851 struct rar5* rar = get_context(a);
2852 const uint64_t cmask = rar->cstate.window_mask;
2853 const uint64_t write_ptr = rar->cstate.write_ptr +
2854 rar->cstate.solid_offset;
2857 if (rar->cstate.window_buf == NULL)
2858 return ARCHIVE_FATAL;
2860 /* The unpacker spends most of the time in this function. It would be
2861 * a good idea to introduce some optimizations here.
2863 * Just remember that this loop treats buffers that overlap differently
2864 * than buffers that do not overlap. This is why a simple memcpy(3)
2865 * call will not be enough. */
2867 for(i = 0; i < len; i++) {
2868 const ssize_t write_idx = (write_ptr + i) & cmask;
2869 const ssize_t read_idx = (write_ptr + i - dist) & cmask;
2870 rar->cstate.window_buf[write_idx] =
2871 rar->cstate.window_buf[read_idx];
2874 rar->cstate.write_ptr += len;
2878 static int do_uncompress_block(struct archive_read* a, const uint8_t* p) {
2879 struct rar5* rar = get_context(a);
2883 const uint64_t cmask = rar->cstate.window_mask;
2884 const struct compressed_block_header* hdr = &rar->last_block_hdr;
2885 const uint8_t bit_size = 1 + bf_bit_size(hdr);
2888 if(rar->cstate.write_ptr - rar->cstate.last_write_ptr >
2889 (rar->cstate.window_size >> 1)) {
2890 /* Don't allow growing data by more than half of the
2891 * window size at a time. In such case, break the loop;
2892 * next call to this function will continue processing
2893 * from this moment. */
2897 if(rar->bits.in_addr > rar->cstate.cur_block_size - 1 ||
2898 (rar->bits.in_addr == rar->cstate.cur_block_size - 1 &&
2899 rar->bits.bit_addr >= bit_size))
2901 /* If the program counter is here, it means the
2902 * function has finished processing the block. */
2903 rar->cstate.block_parsing_finished = 1;
2907 /* Decode the next literal. */
2908 if(ARCHIVE_OK != decode_number(a, &rar->cstate.ld, p, &num)) {
2912 /* Num holds a decompression literal, or 'command code'.
2914 * - Values lower than 256 are just bytes. Those codes
2915 * can be stored in the output buffer directly.
2917 * - Code 256 defines a new filter, which is later used to
2918 * ransform the data block accordingly to the filter type.
2919 * The data block needs to be fully uncompressed first.
2921 * - Code bigger than 257 and smaller than 262 define
2922 * a repetition pattern that should be copied from
2923 * an already uncompressed chunk of data.
2927 /* Directly store the byte. */
2928 int64_t write_idx = rar->cstate.solid_offset +
2929 rar->cstate.write_ptr++;
2931 rar->cstate.window_buf[write_idx & cmask] =
2934 } else if(num >= 262) {
2936 int len = decode_code_length(rar, p, num - 262),
2941 archive_set_error(&a->archive,
2942 ARCHIVE_ERRNO_PROGRAMMER,
2943 "Failed to decode the code length");
2945 return ARCHIVE_FATAL;
2948 if(ARCHIVE_OK != decode_number(a, &rar->cstate.dd, p,
2951 archive_set_error(&a->archive,
2952 ARCHIVE_ERRNO_PROGRAMMER,
2953 "Failed to decode the distance slot");
2955 return ARCHIVE_FATAL;
2962 dbits = dist_slot / 2 - 1;
2964 /* Cast to uint32_t will make sure the shift
2965 * left operation won't produce undefined
2966 * result. Then, the uint32_t type will
2967 * be implicitly casted to int. */
2968 dist += (uint32_t) (2 |
2969 (dist_slot & 1)) << dbits;
2978 if(ARCHIVE_OK != read_bits_32(
2986 skip_bits(rar, dbits - 4);
2992 if(ARCHIVE_OK != decode_number(a,
2993 &rar->cstate.ldd, p, &low_dist))
2995 archive_set_error(&a->archive,
2996 ARCHIVE_ERRNO_PROGRAMMER,
2997 "Failed to decode the "
3000 return ARCHIVE_FATAL;
3003 if(dist >= INT_MAX - low_dist - 1) {
3004 /* This only happens in
3005 * invalid archives. */
3006 archive_set_error(&a->archive,
3007 ARCHIVE_ERRNO_FILE_FORMAT,
3010 return ARCHIVE_FATAL;
3015 /* dbits is one of [0,1,2,3] */
3018 if(ARCHIVE_OK != read_consume_bits(rar,
3020 /* Return EOF if we can't read
3035 if(dist > 0x40000) {
3041 dist_cache_push(rar, dist);
3042 rar->cstate.last_len = len;
3044 if(ARCHIVE_OK != copy_string(a, len, dist))
3045 return ARCHIVE_FATAL;
3048 } else if(num == 256) {
3049 /* Create a filter. */
3050 ret = parse_filter(a, p);
3051 if(ret != ARCHIVE_OK)
3055 } else if(num == 257) {
3056 if(rar->cstate.last_len != 0) {
3057 if(ARCHIVE_OK != copy_string(a,
3058 rar->cstate.last_len,
3059 rar->cstate.dist_cache[0]))
3061 return ARCHIVE_FATAL;
3068 const int idx = num - 258;
3069 const int dist = dist_cache_touch(rar, idx);
3074 if(ARCHIVE_OK != decode_number(a, &rar->cstate.rd, p,
3076 return ARCHIVE_FATAL;
3079 len = decode_code_length(rar, p, len_slot);
3080 rar->cstate.last_len = len;
3082 if(ARCHIVE_OK != copy_string(a, len, dist))
3083 return ARCHIVE_FATAL;
3092 /* Binary search for the RARv5 signature. */
3093 static int scan_for_signature(struct archive_read* a) {
3095 const int chunk_size = 512;
3097 char signature[sizeof(rar5_signature_xor)];
3099 /* If we're here, it means we're on an 'unknown territory' data.
3100 * There's no indication what kind of data we're reading here.
3101 * It could be some text comment, any kind of binary data,
3102 * digital sign, dragons, etc.
3104 * We want to find a valid RARv5 magic header inside this unknown
3107 /* Is it possible in libarchive to just skip everything until the
3108 * end of the file? If so, it would be a better approach than the
3109 * current implementation of this function. */
3111 rar5_signature(signature);
3114 if(!read_ahead(a, chunk_size, &p))
3117 for(i = 0; i < chunk_size - (int)sizeof(rar5_signature_xor);
3119 if(memcmp(&p[i], signature,
3120 sizeof(rar5_signature_xor)) == 0) {
3121 /* Consume the number of bytes we've used to
3122 * search for the signature, as well as the
3123 * number of bytes used by the signature
3124 * itself. After this we should be standing
3125 * on a valid base block header. */
3127 i + sizeof(rar5_signature_xor));
3132 consume(a, chunk_size);
3135 return ARCHIVE_FATAL;
3138 /* This function will switch the multivolume archive file to another file,
3139 * i.e. from part03 to part 04. */
3140 static int advance_multivolume(struct archive_read* a) {
3142 struct rar5* rar = get_context(a);
3144 /* A small state machine that will skip unnecessary data, needed to
3145 * switch from one multivolume to another. Such skipping is needed if
3146 * we want to be an stream-oriented (instead of file-oriented)
3149 * The state machine starts with `rar->main.endarc` == 0. It also
3150 * assumes that current stream pointer points to some base block
3153 * The `endarc` field is being set when the base block parsing
3154 * function encounters the 'end of archive' marker.
3158 if(rar->main.endarc == 1) {
3161 rar->main.endarc = 0;
3164 lret = skip_base_block(a);
3167 /* Continue looping. */
3174 /* Forward any errors to the
3182 /* Skip current base block. In order to properly skip
3183 * it, we really need to simply parse it and discard
3186 lret = skip_base_block(a);
3187 if(lret == ARCHIVE_FATAL || lret == ARCHIVE_FAILED)
3190 /* The `skip_base_block` function tells us if we
3191 * should continue with skipping, or we should stop
3192 * skipping. We're trying to skip everything up to
3193 * a base FILE block. */
3195 if(lret != ARCHIVE_RETRY) {
3196 /* If there was an error during skipping, or we
3197 * have just skipped a FILE base block... */
3199 if(rar->main.endarc == 0) {
3211 /* Merges the partial block from the first multivolume archive file, and
3212 * partial block from the second multivolume archive file. The result is
3213 * a chunk of memory containing the whole block, and the stream pointer
3214 * is advanced to the next block in the second multivolume archive file. */
3215 static int merge_block(struct archive_read* a, ssize_t block_size,
3218 struct rar5* rar = get_context(a);
3219 ssize_t cur_block_size, partial_offset = 0;
3223 if(rar->merge_mode) {
3224 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3225 "Recursive merge is not allowed");
3227 return ARCHIVE_FATAL;
3230 /* Set a flag that we're in the switching mode. */
3231 rar->cstate.switch_multivolume = 1;
3233 /* Reallocate the memory which will hold the whole block. */
3234 if(rar->vol.push_buf)
3235 free((void*) rar->vol.push_buf);
3237 /* Increasing the allocation block by 8 is due to bit reading functions,
3238 * which are using additional 2 or 4 bytes. Allocating the block size
3239 * by exact value would make bit reader perform reads from invalid
3240 * memory block when reading the last byte from the buffer. */
3241 rar->vol.push_buf = malloc(block_size + 8);
3242 if(!rar->vol.push_buf) {
3243 archive_set_error(&a->archive, ENOMEM,
3244 "Can't allocate memory for a merge block buffer.");
3245 return ARCHIVE_FATAL;
3248 /* Valgrind complains if the extension block for bit reader is not
3249 * initialized, so initialize it. */
3250 memset(&rar->vol.push_buf[block_size], 0, 8);
3252 /* A single block can span across multiple multivolume archive files,
3253 * so we use a loop here. This loop will consume enough multivolume
3254 * archive files until the whole block is read. */
3257 /* Get the size of current block chunk in this multivolume
3258 * archive file and read it. */
3259 cur_block_size = rar5_min(rar->file.bytes_remaining,
3260 block_size - partial_offset);
3262 if(cur_block_size == 0) {
3263 archive_set_error(&a->archive,
3264 ARCHIVE_ERRNO_FILE_FORMAT,
3265 "Encountered block size == 0 during block merge");
3266 return ARCHIVE_FATAL;
3269 if(!read_ahead(a, cur_block_size, &lp))
3272 /* Sanity check; there should never be a situation where this
3273 * function reads more data than the block's size. */
3274 if(partial_offset + cur_block_size > block_size) {
3275 archive_set_error(&a->archive,
3276 ARCHIVE_ERRNO_PROGRAMMER,
3277 "Consumed too much data when merging blocks.");
3278 return ARCHIVE_FATAL;
3281 /* Merge previous block chunk with current block chunk,
3282 * or create first block chunk if this is our first
3284 memcpy(&rar->vol.push_buf[partial_offset], lp, cur_block_size);
3286 /* Advance the stream read pointer by this block chunk size. */
3287 if(ARCHIVE_OK != consume(a, cur_block_size))
3290 /* Update the pointers. `partial_offset` contains information
3291 * about the sum of merged block chunks. */
3292 partial_offset += cur_block_size;
3293 rar->file.bytes_remaining -= cur_block_size;
3295 /* If `partial_offset` is the same as `block_size`, this means
3296 * we've merged all block chunks and we have a valid full
3298 if(partial_offset == block_size) {
3302 /* If we don't have any bytes to read, this means we should
3303 * switch to another multivolume archive file. */
3304 if(rar->file.bytes_remaining == 0) {
3306 ret = advance_multivolume(a);
3308 if(ret != ARCHIVE_OK) {
3314 *p = rar->vol.push_buf;
3316 /* If we're here, we can resume unpacking by processing the block
3317 * pointed to by the `*p` memory pointer. */
3322 static int process_block(struct archive_read* a) {
3324 struct rar5* rar = get_context(a);
3327 /* If we don't have any data to be processed, this most probably means
3328 * we need to switch to the next volume. */
3329 if(rar->main.volume && rar->file.bytes_remaining == 0) {
3330 ret = advance_multivolume(a);
3331 if(ret != ARCHIVE_OK)
3335 if(rar->cstate.block_parsing_finished) {
3338 ssize_t cur_block_size;
3340 /* The header size won't be bigger than 6 bytes. */
3341 if(!read_ahead(a, 6, &p)) {
3342 /* Failed to prefetch data block header. */
3347 * Read block_size by parsing block header. Validate the header
3348 * by calculating CRC byte stored inside the header. Size of
3349 * the header is not constant (block size can be stored either
3350 * in 1 or 2 bytes), that's why block size is left out from the
3351 * `compressed_block_header` structure and returned by
3352 * `parse_block_header` as the second argument. */
3354 ret = parse_block_header(a, p, &block_size,
3355 &rar->last_block_hdr);
3356 if(ret != ARCHIVE_OK) {
3360 /* Skip block header. Next data is huffman tables,
3362 to_skip = sizeof(struct compressed_block_header) +
3363 bf_byte_count(&rar->last_block_hdr) + 1;
3365 if(ARCHIVE_OK != consume(a, to_skip))
3368 rar->file.bytes_remaining -= to_skip;
3370 /* The block size gives information about the whole block size,
3371 * but the block could be stored in split form when using
3372 * multi-volume archives. In this case, the block size will be
3373 * bigger than the actual data stored in this file. Remaining
3374 * part of the data will be in another file. */
3377 rar5_min(rar->file.bytes_remaining, block_size);
3379 if(block_size > rar->file.bytes_remaining) {
3380 /* If current blocks' size is bigger than our data
3381 * size, this means we have a multivolume archive.
3382 * In this case, skip all base headers until the end
3383 * of the file, proceed to next "partXXX.rar" volume,
3384 * find its signature, skip all headers up to the first
3385 * FILE base header, and continue from there.
3387 * Note that `merge_block` will update the `rar`
3388 * context structure quite extensively. */
3390 ret = merge_block(a, block_size, &p);
3391 if(ret != ARCHIVE_OK) {
3395 cur_block_size = block_size;
3397 /* Current stream pointer should be now directly
3398 * *after* the block that spanned through multiple
3399 * archive files. `p` pointer should have the data of
3400 * the *whole* block (merged from partial blocks
3401 * stored in multiple archives files). */
3403 rar->cstate.switch_multivolume = 0;
3405 /* Read the whole block size into memory. This can take
3406 * up to 8 megabytes of memory in theoretical cases.
3407 * Might be worth to optimize this and use a standard
3408 * chunk of 4kb's. */
3409 if(!read_ahead(a, 4 + cur_block_size, &p)) {
3410 /* Failed to prefetch block data. */
3415 rar->cstate.block_buf = p;
3416 rar->cstate.cur_block_size = cur_block_size;
3417 rar->cstate.block_parsing_finished = 0;
3419 rar->bits.in_addr = 0;
3420 rar->bits.bit_addr = 0;
3422 if(bf_is_table_present(&rar->last_block_hdr)) {
3423 /* Load Huffman tables. */
3424 ret = parse_tables(a, rar, p);
3425 if(ret != ARCHIVE_OK) {
3426 /* Error during decompression of Huffman
3432 /* Block parsing not finished, reuse previous memory buffer. */
3433 p = rar->cstate.block_buf;
3436 /* Uncompress the block, or a part of it, depending on how many bytes
3437 * will be generated by uncompressing the block.
3439 * In case too many bytes will be generated, calling this function
3440 * again will resume the uncompression operation. */
3441 ret = do_uncompress_block(a, p);
3442 if(ret != ARCHIVE_OK) {
3446 if(rar->cstate.block_parsing_finished &&
3447 rar->cstate.switch_multivolume == 0 &&
3448 rar->cstate.cur_block_size > 0)
3450 /* If we're processing a normal block, consume the whole
3451 * block. We can do this because we've already read the whole
3452 * block to memory. */
3453 if(ARCHIVE_OK != consume(a, rar->cstate.cur_block_size))
3454 return ARCHIVE_FATAL;
3456 rar->file.bytes_remaining -= rar->cstate.cur_block_size;
3457 } else if(rar->cstate.switch_multivolume) {
3458 /* Don't consume the block if we're doing multivolume
3459 * processing. The volume switching function will consume
3460 * the proper count of bytes instead. */
3461 rar->cstate.switch_multivolume = 0;
3467 /* Pops the `buf`, `size` and `offset` from the "data ready" stack.
3469 * Returns ARCHIVE_OK when those arguments can be used, ARCHIVE_RETRY
3470 * when there is no data on the stack. */
3471 static int use_data(struct rar5* rar, const void** buf, size_t* size,
3476 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
3477 struct data_ready *d = &rar->cstate.dready[i];
3480 if(buf) *buf = d->buf;
3481 if(size) *size = d->size;
3482 if(offset) *offset = d->offset;
3489 return ARCHIVE_RETRY;
3492 /* Pushes the `buf`, `size` and `offset` arguments to the rar->cstate.dready
3493 * FIFO stack. Those values will be popped from this stack by the `use_data`
3495 static int push_data_ready(struct archive_read* a, struct rar5* rar,
3496 const uint8_t* buf, size_t size, int64_t offset)
3500 /* Don't push if we're in skip mode. This is needed because solid
3501 * streams need full processing even if we're skipping data. After
3502 * fully processing the stream, we need to discard the generated bytes,
3503 * because we're interested only in the side effect: building up the
3504 * internal window circular buffer. This window buffer will be used
3505 * later during unpacking of requested data. */
3510 if(offset != rar->file.last_offset + rar->file.last_size) {
3511 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3512 "Sanity check error: output stream is not continuous");
3513 return ARCHIVE_FATAL;
3516 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
3517 struct data_ready* d = &rar->cstate.dready[i];
3524 /* These fields are used only in sanity checking. */
3525 rar->file.last_offset = offset;
3526 rar->file.last_size = size;
3528 /* Calculate the checksum of this new block before
3529 * submitting data to libarchive's engine. */
3530 update_crc(rar, d->buf, d->size);
3536 /* Program counter will reach this code if the `rar->cstate.data_ready`
3537 * stack will be filled up so that no new entries will be allowed. The
3538 * code shouldn't allow such situation to occur. So we treat this case
3539 * as an internal error. */
3541 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3542 "Error: premature end of data_ready stack");
3543 return ARCHIVE_FATAL;
3546 /* This function uncompresses the data that is stored in the <FILE> base
3549 * The FILE base block looks like this:
3551 * <header><huffman tables><block_1><block_2>...<block_n>
3553 * The <header> is a block header, that is parsed in parse_block_header().
3554 * It's a "compressed_block_header" structure, containing metadata needed
3555 * to know when we should stop looking for more <block_n> blocks.
3557 * <huffman tables> contain data needed to set up the huffman tables, needed
3558 * for the actual decompression.
3560 * Each <block_n> consists of series of literals:
3562 * <literal><literal><literal>...<literal>
3564 * Those literals generate the uncompression data. They operate on a circular
3565 * buffer, sometimes writing raw data into it, sometimes referencing
3566 * some previous data inside this buffer, and sometimes declaring a filter
3567 * that will need to be executed on the data stored in the circular buffer.
3568 * It all depends on the literal that is used.
3570 * Sometimes blocks produce output data, sometimes they don't. For example, for
3571 * some huge files that use lots of filters, sometimes a block is filled with
3572 * only filter declaration literals. Such blocks won't produce any data in the
3575 * Sometimes blocks will produce 4 bytes of data, and sometimes 1 megabyte,
3576 * because a literal can reference previously decompressed data. For example,
3577 * there can be a literal that says: 'append a byte 0xFE here', and after
3578 * it another literal can say 'append 1 megabyte of data from circular buffer
3579 * offset 0x12345'. This is how RAR format handles compressing repeated
3582 * The RAR compressor creates those literals and the actual efficiency of
3583 * compression depends on what those literals are. The literals can also
3584 * be seen as a kind of a non-turing-complete virtual machine that simply
3585 * tells the decompressor what it should do.
3588 static int do_uncompress_file(struct archive_read* a) {
3589 struct rar5* rar = get_context(a);
3591 int64_t max_end_pos;
3593 if(!rar->cstate.initialized) {
3594 /* Don't perform full context reinitialization if we're
3595 * processing a solid archive. */
3596 if(!rar->main.solid || !rar->cstate.window_buf) {
3600 rar->cstate.initialized = 1;
3603 if(rar->cstate.all_filters_applied == 1) {
3604 /* We use while(1) here, but standard case allows for just 1
3605 * iteration. The loop will iterate if process_block() didn't
3606 * generate any data at all. This can happen if the block
3607 * contains only filter definitions (this is common in big
3610 ret = process_block(a);
3611 if(ret == ARCHIVE_EOF || ret == ARCHIVE_FATAL)
3614 if(rar->cstate.last_write_ptr ==
3615 rar->cstate.write_ptr) {
3616 /* The block didn't generate any new data,
3617 * so just process a new block. */
3621 /* The block has generated some new data, so break
3627 /* Try to run filters. If filters won't be applied, it means that
3628 * insufficient data was generated. */
3629 ret = apply_filters(a);
3630 if(ret == ARCHIVE_RETRY) {
3632 } else if(ret == ARCHIVE_FATAL) {
3633 return ARCHIVE_FATAL;
3636 /* If apply_filters() will return ARCHIVE_OK, we can continue here. */
3638 if(cdeque_size(&rar->cstate.filters) > 0) {
3639 /* Check if we can write something before hitting first
3641 struct filter_info* flt;
3643 /* Get the block_start offset from the first filter. */
3644 if(CDE_OK != cdeque_front(&rar->cstate.filters,
3645 cdeque_filter_p(&flt)))
3647 archive_set_error(&a->archive,
3648 ARCHIVE_ERRNO_PROGRAMMER,
3649 "Can't read first filter");
3650 return ARCHIVE_FATAL;
3653 max_end_pos = rar5_min(flt->block_start,
3654 rar->cstate.write_ptr);
3656 /* There are no filters defined, or all filters were applied.
3657 * This means we can just store the data without any
3658 * postprocessing. */
3659 max_end_pos = rar->cstate.write_ptr;
3662 if(max_end_pos == rar->cstate.last_write_ptr) {
3663 /* We can't write anything yet. The block uncompression
3664 * function did not generate enough data, and no filter can be
3665 * applied. At the same time we don't have any data that can be
3666 * stored without filter postprocessing. This means we need to
3667 * wait for more data to be generated, so we can apply the
3670 * Signal the caller that we need more data to be able to do
3673 return ARCHIVE_RETRY;
3675 /* We can write the data before hitting the first filter.
3676 * So let's do it. The push_window_data() function will
3677 * effectively return the selected data block to the user
3679 push_window_data(a, rar, rar->cstate.last_write_ptr,
3681 rar->cstate.last_write_ptr = max_end_pos;
3687 static int uncompress_file(struct archive_read* a) {
3691 /* Sometimes the uncompression function will return a
3692 * 'retry' signal. If this will happen, we have to retry
3694 ret = do_uncompress_file(a);
3695 if(ret != ARCHIVE_RETRY)
3701 static int do_unstore_file(struct archive_read* a,
3702 struct rar5* rar, const void** buf, size_t* size, int64_t* offset)
3707 if(rar->file.bytes_remaining == 0 && rar->main.volume > 0 &&
3708 rar->generic.split_after > 0)
3712 rar->cstate.switch_multivolume = 1;
3713 ret = advance_multivolume(a);
3714 rar->cstate.switch_multivolume = 0;
3716 if(ret != ARCHIVE_OK) {
3717 /* Failed to advance to next multivolume archive
3723 to_read = rar5_min(rar->file.bytes_remaining, 64 * 1024);
3728 if(!read_ahead(a, to_read, &p)) {
3729 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3730 "I/O error when unstoring file");
3731 return ARCHIVE_FATAL;
3734 if(ARCHIVE_OK != consume(a, to_read)) {
3739 if(size) *size = to_read;
3740 if(offset) *offset = rar->cstate.last_unstore_ptr;
3742 rar->file.bytes_remaining -= to_read;
3743 rar->cstate.last_unstore_ptr += to_read;
3745 update_crc(rar, p, to_read);
3749 static int do_unpack(struct archive_read* a, struct rar5* rar,
3750 const void** buf, size_t* size, int64_t* offset)
3752 enum COMPRESSION_METHOD {
3753 STORE = 0, FASTEST = 1, FAST = 2, NORMAL = 3, GOOD = 4,
3757 if(rar->file.service > 0) {
3758 return do_unstore_file(a, rar, buf, size, offset);
3760 switch(rar->cstate.method) {
3762 return do_unstore_file(a, rar, buf, size,
3773 return uncompress_file(a);
3775 archive_set_error(&a->archive,
3776 ARCHIVE_ERRNO_FILE_FORMAT,
3777 "Compression method not supported: 0x%x",
3778 rar->cstate.method);
3780 return ARCHIVE_FATAL;
3790 static int verify_checksums(struct archive_read* a) {
3792 struct rar5* rar = get_context(a);
3794 /* Check checksums only when actually unpacking the data. There's no
3795 * need to calculate checksum when we're skipping data in solid archives
3796 * (skipping in solid archives is the same thing as unpacking compressed
3797 * data and discarding the result). */
3799 if(!rar->skip_mode) {
3800 /* Always check checksums if we're not in skip mode */
3803 /* We can override the logic above with a compile-time option
3804 * NO_CRC_ON_SOLID_SKIP. This option is used during debugging,
3805 * and it will check checksums of unpacked data even when
3806 * we're skipping it. */
3808 #if defined CHECK_CRC_ON_SOLID_SKIP
3818 /* During unpacking, on each unpacked block we're calling the
3819 * update_crc() function. Since we are here, the unpacking
3820 * process is already over and we can check if calculated
3821 * checksum (CRC32 or BLAKE2sp) is the same as what is stored
3822 * in the archive. */
3823 if(rar->file.stored_crc32 > 0) {
3824 /* Check CRC32 only when the file contains a CRC32
3825 * value for this file. */
3827 if(rar->file.calculated_crc32 !=
3828 rar->file.stored_crc32) {
3829 /* Checksums do not match; the unpacked file
3833 printf("Checksum error: CRC32 "
3834 "(was: %08x, expected: %08x)\n",
3835 rar->file.calculated_crc32,
3836 rar->file.stored_crc32);
3839 #ifndef DONT_FAIL_ON_CRC_ERROR
3840 archive_set_error(&a->archive,
3841 ARCHIVE_ERRNO_FILE_FORMAT,
3842 "Checksum error: CRC32");
3843 return ARCHIVE_FATAL;
3847 printf("Checksum OK: CRC32 "
3849 rar->file.stored_crc32,
3850 rar->file.calculated_crc32);
3855 if(rar->file.has_blake2 > 0) {
3856 /* BLAKE2sp is an optional checksum algorithm that is
3857 * added to RARv5 archives when using the `-htb` switch
3858 * during creation of archive.
3860 * We now finalize the hash calculation by calling the
3861 * `final` function. This will generate the final hash
3862 * value we can use to compare it with the BLAKE2sp
3863 * checksum that is stored in the archive.
3865 * The return value of this `final` function is not
3866 * very helpful, as it guards only against improper use.
3867 * This is why we're explicitly ignoring it. */
3870 (void) blake2sp_final(&rar->file.b2state, b2_buf, 32);
3872 if(memcmp(&rar->file.blake2sp, b2_buf, 32) != 0) {
3873 #ifndef DONT_FAIL_ON_CRC_ERROR
3874 archive_set_error(&a->archive,
3875 ARCHIVE_ERRNO_FILE_FORMAT,
3876 "Checksum error: BLAKE2");
3878 return ARCHIVE_FATAL;
3884 /* Finalization for this file has been successfully completed. */
3888 static int verify_global_checksums(struct archive_read* a) {
3889 return verify_checksums(a);
3893 * Decryption function for the magic signature pattern. Check the comment near
3894 * the `rar5_signature_xor` symbol to read the rationale behind this.
3896 static void rar5_signature(char *buf) {
3899 for(i = 0; i < sizeof(rar5_signature_xor); i++) {
3900 buf[i] = rar5_signature_xor[i] ^ 0xA1;
3904 static int rar5_read_data(struct archive_read *a, const void **buff,
3905 size_t *size, int64_t *offset) {
3907 struct rar5* rar = get_context(a);
3909 if(rar->file.dir > 0) {
3910 /* Don't process any data if this file entry was declared
3911 * as a directory. This is needed, because entries marked as
3912 * directory doesn't have any dictionary buffer allocated, so
3913 * it's impossible to perform any decompression. */
3914 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3915 "Can't decompress an entry marked as a directory");
3916 return ARCHIVE_FAILED;
3919 if(!rar->skip_mode && (rar->cstate.last_write_ptr > rar->file.unpacked_size)) {
3920 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3921 "Unpacker has written too many bytes");
3922 return ARCHIVE_FATAL;
3925 ret = use_data(rar, buff, size, offset);
3926 if(ret == ARCHIVE_OK) {
3930 if(rar->file.eof == 1) {
3934 ret = do_unpack(a, rar, buff, size, offset);
3935 if(ret != ARCHIVE_OK) {
3939 if(rar->file.bytes_remaining == 0 &&
3940 rar->cstate.last_write_ptr == rar->file.unpacked_size)
3942 /* If all bytes of current file were processed, run
3945 * Finalization will check checksum against proper values. If
3946 * some of the checksums will not match, we'll return an error
3947 * value in the last `archive_read_data` call to signal an error
3951 return verify_global_checksums(a);
3957 static int rar5_read_data_skip(struct archive_read *a) {
3958 struct rar5* rar = get_context(a);
3960 if(rar->main.solid) {
3961 /* In solid archives, instead of skipping the data, we need to
3962 * extract it, and dispose the result. The side effect of this
3963 * operation will be setting up the initial window buffer state
3964 * needed to be able to extract the selected file. */
3968 /* Make sure to process all blocks in the compressed stream. */
3969 while(rar->file.bytes_remaining > 0) {
3970 /* Setting the "skip mode" will allow us to skip
3971 * checksum checks during data skipping. Checking the
3972 * checksum of skipped data isn't really necessary and
3973 * it's only slowing things down.
3975 * This is incremented instead of setting to 1 because
3976 * this data skipping function can be called
3980 /* We're disposing 1 block of data, so we use triple
3981 * NULLs in arguments. */
3982 ret = rar5_read_data(a, NULL, NULL, NULL);
3984 /* Turn off "skip mode". */
3987 if(ret < 0 || ret == ARCHIVE_EOF) {
3988 /* Propagate any potential error conditions
3994 /* In standard archives, we can just jump over the compressed
3995 * stream. Each file in non-solid archives starts from an empty
3998 if(ARCHIVE_OK != consume(a, rar->file.bytes_remaining)) {
3999 return ARCHIVE_FATAL;
4002 rar->file.bytes_remaining = 0;
4008 static int64_t rar5_seek_data(struct archive_read *a, int64_t offset,
4015 /* We're a streaming unpacker, and we don't support seeking. */
4017 return ARCHIVE_FATAL;
4020 static int rar5_cleanup(struct archive_read *a) {
4021 struct rar5* rar = get_context(a);
4023 free(rar->cstate.window_buf);
4024 free(rar->cstate.filtered_buf);
4026 free(rar->vol.push_buf);
4029 cdeque_free(&rar->cstate.filters);
4032 a->format->data = NULL;
4037 static int rar5_capabilities(struct archive_read * a) {
4042 static int rar5_has_encrypted_entries(struct archive_read *_a) {
4045 /* Unsupported for now. */
4046 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
4049 static int rar5_init(struct rar5* rar) {
4050 memset(rar, 0, sizeof(struct rar5));
4052 if(CDE_OK != cdeque_init(&rar->cstate.filters, 8192))
4053 return ARCHIVE_FATAL;
4058 int archive_read_support_format_rar5(struct archive *_a) {
4059 struct archive_read* ar;
4063 if(ARCHIVE_OK != (ret = get_archive_read(_a, &ar)))
4066 rar = malloc(sizeof(*rar));
4068 archive_set_error(&ar->archive, ENOMEM,
4069 "Can't allocate rar5 data");
4070 return ARCHIVE_FATAL;
4073 if(ARCHIVE_OK != rar5_init(rar)) {
4074 archive_set_error(&ar->archive, ENOMEM,
4075 "Can't allocate rar5 filter buffer");
4076 return ARCHIVE_FATAL;
4079 ret = __archive_read_register_format(ar,
4086 rar5_read_data_skip,
4090 rar5_has_encrypted_entries);
4092 if(ret != ARCHIVE_OK) {
4093 (void) rar5_cleanup(ar);