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