1 /*-
2 * Copyright (c) 2018 Grzegorz Antoniak (http://antoniak.org)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25 
26 #include "archive_platform.h"
27 
28 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #include <time.h>
32 #ifdef HAVE_ZLIB_H
33 #include <zlib.h> /* crc32 */
34 #endif
35 
36 #include "archive.h"
37 #ifndef HAVE_ZLIB_H
38 #include "archive_crc32.h"
39 #endif
40 
41 #include "archive_entry.h"
42 #include "archive_entry_locale.h"
43 #include "archive_ppmd7_private.h"
44 #include "archive_entry_private.h"
45 
46 #ifdef HAVE_BLAKE2_H
47 #include <blake2.h>
48 #else
49 #include "archive_blake2.h"
50 #endif
51 
52 /*#define CHECK_CRC_ON_SOLID_SKIP*/
53 /*#define DONT_FAIL_ON_CRC_ERROR*/
54 /*#define DEBUG*/
55 
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)))
59 
60 #if defined DEBUG
61 #define DEBUG_CODE if(1)
62 #else
63 #define DEBUG_CODE if(0)
64 #endif
65 
66 /* Real RAR5 magic number is:
67  *
68  * 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00
69  * "Rar!→•☺·\x00"
70  *
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
74  * this "false" one.
75  *
76  * The array itself is decrypted in `rar5_init` function. */
77 
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;
82 
83 struct file_header {
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. */
88 
89     uint8_t solid : 1;           /* Is this a solid stream? */
90     uint8_t service : 1;         /* Is this file a service data? */
91     uint8_t eof : 1;             /* Did we finish unpacking the file? */
92 
93     /* Optional time fields. */
94     uint64_t e_mtime;
95     uint64_t e_ctime;
96     uint64_t e_atime;
97     uint32_t e_unix_ns;
98 
99     /* Optional hash fields. */
100     uint32_t stored_crc32;
101     uint32_t calculated_crc32;
102     uint8_t blake2sp[32];
103     blake2sp_state b2state;
104     char has_blake2;
105 };
106 
107 enum FILTER_TYPE {
108     FILTER_DELTA = 0,   /* Generic pattern. */
109     FILTER_E8    = 1,   /* Intel x86 code. */
110     FILTER_E8E9  = 2,   /* Intel x86 code. */
111     FILTER_ARM   = 3,   /* ARM code. */
112     FILTER_AUDIO = 4,   /* Audio filter, not used in RARv5. */
113     FILTER_RGB   = 5,   /* Color palette, not used in RARv5. */
114     FILTER_ITANIUM = 6, /* Intel's Itanium, not used in RARv5. */
115     FILTER_PPM   = 7,   /* Predictive pattern matching, not used in RARv5. */
116     FILTER_NONE  = 8,
117 };
118 
119 struct filter_info {
120     int type;
121     int channels;
122     int pos_r;
123 
124     int64_t block_start;
125     ssize_t block_length;
126     uint16_t width;
127 };
128 
129 struct data_ready {
130     char used;
131     const uint8_t* buf;
132     size_t size;
133     int64_t offset;
134 };
135 
136 struct cdeque {
137     uint16_t beg_pos;
138     uint16_t end_pos;
139     uint16_t cap_mask;
140     uint16_t size;
141     size_t* arr;
142 };
143 
144 struct decode_table {
145     uint32_t size;
146     int32_t decode_len[16];
147     uint32_t decode_pos[16];
148     uint32_t quick_bits;
149     uint8_t quick_len[1 << 10];
150     uint16_t quick_num[1 << 10];
151     uint16_t decode_num[306];
152 };
153 
154 struct comp_state {
155     /* Flag used to specify if unpacker needs to reinitialize the uncompression
156      * context. */
157     uint8_t initialized : 1;
158 
159     /* Flag used when applying filters. */
160     uint8_t all_filters_applied : 1;
161 
162     /* Flag used to skip file context reinitialization, used when unpacker is
163      * skipping through different multivolume archives. */
164     uint8_t switch_multivolume : 1;
165 
166     /* Flag used to specify if unpacker has processed the whole data block or
167      * just a part of it. */
168     uint8_t block_parsing_finished : 1;
169 
170     int notused : 4;
171 
172     int flags;                   /* Uncompression flags. */
173     int method;                  /* Uncompression algorithm method. */
174     int version;                 /* Uncompression algorithm version. */
175     ssize_t window_size;         /* Size of window_buf. */
176     uint8_t* window_buf;         /* Circular buffer used during
177                                     decompression. */
178     uint8_t* filtered_buf;       /* Buffer used when applying filters. */
179     const uint8_t* block_buf;    /* Buffer used when merging blocks. */
180     size_t window_mask;          /* Convenience field; window_size - 1. */
181     int64_t write_ptr;           /* This amount of data has been unpacked in
182                                     the window buffer. */
183     int64_t last_write_ptr;      /* This amount of data has been stored in
184                                     the output file. */
185     int64_t last_unstore_ptr;    /* Counter of bytes extracted during
186                                     unstoring. This is separate from
187                                     last_write_ptr because of how SERVICE
188                                     base blocks are handled during skipping
189                                     in solid multiarchive archives. */
190     int64_t solid_offset;        /* Additional offset inside the window
191                                     buffer, used in unpacking solid
192                                     archives. */
193     ssize_t cur_block_size;      /* Size of current data block. */
194     int last_len;                /* Flag used in lzss decompression. */
195 
196     /* Decode tables used during lzss uncompression. */
197 
198 #define HUFF_BC 20
199     struct decode_table bd;      /* huffman bit lengths */
200 #define HUFF_NC 306
201     struct decode_table ld;      /* literals */
202 #define HUFF_DC 64
203     struct decode_table dd;      /* distances */
204 #define HUFF_LDC 16
205     struct decode_table ldd;     /* lower bits of distances */
206 #define HUFF_RC 44
207     struct decode_table rd;      /* repeating distances */
208 #define HUFF_TABLE_SIZE (HUFF_NC + HUFF_DC + HUFF_RC + HUFF_LDC)
209 
210     /* Circular deque for storing filters. */
211     struct cdeque filters;
212     int64_t last_block_start;    /* Used for sanity checking. */
213     ssize_t last_block_length;   /* Used for sanity checking. */
214 
215     /* Distance cache used during lzss uncompression. */
216     int dist_cache[4];
217 
218     /* Data buffer stack. */
219     struct data_ready dready[2];
220 };
221 
222 /* Bit reader state. */
223 struct bit_reader {
224     int8_t bit_addr;    /* Current bit pointer inside current byte. */
225     int in_addr;        /* Current byte pointer. */
226 };
227 
228 /* RARv5 block header structure. */
229 struct compressed_block_header {
230     union {
231         struct {
232             uint8_t bit_size : 3;
233             uint8_t byte_count : 3;
234             uint8_t is_last_block : 1;
235             uint8_t is_table_present : 1;
236         } block_flags;
237         uint8_t block_flags_u8;
238     };
239 
240     uint8_t block_cksum;
241 };
242 
243 /* RARv5 main header structure. */
244 struct main_header {
245     /* Does the archive contain solid streams? */
246     uint8_t solid : 1;
247 
248     /* If this a multi-file archive? */
249     uint8_t volume : 1;
250     uint8_t endarc : 1;
251     uint8_t notused : 5;
252 
253     int vol_no;
254 };
255 
256 struct generic_header {
257     uint8_t split_after : 1;
258     uint8_t split_before : 1;
259     uint8_t padding : 6;
260     int size;
261     int last_header_id;
262 };
263 
264 struct multivolume {
265     int expected_vol_no;
266     uint8_t* push_buf;
267 };
268 
269 /* Main context structure. */
270 struct rar5 {
271     int header_initialized;
272 
273     /* Set to 1 if current file is positioned AFTER the magic value
274      * of the archive file. This is used in header reading functions. */
275     int skipped_magic;
276 
277     /* Set to not zero if we're in skip mode (either by calling rar5_data_skip
278      * function or when skipping over solid streams). Set to 0 when in
279      * extraction mode. This is used during checksum calculation functions. */
280     int skip_mode;
281 
282     /* An offset to QuickOpen list. This is not supported by this unpacker,
283      * because we're focusing on streaming interface. QuickOpen is designed
284      * to make things quicker for non-stream interfaces, so it's not our
285      * use case. */
286     uint64_t qlist_offset;
287 
288     /* An offset to additional Recovery data. This is not supported by this
289      * unpacker. Recovery data are additional Reed-Solomon codes that could
290      * be used to calculate bytes that are missing in archive or are
291      * corrupted. */
292     uint64_t rr_offset;
293 
294     /* Various context variables grouped to different structures. */
295     struct generic_header generic;
296     struct main_header main;
297     struct comp_state cstate;
298     struct file_header file;
299     struct bit_reader bits;
300     struct multivolume vol;
301 
302     /* The header of currently processed RARv5 block. Used in main
303      * decompression logic loop. */
304     struct compressed_block_header last_block_hdr;
305 };
306 
307 /* Forward function declarations. */
308 
309 static int verify_global_checksums(struct archive_read* a);
310 static int rar5_read_data_skip(struct archive_read *a);
311 static int push_data_ready(struct archive_read* a, struct rar5* rar,
312         const uint8_t* buf, size_t size, int64_t offset);
313 
314 /* CDE_xxx = Circular Double Ended (Queue) return values. */
315 enum CDE_RETURN_VALUES {
316     CDE_OK, CDE_ALLOC, CDE_PARAM, CDE_OUT_OF_BOUNDS,
317 };
318 
319 /* Clears the contents of this circular deque. */
320 static void cdeque_clear(struct cdeque* d) {
321     d->size = 0;
322     d->beg_pos = 0;
323     d->end_pos = 0;
324 }
325 
326 /* Creates a new circular deque object. Capacity must be power of 2: 8, 16, 32,
327  * 64, 256, etc. When the user will add another item above current capacity,
328  * the circular deque will overwrite the oldest entry. */
329 static int cdeque_init(struct cdeque* d, int max_capacity_power_of_2) {
330     if(d == NULL || max_capacity_power_of_2 == 0)
331         return CDE_PARAM;
332 
333     d->cap_mask = max_capacity_power_of_2 - 1;
334     d->arr = NULL;
335 
336     if((max_capacity_power_of_2 & d->cap_mask) > 0)
337         return CDE_PARAM;
338 
339     cdeque_clear(d);
340     d->arr = malloc(sizeof(void*) * max_capacity_power_of_2);
341 
342     return d->arr ? CDE_OK : CDE_ALLOC;
343 }
344 
345 /* Return the current size (not capacity) of circular deque `d`. */
346 static size_t cdeque_size(struct cdeque* d) {
347     return d->size;
348 }
349 
350 /* Returns the first element of current circular deque. Note that this function
351  * doesn't perform any bounds checking. If you need bounds checking, use
352  * `cdeque_front()` function instead. */
353 static void cdeque_front_fast(struct cdeque* d, void** value) {
354     *value = (void*) d->arr[d->beg_pos];
355 }
356 
357 /* Returns the first element of current circular deque. This function
358  * performs bounds checking. */
359 static int cdeque_front(struct cdeque* d, void** value) {
360     if(d->size > 0) {
361         cdeque_front_fast(d, value);
362         return CDE_OK;
363     } else
364         return CDE_OUT_OF_BOUNDS;
365 }
366 
367 /* Pushes a new element into the end of this circular deque object. If current
368  * size will exceed capacity, the oldest element will be overwritten. */
369 static int cdeque_push_back(struct cdeque* d, void* item) {
370     if(d == NULL)
371         return CDE_PARAM;
372 
373     if(d->size == d->cap_mask + 1)
374         return CDE_OUT_OF_BOUNDS;
375 
376     d->arr[d->end_pos] = (size_t) item;
377     d->end_pos = (d->end_pos + 1) & d->cap_mask;
378     d->size++;
379 
380     return CDE_OK;
381 }
382 
383 /* Pops a front element of this circular deque object and returns its value.
384  * This function doesn't perform any bounds checking. */
385 static void cdeque_pop_front_fast(struct cdeque* d, void** value) {
386     *value = (void*) d->arr[d->beg_pos];
387     d->beg_pos = (d->beg_pos + 1) & d->cap_mask;
388     d->size--;
389 }
390 
391 /* Pops a front element of this circular deque object and returns its value.
392  * This function performs bounds checking. */
393 static int cdeque_pop_front(struct cdeque* d, void** value) {
394     if(!d || !value)
395         return CDE_PARAM;
396 
397     if(d->size == 0)
398         return CDE_OUT_OF_BOUNDS;
399 
400     cdeque_pop_front_fast(d, value);
401     return CDE_OK;
402 }
403 
404 /* Convenience function to cast filter_info** to void **. */
405 static void** cdeque_filter_p(struct filter_info** f) {
406     return (void**) (size_t) f;
407 }
408 
409 /* Convenience function to cast filter_info* to void *. */
410 static void* cdeque_filter(struct filter_info* f) {
411     return (void**) (size_t) f;
412 }
413 
414 /* Destroys this circular deque object. Deallocates the memory of the collection
415  * buffer, but doesn't deallocate the memory of any pointer passed to this
416  * deque as a value. */
417 static void cdeque_free(struct cdeque* d) {
418     if(!d)
419         return;
420 
421     if(!d->arr)
422         return;
423 
424     free(d->arr);
425 
426     d->arr = NULL;
427     d->beg_pos = -1;
428     d->end_pos = -1;
429     d->cap_mask = 0;
430 }
431 
432 static inline struct rar5* get_context(struct archive_read* a) {
433     return (struct rar5*) a->format->data;
434 }
435 
436 // TODO: make sure these functions return a little endian number
437 
438 /* Convenience functions used by filter implementations. */
439 
440 static uint32_t read_filter_data(struct rar5* rar, uint32_t offset) {
441     uint32_t* dptr = (uint32_t*) &rar->cstate.window_buf[offset];
442     // TODO: bswap if big endian
443     return *dptr;
444 }
445 
446 static void write_filter_data(struct rar5* rar, uint32_t offset,
447         uint32_t value)
448 {
449     uint32_t* dptr = (uint32_t*) &rar->cstate.filtered_buf[offset];
450     // TODO: bswap if big endian
451     *dptr = value;
452 }
453 
454 static void circular_memcpy(uint8_t* dst, uint8_t* window, const int mask,
455         int64_t start, int64_t end)
456 {
457     if((start & mask) > (end & mask)) {
458         ssize_t len1 = mask + 1 - (start & mask);
459         ssize_t len2 = end & mask;
460 
461         memcpy(dst, &window[start & mask], len1);
462         memcpy(dst + len1, window, len2);
463     } else {
464         memcpy(dst, &window[start & mask], (size_t) (end - start));
465     }
466 }
467 
468 /* Allocates a new filter descriptor and adds it to the filter array. */
469 static struct filter_info* add_new_filter(struct rar5* rar) {
470     struct filter_info* f =
471         (struct filter_info*) calloc(1, sizeof(struct filter_info));
472 
473     if(!f) {
474         return NULL;
475     }
476 
477     cdeque_push_back(&rar->cstate.filters, cdeque_filter(f));
478     return f;
479 }
480 
481 static int run_delta_filter(struct rar5* rar, struct filter_info* flt) {
482     int i;
483     ssize_t dest_pos, src_pos = 0;
484 
485     for(i = 0; i < flt->channels; i++) {
486         uint8_t prev_byte = 0;
487         for(dest_pos = i;
488                 dest_pos < flt->block_length;
489                 dest_pos += flt->channels)
490         {
491             uint8_t byte;
492 
493             byte = rar->cstate.window_buf[(rar->cstate.solid_offset +
494                     flt->block_start + src_pos) & rar->cstate.window_mask];
495 
496             prev_byte -= byte;
497             rar->cstate.filtered_buf[dest_pos] = prev_byte;
498             src_pos++;
499         }
500     }
501 
502     return ARCHIVE_OK;
503 }
504 
505 static int run_e8e9_filter(struct rar5* rar, struct filter_info* flt,
506         int extended)
507 {
508     const uint32_t file_size = 0x1000000;
509     ssize_t i;
510 
511     circular_memcpy(rar->cstate.filtered_buf,
512         rar->cstate.window_buf,
513         rar->cstate.window_mask,
514         rar->cstate.solid_offset + flt->block_start,
515         rar->cstate.solid_offset + flt->block_start + flt->block_length);
516 
517     for(i = 0; i < flt->block_length - 4;) {
518         uint8_t b = rar->cstate.window_buf[(rar->cstate.solid_offset +
519                 flt->block_start + i++) & rar->cstate.window_mask];
520 
521         /* 0xE8 = x86's call <relative_addr_uint32> (function call)
522          * 0xE9 = x86's jmp <relative_addr_uint32> (unconditional jump) */
523         if(b == 0xE8 || (extended && b == 0xE9)) {
524 
525             uint32_t addr;
526             uint32_t offset = (i + flt->block_start) % file_size;
527 
528             addr = read_filter_data(rar, (rar->cstate.solid_offset +
529                         flt->block_start + i) & rar->cstate.window_mask);
530 
531             if(addr & 0x80000000) {
532                 if(((addr + offset) & 0x80000000) == 0) {
533                     write_filter_data(rar, i, addr + file_size);
534                 }
535             } else {
536                 if((addr - file_size) & 0x80000000) {
537                     uint32_t naddr = addr - offset;
538                     write_filter_data(rar, i, naddr);
539                 }
540             }
541 
542             i += 4;
543         }
544     }
545 
546     return ARCHIVE_OK;
547 }
548 
549 static int run_arm_filter(struct rar5* rar, struct filter_info* flt) {
550     ssize_t i = 0;
551     uint32_t offset;
552     const int mask = rar->cstate.window_mask;
553 
554     circular_memcpy(rar->cstate.filtered_buf,
555         rar->cstate.window_buf,
556         rar->cstate.window_mask,
557         rar->cstate.solid_offset + flt->block_start,
558         rar->cstate.solid_offset + flt->block_start + flt->block_length);
559 
560     for(i = 0; i < flt->block_length - 3; i += 4) {
561         uint8_t* b = &rar->cstate.window_buf[(rar->cstate.solid_offset +
562                 flt->block_start + i) & mask];
563 
564         if(b[3] == 0xEB) {
565             /* 0xEB = ARM's BL (branch + link) instruction. */
566             offset = read_filter_data(rar, (rar->cstate.solid_offset +
567                         flt->block_start + i) & mask) & 0x00ffffff;
568 
569             offset -= (uint32_t) ((i + flt->block_start) / 4);
570             offset = (offset & 0x00ffffff) | 0xeb000000;
571             write_filter_data(rar, i, offset);
572         }
573     }
574 
575     return ARCHIVE_OK;
576 }
577 
578 static int run_filter(struct archive_read* a, struct filter_info* flt) {
579     int ret;
580     struct rar5* rar = get_context(a);
581 
582     if(rar->cstate.filtered_buf)
583         free(rar->cstate.filtered_buf);
584 
585     rar->cstate.filtered_buf = malloc(flt->block_length);
586     if(!rar->cstate.filtered_buf) {
587         archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for "
588                 "filter data.");
589         return ARCHIVE_FATAL;
590     }
591 
592     switch(flt->type) {
593         case FILTER_DELTA:
594             ret = run_delta_filter(rar, flt);
595             break;
596 
597         case FILTER_E8:
598             /* fallthrough */
599         case FILTER_E8E9:
600             ret = run_e8e9_filter(rar, flt, flt->type == FILTER_E8E9);
601             break;
602 
603         case FILTER_ARM:
604             ret = run_arm_filter(rar, flt);
605             break;
606 
607         default:
608             archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
609                     "Unsupported filter type: 0x%02x", flt->type);
610             return ARCHIVE_FATAL;
611     }
612 
613     if(ret != ARCHIVE_OK) {
614         /* Filter has failed. */
615         return ret;
616     }
617 
618     if(ARCHIVE_OK != push_data_ready(a, rar, rar->cstate.filtered_buf,
619                 flt->block_length, rar->cstate.last_write_ptr))
620     {
621         archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
622                 "Stack overflow when submitting unpacked data");
623 
624         return ARCHIVE_FATAL;
625     }
626 
627     rar->cstate.last_write_ptr += flt->block_length;
628     return ARCHIVE_OK;
629 }
630 
631 /* The `push_data` function submits the selected data range to the user.
632  * Next call of `use_data` will use the pointer, size and offset arguments
633  * that are specified here. These arguments are pushed to the FIFO stack here,
634  * and popped from the stack by the `use_data` function. */
635 static void push_data(struct archive_read* a, struct rar5* rar,
636         const uint8_t* buf, int64_t idx_begin, int64_t idx_end)
637 {
638     const int wmask = rar->cstate.window_mask;
639     const ssize_t solid_write_ptr = (rar->cstate.solid_offset +
640         rar->cstate.last_write_ptr) & wmask;
641 
642     idx_begin += rar->cstate.solid_offset;
643     idx_end += rar->cstate.solid_offset;
644 
645     /* Check if our unpacked data is wrapped inside the window circular buffer.
646      * If it's not wrapped, it can be copied out by using a single memcpy,
647      * but when it's wrapped, we need to copy the first part with one
648      * memcpy, and the second part with another memcpy. */
649 
650     if((idx_begin & wmask) > (idx_end & wmask)) {
651         /* The data is wrapped (begin offset sis bigger than end offset). */
652         const ssize_t frag1_size = rar->cstate.window_size - (idx_begin & wmask);
653         const ssize_t frag2_size = idx_end & wmask;
654 
655         /* Copy the first part of the buffer first. */
656         push_data_ready(a, rar, buf + solid_write_ptr, frag1_size,
657             rar->cstate.last_write_ptr);
658 
659         /* Copy the second part of the buffer. */
660         push_data_ready(a, rar, buf, frag2_size,
661             rar->cstate.last_write_ptr + frag1_size);
662 
663         rar->cstate.last_write_ptr += frag1_size + frag2_size;
664     } else {
665         /* Data is not wrapped, so we can just use one call to copy the
666          * data. */
667         push_data_ready(a, rar,
668             buf + solid_write_ptr,
669             (idx_end - idx_begin) & wmask,
670             rar->cstate.last_write_ptr);
671 
672         rar->cstate.last_write_ptr += idx_end - idx_begin;
673     }
674 }
675 
676 /* Convenience function that submits the data to the user. It uses the
677  * unpack window buffer as a source location. */
678 static void push_window_data(struct archive_read* a, struct rar5* rar,
679         int64_t idx_begin, int64_t idx_end)
680 {
681     push_data(a, rar, rar->cstate.window_buf, idx_begin, idx_end);
682 }
683 
684 static int apply_filters(struct archive_read* a) {
685     struct filter_info* flt;
686     struct rar5* rar = get_context(a);
687     int ret;
688 
689     rar->cstate.all_filters_applied = 0;
690 
691     /* Get the first filter that can be applied to our data. The data needs to
692      * be fully unpacked before the filter can be run. */
693     if(CDE_OK ==
694             cdeque_front(&rar->cstate.filters, cdeque_filter_p(&flt)))
695     {
696         /* Check if our unpacked data fully covers this filter's range. */
697         if(rar->cstate.write_ptr > flt->block_start &&
698                 rar->cstate.write_ptr >= flt->block_start + flt->block_length)
699         {
700             /* Check if we have some data pending to be written right before
701              * the filter's start offset. */
702             if(rar->cstate.last_write_ptr == flt->block_start) {
703                 /* Run the filter specified by descriptor `flt`. */
704                 ret = run_filter(a, flt);
705                 if(ret != ARCHIVE_OK) {
706                     /* Filter failure, return error. */
707                     return ret;
708                 }
709 
710                 /* Filter descriptor won't be needed anymore after it's used,
711                  * so remove it from the filter list and free its memory. */
712                 (void) cdeque_pop_front(&rar->cstate.filters,
713                         cdeque_filter_p(&flt));
714 
715                 free(flt);
716             } else {
717                 /* We can't run filters yet, dump the memory right before the
718                  * filter. */
719                 push_window_data(a, rar, rar->cstate.last_write_ptr,
720                         flt->block_start);
721             }
722 
723             /* Return 'filter applied or not needed' state to the caller. */
724             return ARCHIVE_RETRY;
725         }
726     }
727 
728     rar->cstate.all_filters_applied = 1;
729     return ARCHIVE_OK;
730 }
731 
732 static void dist_cache_push(struct rar5* rar, int value) {
733     int* q = rar->cstate.dist_cache;
734 
735     q[3] = q[2];
736     q[2] = q[1];
737     q[1] = q[0];
738     q[0] = value;
739 }
740 
741 static int dist_cache_touch(struct rar5* rar, int idx) {
742     int* q = rar->cstate.dist_cache;
743     int i, dist = q[idx];
744 
745     for(i = idx; i > 0; i--)
746         q[i] = q[i - 1];
747 
748     q[0] = dist;
749     return dist;
750 }
751 
752 static void free_filters(struct rar5* rar) {
753     struct cdeque* d = &rar->cstate.filters;
754 
755     /* Free any remaining filters. All filters should be naturally consumed by
756      * the unpacking function, so remaining filters after unpacking normally
757      * mean that unpacking wasn't successful. But still of course we shouldn't
758      * leak memory in such case. */
759 
760     /* cdeque_size() is a fast operation, so we can use it as a loop
761      * expression. */
762     while(cdeque_size(d) > 0) {
763         struct filter_info* f = NULL;
764 
765         /* Pop_front will also decrease the collection's size. */
766         if(CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)) && f != NULL)
767             free(f);
768     }
769 
770     cdeque_clear(d);
771 
772     /* Also clear out the variables needed for sanity checking. */
773     rar->cstate.last_block_start = 0;
774     rar->cstate.last_block_length = 0;
775 }
776 
777 static void reset_file_context(struct rar5* rar) {
778     memset(&rar->file, 0, sizeof(rar->file));
779     blake2sp_init(&rar->file.b2state, 32);
780 
781     if(rar->main.solid) {
782         rar->cstate.solid_offset += rar->cstate.write_ptr;
783     } else {
784         rar->cstate.solid_offset = 0;
785     }
786 
787     rar->cstate.write_ptr = 0;
788     rar->cstate.last_write_ptr = 0;
789     rar->cstate.last_unstore_ptr = 0;
790 
791     free_filters(rar);
792 }
793 
794 static inline int get_archive_read(struct archive* a,
795         struct archive_read** ar)
796 {
797     *ar = (struct archive_read*) a;
798     archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
799                         "archive_read_support_format_rar5");
800 
801     return ARCHIVE_OK;
802 }
803 
804 static int read_ahead(struct archive_read* a, size_t how_many,
805         const uint8_t** ptr)
806 {
807     if(!ptr)
808         return 0;
809 
810     ssize_t avail = -1;
811     *ptr = __archive_read_ahead(a, how_many, &avail);
812 
813     if(*ptr == NULL) {
814         return 0;
815     }
816 
817     return 1;
818 }
819 
820 static int consume(struct archive_read* a, int64_t how_many) {
821     int ret;
822 
823     ret =
824         how_many == __archive_read_consume(a, how_many)
825         ? ARCHIVE_OK
826         : ARCHIVE_FATAL;
827 
828     return ret;
829 }
830 
831 /**
832  * Read a RAR5 variable sized numeric value. This value will be stored in
833  * `pvalue`. The `pvalue_len` argument points to a variable that will receive
834  * the byte count that was consumed in order to decode the `pvalue` value, plus
835  * one.
836  *
837  * pvalue_len is optional and can be NULL.
838  *
839  * NOTE: if `pvalue_len` is NOT NULL, the caller needs to manually consume
840  * the number of bytes that `pvalue_len` value contains. If the `pvalue_len`
841  * is NULL, this consuming operation is done automatically.
842  *
843  * Returns 1 if *pvalue was successfully read.
844  * Returns 0 if there was an error. In this case, *pvalue contains an
845  *           invalid value.
846  */
847 
848 static int read_var(struct archive_read* a, uint64_t* pvalue,
849         uint64_t* pvalue_len)
850 {
851     uint64_t result = 0;
852     size_t shift, i;
853     const uint8_t* p;
854     uint8_t b;
855 
856     /* We will read maximum of 8 bytes. We don't have to handle the situation
857      * to read the RAR5 variable-sized value stored at the end of the file,
858      * because such situation will never happen. */
859     if(!read_ahead(a, 8, &p))
860         return 0;
861 
862     for(shift = 0, i = 0; i < 8; i++, shift += 7) {
863         b = p[i];
864 
865         /* Strip the MSB from the input byte and add the resulting number
866          * to the `result`. */
867         result += (b & 0x7F) << shift;
868 
869         /* MSB set to 1 means we need to continue decoding process. MSB set
870          * to 0 means we're done.
871          *
872          * This conditional checks for the second case. */
873         if((b & 0x80) == 0) {
874             if(pvalue) {
875                 *pvalue = result;
876             }
877 
878             /* If the caller has passed the `pvalue_len` pointer, store the
879              * number of consumed bytes in it and do NOT consume those bytes,
880              * since the caller has all the information it needs to perform
881              * the consuming process itself. */
882             if(pvalue_len) {
883                 *pvalue_len = 1 + i;
884             } else {
885                 /* If the caller did not provide the `pvalue_len` pointer,
886                  * it will not have the possibility to advance the file
887                  * pointer, because it will not know how many bytes it needs
888                  * to consume. This is why we handle such situation here
889                  * automatically. */
890                 if(ARCHIVE_OK != consume(a, 1 + i)) {
891                     return 0;
892                 }
893             }
894 
895             /* End of decoding process, return success. */
896             return 1;
897         }
898     }
899 
900     /* The decoded value takes the maximum number of 8 bytes. It's a maximum
901      * number of bytes, so end decoding process here even if the first bit
902      * of last byte is 1. */
903     if(pvalue) {
904         *pvalue = result;
905     }
906 
907     if(pvalue_len) {
908         *pvalue_len = 9;
909     } else {
910         if(ARCHIVE_OK != consume(a, 9)) {
911             return 0;
912         }
913     }
914 
915     return 1;
916 }
917 
918 static int read_var_sized(struct archive_read* a, size_t* pvalue,
919         size_t* pvalue_len)
920 {
921     uint64_t v;
922     uint64_t v_size = 0;
923 
924     const int ret = pvalue_len
925                     ? read_var(a, &v, &v_size)
926                     : read_var(a, &v, NULL);
927 
928     if(ret == 1 && pvalue) {
929         *pvalue = (size_t) v;
930     }
931 
932     if(pvalue_len) {
933         /* Possible data truncation should be safe. */
934         *pvalue_len = (size_t) v_size;
935     }
936 
937     return ret;
938 }
939 
940 static int read_bits_32(struct rar5* rar, const uint8_t* p, uint32_t* value) {
941     uint32_t bits = p[rar->bits.in_addr] << 24;
942     bits |= p[rar->bits.in_addr + 1] << 16;
943     bits |= p[rar->bits.in_addr + 2] << 8;
944     bits |= p[rar->bits.in_addr + 3];
945     bits <<= rar->bits.bit_addr;
946     bits |= p[rar->bits.in_addr + 4] >> (8 - rar->bits.bit_addr);
947     *value = bits;
948     return ARCHIVE_OK;
949 }
950 
951 static int read_bits_16(struct rar5* rar, const uint8_t* p, uint16_t* value) {
952     int bits = (int) p[rar->bits.in_addr] << 16;
953     bits |= (int) p[rar->bits.in_addr + 1] << 8;
954     bits |= (int) p[rar->bits.in_addr + 2];
955     bits >>= (8 - rar->bits.bit_addr);
956     *value = bits & 0xffff;
957     return ARCHIVE_OK;
958 }
959 
960 static void skip_bits(struct rar5* rar, int bits) {
961     const int new_bits = rar->bits.bit_addr + bits;
962     rar->bits.in_addr += new_bits >> 3;
963     rar->bits.bit_addr = new_bits & 7;
964 }
965 
966 /* n = up to 16 */
967 static int read_consume_bits(struct rar5* rar, const uint8_t* p, int n,
968         int* value)
969 {
970     uint16_t v;
971     int ret, num;
972 
973     if(n == 0 || n > 16) {
974         /* This is a programmer error and should never happen in runtime. */
975         return ARCHIVE_FATAL;
976     }
977 
978     ret = read_bits_16(rar, p, &v);
979     if(ret != ARCHIVE_OK)
980         return ret;
981 
982     num = (int) v;
983     num >>= 16 - n;
984 
985     skip_bits(rar, n);
986 
987     if(value)
988         *value = num;
989 
990     return ARCHIVE_OK;
991 }
992 
993 static int read_u32(struct archive_read* a, uint32_t* pvalue) {
994     const uint8_t* p;
995     if(!read_ahead(a, 4, &p))
996         return 0;
997 
998     *pvalue = *(const uint32_t*)p;
999 
1000     return ARCHIVE_OK == consume(a, 4) ? 1 : 0;
1001 }
1002 
1003 static int read_u64(struct archive_read* a, uint64_t* pvalue) {
1004     const uint8_t* p;
1005     if(!read_ahead(a, 8, &p))
1006         return 0;
1007 
1008     *pvalue = *(const uint64_t*)p;
1009 
1010     return ARCHIVE_OK == consume(a, 8) ? 1 : 0;
1011 }
1012 
1013 static int bid_standard(struct archive_read* a) {
1014     const uint8_t* p;
1015 
1016     if(!read_ahead(a, rar5_signature_size, &p))
1017         return -1;
1018 
1019     if(!memcmp(rar5_signature, p, rar5_signature_size))
1020         return 30;
1021 
1022     return -1;
1023 }
1024 
1025 static int rar5_bid(struct archive_read* a, int best_bid) {
1026     int my_bid;
1027 
1028     if(best_bid > 30)
1029         return -1;
1030 
1031     my_bid = bid_standard(a);
1032     if(my_bid > -1) {
1033         return my_bid;
1034     }
1035 
1036     return -1;
1037 }
1038 
1039 static int rar5_options(struct archive_read *a, const char *key, const char *val) {
1040     (void) a;
1041     (void) key;
1042     (void) val;
1043 
1044     /* No options supported in this version. Return the ARCHIVE_WARN code to
1045      * signal the options supervisor that the unpacker didn't handle setting
1046      * this option. */
1047 
1048     return ARCHIVE_WARN;
1049 }
1050 
1051 static void init_header(struct archive_read* a) {
1052     a->archive.archive_format = ARCHIVE_FORMAT_RAR_V5;
1053     a->archive.archive_format_name = "RAR5";
1054 }
1055 
1056 enum HEADER_FLAGS {
1057     HFL_EXTRA_DATA = 0x0001, HFL_DATA = 0x0002, HFL_SKIP_IF_UNKNOWN = 0x0004,
1058     HFL_SPLIT_BEFORE = 0x0008, HFL_SPLIT_AFTER = 0x0010, HFL_CHILD = 0x0020,
1059     HFL_INHERITED = 0x0040
1060 };
1061 
1062 static int process_main_locator_extra_block(struct archive_read* a,
1063         struct rar5* rar)
1064 {
1065     uint64_t locator_flags;
1066 
1067     if(!read_var(a, &locator_flags, NULL)) {
1068         return ARCHIVE_EOF;
1069     }
1070 
1071     enum LOCATOR_FLAGS {
1072         QLIST = 0x01, RECOVERY = 0x02,
1073     };
1074 
1075     if(locator_flags & QLIST) {
1076         if(!read_var(a, &rar->qlist_offset, NULL)) {
1077             return ARCHIVE_EOF;
1078         }
1079 
1080         /* qlist is not used */
1081     }
1082 
1083     if(locator_flags & RECOVERY) {
1084         if(!read_var(a, &rar->rr_offset, NULL)) {
1085             return ARCHIVE_EOF;
1086         }
1087 
1088         /* rr is not used */
1089     }
1090 
1091     return ARCHIVE_OK;
1092 }
1093 
1094 static int parse_file_extra_hash(struct archive_read* a, struct rar5* rar,
1095         ssize_t* extra_data_size)
1096 {
1097     size_t hash_type;
1098     size_t value_len;
1099 
1100     if(!read_var_sized(a, &hash_type, &value_len))
1101         return ARCHIVE_EOF;
1102 
1103     *extra_data_size -= value_len;
1104     if(ARCHIVE_OK != consume(a, value_len)) {
1105         return ARCHIVE_EOF;
1106     }
1107 
1108     enum HASH_TYPE {
1109         BLAKE2sp = 0x00
1110     };
1111 
1112     /* The file uses BLAKE2sp checksum algorithm instead of plain old
1113      * CRC32. */
1114     if(hash_type == BLAKE2sp) {
1115         const uint8_t* p;
1116         const int hash_size = sizeof(rar->file.blake2sp);
1117 
1118         if(!read_ahead(a, hash_size, &p))
1119             return ARCHIVE_EOF;
1120 
1121         rar->file.has_blake2 = 1;
1122         memcpy(&rar->file.blake2sp, p, hash_size);
1123 
1124         if(ARCHIVE_OK != consume(a, hash_size)) {
1125             return ARCHIVE_EOF;
1126         }
1127 
1128         *extra_data_size -= hash_size;
1129     } else {
1130         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1131                 "Unsupported hash type (0x%02x)", (int) hash_type);
1132         return ARCHIVE_FATAL;
1133     }
1134 
1135     return ARCHIVE_OK;
1136 }
1137 
1138 static uint64_t time_win_to_unix(uint64_t win_time) {
1139     const size_t ns_in_sec = 10000000;
1140     const uint64_t sec_to_unix = 11644473600LL;
1141     return win_time / ns_in_sec - sec_to_unix;
1142 }
1143 
1144 static int parse_htime_item(struct archive_read* a, char unix_time,
1145         uint64_t* where, ssize_t* extra_data_size)
1146 {
1147     if(unix_time) {
1148         uint32_t time_val;
1149         if(!read_u32(a, &time_val))
1150             return ARCHIVE_EOF;
1151 
1152         *extra_data_size -= 4;
1153         *where = (uint64_t) time_val;
1154     } else {
1155         uint64_t windows_time;
1156         if(!read_u64(a, &windows_time))
1157             return ARCHIVE_EOF;
1158 
1159         *where = time_win_to_unix(windows_time);
1160         *extra_data_size -= 8;
1161     }
1162 
1163     return ARCHIVE_OK;
1164 }
1165 
1166 static int parse_file_extra_htime(struct archive_read* a,
1167         struct archive_entry* e, struct rar5* rar,
1168         ssize_t* extra_data_size)
1169 {
1170     char unix_time = 0;
1171     size_t flags;
1172     size_t value_len;
1173 
1174     enum HTIME_FLAGS {
1175         IS_UNIX       = 0x01,
1176         HAS_MTIME     = 0x02,
1177         HAS_CTIME     = 0x04,
1178         HAS_ATIME     = 0x08,
1179         HAS_UNIX_NS   = 0x10,
1180     };
1181 
1182     if(!read_var_sized(a, &flags, &value_len))
1183         return ARCHIVE_EOF;
1184 
1185     *extra_data_size -= value_len;
1186     if(ARCHIVE_OK != consume(a, value_len)) {
1187         return ARCHIVE_EOF;
1188     }
1189 
1190     unix_time = flags & IS_UNIX;
1191 
1192     if(flags & HAS_MTIME) {
1193         parse_htime_item(a, unix_time, &rar->file.e_mtime, extra_data_size);
1194         archive_entry_set_mtime(e, rar->file.e_mtime, 0);
1195     }
1196 
1197     if(flags & HAS_CTIME) {
1198         parse_htime_item(a, unix_time, &rar->file.e_ctime, extra_data_size);
1199         archive_entry_set_ctime(e, rar->file.e_ctime, 0);
1200     }
1201 
1202     if(flags & HAS_ATIME) {
1203         parse_htime_item(a, unix_time, &rar->file.e_atime, extra_data_size);
1204         archive_entry_set_atime(e, rar->file.e_atime, 0);
1205     }
1206 
1207     if(flags & HAS_UNIX_NS) {
1208         if(!read_u32(a, &rar->file.e_unix_ns))
1209             return ARCHIVE_EOF;
1210 
1211         *extra_data_size -= 4;
1212     }
1213 
1214     return ARCHIVE_OK;
1215 }
1216 
1217 static int process_head_file_extra(struct archive_read* a,
1218         struct archive_entry* e, struct rar5* rar,
1219         ssize_t extra_data_size)
1220 {
1221     size_t extra_field_size;
1222     size_t extra_field_id = 0;
1223     int ret = ARCHIVE_FATAL;
1224     size_t var_size;
1225 
1226     enum EXTRA {
1227         CRYPT = 0x01, HASH = 0x02, HTIME = 0x03, VERSION_ = 0x04,
1228         REDIR = 0x05, UOWNER = 0x06, SUBDATA = 0x07
1229     };
1230 
1231     while(extra_data_size > 0) {
1232         if(!read_var_sized(a, &extra_field_size, &var_size))
1233             return ARCHIVE_EOF;
1234 
1235         extra_data_size -= var_size;
1236         if(ARCHIVE_OK != consume(a, var_size)) {
1237             return ARCHIVE_EOF;
1238         }
1239 
1240         if(!read_var_sized(a, &extra_field_id, &var_size))
1241             return ARCHIVE_EOF;
1242 
1243         extra_data_size -= var_size;
1244         if(ARCHIVE_OK != consume(a, var_size)) {
1245             return ARCHIVE_EOF;
1246         }
1247 
1248         switch(extra_field_id) {
1249             case HASH:
1250                 ret = parse_file_extra_hash(a, rar, &extra_data_size);
1251                 break;
1252             case HTIME:
1253                 ret = parse_file_extra_htime(a, e, rar, &extra_data_size);
1254                 break;
1255             case CRYPT:
1256                 /* fallthrough */
1257             case VERSION_:
1258                 /* fallthrough */
1259             case REDIR:
1260                 /* fallthrough */
1261             case UOWNER:
1262                 /* fallthrough */
1263             case SUBDATA:
1264                 /* fallthrough */
1265             default:
1266                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1267                         "Unknown extra field in file/service block: 0x%02x",
1268                         (int) extra_field_id);
1269                 return ARCHIVE_FATAL;
1270         }
1271     }
1272 
1273     if(ret != ARCHIVE_OK) {
1274         /* Attribute not implemented. */
1275         return ret;
1276     }
1277 
1278     return ARCHIVE_OK;
1279 }
1280 
1281 static int process_head_file(struct archive_read* a, struct rar5* rar,
1282         struct archive_entry* entry, size_t block_flags)
1283 {
1284     ssize_t extra_data_size = 0;
1285     size_t data_size = 0;
1286     size_t file_flags = 0;
1287     size_t file_attr = 0;
1288     size_t compression_info = 0;
1289     size_t host_os = 0;
1290     size_t name_size = 0;
1291     uint64_t unpacked_size;
1292     uint32_t mtime = 0, crc = 0;
1293     int c_method = 0, c_version = 0, is_dir;
1294     char name_utf8_buf[2048 * 4];
1295     const uint8_t* p;
1296 
1297     memset(entry, 0, sizeof(struct archive_entry));
1298 
1299     /* Do not reset file context if we're switching archives. */
1300     if(!rar->cstate.switch_multivolume) {
1301         reset_file_context(rar);
1302     }
1303 
1304     if(block_flags & HFL_EXTRA_DATA) {
1305         size_t edata_size = 0;
1306         if(!read_var_sized(a, &edata_size, NULL))
1307             return ARCHIVE_EOF;
1308 
1309         /* Intentional type cast from unsigned to signed. */
1310         extra_data_size = (ssize_t) edata_size;
1311     }
1312 
1313     if(block_flags & HFL_DATA) {
1314         if(!read_var_sized(a, &data_size, NULL))
1315             return ARCHIVE_EOF;
1316 
1317         rar->file.bytes_remaining = data_size;
1318     } else {
1319         rar->file.bytes_remaining = 0;
1320 
1321         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1322                 "no data found in file/service block");
1323         return ARCHIVE_FATAL;
1324     }
1325 
1326     enum FILE_FLAGS {
1327         DIRECTORY = 0x0001, UTIME = 0x0002, CRC32 = 0x0004,
1328         UNKNOWN_UNPACKED_SIZE = 0x0008,
1329     };
1330 
1331     enum COMP_INFO_FLAGS {
1332         SOLID = 0x0040,
1333     };
1334 
1335     if(!read_var_sized(a, &file_flags, NULL))
1336         return ARCHIVE_EOF;
1337 
1338     if(!read_var(a, &unpacked_size, NULL))
1339         return ARCHIVE_EOF;
1340 
1341     if(file_flags & UNKNOWN_UNPACKED_SIZE) {
1342         archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1343                 "Files with unknown unpacked size are not supported");
1344         return ARCHIVE_FATAL;
1345     }
1346 
1347     is_dir = (int) (file_flags & DIRECTORY);
1348 
1349     if(!read_var_sized(a, &file_attr, NULL))
1350         return ARCHIVE_EOF;
1351 
1352     if(file_flags & UTIME) {
1353         if(!read_u32(a, &mtime))
1354             return ARCHIVE_EOF;
1355     }
1356 
1357     if(file_flags & CRC32) {
1358         if(!read_u32(a, &crc))
1359             return ARCHIVE_EOF;
1360     }
1361 
1362     if(!read_var_sized(a, &compression_info, NULL))
1363         return ARCHIVE_EOF;
1364 
1365     c_method = (int) (compression_info >> 7) & 0x7;
1366     c_version = (int) (compression_info & 0x3f);
1367 
1368     rar->cstate.window_size = is_dir ?
1369         0 :
1370         g_unpack_window_size << ((compression_info >> 10) & 15);
1371     rar->cstate.method = c_method;
1372     rar->cstate.version = c_version + 50;
1373 
1374     rar->file.solid = (compression_info & SOLID) > 0;
1375     rar->file.service = 0;
1376 
1377     if(!read_var_sized(a, &host_os, NULL))
1378         return ARCHIVE_EOF;
1379 
1380     enum HOST_OS {
1381         HOST_WINDOWS = 0,
1382         HOST_UNIX = 1,
1383     };
1384 
1385     if(host_os == HOST_WINDOWS) {
1386         /* Host OS is Windows */
1387 
1388         unsigned short mode = 0660;
1389 
1390         if(is_dir)
1391             mode |= AE_IFDIR;
1392         else
1393             mode |= AE_IFREG;
1394 
1395         archive_entry_set_mode(entry, mode);
1396     } else if(host_os == HOST_UNIX) {
1397         /* Host OS is Unix */
1398         archive_entry_set_mode(entry, (unsigned short) file_attr);
1399     } else {
1400         /* Unknown host OS */
1401         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1402                 "Unsupported Host OS: 0x%02x", (int) host_os);
1403 
1404         return ARCHIVE_FATAL;
1405     }
1406 
1407     if(!read_var_sized(a, &name_size, NULL))
1408         return ARCHIVE_EOF;
1409 
1410     if(!read_ahead(a, name_size, &p))
1411         return ARCHIVE_EOF;
1412 
1413     if(name_size > 2047) {
1414         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1415                 "Filename is too long");
1416 
1417         return ARCHIVE_FATAL;
1418     }
1419 
1420     if(name_size == 0) {
1421         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1422                 "No filename specified");
1423 
1424         return ARCHIVE_FATAL;
1425     }
1426 
1427     memcpy(name_utf8_buf, p, name_size);
1428     name_utf8_buf[name_size] = 0;
1429     if(ARCHIVE_OK != consume(a, name_size)) {
1430         return ARCHIVE_EOF;
1431     }
1432 
1433     if(extra_data_size > 0) {
1434         int ret = process_head_file_extra(a, entry, rar, extra_data_size);
1435 
1436         /* Sanity check. */
1437         if(extra_data_size < 0) {
1438             archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1439                     "File extra data size is not zero");
1440             return ARCHIVE_FATAL;
1441         }
1442 
1443         if(ret != ARCHIVE_OK)
1444             return ret;
1445     }
1446 
1447     if((file_flags & UNKNOWN_UNPACKED_SIZE) == 0) {
1448         rar->file.unpacked_size = (ssize_t) unpacked_size;
1449         archive_entry_set_size(entry, unpacked_size);
1450     }
1451 
1452     if(file_flags & UTIME) {
1453         archive_entry_set_mtime(entry, (time_t) mtime, 0);
1454     }
1455 
1456     if(file_flags & CRC32) {
1457         rar->file.stored_crc32 = crc;
1458     }
1459 
1460     archive_entry_update_pathname_utf8(entry, name_utf8_buf);
1461 
1462     if(!rar->cstate.switch_multivolume) {
1463         /* Do not reinitialize unpacking state if we're switching archives. */
1464         rar->cstate.block_parsing_finished = 1;
1465         rar->cstate.all_filters_applied = 1;
1466         rar->cstate.initialized = 0;
1467     }
1468 
1469     if(rar->generic.split_before > 0) {
1470         /* If now we're standing on a header that has a 'split before' mark,
1471          * it means we're standing on a 'continuation' file header. Signal
1472          * the caller that if it wants to move to another file, it must call
1473          * rar5_read_header() function again. */
1474 
1475         return ARCHIVE_RETRY;
1476     } else {
1477         return ARCHIVE_OK;
1478     }
1479 }
1480 
1481 static int process_head_service(struct archive_read* a, struct rar5* rar,
1482         struct archive_entry* entry, size_t block_flags)
1483 {
1484     /* Process this SERVICE block the same way as FILE blocks. */
1485     int ret = process_head_file(a, rar, entry, block_flags);
1486     if(ret != ARCHIVE_OK)
1487         return ret;
1488 
1489     rar->file.service = 1;
1490 
1491     /* But skip the data part automatically. It's no use for the user anyway.
1492      * It contains only service data, not even needed to properly unpack the
1493      * file. */
1494     ret = rar5_read_data_skip(a);
1495     if(ret != ARCHIVE_OK)
1496         return ret;
1497 
1498     /* After skipping, try parsing another block automatically. */
1499     return ARCHIVE_RETRY;
1500 }
1501 
1502 static int process_head_main(struct archive_read* a, struct rar5* rar,
1503         struct archive_entry* entry, size_t block_flags)
1504 {
1505     (void) entry;
1506 
1507     int ret;
1508     size_t extra_data_size = 0;
1509     size_t extra_field_size = 0;
1510     size_t extra_field_id = 0;
1511     size_t archive_flags = 0;
1512 
1513     if(block_flags & HFL_EXTRA_DATA) {
1514         if(!read_var_sized(a, &extra_data_size, NULL))
1515             return ARCHIVE_EOF;
1516     } else {
1517         extra_data_size = 0;
1518     }
1519 
1520     if(!read_var_sized(a, &archive_flags, NULL)) {
1521         return ARCHIVE_EOF;
1522     }
1523 
1524     enum MAIN_FLAGS {
1525         VOLUME = 0x0001,         /* multi-volume archive */
1526         VOLUME_NUMBER = 0x0002,  /* volume number, first vol doesn't have it */
1527         SOLID = 0x0004,          /* solid archive */
1528         PROTECT = 0x0008,        /* contains Recovery info */
1529         LOCK = 0x0010,           /* readonly flag, not used */
1530     };
1531 
1532     rar->main.volume = (archive_flags & VOLUME) > 0;
1533     rar->main.solid = (archive_flags & SOLID) > 0;
1534 
1535     if(archive_flags & VOLUME_NUMBER) {
1536         size_t v = 0;
1537         if(!read_var_sized(a, &v, NULL)) {
1538             return ARCHIVE_EOF;
1539         }
1540 
1541         rar->main.vol_no = (int) v;
1542     } else {
1543         rar->main.vol_no = 0;
1544     }
1545 
1546     if(rar->vol.expected_vol_no > 0 &&
1547         rar->main.vol_no != rar->vol.expected_vol_no)
1548     {
1549         /* Returning EOF instead of FATAL because of strange libarchive
1550          * behavior. When opening multiple files via
1551          * archive_read_open_filenames(), after reading up the whole last file,
1552          * the __archive_read_ahead function wraps up to the first archive
1553          * instead of returning EOF. */
1554         return ARCHIVE_EOF;
1555     }
1556 
1557     if(extra_data_size == 0) {
1558         /* Early return. */
1559         return ARCHIVE_OK;
1560     }
1561 
1562     if(!read_var_sized(a, &extra_field_size, NULL)) {
1563         return ARCHIVE_EOF;
1564     }
1565 
1566     if(!read_var_sized(a, &extra_field_id, NULL)) {
1567         return ARCHIVE_EOF;
1568     }
1569 
1570     if(extra_field_size == 0) {
1571         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1572                 "Invalid extra field size");
1573         return ARCHIVE_FATAL;
1574     }
1575 
1576     enum MAIN_EXTRA {
1577         // Just one attribute here.
1578         LOCATOR = 0x01,
1579     };
1580 
1581     switch(extra_field_id) {
1582         case LOCATOR:
1583             ret = process_main_locator_extra_block(a, rar);
1584             if(ret != ARCHIVE_OK) {
1585                 /* Error while parsing main locator extra block. */
1586                 return ret;
1587             }
1588 
1589             break;
1590         default:
1591             archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1592                     "Unsupported extra type (0x%02x)", (int) extra_field_id);
1593             return ARCHIVE_FATAL;
1594     }
1595 
1596     return ARCHIVE_OK;
1597 }
1598 
1599 static int scan_for_signature(struct archive_read* a);
1600 
1601 /* Base block processing function. A 'base block' is a RARv5 header block
1602  * that tells the reader what kind of data is stored inside the block.
1603  *
1604  * From the birds-eye view a RAR file looks file this:
1605  *
1606  * <magic><base_block_1><base_block_2>...<base_block_n>
1607  *
1608  * There are a few types of base blocks. Those types are specified inside
1609  * the 'switch' statement in this function. For example purposes, I'll write
1610  * how a standard RARv5 file could look like here:
1611  *
1612  * <magic><MAIN><FILE><FILE><FILE><SERVICE><ENDARC>
1613  *
1614  * The structure above could describe an archive file with 3 files in it,
1615  * one service "QuickOpen" block (that is ignored by this parser), and an
1616  * end of file base block marker.
1617  *
1618  * If the file is stored in multiple archive files ("multiarchive"), it might
1619  * look like this:
1620  *
1621  * .part01.rar: <magic><MAIN><FILE><ENDARC>
1622  * .part02.rar: <magic><MAIN><FILE><ENDARC>
1623  * .part03.rar: <magic><MAIN><FILE><ENDARC>
1624  *
1625  * This example could describe 3 RAR files that contain ONE archived file.
1626  * Or it could describe 3 RAR files that contain 3 different files. Or 3
1627  * RAR files than contain 2 files. It all depends what metadata is stored in
1628  * the headers of <FILE> blocks.
1629  *
1630  * Each <FILE> block contains info about its size, the name of the file it's
1631  * storing inside, and whether this FILE block is a continuation block of
1632  * previous archive ('split before'), and is this FILE block should be
1633  * continued in another archive ('split after'). By parsing the 'split before'
1634  * and 'split after' flags, we're able to tell if multiple <FILE> base blocks
1635  * are describing one file, or multiple files (with the same filename, for
1636  * example).
1637  *
1638  * One thing to note is that if we're parsing the first <FILE> block, and
1639  * we see 'split after' flag, then we need to jump over to another <FILE>
1640  * block to be able to decompress rest of the data. To do this, we need
1641  * to skip the <ENDARC> block, then switch to another file, then skip the
1642  * <magic> block, <MAIN> block, and then we're standing on the proper
1643  * <FILE> block.
1644  */
1645 
1646 static int process_base_block(struct archive_read* a,
1647         struct archive_entry* entry)
1648 {
1649     struct rar5* rar = get_context(a);
1650     uint32_t hdr_crc, computed_crc;
1651     size_t raw_hdr_size = 0, hdr_size_len, hdr_size;
1652     size_t header_id = 0;
1653     size_t header_flags = 0;
1654     const uint8_t* p;
1655     int ret;
1656 
1657     /* Skip any unprocessed data for this file. */
1658     if(rar->file.bytes_remaining) {
1659         ret = rar5_read_data_skip(a);
1660         if(ret != ARCHIVE_OK) {
1661             return ret;
1662         }
1663     }
1664 
1665     /* Read the expected CRC32 checksum. */
1666     if(!read_u32(a, &hdr_crc)) {
1667         return ARCHIVE_EOF;
1668     }
1669 
1670     /* Read header size. */
1671     if(!read_var_sized(a, &raw_hdr_size, &hdr_size_len)) {
1672         return ARCHIVE_EOF;
1673     }
1674 
1675     /* Sanity check, maximum header size for RAR5 is 2MB. */
1676     if(raw_hdr_size > (2 * 1024 * 1024)) {
1677         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1678                 "Base block header is too large");
1679 
1680         return ARCHIVE_FATAL;
1681     }
1682 
1683     hdr_size = raw_hdr_size + hdr_size_len;
1684 
1685     /* Read the whole header data into memory, maximum memory use here is
1686      * 2MB. */
1687     if(!read_ahead(a, hdr_size, &p)) {
1688         return ARCHIVE_EOF;
1689     }
1690 
1691     /* Verify the CRC32 of the header data. */
1692     computed_crc = (uint32_t) crc32(0, p, (int) hdr_size);
1693     if(computed_crc != hdr_crc) {
1694         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1695                 "Header CRC error");
1696 
1697         return ARCHIVE_FATAL;
1698     }
1699 
1700     /* If the checksum is OK, we proceed with parsing. */
1701     if(ARCHIVE_OK != consume(a, hdr_size_len)) {
1702         return ARCHIVE_EOF;
1703     }
1704 
1705     if(!read_var_sized(a, &header_id, NULL))
1706         return ARCHIVE_EOF;
1707 
1708     if(!read_var_sized(a, &header_flags, NULL))
1709         return ARCHIVE_EOF;
1710 
1711     rar->generic.split_after = (header_flags & HFL_SPLIT_AFTER) > 0;
1712     rar->generic.split_before = (header_flags & HFL_SPLIT_BEFORE) > 0;
1713     rar->generic.size = hdr_size;
1714     rar->generic.last_header_id = header_id;
1715     rar->main.endarc = 0;
1716 
1717     /* Those are possible header ids in RARv5. */
1718     enum HEADER_TYPE {
1719         HEAD_MARK    = 0x00, HEAD_MAIN  = 0x01, HEAD_FILE   = 0x02,
1720         HEAD_SERVICE = 0x03, HEAD_CRYPT = 0x04, HEAD_ENDARC = 0x05,
1721         HEAD_UNKNOWN = 0xff,
1722     };
1723 
1724     switch(header_id) {
1725         case HEAD_MAIN:
1726             ret = process_head_main(a, rar, entry, header_flags);
1727 
1728             /* Main header doesn't have any files in it, so it's pointless
1729              * to return to the caller. Retry to next header, which should be
1730              * HEAD_FILE/HEAD_SERVICE. */
1731             if(ret == ARCHIVE_OK)
1732                 return ARCHIVE_RETRY;
1733 
1734             return ret;
1735         case HEAD_SERVICE:
1736             ret = process_head_service(a, rar, entry, header_flags);
1737             return ret;
1738         case HEAD_FILE:
1739             ret = process_head_file(a, rar, entry, header_flags);
1740             return ret;
1741         case HEAD_CRYPT:
1742             archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1743                     "Encryption is not supported");
1744             return ARCHIVE_FATAL;
1745         case HEAD_ENDARC:
1746             rar->main.endarc = 1;
1747 
1748             /* After encountering an end of file marker, we need to take
1749              * into consideration if this archive is continued in another
1750              * file (i.e. is it part01.rar: is there a part02.rar?) */
1751             if(rar->main.volume) {
1752                 /* In case there is part02.rar, position the read pointer
1753                  * in a proper place, so we can resume parsing. */
1754 
1755                 ret = scan_for_signature(a);
1756                 if(ret == ARCHIVE_FATAL) {
1757                     return ARCHIVE_EOF;
1758                 } else {
1759                     rar->vol.expected_vol_no = rar->main.vol_no + 1;
1760                     return ARCHIVE_OK;
1761                 }
1762             } else {
1763                 return ARCHIVE_EOF;
1764             }
1765         case HEAD_MARK:
1766             return ARCHIVE_EOF;
1767         default:
1768             if((header_flags & HFL_SKIP_IF_UNKNOWN) == 0) {
1769                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1770                         "Header type error");
1771                 return ARCHIVE_FATAL;
1772             } else {
1773                 /* If the block is marked as 'skip if unknown', do as the flag
1774                  * says: skip the block instead on failing on it. */
1775                 return ARCHIVE_RETRY;
1776             }
1777     }
1778 
1779 #if !defined WIN32
1780     // Not reached.
1781     archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1782             "Internal unpacker error");
1783     return ARCHIVE_FATAL;
1784 #endif
1785 }
1786 
1787 static int skip_base_block(struct archive_read* a) {
1788     int ret;
1789     struct rar5* rar = get_context(a);
1790 
1791     struct archive_entry entry;
1792     ret = process_base_block(a, &entry);
1793 
1794     if(rar->generic.last_header_id == 2 && rar->generic.split_before > 0)
1795         return ARCHIVE_OK;
1796 
1797     if(ret == ARCHIVE_OK)
1798         return ARCHIVE_RETRY;
1799     else
1800         return ret;
1801 }
1802 
1803 static int rar5_read_header(struct archive_read *a,
1804         struct archive_entry *entry)
1805 {
1806     struct rar5* rar = get_context(a);
1807     int ret;
1808 
1809     if(rar->header_initialized == 0) {
1810         init_header(a);
1811         rar->header_initialized = 1;
1812     }
1813 
1814     if(rar->skipped_magic == 0) {
1815         if(ARCHIVE_OK != consume(a, rar5_signature_size)) {
1816             return ARCHIVE_EOF;
1817         }
1818 
1819         rar->skipped_magic = 1;
1820     }
1821 
1822     do {
1823         ret = process_base_block(a, entry);
1824     } while(ret == ARCHIVE_RETRY ||
1825             (rar->main.endarc > 0 && ret == ARCHIVE_OK));
1826 
1827     return ret;
1828 }
1829 
1830 static void init_unpack(struct rar5* rar) {
1831     rar->file.calculated_crc32 = 0;
1832     rar->cstate.window_mask = rar->cstate.window_size - 1;
1833 
1834     if(rar->cstate.window_buf)
1835         free(rar->cstate.window_buf);
1836 
1837     if(rar->cstate.filtered_buf)
1838         free(rar->cstate.filtered_buf);
1839 
1840     rar->cstate.window_buf = calloc(1, rar->cstate.window_size);
1841     rar->cstate.filtered_buf = calloc(1, rar->cstate.window_size);
1842 
1843     rar->cstate.write_ptr = 0;
1844     rar->cstate.last_write_ptr = 0;
1845 
1846     memset(&rar->cstate.bd, 0, sizeof(rar->cstate.bd));
1847     memset(&rar->cstate.ld, 0, sizeof(rar->cstate.ld));
1848     memset(&rar->cstate.dd, 0, sizeof(rar->cstate.dd));
1849     memset(&rar->cstate.ldd, 0, sizeof(rar->cstate.ldd));
1850     memset(&rar->cstate.rd, 0, sizeof(rar->cstate.rd));
1851 }
1852 
1853 static void update_crc(struct rar5* rar, const uint8_t* p, size_t to_read) {
1854     int verify_crc;
1855 
1856     if(rar->skip_mode) {
1857 #if defined CHECK_CRC_ON_SOLID_SKIP
1858         verify_crc = 1;
1859 #else
1860         verify_crc = 0;
1861 #endif
1862     } else
1863         verify_crc = 1;
1864 
1865     if(verify_crc) {
1866         /* Don't update CRC32 if the file doesn't have the `stored_crc32` info
1867            filled in. */
1868         if(rar->file.stored_crc32 > 0) {
1869             rar->file.calculated_crc32 =
1870                 crc32(rar->file.calculated_crc32, p, to_read);
1871         }
1872 
1873         /* Check if the file uses an optional BLAKE2sp checksum algorithm. */
1874         if(rar->file.has_blake2 > 0) {
1875             /* Return value of the `update` function is always 0, so we can
1876              * explicitly ignore it here. */
1877             (void) blake2sp_update(&rar->file.b2state, p, to_read);
1878         }
1879     }
1880 }
1881 
1882 static int create_decode_tables(uint8_t* bit_length,
1883         struct decode_table* table,
1884         int size)
1885 {
1886     int code, upper_limit = 0, i, lc[16];
1887     uint32_t decode_pos_clone[rar5_countof(table->decode_pos)];
1888     ssize_t cur_len, quick_data_size;
1889 
1890     memset(&lc, 0, sizeof(lc));
1891     memset(table->decode_num, 0, sizeof(table->decode_num));
1892     table->size = size;
1893     table->quick_bits = size == HUFF_NC ? 10 : 7;
1894 
1895     for(i = 0; i < size; i++) {
1896         lc[bit_length[i] & 15]++;
1897     }
1898 
1899     lc[0] = 0;
1900     table->decode_pos[0] = 0;
1901     table->decode_len[0] = 0;
1902 
1903     for(i = 1; i < 16; i++) {
1904         upper_limit += lc[i];
1905 
1906         table->decode_len[i] = upper_limit << (16 - i);
1907         table->decode_pos[i] = table->decode_pos[i - 1] + lc[i - 1];
1908 
1909         upper_limit <<= 1;
1910     }
1911 
1912     memcpy(decode_pos_clone, table->decode_pos, sizeof(decode_pos_clone));
1913 
1914     for(i = 0; i < size; i++) {
1915         uint8_t clen = bit_length[i] & 15;
1916         if(clen > 0) {
1917             int last_pos = decode_pos_clone[clen];
1918             table->decode_num[last_pos] = i;
1919             decode_pos_clone[clen]++;
1920         }
1921     }
1922 
1923     quick_data_size = 1 << table->quick_bits;
1924     cur_len = 1;
1925     for(code = 0; code < quick_data_size; code++) {
1926         int bit_field = code << (16 - table->quick_bits);
1927         int dist, pos;
1928 
1929         while(cur_len < rar5_countof(table->decode_len) &&
1930                 bit_field >= table->decode_len[cur_len]) {
1931             cur_len++;
1932         }
1933 
1934         table->quick_len[code] = (uint8_t) cur_len;
1935 
1936         dist = bit_field - table->decode_len[cur_len - 1];
1937         dist >>= (16 - cur_len);
1938 
1939         pos = table->decode_pos[cur_len] + dist;
1940         if(cur_len < rar5_countof(table->decode_pos) && pos < size) {
1941             table->quick_num[code] = table->decode_num[pos];
1942         } else {
1943             table->quick_num[code] = 0;
1944         }
1945     }
1946 
1947     return ARCHIVE_OK;
1948 }
1949 
1950 static int decode_number(struct archive_read* a, struct decode_table* table,
1951         const uint8_t* p, uint16_t* num)
1952 {
1953     int i, bits, dist;
1954     uint16_t bitfield;
1955     uint32_t pos;
1956     struct rar5* rar = get_context(a);
1957 
1958     if(ARCHIVE_OK != read_bits_16(rar, p, &bitfield)) {
1959         return ARCHIVE_EOF;
1960     }
1961 
1962     bitfield &= 0xfffe;
1963 
1964     if(bitfield < table->decode_len[table->quick_bits]) {
1965         int code = bitfield >> (16 - table->quick_bits);
1966         skip_bits(rar, table->quick_len[code]);
1967         *num = table->quick_num[code];
1968         return ARCHIVE_OK;
1969     }
1970 
1971     bits = 15;
1972 
1973     for(i = table->quick_bits + 1; i < 15; i++) {
1974         if(bitfield < table->decode_len[i]) {
1975             bits = i;
1976             break;
1977         }
1978     }
1979 
1980     skip_bits(rar, bits);
1981 
1982     dist = bitfield - table->decode_len[bits - 1];
1983     dist >>= (16 - bits);
1984     pos = table->decode_pos[bits] + dist;
1985 
1986     if(pos >= table->size)
1987         pos = 0;
1988 
1989     *num = table->decode_num[pos];
1990     return ARCHIVE_OK;
1991 }
1992 
1993 /* Reads and parses Huffman tables from the beginning of the block. */
1994 static int parse_tables(struct archive_read* a, struct rar5* rar,
1995         const uint8_t* p)
1996 {
1997     int ret, value, i, w, idx = 0;
1998     uint8_t bit_length[HUFF_BC],
1999         table[HUFF_TABLE_SIZE],
2000         nibble_mask = 0xF0,
2001         nibble_shift = 4;
2002 
2003     enum { ESCAPE = 15 };
2004 
2005     /* The data for table generation is compressed using a simple RLE-like
2006      * algorithm when storing zeroes, so we need to unpack it first. */
2007     for(w = 0, i = 0; w < HUFF_BC;) {
2008         value = (p[i] & nibble_mask) >> nibble_shift;
2009 
2010         if(nibble_mask == 0x0F)
2011             ++i;
2012 
2013         nibble_mask ^= 0xFF;
2014         nibble_shift ^= 4;
2015 
2016         /* Values smaller than 15 is data, so we write it directly. Value 15
2017          * is a flag telling us that we need to unpack more bytes. */
2018         if(value == ESCAPE) {
2019             value = (p[i] & nibble_mask) >> nibble_shift;
2020             if(nibble_mask == 0x0F)
2021                 ++i;
2022             nibble_mask ^= 0xFF;
2023             nibble_shift ^= 4;
2024 
2025             if(value == 0) {
2026                 /* We sometimes need to write the actual value of 15, so this
2027                  * case handles that. */
2028                 bit_length[w++] = ESCAPE;
2029             } else {
2030                 int k;
2031 
2032                 /* Fill zeroes. */
2033                 for(k = 0; k < value + 2; k++) {
2034                     bit_length[w++] = 0;
2035                 }
2036             }
2037         } else {
2038             bit_length[w++] = value;
2039         }
2040     }
2041 
2042     rar->bits.in_addr = i;
2043     rar->bits.bit_addr = nibble_shift ^ 4;
2044 
2045     ret = create_decode_tables(bit_length, &rar->cstate.bd, HUFF_BC);
2046     if(ret != ARCHIVE_OK) {
2047         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2048                 "Decoding huffman tables failed");
2049         return ARCHIVE_FATAL;
2050     }
2051 
2052     for(i = 0; i < HUFF_TABLE_SIZE;) {
2053         uint16_t num;
2054 
2055         ret = decode_number(a, &rar->cstate.bd, p, &num);
2056         if(ret != ARCHIVE_OK) {
2057             archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2058                     "Decoding huffman tables failed");
2059             return ARCHIVE_FATAL;
2060         }
2061 
2062         if(num < 16) {
2063             /* 0..15: store directly */
2064             table[i] = (uint8_t) num;
2065             i++;
2066             continue;
2067         }
2068 
2069         if(num < 18) {
2070             /* 16..17: repeat previous code */
2071             uint16_t n;
2072             if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2073                 return ARCHIVE_EOF;
2074 
2075             if(num == 16) {
2076                 n >>= 13;
2077                 n += 3;
2078                 skip_bits(rar, 3);
2079             } else {
2080                 n >>= 9;
2081                 n += 11;
2082                 skip_bits(rar, 7);
2083             }
2084 
2085             if(i > 0) {
2086                 while(n-- > 0 && i < HUFF_TABLE_SIZE) {
2087                     table[i] = table[i - 1];
2088                     i++;
2089                 }
2090             } else {
2091                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2092                         "Unexpected error when decoding huffman tables");
2093                 return ARCHIVE_FATAL;
2094             }
2095 
2096             continue;
2097         }
2098 
2099         /* other codes: fill with zeroes `n` times */
2100         uint16_t n;
2101         if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2102             return ARCHIVE_EOF;
2103 
2104         if(num == 18) {
2105             n >>= 13;
2106             n += 3;
2107             skip_bits(rar, 3);
2108         } else {
2109             n >>= 9;
2110             n += 11;
2111             skip_bits(rar, 7);
2112         }
2113 
2114         while(n-- > 0 && i < HUFF_TABLE_SIZE)
2115             table[i++] = 0;
2116     }
2117 
2118     ret = create_decode_tables(&table[idx], &rar->cstate.ld, HUFF_NC);
2119     if(ret != ARCHIVE_OK) {
2120         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2121                 "Failed to create literal table");
2122         return ARCHIVE_FATAL;
2123     }
2124 
2125     idx += HUFF_NC;
2126 
2127     ret = create_decode_tables(&table[idx], &rar->cstate.dd, HUFF_DC);
2128     if(ret != ARCHIVE_OK) {
2129         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2130                 "Failed to create distance table");
2131         return ARCHIVE_FATAL;
2132     }
2133 
2134     idx += HUFF_DC;
2135 
2136     ret = create_decode_tables(&table[idx], &rar->cstate.ldd, HUFF_LDC);
2137     if(ret != ARCHIVE_OK) {
2138         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2139                 "Failed to create lower bits of distances table");
2140         return ARCHIVE_FATAL;
2141     }
2142 
2143     idx += HUFF_LDC;
2144 
2145     ret = create_decode_tables(&table[idx], &rar->cstate.rd, HUFF_RC);
2146     if(ret != ARCHIVE_OK) {
2147         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2148                 "Failed to create repeating distances table");
2149         return ARCHIVE_FATAL;
2150     }
2151 
2152     return ARCHIVE_OK;
2153 }
2154 
2155 /* Parses the block header, verifies its CRC byte, and saves the header
2156  * fields inside the `hdr` pointer. */
2157 static int parse_block_header(struct archive_read* a, const uint8_t* p,
2158         ssize_t* block_size, struct compressed_block_header* hdr)
2159 {
2160     memcpy(hdr, p, sizeof(struct compressed_block_header));
2161 
2162     if(hdr->block_flags.byte_count > 2) {
2163         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2164                 "Unsupported block header size (was %d, max is 2)",
2165                 hdr->block_flags.byte_count);
2166         return ARCHIVE_FATAL;
2167     }
2168 
2169     /* This should probably use bit reader interface in order to be more
2170      * future-proof. */
2171     *block_size = 0;
2172     switch(hdr->block_flags.byte_count) {
2173         /* 1-byte block size */
2174         case 0:
2175             *block_size = *(const uint8_t*) &p[2];
2176             break;
2177 
2178         /* 2-byte block size */
2179         case 1:
2180             *block_size = *(const uint16_t*) &p[2];
2181             break;
2182 
2183         /* 3-byte block size */
2184         case 2:
2185             *block_size = *(const uint32_t*) &p[2];
2186             *block_size &= 0x00FFFFFF;
2187             break;
2188 
2189         /* Other block sizes are not supported. This case is not reached,
2190          * because we have an 'if' guard before the switch that makes sure
2191          * of it. */
2192         default:
2193             return ARCHIVE_FATAL;
2194     }
2195 
2196     /* Verify the block header checksum. 0x5A is a magic value and is always
2197      * constant. */
2198     uint8_t calculated_cksum = 0x5A
2199                                ^ (uint8_t) hdr->block_flags_u8
2200                                ^ (uint8_t) *block_size
2201                                ^ (uint8_t) (*block_size >> 8)
2202                                ^ (uint8_t) (*block_size >> 16);
2203 
2204     if(calculated_cksum != hdr->block_cksum) {
2205         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2206                 "Block checksum error: got 0x%02x, expected 0x%02x",
2207                 hdr->block_cksum, calculated_cksum);
2208 
2209         return ARCHIVE_FATAL;
2210     }
2211 
2212     return ARCHIVE_OK;
2213 }
2214 
2215 /* Convenience function used during filter processing. */
2216 static int parse_filter_data(struct rar5* rar, const uint8_t* p,
2217         uint32_t* filter_data)
2218 {
2219     int i, bytes;
2220     uint32_t data = 0;
2221 
2222     if(ARCHIVE_OK != read_consume_bits(rar, p, 2, &bytes))
2223         return ARCHIVE_EOF;
2224 
2225     bytes++;
2226 
2227     for(i = 0; i < bytes; i++) {
2228         uint16_t byte;
2229 
2230         if(ARCHIVE_OK != read_bits_16(rar, p, &byte)) {
2231             return ARCHIVE_EOF;
2232         }
2233 
2234         data += (byte >> 8) << (i * 8);
2235         skip_bits(rar, 8);
2236     }
2237 
2238     *filter_data = data;
2239     return ARCHIVE_OK;
2240 }
2241 
2242 /* Function is used during sanity checking. */
2243 static int is_valid_filter_block_start(struct rar5* rar,
2244         uint32_t start)
2245 {
2246     const int64_t block_start = (ssize_t) start + rar->cstate.write_ptr;
2247     const int64_t last_bs = rar->cstate.last_block_start;
2248     const ssize_t last_bl = rar->cstate.last_block_length;
2249 
2250     if(last_bs == 0 || last_bl == 0) {
2251         /* We didn't have any filters yet, so accept this offset. */
2252         return 1;
2253     }
2254 
2255     if(block_start >= last_bs + last_bl) {
2256         /* Current offset is bigger than last block's end offset, so
2257          * accept current offset. */
2258         return 1;
2259     }
2260 
2261     /* Any other case is not a normal situation and we should fail. */
2262     return 0;
2263 }
2264 
2265 /* The function will create a new filter, read its parameters from the input
2266  * stream and add it to the filter collection. */
2267 static int parse_filter(struct archive_read* ar, const uint8_t* p) {
2268     uint32_t block_start, block_length;
2269     uint16_t filter_type;
2270     struct rar5* rar = get_context(ar);
2271 
2272     /* Read the parameters from the input stream. */
2273     if(ARCHIVE_OK != parse_filter_data(rar, p, &block_start))
2274         return ARCHIVE_EOF;
2275 
2276     if(ARCHIVE_OK != parse_filter_data(rar, p, &block_length))
2277         return ARCHIVE_EOF;
2278 
2279     if(ARCHIVE_OK != read_bits_16(rar, p, &filter_type))
2280         return ARCHIVE_EOF;
2281 
2282     filter_type >>= 13;
2283     skip_bits(rar, 3);
2284 
2285     /* Perform some sanity checks on this filter parameters. Note that we
2286      * allow only DELTA, E8/E9 and ARM filters here, because rest of filters
2287      * are not used in RARv5. */
2288 
2289     if(block_length < 4 ||
2290         block_length > 0x400000 ||
2291         filter_type > FILTER_ARM ||
2292         !is_valid_filter_block_start(rar, block_start))
2293     {
2294         archive_set_error(&ar->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Invalid "
2295                 "filter encountered");
2296         return ARCHIVE_FATAL;
2297     }
2298 
2299     /* Allocate a new filter. */
2300     struct filter_info* filt = add_new_filter(rar);
2301     if(filt == NULL) {
2302         archive_set_error(&ar->archive, ENOMEM, "Can't allocate memory for a "
2303                 "filter descriptor.");
2304         return ARCHIVE_FATAL;
2305     }
2306 
2307     filt->type = filter_type;
2308     filt->block_start = rar->cstate.write_ptr + block_start;
2309     filt->block_length = block_length;
2310 
2311     rar->cstate.last_block_start = filt->block_start;
2312     rar->cstate.last_block_length = filt->block_length;
2313 
2314     /* Read some more data in case this is a DELTA filter. Other filter types
2315      * don't require any additional data over what was already read. */
2316     if(filter_type == FILTER_DELTA) {
2317         int channels;
2318 
2319         if(ARCHIVE_OK != read_consume_bits(rar, p, 5, &channels))
2320             return ARCHIVE_EOF;
2321 
2322         filt->channels = channels + 1;
2323     }
2324 
2325     return ARCHIVE_OK;
2326 }
2327 
2328 static int decode_code_length(struct rar5* rar, const uint8_t* p,
2329         uint16_t code)
2330 {
2331     int lbits, length = 2;
2332     if(code < 8) {
2333         lbits = 0;
2334         length += code;
2335     } else {
2336         lbits = code / 4 - 1;
2337         length += (4 | (code & 3)) << lbits;
2338     }
2339 
2340     if(lbits > 0) {
2341         int add;
2342 
2343         if(ARCHIVE_OK != read_consume_bits(rar, p, lbits, &add))
2344             return -1;
2345 
2346         length += add;
2347     }
2348 
2349     return length;
2350 }
2351 
2352 static int copy_string(struct archive_read* a, int len, int dist) {
2353     struct rar5* rar = get_context(a);
2354     const int cmask = rar->cstate.window_mask;
2355     const int64_t write_ptr = rar->cstate.write_ptr + rar->cstate.solid_offset;
2356     int i;
2357 
2358     /* The unpacker spends most of the time in this function. It would be
2359      * a good idea to introduce some optimizations here.
2360      *
2361      * Just remember that this loop treats buffers that overlap differently
2362      * than buffers that do not overlap. This is why a simple memcpy(3) call
2363      * will not be enough. */
2364 
2365     for(i = 0; i < len; i++) {
2366         const ssize_t write_idx = (write_ptr + i) & cmask;
2367         const ssize_t read_idx = (write_ptr + i - dist) & cmask;
2368         rar->cstate.window_buf[write_idx] = rar->cstate.window_buf[read_idx];
2369     }
2370 
2371     rar->cstate.write_ptr += len;
2372     return ARCHIVE_OK;
2373 }
2374 
2375 static int do_uncompress_block(struct archive_read* a, const uint8_t* p) {
2376     struct rar5* rar = get_context(a);
2377     uint16_t num;
2378     int ret;
2379 
2380     const int cmask = rar->cstate.window_mask;
2381     const struct compressed_block_header* hdr = &rar->last_block_hdr;
2382     const uint8_t bit_size = 1 + hdr->block_flags.bit_size;
2383 
2384     while(1) {
2385         if(rar->cstate.write_ptr - rar->cstate.last_write_ptr >
2386                 (rar->cstate.window_size >> 1)) {
2387 
2388             /* Don't allow growing data by more than half of the window size
2389              * at a time. In such case, break the loop; next call to this
2390              * function will continue processing from this moment. */
2391 
2392             break;
2393         }
2394 
2395         if(rar->bits.in_addr > rar->cstate.cur_block_size - 1 ||
2396                 (rar->bits.in_addr == rar->cstate.cur_block_size - 1 &&
2397                  rar->bits.bit_addr >= bit_size))
2398         {
2399             /* If the program counter is here, it means the function has
2400              * finished processing the block. */
2401             rar->cstate.block_parsing_finished = 1;
2402             break;
2403         }
2404 
2405         /* Decode the next literal. */
2406         if(ARCHIVE_OK != decode_number(a, &rar->cstate.ld, p, &num)) {
2407             return ARCHIVE_EOF;
2408         }
2409 
2410         /* Num holds a decompression literal, or 'command code'.
2411          *
2412          * - Values lower than 256 are just bytes. Those codes can be stored
2413          *   in the output buffer directly.
2414          *
2415          * - Code 256 defines a new filter, which is later used to transform
2416          *   the data block accordingly to the filter type. The data block
2417          *   needs to be fully uncompressed first.
2418          *
2419          * - Code bigger than 257 and smaller than 262 define a repetition
2420          *   pattern that should be copied from an already uncompressed chunk
2421          *   of data.
2422          */
2423 
2424         if(num < 256) {
2425             /* Directly store the byte. */
2426 
2427             int64_t write_idx = rar->cstate.solid_offset +
2428                 rar->cstate.write_ptr++;
2429 
2430             rar->cstate.window_buf[write_idx & cmask] = (uint8_t) num;
2431             continue;
2432         } else if(num >= 262) {
2433             uint16_t dist_slot;
2434             int len = decode_code_length(rar, p, num - 262),
2435                 dbits,
2436                 dist = 1;
2437 
2438             if(len == -1) {
2439                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2440                     "Failed to decode the code length");
2441 
2442                 return ARCHIVE_FATAL;
2443             }
2444 
2445             if(ARCHIVE_OK != decode_number(a, &rar->cstate.dd, p, &dist_slot))
2446             {
2447                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2448                     "Failed to decode the distance slot");
2449 
2450                 return ARCHIVE_FATAL;
2451             }
2452 
2453             if(dist_slot < 4) {
2454                 dbits = 0;
2455                 dist += dist_slot;
2456             } else {
2457                 dbits = dist_slot / 2 - 1;
2458                 dist += (2 | (dist_slot & 1)) << dbits;
2459             }
2460 
2461             if(dbits > 0) {
2462                 if(dbits >= 4) {
2463                     uint32_t add = 0;
2464                     uint16_t low_dist;
2465 
2466                     if(dbits > 4) {
2467                         if(ARCHIVE_OK != read_bits_32(rar, p, &add)) {
2468                             /* Return EOF if we can't read more data. */
2469                             return ARCHIVE_EOF;
2470                         }
2471 
2472                         skip_bits(rar, dbits - 4);
2473                         add = (add >> (36 - dbits)) << 4;
2474                         dist += add;
2475                     }
2476 
2477                     if(ARCHIVE_OK != decode_number(a, &rar->cstate.ldd, p,
2478                                 &low_dist))
2479                     {
2480                         archive_set_error(&a->archive,
2481                                 ARCHIVE_ERRNO_PROGRAMMER,
2482                                 "Failed to decode the distance slot");
2483 
2484                         return ARCHIVE_FATAL;
2485                     }
2486 
2487                     dist += low_dist;
2488                 } else {
2489                     /* dbits is one of [0,1,2,3] */
2490                     int add;
2491 
2492                     if(ARCHIVE_OK != read_consume_bits(rar, p, dbits, &add)) {
2493                         /* Return EOF if we can't read more data. */
2494                         return ARCHIVE_EOF;
2495                     }
2496 
2497                     dist += add;
2498                 }
2499             }
2500 
2501             if(dist > 0x100) {
2502                 len++;
2503 
2504                 if(dist > 0x2000) {
2505                     len++;
2506 
2507                     if(dist > 0x40000) {
2508                         len++;
2509                     }
2510                 }
2511             }
2512 
2513             dist_cache_push(rar, dist);
2514             rar->cstate.last_len = len;
2515 
2516             if(ARCHIVE_OK != copy_string(a, len, dist))
2517                 return ARCHIVE_FATAL;
2518 
2519             continue;
2520         } else if(num == 256) {
2521             /* Create a filter. */
2522             ret = parse_filter(a, p);
2523             if(ret != ARCHIVE_OK)
2524                 return ret;
2525 
2526             continue;
2527         } else if(num == 257) {
2528             if(rar->cstate.last_len != 0) {
2529                 if(ARCHIVE_OK != copy_string(a, rar->cstate.last_len,
2530                             rar->cstate.dist_cache[0]))
2531                 {
2532                     return ARCHIVE_FATAL;
2533                 }
2534             }
2535 
2536             continue;
2537         } else if(num < 262) {
2538             const int idx = num - 258;
2539             const int dist = dist_cache_touch(rar, idx);
2540 
2541             uint16_t len_slot;
2542             int len;
2543 
2544             if(ARCHIVE_OK != decode_number(a, &rar->cstate.rd, p, &len_slot)) {
2545                 return ARCHIVE_FATAL;
2546             }
2547 
2548             len = decode_code_length(rar, p, len_slot);
2549             rar->cstate.last_len = len;
2550 
2551             if(ARCHIVE_OK != copy_string(a, len, dist))
2552                 return ARCHIVE_FATAL;
2553 
2554             continue;
2555         }
2556 
2557         /* The program counter shouldn't reach here. */
2558         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2559                 "Unsupported block code: 0x%02x", num);
2560 
2561         return ARCHIVE_FATAL;
2562     }
2563 
2564     return ARCHIVE_OK;
2565 }
2566 
2567 /* Binary search for the RARv5 signature. */
2568 static int scan_for_signature(struct archive_read* a) {
2569     const uint8_t* p;
2570     const int chunk_size = 512;
2571     ssize_t i;
2572 
2573     /* If we're here, it means we're on an 'unknown territory' data.
2574      * There's no indication what kind of data we're reading here. It could be
2575      * some text comment, any kind of binary data, digital sign, dragons, etc.
2576      *
2577      * We want to find a valid RARv5 magic header inside this unknown data. */
2578 
2579     /* Is it possible in libarchive to just skip everything until the
2580      * end of the file? If so, it would be a better approach than the
2581      * current implementation of this function. */
2582 
2583     while(1) {
2584         if(!read_ahead(a, chunk_size, &p))
2585             return ARCHIVE_EOF;
2586 
2587         for(i = 0; i < chunk_size - rar5_signature_size; i++) {
2588             if(memcmp(&p[i], rar5_signature, rar5_signature_size) == 0) {
2589                 /* Consume the number of bytes we've used to search for the
2590                  * signature, as well as the number of bytes used by the
2591                  * signature itself. After this we should be standing on a
2592                  * valid base block header. */
2593                 (void) consume(a, i + rar5_signature_size);
2594                 return ARCHIVE_OK;
2595             }
2596         }
2597 
2598         consume(a, chunk_size);
2599     }
2600 
2601     return ARCHIVE_FATAL;
2602 }
2603 
2604 /* This function will switch the multivolume archive file to another file,
2605  * i.e. from part03 to part 04. */
2606 static int advance_multivolume(struct archive_read* a) {
2607     int lret;
2608     struct rar5* rar = get_context(a);
2609 
2610     /* A small state machine that will skip unnecessary data, needed to
2611      * switch from one multivolume to another. Such skipping is needed if
2612      * we want to be an stream-oriented (instead of file-oriented)
2613      * unpacker.
2614      *
2615      * The state machine starts with `rar->main.endarc` == 0. It also
2616      * assumes that current stream pointer points to some base block header.
2617      *
2618      * The `endarc` field is being set when the base block parsing function
2619      * encounters the 'end of archive' marker.
2620      */
2621 
2622     while(1) {
2623         if(rar->main.endarc == 1) {
2624             rar->main.endarc = 0;
2625             while(ARCHIVE_RETRY == skip_base_block(a));
2626             break;
2627         } else {
2628             /* Skip current base block. In order to properly skip it,
2629              * we really need to simply parse it and discard the results. */
2630 
2631             lret = skip_base_block(a);
2632 
2633             /* The `skip_base_block` function tells us if we should continue
2634              * with skipping, or we should stop skipping. We're trying to skip
2635              * everything up to a base FILE block. */
2636 
2637             if(lret != ARCHIVE_RETRY) {
2638                 /* If there was an error during skipping, or we have just
2639                  * skipped a FILE base block... */
2640 
2641                 if(rar->main.endarc == 0) {
2642                     return lret;
2643                 } else {
2644                     continue;
2645                 }
2646             }
2647         }
2648     }
2649 
2650     return ARCHIVE_OK;
2651 }
2652 
2653 /* Merges the partial block from the first multivolume archive file, and
2654  * partial block from the second multivolume archive file. The result is
2655  * a chunk of memory containing the whole block, and the stream pointer
2656  * is advanced to the next block in the second multivolume archive file. */
2657 static int merge_block(struct archive_read* a, ssize_t block_size,
2658         const uint8_t** p)
2659 {
2660     struct rar5* rar = get_context(a);
2661     ssize_t cur_block_size, partial_offset = 0;
2662     const uint8_t* lp;
2663     int ret;
2664 
2665     /* Set a flag that we're in the switching mode. */
2666     rar->cstate.switch_multivolume = 1;
2667 
2668     /* Reallocate the memory which will hold the whole block. */
2669     if(rar->vol.push_buf)
2670         free((void*) rar->vol.push_buf);
2671 
2672     rar->vol.push_buf = malloc(block_size);
2673     if(!rar->vol.push_buf) {
2674         archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for a "
2675                 "merge block buffer.");
2676         return ARCHIVE_FATAL;
2677     }
2678 
2679     /* A single block can span across multiple multivolume archive files,
2680      * so we use a loop here. This loop will consume enough multivolume
2681      * archive files until the whole block is read. */
2682 
2683     while(1) {
2684         /* Get the size of current block chunk in this multivolume archive
2685          * file and read it. */
2686         cur_block_size =
2687             rar5_min(rar->file.bytes_remaining, block_size - partial_offset);
2688 
2689         if(cur_block_size == 0) {
2690             archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2691                     "Encountered block size == 0 during block merge");
2692             return ARCHIVE_FATAL;
2693         }
2694 
2695         if(!read_ahead(a, cur_block_size, &lp))
2696             return ARCHIVE_EOF;
2697 
2698         /* Sanity check; there should never be a situation where this function
2699          * reads more data than the block's size. */
2700         if(partial_offset + cur_block_size > block_size) {
2701             archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2702                 "Consumed too much data when merging blocks.");
2703             return ARCHIVE_FATAL;
2704         }
2705 
2706         /* Merge previous block chunk with current block chunk, or create
2707          * first block chunk if this is our first iteration. */
2708         memcpy(&rar->vol.push_buf[partial_offset], lp, cur_block_size);
2709 
2710         /* Advance the stream read pointer by this block chunk size. */
2711         if(ARCHIVE_OK != consume(a, cur_block_size))
2712             return ARCHIVE_EOF;
2713 
2714         /* Update the pointers. `partial_offset` contains information about
2715          * the sum of merged block chunks. */
2716         partial_offset += cur_block_size;
2717         rar->file.bytes_remaining -= cur_block_size;
2718 
2719         /* If `partial_offset` is the same as `block_size`, this means we've
2720          * merged all block chunks and we have a valid full block. */
2721         if(partial_offset == block_size) {
2722             break;
2723         }
2724 
2725         /* If we don't have any bytes to read, this means we should switch
2726          * to another multivolume archive file. */
2727         if(rar->file.bytes_remaining == 0) {
2728             ret = advance_multivolume(a);
2729             if(ret != ARCHIVE_OK)
2730                 return ret;
2731         }
2732     }
2733 
2734     *p = rar->vol.push_buf;
2735 
2736     /* If we're here, we can resume unpacking by processing the block pointed
2737      * to by the `*p` memory pointer. */
2738 
2739     return ARCHIVE_OK;
2740 }
2741 
2742 static int process_block(struct archive_read* a) {
2743     const uint8_t* p;
2744     struct rar5* rar = get_context(a);
2745     int ret;
2746 
2747     /* If we don't have any data to be processed, this most probably means
2748      * we need to switch to the next volume. */
2749     if(rar->main.volume && rar->file.bytes_remaining == 0) {
2750         ret = advance_multivolume(a);
2751         if(ret != ARCHIVE_OK)
2752             return ret;
2753     }
2754 
2755     if(rar->cstate.block_parsing_finished) {
2756         ssize_t block_size;
2757 
2758         rar->cstate.block_parsing_finished = 0;
2759 
2760         /* The header size won't be bigger than 6 bytes. */
2761         if(!read_ahead(a, 6, &p)) {
2762             /* Failed to prefetch data block header. */
2763             return ARCHIVE_EOF;
2764         }
2765 
2766         /*
2767          * Read block_size by parsing block header. Validate the header by
2768          * calculating CRC byte stored inside the header. Size of the header is
2769          * not constant (block size can be stored either in 1 or 2 bytes),
2770          * that's why block size is left out from the `compressed_block_header`
2771          * structure and returned by `parse_block_header` as the second
2772          * argument. */
2773 
2774         ret = parse_block_header(a, p, &block_size, &rar->last_block_hdr);
2775         if(ret != ARCHIVE_OK)
2776             return ret;
2777 
2778         /* Skip block header. Next data is huffman tables, if present. */
2779         ssize_t to_skip = sizeof(struct compressed_block_header) +
2780             rar->last_block_hdr.block_flags.byte_count + 1;
2781 
2782         if(ARCHIVE_OK != consume(a, to_skip))
2783             return ARCHIVE_EOF;
2784 
2785         rar->file.bytes_remaining -= to_skip;
2786 
2787         /* The block size gives information about the whole block size, but
2788          * the block could be stored in split form when using multi-volume
2789          * archives. In this case, the block size will be bigger than the
2790          * actual data stored in this file. Remaining part of the data will
2791          * be in another file. */
2792 
2793         ssize_t cur_block_size =
2794             rar5_min(rar->file.bytes_remaining, block_size);
2795 
2796         if(block_size > rar->file.bytes_remaining) {
2797             /* If current blocks' size is bigger than our data size, this
2798              * means we have a multivolume archive. In this case, skip
2799              * all base headers until the end of the file, proceed to next
2800              * "partXXX.rar" volume, find its signature, skip all headers up
2801              * to the first FILE base header, and continue from there.
2802              *
2803              * Note that `merge_block` will update the `rar` context structure
2804              * quite extensively. */
2805 
2806             ret = merge_block(a, block_size, &p);
2807             if(ret != ARCHIVE_OK) {
2808                 return ret;
2809             }
2810 
2811             cur_block_size = block_size;
2812 
2813             /* Current stream pointer should be now directly *after* the
2814              * block that spanned through multiple archive files. `p` pointer
2815              * should have the data of the *whole* block (merged from
2816              * partial blocks stored in multiple archives files). */
2817         } else {
2818             rar->cstate.switch_multivolume = 0;
2819 
2820             /* Read the whole block size into memory. This can take up to
2821              * 8 megabytes of memory in theoretical cases. Might be worth to
2822              * optimize this and use a standard chunk of 4kb's. */
2823 
2824             if(!read_ahead(a, 4 + cur_block_size, &p)) {
2825                 /* Failed to prefetch block data. */
2826                 return ARCHIVE_EOF;
2827             }
2828         }
2829 
2830         rar->cstate.block_buf = p;
2831         rar->cstate.cur_block_size = cur_block_size;
2832 
2833         rar->bits.in_addr = 0;
2834         rar->bits.bit_addr = 0;
2835 
2836         if(rar->last_block_hdr.block_flags.is_table_present) {
2837             /* Load Huffman tables. */
2838             ret = parse_tables(a, rar, p);
2839             if(ret != ARCHIVE_OK) {
2840                 /* Error during decompression of Huffman tables. */
2841                 return ret;
2842             }
2843         }
2844     } else {
2845         p = rar->cstate.block_buf;
2846     }
2847 
2848     /* Uncompress the block, or a part of it, depending on how many bytes
2849      * will be generated by uncompressing the block.
2850      *
2851      * In case too many bytes will be generated, calling this function again
2852      * will resume the uncompression operation. */
2853     ret = do_uncompress_block(a, p);
2854     if(ret != ARCHIVE_OK) {
2855         return ret;
2856     }
2857 
2858     if(rar->cstate.block_parsing_finished &&
2859             rar->cstate.switch_multivolume == 0 &&
2860             rar->cstate.cur_block_size > 0)
2861     {
2862         /* If we're processing a normal block, consume the whole block. We
2863          * can do this because we've already read the whole block to memory.
2864          */
2865         if(ARCHIVE_OK != consume(a, rar->cstate.cur_block_size))
2866             return ARCHIVE_FATAL;
2867 
2868         rar->file.bytes_remaining -= rar->cstate.cur_block_size;
2869     } else if(rar->cstate.switch_multivolume) {
2870         /* Don't consume the block if we're doing multivolume processing.
2871          * The volume switching function will consume the proper count of
2872          * bytes instead. */
2873 
2874         rar->cstate.switch_multivolume = 0;
2875     }
2876 
2877     return ARCHIVE_OK;
2878 }
2879 
2880 /* Pops the `buf`, `size` and `offset` from the "data ready" stack.
2881  *
2882  * Returns ARCHIVE_OK when those arguments can be used, ARCHIVE_RETRY
2883  * when there is no data on the stack. */
2884 static int use_data(struct rar5* rar, const void** buf, size_t* size,
2885         int64_t* offset)
2886 {
2887     int i;
2888 
2889     for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
2890         struct data_ready *d = &rar->cstate.dready[i];
2891 
2892         if(d->used) {
2893             if(buf)    *buf = d->buf;
2894             if(size)   *size = d->size;
2895             if(offset) *offset = d->offset;
2896 
2897             d->used = 0;
2898             return ARCHIVE_OK;
2899         }
2900     }
2901 
2902     return ARCHIVE_RETRY;
2903 }
2904 
2905 /* Pushes the `buf`, `size` and `offset` arguments to the rar->cstate.dready
2906  * FIFO stack. Those values will be popped from this stack by the `use_data`
2907  * function. */
2908 static int push_data_ready(struct archive_read* a, struct rar5* rar,
2909         const uint8_t* buf, size_t size, int64_t offset)
2910 {
2911     int i;
2912 
2913     /* Don't push if we're in skip mode. This is needed because solid
2914      * streams need full processing even if we're skipping data. After fully
2915      * processing the stream, we need to discard the generated bytes, because
2916      * we're interested only in the side effect: building up the internal
2917      * window circular buffer. This window buffer will be used later during
2918      * unpacking of requested data. */
2919     if(rar->skip_mode)
2920         return ARCHIVE_OK;
2921 
2922     /* Sanity check. */
2923     if(offset != rar->file.last_offset + rar->file.last_size) {
2924         archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "Sanity "
2925                 "check error: output stream is not continuous");
2926         return ARCHIVE_FATAL;
2927     }
2928 
2929     for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
2930         struct data_ready* d = &rar->cstate.dready[i];
2931         if(!d->used) {
2932             d->used = 1;
2933             d->buf = buf;
2934             d->size = size;
2935             d->offset = offset;
2936 
2937             /* These fields are used only in sanity checking. */
2938             rar->file.last_offset = offset;
2939             rar->file.last_size = size;
2940 
2941             /* Calculate the checksum of this new block before submitting
2942              * data to libarchive's engine. */
2943             update_crc(rar, d->buf, d->size);
2944 
2945             return ARCHIVE_OK;
2946         }
2947     }
2948 
2949     /* Program counter will reach this code if the `rar->cstate.data_ready`
2950      * stack will be filled up so that no new entries will be allowed. The
2951      * code shouldn't allow such situation to occur. So we treat this case
2952      * as an internal error. */
2953 
2954     archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "Error: "
2955             "premature end of data_ready stack");
2956     return ARCHIVE_FATAL;
2957 }
2958 
2959 /* This function uncompresses the data that is stored in the <FILE> base
2960  * block.
2961  *
2962  * The FILE base block looks like this:
2963  *
2964  * <header><huffman tables><block_1><block_2>...<block_n>
2965  *
2966  * The <header> is a block header, that is parsed in parse_block_header().
2967  * It's a "compressed_block_header" structure, containing metadata needed
2968  * to know when we should stop looking for more <block_n> blocks.
2969  *
2970  * <huffman tables> contain data needed to set up the huffman tables, needed
2971  * for the actual decompression.
2972  *
2973  * Each <block_n> consists of series of literals:
2974  *
2975  * <literal><literal><literal>...<literal>
2976  *
2977  * Those literals generate the uncompression data. They operate on a circular
2978  * buffer, sometimes writing raw data into it, sometimes referencing
2979  * some previous data inside this buffer, and sometimes declaring a filter
2980  * that will need to be executed on the data stored in the circular buffer.
2981  * It all depends on the literal that is used.
2982  *
2983  * Sometimes blocks produce output data, sometimes they don't. For example, for
2984  * some huge files that use lots of filters, sometimes a block is filled with
2985  * only filter declaration literals. Such blocks won't produce any data in the
2986  * circular buffer.
2987  *
2988  * Sometimes blocks will produce 4 bytes of data, and sometimes 1 megabyte,
2989  * because a literal can reference previously decompressed data. For example,
2990  * there can be a literal that says: 'append a byte 0xFE here', and after
2991  * it another literal can say 'append 1 megabyte of data from circular buffer
2992  * offset 0x12345'. This is how RAR format handles compressing repeated
2993  * patterns.
2994  *
2995  * The RAR compressor creates those literals and the actual efficiency of
2996  * compression depends on what those literals are. The literals can also
2997  * be seen as a kind of a non-turing-complete virtual machine that simply
2998  * tells the decompressor what it should do.
2999  * */
3000 
3001 static int do_uncompress_file(struct archive_read* a) {
3002     struct rar5* rar = get_context(a);
3003     int ret;
3004     int64_t max_end_pos;
3005 
3006     if(!rar->cstate.initialized) {
3007         /* Don't perform full context reinitialization if we're processing
3008          * a solid archive. */
3009         if(!rar->main.solid || !rar->cstate.window_buf) {
3010             init_unpack(rar);
3011         }
3012 
3013         rar->cstate.initialized = 1;
3014     }
3015 
3016     if(rar->cstate.all_filters_applied == 1) {
3017         /* We use while(1) here, but standard case allows for just 1 iteration.
3018          * The loop will iterate if process_block() didn't generate any data at
3019          * all. This can happen if the block contains only filter definitions
3020          * (this is common in big files). */
3021 
3022         while(1) {
3023             ret = process_block(a);
3024             if(ret == ARCHIVE_EOF || ret == ARCHIVE_FATAL)
3025                 return ret;
3026 
3027             if(rar->cstate.last_write_ptr == rar->cstate.write_ptr) {
3028                 /* The block didn't generate any new data, so just process
3029                  * a new block. */
3030                 continue;
3031             }
3032 
3033             /* The block has generated some new data, so break the loop. */
3034             break;
3035         }
3036     }
3037 
3038     /* Try to run filters. If filters won't be applied, it means that
3039      * insufficient data was generated. */
3040     ret = apply_filters(a);
3041     if(ret == ARCHIVE_RETRY) {
3042         return ARCHIVE_OK;
3043     } else if(ret == ARCHIVE_FATAL) {
3044         return ARCHIVE_FATAL;
3045     }
3046 
3047     /* If apply_filters() will return ARCHIVE_OK, we can continue here. */
3048 
3049     if(cdeque_size(&rar->cstate.filters) > 0) {
3050         /* Check if we can write something before hitting first filter. */
3051         struct filter_info* flt;
3052 
3053         /* Get the block_start offset from the first filter. */
3054         if(CDE_OK != cdeque_front(&rar->cstate.filters, cdeque_filter_p(&flt)))
3055         {
3056             archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3057                     "Can't read first filter");
3058             return ARCHIVE_FATAL;
3059         }
3060 
3061         max_end_pos = rar5_min(flt->block_start, rar->cstate.write_ptr);
3062     } else {
3063         /* There are no filters defined, or all filters were applied. This
3064          * means we can just store the data without any postprocessing. */
3065         max_end_pos = rar->cstate.write_ptr;
3066     }
3067 
3068     if(max_end_pos == rar->cstate.last_write_ptr) {
3069         /* We can't write anything yet. The block uncompression function did
3070          * not generate enough data, and no filter can be applied. At the same
3071          * time we don't have any data that can be stored without filter
3072          * postprocessing. This means we need to wait for more data to be
3073          * generated, so we can apply the filters.
3074          *
3075          * Signal the caller that we need more data to be able to do anything.
3076          */
3077         return ARCHIVE_RETRY;
3078     } else {
3079         /* We can write the data before hitting the first filter. So let's
3080          * do it. The push_window_data() function will effectively return
3081          * the selected data block to the user application. */
3082         push_window_data(a, rar, rar->cstate.last_write_ptr, max_end_pos);
3083         rar->cstate.last_write_ptr = max_end_pos;
3084     }
3085 
3086     return ARCHIVE_OK;
3087 }
3088 
3089 static int uncompress_file(struct archive_read* a) {
3090     int ret;
3091 
3092     while(1) {
3093         /* Sometimes the uncompression function will return a 'retry' signal.
3094          * If this will happen, we have to retry the function. */
3095         ret = do_uncompress_file(a);
3096         if(ret != ARCHIVE_RETRY)
3097             return ret;
3098     }
3099 }
3100 
3101 
3102 static int do_unstore_file(struct archive_read* a,
3103                            struct rar5* rar,
3104                            const void** buf,
3105                            size_t* size,
3106                            int64_t* offset)
3107 {
3108     const uint8_t* p;
3109 
3110     if(rar->file.bytes_remaining == 0 && rar->main.volume > 0 &&
3111             rar->generic.split_after > 0)
3112     {
3113         int ret;
3114 
3115         rar->cstate.switch_multivolume = 1;
3116         ret = advance_multivolume(a);
3117         rar->cstate.switch_multivolume = 0;
3118 
3119         if(ret != ARCHIVE_OK) {
3120             /* Failed to advance to next multivolume archive file. */
3121             return ret;
3122         }
3123     }
3124 
3125     size_t to_read = rar5_min(rar->file.bytes_remaining, 64 * 1024);
3126     if(to_read == 0) {
3127         return ARCHIVE_EOF;
3128     }
3129 
3130     if(!read_ahead(a, to_read, &p)) {
3131         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "I/O error "
3132                 "when unstoring file");
3133         return ARCHIVE_FATAL;
3134     }
3135 
3136     if(ARCHIVE_OK != consume(a, to_read)) {
3137         return ARCHIVE_EOF;
3138     }
3139 
3140     if(buf)    *buf = p;
3141     if(size)   *size = to_read;
3142     if(offset) *offset = rar->cstate.last_unstore_ptr;
3143 
3144     rar->file.bytes_remaining -= to_read;
3145     rar->cstate.last_unstore_ptr += to_read;
3146 
3147     update_crc(rar, p, to_read);
3148     return ARCHIVE_OK;
3149 }
3150 
3151 static int do_unpack(struct archive_read* a, struct rar5* rar,
3152         const void** buf, size_t* size, int64_t* offset)
3153 {
3154     enum COMPRESSION_METHOD {
3155         STORE = 0, FASTEST = 1, FAST = 2, NORMAL = 3, GOOD = 4, BEST = 5
3156     };
3157 
3158     if(rar->file.service > 0) {
3159         return do_unstore_file(a, rar, buf, size, offset);
3160     } else {
3161         switch(rar->cstate.method) {
3162             case STORE:
3163                 return do_unstore_file(a, rar, buf, size, offset);
3164             case FASTEST:
3165                 /* fallthrough */
3166             case FAST:
3167                 /* fallthrough */
3168             case NORMAL:
3169                 /* fallthrough */
3170             case GOOD:
3171                 /* fallthrough */
3172             case BEST:
3173                 return uncompress_file(a);
3174             default:
3175                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3176                         "Compression method not supported: 0x%08x",
3177                         rar->cstate.method);
3178 
3179                 return ARCHIVE_FATAL;
3180         }
3181     }
3182 
3183 #if !defined WIN32
3184     /* Not reached. */
3185     return ARCHIVE_OK;
3186 #endif
3187 }
3188 
3189 static int verify_checksums(struct archive_read* a) {
3190     int verify_crc;
3191     struct rar5* rar = get_context(a);
3192 
3193     /* Check checksums only when actually unpacking the data. There's no need
3194      * to calculate checksum when we're skipping data in solid archives
3195      * (skipping in solid archives is the same thing as unpacking compressed
3196      * data and discarding the result). */
3197 
3198     if(!rar->skip_mode) {
3199         /* Always check checksums if we're not in skip mode */
3200         verify_crc = 1;
3201     } else {
3202         /* We can override the logic above with a compile-time option
3203          * NO_CRC_ON_SOLID_SKIP. This option is used during debugging, and it
3204          * will check checksums of unpacked data even when we're skipping it.
3205          */
3206 
3207 #if defined CHECK_CRC_ON_SOLID_SKIP
3208         /* Debug case */
3209         verify_crc = 1;
3210 #else
3211         /* Normal case */
3212         verify_crc = 0;
3213 #endif
3214     }
3215 
3216     if(verify_crc) {
3217         /* During unpacking, on each unpacked block we're calling the
3218          * update_crc() function. Since we are here, the unpacking process is
3219          * already over and we can check if calculated checksum (CRC32 or
3220          * BLAKE2sp) is the same as what is stored in the archive.
3221          */
3222         if(rar->file.stored_crc32 > 0) {
3223             /* Check CRC32 only when the file contains a CRC32 value for this
3224              * file. */
3225 
3226             if(rar->file.calculated_crc32 != rar->file.stored_crc32) {
3227                 /* Checksums do not match; the unpacked file is corrupted. */
3228 
3229                 DEBUG_CODE {
3230                     printf("Checksum error: CRC32 (was: %08x, expected: %08x)\n",
3231                         rar->file.calculated_crc32, rar->file.stored_crc32);
3232                 }
3233 
3234 #ifndef DONT_FAIL_ON_CRC_ERROR
3235                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3236                                   "Checksum error: CRC32");
3237                 return ARCHIVE_FATAL;
3238 #endif
3239             } else {
3240                 DEBUG_CODE {
3241                     printf("Checksum OK: CRC32 (%08x/%08x)\n",
3242                         rar->file.stored_crc32,
3243                         rar->file.calculated_crc32);
3244                 }
3245             }
3246         }
3247 
3248         if(rar->file.has_blake2 > 0) {
3249             /* BLAKE2sp is an optional checksum algorithm that is added to
3250              * RARv5 archives when using the `-htb` switch during creation of
3251              * archive.
3252              *
3253              * We now finalize the hash calculation by calling the `final`
3254              * function. This will generate the final hash value we can use to
3255              * compare it with the BLAKE2sp checksum that is stored in the
3256              * archive.
3257              *
3258              * The return value of this `final` function is not very helpful,
3259              * as it guards only against improper use. This is why we're
3260              * explicitly ignoring it. */
3261 
3262             uint8_t b2_buf[32];
3263             (void) blake2sp_final(&rar->file.b2state, b2_buf, 32);
3264 
3265             if(memcmp(&rar->file.blake2sp, b2_buf, 32) != 0) {
3266 #ifndef DONT_FAIL_ON_CRC_ERROR
3267                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3268                                   "Checksum error: BLAKE2");
3269 
3270                 return ARCHIVE_FATAL;
3271 #endif
3272             }
3273         }
3274     }
3275 
3276     /* Finalization for this file has been successfully completed. */
3277     return ARCHIVE_OK;
3278 }
3279 
3280 static int verify_global_checksums(struct archive_read* a) {
3281     return verify_checksums(a);
3282 }
3283 
3284 static int rar5_read_data(struct archive_read *a, const void **buff,
3285                                   size_t *size, int64_t *offset) {
3286     int ret;
3287     struct rar5* rar = get_context(a);
3288 
3289     if(!rar->skip_mode && (rar->cstate.last_write_ptr > rar->file.unpacked_size)) {
3290         archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3291                 "Unpacker has written too many bytes");
3292         return ARCHIVE_FATAL;
3293     }
3294 
3295     ret = use_data(rar, buff, size, offset);
3296     if(ret == ARCHIVE_OK) {
3297         return ret;
3298     }
3299 
3300     if(rar->file.eof == 1) {
3301         return ARCHIVE_EOF;
3302     }
3303 
3304     ret = do_unpack(a, rar, buff, size, offset);
3305     if(ret != ARCHIVE_OK) {
3306         return ret;
3307     }
3308 
3309     if(rar->file.bytes_remaining == 0 &&
3310             rar->cstate.last_write_ptr == rar->file.unpacked_size)
3311     {
3312         /* If all bytes of current file were processed, run finalization.
3313          *
3314          * Finalization will check checksum against proper values. If
3315          * some of the checksums will not match, we'll return an error
3316          * value in the last `archive_read_data` call to signal an error
3317          * to the user. */
3318 
3319         rar->file.eof = 1;
3320         return verify_global_checksums(a);
3321     }
3322 
3323     return ARCHIVE_OK;
3324 }
3325 
3326 static int rar5_read_data_skip(struct archive_read *a) {
3327     struct rar5* rar = get_context(a);
3328 
3329     if(rar->main.solid) {
3330         /* In solid archives, instead of skipping the data, we need to extract
3331          * it, and dispose the result. The side effect of this operation will
3332          * be setting up the initial window buffer state needed to be able to
3333          * extract the selected file. */
3334 
3335         int ret;
3336 
3337         /* Make sure to process all blocks in the compressed stream. */
3338         while(rar->file.bytes_remaining > 0) {
3339             /* Setting the "skip mode" will allow us to skip checksum checks
3340              * during data skipping. Checking the checksum of skipped data
3341              * isn't really necessary and it's only slowing things down.
3342              *
3343              * This is incremented instead of setting to 1 because this data
3344              * skipping function can be called recursively. */
3345             rar->skip_mode++;
3346 
3347             /* We're disposing 1 block of data, so we use triple NULLs in
3348              * arguments.
3349              */
3350             ret = rar5_read_data(a, NULL, NULL, NULL);
3351 
3352             /* Turn off "skip mode". */
3353             rar->skip_mode--;
3354 
3355             if(ret < 0) {
3356                 /* Propagate any potential error conditions to the caller. */
3357                 return ret;
3358             }
3359         }
3360     } else {
3361         /* In standard archives, we can just jump over the compressed stream.
3362          * Each file in non-solid archives starts from an empty window buffer.
3363          */
3364 
3365         if(ARCHIVE_OK != consume(a, rar->file.bytes_remaining)) {
3366             return ARCHIVE_FATAL;
3367         }
3368 
3369         rar->file.bytes_remaining = 0;
3370     }
3371 
3372     return ARCHIVE_OK;
3373 }
3374 
3375 static int64_t rar5_seek_data(struct archive_read *a, int64_t offset,
3376         int whence)
3377 {
3378     (void) a;
3379     (void) offset;
3380     (void) whence;
3381 
3382     /* We're a streaming unpacker, and we don't support seeking. */
3383 
3384     return ARCHIVE_FATAL;
3385 }
3386 
3387 static int rar5_cleanup(struct archive_read *a) {
3388     struct rar5* rar = get_context(a);
3389 
3390     if(rar->cstate.window_buf)
3391         free(rar->cstate.window_buf);
3392 
3393     if(rar->cstate.filtered_buf)
3394         free(rar->cstate.filtered_buf);
3395 
3396     if(rar->vol.push_buf)
3397         free(rar->vol.push_buf);
3398 
3399     free_filters(rar);
3400     cdeque_free(&rar->cstate.filters);
3401 
3402     free(rar);
3403     a->format->data = NULL;
3404 
3405     return ARCHIVE_OK;
3406 }
3407 
3408 static int rar5_capabilities(struct archive_read * a) {
3409     (void) a;
3410     return 0;
3411 }
3412 
3413 static int rar5_has_encrypted_entries(struct archive_read *_a) {
3414     (void) _a;
3415 
3416     /* Unsupported for now. */
3417     return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
3418 }
3419 
3420 static int rar5_init(struct rar5* rar) {
3421     ssize_t i;
3422 
3423     memset(rar, 0, sizeof(struct rar5));
3424 
3425     /* Decrypt the magic signature pattern. Check the comment near the
3426      * `rar5_signature` symbol to read the rationale behind this. */
3427 
3428     if(rar5_signature[0] == 243) {
3429         for(i = 0; i < rar5_signature_size; i++) {
3430             rar5_signature[i] ^= 0xA1;
3431         }
3432     }
3433 
3434     if(CDE_OK != cdeque_init(&rar->cstate.filters, 8192))
3435         return ARCHIVE_FATAL;
3436 
3437     return ARCHIVE_OK;
3438 }
3439 
3440 int archive_read_support_format_rar5(struct archive *_a) {
3441     struct archive_read* ar;
3442     int ret;
3443     struct rar5* rar;
3444 
3445     if(ARCHIVE_OK != (ret = get_archive_read(_a, &ar)))
3446         return ret;
3447 
3448     rar = malloc(sizeof(*rar));
3449     if(rar == NULL) {
3450         archive_set_error(&ar->archive, ENOMEM, "Can't allocate rar5 data");
3451         return ARCHIVE_FATAL;
3452     }
3453 
3454     if(ARCHIVE_OK != rar5_init(rar)) {
3455         archive_set_error(&ar->archive, ENOMEM, "Can't allocate rar5 filter "
3456                 "buffer");
3457         return ARCHIVE_FATAL;
3458     }
3459 
3460     ret = __archive_read_register_format(ar,
3461                                          rar,
3462                                          "rar5",
3463                                          rar5_bid,
3464                                          rar5_options,
3465                                          rar5_read_header,
3466                                          rar5_read_data,
3467                                          rar5_read_data_skip,
3468                                          rar5_seek_data,
3469                                          rar5_cleanup,
3470                                          rar5_capabilities,
3471                                          rar5_has_encrypted_entries);
3472 
3473     if(ret != ARCHIVE_OK) {
3474         (void) rar5_cleanup(ar);
3475     }
3476 
3477     return ret;
3478 }
3479