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"
33 #include <zlib.h> /* crc32 */
38 #include "archive_crc32.h"
41 #include "archive_entry.h"
42 #include "archive_entry_locale.h"
43 #include "archive_ppmd7_private.h"
44 #include "archive_entry_private.h"
49 #include "archive_blake2.h"
52 /*#define CHECK_CRC_ON_SOLID_SKIP*/
53 /*#define DONT_FAIL_ON_CRC_ERROR*/
56 #define rar5_min(a, b) (((a) > (b)) ? (b) : (a))
57 #define rar5_max(a, b) (((a) > (b)) ? (a) : (b))
58 #define rar5_countof(X) ((const ssize_t) (sizeof(X) / sizeof(*X)))
61 #define DEBUG_CODE if(1)
63 #define DEBUG_CODE if(0)
66 /* Real RAR5 magic number is:
68 * 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00
71 * It's stored in `rar5_signature` after XOR'ing it with 0xA1, because I don't
72 * want to put this magic sequence in each binary that uses libarchive, so
73 * applications that scan through the file for this marker won't trigger on
76 * The array itself is decrypted in `rar5_init` function. */
78 static unsigned char rar5_signature[] = { 243, 192, 211, 128, 187, 166, 160, 161 };
79 static const ssize_t rar5_signature_size = sizeof(rar5_signature);
80 /* static const size_t g_unpack_buf_chunk_size = 1024; */
81 static const size_t g_unpack_window_size = 0x20000;
84 ssize_t bytes_remaining;
85 ssize_t unpacked_size;
86 int64_t last_offset; /* Used in sanity checks. */
87 int64_t last_size; /* Used in sanity checks. */
89 uint8_t solid : 1; /* Is this a solid stream? */
90 uint8_t service : 1; /* Is this file a service data? */
92 /* Optional time fields. */
98 /* Optional hash fields. */
99 uint32_t stored_crc32;
100 uint32_t calculated_crc32;
101 uint8_t blake2sp[32];
102 blake2sp_state b2state;
107 FILTER_DELTA = 0, /* Generic pattern. */
108 FILTER_E8 = 1, /* Intel x86 code. */
109 FILTER_E8E9 = 2, /* Intel x86 code. */
110 FILTER_ARM = 3, /* ARM code. */
111 FILTER_AUDIO = 4, /* Audio filter, not used in RARv5. */
112 FILTER_RGB = 5, /* Color palette, not used in RARv5. */
113 FILTER_ITANIUM = 6, /* Intel's Itanium, not used in RARv5. */
114 FILTER_PPM = 7, /* Predictive pattern matching, not used in RARv5. */
124 ssize_t block_length;
143 struct decode_table {
145 int32_t decode_len[16];
146 uint32_t decode_pos[16];
148 uint8_t quick_len[1 << 10];
149 uint16_t quick_num[1 << 10];
150 uint16_t decode_num[306];
154 /* Flag used to specify if unpacker needs to reinitialize the uncompression
156 uint8_t initialized : 1;
158 /* Flag used when applying filters. */
159 uint8_t all_filters_applied : 1;
161 /* Flag used to skip file context reinitialization, used when unpacker is
162 * skipping through different multivolume archives. */
163 uint8_t switch_multivolume : 1;
165 /* Flag used to specify if unpacker has processed the whole data block or
166 * just a part of it. */
167 uint8_t block_parsing_finished : 1;
171 int flags; /* Uncompression flags. */
172 int method; /* Uncompression algorithm method. */
173 int version; /* Uncompression algorithm version. */
174 ssize_t window_size; /* Size of window_buf. */
175 uint8_t* window_buf; /* Circular buffer used during
177 uint8_t* filtered_buf; /* Buffer used when applying filters. */
178 const uint8_t* block_buf; /* Buffer used when merging blocks. */
179 size_t window_mask; /* Convinience field; window_size - 1. */
180 int64_t write_ptr; /* This amount of data has been unpacked in
181 the window buffer. */
182 int64_t last_write_ptr; /* This amount of data has been stored in
184 int64_t last_unstore_ptr; /* Counter of bytes extracted during
185 unstoring. This is separate from
186 last_write_ptr because of how SERVICE
187 base blocks are handled during skipping
188 in solid multiarchive archives. */
189 int64_t solid_offset; /* Additional offset inside the window
190 buffer, used in unpacking solid
192 ssize_t cur_block_size; /* Size of current data block. */
193 int last_len; /* Flag used in lzss decompression. */
195 /* Decode tables used during lzss uncompression. */
198 struct decode_table bd; /* huffman bit lengths */
200 struct decode_table ld; /* literals */
202 struct decode_table dd; /* distances */
204 struct decode_table ldd; /* lower bits of distances */
206 struct decode_table rd; /* repeating distances */
207 #define HUFF_TABLE_SIZE (HUFF_NC + HUFF_DC + HUFF_RC + HUFF_LDC)
209 /* Circular deque for storing filters. */
210 struct cdeque filters;
211 int64_t last_block_start; /* Used for sanity checking. */
212 ssize_t last_block_length; /* Used for sanity checking. */
214 /* Distance cache used during lzss uncompression. */
217 /* Data buffer stack. */
218 struct data_ready dready[2];
221 /* Bit reader state. */
223 int8_t bit_addr; /* Current bit pointer inside current byte. */
224 int in_addr; /* Current byte pointer. */
227 /* RARv5 block header structure. */
228 struct compressed_block_header {
231 uint8_t bit_size : 3;
232 uint8_t byte_count : 3;
233 uint8_t is_last_block : 1;
234 uint8_t is_table_present : 1;
236 uint8_t block_flags_u8;
242 /* RARv5 main header structure. */
244 /* Does the archive contain solid streams? */
247 /* If this a multi-file archive? */
255 struct generic_header {
256 uint8_t split_after : 1;
257 uint8_t split_before : 1;
268 /* Main context structure. */
270 int header_initialized;
272 /* Set to 1 if current file is positioned AFTER the magic value
273 * of the archive file. This is used in header reading functions. */
276 /* Set to not zero if we're in skip mode (either by calling rar5_data_skip
277 * function or when skipping over solid streams). Set to 0 when in
278 * extraction mode. This is used during checksum calculation functions. */
281 /* An offset to QuickOpen list. This is not supported by this unpacker,
282 * becuase we're focusing on streaming interface. QuickOpen is designed
283 * to make things quicker for non-stream interfaces, so it's not our
285 uint64_t qlist_offset;
287 /* An offset to additional Recovery data. This is not supported by this
288 * unpacker. Recovery data are additional Reed-Solomon codes that could
289 * be used to calculate bytes that are missing in archive or are
293 /* Various context variables grouped to different structures. */
294 struct generic_header generic;
295 struct main_header main;
296 struct comp_state cstate;
297 struct file_header file;
298 struct bit_reader bits;
299 struct multivolume vol;
301 /* The header of currently processed RARv5 block. Used in main
302 * decompression logic loop. */
303 struct compressed_block_header last_block_hdr;
306 /* Forward function declarations. */
308 static int verify_global_checksums(struct archive_read* a);
309 static int rar5_read_data_skip(struct archive_read *a);
310 static int push_data_ready(struct archive_read* a, struct rar5* rar,
311 const uint8_t* buf, size_t size, int64_t offset);
313 /* CDE_xxx = Circular Double Ended (Queue) return values. */
314 enum CDE_RETURN_VALUES {
315 CDE_OK, CDE_ALLOC, CDE_PARAM, CDE_OUT_OF_BOUNDS,
318 /* Clears the contents of this circular deque. */
319 static void cdeque_clear(struct cdeque* d) {
325 /* Creates a new circular deque object. Capacity must be power of 2: 8, 16, 32,
326 * 64, 256, etc. When the user will add another item above current capacity,
327 * the circular deque will overwrite the oldest entry. */
328 static int cdeque_init(struct cdeque* d, int max_capacity_power_of_2) {
329 if(d == NULL || max_capacity_power_of_2 == 0)
332 d->cap_mask = max_capacity_power_of_2 - 1;
335 if((max_capacity_power_of_2 & d->cap_mask) > 0)
339 d->arr = malloc(sizeof(void*) * max_capacity_power_of_2);
341 return d->arr ? CDE_OK : CDE_ALLOC;
344 /* Return the current size (not capacity) of circular deque `d`. */
345 static size_t cdeque_size(struct cdeque* d) {
349 /* Returns the first element of current circular deque. Note that this function
350 * doesn't perform any bounds checking. If you need bounds checking, use
351 * `cdeque_front()` function instead. */
352 static void cdeque_front_fast(struct cdeque* d, void** value) {
353 *value = (void*) d->arr[d->beg_pos];
356 /* Returns the first element of current circular deque. This function
357 * performs bounds checking. */
358 static int cdeque_front(struct cdeque* d, void** value) {
360 cdeque_front_fast(d, value);
363 return CDE_OUT_OF_BOUNDS;
366 /* Pushes a new element into the end of this circular deque object. If current
367 * size will exceed capacity, the oldest element will be overwritten. */
368 static int cdeque_push_back(struct cdeque* d, void* item) {
372 if(d->size == d->cap_mask + 1)
373 return CDE_OUT_OF_BOUNDS;
375 d->arr[d->end_pos] = (size_t) item;
376 d->end_pos = (d->end_pos + 1) & d->cap_mask;
382 /* Pops a front element of this circular deque object and returns its value.
383 * This function doesn't perform any bounds checking. */
384 static void cdeque_pop_front_fast(struct cdeque* d, void** value) {
385 *value = (void*) d->arr[d->beg_pos];
386 d->beg_pos = (d->beg_pos + 1) & d->cap_mask;
390 /* Pops a front element of this cicrular deque object and returns its value.
391 * This function performs bounds checking. */
392 static int cdeque_pop_front(struct cdeque* d, void** value) {
397 return CDE_OUT_OF_BOUNDS;
399 cdeque_pop_front_fast(d, value);
403 /* Convinience function to cast filter_info** to void **. */
404 static void** cdeque_filter_p(struct filter_info** f) {
405 return (void**) (size_t) f;
408 /* Convinience function to cast filter_info* to void *. */
409 static void* cdeque_filter(struct filter_info* f) {
410 return (void**) (size_t) f;
413 /* Destroys this circular deque object. Dellocates the memory of the collection
414 * buffer, but doesn't deallocate the memory of any pointer passed to this
415 * deque as a value. */
416 static void cdeque_free(struct cdeque* d) {
431 static inline struct rar5* get_context(struct archive_read* a) {
432 return (struct rar5*) a->format->data;
435 // TODO: make sure these functions return a little endian number
437 /* Convinience functions used by filter implementations. */
439 static uint32_t read_filter_data(struct rar5* rar, uint32_t offset) {
440 uint32_t* dptr = (uint32_t*) &rar->cstate.window_buf[offset];
441 // TODO: bswap if big endian
445 static void write_filter_data(struct rar5* rar, uint32_t offset,
448 uint32_t* dptr = (uint32_t*) &rar->cstate.filtered_buf[offset];
449 // TODO: bswap if big endian
453 static void circular_memcpy(uint8_t* dst, uint8_t* window, const int mask,
454 int64_t start, int64_t end)
456 if((start & mask) > (end & mask)) {
457 ssize_t len1 = mask + 1 - (start & mask);
458 ssize_t len2 = end & mask;
460 memcpy(dst, &window[start & mask], len1);
461 memcpy(dst + len1, window, len2);
463 memcpy(dst, &window[start & mask], (size_t) (end - start));
467 /* Allocates a new filter descriptor and adds it to the filter array. */
468 static struct filter_info* add_new_filter(struct rar5* rar) {
469 struct filter_info* f =
470 (struct filter_info*) calloc(1, sizeof(struct filter_info));
476 cdeque_push_back(&rar->cstate.filters, cdeque_filter(f));
480 static int run_delta_filter(struct rar5* rar, struct filter_info* flt) {
482 ssize_t dest_pos, src_pos = 0;
484 for(i = 0; i < flt->channels; i++) {
485 uint8_t prev_byte = 0;
487 dest_pos < flt->block_length;
488 dest_pos += flt->channels)
492 byte = rar->cstate.window_buf[(rar->cstate.solid_offset +
493 flt->block_start + src_pos) & rar->cstate.window_mask];
496 rar->cstate.filtered_buf[dest_pos] = prev_byte;
504 static int run_e8e9_filter(struct rar5* rar, struct filter_info* flt,
507 const uint32_t file_size = 0x1000000;
510 circular_memcpy(rar->cstate.filtered_buf,
511 rar->cstate.window_buf,
512 rar->cstate.window_mask,
513 rar->cstate.solid_offset + flt->block_start,
514 rar->cstate.solid_offset + flt->block_start + flt->block_length);
516 for(i = 0; i < flt->block_length - 4;) {
517 uint8_t b = rar->cstate.window_buf[(rar->cstate.solid_offset +
518 flt->block_start + i++) & rar->cstate.window_mask];
520 /* 0xE8 = x86's call <relative_addr_uint32> (function call)
521 * 0xE9 = x86's jmp <relative_addr_uint32> (unconditional jump) */
522 if(b == 0xE8 || (extended && b == 0xE9)) {
525 uint32_t offset = (i + flt->block_start) % file_size;
527 addr = read_filter_data(rar, (rar->cstate.solid_offset +
528 flt->block_start + i) & rar->cstate.window_mask);
530 if(addr & 0x80000000) {
531 if(((addr + offset) & 0x80000000) == 0) {
532 write_filter_data(rar, i, addr + file_size);
535 if((addr - file_size) & 0x80000000) {
536 uint32_t naddr = addr - offset;
537 write_filter_data(rar, i, naddr);
548 static int run_arm_filter(struct rar5* rar, struct filter_info* flt) {
551 const int mask = rar->cstate.window_mask;
553 circular_memcpy(rar->cstate.filtered_buf,
554 rar->cstate.window_buf,
555 rar->cstate.window_mask,
556 rar->cstate.solid_offset + flt->block_start,
557 rar->cstate.solid_offset + flt->block_start + flt->block_length);
559 for(i = 0; i < flt->block_length - 3; i += 4) {
560 uint8_t* b = &rar->cstate.window_buf[(rar->cstate.solid_offset +
561 flt->block_start + i) & mask];
564 /* 0xEB = ARM's BL (branch + link) instruction. */
565 offset = read_filter_data(rar, (rar->cstate.solid_offset +
566 flt->block_start + i) & mask) & 0x00ffffff;
568 offset -= (uint32_t) ((i + flt->block_start) / 4);
569 offset = (offset & 0x00ffffff) | 0xeb000000;
570 write_filter_data(rar, i, offset);
577 static int run_filter(struct archive_read* a, struct filter_info* flt) {
579 struct rar5* rar = get_context(a);
581 if(rar->cstate.filtered_buf)
582 free(rar->cstate.filtered_buf);
584 rar->cstate.filtered_buf = malloc(flt->block_length);
585 if(!rar->cstate.filtered_buf) {
586 archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for "
588 return ARCHIVE_FATAL;
593 ret = run_delta_filter(rar, flt);
599 ret = run_e8e9_filter(rar, flt, flt->type == FILTER_E8E9);
603 ret = run_arm_filter(rar, flt);
607 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
608 "Unsupported filter type: 0x%02x", flt->type);
609 return ARCHIVE_FATAL;
612 if(ret != ARCHIVE_OK) {
613 /* Filter has failed. */
617 if(ARCHIVE_OK != push_data_ready(a, rar, rar->cstate.filtered_buf,
618 flt->block_length, rar->cstate.last_write_ptr))
620 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
621 "Stack overflow when submitting unpacked data");
623 return ARCHIVE_FATAL;
626 rar->cstate.last_write_ptr += flt->block_length;
630 /* The `push_data` function submits the selected data range to the user.
631 * Next call of `use_data` will use the pointer, size and offset arguments
632 * that are specified here. These arguments are pushed to the FIFO stack here,
633 * and popped from the stack by the `use_data` function. */
634 static void push_data(struct archive_read* a, struct rar5* rar,
635 const uint8_t* buf, int64_t idx_begin, int64_t idx_end)
637 const int wmask = rar->cstate.window_mask;
638 const ssize_t solid_write_ptr = (rar->cstate.solid_offset +
639 rar->cstate.last_write_ptr) & wmask;
641 idx_begin += rar->cstate.solid_offset;
642 idx_end += rar->cstate.solid_offset;
644 /* Check if our unpacked data is wrapped inside the window circular buffer.
645 * If it's not wrapped, it can be copied out by using a single memcpy,
646 * but when it's wrapped, we need to copy the first part with one
647 * memcpy, and the second part with another memcpy. */
649 if((idx_begin & wmask) > (idx_end & wmask)) {
650 /* The data is wrapped (begin offset sis bigger than end offset). */
651 const ssize_t frag1_size = rar->cstate.window_size - (idx_begin & wmask);
652 const ssize_t frag2_size = idx_end & wmask;
654 /* Copy the first part of the buffer first. */
655 push_data_ready(a, rar, buf + solid_write_ptr, frag1_size,
656 rar->cstate.last_write_ptr);
658 /* Copy the second part of the buffer. */
659 push_data_ready(a, rar, buf, frag2_size,
660 rar->cstate.last_write_ptr + frag1_size);
662 rar->cstate.last_write_ptr += frag1_size + frag2_size;
664 /* Data is not wrapped, so we can just use one call to copy the
666 push_data_ready(a, rar,
667 buf + solid_write_ptr,
668 (idx_end - idx_begin) & wmask,
669 rar->cstate.last_write_ptr);
671 rar->cstate.last_write_ptr += idx_end - idx_begin;
675 /* Convinience function that submits the data to the user. It uses the
676 * unpack window buffer as a source location. */
677 static void push_window_data(struct archive_read* a, struct rar5* rar,
678 int64_t idx_begin, int64_t idx_end)
680 push_data(a, rar, rar->cstate.window_buf, idx_begin, idx_end);
683 static int apply_filters(struct archive_read* a) {
684 struct filter_info* flt;
685 struct rar5* rar = get_context(a);
688 rar->cstate.all_filters_applied = 0;
690 /* Get the first filter that can be applied to our data. The data needs to
691 * be fully unpacked before the filter can be run. */
693 cdeque_front(&rar->cstate.filters, cdeque_filter_p(&flt)))
695 /* Check if our unpacked data fully covers this filter's range. */
696 if(rar->cstate.write_ptr > flt->block_start &&
697 rar->cstate.write_ptr >= flt->block_start + flt->block_length)
699 /* Check if we have some data pending to be written right before
700 * the filter's start offset. */
701 if(rar->cstate.last_write_ptr == flt->block_start) {
702 /* Run the filter specified by descriptor `flt`. */
703 ret = run_filter(a, flt);
704 if(ret != ARCHIVE_OK) {
705 /* Filter failure, return error. */
709 /* Filter descriptor won't be needed anymore after it's used,
710 * so remove it from the filter list and free its memory. */
711 (void) cdeque_pop_front(&rar->cstate.filters,
712 cdeque_filter_p(&flt));
716 /* We can't run filters yet, dump the memory right before the
718 push_window_data(a, rar, rar->cstate.last_write_ptr,
722 /* Return 'filter applied or not needed' state to the caller. */
723 return ARCHIVE_RETRY;
727 rar->cstate.all_filters_applied = 1;
731 static void dist_cache_push(struct rar5* rar, int value) {
732 int* q = rar->cstate.dist_cache;
740 static int dist_cache_touch(struct rar5* rar, int index) {
741 int* q = rar->cstate.dist_cache;
742 int i, dist = q[index];
744 for(i = index; i > 0; i--)
751 static void free_filters(struct rar5* rar) {
752 struct cdeque* d = &rar->cstate.filters;
754 /* Free any remaining filters. All filters should be naturally consumed by
755 * the unpacking function, so remaining filters after unpacking normally
756 * mean that unpacking wasn't successfull. But still of course we shouldn't
757 * leak memory in such case. */
759 /* cdeque_size() is a fast operation, so we can use it as a loop
761 while(cdeque_size(d) > 0) {
762 struct filter_info* f = NULL;
764 /* Pop_front will also decrease the collection's size. */
765 if(CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)) && f != NULL)
771 /* Also clear out the variables needed for sanity checking. */
772 rar->cstate.last_block_start = 0;
773 rar->cstate.last_block_length = 0;
776 static void reset_file_context(struct rar5* rar) {
777 memset(&rar->file, 0, sizeof(rar->file));
778 blake2sp_init(&rar->file.b2state, 32);
780 if(rar->main.solid) {
781 rar->cstate.solid_offset += rar->cstate.write_ptr;
783 rar->cstate.solid_offset = 0;
786 rar->cstate.write_ptr = 0;
787 rar->cstate.last_write_ptr = 0;
788 rar->cstate.last_unstore_ptr = 0;
793 static inline int get_archive_read(struct archive* a,
794 struct archive_read** ar)
796 *ar = (struct archive_read*) a;
797 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
798 "archive_read_support_format_rar5");
803 static int read_ahead(struct archive_read* a, size_t how_many,
810 *ptr = __archive_read_ahead(a, how_many, &avail);
819 static int consume(struct archive_read* a, int64_t how_many) {
823 how_many == __archive_read_consume(a, how_many)
831 * Read a RAR5 variable sized numeric value. This value will be stored in
832 * `pvalue`. The `pvalue_len` argument points to a variable that will receive
833 * the byte count that was consumed in order to decode the `pvalue` value, plus
836 * pvalue_len is optional and can be NULL.
838 * NOTE: if `pvalue_len` is NOT NULL, the caller needs to manually consume
839 * the number of bytes that `pvalue_len` value contains. If the `pvalue_len`
840 * is NULL, this consuming operation is done automatically.
842 * Returns 1 if *pvalue was successfully read.
843 * Returns 0 if there was an error. In this case, *pvalue contains an
847 static int read_var(struct archive_read* a, uint64_t* pvalue,
848 uint64_t* pvalue_len)
855 /* We will read maximum of 8 bytes. We don't have to handle the situation
856 * to read the RAR5 variable-sized value stored at the end of the file,
857 * because such situation will never happen. */
858 if(!read_ahead(a, 8, &p))
861 for(shift = 0, i = 0; i < 8; i++, shift += 7) {
864 /* Strip the MSB from the input byte and add the resulting number
865 * to the `result`. */
866 result += (b & 0x7F) << shift;
868 /* MSB set to 1 means we need to continue decoding process. MSB set
869 * to 0 means we're done.
871 * This conditional checks for the second case. */
872 if((b & 0x80) == 0) {
877 /* If the caller has passed the `pvalue_len` pointer, store the
878 * number of consumed bytes in it and do NOT consume those bytes,
879 * since the caller has all the information it needs to perform
880 * the consuming process itself. */
884 /* If the caller did not provide the `pvalue_len` pointer,
885 * it will not have the possibility to advance the file
886 * pointer, because it will not know how many bytes it needs
887 * to consume. This is why we handle such situation here
889 if(ARCHIVE_OK != consume(a, 1 + i)) {
894 /* End of decoding process, return success. */
899 /* The decoded value takes the maximum number of 8 bytes. It's a maximum
900 * number of bytes, so end decoding process here even if the first bit
901 * of last byte is 1. */
909 if(ARCHIVE_OK != consume(a, 9)) {
917 static int read_var_sized(struct archive_read* a, size_t* pvalue,
923 const int ret = pvalue_len
924 ? read_var(a, &v, &v_size)
925 : read_var(a, &v, NULL);
927 if(ret == 1 && pvalue) {
928 *pvalue = (size_t) v;
932 /* Possible data truncation should be safe. */
933 *pvalue_len = (size_t) v_size;
939 static int read_bits_32(struct rar5* rar, const uint8_t* p, uint32_t* value) {
940 uint32_t bits = p[rar->bits.in_addr] << 24;
941 bits |= p[rar->bits.in_addr + 1] << 16;
942 bits |= p[rar->bits.in_addr + 2] << 8;
943 bits |= p[rar->bits.in_addr + 3];
944 bits <<= rar->bits.bit_addr;
945 bits |= p[rar->bits.in_addr + 4] >> (8 - rar->bits.bit_addr);
950 static int read_bits_16(struct rar5* rar, const uint8_t* p, uint16_t* value) {
951 int bits = (int) p[rar->bits.in_addr] << 16;
952 bits |= (int) p[rar->bits.in_addr + 1] << 8;
953 bits |= (int) p[rar->bits.in_addr + 2];
954 bits >>= (8 - rar->bits.bit_addr);
955 *value = bits & 0xffff;
959 static void skip_bits(struct rar5* rar, int bits) {
960 const int new_bits = rar->bits.bit_addr + bits;
961 rar->bits.in_addr += new_bits >> 3;
962 rar->bits.bit_addr = new_bits & 7;
966 static int read_consume_bits(struct rar5* rar, const uint8_t* p, int n,
972 if(n == 0 || n > 16) {
973 /* This is a programmer error and should never happen in runtime. */
974 return ARCHIVE_FATAL;
977 ret = read_bits_16(rar, p, &v);
978 if(ret != ARCHIVE_OK)
992 static int read_u32(struct archive_read* a, uint32_t* pvalue) {
994 if(!read_ahead(a, 4, &p))
997 *pvalue = *(const uint32_t*)p;
999 return ARCHIVE_OK == consume(a, 4) ? 1 : 0;
1002 static int read_u64(struct archive_read* a, uint64_t* pvalue) {
1004 if(!read_ahead(a, 8, &p))
1007 *pvalue = *(const uint64_t*)p;
1009 return ARCHIVE_OK == consume(a, 8) ? 1 : 0;
1012 static int bid_standard(struct archive_read* a) {
1015 if(!read_ahead(a, rar5_signature_size, &p))
1018 if(!memcmp(rar5_signature, p, rar5_signature_size))
1024 static int rar5_bid(struct archive_read* a, int best_bid) {
1030 my_bid = bid_standard(a);
1038 static int rar5_options(struct archive_read *a, const char *key, const char *val) {
1043 /* No options supported in this version. Return the ARCHIVE_WARN code to
1044 * signal the options supervisor that the unpacker didn't handle setting
1047 return ARCHIVE_WARN;
1050 static void init_header(struct archive_read* a) {
1051 a->archive.archive_format = ARCHIVE_FORMAT_RAR_V5;
1052 a->archive.archive_format_name = "RAR5";
1056 HFL_EXTRA_DATA = 0x0001, HFL_DATA = 0x0002, HFL_SKIP_IF_UNKNOWN = 0x0004,
1057 HFL_SPLIT_BEFORE = 0x0008, HFL_SPLIT_AFTER = 0x0010, HFL_CHILD = 0x0020,
1058 HFL_INHERITED = 0x0040
1061 static int process_main_locator_extra_block(struct archive_read* a,
1064 uint64_t locator_flags;
1066 if(!read_var(a, &locator_flags, NULL)) {
1070 enum LOCATOR_FLAGS {
1071 QLIST = 0x01, RECOVERY = 0x02,
1074 if(locator_flags & QLIST) {
1075 if(!read_var(a, &rar->qlist_offset, NULL)) {
1079 /* qlist is not used */
1082 if(locator_flags & RECOVERY) {
1083 if(!read_var(a, &rar->rr_offset, NULL)) {
1087 /* rr is not used */
1093 static int parse_file_extra_hash(struct archive_read* a, struct rar5* rar,
1094 ssize_t* extra_data_size)
1099 if(!read_var_sized(a, &hash_type, &value_len))
1102 *extra_data_size -= value_len;
1103 if(ARCHIVE_OK != consume(a, value_len)) {
1111 /* The file uses BLAKE2sp checksum algorithm instead of plain old
1113 if(hash_type == BLAKE2sp) {
1115 const int hash_size = sizeof(rar->file.blake2sp);
1117 if(!read_ahead(a, hash_size, &p))
1120 rar->file.has_blake2 = 1;
1121 memcpy(&rar->file.blake2sp, p, hash_size);
1123 if(ARCHIVE_OK != consume(a, hash_size)) {
1127 *extra_data_size -= hash_size;
1129 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1130 "Unsupported hash type (0x%02x)", (int) hash_type);
1131 return ARCHIVE_FATAL;
1137 static uint64_t time_win_to_unix(uint64_t win_time) {
1138 const size_t ns_in_sec = 10000000;
1139 const uint64_t sec_to_unix = 11644473600LL;
1140 return win_time / ns_in_sec - sec_to_unix;
1143 static int parse_htime_item(struct archive_read* a, char unix_time,
1144 uint64_t* where, ssize_t* extra_data_size)
1148 if(!read_u32(a, &time_val))
1151 *extra_data_size -= 4;
1152 *where = (uint64_t) time_val;
1154 uint64_t windows_time;
1155 if(!read_u64(a, &windows_time))
1158 *where = time_win_to_unix(windows_time);
1159 *extra_data_size -= 8;
1165 static int parse_file_extra_htime(struct archive_read* a,
1166 struct archive_entry* e, struct rar5* rar,
1167 ssize_t* extra_data_size)
1181 if(!read_var_sized(a, &flags, &value_len))
1184 *extra_data_size -= value_len;
1185 if(ARCHIVE_OK != consume(a, value_len)) {
1189 unix_time = flags & IS_UNIX;
1191 if(flags & HAS_MTIME) {
1192 parse_htime_item(a, unix_time, &rar->file.e_mtime, extra_data_size);
1193 archive_entry_set_mtime(e, rar->file.e_mtime, 0);
1196 if(flags & HAS_CTIME) {
1197 parse_htime_item(a, unix_time, &rar->file.e_ctime, extra_data_size);
1198 archive_entry_set_ctime(e, rar->file.e_ctime, 0);
1201 if(flags & HAS_ATIME) {
1202 parse_htime_item(a, unix_time, &rar->file.e_atime, extra_data_size);
1203 archive_entry_set_atime(e, rar->file.e_atime, 0);
1206 if(flags & HAS_UNIX_NS) {
1207 if(!read_u32(a, &rar->file.e_unix_ns))
1210 *extra_data_size -= 4;
1216 static int process_head_file_extra(struct archive_read* a,
1217 struct archive_entry* e, struct rar5* rar,
1218 ssize_t extra_data_size)
1220 size_t extra_field_size;
1221 size_t extra_field_id;
1222 int ret = ARCHIVE_FATAL;
1226 CRYPT = 0x01, HASH = 0x02, HTIME = 0x03, VERSION_ = 0x04,
1227 REDIR = 0x05, UOWNER = 0x06, SUBDATA = 0x07
1230 while(extra_data_size > 0) {
1231 if(!read_var_sized(a, &extra_field_size, &var_size))
1234 extra_data_size -= var_size;
1235 if(ARCHIVE_OK != consume(a, var_size)) {
1239 if(!read_var_sized(a, &extra_field_id, &var_size))
1242 extra_data_size -= var_size;
1243 if(ARCHIVE_OK != consume(a, var_size)) {
1247 switch(extra_field_id) {
1249 ret = parse_file_extra_hash(a, rar, &extra_data_size);
1252 ret = parse_file_extra_htime(a, e, rar, &extra_data_size);
1265 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1266 "Unknown extra field in file/service block: 0x%02x",
1267 (int) extra_field_id);
1268 return ARCHIVE_FATAL;
1272 if(ret != ARCHIVE_OK) {
1273 /* Attribute not implemented. */
1280 static int process_head_file(struct archive_read* a, struct rar5* rar,
1281 struct archive_entry* entry, size_t block_flags)
1283 ssize_t extra_data_size = 0;
1284 size_t data_size, file_flags, file_attr, compression_info, host_os,
1286 uint64_t unpacked_size;
1287 uint32_t mtime = 0, crc;
1288 int c_method = 0, c_version = 0, is_dir;
1289 char name_utf8_buf[2048 * 4];
1292 memset(entry, 0, sizeof(struct archive_entry));
1294 /* Do not reset file context if we're switching archives. */
1295 if(!rar->cstate.switch_multivolume) {
1296 reset_file_context(rar);
1299 if(block_flags & HFL_EXTRA_DATA) {
1301 if(!read_var_sized(a, &edata_size, NULL))
1304 /* Intentional type cast from unsigned to signed. */
1305 extra_data_size = (ssize_t) edata_size;
1308 if(block_flags & HFL_DATA) {
1309 if(!read_var_sized(a, &data_size, NULL))
1312 rar->file.bytes_remaining = data_size;
1314 rar->file.bytes_remaining = 0;
1316 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1317 "no data found in file/service block");
1318 return ARCHIVE_FATAL;
1322 DIRECTORY = 0x0001, UTIME = 0x0002, CRC32 = 0x0004,
1323 UNKNOWN_UNPACKED_SIZE = 0x0008,
1326 enum COMP_INFO_FLAGS {
1330 if(!read_var_sized(a, &file_flags, NULL))
1333 if(!read_var(a, &unpacked_size, NULL))
1336 if(file_flags & UNKNOWN_UNPACKED_SIZE) {
1337 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1338 "Files with unknown unpacked size are not supported");
1339 return ARCHIVE_FATAL;
1342 is_dir = (int) (file_flags & DIRECTORY);
1344 if(!read_var_sized(a, &file_attr, NULL))
1347 if(file_flags & UTIME) {
1348 if(!read_u32(a, &mtime))
1352 if(file_flags & CRC32) {
1353 if(!read_u32(a, &crc))
1357 if(!read_var_sized(a, &compression_info, NULL))
1360 c_method = (int) (compression_info >> 7) & 0x7;
1361 c_version = (int) (compression_info & 0x3f);
1363 rar->cstate.window_size = is_dir ?
1365 g_unpack_window_size << ((compression_info >> 10) & 15);
1366 rar->cstate.method = c_method;
1367 rar->cstate.version = c_version + 50;
1369 rar->file.solid = (compression_info & SOLID) > 0;
1370 rar->file.service = 0;
1372 if(!read_var_sized(a, &host_os, NULL))
1380 if(host_os == HOST_WINDOWS) {
1381 /* Host OS is Windows */
1383 unsigned short mode = 0660;
1390 archive_entry_set_mode(entry, mode);
1391 } else if(host_os == HOST_UNIX) {
1392 /* Host OS is Unix */
1393 archive_entry_set_mode(entry, (unsigned short) file_attr);
1395 /* Unknown host OS */
1396 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1397 "Unsupported Host OS: 0x%02x", (int) host_os);
1399 return ARCHIVE_FATAL;
1402 if(!read_var_sized(a, &name_size, NULL))
1405 if(!read_ahead(a, name_size, &p))
1408 if(name_size > 2047) {
1409 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1410 "Filename is too long");
1412 return ARCHIVE_FATAL;
1415 if(name_size == 0) {
1416 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1417 "No filename specified");
1419 return ARCHIVE_FATAL;
1422 memcpy(name_utf8_buf, p, name_size);
1423 name_utf8_buf[name_size] = 0;
1424 if(ARCHIVE_OK != consume(a, name_size)) {
1428 if(extra_data_size > 0) {
1429 int ret = process_head_file_extra(a, entry, rar, extra_data_size);
1432 if(extra_data_size < 0) {
1433 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1434 "File extra data size is not zero");
1435 return ARCHIVE_FATAL;
1438 if(ret != ARCHIVE_OK)
1442 if((file_flags & UNKNOWN_UNPACKED_SIZE) == 0) {
1443 rar->file.unpacked_size = (ssize_t) unpacked_size;
1444 archive_entry_set_size(entry, unpacked_size);
1447 if(file_flags & UTIME) {
1448 archive_entry_set_mtime(entry, (time_t) mtime, 0);
1451 if(file_flags & CRC32) {
1452 rar->file.stored_crc32 = crc;
1455 archive_entry_update_pathname_utf8(entry, name_utf8_buf);
1457 if(!rar->cstate.switch_multivolume) {
1458 /* Do not reinitialize unpacking state if we're switching archives. */
1459 rar->cstate.block_parsing_finished = 1;
1460 rar->cstate.all_filters_applied = 1;
1461 rar->cstate.initialized = 0;
1464 if(rar->generic.split_before > 0) {
1465 /* If now we're standing on a header that has a 'split before' mark,
1466 * it means we're standing on a 'continuation' file header. Signal
1467 * the caller that if it wants to move to another file, it must call
1468 * rar5_read_header() function again. */
1470 return ARCHIVE_RETRY;
1476 static int process_head_service(struct archive_read* a, struct rar5* rar,
1477 struct archive_entry* entry, size_t block_flags)
1479 /* Process this SERVICE block the same way as FILE blocks. */
1480 int ret = process_head_file(a, rar, entry, block_flags);
1481 if(ret != ARCHIVE_OK)
1484 rar->file.service = 1;
1486 /* But skip the data part automatically. It's no use for the user anyway.
1487 * It contains only service data, not even needed to properly unpack the
1489 ret = rar5_read_data_skip(a);
1490 if(ret != ARCHIVE_OK)
1493 /* After skipping, try parsing another block automatically. */
1494 return ARCHIVE_RETRY;
1497 static int process_head_main(struct archive_read* a, struct rar5* rar,
1498 struct archive_entry* entry, size_t block_flags)
1503 size_t extra_data_size,
1508 if(block_flags & HFL_EXTRA_DATA) {
1509 if(!read_var_sized(a, &extra_data_size, NULL))
1512 extra_data_size = 0;
1515 if(!read_var_sized(a, &archive_flags, NULL)) {
1520 VOLUME = 0x0001, /* multi-volume archive */
1521 VOLUME_NUMBER = 0x0002, /* volume number, first vol doesnt have it */
1522 SOLID = 0x0004, /* solid archive */
1523 PROTECT = 0x0008, /* contains Recovery info */
1524 LOCK = 0x0010, /* readonly flag, not used */
1527 rar->main.volume = (archive_flags & VOLUME) > 0;
1528 rar->main.solid = (archive_flags & SOLID) > 0;
1530 if(archive_flags & VOLUME_NUMBER) {
1532 if(!read_var_sized(a, &v, NULL)) {
1536 rar->main.vol_no = (int) v;
1538 rar->main.vol_no = 0;
1541 if(rar->vol.expected_vol_no > 0 &&
1542 rar->main.vol_no != rar->vol.expected_vol_no)
1544 /* Returning EOF instead of FATAL because of strange libarchive
1545 * behavior. When opening multiple files via
1546 * archive_read_open_filenames(), after reading up the whole last file,
1547 * the __archive_read_ahead function wraps up to the first archive
1548 * instead of returning EOF. */
1552 if(extra_data_size == 0) {
1557 if(!read_var_sized(a, &extra_field_size, NULL)) {
1561 if(!read_var_sized(a, &extra_field_id, NULL)) {
1565 if(extra_field_size == 0) {
1566 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1567 "Invalid extra field size");
1568 return ARCHIVE_FATAL;
1572 // Just one attribute here.
1576 switch(extra_field_id) {
1578 ret = process_main_locator_extra_block(a, rar);
1579 if(ret != ARCHIVE_OK) {
1580 /* Error while parsing main locator extra block. */
1586 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1587 "Unsupported extra type (0x%02x)", (int) extra_field_id);
1588 return ARCHIVE_FATAL;
1594 static int scan_for_signature(struct archive_read* a);
1596 /* Base block processing function. A 'base block' is a RARv5 header block
1597 * that tells the reader what kind of data is stored inside the block.
1599 * From the birds-eye view a RAR file looks file this:
1601 * <magic><base_block_1><base_block_2>...<base_block_n>
1603 * There are a few types of base blocks. Those types are specified inside
1604 * the 'switch' statement in this function. For example purposes, I'll write
1605 * how a standard RARv5 file could look like here:
1607 * <magic><MAIN><FILE><FILE><FILE><SERVICE><ENDARC>
1609 * The structure above could describe an archive file with 3 files in it,
1610 * one service "QuickOpen" block (that is ignored by this parser), and an
1611 * end of file base block marker.
1613 * If the file is stored in multiple archive files ("multiarchive"), it might
1616 * .part01.rar: <magic><MAIN><FILE><ENDARC>
1617 * .part02.rar: <magic><MAIN><FILE><ENDARC>
1618 * .part03.rar: <magic><MAIN><FILE><ENDARC>
1620 * This example could describe 3 RAR files that contain ONE archived file.
1621 * Or it could describe 3 RAR files that contain 3 different files. Or 3
1622 * RAR files than contain 2 files. It all depends what metadata is stored in
1623 * the headers of <FILE> blocks.
1625 * Each <FILE> block contains info about its size, the name of the file it's
1626 * storing inside, and whether this FILE block is a continuation block of
1627 * previous archive ('split before'), and is this FILE block should be
1628 * continued in another archive ('split after'). By parsing the 'split before'
1629 * and 'split after' flags, we're able to tell if multiple <FILE> base blocks
1630 * are describing one file, or multiple files (with the same filename, for
1633 * One thing to note is that if we're parsing the first <FILE> block, and
1634 * we see 'split after' flag, then we need to jump over to another <FILE>
1635 * block to be able to decompress rest of the data. To do this, we need
1636 * to skip the <ENDARC> block, then switch to another file, then skip the
1637 * <magic> block, <MAIN> block, and then we're standing on the proper
1641 static int process_base_block(struct archive_read* a,
1642 struct archive_entry* entry)
1644 struct rar5* rar = get_context(a);
1645 uint32_t hdr_crc, computed_crc;
1646 size_t raw_hdr_size, hdr_size_len, hdr_size;
1647 size_t header_id, header_flags;
1651 /* Skip any unprocessed data for this file. */
1652 if(rar->file.bytes_remaining) {
1653 ret = rar5_read_data_skip(a);
1654 if(ret != ARCHIVE_OK) {
1659 /* Read the expected CRC32 checksum. */
1660 if(!read_u32(a, &hdr_crc)) {
1664 /* Read header size. */
1665 if(!read_var_sized(a, &raw_hdr_size, &hdr_size_len)) {
1669 /* Sanity check, maximum header size for RAR5 is 2MB. */
1670 if(raw_hdr_size > (2 * 1024 * 1024)) {
1671 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1672 "Base block header is too large");
1674 return ARCHIVE_FATAL;
1677 hdr_size = raw_hdr_size + hdr_size_len;
1679 /* Read the whole header data into memory, maximum memory use here is
1681 if(!read_ahead(a, hdr_size, &p)) {
1685 /* Verify the CRC32 of the header data. */
1686 computed_crc = (uint32_t) crc32(0, p, (int) hdr_size);
1687 if(computed_crc != hdr_crc) {
1688 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1689 "Header CRC error");
1691 return ARCHIVE_FATAL;
1694 /* If the checksum is OK, we proceed with parsing. */
1695 if(ARCHIVE_OK != consume(a, hdr_size_len)) {
1699 if(!read_var_sized(a, &header_id, NULL))
1702 if(!read_var_sized(a, &header_flags, NULL))
1705 rar->generic.split_after = (header_flags & HFL_SPLIT_AFTER) > 0;
1706 rar->generic.split_before = (header_flags & HFL_SPLIT_BEFORE) > 0;
1707 rar->generic.size = hdr_size;
1708 rar->generic.last_header_id = header_id;
1709 rar->main.endarc = 0;
1711 /* Those are possible header ids in RARv5. */
1713 HEAD_MARK = 0x00, HEAD_MAIN = 0x01, HEAD_FILE = 0x02,
1714 HEAD_SERVICE = 0x03, HEAD_CRYPT = 0x04, HEAD_ENDARC = 0x05,
1715 HEAD_UNKNOWN = 0xff,
1720 ret = process_head_main(a, rar, entry, header_flags);
1722 /* Main header doesn't have any files in it, so it's pointless
1723 * to return to the caller. Retry to next header, which should be
1724 * HEAD_FILE/HEAD_SERVICE. */
1725 if(ret == ARCHIVE_OK)
1726 return ARCHIVE_RETRY;
1730 ret = process_head_service(a, rar, entry, header_flags);
1733 ret = process_head_file(a, rar, entry, header_flags);
1736 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1737 "Encryption is not supported");
1738 return ARCHIVE_FATAL;
1740 rar->main.endarc = 1;
1742 /* After encountering an end of file marker, we need to take
1743 * into consideration if this archive is continued in another
1744 * file (i.e. is it part01.rar: is there a part02.rar?) */
1745 if(rar->main.volume) {
1746 /* In case there is part02.rar, position the read pointer
1747 * in a proper place, so we can resume parsing. */
1749 ret = scan_for_signature(a);
1750 if(ret == ARCHIVE_FATAL) {
1753 rar->vol.expected_vol_no = rar->main.vol_no + 1;
1762 if((header_flags & HFL_SKIP_IF_UNKNOWN) == 0) {
1763 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1764 "Header type error");
1765 return ARCHIVE_FATAL;
1767 /* If the block is marked as 'skip if unknown', do as the flag
1768 * says: skip the block instead on failing on it. */
1769 return ARCHIVE_RETRY;
1775 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1776 "Internal unpacker error");
1777 return ARCHIVE_FATAL;
1781 static int skip_base_block(struct archive_read* a) {
1783 struct rar5* rar = get_context(a);
1785 struct archive_entry entry;
1786 ret = process_base_block(a, &entry);
1788 if(rar->generic.last_header_id == 2 && rar->generic.split_before > 0)
1791 if(ret == ARCHIVE_OK)
1792 return ARCHIVE_RETRY;
1797 static int rar5_read_header(struct archive_read *a,
1798 struct archive_entry *entry)
1800 struct rar5* rar = get_context(a);
1803 if(rar->header_initialized == 0) {
1805 rar->header_initialized = 1;
1808 if(rar->skipped_magic == 0) {
1809 if(ARCHIVE_OK != consume(a, rar5_signature_size)) {
1813 rar->skipped_magic = 1;
1817 ret = process_base_block(a, entry);
1818 } while(ret == ARCHIVE_RETRY ||
1819 (rar->main.endarc > 0 && ret == ARCHIVE_OK));
1824 static void init_unpack(struct rar5* rar) {
1825 rar->file.calculated_crc32 = 0;
1826 rar->cstate.window_mask = rar->cstate.window_size - 1;
1828 if(rar->cstate.window_buf)
1829 free(rar->cstate.window_buf);
1831 if(rar->cstate.filtered_buf)
1832 free(rar->cstate.filtered_buf);
1834 rar->cstate.window_buf = calloc(1, rar->cstate.window_size);
1835 rar->cstate.filtered_buf = calloc(1, rar->cstate.window_size);
1837 rar->cstate.write_ptr = 0;
1838 rar->cstate.last_write_ptr = 0;
1840 memset(&rar->cstate.bd, 0, sizeof(rar->cstate.bd));
1841 memset(&rar->cstate.ld, 0, sizeof(rar->cstate.ld));
1842 memset(&rar->cstate.dd, 0, sizeof(rar->cstate.dd));
1843 memset(&rar->cstate.ldd, 0, sizeof(rar->cstate.ldd));
1844 memset(&rar->cstate.rd, 0, sizeof(rar->cstate.rd));
1847 static void update_crc(struct rar5* rar, const uint8_t* p, size_t to_read) {
1850 if(rar->skip_mode) {
1851 #if defined CHECK_CRC_ON_SOLID_SKIP
1860 /* Don't update CRC32 if the file doesn't have the `stored_crc32` info
1862 if(rar->file.stored_crc32 > 0) {
1863 rar->file.calculated_crc32 =
1864 crc32(rar->file.calculated_crc32, p, to_read);
1867 /* Check if the file uses an optional BLAKE2sp checksum algorithm. */
1868 if(rar->file.has_blake2 > 0) {
1869 /* Return value of the `update` function is always 0, so we can
1870 * explicitly ignore it here. */
1871 (void) blake2sp_update(&rar->file.b2state, p, to_read);
1876 static int create_decode_tables(uint8_t* bit_length,
1877 struct decode_table* table,
1880 int code, upper_limit = 0, i, lc[16];
1881 uint32_t decode_pos_clone[rar5_countof(table->decode_pos)];
1882 ssize_t cur_len, quick_data_size;
1884 memset(&lc, 0, sizeof(lc));
1885 memset(table->decode_num, 0, sizeof(table->decode_num));
1887 table->quick_bits = size == HUFF_NC ? 10 : 7;
1889 for(i = 0; i < size; i++) {
1890 lc[bit_length[i] & 15]++;
1894 table->decode_pos[0] = 0;
1895 table->decode_len[0] = 0;
1897 for(i = 1; i < 16; i++) {
1898 upper_limit += lc[i];
1900 table->decode_len[i] = upper_limit << (16 - i);
1901 table->decode_pos[i] = table->decode_pos[i - 1] + lc[i - 1];
1906 memcpy(decode_pos_clone, table->decode_pos, sizeof(decode_pos_clone));
1908 for(i = 0; i < size; i++) {
1909 uint8_t clen = bit_length[i] & 15;
1911 int last_pos = decode_pos_clone[clen];
1912 table->decode_num[last_pos] = i;
1913 decode_pos_clone[clen]++;
1917 quick_data_size = 1 << table->quick_bits;
1919 for(code = 0; code < quick_data_size; code++) {
1920 int bit_field = code << (16 - table->quick_bits);
1923 while(cur_len < rar5_countof(table->decode_len) &&
1924 bit_field >= table->decode_len[cur_len]) {
1928 table->quick_len[code] = (uint8_t) cur_len;
1930 dist = bit_field - table->decode_len[cur_len - 1];
1931 dist >>= (16 - cur_len);
1933 pos = table->decode_pos[cur_len] + dist;
1934 if(cur_len < rar5_countof(table->decode_pos) && pos < size) {
1935 table->quick_num[code] = table->decode_num[pos];
1937 table->quick_num[code] = 0;
1944 static int decode_number(struct archive_read* a, struct decode_table* table,
1945 const uint8_t* p, uint16_t* num)
1950 struct rar5* rar = get_context(a);
1952 if(ARCHIVE_OK != read_bits_16(rar, p, &bitfield)) {
1958 if(bitfield < table->decode_len[table->quick_bits]) {
1959 int code = bitfield >> (16 - table->quick_bits);
1960 skip_bits(rar, table->quick_len[code]);
1961 *num = table->quick_num[code];
1967 for(i = table->quick_bits + 1; i < 15; i++) {
1968 if(bitfield < table->decode_len[i]) {
1974 skip_bits(rar, bits);
1976 dist = bitfield - table->decode_len[bits - 1];
1977 dist >>= (16 - bits);
1978 pos = table->decode_pos[bits] + dist;
1980 if(pos >= table->size)
1983 *num = table->decode_num[pos];
1987 /* Reads and parses Huffman tables from the beginning of the block. */
1988 static int parse_tables(struct archive_read* a, struct rar5* rar,
1991 int ret, value, i, w, idx = 0;
1992 uint8_t bit_length[HUFF_BC],
1993 table[HUFF_TABLE_SIZE],
1997 enum { ESCAPE = 15 };
1999 /* The data for table generation is compressed using a simple RLE-like
2000 * algorithm when storing zeroes, so we need to unpack it first. */
2001 for(w = 0, i = 0; w < HUFF_BC;) {
2002 value = (p[i] & nibble_mask) >> nibble_shift;
2004 if(nibble_mask == 0x0F)
2007 nibble_mask ^= 0xFF;
2010 /* Values smaller than 15 is data, so we write it directly. Value 15
2011 * is a flag telling us that we need to unpack more bytes. */
2012 if(value == ESCAPE) {
2013 value = (p[i] & nibble_mask) >> nibble_shift;
2014 if(nibble_mask == 0x0F)
2016 nibble_mask ^= 0xFF;
2020 /* We sometimes need to write the actual value of 15, so this
2021 * case handles that. */
2022 bit_length[w++] = ESCAPE;
2027 for(k = 0; k < value + 2; k++) {
2028 bit_length[w++] = 0;
2032 bit_length[w++] = value;
2036 rar->bits.in_addr = i;
2037 rar->bits.bit_addr = nibble_shift ^ 4;
2039 ret = create_decode_tables(bit_length, &rar->cstate.bd, HUFF_BC);
2040 if(ret != ARCHIVE_OK) {
2041 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2042 "Decoding huffman tables failed");
2043 return ARCHIVE_FATAL;
2046 for(i = 0; i < HUFF_TABLE_SIZE;) {
2049 ret = decode_number(a, &rar->cstate.bd, p, &num);
2050 if(ret != ARCHIVE_OK) {
2051 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2052 "Decoding huffman tables failed");
2053 return ARCHIVE_FATAL;
2057 /* 0..15: store directly */
2058 table[i] = (uint8_t) num;
2064 /* 16..17: repeat previous code */
2066 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2080 while(n-- > 0 && i < HUFF_TABLE_SIZE) {
2081 table[i] = table[i - 1];
2085 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2086 "Unexpected error when decoding huffman tables");
2087 return ARCHIVE_FATAL;
2093 /* other codes: fill with zeroes `n` times */
2095 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2108 while(n-- > 0 && i < HUFF_TABLE_SIZE)
2112 ret = create_decode_tables(&table[idx], &rar->cstate.ld, HUFF_NC);
2113 if(ret != ARCHIVE_OK) {
2114 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2115 "Failed to create literal table");
2116 return ARCHIVE_FATAL;
2121 ret = create_decode_tables(&table[idx], &rar->cstate.dd, HUFF_DC);
2122 if(ret != ARCHIVE_OK) {
2123 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2124 "Failed to create distance table");
2125 return ARCHIVE_FATAL;
2130 ret = create_decode_tables(&table[idx], &rar->cstate.ldd, HUFF_LDC);
2131 if(ret != ARCHIVE_OK) {
2132 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2133 "Failed to create lower bits of distances table");
2134 return ARCHIVE_FATAL;
2139 ret = create_decode_tables(&table[idx], &rar->cstate.rd, HUFF_RC);
2140 if(ret != ARCHIVE_OK) {
2141 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2142 "Failed to create repeating distances table");
2143 return ARCHIVE_FATAL;
2149 /* Parses the block header, verifies its CRC byte, and saves the header
2150 * fields inside the `hdr` pointer. */
2151 static int parse_block_header(struct archive_read* a, const uint8_t* p,
2152 ssize_t* block_size, struct compressed_block_header* hdr)
2154 memcpy(hdr, p, sizeof(struct compressed_block_header));
2156 if(hdr->block_flags.byte_count > 2) {
2157 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2158 "Unsupported block header size (was %d, max is 2)",
2159 hdr->block_flags.byte_count);
2160 return ARCHIVE_FATAL;
2163 /* This should probably use bit reader interface in order to be more
2166 switch(hdr->block_flags.byte_count) {
2167 /* 1-byte block size */
2169 *block_size = *(const uint8_t*) &p[2];
2172 /* 2-byte block size */
2174 *block_size = *(const uint16_t*) &p[2];
2177 /* 3-byte block size */
2179 *block_size = *(const uint32_t*) &p[2];
2180 *block_size &= 0x00FFFFFF;
2183 /* Other block sizes are not supported. This case is not reached,
2184 * because we have an 'if' guard before the switch that makes sure
2187 return ARCHIVE_FATAL;
2190 /* Verify the block header checksum. 0x5A is a magic value and is always
2192 uint8_t calculated_cksum = 0x5A
2193 ^ (uint8_t) hdr->block_flags_u8
2194 ^ (uint8_t) *block_size
2195 ^ (uint8_t) (*block_size >> 8)
2196 ^ (uint8_t) (*block_size >> 16);
2198 if(calculated_cksum != hdr->block_cksum) {
2199 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2200 "Block checksum error: got 0x%02x, expected 0x%02x",
2201 hdr->block_cksum, calculated_cksum);
2203 return ARCHIVE_FATAL;
2209 /* Convinience function used during filter processing. */
2210 static int parse_filter_data(struct rar5* rar, const uint8_t* p,
2211 uint32_t* filter_data)
2216 if(ARCHIVE_OK != read_consume_bits(rar, p, 2, &bytes))
2221 for(i = 0; i < bytes; i++) {
2224 if(ARCHIVE_OK != read_bits_16(rar, p, &byte)) {
2228 data += (byte >> 8) << (i * 8);
2232 *filter_data = data;
2236 /* Function is used during sanity checking. */
2237 static int is_valid_filter_block_start(struct rar5* rar,
2240 const int64_t block_start = (ssize_t) start + rar->cstate.write_ptr;
2241 const int64_t last_bs = rar->cstate.last_block_start;
2242 const ssize_t last_bl = rar->cstate.last_block_length;
2244 if(last_bs == 0 || last_bl == 0) {
2245 /* We didn't have any filters yet, so accept this offset. */
2249 if(block_start >= last_bs + last_bl) {
2250 /* Current offset is bigger than last block's end offset, so
2251 * accept current offset. */
2255 /* Any other case is not a normal situation and we should fail. */
2259 /* The function will create a new filter, read its parameters from the input
2260 * stream and add it to the filter collection. */
2261 static int parse_filter(struct archive_read* ar, const uint8_t* p) {
2262 uint32_t block_start, block_length;
2263 uint16_t filter_type;
2264 struct rar5* rar = get_context(ar);
2266 /* Read the parameters from the input stream. */
2267 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_start))
2270 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_length))
2273 if(ARCHIVE_OK != read_bits_16(rar, p, &filter_type))
2279 /* Perform some sanity checks on this filter parameters. Note that we
2280 * allow only DELTA, E8/E9 and ARM filters here, because rest of filters
2281 * are not used in RARv5. */
2283 if(block_length < 4 ||
2284 block_length > 0x400000 ||
2285 filter_type > FILTER_ARM ||
2286 !is_valid_filter_block_start(rar, block_start))
2288 archive_set_error(&ar->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Invalid "
2289 "filter encountered");
2290 return ARCHIVE_FATAL;
2293 /* Allocate a new filter. */
2294 struct filter_info* filt = add_new_filter(rar);
2296 archive_set_error(&ar->archive, ENOMEM, "Can't allocate memory for a "
2297 "filter descriptor.");
2298 return ARCHIVE_FATAL;
2301 filt->type = filter_type;
2302 filt->block_start = rar->cstate.write_ptr + block_start;
2303 filt->block_length = block_length;
2305 rar->cstate.last_block_start = filt->block_start;
2306 rar->cstate.last_block_length = filt->block_length;
2308 /* Read some more data in case this is a DELTA filter. Other filter types
2309 * don't require any additional data over what was already read. */
2310 if(filter_type == FILTER_DELTA) {
2313 if(ARCHIVE_OK != read_consume_bits(rar, p, 5, &channels))
2316 filt->channels = channels + 1;
2322 static int decode_code_length(struct rar5* rar, const uint8_t* p,
2325 int lbits, length = 2;
2330 lbits = code / 4 - 1;
2331 length += (4 | (code & 3)) << lbits;
2337 if(ARCHIVE_OK != read_consume_bits(rar, p, lbits, &add))
2346 static int copy_string(struct archive_read* a, int len, int dist) {
2347 struct rar5* rar = get_context(a);
2348 const int cmask = rar->cstate.window_mask;
2349 const int64_t write_ptr = rar->cstate.write_ptr + rar->cstate.solid_offset;
2352 /* The unpacker spends most of the time in this function. It would be
2353 * a good idea to introduce some optimizations here.
2355 * Just remember that this loop treats buffers that overlap differently
2356 * than buffers that do not overlap. This is why a simple memcpy(3) call
2357 * will not be enough. */
2359 for(i = 0; i < len; i++) {
2360 const ssize_t write_idx = (write_ptr + i) & cmask;
2361 const ssize_t read_idx = (write_ptr + i - dist) & cmask;
2362 rar->cstate.window_buf[write_idx] = rar->cstate.window_buf[read_idx];
2365 rar->cstate.write_ptr += len;
2369 static int do_uncompress_block(struct archive_read* a, const uint8_t* p) {
2370 struct rar5* rar = get_context(a);
2374 const int cmask = rar->cstate.window_mask;
2375 const struct compressed_block_header* hdr = &rar->last_block_hdr;
2376 const uint8_t bit_size = 1 + hdr->block_flags.bit_size;
2379 if(rar->cstate.write_ptr - rar->cstate.last_write_ptr >
2380 (rar->cstate.window_size >> 1)) {
2382 /* Don't allow growing data by more than half of the window size
2383 * at a time. In such case, break the loop; next call to this
2384 * function will continue processing from this moment. */
2389 if(rar->bits.in_addr > rar->cstate.cur_block_size - 1 ||
2390 (rar->bits.in_addr == rar->cstate.cur_block_size - 1 &&
2391 rar->bits.bit_addr >= bit_size))
2393 /* If the program counter is here, it means the function has
2394 * finished processing the block. */
2395 rar->cstate.block_parsing_finished = 1;
2399 /* Decode the next literal. */
2400 if(ARCHIVE_OK != decode_number(a, &rar->cstate.ld, p, &num)) {
2404 /* Num holds a decompression literal, or 'command code'.
2406 * - Values lower than 256 are just bytes. Those codes can be stored
2407 * in the output buffer directly.
2409 * - Code 256 defines a new filter, which is later used to transform
2410 * the data block accordingly to the filter type. The data block
2411 * needs to be fully uncompressed first.
2413 * - Code bigger than 257 and smaller than 262 define a repetition
2414 * pattern that should be copied from an already uncompressed chunk
2419 /* Directly store the byte. */
2421 int64_t write_idx = rar->cstate.solid_offset +
2422 rar->cstate.write_ptr++;
2424 rar->cstate.window_buf[write_idx & cmask] = (uint8_t) num;
2426 } else if(num >= 262) {
2428 int len = decode_code_length(rar, p, num - 262),
2433 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2434 "Failed to decode the code length");
2436 return ARCHIVE_FATAL;
2439 if(ARCHIVE_OK != decode_number(a, &rar->cstate.dd, p, &dist_slot))
2441 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2442 "Failed to decode the distance slot");
2444 return ARCHIVE_FATAL;
2451 dbits = dist_slot / 2 - 1;
2452 dist += (2 | (dist_slot & 1)) << dbits;
2461 if(ARCHIVE_OK != read_bits_32(rar, p, &add)) {
2462 /* Return EOF if we can't read more data. */
2466 skip_bits(rar, dbits - 4);
2467 add = (add >> (36 - dbits)) << 4;
2471 if(ARCHIVE_OK != decode_number(a, &rar->cstate.ldd, p,
2474 archive_set_error(&a->archive,
2475 ARCHIVE_ERRNO_PROGRAMMER,
2476 "Failed to decode the distance slot");
2478 return ARCHIVE_FATAL;
2483 /* dbits is one of [0,1,2,3] */
2486 if(ARCHIVE_OK != read_consume_bits(rar, p, dbits, &add)) {
2487 /* Return EOF if we can't read more data. */
2501 if(dist > 0x40000) {
2507 dist_cache_push(rar, dist);
2508 rar->cstate.last_len = len;
2510 if(ARCHIVE_OK != copy_string(a, len, dist))
2511 return ARCHIVE_FATAL;
2514 } else if(num == 256) {
2515 /* Create a filter. */
2516 ret = parse_filter(a, p);
2517 if(ret != ARCHIVE_OK)
2521 } else if(num == 257) {
2522 if(rar->cstate.last_len != 0) {
2523 if(ARCHIVE_OK != copy_string(a, rar->cstate.last_len,
2524 rar->cstate.dist_cache[0]))
2526 return ARCHIVE_FATAL;
2531 } else if(num < 262) {
2532 const int index = num - 258;
2533 const int dist = dist_cache_touch(rar, index);
2538 if(ARCHIVE_OK != decode_number(a, &rar->cstate.rd, p, &len_slot)) {
2539 return ARCHIVE_FATAL;
2542 len = decode_code_length(rar, p, len_slot);
2543 rar->cstate.last_len = len;
2545 if(ARCHIVE_OK != copy_string(a, len, dist))
2546 return ARCHIVE_FATAL;
2551 /* The program counter shouldn't reach here. */
2552 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2553 "Unsupported block code: 0x%02x", num);
2555 return ARCHIVE_FATAL;
2561 /* Binary search for the RARv5 signature. */
2562 static int scan_for_signature(struct archive_read* a) {
2564 const int chunk_size = 512;
2567 /* If we're here, it means we're on an 'unknown territory' data.
2568 * There's no indication what kind of data we're reading here. It could be
2569 * some text comment, any kind of binary data, digital sign, dragons, etc.
2571 * We want to find a valid RARv5 magic header inside this unknown data. */
2573 /* Is it possible in libarchive to just skip everything until the
2574 * end of the file? If so, it would be a better approach than the
2575 * current implementation of this function. */
2578 if(!read_ahead(a, chunk_size, &p))
2581 for(i = 0; i < chunk_size - rar5_signature_size; i++) {
2582 if(memcmp(&p[i], rar5_signature, rar5_signature_size) == 0) {
2583 /* Consume the number of bytes we've used to search for the
2584 * signature, as well as the number of bytes used by the
2585 * signature itself. After this we should be standing on a
2586 * valid base block header. */
2587 (void) consume(a, i + rar5_signature_size);
2592 consume(a, chunk_size);
2595 return ARCHIVE_FATAL;
2598 /* This function will switch the multivolume archive file to another file,
2599 * i.e. from part03 to part 04. */
2600 static int advance_multivolume(struct archive_read* a) {
2602 struct rar5* rar = get_context(a);
2604 /* A small state machine that will skip unnecessary data, needed to
2605 * switch from one multivolume to another. Such skipping is needed if
2606 * we want to be an stream-oriented (instead of file-oriented)
2609 * The state machine starts with `rar->main.endarc` == 0. It also
2610 * assumes that current stream pointer points to some base block header.
2612 * The `endarc` field is being set when the base block parsing function
2613 * encounters the 'end of archive' marker.
2617 if(rar->main.endarc == 1) {
2618 rar->main.endarc = 0;
2619 while(ARCHIVE_RETRY == skip_base_block(a));
2622 /* Skip current base block. In order to properly skip it,
2623 * we really need to simply parse it and discard the results. */
2625 lret = skip_base_block(a);
2627 /* The `skip_base_block` function tells us if we should continue
2628 * with skipping, or we should stop skipping. We're trying to skip
2629 * everything up to a base FILE block. */
2631 if(lret != ARCHIVE_RETRY) {
2632 /* If there was an error during skipping, or we have just
2633 * skipped a FILE base block... */
2635 if(rar->main.endarc == 0) {
2647 /* Merges the partial block from the first multivolume archive file, and
2648 * partial block from the second multivolume archive file. The result is
2649 * a chunk of memory containing the whole block, and the stream pointer
2650 * is advanced to the next block in the second multivolume archive file. */
2651 static int merge_block(struct archive_read* a, ssize_t block_size,
2654 struct rar5* rar = get_context(a);
2655 ssize_t cur_block_size, partial_offset = 0;
2659 /* Set a flag that we're in the switching mode. */
2660 rar->cstate.switch_multivolume = 1;
2662 /* Reallocate the memory which will hold the whole block. */
2663 if(rar->vol.push_buf)
2664 free((void*) rar->vol.push_buf);
2666 rar->vol.push_buf = malloc(block_size);
2667 if(!rar->vol.push_buf) {
2668 archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for a "
2669 "merge block buffer.");
2670 return ARCHIVE_FATAL;
2673 /* A single block can span across multiple multivolume archive files,
2674 * so we use a loop here. This loop will consume enough multivolume
2675 * archive files until the whole block is read. */
2678 /* Get the size of current block chunk in this multivolume archive
2679 * file and read it. */
2681 rar5_min(rar->file.bytes_remaining, block_size - partial_offset);
2683 if(!read_ahead(a, cur_block_size, &lp))
2686 /* Sanity check; there should never be a situation where this function
2687 * reads more data than the block's size. */
2688 if(partial_offset + cur_block_size > block_size) {
2689 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2690 "Consumed too much data when merging blocks.");
2691 return ARCHIVE_FATAL;
2694 /* Merge previous block chunk with current block chunk, or create
2695 * first block chunk if this is our first iteration. */
2696 memcpy(&rar->vol.push_buf[partial_offset], lp, cur_block_size);
2698 /* Advance the stream read pointer by this block chunk size. */
2699 if(ARCHIVE_OK != consume(a, cur_block_size))
2702 /* Update the pointers. `partial_offset` contains information about
2703 * the sum of merged block chunks. */
2704 partial_offset += cur_block_size;
2705 rar->file.bytes_remaining -= cur_block_size;
2707 /* If `partial_offset` is the same as `block_size`, this means we've
2708 * merged all block chunks and we have a valid full block. */
2709 if(partial_offset == block_size) {
2713 /* If we don't have any bytes to read, this means we should switch
2714 * to another multivolume archive file. */
2715 if(rar->file.bytes_remaining == 0) {
2716 ret = advance_multivolume(a);
2717 if(ret != ARCHIVE_OK)
2722 *p = rar->vol.push_buf;
2724 /* If we're here, we can resume unpacking by processing the block pointed
2725 * to by the `*p` memory pointer. */
2730 static int process_block(struct archive_read* a) {
2732 struct rar5* rar = get_context(a);
2735 /* If we don't have any data to be processed, this most probably means
2736 * we need to switch to the next volume. */
2737 if(rar->main.volume && rar->file.bytes_remaining == 0) {
2738 ret = advance_multivolume(a);
2739 if(ret != ARCHIVE_OK)
2743 if(rar->cstate.block_parsing_finished) {
2746 rar->cstate.block_parsing_finished = 0;
2748 /* The header size won't be bigger than 6 bytes. */
2749 if(!read_ahead(a, 6, &p)) {
2750 /* Failed to prefetch data block header. */
2755 * Read block_size by parsing block header. Validate the header by
2756 * calculating CRC byte stored inside the header. Size of the header is
2757 * not constant (block size can be stored either in 1 or 2 bytes),
2758 * that's why block size is left out from the `compressed_block_header`
2759 * structure and returned by `parse_block_header` as the second
2762 ret = parse_block_header(a, p, &block_size, &rar->last_block_hdr);
2763 if(ret != ARCHIVE_OK)
2766 /* Skip block header. Next data is huffman tables, if present. */
2767 ssize_t to_skip = sizeof(struct compressed_block_header) +
2768 rar->last_block_hdr.block_flags.byte_count + 1;
2770 if(ARCHIVE_OK != consume(a, to_skip))
2773 rar->file.bytes_remaining -= to_skip;
2775 /* The block size gives information about the whole block size, but
2776 * the block could be stored in split form when using multi-volume
2777 * archives. In this case, the block size will be bigger than the
2778 * actual data stored in this file. Remaining part of the data will
2779 * be in another file. */
2781 ssize_t cur_block_size =
2782 rar5_min(rar->file.bytes_remaining, block_size);
2784 if(block_size > rar->file.bytes_remaining) {
2785 /* If current blocks' size is bigger than our data size, this
2786 * means we have a multivolume archive. In this case, skip
2787 * all base headers until the end of the file, proceed to next
2788 * "partXXX.rar" volume, find its signature, skip all headers up
2789 * to the first FILE base header, and continue from there.
2791 * Note that `merge_block` will update the `rar` context structure
2792 * quite extensively. */
2794 ret = merge_block(a, block_size, &p);
2795 if(ret != ARCHIVE_OK) {
2799 cur_block_size = block_size;
2801 /* Current stream pointer should be now directly *after* the
2802 * block that spanned through multiple archive files. `p` pointer
2803 * should have the data of the *whole* block (merged from
2804 * partial blocks stored in multiple archives files). */
2806 rar->cstate.switch_multivolume = 0;
2808 /* Read the whole block size into memory. This can take up to
2809 * 8 megabytes of memory in theoretical cases. Might be worth to
2810 * optimize this and use a standard chunk of 4kb's. */
2812 if(!read_ahead(a, 4 + cur_block_size, &p)) {
2813 /* Failed to prefetch block data. */
2818 rar->cstate.block_buf = p;
2819 rar->cstate.cur_block_size = cur_block_size;
2821 rar->bits.in_addr = 0;
2822 rar->bits.bit_addr = 0;
2824 if(rar->last_block_hdr.block_flags.is_table_present) {
2825 /* Load Huffman tables. */
2826 ret = parse_tables(a, rar, p);
2827 if(ret != ARCHIVE_OK) {
2828 /* Error during decompression of Huffman tables. */
2833 p = rar->cstate.block_buf;
2836 /* Uncompress the block, or a part of it, depending on how many bytes
2837 * will be generated by uncompressing the block.
2839 * In case too many bytes will be generated, calling this function again
2840 * will resume the uncompression operation. */
2841 ret = do_uncompress_block(a, p);
2842 if(ret != ARCHIVE_OK) {
2846 if(rar->cstate.block_parsing_finished &&
2847 rar->cstate.switch_multivolume == 0 &&
2848 rar->cstate.cur_block_size > 0)
2850 /* If we're processing a normal block, consume the whole block. We
2851 * can do this because we've already read the whole block to memory.
2853 if(ARCHIVE_OK != consume(a, rar->cstate.cur_block_size))
2854 return ARCHIVE_FATAL;
2856 rar->file.bytes_remaining -= rar->cstate.cur_block_size;
2857 } else if(rar->cstate.switch_multivolume) {
2858 /* Don't consume the block if we're doing multivolume processing.
2859 * The volume switching function will consume the proper count of
2862 rar->cstate.switch_multivolume = 0;
2868 /* Pops the `buf`, `size` and `offset` from the "data ready" stack.
2870 * Returns ARCHIVE_OK when those arguments can be used, ARCHIVE_RETRY
2871 * when there is no data on the stack. */
2872 static int use_data(struct rar5* rar, const void** buf, size_t* size,
2877 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
2878 struct data_ready *d = &rar->cstate.dready[i];
2881 if(buf) *buf = d->buf;
2882 if(size) *size = d->size;
2883 if(offset) *offset = d->offset;
2890 return ARCHIVE_RETRY;
2893 /* Pushes the `buf`, `size` and `offset` arguments to the rar->cstate.dready
2894 * FIFO stack. Those values will be popped from this stack by the `use_data`
2896 static int push_data_ready(struct archive_read* a, struct rar5* rar,
2897 const uint8_t* buf, size_t size, int64_t offset)
2901 /* Don't push if we're in skip mode. This is needed because solid
2902 * streams need full processing even if we're skipping data. After fully
2903 * processing the stream, we need to discard the generated bytes, because
2904 * we're interested only in the side effect: building up the internal
2905 * window circular buffer. This window buffer will be used later during
2906 * unpacking of requested data. */
2911 if(offset != rar->file.last_offset + rar->file.last_size) {
2912 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "Sanity "
2913 "check error: output stream is not continuous");
2914 return ARCHIVE_FATAL;
2917 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
2918 struct data_ready* d = &rar->cstate.dready[i];
2925 /* These fields are used only in sanity checking. */
2926 rar->file.last_offset = offset;
2927 rar->file.last_size = size;
2929 /* Calculate the checksum of this new block before submitting
2930 * data to libarchive's engine. */
2931 update_crc(rar, d->buf, d->size);
2937 /* Program counter will reach this code if the `rar->cstate.data_ready`
2938 * stack will be filled up so that no new entries will be allowed. The
2939 * code shouldn't allow such situation to occur. So we treat this case
2940 * as an internal error. */
2942 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "Error: "
2943 "premature end of data_ready stack");
2944 return ARCHIVE_FATAL;
2947 /* This function uncompresses the data that is stored in the <FILE> base
2950 * The FILE base block looks like this:
2952 * <header><huffman tables><block_1><block_2>...<block_n>
2954 * The <header> is a block header, that is parsed in parse_block_header().
2955 * It's a "compressed_block_header" structure, containing metadata needed
2956 * to know when we should stop looking for more <block_n> blocks.
2958 * <huffman tables> contain data needed to set up the huffman tables, needed
2959 * for the actual decompression.
2961 * Each <block_n> consists of series of literals:
2963 * <literal><literal><literal>...<literal>
2965 * Those literals generate the uncompression data. They operate on a circular
2966 * buffer, sometimes writing raw data into it, sometimes referencing
2967 * some previous data inside this buffer, and sometimes declaring a filter
2968 * that will need to be executed on the data stored in the circular buffer.
2969 * It all depends on the literal that is used.
2971 * Sometimes blocks produce output data, sometimes they don't. For example, for
2972 * some huge files that use lots of filters, sometimes a block is filled with
2973 * only filter declaration literals. Such blocks won't produce any data in the
2976 * Sometimes blocks will produce 4 bytes of data, and sometimes 1 megabyte,
2977 * because a literal can reference previously decompressed data. For example,
2978 * there can be a literal that says: 'append a byte 0xFE here', and after
2979 * it another literal can say 'append 1 megabyte of data from circular buffer
2980 * offset 0x12345'. This is how RAR format handles compressing repeated
2983 * The RAR compressor creates those literals and the actual efficiency of
2984 * compression depends on what those literals are. The literals can also
2985 * be seen as a kind of a non-turing-complete virtual machine that simply
2986 * tells the decompressor what it should do.
2989 static int do_uncompress_file(struct archive_read* a) {
2990 struct rar5* rar = get_context(a);
2992 int64_t max_end_pos;
2994 if(!rar->cstate.initialized) {
2995 /* Don't perform full context reinitialization if we're processing
2996 * a solid archive. */
2997 if(!rar->main.solid || !rar->cstate.window_buf) {
3001 rar->cstate.initialized = 1;
3004 if(rar->cstate.all_filters_applied == 1) {
3005 /* We use while(1) here, but standard case allows for just 1 iteration.
3006 * The loop will iterate if process_block() didn't generate any data at
3007 * all. This can happen if the block contains only filter definitions
3008 * (this is common in big files). */
3011 ret = process_block(a);
3012 if(ret == ARCHIVE_EOF || ret == ARCHIVE_FATAL)
3015 if(rar->cstate.last_write_ptr == rar->cstate.write_ptr) {
3016 /* The block didn't generate any new data, so just process
3021 /* The block has generated some new data, so break the loop. */
3026 /* Try to run filters. If filters won't be applied, it means that
3027 * insufficient data was generated. */
3028 ret = apply_filters(a);
3029 if(ret == ARCHIVE_RETRY) {
3031 } else if(ret == ARCHIVE_FATAL) {
3032 return ARCHIVE_FATAL;
3035 /* If apply_filters() will return ARCHIVE_OK, we can continue here. */
3037 if(cdeque_size(&rar->cstate.filters) > 0) {
3038 /* Check if we can write something before hitting first filter. */
3039 struct filter_info* flt;
3041 /* Get the block_start offset from the first filter. */
3042 if(CDE_OK != cdeque_front(&rar->cstate.filters, cdeque_filter_p(&flt)))
3044 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3045 "Can't read first filter");
3046 return ARCHIVE_FATAL;
3049 max_end_pos = rar5_min(flt->block_start, rar->cstate.write_ptr);
3051 /* There are no filters defined, or all filters were applied. This
3052 * means we can just store the data without any postprocessing. */
3053 max_end_pos = rar->cstate.write_ptr;
3056 if(max_end_pos == rar->cstate.last_write_ptr) {
3057 /* We can't write anything yet. The block uncompression function did
3058 * not generate enough data, and no filter can be applied. At the same
3059 * time we don't have any data that can be stored without filter
3060 * postprocessing. This means we need to wait for more data to be
3061 * generated, so we can apply the filters.
3063 * Signal the caller that we need more data to be able to do anything.
3065 return ARCHIVE_RETRY;
3067 /* We can write the data before hitting the first filter. So let's
3068 * do it. The push_window_data() function will effectively return
3069 * the selected data block to the user application. */
3070 push_window_data(a, rar, rar->cstate.last_write_ptr, max_end_pos);
3071 rar->cstate.last_write_ptr = max_end_pos;
3077 static int uncompress_file(struct archive_read* a) {
3081 /* Sometimes the uncompression function will return a 'retry' signal.
3082 * If this will happen, we have to retry the function. */
3083 ret = do_uncompress_file(a);
3084 if(ret != ARCHIVE_RETRY)
3090 static int do_unstore_file(struct archive_read* a,
3098 if(rar->file.bytes_remaining == 0 && rar->main.volume > 0 &&
3099 rar->generic.split_after > 0)
3103 rar->cstate.switch_multivolume = 1;
3104 ret = advance_multivolume(a);
3105 rar->cstate.switch_multivolume = 0;
3107 if(ret != ARCHIVE_OK) {
3108 /* Failed to advance to next multivolume archive file. */
3113 size_t to_read = rar5_min(rar->file.bytes_remaining, 64 * 1024);
3115 if(!read_ahead(a, to_read, &p)) {
3116 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "I/O error "
3117 "when unstoring file");
3118 return ARCHIVE_FATAL;
3121 if(ARCHIVE_OK != consume(a, to_read)) {
3126 if(size) *size = to_read;
3127 if(offset) *offset = rar->cstate.last_unstore_ptr;
3129 rar->file.bytes_remaining -= to_read;
3130 rar->cstate.last_unstore_ptr += to_read;
3132 update_crc(rar, p, to_read);
3136 static int do_unpack(struct archive_read* a, struct rar5* rar,
3137 const void** buf, size_t* size, int64_t* offset)
3139 enum COMPRESSION_METHOD {
3140 STORE = 0, FASTEST = 1, FAST = 2, NORMAL = 3, GOOD = 4, BEST = 5
3143 if(rar->file.service > 0) {
3144 return do_unstore_file(a, rar, buf, size, offset);
3146 switch(rar->cstate.method) {
3148 return do_unstore_file(a, rar, buf, size, offset);
3158 return uncompress_file(a);
3160 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3161 "Compression method not supported: 0x%08x",
3162 rar->cstate.method);
3164 return ARCHIVE_FATAL;
3174 static int verify_checksums(struct archive_read* a) {
3176 struct rar5* rar = get_context(a);
3178 /* Check checksums only when actually unpacking the data. There's no need
3179 * to calculate checksum when we're skipping data in solid archives
3180 * (skipping in solid archives is the same thing as unpacking compressed
3181 * data and discarding the result). */
3183 if(!rar->skip_mode) {
3184 /* Always check checkums if we're not in skip mode */
3187 /* We can override the logic above with a compile-time option
3188 * NO_CRC_ON_SOLID_SKIP. This option is used during debugging, and it
3189 * will check checksums of unpacked data even when we're skipping it.
3192 #if defined CHECK_CRC_ON_SOLID_SKIP
3202 /* During unpacking, on each unpacked block we're calling the
3203 * update_crc() function. Since we are here, the unpacking process is
3204 * already over and we can check if calculated checksum (CRC32 or
3205 * BLAKE2sp) is the same as what is stored in the archive.
3207 if(rar->file.stored_crc32 > 0) {
3208 /* Check CRC32 only when the file contains a CRC32 value for this
3211 if(rar->file.calculated_crc32 != rar->file.stored_crc32) {
3212 /* Checksums do not match; the unpacked file is corrupted. */
3215 printf("Checksum error: CRC32 (was: %08x, expected: %08x)\n",
3216 rar->file.calculated_crc32, rar->file.stored_crc32);
3219 #ifndef DONT_FAIL_ON_CRC_ERROR
3220 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3221 "Checksum error: CRC32");
3222 return ARCHIVE_FATAL;
3226 printf("Checksum OK: CRC32 (%08x/%08x)\n",
3227 rar->file.stored_crc32,
3228 rar->file.calculated_crc32);
3233 if(rar->file.has_blake2 > 0) {
3234 /* BLAKE2sp is an optional checksum algorithm that is added to
3235 * RARv5 archives when using the `-htb` switch during creation of
3238 * We now finalize the hash calculation by calling the `final`
3239 * function. This will generate the final hash value we can use to
3240 * compare it with the BLAKE2sp checksum that is stored in the
3243 * The return value of this `final` function is not very helpful,
3244 * as it guards only against improper use. This is why we're
3245 * explicitly ignoring it. */
3248 (void) blake2sp_final(&rar->file.b2state, b2_buf, 32);
3250 if(memcmp(&rar->file.blake2sp, b2_buf, 32) != 0) {
3251 #ifndef DONT_FAIL_ON_CRC_ERROR
3252 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3253 "Checksum error: BLAKE2");
3255 return ARCHIVE_FATAL;
3261 /* Finalization for this file has been successfully completed. */
3265 static int verify_global_checksums(struct archive_read* a) {
3266 return verify_checksums(a);
3269 static int rar5_read_data(struct archive_read *a, const void **buff,
3270 size_t *size, int64_t *offset) {
3272 struct rar5* rar = get_context(a);
3274 if(!rar->skip_mode && (rar->cstate.last_write_ptr > rar->file.unpacked_size)) {
3275 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3276 "Unpacker has written too many bytes");
3277 return ARCHIVE_FATAL;
3280 ret = use_data(rar, buff, size, offset);
3281 if(ret == ARCHIVE_OK)
3284 ret = do_unpack(a, rar, buff, size, offset);
3285 if(ret != ARCHIVE_OK) {
3289 if(rar->file.bytes_remaining == 0 &&
3290 rar->cstate.last_write_ptr == rar->file.unpacked_size)
3292 /* If all bytes of current file were processed, run finalization.
3294 * Finalization will check checksum against proper values. If
3295 * some of the checksums will not match, we'll return an error
3296 * value in the last `archive_read_data` call to signal an error
3299 return verify_global_checksums(a);
3305 static int rar5_read_data_skip(struct archive_read *a) {
3306 struct rar5* rar = get_context(a);
3308 if(rar->main.solid) {
3309 /* In solid archives, instead of skipping the data, we need to extract
3310 * it, and dispose the result. The side effect of this operation will
3311 * be setting up the initial window buffer state needed to be able to
3312 * extract the selected file. */
3316 /* Make sure to process all blocks in the compressed stream. */
3317 while(rar->file.bytes_remaining > 0) {
3318 /* Setting the "skip mode" will allow us to skip checksum checks
3319 * during data skipping. Checking the checksum of skipped data
3320 * isn't really necessary and it's only slowing things down.
3322 * This is incremented instead of setting to 1 because this data
3323 * skipping function can be called recursively. */
3326 /* We're disposing 1 block of data, so we use triple NULLs in
3329 ret = rar5_read_data(a, NULL, NULL, NULL);
3331 /* Turn off "skip mode". */
3335 /* Propagate any potential error conditions to the caller. */
3340 /* In standard archives, we can just jump over the compressed stream.
3341 * Each file in non-solid archives starts from an empty window buffer.
3344 if(ARCHIVE_OK != consume(a, rar->file.bytes_remaining)) {
3345 return ARCHIVE_FATAL;
3348 rar->file.bytes_remaining = 0;
3354 static int64_t rar5_seek_data(struct archive_read *a, int64_t offset,
3361 /* We're a streaming unpacker, and we don't support seeking. */
3363 return ARCHIVE_FATAL;
3366 static int rar5_cleanup(struct archive_read *a) {
3367 struct rar5* rar = get_context(a);
3369 if(rar->cstate.window_buf)
3370 free(rar->cstate.window_buf);
3372 if(rar->cstate.filtered_buf)
3373 free(rar->cstate.filtered_buf);
3375 if(rar->vol.push_buf)
3376 free(rar->vol.push_buf);
3379 cdeque_free(&rar->cstate.filters);
3382 a->format->data = NULL;
3387 static int rar5_capabilities(struct archive_read * a) {
3392 static int rar5_has_encrypted_entries(struct archive_read *_a) {
3395 /* Unsupported for now. */
3396 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
3399 static int rar5_init(struct rar5* rar) {
3402 memset(rar, 0, sizeof(struct rar5));
3404 /* Decrypt the magic signature pattern. Check the comment near the
3405 * `rar5_signature` symbol to read the rationale behind this. */
3407 if(rar5_signature[0] == 243) {
3408 for(i = 0; i < rar5_signature_size; i++) {
3409 rar5_signature[i] ^= 0xA1;
3413 if(CDE_OK != cdeque_init(&rar->cstate.filters, 8192))
3414 return ARCHIVE_FATAL;
3419 int archive_read_support_format_rar5(struct archive *_a) {
3420 struct archive_read* ar;
3424 if(ARCHIVE_OK != (ret = get_archive_read(_a, &ar)))
3427 rar = malloc(sizeof(*rar));
3429 archive_set_error(&ar->archive, ENOMEM, "Can't allocate rar5 data");
3430 return ARCHIVE_FATAL;
3433 if(ARCHIVE_OK != rar5_init(rar)) {
3434 archive_set_error(&ar->archive, ENOMEM, "Can't allocate rar5 filter "
3436 return ARCHIVE_FATAL;
3439 ret = __archive_read_register_format(ar,
3446 rar5_read_data_skip,
3450 rar5_has_encrypted_entries);
3452 if(ret != ARCHIVE_OK) {
3453 (void) rar5_cleanup(ar);