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