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