Deleted Added
full compact
archive_read_support_format_rar5.c (346104) archive_read_support_format_rar5.c (348607)
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

--- 19 unchanged lines hidden (view full) ---

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
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

--- 19 unchanged lines hidden (view full) ---

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
36
37#include "archive.h"
38#ifndef HAVE_ZLIB_H
39#include "archive_crc32.h"
40#endif
41
42#include "archive_entry.h"
43#include "archive_entry_locale.h"

--- 29 unchanged lines hidden (view full) ---

73 * want to put this magic sequence in each binary that uses libarchive, so
74 * applications that scan through the file for this marker won't trigger on
75 * this "false" one.
76 *
77 * The array itself is decrypted in `rar5_init` function. */
78
79static unsigned char rar5_signature[] = { 243, 192, 211, 128, 187, 166, 160, 161 };
80static const ssize_t rar5_signature_size = sizeof(rar5_signature);
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"

--- 29 unchanged lines hidden (view full) ---

76 * want to put this magic sequence in each binary that uses libarchive, so
77 * applications that scan through the file for this marker won't trigger on
78 * this "false" one.
79 *
80 * The array itself is decrypted in `rar5_init` function. */
81
82static unsigned char rar5_signature[] = { 243, 192, 211, 128, 187, 166, 160, 161 };
83static const ssize_t rar5_signature_size = sizeof(rar5_signature);
81/* static const size_t g_unpack_buf_chunk_size = 1024; */
82static const size_t g_unpack_window_size = 0x20000;
83
84static const size_t g_unpack_window_size = 0x20000;
85
86/* These could have been static const's, but they aren't, because of
87 * Visual Studio. */
88#define MAX_NAME_IN_CHARS 2048
89#define MAX_NAME_IN_BYTES (4 * MAX_NAME_IN_CHARS)
90
84struct file_header {
91struct file_header {
85 ssize_t bytes_remaining;
86 ssize_t unpacked_size;
87 int64_t last_offset; /* Used in sanity checks. */
88 int64_t last_size; /* Used in sanity checks. */
92 ssize_t bytes_remaining;
93 ssize_t unpacked_size;
94 int64_t last_offset; /* Used in sanity checks. */
95 int64_t last_size; /* Used in sanity checks. */
89
96
90 uint8_t solid : 1; /* Is this a solid stream? */
91 uint8_t service : 1; /* Is this file a service data? */
92 uint8_t eof : 1; /* Did we finish unpacking the file? */
97 uint8_t solid : 1; /* Is this a solid stream? */
98 uint8_t service : 1; /* Is this file a service data? */
99 uint8_t eof : 1; /* Did we finish unpacking the file? */
100 uint8_t dir : 1; /* Is this file entry a directory? */
93
101
94 /* Optional time fields. */
95 uint64_t e_mtime;
96 uint64_t e_ctime;
97 uint64_t e_atime;
98 uint32_t e_unix_ns;
102 /* Optional time fields. */
103 uint64_t e_mtime;
104 uint64_t e_ctime;
105 uint64_t e_atime;
106 uint32_t e_unix_ns;
99
107
100 /* Optional hash fields. */
101 uint32_t stored_crc32;
102 uint32_t calculated_crc32;
103 uint8_t blake2sp[32];
104 blake2sp_state b2state;
105 char has_blake2;
108 /* Optional hash fields. */
109 uint32_t stored_crc32;
110 uint32_t calculated_crc32;
111 uint8_t blake2sp[32];
112 blake2sp_state b2state;
113 char has_blake2;
114
115 /* Optional redir fields */
116 uint64_t redir_type;
117 uint64_t redir_flags;
106};
107
118};
119
120enum EXTRA {
121 EX_CRYPT = 0x01,
122 EX_HASH = 0x02,
123 EX_HTIME = 0x03,
124 EX_VERSION = 0x04,
125 EX_REDIR = 0x05,
126 EX_UOWNER = 0x06,
127 EX_SUBDATA = 0x07
128};
129
130#define REDIR_SYMLINK_IS_DIR 1
131
132enum REDIR_TYPE {
133 REDIR_TYPE_NONE = 0,
134 REDIR_TYPE_UNIXSYMLINK = 1,
135 REDIR_TYPE_WINSYMLINK = 2,
136 REDIR_TYPE_JUNCTION = 3,
137 REDIR_TYPE_HARDLINK = 4,
138 REDIR_TYPE_FILECOPY = 5,
139};
140
141#define OWNER_USER_NAME 0x01
142#define OWNER_GROUP_NAME 0x02
143#define OWNER_USER_UID 0x04
144#define OWNER_GROUP_GID 0x08
145#define OWNER_MAXNAMELEN 256
146
108enum FILTER_TYPE {
147enum FILTER_TYPE {
109 FILTER_DELTA = 0, /* Generic pattern. */
110 FILTER_E8 = 1, /* Intel x86 code. */
111 FILTER_E8E9 = 2, /* Intel x86 code. */
112 FILTER_ARM = 3, /* ARM code. */
113 FILTER_AUDIO = 4, /* Audio filter, not used in RARv5. */
114 FILTER_RGB = 5, /* Color palette, not used in RARv5. */
115 FILTER_ITANIUM = 6, /* Intel's Itanium, not used in RARv5. */
116 FILTER_PPM = 7, /* Predictive pattern matching, not used in RARv5. */
117 FILTER_NONE = 8,
148 FILTER_DELTA = 0, /* Generic pattern. */
149 FILTER_E8 = 1, /* Intel x86 code. */
150 FILTER_E8E9 = 2, /* Intel x86 code. */
151 FILTER_ARM = 3, /* ARM code. */
152 FILTER_AUDIO = 4, /* Audio filter, not used in RARv5. */
153 FILTER_RGB = 5, /* Color palette, not used in RARv5. */
154 FILTER_ITANIUM = 6, /* Intel's Itanium, not used in RARv5. */
155 FILTER_PPM = 7, /* Predictive pattern matching, not used in
156 RARv5. */
157 FILTER_NONE = 8,
118};
119
120struct filter_info {
158};
159
160struct filter_info {
121 int type;
122 int channels;
123 int pos_r;
161 int type;
162 int channels;
163 int pos_r;
124
164
125 int64_t block_start;
126 ssize_t block_length;
127 uint16_t width;
165 int64_t block_start;
166 ssize_t block_length;
167 uint16_t width;
128};
129
130struct data_ready {
168};
169
170struct data_ready {
131 char used;
132 const uint8_t* buf;
133 size_t size;
134 int64_t offset;
171 char used;
172 const uint8_t* buf;
173 size_t size;
174 int64_t offset;
135};
136
137struct cdeque {
175};
176
177struct cdeque {
138 uint16_t beg_pos;
139 uint16_t end_pos;
140 uint16_t cap_mask;
141 uint16_t size;
142 size_t* arr;
178 uint16_t beg_pos;
179 uint16_t end_pos;
180 uint16_t cap_mask;
181 uint16_t size;
182 size_t* arr;
143};
144
145struct decode_table {
183};
184
185struct decode_table {
146 uint32_t size;
147 int32_t decode_len[16];
148 uint32_t decode_pos[16];
149 uint32_t quick_bits;
150 uint8_t quick_len[1 << 10];
151 uint16_t quick_num[1 << 10];
152 uint16_t decode_num[306];
186 uint32_t size;
187 int32_t decode_len[16];
188 uint32_t decode_pos[16];
189 uint32_t quick_bits;
190 uint8_t quick_len[1 << 10];
191 uint16_t quick_num[1 << 10];
192 uint16_t decode_num[306];
153};
154
155struct comp_state {
193};
194
195struct comp_state {
156 /* Flag used to specify if unpacker needs to reinitialize the uncompression
157 * context. */
158 uint8_t initialized : 1;
196 /* Flag used to specify if unpacker needs to reinitialize the
197 uncompression context. */
198 uint8_t initialized : 1;
159
199
160 /* Flag used when applying filters. */
161 uint8_t all_filters_applied : 1;
200 /* Flag used when applying filters. */
201 uint8_t all_filters_applied : 1;
162
202
163 /* Flag used to skip file context reinitialization, used when unpacker is
164 * skipping through different multivolume archives. */
165 uint8_t switch_multivolume : 1;
203 /* Flag used to skip file context reinitialization, used when unpacker
204 is skipping through different multivolume archives. */
205 uint8_t switch_multivolume : 1;
166
206
167 /* Flag used to specify if unpacker has processed the whole data block or
168 * just a part of it. */
169 uint8_t block_parsing_finished : 1;
207 /* Flag used to specify if unpacker has processed the whole data block
208 or just a part of it. */
209 uint8_t block_parsing_finished : 1;
170
210
171 int notused : 4;
211 int notused : 4;
172
212
173 int flags; /* Uncompression flags. */
174 int method; /* Uncompression algorithm method. */
175 int version; /* Uncompression algorithm version. */
176 ssize_t window_size; /* Size of window_buf. */
177 uint8_t* window_buf; /* Circular buffer used during
178 decompression. */
179 uint8_t* filtered_buf; /* Buffer used when applying filters. */
180 const uint8_t* block_buf; /* Buffer used when merging blocks. */
181 size_t window_mask; /* Convenience field; window_size - 1. */
182 int64_t write_ptr; /* This amount of data has been unpacked in
183 the window buffer. */
184 int64_t last_write_ptr; /* This amount of data has been stored in
185 the output file. */
186 int64_t last_unstore_ptr; /* Counter of bytes extracted during
187 unstoring. This is separate from
188 last_write_ptr because of how SERVICE
189 base blocks are handled during skipping
190 in solid multiarchive archives. */
191 int64_t solid_offset; /* Additional offset inside the window
192 buffer, used in unpacking solid
193 archives. */
194 ssize_t cur_block_size; /* Size of current data block. */
195 int last_len; /* Flag used in lzss decompression. */
213 int flags; /* Uncompression flags. */
214 int method; /* Uncompression algorithm method. */
215 int version; /* Uncompression algorithm version. */
216 ssize_t window_size; /* Size of window_buf. */
217 uint8_t* window_buf; /* Circular buffer used during
218 decompression. */
219 uint8_t* filtered_buf; /* Buffer used when applying filters. */
220 const uint8_t* block_buf; /* Buffer used when merging blocks. */
221 size_t window_mask; /* Convenience field; window_size - 1. */
222 int64_t write_ptr; /* This amount of data has been unpacked
223 in the window buffer. */
224 int64_t last_write_ptr; /* This amount of data has been stored in
225 the output file. */
226 int64_t last_unstore_ptr; /* Counter of bytes extracted during
227 unstoring. This is separate from
228 last_write_ptr because of how SERVICE
229 base blocks are handled during skipping
230 in solid multiarchive archives. */
231 int64_t solid_offset; /* Additional offset inside the window
232 buffer, used in unpacking solid
233 archives. */
234 ssize_t cur_block_size; /* Size of current data block. */
235 int last_len; /* Flag used in lzss decompression. */
196
236
197 /* Decode tables used during lzss uncompression. */
237 /* Decode tables used during lzss uncompression. */
198
199#define HUFF_BC 20
238
239#define HUFF_BC 20
200 struct decode_table bd; /* huffman bit lengths */
240 struct decode_table bd; /* huffman bit lengths */
201#define HUFF_NC 306
241#define HUFF_NC 306
202 struct decode_table ld; /* literals */
242 struct decode_table ld; /* literals */
203#define HUFF_DC 64
243#define HUFF_DC 64
204 struct decode_table dd; /* distances */
244 struct decode_table dd; /* distances */
205#define HUFF_LDC 16
245#define HUFF_LDC 16
206 struct decode_table ldd; /* lower bits of distances */
246 struct decode_table ldd; /* lower bits of distances */
207#define HUFF_RC 44
247#define HUFF_RC 44
208 struct decode_table rd; /* repeating distances */
248 struct decode_table rd; /* repeating distances */
209#define HUFF_TABLE_SIZE (HUFF_NC + HUFF_DC + HUFF_RC + HUFF_LDC)
210
249#define HUFF_TABLE_SIZE (HUFF_NC + HUFF_DC + HUFF_RC + HUFF_LDC)
250
211 /* Circular deque for storing filters. */
212 struct cdeque filters;
213 int64_t last_block_start; /* Used for sanity checking. */
214 ssize_t last_block_length; /* Used for sanity checking. */
251 /* Circular deque for storing filters. */
252 struct cdeque filters;
253 int64_t last_block_start; /* Used for sanity checking. */
254 ssize_t last_block_length; /* Used for sanity checking. */
215
255
216 /* Distance cache used during lzss uncompression. */
217 int dist_cache[4];
256 /* Distance cache used during lzss uncompression. */
257 int dist_cache[4];
218
258
219 /* Data buffer stack. */
220 struct data_ready dready[2];
259 /* Data buffer stack. */
260 struct data_ready dready[2];
221};
222
223/* Bit reader state. */
224struct bit_reader {
261};
262
263/* Bit reader state. */
264struct bit_reader {
225 int8_t bit_addr; /* Current bit pointer inside current byte. */
226 int in_addr; /* Current byte pointer. */
265 int8_t bit_addr; /* Current bit pointer inside current byte. */
266 int in_addr; /* Current byte pointer. */
227};
228
229/* RARv5 block header structure. Use bf_* functions to get values from
230 * block_flags_u8 field. I.e. bf_byte_count, etc. */
231struct compressed_block_header {
267};
268
269/* RARv5 block header structure. Use bf_* functions to get values from
270 * block_flags_u8 field. I.e. bf_byte_count, etc. */
271struct compressed_block_header {
232 /* block_flags_u8 contain fields encoded in little-endian bitfield:
233 *
234 * - table present flag (shr 7, and 1),
235 * - last block flag (shr 6, and 1),
236 * - byte_count (shr 3, and 7),
237 * - bit_size (shr 0, and 7).
238 */
239 uint8_t block_flags_u8;
240 uint8_t block_cksum;
272 /* block_flags_u8 contain fields encoded in little-endian bitfield:
273 *
274 * - table present flag (shr 7, and 1),
275 * - last block flag (shr 6, and 1),
276 * - byte_count (shr 3, and 7),
277 * - bit_size (shr 0, and 7).
278 */
279 uint8_t block_flags_u8;
280 uint8_t block_cksum;
241};
242
243/* RARv5 main header structure. */
244struct main_header {
281};
282
283/* RARv5 main header structure. */
284struct main_header {
245 /* Does the archive contain solid streams? */
246 uint8_t solid : 1;
285 /* Does the archive contain solid streams? */
286 uint8_t solid : 1;
247
287
248 /* If this a multi-file archive? */
249 uint8_t volume : 1;
250 uint8_t endarc : 1;
251 uint8_t notused : 5;
288 /* If this a multi-file archive? */
289 uint8_t volume : 1;
290 uint8_t endarc : 1;
291 uint8_t notused : 5;
252
292
253 int vol_no;
293 unsigned int vol_no;
254};
255
256struct generic_header {
294};
295
296struct generic_header {
257 uint8_t split_after : 1;
258 uint8_t split_before : 1;
259 uint8_t padding : 6;
260 int size;
261 int last_header_id;
297 uint8_t split_after : 1;
298 uint8_t split_before : 1;
299 uint8_t padding : 6;
300 int size;
301 int last_header_id;
262};
263
264struct multivolume {
302};
303
304struct multivolume {
265 int expected_vol_no;
266 uint8_t* push_buf;
305 unsigned int expected_vol_no;
306 uint8_t* push_buf;
267};
268
269/* Main context structure. */
270struct rar5 {
307};
308
309/* Main context structure. */
310struct rar5 {
271 int header_initialized;
311 int header_initialized;
272
312
273 /* Set to 1 if current file is positioned AFTER the magic value
274 * of the archive file. This is used in header reading functions. */
275 int skipped_magic;
313 /* Set to 1 if current file is positioned AFTER the magic value
314 * of the archive file. This is used in header reading functions. */
315 int skipped_magic;
276
316
277 /* Set to not zero if we're in skip mode (either by calling rar5_data_skip
278 * function or when skipping over solid streams). Set to 0 when in
279 * extraction mode. This is used during checksum calculation functions. */
280 int skip_mode;
317 /* Set to not zero if we're in skip mode (either by calling
318 * rar5_data_skip function or when skipping over solid streams).
319 * Set to 0 when in * extraction mode. This is used during checksum
320 * calculation functions. */
321 int skip_mode;
281
322
282 /* An offset to QuickOpen list. This is not supported by this unpacker,
283 * because we're focusing on streaming interface. QuickOpen is designed
284 * to make things quicker for non-stream interfaces, so it's not our
285 * use case. */
286 uint64_t qlist_offset;
323 /* Set to not zero if we're in block merging mode (i.e. when switching
324 * to another file in multivolume archive, last block from 1st archive
325 * needs to be merged with 1st block from 2nd archive). This flag
326 * guards against recursive use of the merging function, which doesn't
327 * support recursive calls. */
328 int merge_mode;
287
329
288 /* An offset to additional Recovery data. This is not supported by this
289 * unpacker. Recovery data are additional Reed-Solomon codes that could
290 * be used to calculate bytes that are missing in archive or are
291 * corrupted. */
292 uint64_t rr_offset;
330 /* An offset to QuickOpen list. This is not supported by this unpacker,
331 * because we're focusing on streaming interface. QuickOpen is designed
332 * to make things quicker for non-stream interfaces, so it's not our
333 * use case. */
334 uint64_t qlist_offset;
293
335
294 /* Various context variables grouped to different structures. */
295 struct generic_header generic;
296 struct main_header main;
297 struct comp_state cstate;
298 struct file_header file;
299 struct bit_reader bits;
300 struct multivolume vol;
336 /* An offset to additional Recovery data. This is not supported by this
337 * unpacker. Recovery data are additional Reed-Solomon codes that could
338 * be used to calculate bytes that are missing in archive or are
339 * corrupted. */
340 uint64_t rr_offset;
301
341
302 /* The header of currently processed RARv5 block. Used in main
303 * decompression logic loop. */
304 struct compressed_block_header last_block_hdr;
342 /* Various context variables grouped to different structures. */
343 struct generic_header generic;
344 struct main_header main;
345 struct comp_state cstate;
346 struct file_header file;
347 struct bit_reader bits;
348 struct multivolume vol;
349
350 /* The header of currently processed RARv5 block. Used in main
351 * decompression logic loop. */
352 struct compressed_block_header last_block_hdr;
305};
306
307/* Forward function declarations. */
308
309static int verify_global_checksums(struct archive_read* a);
310static int rar5_read_data_skip(struct archive_read *a);
311static int push_data_ready(struct archive_read* a, struct rar5* rar,
353};
354
355/* Forward function declarations. */
356
357static int verify_global_checksums(struct archive_read* a);
358static int rar5_read_data_skip(struct archive_read *a);
359static int push_data_ready(struct archive_read* a, struct rar5* rar,
312 const uint8_t* buf, size_t size, int64_t offset);
360 const uint8_t* buf, size_t size, int64_t offset);
313
314/* CDE_xxx = Circular Double Ended (Queue) return values. */
315enum CDE_RETURN_VALUES {
361
362/* CDE_xxx = Circular Double Ended (Queue) return values. */
363enum CDE_RETURN_VALUES {
316 CDE_OK, CDE_ALLOC, CDE_PARAM, CDE_OUT_OF_BOUNDS,
364 CDE_OK, CDE_ALLOC, CDE_PARAM, CDE_OUT_OF_BOUNDS,
317};
318
319/* Clears the contents of this circular deque. */
320static void cdeque_clear(struct cdeque* d) {
365};
366
367/* Clears the contents of this circular deque. */
368static void cdeque_clear(struct cdeque* d) {
321 d->size = 0;
322 d->beg_pos = 0;
323 d->end_pos = 0;
369 d->size = 0;
370 d->beg_pos = 0;
371 d->end_pos = 0;
324}
325
326/* Creates a new circular deque object. Capacity must be power of 2: 8, 16, 32,
327 * 64, 256, etc. When the user will add another item above current capacity,
328 * the circular deque will overwrite the oldest entry. */
329static int cdeque_init(struct cdeque* d, int max_capacity_power_of_2) {
372}
373
374/* Creates a new circular deque object. Capacity must be power of 2: 8, 16, 32,
375 * 64, 256, etc. When the user will add another item above current capacity,
376 * the circular deque will overwrite the oldest entry. */
377static int cdeque_init(struct cdeque* d, int max_capacity_power_of_2) {
330 if(d == NULL || max_capacity_power_of_2 == 0)
331 return CDE_PARAM;
378 if(d == NULL || max_capacity_power_of_2 == 0)
379 return CDE_PARAM;
332
380
333 d->cap_mask = max_capacity_power_of_2 - 1;
334 d->arr = NULL;
381 d->cap_mask = max_capacity_power_of_2 - 1;
382 d->arr = NULL;
335
383
336 if((max_capacity_power_of_2 & d->cap_mask) > 0)
337 return CDE_PARAM;
384 if((max_capacity_power_of_2 & d->cap_mask) > 0)
385 return CDE_PARAM;
338
386
339 cdeque_clear(d);
340 d->arr = malloc(sizeof(void*) * max_capacity_power_of_2);
387 cdeque_clear(d);
388 d->arr = malloc(sizeof(void*) * max_capacity_power_of_2);
341
389
342 return d->arr ? CDE_OK : CDE_ALLOC;
390 return d->arr ? CDE_OK : CDE_ALLOC;
343}
344
345/* Return the current size (not capacity) of circular deque `d`. */
346static size_t cdeque_size(struct cdeque* d) {
391}
392
393/* Return the current size (not capacity) of circular deque `d`. */
394static size_t cdeque_size(struct cdeque* d) {
347 return d->size;
395 return d->size;
348}
349
350/* Returns the first element of current circular deque. Note that this function
351 * doesn't perform any bounds checking. If you need bounds checking, use
352 * `cdeque_front()` function instead. */
353static void cdeque_front_fast(struct cdeque* d, void** value) {
396}
397
398/* Returns the first element of current circular deque. Note that this function
399 * doesn't perform any bounds checking. If you need bounds checking, use
400 * `cdeque_front()` function instead. */
401static void cdeque_front_fast(struct cdeque* d, void** value) {
354 *value = (void*) d->arr[d->beg_pos];
402 *value = (void*) d->arr[d->beg_pos];
355}
356
357/* Returns the first element of current circular deque. This function
358 * performs bounds checking. */
359static int cdeque_front(struct cdeque* d, void** value) {
403}
404
405/* Returns the first element of current circular deque. This function
406 * performs bounds checking. */
407static int cdeque_front(struct cdeque* d, void** value) {
360 if(d->size > 0) {
361 cdeque_front_fast(d, value);
362 return CDE_OK;
363 } else
364 return CDE_OUT_OF_BOUNDS;
408 if(d->size > 0) {
409 cdeque_front_fast(d, value);
410 return CDE_OK;
411 } else
412 return CDE_OUT_OF_BOUNDS;
365}
366
367/* Pushes a new element into the end of this circular deque object. If current
368 * size will exceed capacity, the oldest element will be overwritten. */
369static int cdeque_push_back(struct cdeque* d, void* item) {
413}
414
415/* Pushes a new element into the end of this circular deque object. If current
416 * size will exceed capacity, the oldest element will be overwritten. */
417static int cdeque_push_back(struct cdeque* d, void* item) {
370 if(d == NULL)
371 return CDE_PARAM;
418 if(d == NULL)
419 return CDE_PARAM;
372
420
373 if(d->size == d->cap_mask + 1)
374 return CDE_OUT_OF_BOUNDS;
421 if(d->size == d->cap_mask + 1)
422 return CDE_OUT_OF_BOUNDS;
375
423
376 d->arr[d->end_pos] = (size_t) item;
377 d->end_pos = (d->end_pos + 1) & d->cap_mask;
378 d->size++;
424 d->arr[d->end_pos] = (size_t) item;
425 d->end_pos = (d->end_pos + 1) & d->cap_mask;
426 d->size++;
379
427
380 return CDE_OK;
428 return CDE_OK;
381}
382
383/* Pops a front element of this circular deque object and returns its value.
384 * This function doesn't perform any bounds checking. */
385static void cdeque_pop_front_fast(struct cdeque* d, void** value) {
429}
430
431/* Pops a front element of this circular deque object and returns its value.
432 * This function doesn't perform any bounds checking. */
433static void cdeque_pop_front_fast(struct cdeque* d, void** value) {
386 *value = (void*) d->arr[d->beg_pos];
387 d->beg_pos = (d->beg_pos + 1) & d->cap_mask;
388 d->size--;
434 *value = (void*) d->arr[d->beg_pos];
435 d->beg_pos = (d->beg_pos + 1) & d->cap_mask;
436 d->size--;
389}
390
391/* Pops a front element of this circular deque object and returns its value.
392 * This function performs bounds checking. */
393static int cdeque_pop_front(struct cdeque* d, void** value) {
437}
438
439/* Pops a front element of this circular deque object and returns its value.
440 * This function performs bounds checking. */
441static int cdeque_pop_front(struct cdeque* d, void** value) {
394 if(!d || !value)
395 return CDE_PARAM;
442 if(!d || !value)
443 return CDE_PARAM;
396
444
397 if(d->size == 0)
398 return CDE_OUT_OF_BOUNDS;
445 if(d->size == 0)
446 return CDE_OUT_OF_BOUNDS;
399
447
400 cdeque_pop_front_fast(d, value);
401 return CDE_OK;
448 cdeque_pop_front_fast(d, value);
449 return CDE_OK;
402}
403
404/* Convenience function to cast filter_info** to void **. */
405static void** cdeque_filter_p(struct filter_info** f) {
450}
451
452/* Convenience function to cast filter_info** to void **. */
453static void** cdeque_filter_p(struct filter_info** f) {
406 return (void**) (size_t) f;
454 return (void**) (size_t) f;
407}
408
409/* Convenience function to cast filter_info* to void *. */
410static void* cdeque_filter(struct filter_info* f) {
455}
456
457/* Convenience function to cast filter_info* to void *. */
458static void* cdeque_filter(struct filter_info* f) {
411 return (void**) (size_t) f;
459 return (void**) (size_t) f;
412}
413
460}
461
414/* Destroys this circular deque object. Deallocates the memory of the collection
415 * buffer, but doesn't deallocate the memory of any pointer passed to this
416 * deque as a value. */
462/* Destroys this circular deque object. Deallocates the memory of the
463 * collection buffer, but doesn't deallocate the memory of any pointer passed
464 * to this deque as a value. */
417static void cdeque_free(struct cdeque* d) {
465static void cdeque_free(struct cdeque* d) {
418 if(!d)
419 return;
466 if(!d)
467 return;
420
468
421 if(!d->arr)
422 return;
469 if(!d->arr)
470 return;
423
471
424 free(d->arr);
472 free(d->arr);
425
473
426 d->arr = NULL;
427 d->beg_pos = -1;
428 d->end_pos = -1;
429 d->cap_mask = 0;
474 d->arr = NULL;
475 d->beg_pos = -1;
476 d->end_pos = -1;
477 d->cap_mask = 0;
430}
431
432static inline
433uint8_t bf_bit_size(const struct compressed_block_header* hdr) {
478}
479
480static inline
481uint8_t bf_bit_size(const struct compressed_block_header* hdr) {
434 return hdr->block_flags_u8 & 7;
482 return hdr->block_flags_u8 & 7;
435}
436
437static inline
438uint8_t bf_byte_count(const struct compressed_block_header* hdr) {
483}
484
485static inline
486uint8_t bf_byte_count(const struct compressed_block_header* hdr) {
439 return (hdr->block_flags_u8 >> 3) & 7;
487 return (hdr->block_flags_u8 >> 3) & 7;
440}
441
442static inline
443uint8_t bf_is_table_present(const struct compressed_block_header* hdr) {
488}
489
490static inline
491uint8_t bf_is_table_present(const struct compressed_block_header* hdr) {
444 return (hdr->block_flags_u8 >> 7) & 1;
492 return (hdr->block_flags_u8 >> 7) & 1;
445}
446
447static inline struct rar5* get_context(struct archive_read* a) {
493}
494
495static inline struct rar5* get_context(struct archive_read* a) {
448 return (struct rar5*) a->format->data;
496 return (struct rar5*) a->format->data;
449}
450
451/* Convenience functions used by filter implementations. */
497}
498
499/* Convenience functions used by filter implementations. */
500static void circular_memcpy(uint8_t* dst, uint8_t* window, const uint64_t mask,
501 int64_t start, int64_t end)
502{
503 if((start & mask) > (end & mask)) {
504 ssize_t len1 = mask + 1 - (start & mask);
505 ssize_t len2 = end & mask;
452
506
507 memcpy(dst, &window[start & mask], len1);
508 memcpy(dst + len1, window, len2);
509 } else {
510 memcpy(dst, &window[start & mask], (size_t) (end - start));
511 }
512}
513
453static uint32_t read_filter_data(struct rar5* rar, uint32_t offset) {
514static uint32_t read_filter_data(struct rar5* rar, uint32_t offset) {
454 return archive_le32dec(&rar->cstate.window_buf[offset]);
515 uint8_t linear_buf[4];
516 circular_memcpy(linear_buf, rar->cstate.window_buf,
517 rar->cstate.window_mask, offset, offset + 4);
518 return archive_le32dec(linear_buf);
455}
456
457static void write_filter_data(struct rar5* rar, uint32_t offset,
519}
520
521static void write_filter_data(struct rar5* rar, uint32_t offset,
458 uint32_t value)
522 uint32_t value)
459{
523{
460 archive_le32enc(&rar->cstate.filtered_buf[offset], value);
524 archive_le32enc(&rar->cstate.filtered_buf[offset], value);
461}
462
525}
526
463static void circular_memcpy(uint8_t* dst, uint8_t* window, const int mask,
464 int64_t start, int64_t end)
465{
466 if((start & mask) > (end & mask)) {
467 ssize_t len1 = mask + 1 - (start & mask);
468 ssize_t len2 = end & mask;
469
470 memcpy(dst, &window[start & mask], len1);
471 memcpy(dst + len1, window, len2);
472 } else {
473 memcpy(dst, &window[start & mask], (size_t) (end - start));
474 }
475}
476
477/* Allocates a new filter descriptor and adds it to the filter array. */
478static struct filter_info* add_new_filter(struct rar5* rar) {
527/* Allocates a new filter descriptor and adds it to the filter array. */
528static struct filter_info* add_new_filter(struct rar5* rar) {
479 struct filter_info* f =
480 (struct filter_info*) calloc(1, sizeof(struct filter_info));
529 struct filter_info* f =
530 (struct filter_info*) calloc(1, sizeof(struct filter_info));
481
531
482 if(!f) {
483 return NULL;
484 }
532 if(!f) {
533 return NULL;
534 }
485
535
486 cdeque_push_back(&rar->cstate.filters, cdeque_filter(f));
487 return f;
536 cdeque_push_back(&rar->cstate.filters, cdeque_filter(f));
537 return f;
488}
489
490static int run_delta_filter(struct rar5* rar, struct filter_info* flt) {
538}
539
540static int run_delta_filter(struct rar5* rar, struct filter_info* flt) {
491 int i;
492 ssize_t dest_pos, src_pos = 0;
541 int i;
542 ssize_t dest_pos, src_pos = 0;
493
543
494 for(i = 0; i < flt->channels; i++) {
495 uint8_t prev_byte = 0;
496 for(dest_pos = i;
497 dest_pos < flt->block_length;
498 dest_pos += flt->channels)
499 {
500 uint8_t byte;
544 for(i = 0; i < flt->channels; i++) {
545 uint8_t prev_byte = 0;
546 for(dest_pos = i;
547 dest_pos < flt->block_length;
548 dest_pos += flt->channels)
549 {
550 uint8_t byte;
501
551
502 byte = rar->cstate.window_buf[(rar->cstate.solid_offset +
503 flt->block_start + src_pos) & rar->cstate.window_mask];
552 byte = rar->cstate.window_buf[
553 (rar->cstate.solid_offset + flt->block_start +
554 src_pos) & rar->cstate.window_mask];
504
555
505 prev_byte -= byte;
506 rar->cstate.filtered_buf[dest_pos] = prev_byte;
507 src_pos++;
508 }
509 }
556 prev_byte -= byte;
557 rar->cstate.filtered_buf[dest_pos] = prev_byte;
558 src_pos++;
559 }
560 }
510
561
511 return ARCHIVE_OK;
562 return ARCHIVE_OK;
512}
513
514static int run_e8e9_filter(struct rar5* rar, struct filter_info* flt,
563}
564
565static int run_e8e9_filter(struct rar5* rar, struct filter_info* flt,
515 int extended)
566 int extended)
516{
567{
517 const uint32_t file_size = 0x1000000;
518 ssize_t i;
568 const uint32_t file_size = 0x1000000;
569 ssize_t i;
519
570
520 const int mask = (int)rar->cstate.window_mask;
521 circular_memcpy(rar->cstate.filtered_buf,
522 rar->cstate.window_buf,
523 mask,
524 rar->cstate.solid_offset + flt->block_start,
525 rar->cstate.solid_offset + flt->block_start + flt->block_length);
571 circular_memcpy(rar->cstate.filtered_buf,
572 rar->cstate.window_buf, rar->cstate.window_mask,
573 rar->cstate.solid_offset + flt->block_start,
574 rar->cstate.solid_offset + flt->block_start + flt->block_length);
526
575
527 for(i = 0; i < flt->block_length - 4;) {
528 uint8_t b = rar->cstate.window_buf[(rar->cstate.solid_offset +
529 flt->block_start + i++) & mask];
576 for(i = 0; i < flt->block_length - 4;) {
577 uint8_t b = rar->cstate.window_buf[
578 (rar->cstate.solid_offset + flt->block_start +
579 i++) & rar->cstate.window_mask];
530
580
531 /* 0xE8 = x86's call <relative_addr_uint32> (function call)
532 * 0xE9 = x86's jmp <relative_addr_uint32> (unconditional jump) */
533 if(b == 0xE8 || (extended && b == 0xE9)) {
581 /*
582 * 0xE8 = x86's call <relative_addr_uint32> (function call)
583 * 0xE9 = x86's jmp <relative_addr_uint32> (unconditional jump)
584 */
585 if(b == 0xE8 || (extended && b == 0xE9)) {
534
586
535 uint32_t addr;
536 uint32_t offset = (i + flt->block_start) % file_size;
587 uint32_t addr;
588 uint32_t offset = (i + flt->block_start) % file_size;
537
589
538 addr = read_filter_data(rar, (uint32_t)(rar->cstate.solid_offset +
539 flt->block_start + i) & rar->cstate.window_mask);
590 addr = read_filter_data(rar,
591 (uint32_t)(rar->cstate.solid_offset +
592 flt->block_start + i) & rar->cstate.window_mask);
540
593
541 if(addr & 0x80000000) {
542 if(((addr + offset) & 0x80000000) == 0) {
543 write_filter_data(rar, (uint32_t)i, addr + file_size);
544 }
545 } else {
546 if((addr - file_size) & 0x80000000) {
547 uint32_t naddr = addr - offset;
548 write_filter_data(rar, (uint32_t)i, naddr);
549 }
550 }
594 if(addr & 0x80000000) {
595 if(((addr + offset) & 0x80000000) == 0) {
596 write_filter_data(rar, (uint32_t)i,
597 addr + file_size);
598 }
599 } else {
600 if((addr - file_size) & 0x80000000) {
601 uint32_t naddr = addr - offset;
602 write_filter_data(rar, (uint32_t)i,
603 naddr);
604 }
605 }
551
606
552 i += 4;
553 }
554 }
607 i += 4;
608 }
609 }
555
610
556 return ARCHIVE_OK;
611 return ARCHIVE_OK;
557}
558
559static int run_arm_filter(struct rar5* rar, struct filter_info* flt) {
612}
613
614static int run_arm_filter(struct rar5* rar, struct filter_info* flt) {
560 ssize_t i = 0;
561 uint32_t offset;
562 const int mask = (int)rar->cstate.window_mask;
615 ssize_t i = 0;
616 uint32_t offset;
563
617
564 circular_memcpy(rar->cstate.filtered_buf,
565 rar->cstate.window_buf,
566 mask,
567 rar->cstate.solid_offset + flt->block_start,
568 rar->cstate.solid_offset + flt->block_start + flt->block_length);
618 circular_memcpy(rar->cstate.filtered_buf,
619 rar->cstate.window_buf, rar->cstate.window_mask,
620 rar->cstate.solid_offset + flt->block_start,
621 rar->cstate.solid_offset + flt->block_start + flt->block_length);
569
622
570 for(i = 0; i < flt->block_length - 3; i += 4) {
571 uint8_t* b = &rar->cstate.window_buf[(rar->cstate.solid_offset +
572 flt->block_start + i) & mask];
623 for(i = 0; i < flt->block_length - 3; i += 4) {
624 uint8_t* b = &rar->cstate.window_buf[
625 (rar->cstate.solid_offset +
626 flt->block_start + i) & rar->cstate.window_mask];
573
627
574 if(b[3] == 0xEB) {
575 /* 0xEB = ARM's BL (branch + link) instruction. */
576 offset = read_filter_data(rar, (rar->cstate.solid_offset +
577 flt->block_start + i) & mask) & 0x00ffffff;
628 if(b[3] == 0xEB) {
629 /* 0xEB = ARM's BL (branch + link) instruction. */
630 offset = read_filter_data(rar,
631 (rar->cstate.solid_offset + flt->block_start + i) &
632 rar->cstate.window_mask) & 0x00ffffff;
578
633
579 offset -= (uint32_t) ((i + flt->block_start) / 4);
580 offset = (offset & 0x00ffffff) | 0xeb000000;
581 write_filter_data(rar, (uint32_t)i, offset);
582 }
583 }
634 offset -= (uint32_t) ((i + flt->block_start) / 4);
635 offset = (offset & 0x00ffffff) | 0xeb000000;
636 write_filter_data(rar, (uint32_t)i, offset);
637 }
638 }
584
639
585 return ARCHIVE_OK;
640 return ARCHIVE_OK;
586}
587
588static int run_filter(struct archive_read* a, struct filter_info* flt) {
641}
642
643static int run_filter(struct archive_read* a, struct filter_info* flt) {
589 int ret;
590 struct rar5* rar = get_context(a);
644 int ret;
645 struct rar5* rar = get_context(a);
591
646
592 free(rar->cstate.filtered_buf);
647 free(rar->cstate.filtered_buf);
593
648
594 rar->cstate.filtered_buf = malloc(flt->block_length);
595 if(!rar->cstate.filtered_buf) {
596 archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for "
597 "filter data.");
598 return ARCHIVE_FATAL;
599 }
649 rar->cstate.filtered_buf = malloc(flt->block_length);
650 if(!rar->cstate.filtered_buf) {
651 archive_set_error(&a->archive, ENOMEM,
652 "Can't allocate memory for filter data.");
653 return ARCHIVE_FATAL;
654 }
600
655
601 switch(flt->type) {
602 case FILTER_DELTA:
603 ret = run_delta_filter(rar, flt);
604 break;
656 switch(flt->type) {
657 case FILTER_DELTA:
658 ret = run_delta_filter(rar, flt);
659 break;
605
660
606 case FILTER_E8:
607 /* fallthrough */
608 case FILTER_E8E9:
609 ret = run_e8e9_filter(rar, flt, flt->type == FILTER_E8E9);
610 break;
661 case FILTER_E8:
662 /* fallthrough */
663 case FILTER_E8E9:
664 ret = run_e8e9_filter(rar, flt,
665 flt->type == FILTER_E8E9);
666 break;
611
667
612 case FILTER_ARM:
613 ret = run_arm_filter(rar, flt);
614 break;
668 case FILTER_ARM:
669 ret = run_arm_filter(rar, flt);
670 break;
615
671
616 default:
617 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
618 "Unsupported filter type: 0x%02x", flt->type);
619 return ARCHIVE_FATAL;
620 }
672 default:
673 archive_set_error(&a->archive,
674 ARCHIVE_ERRNO_FILE_FORMAT,
675 "Unsupported filter type: 0x%x", flt->type);
676 return ARCHIVE_FATAL;
677 }
621
678
622 if(ret != ARCHIVE_OK) {
623 /* Filter has failed. */
624 return ret;
625 }
679 if(ret != ARCHIVE_OK) {
680 /* Filter has failed. */
681 return ret;
682 }
626
683
627 if(ARCHIVE_OK != push_data_ready(a, rar, rar->cstate.filtered_buf,
628 flt->block_length, rar->cstate.last_write_ptr))
629 {
630 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
631 "Stack overflow when submitting unpacked data");
684 if(ARCHIVE_OK != push_data_ready(a, rar, rar->cstate.filtered_buf,
685 flt->block_length, rar->cstate.last_write_ptr))
686 {
687 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
688 "Stack overflow when submitting unpacked data");
632
689
633 return ARCHIVE_FATAL;
634 }
690 return ARCHIVE_FATAL;
691 }
635
692
636 rar->cstate.last_write_ptr += flt->block_length;
637 return ARCHIVE_OK;
693 rar->cstate.last_write_ptr += flt->block_length;
694 return ARCHIVE_OK;
638}
639
640/* The `push_data` function submits the selected data range to the user.
641 * Next call of `use_data` will use the pointer, size and offset arguments
642 * that are specified here. These arguments are pushed to the FIFO stack here,
643 * and popped from the stack by the `use_data` function. */
644static void push_data(struct archive_read* a, struct rar5* rar,
695}
696
697/* The `push_data` function submits the selected data range to the user.
698 * Next call of `use_data` will use the pointer, size and offset arguments
699 * that are specified here. These arguments are pushed to the FIFO stack here,
700 * and popped from the stack by the `use_data` function. */
701static void push_data(struct archive_read* a, struct rar5* rar,
645 const uint8_t* buf, int64_t idx_begin, int64_t idx_end)
702 const uint8_t* buf, int64_t idx_begin, int64_t idx_end)
646{
703{
647 const int wmask = (int)rar->cstate.window_mask;
648 const ssize_t solid_write_ptr = (rar->cstate.solid_offset +
649 rar->cstate.last_write_ptr) & wmask;
704 const uint64_t wmask = rar->cstate.window_mask;
705 const ssize_t solid_write_ptr = (rar->cstate.solid_offset +
706 rar->cstate.last_write_ptr) & wmask;
650
707
651 idx_begin += rar->cstate.solid_offset;
652 idx_end += rar->cstate.solid_offset;
708 idx_begin += rar->cstate.solid_offset;
709 idx_end += rar->cstate.solid_offset;
653
710
654 /* Check if our unpacked data is wrapped inside the window circular buffer.
655 * If it's not wrapped, it can be copied out by using a single memcpy,
656 * but when it's wrapped, we need to copy the first part with one
657 * memcpy, and the second part with another memcpy. */
711 /* Check if our unpacked data is wrapped inside the window circular
712 * buffer. If it's not wrapped, it can be copied out by using
713 * a single memcpy, but when it's wrapped, we need to copy the first
714 * part with one memcpy, and the second part with another memcpy. */
658
715
659 if((idx_begin & wmask) > (idx_end & wmask)) {
660 /* The data is wrapped (begin offset sis bigger than end offset). */
661 const ssize_t frag1_size = rar->cstate.window_size - (idx_begin & wmask);
662 const ssize_t frag2_size = idx_end & wmask;
716 if((idx_begin & wmask) > (idx_end & wmask)) {
717 /* The data is wrapped (begin offset sis bigger than end
718 * offset). */
719 const ssize_t frag1_size = rar->cstate.window_size -
720 (idx_begin & wmask);
721 const ssize_t frag2_size = idx_end & wmask;
663
722
664 /* Copy the first part of the buffer first. */
665 push_data_ready(a, rar, buf + solid_write_ptr, frag1_size,
666 rar->cstate.last_write_ptr);
723 /* Copy the first part of the buffer first. */
724 push_data_ready(a, rar, buf + solid_write_ptr, frag1_size,
725 rar->cstate.last_write_ptr);
667
726
668 /* Copy the second part of the buffer. */
669 push_data_ready(a, rar, buf, frag2_size,
670 rar->cstate.last_write_ptr + frag1_size);
727 /* Copy the second part of the buffer. */
728 push_data_ready(a, rar, buf, frag2_size,
729 rar->cstate.last_write_ptr + frag1_size);
671
730
672 rar->cstate.last_write_ptr += frag1_size + frag2_size;
673 } else {
674 /* Data is not wrapped, so we can just use one call to copy the
675 * data. */
676 push_data_ready(a, rar,
677 buf + solid_write_ptr,
678 (idx_end - idx_begin) & wmask,
679 rar->cstate.last_write_ptr);
731 rar->cstate.last_write_ptr += frag1_size + frag2_size;
732 } else {
733 /* Data is not wrapped, so we can just use one call to copy the
734 * data. */
735 push_data_ready(a, rar,
736 buf + solid_write_ptr, (idx_end - idx_begin) & wmask,
737 rar->cstate.last_write_ptr);
680
738
681 rar->cstate.last_write_ptr += idx_end - idx_begin;
682 }
739 rar->cstate.last_write_ptr += idx_end - idx_begin;
740 }
683}
684
685/* Convenience function that submits the data to the user. It uses the
686 * unpack window buffer as a source location. */
687static void push_window_data(struct archive_read* a, struct rar5* rar,
741}
742
743/* Convenience function that submits the data to the user. It uses the
744 * unpack window buffer as a source location. */
745static void push_window_data(struct archive_read* a, struct rar5* rar,
688 int64_t idx_begin, int64_t idx_end)
746 int64_t idx_begin, int64_t idx_end)
689{
747{
690 push_data(a, rar, rar->cstate.window_buf, idx_begin, idx_end);
748 push_data(a, rar, rar->cstate.window_buf, idx_begin, idx_end);
691}
692
693static int apply_filters(struct archive_read* a) {
749}
750
751static int apply_filters(struct archive_read* a) {
694 struct filter_info* flt;
695 struct rar5* rar = get_context(a);
696 int ret;
752 struct filter_info* flt;
753 struct rar5* rar = get_context(a);
754 int ret;
697
755
698 rar->cstate.all_filters_applied = 0;
756 rar->cstate.all_filters_applied = 0;
699
757
700 /* Get the first filter that can be applied to our data. The data needs to
701 * be fully unpacked before the filter can be run. */
702 if(CDE_OK ==
703 cdeque_front(&rar->cstate.filters, cdeque_filter_p(&flt)))
704 {
705 /* Check if our unpacked data fully covers this filter's range. */
706 if(rar->cstate.write_ptr > flt->block_start &&
707 rar->cstate.write_ptr >= flt->block_start + flt->block_length)
708 {
709 /* Check if we have some data pending to be written right before
710 * the filter's start offset. */
711 if(rar->cstate.last_write_ptr == flt->block_start) {
712 /* Run the filter specified by descriptor `flt`. */
713 ret = run_filter(a, flt);
714 if(ret != ARCHIVE_OK) {
715 /* Filter failure, return error. */
716 return ret;
717 }
758 /* Get the first filter that can be applied to our data. The data
759 * needs to be fully unpacked before the filter can be run. */
760 if(CDE_OK == cdeque_front(&rar->cstate.filters,
761 cdeque_filter_p(&flt))) {
762 /* Check if our unpacked data fully covers this filter's
763 * range. */
764 if(rar->cstate.write_ptr > flt->block_start &&
765 rar->cstate.write_ptr >= flt->block_start +
766 flt->block_length) {
767 /* Check if we have some data pending to be written
768 * right before the filter's start offset. */
769 if(rar->cstate.last_write_ptr == flt->block_start) {
770 /* Run the filter specified by descriptor
771 * `flt`. */
772 ret = run_filter(a, flt);
773 if(ret != ARCHIVE_OK) {
774 /* Filter failure, return error. */
775 return ret;
776 }
718
777
719 /* Filter descriptor won't be needed anymore after it's used,
720 * so remove it from the filter list and free its memory. */
721 (void) cdeque_pop_front(&rar->cstate.filters,
722 cdeque_filter_p(&flt));
778 /* Filter descriptor won't be needed anymore
779 * after it's used, * so remove it from the
780 * filter list and free its memory. */
781 (void) cdeque_pop_front(&rar->cstate.filters,
782 cdeque_filter_p(&flt));
723
783
724 free(flt);
725 } else {
726 /* We can't run filters yet, dump the memory right before the
727 * filter. */
728 push_window_data(a, rar, rar->cstate.last_write_ptr,
729 flt->block_start);
730 }
784 free(flt);
785 } else {
786 /* We can't run filters yet, dump the memory
787 * right before the filter. */
788 push_window_data(a, rar,
789 rar->cstate.last_write_ptr,
790 flt->block_start);
791 }
731
792
732 /* Return 'filter applied or not needed' state to the caller. */
733 return ARCHIVE_RETRY;
734 }
735 }
793 /* Return 'filter applied or not needed' state to the
794 * caller. */
795 return ARCHIVE_RETRY;
796 }
797 }
736
798
737 rar->cstate.all_filters_applied = 1;
738 return ARCHIVE_OK;
799 rar->cstate.all_filters_applied = 1;
800 return ARCHIVE_OK;
739}
740
741static void dist_cache_push(struct rar5* rar, int value) {
801}
802
803static void dist_cache_push(struct rar5* rar, int value) {
742 int* q = rar->cstate.dist_cache;
804 int* q = rar->cstate.dist_cache;
743
805
744 q[3] = q[2];
745 q[2] = q[1];
746 q[1] = q[0];
747 q[0] = value;
806 q[3] = q[2];
807 q[2] = q[1];
808 q[1] = q[0];
809 q[0] = value;
748}
749
750static int dist_cache_touch(struct rar5* rar, int idx) {
810}
811
812static int dist_cache_touch(struct rar5* rar, int idx) {
751 int* q = rar->cstate.dist_cache;
752 int i, dist = q[idx];
813 int* q = rar->cstate.dist_cache;
814 int i, dist = q[idx];
753
815
754 for(i = idx; i > 0; i--)
755 q[i] = q[i - 1];
816 for(i = idx; i > 0; i--)
817 q[i] = q[i - 1];
756
818
757 q[0] = dist;
758 return dist;
819 q[0] = dist;
820 return dist;
759}
760
761static void free_filters(struct rar5* rar) {
821}
822
823static void free_filters(struct rar5* rar) {
762 struct cdeque* d = &rar->cstate.filters;
824 struct cdeque* d = &rar->cstate.filters;
763
825
764 /* Free any remaining filters. All filters should be naturally consumed by
765 * the unpacking function, so remaining filters after unpacking normally
766 * mean that unpacking wasn't successful. But still of course we shouldn't
767 * leak memory in such case. */
826 /* Free any remaining filters. All filters should be naturally
827 * consumed by the unpacking function, so remaining filters after
828 * unpacking normally mean that unpacking wasn't successful.
829 * But still of course we shouldn't leak memory in such case. */
768
830
769 /* cdeque_size() is a fast operation, so we can use it as a loop
770 * expression. */
771 while(cdeque_size(d) > 0) {
772 struct filter_info* f = NULL;
831 /* cdeque_size() is a fast operation, so we can use it as a loop
832 * expression. */
833 while(cdeque_size(d) > 0) {
834 struct filter_info* f = NULL;
773
835
774 /* Pop_front will also decrease the collection's size. */
775 if (CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)))
776 free(f);
777 }
836 /* Pop_front will also decrease the collection's size. */
837 if (CDE_OK == cdeque_pop_front(d, cdeque_filter_p(&f)))
838 free(f);
839 }
778
840
779 cdeque_clear(d);
841 cdeque_clear(d);
780
842
781 /* Also clear out the variables needed for sanity checking. */
782 rar->cstate.last_block_start = 0;
783 rar->cstate.last_block_length = 0;
843 /* Also clear out the variables needed for sanity checking. */
844 rar->cstate.last_block_start = 0;
845 rar->cstate.last_block_length = 0;
784}
785
786static void reset_file_context(struct rar5* rar) {
846}
847
848static void reset_file_context(struct rar5* rar) {
787 memset(&rar->file, 0, sizeof(rar->file));
788 blake2sp_init(&rar->file.b2state, 32);
849 memset(&rar->file, 0, sizeof(rar->file));
850 blake2sp_init(&rar->file.b2state, 32);
789
851
790 if(rar->main.solid) {
791 rar->cstate.solid_offset += rar->cstate.write_ptr;
792 } else {
793 rar->cstate.solid_offset = 0;
794 }
852 if(rar->main.solid) {
853 rar->cstate.solid_offset += rar->cstate.write_ptr;
854 } else {
855 rar->cstate.solid_offset = 0;
856 }
795
857
796 rar->cstate.write_ptr = 0;
797 rar->cstate.last_write_ptr = 0;
798 rar->cstate.last_unstore_ptr = 0;
858 rar->cstate.write_ptr = 0;
859 rar->cstate.last_write_ptr = 0;
860 rar->cstate.last_unstore_ptr = 0;
799
861
800 free_filters(rar);
862 rar->file.redir_type = REDIR_TYPE_NONE;
863 rar->file.redir_flags = 0;
864
865 free_filters(rar);
801}
802
803static inline int get_archive_read(struct archive* a,
866}
867
868static inline int get_archive_read(struct archive* a,
804 struct archive_read** ar)
869 struct archive_read** ar)
805{
870{
806 *ar = (struct archive_read*) a;
807 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
808 "archive_read_support_format_rar5");
871 *ar = (struct archive_read*) a;
872 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
873 "archive_read_support_format_rar5");
809
874
810 return ARCHIVE_OK;
875 return ARCHIVE_OK;
811}
812
813static int read_ahead(struct archive_read* a, size_t how_many,
876}
877
878static int read_ahead(struct archive_read* a, size_t how_many,
814 const uint8_t** ptr)
879 const uint8_t** ptr)
815{
880{
816 if(!ptr)
817 return 0;
881 if(!ptr)
882 return 0;
818
883
819 ssize_t avail = -1;
820 *ptr = __archive_read_ahead(a, how_many, &avail);
884 ssize_t avail = -1;
885 *ptr = __archive_read_ahead(a, how_many, &avail);
886 if(*ptr == NULL) {
887 return 0;
888 }
821
889
822 if(*ptr == NULL) {
823 return 0;
824 }
825
826 return 1;
890 return 1;
827}
828
829static int consume(struct archive_read* a, int64_t how_many) {
891}
892
893static int consume(struct archive_read* a, int64_t how_many) {
830 int ret;
894 int ret;
831
895
832 ret =
833 how_many == __archive_read_consume(a, how_many)
834 ? ARCHIVE_OK
835 : ARCHIVE_FATAL;
896 ret = how_many == __archive_read_consume(a, how_many)
897 ? ARCHIVE_OK
898 : ARCHIVE_FATAL;
836
899
837 return ret;
900 return ret;
838}
839
840/**
841 * Read a RAR5 variable sized numeric value. This value will be stored in
842 * `pvalue`. The `pvalue_len` argument points to a variable that will receive
843 * the byte count that was consumed in order to decode the `pvalue` value, plus
844 * one.
845 *

--- 4 unchanged lines hidden (view full) ---

850 * is NULL, this consuming operation is done automatically.
851 *
852 * Returns 1 if *pvalue was successfully read.
853 * Returns 0 if there was an error. In this case, *pvalue contains an
854 * invalid value.
855 */
856
857static int read_var(struct archive_read* a, uint64_t* pvalue,
901}
902
903/**
904 * Read a RAR5 variable sized numeric value. This value will be stored in
905 * `pvalue`. The `pvalue_len` argument points to a variable that will receive
906 * the byte count that was consumed in order to decode the `pvalue` value, plus
907 * one.
908 *

--- 4 unchanged lines hidden (view full) ---

913 * is NULL, this consuming operation is done automatically.
914 *
915 * Returns 1 if *pvalue was successfully read.
916 * Returns 0 if there was an error. In this case, *pvalue contains an
917 * invalid value.
918 */
919
920static int read_var(struct archive_read* a, uint64_t* pvalue,
858 uint64_t* pvalue_len)
921 uint64_t* pvalue_len)
859{
922{
860 uint64_t result = 0;
861 size_t shift, i;
862 const uint8_t* p;
863 uint8_t b;
923 uint64_t result = 0;
924 size_t shift, i;
925 const uint8_t* p;
926 uint8_t b;
864
927
865 /* We will read maximum of 8 bytes. We don't have to handle the situation
866 * to read the RAR5 variable-sized value stored at the end of the file,
867 * because such situation will never happen. */
868 if(!read_ahead(a, 8, &p))
869 return 0;
928 /* We will read maximum of 8 bytes. We don't have to handle the
929 * situation to read the RAR5 variable-sized value stored at the end of
930 * the file, because such situation will never happen. */
931 if(!read_ahead(a, 8, &p))
932 return 0;
870
933
871 for(shift = 0, i = 0; i < 8; i++, shift += 7) {
872 b = p[i];
934 for(shift = 0, i = 0; i < 8; i++, shift += 7) {
935 b = p[i];
873
936
874 /* Strip the MSB from the input byte and add the resulting number
875 * to the `result`. */
876 result += (b & (uint64_t)0x7F) << shift;
937 /* Strip the MSB from the input byte and add the resulting
938 * number to the `result`. */
939 result += (b & (uint64_t)0x7F) << shift;
877
940
878 /* MSB set to 1 means we need to continue decoding process. MSB set
879 * to 0 means we're done.
880 *
881 * This conditional checks for the second case. */
882 if((b & 0x80) == 0) {
883 if(pvalue) {
884 *pvalue = result;
885 }
941 /* MSB set to 1 means we need to continue decoding process.
942 * MSB set to 0 means we're done.
943 *
944 * This conditional checks for the second case. */
945 if((b & 0x80) == 0) {
946 if(pvalue) {
947 *pvalue = result;
948 }
886
949
887 /* If the caller has passed the `pvalue_len` pointer, store the
888 * number of consumed bytes in it and do NOT consume those bytes,
889 * since the caller has all the information it needs to perform
890 * the consuming process itself. */
891 if(pvalue_len) {
892 *pvalue_len = 1 + i;
893 } else {
894 /* If the caller did not provide the `pvalue_len` pointer,
895 * it will not have the possibility to advance the file
896 * pointer, because it will not know how many bytes it needs
897 * to consume. This is why we handle such situation here
898 * automatically. */
899 if(ARCHIVE_OK != consume(a, 1 + i)) {
900 return 0;
901 }
902 }
950 /* If the caller has passed the `pvalue_len` pointer,
951 * store the number of consumed bytes in it and do NOT
952 * consume those bytes, since the caller has all the
953 * information it needs to perform */
954 if(pvalue_len) {
955 *pvalue_len = 1 + i;
956 } else {
957 /* If the caller did not provide the
958 * `pvalue_len` pointer, it will not have the
959 * possibility to advance the file pointer,
960 * because it will not know how many bytes it
961 * needs to consume. This is why we handle
962 * such situation here automatically. */
963 if(ARCHIVE_OK != consume(a, 1 + i)) {
964 return 0;
965 }
966 }
903
967
904 /* End of decoding process, return success. */
905 return 1;
906 }
907 }
968 /* End of decoding process, return success. */
969 return 1;
970 }
971 }
908
972
909 /* The decoded value takes the maximum number of 8 bytes. It's a maximum
910 * number of bytes, so end decoding process here even if the first bit
911 * of last byte is 1. */
912 if(pvalue) {
913 *pvalue = result;
914 }
973 /* The decoded value takes the maximum number of 8 bytes.
974 * It's a maximum number of bytes, so end decoding process here
975 * even if the first bit of last byte is 1. */
976 if(pvalue) {
977 *pvalue = result;
978 }
915
979
916 if(pvalue_len) {
917 *pvalue_len = 9;
918 } else {
919 if(ARCHIVE_OK != consume(a, 9)) {
920 return 0;
921 }
922 }
980 if(pvalue_len) {
981 *pvalue_len = 9;
982 } else {
983 if(ARCHIVE_OK != consume(a, 9)) {
984 return 0;
985 }
986 }
923
987
924 return 1;
988 return 1;
925}
926
927static int read_var_sized(struct archive_read* a, size_t* pvalue,
989}
990
991static int read_var_sized(struct archive_read* a, size_t* pvalue,
928 size_t* pvalue_len)
992 size_t* pvalue_len)
929{
993{
930 uint64_t v;
931 uint64_t v_size = 0;
994 uint64_t v;
995 uint64_t v_size = 0;
932
996
933 const int ret = pvalue_len
934 ? read_var(a, &v, &v_size)
935 : read_var(a, &v, NULL);
997 const int ret = pvalue_len ? read_var(a, &v, &v_size)
998 : read_var(a, &v, NULL);
936
999
937 if(ret == 1 && pvalue) {
938 *pvalue = (size_t) v;
939 }
1000 if(ret == 1 && pvalue) {
1001 *pvalue = (size_t) v;
1002 }
940
1003
941 if(pvalue_len) {
942 /* Possible data truncation should be safe. */
943 *pvalue_len = (size_t) v_size;
944 }
1004 if(pvalue_len) {
1005 /* Possible data truncation should be safe. */
1006 *pvalue_len = (size_t) v_size;
1007 }
945
1008
946 return ret;
1009 return ret;
947}
948
949static int read_bits_32(struct rar5* rar, const uint8_t* p, uint32_t* value) {
1010}
1011
1012static int read_bits_32(struct rar5* rar, const uint8_t* p, uint32_t* value) {
950 uint32_t bits = p[rar->bits.in_addr] << 24;
951 bits |= p[rar->bits.in_addr + 1] << 16;
952 bits |= p[rar->bits.in_addr + 2] << 8;
953 bits |= p[rar->bits.in_addr + 3];
954 bits <<= rar->bits.bit_addr;
955 bits |= p[rar->bits.in_addr + 4] >> (8 - rar->bits.bit_addr);
956 *value = bits;
957 return ARCHIVE_OK;
1013 uint32_t bits = ((uint32_t) p[rar->bits.in_addr]) << 24;
1014 bits |= p[rar->bits.in_addr + 1] << 16;
1015 bits |= p[rar->bits.in_addr + 2] << 8;
1016 bits |= p[rar->bits.in_addr + 3];
1017 bits <<= rar->bits.bit_addr;
1018 bits |= p[rar->bits.in_addr + 4] >> (8 - rar->bits.bit_addr);
1019 *value = bits;
1020 return ARCHIVE_OK;
958}
959
960static int read_bits_16(struct rar5* rar, const uint8_t* p, uint16_t* value) {
1021}
1022
1023static int read_bits_16(struct rar5* rar, const uint8_t* p, uint16_t* value) {
961 int bits = (int) p[rar->bits.in_addr] << 16;
962 bits |= (int) p[rar->bits.in_addr + 1] << 8;
963 bits |= (int) p[rar->bits.in_addr + 2];
964 bits >>= (8 - rar->bits.bit_addr);
965 *value = bits & 0xffff;
966 return ARCHIVE_OK;
1024 int bits = (int) ((uint32_t) p[rar->bits.in_addr]) << 16;
1025 bits |= (int) p[rar->bits.in_addr + 1] << 8;
1026 bits |= (int) p[rar->bits.in_addr + 2];
1027 bits >>= (8 - rar->bits.bit_addr);
1028 *value = bits & 0xffff;
1029 return ARCHIVE_OK;
967}
968
969static void skip_bits(struct rar5* rar, int bits) {
1030}
1031
1032static void skip_bits(struct rar5* rar, int bits) {
970 const int new_bits = rar->bits.bit_addr + bits;
971 rar->bits.in_addr += new_bits >> 3;
972 rar->bits.bit_addr = new_bits & 7;
1033 const int new_bits = rar->bits.bit_addr + bits;
1034 rar->bits.in_addr += new_bits >> 3;
1035 rar->bits.bit_addr = new_bits & 7;
973}
974
975/* n = up to 16 */
976static int read_consume_bits(struct rar5* rar, const uint8_t* p, int n,
1036}
1037
1038/* n = up to 16 */
1039static int read_consume_bits(struct rar5* rar, const uint8_t* p, int n,
977 int* value)
1040 int* value)
978{
1041{
979 uint16_t v;
980 int ret, num;
1042 uint16_t v;
1043 int ret, num;
981
1044
982 if(n == 0 || n > 16) {
983 /* This is a programmer error and should never happen in runtime. */
984 return ARCHIVE_FATAL;
985 }
1045 if(n == 0 || n > 16) {
1046 /* This is a programmer error and should never happen
1047 * in runtime. */
1048 return ARCHIVE_FATAL;
1049 }
986
1050
987 ret = read_bits_16(rar, p, &v);
988 if(ret != ARCHIVE_OK)
989 return ret;
1051 ret = read_bits_16(rar, p, &v);
1052 if(ret != ARCHIVE_OK)
1053 return ret;
990
1054
991 num = (int) v;
992 num >>= 16 - n;
1055 num = (int) v;
1056 num >>= 16 - n;
993
1057
994 skip_bits(rar, n);
1058 skip_bits(rar, n);
995
1059
996 if(value)
997 *value = num;
1060 if(value)
1061 *value = num;
998
1062
999 return ARCHIVE_OK;
1063 return ARCHIVE_OK;
1000}
1001
1002static int read_u32(struct archive_read* a, uint32_t* pvalue) {
1064}
1065
1066static int read_u32(struct archive_read* a, uint32_t* pvalue) {
1003 const uint8_t* p;
1004 if(!read_ahead(a, 4, &p))
1005 return 0;
1067 const uint8_t* p;
1068 if(!read_ahead(a, 4, &p))
1069 return 0;
1006
1070
1007 *pvalue = archive_le32dec(p);
1008 return ARCHIVE_OK == consume(a, 4) ? 1 : 0;
1071 *pvalue = archive_le32dec(p);
1072 return ARCHIVE_OK == consume(a, 4) ? 1 : 0;
1009}
1010
1011static int read_u64(struct archive_read* a, uint64_t* pvalue) {
1073}
1074
1075static int read_u64(struct archive_read* a, uint64_t* pvalue) {
1012 const uint8_t* p;
1013 if(!read_ahead(a, 8, &p))
1014 return 0;
1076 const uint8_t* p;
1077 if(!read_ahead(a, 8, &p))
1078 return 0;
1015
1079
1016 *pvalue = archive_le64dec(p);
1017 return ARCHIVE_OK == consume(a, 8) ? 1 : 0;
1080 *pvalue = archive_le64dec(p);
1081 return ARCHIVE_OK == consume(a, 8) ? 1 : 0;
1018}
1019
1020static int bid_standard(struct archive_read* a) {
1082}
1083
1084static int bid_standard(struct archive_read* a) {
1021 const uint8_t* p;
1085 const uint8_t* p;
1022
1086
1023 if(!read_ahead(a, rar5_signature_size, &p))
1024 return -1;
1087 if(!read_ahead(a, rar5_signature_size, &p))
1088 return -1;
1025
1089
1026 if(!memcmp(rar5_signature, p, rar5_signature_size))
1027 return 30;
1090 if(!memcmp(rar5_signature, p, rar5_signature_size))
1091 return 30;
1028
1092
1029 return -1;
1093 return -1;
1030}
1031
1032static int rar5_bid(struct archive_read* a, int best_bid) {
1094}
1095
1096static int rar5_bid(struct archive_read* a, int best_bid) {
1033 int my_bid;
1097 int my_bid;
1034
1098
1035 if(best_bid > 30)
1036 return -1;
1099 if(best_bid > 30)
1100 return -1;
1037
1101
1038 my_bid = bid_standard(a);
1039 if(my_bid > -1) {
1040 return my_bid;
1041 }
1102 my_bid = bid_standard(a);
1103 if(my_bid > -1) {
1104 return my_bid;
1105 }
1042
1106
1043 return -1;
1107 return -1;
1044}
1045
1108}
1109
1046static int rar5_options(struct archive_read *a, const char *key, const char *val) {
1047 (void) a;
1048 (void) key;
1049 (void) val;
1110static int rar5_options(struct archive_read *a, const char *key,
1111 const char *val) {
1112 (void) a;
1113 (void) key;
1114 (void) val;
1050
1115
1051 /* No options supported in this version. Return the ARCHIVE_WARN code to
1052 * signal the options supervisor that the unpacker didn't handle setting
1053 * this option. */
1116 /* No options supported in this version. Return the ARCHIVE_WARN code
1117 * to signal the options supervisor that the unpacker didn't handle
1118 * setting this option. */
1054
1119
1055 return ARCHIVE_WARN;
1120 return ARCHIVE_WARN;
1056}
1057
1058static void init_header(struct archive_read* a) {
1121}
1122
1123static void init_header(struct archive_read* a) {
1059 a->archive.archive_format = ARCHIVE_FORMAT_RAR_V5;
1060 a->archive.archive_format_name = "RAR5";
1124 a->archive.archive_format = ARCHIVE_FORMAT_RAR_V5;
1125 a->archive.archive_format_name = "RAR5";
1061}
1062
1063enum HEADER_FLAGS {
1126}
1127
1128enum HEADER_FLAGS {
1064 HFL_EXTRA_DATA = 0x0001, HFL_DATA = 0x0002, HFL_SKIP_IF_UNKNOWN = 0x0004,
1065 HFL_SPLIT_BEFORE = 0x0008, HFL_SPLIT_AFTER = 0x0010, HFL_CHILD = 0x0020,
1066 HFL_INHERITED = 0x0040
1129 HFL_EXTRA_DATA = 0x0001,
1130 HFL_DATA = 0x0002,
1131 HFL_SKIP_IF_UNKNOWN = 0x0004,
1132 HFL_SPLIT_BEFORE = 0x0008,
1133 HFL_SPLIT_AFTER = 0x0010,
1134 HFL_CHILD = 0x0020,
1135 HFL_INHERITED = 0x0040
1067};
1068
1069static int process_main_locator_extra_block(struct archive_read* a,
1136};
1137
1138static int process_main_locator_extra_block(struct archive_read* a,
1070 struct rar5* rar)
1139 struct rar5* rar)
1071{
1140{
1072 uint64_t locator_flags;
1141 uint64_t locator_flags;
1073
1142
1074 if(!read_var(a, &locator_flags, NULL)) {
1075 return ARCHIVE_EOF;
1076 }
1143 if(!read_var(a, &locator_flags, NULL)) {
1144 return ARCHIVE_EOF;
1145 }
1077
1146
1078 enum LOCATOR_FLAGS {
1079 QLIST = 0x01, RECOVERY = 0x02,
1080 };
1147 enum LOCATOR_FLAGS {
1148 QLIST = 0x01, RECOVERY = 0x02,
1149 };
1081
1150
1082 if(locator_flags & QLIST) {
1083 if(!read_var(a, &rar->qlist_offset, NULL)) {
1084 return ARCHIVE_EOF;
1085 }
1151 if(locator_flags & QLIST) {
1152 if(!read_var(a, &rar->qlist_offset, NULL)) {
1153 return ARCHIVE_EOF;
1154 }
1086
1155
1087 /* qlist is not used */
1088 }
1156 /* qlist is not used */
1157 }
1089
1158
1090 if(locator_flags & RECOVERY) {
1091 if(!read_var(a, &rar->rr_offset, NULL)) {
1092 return ARCHIVE_EOF;
1093 }
1159 if(locator_flags & RECOVERY) {
1160 if(!read_var(a, &rar->rr_offset, NULL)) {
1161 return ARCHIVE_EOF;
1162 }
1094
1163
1095 /* rr is not used */
1096 }
1164 /* rr is not used */
1165 }
1097
1166
1098 return ARCHIVE_OK;
1167 return ARCHIVE_OK;
1099}
1100
1101static int parse_file_extra_hash(struct archive_read* a, struct rar5* rar,
1168}
1169
1170static int parse_file_extra_hash(struct archive_read* a, struct rar5* rar,
1102 ssize_t* extra_data_size)
1171 ssize_t* extra_data_size)
1103{
1172{
1104 size_t hash_type;
1105 size_t value_len;
1173 size_t hash_type;
1174 size_t value_len;
1106
1175
1107 if(!read_var_sized(a, &hash_type, &value_len))
1108 return ARCHIVE_EOF;
1176 if(!read_var_sized(a, &hash_type, &value_len))
1177 return ARCHIVE_EOF;
1109
1178
1110 *extra_data_size -= value_len;
1111 if(ARCHIVE_OK != consume(a, value_len)) {
1112 return ARCHIVE_EOF;
1113 }
1179 *extra_data_size -= value_len;
1180 if(ARCHIVE_OK != consume(a, value_len)) {
1181 return ARCHIVE_EOF;
1182 }
1114
1183
1115 enum HASH_TYPE {
1116 BLAKE2sp = 0x00
1117 };
1184 enum HASH_TYPE {
1185 BLAKE2sp = 0x00
1186 };
1118
1187
1119 /* The file uses BLAKE2sp checksum algorithm instead of plain old
1120 * CRC32. */
1121 if(hash_type == BLAKE2sp) {
1122 const uint8_t* p;
1123 const int hash_size = sizeof(rar->file.blake2sp);
1188 /* The file uses BLAKE2sp checksum algorithm instead of plain old
1189 * CRC32. */
1190 if(hash_type == BLAKE2sp) {
1191 const uint8_t* p;
1192 const int hash_size = sizeof(rar->file.blake2sp);
1124
1193
1125 if(!read_ahead(a, hash_size, &p))
1126 return ARCHIVE_EOF;
1194 if(!read_ahead(a, hash_size, &p))
1195 return ARCHIVE_EOF;
1127
1196
1128 rar->file.has_blake2 = 1;
1129 memcpy(&rar->file.blake2sp, p, hash_size);
1197 rar->file.has_blake2 = 1;
1198 memcpy(&rar->file.blake2sp, p, hash_size);
1130
1199
1131 if(ARCHIVE_OK != consume(a, hash_size)) {
1132 return ARCHIVE_EOF;
1133 }
1200 if(ARCHIVE_OK != consume(a, hash_size)) {
1201 return ARCHIVE_EOF;
1202 }
1134
1203
1135 *extra_data_size -= hash_size;
1136 } else {
1137 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1138 "Unsupported hash type (0x%02x)", (int) hash_type);
1139 return ARCHIVE_FATAL;
1140 }
1204 *extra_data_size -= hash_size;
1205 } else {
1206 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1207 "Unsupported hash type (0x%x)", (int) hash_type);
1208 return ARCHIVE_FATAL;
1209 }
1141
1210
1142 return ARCHIVE_OK;
1211 return ARCHIVE_OK;
1143}
1144
1145static uint64_t time_win_to_unix(uint64_t win_time) {
1212}
1213
1214static uint64_t time_win_to_unix(uint64_t win_time) {
1146 const size_t ns_in_sec = 10000000;
1147 const uint64_t sec_to_unix = 11644473600LL;
1148 return win_time / ns_in_sec - sec_to_unix;
1215 const size_t ns_in_sec = 10000000;
1216 const uint64_t sec_to_unix = 11644473600LL;
1217 return win_time / ns_in_sec - sec_to_unix;
1149}
1150
1151static int parse_htime_item(struct archive_read* a, char unix_time,
1218}
1219
1220static int parse_htime_item(struct archive_read* a, char unix_time,
1152 uint64_t* where, ssize_t* extra_data_size)
1221 uint64_t* where, ssize_t* extra_data_size)
1153{
1222{
1154 if(unix_time) {
1155 uint32_t time_val;
1156 if(!read_u32(a, &time_val))
1157 return ARCHIVE_EOF;
1223 if(unix_time) {
1224 uint32_t time_val;
1225 if(!read_u32(a, &time_val))
1226 return ARCHIVE_EOF;
1158
1227
1159 *extra_data_size -= 4;
1160 *where = (uint64_t) time_val;
1161 } else {
1162 uint64_t windows_time;
1163 if(!read_u64(a, &windows_time))
1164 return ARCHIVE_EOF;
1228 *extra_data_size -= 4;
1229 *where = (uint64_t) time_val;
1230 } else {
1231 uint64_t windows_time;
1232 if(!read_u64(a, &windows_time))
1233 return ARCHIVE_EOF;
1165
1234
1166 *where = time_win_to_unix(windows_time);
1167 *extra_data_size -= 8;
1168 }
1235 *where = time_win_to_unix(windows_time);
1236 *extra_data_size -= 8;
1237 }
1169
1238
1170 return ARCHIVE_OK;
1239 return ARCHIVE_OK;
1171}
1172
1240}
1241
1242static int parse_file_extra_version(struct archive_read* a,
1243 struct archive_entry* e, ssize_t* extra_data_size)
1244{
1245 size_t flags = 0;
1246 size_t version = 0;
1247 size_t value_len = 0;
1248 struct archive_string version_string;
1249 struct archive_string name_utf8_string;
1250
1251 /* Flags are ignored. */
1252 if(!read_var_sized(a, &flags, &value_len))
1253 return ARCHIVE_EOF;
1254
1255 *extra_data_size -= value_len;
1256 if(ARCHIVE_OK != consume(a, value_len))
1257 return ARCHIVE_EOF;
1258
1259 if(!read_var_sized(a, &version, &value_len))
1260 return ARCHIVE_EOF;
1261
1262 *extra_data_size -= value_len;
1263 if(ARCHIVE_OK != consume(a, value_len))
1264 return ARCHIVE_EOF;
1265
1266 /* extra_data_size should be zero here. */
1267
1268 const char* cur_filename = archive_entry_pathname_utf8(e);
1269 if(cur_filename == NULL) {
1270 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1271 "Version entry without file name");
1272 return ARCHIVE_FATAL;
1273 }
1274
1275 archive_string_init(&version_string);
1276 archive_string_init(&name_utf8_string);
1277
1278 /* Prepare a ;123 suffix for the filename, where '123' is the version
1279 * value of this file. */
1280 archive_string_sprintf(&version_string, ";%zu", version);
1281
1282 /* Build the new filename. */
1283 archive_strcat(&name_utf8_string, cur_filename);
1284 archive_strcat(&name_utf8_string, version_string.s);
1285
1286 /* Apply the new filename into this file's context. */
1287 archive_entry_update_pathname_utf8(e, name_utf8_string.s);
1288
1289 /* Free buffers. */
1290 archive_string_free(&version_string);
1291 archive_string_free(&name_utf8_string);
1292 return ARCHIVE_OK;
1293}
1294
1173static int parse_file_extra_htime(struct archive_read* a,
1295static int parse_file_extra_htime(struct archive_read* a,
1174 struct archive_entry* e, struct rar5* rar,
1175 ssize_t* extra_data_size)
1296 struct archive_entry* e, struct rar5* rar, ssize_t* extra_data_size)
1176{
1297{
1177 char unix_time = 0;
1178 size_t flags;
1179 size_t value_len;
1298 char unix_time = 0;
1299 size_t flags;
1300 size_t value_len;
1180
1301
1181 enum HTIME_FLAGS {
1182 IS_UNIX = 0x01,
1183 HAS_MTIME = 0x02,
1184 HAS_CTIME = 0x04,
1185 HAS_ATIME = 0x08,
1186 HAS_UNIX_NS = 0x10,
1187 };
1302 enum HTIME_FLAGS {
1303 IS_UNIX = 0x01,
1304 HAS_MTIME = 0x02,
1305 HAS_CTIME = 0x04,
1306 HAS_ATIME = 0x08,
1307 HAS_UNIX_NS = 0x10,
1308 };
1188
1309
1189 if(!read_var_sized(a, &flags, &value_len))
1190 return ARCHIVE_EOF;
1310 if(!read_var_sized(a, &flags, &value_len))
1311 return ARCHIVE_EOF;
1191
1312
1192 *extra_data_size -= value_len;
1193 if(ARCHIVE_OK != consume(a, value_len)) {
1194 return ARCHIVE_EOF;
1195 }
1313 *extra_data_size -= value_len;
1314 if(ARCHIVE_OK != consume(a, value_len)) {
1315 return ARCHIVE_EOF;
1316 }
1196
1317
1197 unix_time = flags & IS_UNIX;
1318 unix_time = flags & IS_UNIX;
1198
1319
1199 if(flags & HAS_MTIME) {
1200 parse_htime_item(a, unix_time, &rar->file.e_mtime, extra_data_size);
1201 archive_entry_set_mtime(e, rar->file.e_mtime, 0);
1202 }
1320 if(flags & HAS_MTIME) {
1321 parse_htime_item(a, unix_time, &rar->file.e_mtime,
1322 extra_data_size);
1323 archive_entry_set_mtime(e, rar->file.e_mtime, 0);
1324 }
1203
1325
1204 if(flags & HAS_CTIME) {
1205 parse_htime_item(a, unix_time, &rar->file.e_ctime, extra_data_size);
1206 archive_entry_set_ctime(e, rar->file.e_ctime, 0);
1207 }
1326 if(flags & HAS_CTIME) {
1327 parse_htime_item(a, unix_time, &rar->file.e_ctime,
1328 extra_data_size);
1329 archive_entry_set_ctime(e, rar->file.e_ctime, 0);
1330 }
1208
1331
1209 if(flags & HAS_ATIME) {
1210 parse_htime_item(a, unix_time, &rar->file.e_atime, extra_data_size);
1211 archive_entry_set_atime(e, rar->file.e_atime, 0);
1212 }
1332 if(flags & HAS_ATIME) {
1333 parse_htime_item(a, unix_time, &rar->file.e_atime,
1334 extra_data_size);
1335 archive_entry_set_atime(e, rar->file.e_atime, 0);
1336 }
1213
1337
1214 if(flags & HAS_UNIX_NS) {
1215 if(!read_u32(a, &rar->file.e_unix_ns))
1216 return ARCHIVE_EOF;
1338 if(flags & HAS_UNIX_NS) {
1339 if(!read_u32(a, &rar->file.e_unix_ns))
1340 return ARCHIVE_EOF;
1217
1341
1218 *extra_data_size -= 4;
1219 }
1342 *extra_data_size -= 4;
1343 }
1220
1344
1221 return ARCHIVE_OK;
1345 return ARCHIVE_OK;
1222}
1223
1346}
1347
1224static int process_head_file_extra(struct archive_read* a,
1225 struct archive_entry* e, struct rar5* rar,
1226 ssize_t extra_data_size)
1348static int parse_file_extra_redir(struct archive_read* a,
1349 struct archive_entry* e, struct rar5* rar, ssize_t* extra_data_size)
1227{
1350{
1228 size_t extra_field_size;
1229 size_t extra_field_id = 0;
1230 int ret = ARCHIVE_FATAL;
1231 size_t var_size;
1351 uint64_t value_size = 0;
1352 size_t target_size = 0;
1353 char target_utf8_buf[MAX_NAME_IN_BYTES];
1354 const uint8_t* p;
1232
1355
1233 enum EXTRA {
1234 CRYPT = 0x01, HASH = 0x02, HTIME = 0x03, VERSION_ = 0x04,
1235 REDIR = 0x05, UOWNER = 0x06, SUBDATA = 0x07
1236 };
1356 if(!read_var(a, &rar->file.redir_type, &value_size))
1357 return ARCHIVE_EOF;
1358 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1359 return ARCHIVE_EOF;
1360 *extra_data_size -= value_size;
1237
1361
1238 while(extra_data_size > 0) {
1239 if(!read_var_sized(a, &extra_field_size, &var_size))
1240 return ARCHIVE_EOF;
1362 if(!read_var(a, &rar->file.redir_flags, &value_size))
1363 return ARCHIVE_EOF;
1364 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1365 return ARCHIVE_EOF;
1366 *extra_data_size -= value_size;
1241
1367
1242 extra_data_size -= var_size;
1243 if(ARCHIVE_OK != consume(a, var_size)) {
1244 return ARCHIVE_EOF;
1245 }
1368 if(!read_var_sized(a, &target_size, NULL))
1369 return ARCHIVE_EOF;
1370 *extra_data_size -= target_size + 1;
1246
1371
1247 if(!read_var_sized(a, &extra_field_id, &var_size))
1248 return ARCHIVE_EOF;
1372 if(!read_ahead(a, target_size, &p))
1373 return ARCHIVE_EOF;
1249
1374
1250 extra_data_size -= var_size;
1251 if(ARCHIVE_OK != consume(a, var_size)) {
1252 return ARCHIVE_EOF;
1253 }
1375 if(target_size > (MAX_NAME_IN_CHARS - 1)) {
1376 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1377 "Link target is too long");
1378 return ARCHIVE_FATAL;
1379 }
1254
1380
1255 switch(extra_field_id) {
1256 case HASH:
1257 ret = parse_file_extra_hash(a, rar, &extra_data_size);
1258 break;
1259 case HTIME:
1260 ret = parse_file_extra_htime(a, e, rar, &extra_data_size);
1261 break;
1262 case CRYPT:
1263 /* fallthrough */
1264 case VERSION_:
1265 /* fallthrough */
1266 case REDIR:
1267 /* fallthrough */
1268 case UOWNER:
1269 /* fallthrough */
1270 case SUBDATA:
1271 /* fallthrough */
1272 default:
1273 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1274 "Unknown extra field in file/service block: 0x%02x",
1275 (int) extra_field_id);
1276 return ARCHIVE_FATAL;
1277 }
1278 }
1381 if(target_size == 0) {
1382 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1383 "No link target specified");
1384 return ARCHIVE_FATAL;
1385 }
1279
1386
1280 if(ret != ARCHIVE_OK) {
1281 /* Attribute not implemented. */
1282 return ret;
1283 }
1387 memcpy(target_utf8_buf, p, target_size);
1388 target_utf8_buf[target_size] = 0;
1284
1389
1285 return ARCHIVE_OK;
1390 if(ARCHIVE_OK != consume(a, (int64_t)target_size))
1391 return ARCHIVE_EOF;
1392
1393 switch(rar->file.redir_type) {
1394 case REDIR_TYPE_UNIXSYMLINK:
1395 case REDIR_TYPE_WINSYMLINK:
1396 archive_entry_set_filetype(e, AE_IFLNK);
1397 archive_entry_update_symlink_utf8(e, target_utf8_buf);
1398 if (rar->file.redir_flags & REDIR_SYMLINK_IS_DIR) {
1399 archive_entry_set_symlink_type(e,
1400 AE_SYMLINK_TYPE_DIRECTORY);
1401 } else {
1402 archive_entry_set_symlink_type(e,
1403 AE_SYMLINK_TYPE_FILE);
1404 }
1405 break;
1406
1407 case REDIR_TYPE_HARDLINK:
1408 archive_entry_set_filetype(e, AE_IFREG);
1409 archive_entry_update_hardlink_utf8(e, target_utf8_buf);
1410 break;
1411
1412 default:
1413 /* Unknown redir type, skip it. */
1414 break;
1415 }
1416 return ARCHIVE_OK;
1286}
1287
1417}
1418
1419static int parse_file_extra_owner(struct archive_read* a,
1420 struct archive_entry* e, ssize_t* extra_data_size)
1421{
1422 uint64_t flags = 0;
1423 uint64_t value_size = 0;
1424 uint64_t id = 0;
1425 size_t name_len = 0;
1426 size_t name_size = 0;
1427 char namebuf[OWNER_MAXNAMELEN];
1428 const uint8_t* p;
1429
1430 if(!read_var(a, &flags, &value_size))
1431 return ARCHIVE_EOF;
1432 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1433 return ARCHIVE_EOF;
1434 *extra_data_size -= value_size;
1435
1436 if ((flags & OWNER_USER_NAME) != 0) {
1437 if(!read_var_sized(a, &name_size, NULL))
1438 return ARCHIVE_EOF;
1439 *extra_data_size -= name_size + 1;
1440
1441 if(!read_ahead(a, name_size, &p))
1442 return ARCHIVE_EOF;
1443
1444 if (name_size >= OWNER_MAXNAMELEN) {
1445 name_len = OWNER_MAXNAMELEN - 1;
1446 } else {
1447 name_len = name_size;
1448 }
1449
1450 memcpy(namebuf, p, name_len);
1451 namebuf[name_len] = 0;
1452 if(ARCHIVE_OK != consume(a, (int64_t)name_size))
1453 return ARCHIVE_EOF;
1454
1455 archive_entry_set_uname(e, namebuf);
1456 }
1457 if ((flags & OWNER_GROUP_NAME) != 0) {
1458 if(!read_var_sized(a, &name_size, NULL))
1459 return ARCHIVE_EOF;
1460 *extra_data_size -= name_size + 1;
1461
1462 if(!read_ahead(a, name_size, &p))
1463 return ARCHIVE_EOF;
1464
1465 if (name_size >= OWNER_MAXNAMELEN) {
1466 name_len = OWNER_MAXNAMELEN - 1;
1467 } else {
1468 name_len = name_size;
1469 }
1470
1471 memcpy(namebuf, p, name_len);
1472 namebuf[name_len] = 0;
1473 if(ARCHIVE_OK != consume(a, (int64_t)name_size))
1474 return ARCHIVE_EOF;
1475
1476 archive_entry_set_gname(e, namebuf);
1477 }
1478 if ((flags & OWNER_USER_UID) != 0) {
1479 if(!read_var(a, &id, &value_size))
1480 return ARCHIVE_EOF;
1481 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1482 return ARCHIVE_EOF;
1483 *extra_data_size -= value_size;
1484
1485 archive_entry_set_uid(e, (la_int64_t)id);
1486 }
1487 if ((flags & OWNER_GROUP_GID) != 0) {
1488 if(!read_var(a, &id, &value_size))
1489 return ARCHIVE_EOF;
1490 if(ARCHIVE_OK != consume(a, (int64_t)value_size))
1491 return ARCHIVE_EOF;
1492 *extra_data_size -= value_size;
1493
1494 archive_entry_set_gid(e, (la_int64_t)id);
1495 }
1496 return ARCHIVE_OK;
1497}
1498
1499static int process_head_file_extra(struct archive_read* a,
1500 struct archive_entry* e, struct rar5* rar, ssize_t extra_data_size)
1501{
1502 size_t extra_field_size;
1503 size_t extra_field_id = 0;
1504 int ret = ARCHIVE_FATAL;
1505 size_t var_size;
1506
1507 while(extra_data_size > 0) {
1508 if(!read_var_sized(a, &extra_field_size, &var_size))
1509 return ARCHIVE_EOF;
1510
1511 extra_data_size -= var_size;
1512 if(ARCHIVE_OK != consume(a, var_size)) {
1513 return ARCHIVE_EOF;
1514 }
1515
1516 if(!read_var_sized(a, &extra_field_id, &var_size))
1517 return ARCHIVE_EOF;
1518
1519 extra_data_size -= var_size;
1520 if(ARCHIVE_OK != consume(a, var_size)) {
1521 return ARCHIVE_EOF;
1522 }
1523
1524 switch(extra_field_id) {
1525 case EX_HASH:
1526 ret = parse_file_extra_hash(a, rar,
1527 &extra_data_size);
1528 break;
1529 case EX_HTIME:
1530 ret = parse_file_extra_htime(a, e, rar,
1531 &extra_data_size);
1532 break;
1533 case EX_REDIR:
1534 ret = parse_file_extra_redir(a, e, rar,
1535 &extra_data_size);
1536 break;
1537 case EX_UOWNER:
1538 ret = parse_file_extra_owner(a, e,
1539 &extra_data_size);
1540 break;
1541 case EX_VERSION:
1542 ret = parse_file_extra_version(a, e,
1543 &extra_data_size);
1544 break;
1545 case EX_CRYPT:
1546 /* fallthrough */
1547 case EX_SUBDATA:
1548 /* fallthrough */
1549 default:
1550 /* Skip unsupported entry. */
1551 return consume(a, extra_data_size);
1552 }
1553 }
1554
1555 if(ret != ARCHIVE_OK) {
1556 /* Attribute not implemented. */
1557 return ret;
1558 }
1559
1560 return ARCHIVE_OK;
1561}
1562
1288static int process_head_file(struct archive_read* a, struct rar5* rar,
1563static int process_head_file(struct archive_read* a, struct rar5* rar,
1289 struct archive_entry* entry, size_t block_flags)
1564 struct archive_entry* entry, size_t block_flags)
1290{
1565{
1291 ssize_t extra_data_size = 0;
1292 size_t data_size = 0;
1293 size_t file_flags = 0;
1294 size_t file_attr = 0;
1295 size_t compression_info = 0;
1296 size_t host_os = 0;
1297 size_t name_size = 0;
1298 uint64_t unpacked_size;
1299 uint32_t mtime = 0, crc = 0;
1300 int c_method = 0, c_version = 0, is_dir;
1301 char name_utf8_buf[2048 * 4];
1302 const uint8_t* p;
1566 ssize_t extra_data_size = 0;
1567 size_t data_size = 0;
1568 size_t file_flags = 0;
1569 size_t file_attr = 0;
1570 size_t compression_info = 0;
1571 size_t host_os = 0;
1572 size_t name_size = 0;
1573 uint64_t unpacked_size, window_size;
1574 uint32_t mtime = 0, crc = 0;
1575 int c_method = 0, c_version = 0;
1576 char name_utf8_buf[MAX_NAME_IN_BYTES];
1577 const uint8_t* p;
1303
1578
1304 archive_entry_clear(entry);
1579 archive_entry_clear(entry);
1305
1580
1306 /* Do not reset file context if we're switching archives. */
1307 if(!rar->cstate.switch_multivolume) {
1308 reset_file_context(rar);
1309 }
1581 /* Do not reset file context if we're switching archives. */
1582 if(!rar->cstate.switch_multivolume) {
1583 reset_file_context(rar);
1584 }
1310
1585
1311 if(block_flags & HFL_EXTRA_DATA) {
1312 size_t edata_size = 0;
1313 if(!read_var_sized(a, &edata_size, NULL))
1314 return ARCHIVE_EOF;
1586 if(block_flags & HFL_EXTRA_DATA) {
1587 size_t edata_size = 0;
1588 if(!read_var_sized(a, &edata_size, NULL))
1589 return ARCHIVE_EOF;
1315
1590
1316 /* Intentional type cast from unsigned to signed. */
1317 extra_data_size = (ssize_t) edata_size;
1318 }
1591 /* Intentional type cast from unsigned to signed. */
1592 extra_data_size = (ssize_t) edata_size;
1593 }
1319
1594
1320 if(block_flags & HFL_DATA) {
1321 if(!read_var_sized(a, &data_size, NULL))
1322 return ARCHIVE_EOF;
1595 if(block_flags & HFL_DATA) {
1596 if(!read_var_sized(a, &data_size, NULL))
1597 return ARCHIVE_EOF;
1323
1598
1324 rar->file.bytes_remaining = data_size;
1325 } else {
1326 rar->file.bytes_remaining = 0;
1599 rar->file.bytes_remaining = data_size;
1600 } else {
1601 rar->file.bytes_remaining = 0;
1327
1602
1328 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1329 "no data found in file/service block");
1330 return ARCHIVE_FATAL;
1331 }
1603 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1604 "no data found in file/service block");
1605 return ARCHIVE_FATAL;
1606 }
1332
1607
1333 enum FILE_FLAGS {
1334 DIRECTORY = 0x0001, UTIME = 0x0002, CRC32 = 0x0004,
1335 UNKNOWN_UNPACKED_SIZE = 0x0008,
1336 };
1608 enum FILE_FLAGS {
1609 DIRECTORY = 0x0001, UTIME = 0x0002, CRC32 = 0x0004,
1610 UNKNOWN_UNPACKED_SIZE = 0x0008,
1611 };
1337
1612
1338 enum COMP_INFO_FLAGS {
1339 SOLID = 0x0040,
1340 };
1613 enum FILE_ATTRS {
1614 ATTR_READONLY = 0x1, ATTR_HIDDEN = 0x2, ATTR_SYSTEM = 0x4,
1615 ATTR_DIRECTORY = 0x10,
1616 };
1341
1617
1342 if(!read_var_sized(a, &file_flags, NULL))
1343 return ARCHIVE_EOF;
1618 enum COMP_INFO_FLAGS {
1619 SOLID = 0x0040,
1620 };
1344
1621
1345 if(!read_var(a, &unpacked_size, NULL))
1346 return ARCHIVE_EOF;
1622 if(!read_var_sized(a, &file_flags, NULL))
1623 return ARCHIVE_EOF;
1347
1624
1348 if(file_flags & UNKNOWN_UNPACKED_SIZE) {
1349 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1350 "Files with unknown unpacked size are not supported");
1351 return ARCHIVE_FATAL;
1352 }
1625 if(!read_var(a, &unpacked_size, NULL))
1626 return ARCHIVE_EOF;
1353
1627
1354 is_dir = (int) (file_flags & DIRECTORY);
1628 if(file_flags & UNKNOWN_UNPACKED_SIZE) {
1629 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1630 "Files with unknown unpacked size are not supported");
1631 return ARCHIVE_FATAL;
1632 }
1355
1633
1356 if(!read_var_sized(a, &file_attr, NULL))
1357 return ARCHIVE_EOF;
1634 rar->file.dir = (uint8_t) ((file_flags & DIRECTORY) > 0);
1358
1635
1359 if(file_flags & UTIME) {
1360 if(!read_u32(a, &mtime))
1361 return ARCHIVE_EOF;
1362 }
1636 if(!read_var_sized(a, &file_attr, NULL))
1637 return ARCHIVE_EOF;
1363
1638
1364 if(file_flags & CRC32) {
1365 if(!read_u32(a, &crc))
1366 return ARCHIVE_EOF;
1367 }
1639 if(file_flags & UTIME) {
1640 if(!read_u32(a, &mtime))
1641 return ARCHIVE_EOF;
1642 }
1368
1643
1369 if(!read_var_sized(a, &compression_info, NULL))
1370 return ARCHIVE_EOF;
1644 if(file_flags & CRC32) {
1645 if(!read_u32(a, &crc))
1646 return ARCHIVE_EOF;
1647 }
1371
1648
1372 c_method = (int) (compression_info >> 7) & 0x7;
1373 c_version = (int) (compression_info & 0x3f);
1649 if(!read_var_sized(a, &compression_info, NULL))
1650 return ARCHIVE_EOF;
1374
1651
1375 rar->cstate.window_size = is_dir ?
1376 0 :
1377 g_unpack_window_size << ((compression_info >> 10) & 15);
1378 rar->cstate.method = c_method;
1379 rar->cstate.version = c_version + 50;
1652 c_method = (int) (compression_info >> 7) & 0x7;
1653 c_version = (int) (compression_info & 0x3f);
1380
1654
1381 rar->file.solid = (compression_info & SOLID) > 0;
1382 rar->file.service = 0;
1655 /* RAR5 seems to limit the dictionary size to 64MB. */
1656 window_size = (rar->file.dir > 0) ?
1657 0 :
1658 g_unpack_window_size << ((compression_info >> 10) & 15);
1659 rar->cstate.method = c_method;
1660 rar->cstate.version = c_version + 50;
1383
1661
1384 if(!read_var_sized(a, &host_os, NULL))
1385 return ARCHIVE_EOF;
1662 /* Check if window_size is a sane value. Also, if the file is not
1663 * declared as a directory, disallow window_size == 0. */
1664 if(window_size > (64 * 1024 * 1024) ||
1665 (rar->file.dir == 0 && window_size == 0))
1666 {
1667 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1668 "Declared dictionary size is not supported.");
1669 return ARCHIVE_FATAL;
1670 }
1386
1671
1387 enum HOST_OS {
1388 HOST_WINDOWS = 0,
1389 HOST_UNIX = 1,
1390 };
1672 /* Values up to 64M should fit into ssize_t on every
1673 * architecture. */
1674 rar->cstate.window_size = (ssize_t) window_size;
1391
1675
1392 if(host_os == HOST_WINDOWS) {
1393 /* Host OS is Windows */
1676 rar->file.solid = (compression_info & SOLID) > 0;
1677 rar->file.service = 0;
1394
1678
1395 unsigned short mode = 0660;
1679 if(!read_var_sized(a, &host_os, NULL))
1680 return ARCHIVE_EOF;
1396
1681
1397 if(is_dir)
1398 mode |= AE_IFDIR;
1399 else
1400 mode |= AE_IFREG;
1682 enum HOST_OS {
1683 HOST_WINDOWS = 0,
1684 HOST_UNIX = 1,
1685 };
1401
1686
1402 archive_entry_set_mode(entry, mode);
1403 } else if(host_os == HOST_UNIX) {
1404 /* Host OS is Unix */
1405 archive_entry_set_mode(entry, (unsigned short) file_attr);
1406 } else {
1407 /* Unknown host OS */
1408 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1409 "Unsupported Host OS: 0x%02x", (int) host_os);
1687 if(host_os == HOST_WINDOWS) {
1688 /* Host OS is Windows */
1410
1689
1411 return ARCHIVE_FATAL;
1412 }
1690 __LA_MODE_T mode;
1413
1691
1414 if(!read_var_sized(a, &name_size, NULL))
1415 return ARCHIVE_EOF;
1692 if(file_attr & ATTR_DIRECTORY) {
1693 if (file_attr & ATTR_READONLY) {
1694 mode = 0555 | AE_IFDIR;
1695 } else {
1696 mode = 0755 | AE_IFDIR;
1697 }
1698 } else {
1699 if (file_attr & ATTR_READONLY) {
1700 mode = 0444 | AE_IFREG;
1701 } else {
1702 mode = 0644 | AE_IFREG;
1703 }
1704 }
1416
1705
1417 if(!read_ahead(a, name_size, &p))
1418 return ARCHIVE_EOF;
1706 archive_entry_set_mode(entry, mode);
1419
1707
1420 if(name_size > 2047) {
1421 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1422 "Filename is too long");
1708 if (file_attr & (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM)) {
1709 char *fflags_text, *ptr;
1710 /* allocate for "rdonly,hidden,system," */
1711 fflags_text = malloc(22 * sizeof(char));
1712 if (fflags_text != NULL) {
1713 ptr = fflags_text;
1714 if (file_attr & ATTR_READONLY) {
1715 strcpy(ptr, "rdonly,");
1716 ptr = ptr + 7;
1717 }
1718 if (file_attr & ATTR_HIDDEN) {
1719 strcpy(ptr, "hidden,");
1720 ptr = ptr + 7;
1721 }
1722 if (file_attr & ATTR_SYSTEM) {
1723 strcpy(ptr, "system,");
1724 ptr = ptr + 7;
1725 }
1726 if (ptr > fflags_text) {
1727 /* Delete trailing comma */
1728 *(ptr - 1) = '\0';
1729 archive_entry_copy_fflags_text(entry,
1730 fflags_text);
1731 }
1732 free(fflags_text);
1733 }
1734 }
1735 } else if(host_os == HOST_UNIX) {
1736 /* Host OS is Unix */
1737 archive_entry_set_mode(entry, (__LA_MODE_T) file_attr);
1738 } else {
1739 /* Unknown host OS */
1740 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1741 "Unsupported Host OS: 0x%x", (int) host_os);
1423
1742
1424 return ARCHIVE_FATAL;
1425 }
1743 return ARCHIVE_FATAL;
1744 }
1426
1745
1427 if(name_size == 0) {
1428 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1429 "No filename specified");
1746 if(!read_var_sized(a, &name_size, NULL))
1747 return ARCHIVE_EOF;
1430
1748
1431 return ARCHIVE_FATAL;
1432 }
1749 if(!read_ahead(a, name_size, &p))
1750 return ARCHIVE_EOF;
1433
1751
1434 memcpy(name_utf8_buf, p, name_size);
1435 name_utf8_buf[name_size] = 0;
1436 if(ARCHIVE_OK != consume(a, name_size)) {
1437 return ARCHIVE_EOF;
1438 }
1752 if(name_size > (MAX_NAME_IN_CHARS - 1)) {
1753 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1754 "Filename is too long");
1439
1755
1440 if(extra_data_size > 0) {
1441 int ret = process_head_file_extra(a, entry, rar, extra_data_size);
1756 return ARCHIVE_FATAL;
1757 }
1442
1758
1443 /* Sanity check. */
1444 if(extra_data_size < 0) {
1445 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1446 "File extra data size is not zero");
1447 return ARCHIVE_FATAL;
1448 }
1759 if(name_size == 0) {
1760 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1761 "No filename specified");
1449
1762
1450 if(ret != ARCHIVE_OK)
1451 return ret;
1452 }
1763 return ARCHIVE_FATAL;
1764 }
1453
1765
1454 if((file_flags & UNKNOWN_UNPACKED_SIZE) == 0) {
1455 rar->file.unpacked_size = (ssize_t) unpacked_size;
1456 archive_entry_set_size(entry, unpacked_size);
1457 }
1766 memcpy(name_utf8_buf, p, name_size);
1767 name_utf8_buf[name_size] = 0;
1768 if(ARCHIVE_OK != consume(a, name_size)) {
1769 return ARCHIVE_EOF;
1770 }
1458
1771
1459 if(file_flags & UTIME) {
1460 archive_entry_set_mtime(entry, (time_t) mtime, 0);
1461 }
1772 archive_entry_update_pathname_utf8(entry, name_utf8_buf);
1462
1773
1463 if(file_flags & CRC32) {
1464 rar->file.stored_crc32 = crc;
1465 }
1774 if(extra_data_size > 0) {
1775 int ret = process_head_file_extra(a, entry, rar,
1776 extra_data_size);
1466
1777
1467 archive_entry_update_pathname_utf8(entry, name_utf8_buf);
1778 /* Sanity check. */
1779 if(extra_data_size < 0) {
1780 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1781 "File extra data size is not zero");
1782 return ARCHIVE_FATAL;
1783 }
1468
1784
1469 if(!rar->cstate.switch_multivolume) {
1470 /* Do not reinitialize unpacking state if we're switching archives. */
1471 rar->cstate.block_parsing_finished = 1;
1472 rar->cstate.all_filters_applied = 1;
1473 rar->cstate.initialized = 0;
1474 }
1785 if(ret != ARCHIVE_OK)
1786 return ret;
1787 }
1475
1788
1476 if(rar->generic.split_before > 0) {
1477 /* If now we're standing on a header that has a 'split before' mark,
1478 * it means we're standing on a 'continuation' file header. Signal
1479 * the caller that if it wants to move to another file, it must call
1480 * rar5_read_header() function again. */
1789 if((file_flags & UNKNOWN_UNPACKED_SIZE) == 0) {
1790 rar->file.unpacked_size = (ssize_t) unpacked_size;
1791 if(rar->file.redir_type == REDIR_TYPE_NONE)
1792 archive_entry_set_size(entry, unpacked_size);
1793 }
1481
1794
1482 return ARCHIVE_RETRY;
1483 } else {
1484 return ARCHIVE_OK;
1485 }
1795 if(file_flags & UTIME) {
1796 archive_entry_set_mtime(entry, (time_t) mtime, 0);
1797 }
1798
1799 if(file_flags & CRC32) {
1800 rar->file.stored_crc32 = crc;
1801 }
1802
1803 if(!rar->cstate.switch_multivolume) {
1804 /* Do not reinitialize unpacking state if we're switching
1805 * archives. */
1806 rar->cstate.block_parsing_finished = 1;
1807 rar->cstate.all_filters_applied = 1;
1808 rar->cstate.initialized = 0;
1809 }
1810
1811 if(rar->generic.split_before > 0) {
1812 /* If now we're standing on a header that has a 'split before'
1813 * mark, it means we're standing on a 'continuation' file
1814 * header. Signal the caller that if it wants to move to
1815 * another file, it must call rar5_read_header() function
1816 * again. */
1817
1818 return ARCHIVE_RETRY;
1819 } else {
1820 return ARCHIVE_OK;
1821 }
1486}
1487
1488static int process_head_service(struct archive_read* a, struct rar5* rar,
1822}
1823
1824static int process_head_service(struct archive_read* a, struct rar5* rar,
1489 struct archive_entry* entry, size_t block_flags)
1825 struct archive_entry* entry, size_t block_flags)
1490{
1826{
1491 /* Process this SERVICE block the same way as FILE blocks. */
1492 int ret = process_head_file(a, rar, entry, block_flags);
1493 if(ret != ARCHIVE_OK)
1494 return ret;
1827 /* Process this SERVICE block the same way as FILE blocks. */
1828 int ret = process_head_file(a, rar, entry, block_flags);
1829 if(ret != ARCHIVE_OK)
1830 return ret;
1495
1831
1496 rar->file.service = 1;
1832 rar->file.service = 1;
1497
1833
1498 /* But skip the data part automatically. It's no use for the user anyway.
1499 * It contains only service data, not even needed to properly unpack the
1500 * file. */
1501 ret = rar5_read_data_skip(a);
1502 if(ret != ARCHIVE_OK)
1503 return ret;
1834 /* But skip the data part automatically. It's no use for the user
1835 * anyway. It contains only service data, not even needed to
1836 * properly unpack the file. */
1837 ret = rar5_read_data_skip(a);
1838 if(ret != ARCHIVE_OK)
1839 return ret;
1504
1840
1505 /* After skipping, try parsing another block automatically. */
1506 return ARCHIVE_RETRY;
1841 /* After skipping, try parsing another block automatically. */
1842 return ARCHIVE_RETRY;
1507}
1508
1509static int process_head_main(struct archive_read* a, struct rar5* rar,
1843}
1844
1845static int process_head_main(struct archive_read* a, struct rar5* rar,
1510 struct archive_entry* entry, size_t block_flags)
1846 struct archive_entry* entry, size_t block_flags)
1511{
1847{
1512 (void) entry;
1848 (void) entry;
1513
1849
1514 int ret;
1515 size_t extra_data_size = 0;
1516 size_t extra_field_size = 0;
1517 size_t extra_field_id = 0;
1518 size_t archive_flags = 0;
1850 int ret;
1851 size_t extra_data_size = 0;
1852 size_t extra_field_size = 0;
1853 size_t extra_field_id = 0;
1854 size_t archive_flags = 0;
1519
1855
1520 if(block_flags & HFL_EXTRA_DATA) {
1521 if(!read_var_sized(a, &extra_data_size, NULL))
1522 return ARCHIVE_EOF;
1523 } else {
1524 extra_data_size = 0;
1525 }
1856 if(block_flags & HFL_EXTRA_DATA) {
1857 if(!read_var_sized(a, &extra_data_size, NULL))
1858 return ARCHIVE_EOF;
1859 } else {
1860 extra_data_size = 0;
1861 }
1526
1862
1527 if(!read_var_sized(a, &archive_flags, NULL)) {
1528 return ARCHIVE_EOF;
1529 }
1863 if(!read_var_sized(a, &archive_flags, NULL)) {
1864 return ARCHIVE_EOF;
1865 }
1530
1866
1531 enum MAIN_FLAGS {
1532 VOLUME = 0x0001, /* multi-volume archive */
1533 VOLUME_NUMBER = 0x0002, /* volume number, first vol doesn't have it */
1534 SOLID = 0x0004, /* solid archive */
1535 PROTECT = 0x0008, /* contains Recovery info */
1536 LOCK = 0x0010, /* readonly flag, not used */
1537 };
1867 enum MAIN_FLAGS {
1868 VOLUME = 0x0001, /* multi-volume archive */
1869 VOLUME_NUMBER = 0x0002, /* volume number, first vol doesn't
1870 * have it */
1871 SOLID = 0x0004, /* solid archive */
1872 PROTECT = 0x0008, /* contains Recovery info */
1873 LOCK = 0x0010, /* readonly flag, not used */
1874 };
1538
1875
1539 rar->main.volume = (archive_flags & VOLUME) > 0;
1540 rar->main.solid = (archive_flags & SOLID) > 0;
1876 rar->main.volume = (archive_flags & VOLUME) > 0;
1877 rar->main.solid = (archive_flags & SOLID) > 0;
1541
1878
1542 if(archive_flags & VOLUME_NUMBER) {
1543 size_t v = 0;
1544 if(!read_var_sized(a, &v, NULL)) {
1545 return ARCHIVE_EOF;
1546 }
1879 if(archive_flags & VOLUME_NUMBER) {
1880 size_t v = 0;
1881 if(!read_var_sized(a, &v, NULL)) {
1882 return ARCHIVE_EOF;
1883 }
1547
1884
1548 rar->main.vol_no = (int) v;
1549 } else {
1550 rar->main.vol_no = 0;
1551 }
1885 if (v > UINT_MAX) {
1886 archive_set_error(&a->archive,
1887 ARCHIVE_ERRNO_FILE_FORMAT,
1888 "Invalid volume number");
1889 return ARCHIVE_FATAL;
1890 }
1552
1891
1553 if(rar->vol.expected_vol_no > 0 &&
1554 rar->main.vol_no != rar->vol.expected_vol_no)
1555 {
1556 /* Returning EOF instead of FATAL because of strange libarchive
1557 * behavior. When opening multiple files via
1558 * archive_read_open_filenames(), after reading up the whole last file,
1559 * the __archive_read_ahead function wraps up to the first archive
1560 * instead of returning EOF. */
1561 return ARCHIVE_EOF;
1562 }
1892 rar->main.vol_no = (unsigned int) v;
1893 } else {
1894 rar->main.vol_no = 0;
1895 }
1563
1896
1564 if(extra_data_size == 0) {
1565 /* Early return. */
1566 return ARCHIVE_OK;
1567 }
1897 if(rar->vol.expected_vol_no > 0 &&
1898 rar->main.vol_no != rar->vol.expected_vol_no)
1899 {
1900 /* Returning EOF instead of FATAL because of strange
1901 * libarchive behavior. When opening multiple files via
1902 * archive_read_open_filenames(), after reading up the whole
1903 * last file, the __archive_read_ahead function wraps up to
1904 * the first archive instead of returning EOF. */
1905 return ARCHIVE_EOF;
1906 }
1568
1907
1569 if(!read_var_sized(a, &extra_field_size, NULL)) {
1570 return ARCHIVE_EOF;
1571 }
1908 if(extra_data_size == 0) {
1909 /* Early return. */
1910 return ARCHIVE_OK;
1911 }
1572
1912
1573 if(!read_var_sized(a, &extra_field_id, NULL)) {
1574 return ARCHIVE_EOF;
1575 }
1913 if(!read_var_sized(a, &extra_field_size, NULL)) {
1914 return ARCHIVE_EOF;
1915 }
1576
1916
1577 if(extra_field_size == 0) {
1578 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1579 "Invalid extra field size");
1580 return ARCHIVE_FATAL;
1581 }
1917 if(!read_var_sized(a, &extra_field_id, NULL)) {
1918 return ARCHIVE_EOF;
1919 }
1582
1920
1583 enum MAIN_EXTRA {
1584 // Just one attribute here.
1585 LOCATOR = 0x01,
1586 };
1921 if(extra_field_size == 0) {
1922 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1923 "Invalid extra field size");
1924 return ARCHIVE_FATAL;
1925 }
1587
1926
1588 switch(extra_field_id) {
1589 case LOCATOR:
1590 ret = process_main_locator_extra_block(a, rar);
1591 if(ret != ARCHIVE_OK) {
1592 /* Error while parsing main locator extra block. */
1593 return ret;
1594 }
1927 enum MAIN_EXTRA {
1928 // Just one attribute here.
1929 LOCATOR = 0x01,
1930 };
1595
1931
1596 break;
1597 default:
1598 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1599 "Unsupported extra type (0x%02x)", (int) extra_field_id);
1600 return ARCHIVE_FATAL;
1601 }
1932 switch(extra_field_id) {
1933 case LOCATOR:
1934 ret = process_main_locator_extra_block(a, rar);
1935 if(ret != ARCHIVE_OK) {
1936 /* Error while parsing main locator extra
1937 * block. */
1938 return ret;
1939 }
1602
1940
1603 return ARCHIVE_OK;
1941 break;
1942 default:
1943 archive_set_error(&a->archive,
1944 ARCHIVE_ERRNO_FILE_FORMAT,
1945 "Unsupported extra type (0x%x)",
1946 (int) extra_field_id);
1947 return ARCHIVE_FATAL;
1948 }
1949
1950 return ARCHIVE_OK;
1604}
1605
1951}
1952
1953static int skip_unprocessed_bytes(struct archive_read* a) {
1954 struct rar5* rar = get_context(a);
1955 int ret;
1956
1957 if(rar->file.bytes_remaining) {
1958 /* Use different skipping method in block merging mode than in
1959 * normal mode. If merge mode is active, rar5_read_data_skip
1960 * can't be used, because it could allow recursive use of
1961 * merge_block() * function, and this function doesn't support
1962 * recursive use. */
1963 if(rar->merge_mode) {
1964 /* Discard whole merged block. This is valid in solid
1965 * mode as well, because the code will discard blocks
1966 * only if those blocks are safe to discard (i.e.
1967 * they're not FILE blocks). */
1968 ret = consume(a, rar->file.bytes_remaining);
1969 if(ret != ARCHIVE_OK) {
1970 return ret;
1971 }
1972 rar->file.bytes_remaining = 0;
1973 } else {
1974 /* If we're not in merge mode, use safe skipping code.
1975 * This will ensure we'll handle solid archives
1976 * properly. */
1977 ret = rar5_read_data_skip(a);
1978 if(ret != ARCHIVE_OK) {
1979 return ret;
1980 }
1981 }
1982 }
1983
1984 return ARCHIVE_OK;
1985}
1986
1606static int scan_for_signature(struct archive_read* a);
1607
1608/* Base block processing function. A 'base block' is a RARv5 header block
1609 * that tells the reader what kind of data is stored inside the block.
1610 *
1611 * From the birds-eye view a RAR file looks file this:
1612 *
1613 * <magic><base_block_1><base_block_2>...<base_block_n>

--- 32 unchanged lines hidden (view full) ---

1646 * we see 'split after' flag, then we need to jump over to another <FILE>
1647 * block to be able to decompress rest of the data. To do this, we need
1648 * to skip the <ENDARC> block, then switch to another file, then skip the
1649 * <magic> block, <MAIN> block, and then we're standing on the proper
1650 * <FILE> block.
1651 */
1652
1653static int process_base_block(struct archive_read* a,
1987static int scan_for_signature(struct archive_read* a);
1988
1989/* Base block processing function. A 'base block' is a RARv5 header block
1990 * that tells the reader what kind of data is stored inside the block.
1991 *
1992 * From the birds-eye view a RAR file looks file this:
1993 *
1994 * <magic><base_block_1><base_block_2>...<base_block_n>

--- 32 unchanged lines hidden (view full) ---

2027 * we see 'split after' flag, then we need to jump over to another <FILE>
2028 * block to be able to decompress rest of the data. To do this, we need
2029 * to skip the <ENDARC> block, then switch to another file, then skip the
2030 * <magic> block, <MAIN> block, and then we're standing on the proper
2031 * <FILE> block.
2032 */
2033
2034static int process_base_block(struct archive_read* a,
1654 struct archive_entry* entry)
2035 struct archive_entry* entry)
1655{
2036{
1656 struct rar5* rar = get_context(a);
1657 uint32_t hdr_crc, computed_crc;
1658 size_t raw_hdr_size = 0, hdr_size_len, hdr_size;
1659 size_t header_id = 0;
1660 size_t header_flags = 0;
1661 const uint8_t* p;
1662 int ret;
2037 struct rar5* rar = get_context(a);
2038 uint32_t hdr_crc, computed_crc;
2039 size_t raw_hdr_size = 0, hdr_size_len, hdr_size;
2040 size_t header_id = 0;
2041 size_t header_flags = 0;
2042 const uint8_t* p;
2043 int ret;
1663
2044
1664 /* Skip any unprocessed data for this file. */
1665 if(rar->file.bytes_remaining) {
1666 ret = rar5_read_data_skip(a);
1667 if(ret != ARCHIVE_OK) {
1668 return ret;
1669 }
1670 }
2045 /* Skip any unprocessed data for this file. */
2046 ret = skip_unprocessed_bytes(a);
2047 if(ret != ARCHIVE_OK)
2048 return ret;
1671
2049
1672 /* Read the expected CRC32 checksum. */
1673 if(!read_u32(a, &hdr_crc)) {
1674 return ARCHIVE_EOF;
1675 }
2050 /* Read the expected CRC32 checksum. */
2051 if(!read_u32(a, &hdr_crc)) {
2052 return ARCHIVE_EOF;
2053 }
1676
2054
1677 /* Read header size. */
1678 if(!read_var_sized(a, &raw_hdr_size, &hdr_size_len)) {
1679 return ARCHIVE_EOF;
1680 }
2055 /* Read header size. */
2056 if(!read_var_sized(a, &raw_hdr_size, &hdr_size_len)) {
2057 return ARCHIVE_EOF;
2058 }
1681
2059
1682 /* Sanity check, maximum header size for RAR5 is 2MB. */
1683 if(raw_hdr_size > (2 * 1024 * 1024)) {
1684 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1685 "Base block header is too large");
2060 /* Sanity check, maximum header size for RAR5 is 2MB. */
2061 if(raw_hdr_size > (2 * 1024 * 1024)) {
2062 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2063 "Base block header is too large");
1686
2064
1687 return ARCHIVE_FATAL;
1688 }
2065 return ARCHIVE_FATAL;
2066 }
1689
2067
1690 hdr_size = raw_hdr_size + hdr_size_len;
2068 hdr_size = raw_hdr_size + hdr_size_len;
1691
2069
1692 /* Read the whole header data into memory, maximum memory use here is
1693 * 2MB. */
1694 if(!read_ahead(a, hdr_size, &p)) {
1695 return ARCHIVE_EOF;
1696 }
2070 /* Read the whole header data into memory, maximum memory use here is
2071 * 2MB. */
2072 if(!read_ahead(a, hdr_size, &p)) {
2073 return ARCHIVE_EOF;
2074 }
1697
2075
1698 /* Verify the CRC32 of the header data. */
1699 computed_crc = (uint32_t) crc32(0, p, (int) hdr_size);
1700 if(computed_crc != hdr_crc) {
1701 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1702 "Header CRC error");
2076 /* Verify the CRC32 of the header data. */
2077 computed_crc = (uint32_t) crc32(0, p, (int) hdr_size);
2078 if(computed_crc != hdr_crc) {
2079 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2080 "Header CRC error");
1703
2081
1704 return ARCHIVE_FATAL;
1705 }
2082 return ARCHIVE_FATAL;
2083 }
1706
2084
1707 /* If the checksum is OK, we proceed with parsing. */
1708 if(ARCHIVE_OK != consume(a, hdr_size_len)) {
1709 return ARCHIVE_EOF;
1710 }
2085 /* If the checksum is OK, we proceed with parsing. */
2086 if(ARCHIVE_OK != consume(a, hdr_size_len)) {
2087 return ARCHIVE_EOF;
2088 }
1711
2089
1712 if(!read_var_sized(a, &header_id, NULL))
1713 return ARCHIVE_EOF;
2090 if(!read_var_sized(a, &header_id, NULL))
2091 return ARCHIVE_EOF;
1714
2092
1715 if(!read_var_sized(a, &header_flags, NULL))
1716 return ARCHIVE_EOF;
2093 if(!read_var_sized(a, &header_flags, NULL))
2094 return ARCHIVE_EOF;
1717
2095
1718 rar->generic.split_after = (header_flags & HFL_SPLIT_AFTER) > 0;
1719 rar->generic.split_before = (header_flags & HFL_SPLIT_BEFORE) > 0;
1720 rar->generic.size = (int)hdr_size;
1721 rar->generic.last_header_id = (int)header_id;
1722 rar->main.endarc = 0;
2096 rar->generic.split_after = (header_flags & HFL_SPLIT_AFTER) > 0;
2097 rar->generic.split_before = (header_flags & HFL_SPLIT_BEFORE) > 0;
2098 rar->generic.size = (int)hdr_size;
2099 rar->generic.last_header_id = (int)header_id;
2100 rar->main.endarc = 0;
1723
2101
1724 /* Those are possible header ids in RARv5. */
1725 enum HEADER_TYPE {
1726 HEAD_MARK = 0x00, HEAD_MAIN = 0x01, HEAD_FILE = 0x02,
1727 HEAD_SERVICE = 0x03, HEAD_CRYPT = 0x04, HEAD_ENDARC = 0x05,
1728 HEAD_UNKNOWN = 0xff,
1729 };
2102 /* Those are possible header ids in RARv5. */
2103 enum HEADER_TYPE {
2104 HEAD_MARK = 0x00, HEAD_MAIN = 0x01, HEAD_FILE = 0x02,
2105 HEAD_SERVICE = 0x03, HEAD_CRYPT = 0x04, HEAD_ENDARC = 0x05,
2106 HEAD_UNKNOWN = 0xff,
2107 };
1730
2108
1731 switch(header_id) {
1732 case HEAD_MAIN:
1733 ret = process_head_main(a, rar, entry, header_flags);
2109 switch(header_id) {
2110 case HEAD_MAIN:
2111 ret = process_head_main(a, rar, entry, header_flags);
1734
2112
1735 /* Main header doesn't have any files in it, so it's pointless
1736 * to return to the caller. Retry to next header, which should be
1737 * HEAD_FILE/HEAD_SERVICE. */
1738 if(ret == ARCHIVE_OK)
1739 return ARCHIVE_RETRY;
2113 /* Main header doesn't have any files in it, so it's
2114 * pointless to return to the caller. Retry to next
2115 * header, which should be HEAD_FILE/HEAD_SERVICE. */
2116 if(ret == ARCHIVE_OK)
2117 return ARCHIVE_RETRY;
1740
2118
1741 return ret;
1742 case HEAD_SERVICE:
1743 ret = process_head_service(a, rar, entry, header_flags);
1744 return ret;
1745 case HEAD_FILE:
1746 ret = process_head_file(a, rar, entry, header_flags);
1747 return ret;
1748 case HEAD_CRYPT:
1749 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1750 "Encryption is not supported");
1751 return ARCHIVE_FATAL;
1752 case HEAD_ENDARC:
1753 rar->main.endarc = 1;
2119 return ret;
2120 case HEAD_SERVICE:
2121 ret = process_head_service(a, rar, entry, header_flags);
2122 return ret;
2123 case HEAD_FILE:
2124 ret = process_head_file(a, rar, entry, header_flags);
2125 return ret;
2126 case HEAD_CRYPT:
2127 archive_set_error(&a->archive,
2128 ARCHIVE_ERRNO_FILE_FORMAT,
2129 "Encryption is not supported");
2130 return ARCHIVE_FATAL;
2131 case HEAD_ENDARC:
2132 rar->main.endarc = 1;
1754
2133
1755 /* After encountering an end of file marker, we need to take
1756 * into consideration if this archive is continued in another
1757 * file (i.e. is it part01.rar: is there a part02.rar?) */
1758 if(rar->main.volume) {
1759 /* In case there is part02.rar, position the read pointer
1760 * in a proper place, so we can resume parsing. */
2134 /* After encountering an end of file marker, we need
2135 * to take into consideration if this archive is
2136 * continued in another file (i.e. is it part01.rar:
2137 * is there a part02.rar?) */
2138 if(rar->main.volume) {
2139 /* In case there is part02.rar, position the
2140 * read pointer in a proper place, so we can
2141 * resume parsing. */
2142 ret = scan_for_signature(a);
2143 if(ret == ARCHIVE_FATAL) {
2144 return ARCHIVE_EOF;
2145 } else {
2146 if(rar->vol.expected_vol_no ==
2147 UINT_MAX) {
2148 archive_set_error(&a->archive,
2149 ARCHIVE_ERRNO_FILE_FORMAT,
2150 "Header error");
2151 return ARCHIVE_FATAL;
2152 }
1761
2153
1762 ret = scan_for_signature(a);
1763 if(ret == ARCHIVE_FATAL) {
1764 return ARCHIVE_EOF;
1765 } else {
1766 rar->vol.expected_vol_no = rar->main.vol_no + 1;
1767 return ARCHIVE_OK;
1768 }
1769 } else {
1770 return ARCHIVE_EOF;
1771 }
1772 case HEAD_MARK:
1773 return ARCHIVE_EOF;
1774 default:
1775 if((header_flags & HFL_SKIP_IF_UNKNOWN) == 0) {
1776 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1777 "Header type error");
1778 return ARCHIVE_FATAL;
1779 } else {
1780 /* If the block is marked as 'skip if unknown', do as the flag
1781 * says: skip the block instead on failing on it. */
1782 return ARCHIVE_RETRY;
1783 }
1784 }
2154 rar->vol.expected_vol_no =
2155 rar->main.vol_no + 1;
2156 return ARCHIVE_OK;
2157 }
2158 } else {
2159 return ARCHIVE_EOF;
2160 }
2161 case HEAD_MARK:
2162 return ARCHIVE_EOF;
2163 default:
2164 if((header_flags & HFL_SKIP_IF_UNKNOWN) == 0) {
2165 archive_set_error(&a->archive,
2166 ARCHIVE_ERRNO_FILE_FORMAT,
2167 "Header type error");
2168 return ARCHIVE_FATAL;
2169 } else {
2170 /* If the block is marked as 'skip if unknown',
2171 * do as the flag says: skip the block
2172 * instead on failing on it. */
2173 return ARCHIVE_RETRY;
2174 }
2175 }
1785
1786#if !defined WIN32
2176
2177#if !defined WIN32
1787 // Not reached.
1788 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1789 "Internal unpacker error");
1790 return ARCHIVE_FATAL;
2178 // Not reached.
2179 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2180 "Internal unpacker error");
2181 return ARCHIVE_FATAL;
1791#endif
1792}
1793
1794static int skip_base_block(struct archive_read* a) {
2182#endif
2183}
2184
2185static int skip_base_block(struct archive_read* a) {
1795 int ret;
1796 struct rar5* rar = get_context(a);
2186 int ret;
2187 struct rar5* rar = get_context(a);
1797
2188
1798 /* Create a new local archive_entry structure that will be operated on
1799 * by header reader; operations on this archive_entry will be discarded.
1800 */
1801 struct archive_entry* entry = archive_entry_new();
1802 ret = process_base_block(a, entry);
2189 /* Create a new local archive_entry structure that will be operated on
2190 * by header reader; operations on this archive_entry will be discarded.
2191 */
2192 struct archive_entry* entry = archive_entry_new();
2193 ret = process_base_block(a, entry);
1803
2194
1804 /* Discard operations on this archive_entry structure. */
1805 archive_entry_free(entry);
2195 /* Discard operations on this archive_entry structure. */
2196 archive_entry_free(entry);
2197 if(ret == ARCHIVE_FATAL)
2198 return ret;
1806
2199
1807 if(rar->generic.last_header_id == 2 && rar->generic.split_before > 0)
1808 return ARCHIVE_OK;
2200 if(rar->generic.last_header_id == 2 && rar->generic.split_before > 0)
2201 return ARCHIVE_OK;
1809
2202
1810 if(ret == ARCHIVE_OK)
1811 return ARCHIVE_RETRY;
1812 else
1813 return ret;
2203 if(ret == ARCHIVE_OK)
2204 return ARCHIVE_RETRY;
2205 else
2206 return ret;
1814}
1815
1816static int rar5_read_header(struct archive_read *a,
2207}
2208
2209static int rar5_read_header(struct archive_read *a,
1817 struct archive_entry *entry)
2210 struct archive_entry *entry)
1818{
2211{
1819 struct rar5* rar = get_context(a);
1820 int ret;
2212 struct rar5* rar = get_context(a);
2213 int ret;
1821
2214
1822 if(rar->header_initialized == 0) {
1823 init_header(a);
1824 rar->header_initialized = 1;
1825 }
2215 if(rar->header_initialized == 0) {
2216 init_header(a);
2217 rar->header_initialized = 1;
2218 }
1826
2219
1827 if(rar->skipped_magic == 0) {
1828 if(ARCHIVE_OK != consume(a, rar5_signature_size)) {
1829 return ARCHIVE_EOF;
1830 }
2220 if(rar->skipped_magic == 0) {
2221 if(ARCHIVE_OK != consume(a, rar5_signature_size)) {
2222 return ARCHIVE_EOF;
2223 }
1831
2224
1832 rar->skipped_magic = 1;
1833 }
2225 rar->skipped_magic = 1;
2226 }
1834
2227
1835 do {
1836 ret = process_base_block(a, entry);
1837 } while(ret == ARCHIVE_RETRY ||
1838 (rar->main.endarc > 0 && ret == ARCHIVE_OK));
2228 do {
2229 ret = process_base_block(a, entry);
2230 } while(ret == ARCHIVE_RETRY ||
2231 (rar->main.endarc > 0 && ret == ARCHIVE_OK));
1839
2232
1840 return ret;
2233 return ret;
1841}
1842
1843static void init_unpack(struct rar5* rar) {
2234}
2235
2236static void init_unpack(struct rar5* rar) {
1844 rar->file.calculated_crc32 = 0;
1845 if (rar->cstate.window_size)
1846 rar->cstate.window_mask = rar->cstate.window_size - 1;
1847 else
1848 rar->cstate.window_mask = 0;
2237 rar->file.calculated_crc32 = 0;
2238 if (rar->cstate.window_size)
2239 rar->cstate.window_mask = rar->cstate.window_size - 1;
2240 else
2241 rar->cstate.window_mask = 0;
1849
2242
1850 free(rar->cstate.window_buf);
2243 free(rar->cstate.window_buf);
2244 free(rar->cstate.filtered_buf);
1851
2245
1852 free(rar->cstate.filtered_buf);
2246 if(rar->cstate.window_size > 0) {
2247 rar->cstate.window_buf = calloc(1, rar->cstate.window_size);
2248 rar->cstate.filtered_buf = calloc(1, rar->cstate.window_size);
2249 } else {
2250 rar->cstate.window_buf = NULL;
2251 rar->cstate.filtered_buf = NULL;
2252 }
1853
2253
1854 rar->cstate.window_buf = calloc(1, rar->cstate.window_size);
1855 rar->cstate.filtered_buf = calloc(1, rar->cstate.window_size);
2254 rar->cstate.write_ptr = 0;
2255 rar->cstate.last_write_ptr = 0;
1856
2256
1857 rar->cstate.write_ptr = 0;
1858 rar->cstate.last_write_ptr = 0;
1859
1860 memset(&rar->cstate.bd, 0, sizeof(rar->cstate.bd));
1861 memset(&rar->cstate.ld, 0, sizeof(rar->cstate.ld));
1862 memset(&rar->cstate.dd, 0, sizeof(rar->cstate.dd));
1863 memset(&rar->cstate.ldd, 0, sizeof(rar->cstate.ldd));
1864 memset(&rar->cstate.rd, 0, sizeof(rar->cstate.rd));
2257 memset(&rar->cstate.bd, 0, sizeof(rar->cstate.bd));
2258 memset(&rar->cstate.ld, 0, sizeof(rar->cstate.ld));
2259 memset(&rar->cstate.dd, 0, sizeof(rar->cstate.dd));
2260 memset(&rar->cstate.ldd, 0, sizeof(rar->cstate.ldd));
2261 memset(&rar->cstate.rd, 0, sizeof(rar->cstate.rd));
1865}
1866
1867static void update_crc(struct rar5* rar, const uint8_t* p, size_t to_read) {
1868 int verify_crc;
1869
2262}
2263
2264static void update_crc(struct rar5* rar, const uint8_t* p, size_t to_read) {
2265 int verify_crc;
2266
1870 if(rar->skip_mode) {
2267 if(rar->skip_mode) {
1871#if defined CHECK_CRC_ON_SOLID_SKIP
2268#if defined CHECK_CRC_ON_SOLID_SKIP
1872 verify_crc = 1;
2269 verify_crc = 1;
1873#else
2270#else
1874 verify_crc = 0;
2271 verify_crc = 0;
1875#endif
2272#endif
1876 } else
1877 verify_crc = 1;
2273 } else
2274 verify_crc = 1;
1878
2275
1879 if(verify_crc) {
1880 /* Don't update CRC32 if the file doesn't have the `stored_crc32` info
1881 filled in. */
1882 if(rar->file.stored_crc32 > 0) {
1883 rar->file.calculated_crc32 =
1884 crc32(rar->file.calculated_crc32, p, to_read);
1885 }
2276 if(verify_crc) {
2277 /* Don't update CRC32 if the file doesn't have the
2278 * `stored_crc32` info filled in. */
2279 if(rar->file.stored_crc32 > 0) {
2280 rar->file.calculated_crc32 =
2281 crc32(rar->file.calculated_crc32, p, to_read);
2282 }
1886
2283
1887 /* Check if the file uses an optional BLAKE2sp checksum algorithm. */
1888 if(rar->file.has_blake2 > 0) {
1889 /* Return value of the `update` function is always 0, so we can
1890 * explicitly ignore it here. */
1891 (void) blake2sp_update(&rar->file.b2state, p, to_read);
1892 }
1893 }
2284 /* Check if the file uses an optional BLAKE2sp checksum
2285 * algorithm. */
2286 if(rar->file.has_blake2 > 0) {
2287 /* Return value of the `update` function is always 0,
2288 * so we can explicitly ignore it here. */
2289 (void) blake2sp_update(&rar->file.b2state, p, to_read);
2290 }
2291 }
1894}
1895
1896static int create_decode_tables(uint8_t* bit_length,
2292}
2293
2294static int create_decode_tables(uint8_t* bit_length,
1897 struct decode_table* table,
1898 int size)
2295 struct decode_table* table, int size)
1899{
2296{
1900 int code, upper_limit = 0, i, lc[16];
1901 uint32_t decode_pos_clone[rar5_countof(table->decode_pos)];
1902 ssize_t cur_len, quick_data_size;
2297 int code, upper_limit = 0, i, lc[16];
2298 uint32_t decode_pos_clone[rar5_countof(table->decode_pos)];
2299 ssize_t cur_len, quick_data_size;
1903
2300
1904 memset(&lc, 0, sizeof(lc));
1905 memset(table->decode_num, 0, sizeof(table->decode_num));
1906 table->size = size;
1907 table->quick_bits = size == HUFF_NC ? 10 : 7;
2301 memset(&lc, 0, sizeof(lc));
2302 memset(table->decode_num, 0, sizeof(table->decode_num));
2303 table->size = size;
2304 table->quick_bits = size == HUFF_NC ? 10 : 7;
1908
2305
1909 for(i = 0; i < size; i++) {
1910 lc[bit_length[i] & 15]++;
1911 }
2306 for(i = 0; i < size; i++) {
2307 lc[bit_length[i] & 15]++;
2308 }
1912
2309
1913 lc[0] = 0;
1914 table->decode_pos[0] = 0;
1915 table->decode_len[0] = 0;
2310 lc[0] = 0;
2311 table->decode_pos[0] = 0;
2312 table->decode_len[0] = 0;
1916
2313
1917 for(i = 1; i < 16; i++) {
1918 upper_limit += lc[i];
2314 for(i = 1; i < 16; i++) {
2315 upper_limit += lc[i];
1919
2316
1920 table->decode_len[i] = upper_limit << (16 - i);
1921 table->decode_pos[i] = table->decode_pos[i - 1] + lc[i - 1];
2317 table->decode_len[i] = upper_limit << (16 - i);
2318 table->decode_pos[i] = table->decode_pos[i - 1] + lc[i - 1];
1922
2319
1923 upper_limit <<= 1;
1924 }
2320 upper_limit <<= 1;
2321 }
1925
2322
1926 memcpy(decode_pos_clone, table->decode_pos, sizeof(decode_pos_clone));
2323 memcpy(decode_pos_clone, table->decode_pos, sizeof(decode_pos_clone));
1927
2324
1928 for(i = 0; i < size; i++) {
1929 uint8_t clen = bit_length[i] & 15;
1930 if(clen > 0) {
1931 int last_pos = decode_pos_clone[clen];
1932 table->decode_num[last_pos] = i;
1933 decode_pos_clone[clen]++;
1934 }
1935 }
2325 for(i = 0; i < size; i++) {
2326 uint8_t clen = bit_length[i] & 15;
2327 if(clen > 0) {
2328 int last_pos = decode_pos_clone[clen];
2329 table->decode_num[last_pos] = i;
2330 decode_pos_clone[clen]++;
2331 }
2332 }
1936
2333
1937 quick_data_size = (int64_t)1 << table->quick_bits;
1938 cur_len = 1;
1939 for(code = 0; code < quick_data_size; code++) {
1940 int bit_field = code << (16 - table->quick_bits);
1941 int dist, pos;
2334 quick_data_size = (int64_t)1 << table->quick_bits;
2335 cur_len = 1;
2336 for(code = 0; code < quick_data_size; code++) {
2337 int bit_field = code << (16 - table->quick_bits);
2338 int dist, pos;
1942
2339
1943 while(cur_len < rar5_countof(table->decode_len) &&
1944 bit_field >= table->decode_len[cur_len]) {
1945 cur_len++;
1946 }
2340 while(cur_len < rar5_countof(table->decode_len) &&
2341 bit_field >= table->decode_len[cur_len]) {
2342 cur_len++;
2343 }
1947
2344
1948 table->quick_len[code] = (uint8_t) cur_len;
2345 table->quick_len[code] = (uint8_t) cur_len;
1949
2346
1950 dist = bit_field - table->decode_len[cur_len - 1];
1951 dist >>= (16 - cur_len);
2347 dist = bit_field - table->decode_len[cur_len - 1];
2348 dist >>= (16 - cur_len);
1952
2349
1953 pos = table->decode_pos[cur_len & 15] + dist;
1954 if(cur_len < rar5_countof(table->decode_pos) && pos < size) {
1955 table->quick_num[code] = table->decode_num[pos];
1956 } else {
1957 table->quick_num[code] = 0;
1958 }
1959 }
2350 pos = table->decode_pos[cur_len & 15] + dist;
2351 if(cur_len < rar5_countof(table->decode_pos) && pos < size) {
2352 table->quick_num[code] = table->decode_num[pos];
2353 } else {
2354 table->quick_num[code] = 0;
2355 }
2356 }
1960
2357
1961 return ARCHIVE_OK;
2358 return ARCHIVE_OK;
1962}
1963
1964static int decode_number(struct archive_read* a, struct decode_table* table,
2359}
2360
2361static int decode_number(struct archive_read* a, struct decode_table* table,
1965 const uint8_t* p, uint16_t* num)
2362 const uint8_t* p, uint16_t* num)
1966{
2363{
1967 int i, bits, dist;
1968 uint16_t bitfield;
1969 uint32_t pos;
1970 struct rar5* rar = get_context(a);
2364 int i, bits, dist;
2365 uint16_t bitfield;
2366 uint32_t pos;
2367 struct rar5* rar = get_context(a);
1971
2368
1972 if(ARCHIVE_OK != read_bits_16(rar, p, &bitfield)) {
1973 return ARCHIVE_EOF;
1974 }
2369 if(ARCHIVE_OK != read_bits_16(rar, p, &bitfield)) {
2370 return ARCHIVE_EOF;
2371 }
1975
2372
1976 bitfield &= 0xfffe;
2373 bitfield &= 0xfffe;
1977
2374
1978 if(bitfield < table->decode_len[table->quick_bits]) {
1979 int code = bitfield >> (16 - table->quick_bits);
1980 skip_bits(rar, table->quick_len[code]);
1981 *num = table->quick_num[code];
1982 return ARCHIVE_OK;
1983 }
2375 if(bitfield < table->decode_len[table->quick_bits]) {
2376 int code = bitfield >> (16 - table->quick_bits);
2377 skip_bits(rar, table->quick_len[code]);
2378 *num = table->quick_num[code];
2379 return ARCHIVE_OK;
2380 }
1984
2381
1985 bits = 15;
2382 bits = 15;
1986
2383
1987 for(i = table->quick_bits + 1; i < 15; i++) {
1988 if(bitfield < table->decode_len[i]) {
1989 bits = i;
1990 break;
1991 }
1992 }
2384 for(i = table->quick_bits + 1; i < 15; i++) {
2385 if(bitfield < table->decode_len[i]) {
2386 bits = i;
2387 break;
2388 }
2389 }
1993
2390
1994 skip_bits(rar, bits);
2391 skip_bits(rar, bits);
1995
2392
1996 dist = bitfield - table->decode_len[bits - 1];
1997 dist >>= (16 - bits);
1998 pos = table->decode_pos[bits] + dist;
2393 dist = bitfield - table->decode_len[bits - 1];
2394 dist >>= (16 - bits);
2395 pos = table->decode_pos[bits] + dist;
1999
2396
2000 if(pos >= table->size)
2001 pos = 0;
2397 if(pos >= table->size)
2398 pos = 0;
2002
2399
2003 *num = table->decode_num[pos];
2004 return ARCHIVE_OK;
2400 *num = table->decode_num[pos];
2401 return ARCHIVE_OK;
2005}
2006
2007/* Reads and parses Huffman tables from the beginning of the block. */
2008static int parse_tables(struct archive_read* a, struct rar5* rar,
2402}
2403
2404/* Reads and parses Huffman tables from the beginning of the block. */
2405static int parse_tables(struct archive_read* a, struct rar5* rar,
2009 const uint8_t* p)
2406 const uint8_t* p)
2010{
2407{
2011 int ret, value, i, w, idx = 0;
2012 uint8_t bit_length[HUFF_BC],
2013 table[HUFF_TABLE_SIZE],
2014 nibble_mask = 0xF0,
2015 nibble_shift = 4;
2408 int ret, value, i, w, idx = 0;
2409 uint8_t bit_length[HUFF_BC],
2410 table[HUFF_TABLE_SIZE],
2411 nibble_mask = 0xF0,
2412 nibble_shift = 4;
2016
2413
2017 enum { ESCAPE = 15 };
2414 enum { ESCAPE = 15 };
2018
2415
2019 /* The data for table generation is compressed using a simple RLE-like
2020 * algorithm when storing zeroes, so we need to unpack it first. */
2021 for(w = 0, i = 0; w < HUFF_BC;) {
2022 value = (p[i] & nibble_mask) >> nibble_shift;
2416 /* The data for table generation is compressed using a simple RLE-like
2417 * algorithm when storing zeroes, so we need to unpack it first. */
2418 for(w = 0, i = 0; w < HUFF_BC;) {
2419 if(i >= rar->cstate.cur_block_size) {
2420 /* Truncated data, can't continue. */
2421 archive_set_error(&a->archive,
2422 ARCHIVE_ERRNO_FILE_FORMAT,
2423 "Truncated data in huffman tables");
2424 return ARCHIVE_FATAL;
2425 }
2023
2426
2024 if(nibble_mask == 0x0F)
2025 ++i;
2427 value = (p[i] & nibble_mask) >> nibble_shift;
2026
2428
2027 nibble_mask ^= 0xFF;
2028 nibble_shift ^= 4;
2429 if(nibble_mask == 0x0F)
2430 ++i;
2029
2431
2030 /* Values smaller than 15 is data, so we write it directly. Value 15
2031 * is a flag telling us that we need to unpack more bytes. */
2032 if(value == ESCAPE) {
2033 value = (p[i] & nibble_mask) >> nibble_shift;
2034 if(nibble_mask == 0x0F)
2035 ++i;
2036 nibble_mask ^= 0xFF;
2037 nibble_shift ^= 4;
2432 nibble_mask ^= 0xFF;
2433 nibble_shift ^= 4;
2038
2434
2039 if(value == 0) {
2040 /* We sometimes need to write the actual value of 15, so this
2041 * case handles that. */
2042 bit_length[w++] = ESCAPE;
2043 } else {
2044 int k;
2435 /* Values smaller than 15 is data, so we write it directly.
2436 * Value 15 is a flag telling us that we need to unpack more
2437 * bytes. */
2438 if(value == ESCAPE) {
2439 value = (p[i] & nibble_mask) >> nibble_shift;
2440 if(nibble_mask == 0x0F)
2441 ++i;
2442 nibble_mask ^= 0xFF;
2443 nibble_shift ^= 4;
2045
2444
2046 /* Fill zeroes. */
2047 for(k = 0; k < value + 2; k++) {
2048 bit_length[w++] = 0;
2049 }
2050 }
2051 } else {
2052 bit_length[w++] = value;
2053 }
2054 }
2445 if(value == 0) {
2446 /* We sometimes need to write the actual value
2447 * of 15, so this case handles that. */
2448 bit_length[w++] = ESCAPE;
2449 } else {
2450 int k;
2055
2451
2056 rar->bits.in_addr = i;
2057 rar->bits.bit_addr = nibble_shift ^ 4;
2452 /* Fill zeroes. */
2453 for(k = 0; (k < value + 2) && (w < HUFF_BC);
2454 k++) {
2455 bit_length[w++] = 0;
2456 }
2457 }
2458 } else {
2459 bit_length[w++] = value;
2460 }
2461 }
2058
2462
2059 ret = create_decode_tables(bit_length, &rar->cstate.bd, HUFF_BC);
2060 if(ret != ARCHIVE_OK) {
2061 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2062 "Decoding huffman tables failed");
2063 return ARCHIVE_FATAL;
2064 }
2463 rar->bits.in_addr = i;
2464 rar->bits.bit_addr = nibble_shift ^ 4;
2065
2465
2066 for(i = 0; i < HUFF_TABLE_SIZE;) {
2067 uint16_t num;
2466 ret = create_decode_tables(bit_length, &rar->cstate.bd, HUFF_BC);
2467 if(ret != ARCHIVE_OK) {
2468 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2469 "Decoding huffman tables failed");
2470 return ARCHIVE_FATAL;
2471 }
2068
2472
2069 ret = decode_number(a, &rar->cstate.bd, p, &num);
2070 if(ret != ARCHIVE_OK) {
2071 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2072 "Decoding huffman tables failed");
2073 return ARCHIVE_FATAL;
2074 }
2473 for(i = 0; i < HUFF_TABLE_SIZE;) {
2474 uint16_t num;
2075
2475
2076 if(num < 16) {
2077 /* 0..15: store directly */
2078 table[i] = (uint8_t) num;
2079 i++;
2080 continue;
2081 }
2476 if((rar->bits.in_addr + 6) >= rar->cstate.cur_block_size) {
2477 /* Truncated data, can't continue. */
2478 archive_set_error(&a->archive,
2479 ARCHIVE_ERRNO_FILE_FORMAT,
2480 "Truncated data in huffman tables (#2)");
2481 return ARCHIVE_FATAL;
2482 }
2082
2483
2083 if(num < 18) {
2084 /* 16..17: repeat previous code */
2085 uint16_t n;
2086 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2087 return ARCHIVE_EOF;
2484 ret = decode_number(a, &rar->cstate.bd, p, &num);
2485 if(ret != ARCHIVE_OK) {
2486 archive_set_error(&a->archive,
2487 ARCHIVE_ERRNO_FILE_FORMAT,
2488 "Decoding huffman tables failed");
2489 return ARCHIVE_FATAL;
2490 }
2088
2491
2089 if(num == 16) {
2090 n >>= 13;
2091 n += 3;
2092 skip_bits(rar, 3);
2093 } else {
2094 n >>= 9;
2095 n += 11;
2096 skip_bits(rar, 7);
2097 }
2492 if(num < 16) {
2493 /* 0..15: store directly */
2494 table[i] = (uint8_t) num;
2495 i++;
2496 continue;
2497 }
2098
2498
2099 if(i > 0) {
2100 while(n-- > 0 && i < HUFF_TABLE_SIZE) {
2101 table[i] = table[i - 1];
2102 i++;
2103 }
2104 } else {
2105 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2106 "Unexpected error when decoding huffman tables");
2107 return ARCHIVE_FATAL;
2108 }
2499 if(num < 18) {
2500 /* 16..17: repeat previous code */
2501 uint16_t n;
2502 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2503 return ARCHIVE_EOF;
2109
2504
2110 continue;
2111 }
2505 if(num == 16) {
2506 n >>= 13;
2507 n += 3;
2508 skip_bits(rar, 3);
2509 } else {
2510 n >>= 9;
2511 n += 11;
2512 skip_bits(rar, 7);
2513 }
2112
2514
2113 /* other codes: fill with zeroes `n` times */
2114 uint16_t n;
2115 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2116 return ARCHIVE_EOF;
2515 if(i > 0) {
2516 while(n-- > 0 && i < HUFF_TABLE_SIZE) {
2517 table[i] = table[i - 1];
2518 i++;
2519 }
2520 } else {
2521 archive_set_error(&a->archive,
2522 ARCHIVE_ERRNO_FILE_FORMAT,
2523 "Unexpected error when decoding "
2524 "huffman tables");
2525 return ARCHIVE_FATAL;
2526 }
2117
2527
2118 if(num == 18) {
2119 n >>= 13;
2120 n += 3;
2121 skip_bits(rar, 3);
2122 } else {
2123 n >>= 9;
2124 n += 11;
2125 skip_bits(rar, 7);
2126 }
2528 continue;
2529 }
2127
2530
2128 while(n-- > 0 && i < HUFF_TABLE_SIZE)
2129 table[i++] = 0;
2130 }
2531 /* other codes: fill with zeroes `n` times */
2532 uint16_t n;
2533 if(ARCHIVE_OK != read_bits_16(rar, p, &n))
2534 return ARCHIVE_EOF;
2131
2535
2132 ret = create_decode_tables(&table[idx], &rar->cstate.ld, HUFF_NC);
2133 if(ret != ARCHIVE_OK) {
2134 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2135 "Failed to create literal table");
2136 return ARCHIVE_FATAL;
2137 }
2536 if(num == 18) {
2537 n >>= 13;
2538 n += 3;
2539 skip_bits(rar, 3);
2540 } else {
2541 n >>= 9;
2542 n += 11;
2543 skip_bits(rar, 7);
2544 }
2138
2545
2139 idx += HUFF_NC;
2546 while(n-- > 0 && i < HUFF_TABLE_SIZE)
2547 table[i++] = 0;
2548 }
2140
2549
2141 ret = create_decode_tables(&table[idx], &rar->cstate.dd, HUFF_DC);
2142 if(ret != ARCHIVE_OK) {
2143 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2144 "Failed to create distance table");
2145 return ARCHIVE_FATAL;
2146 }
2550 ret = create_decode_tables(&table[idx], &rar->cstate.ld, HUFF_NC);
2551 if(ret != ARCHIVE_OK) {
2552 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2553 "Failed to create literal table");
2554 return ARCHIVE_FATAL;
2555 }
2147
2556
2148 idx += HUFF_DC;
2557 idx += HUFF_NC;
2149
2558
2150 ret = create_decode_tables(&table[idx], &rar->cstate.ldd, HUFF_LDC);
2151 if(ret != ARCHIVE_OK) {
2152 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2153 "Failed to create lower bits of distances table");
2154 return ARCHIVE_FATAL;
2155 }
2559 ret = create_decode_tables(&table[idx], &rar->cstate.dd, HUFF_DC);
2560 if(ret != ARCHIVE_OK) {
2561 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2562 "Failed to create distance table");
2563 return ARCHIVE_FATAL;
2564 }
2156
2565
2157 idx += HUFF_LDC;
2566 idx += HUFF_DC;
2158
2567
2159 ret = create_decode_tables(&table[idx], &rar->cstate.rd, HUFF_RC);
2160 if(ret != ARCHIVE_OK) {
2161 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2162 "Failed to create repeating distances table");
2163 return ARCHIVE_FATAL;
2164 }
2568 ret = create_decode_tables(&table[idx], &rar->cstate.ldd, HUFF_LDC);
2569 if(ret != ARCHIVE_OK) {
2570 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2571 "Failed to create lower bits of distances table");
2572 return ARCHIVE_FATAL;
2573 }
2165
2574
2166 return ARCHIVE_OK;
2575 idx += HUFF_LDC;
2576
2577 ret = create_decode_tables(&table[idx], &rar->cstate.rd, HUFF_RC);
2578 if(ret != ARCHIVE_OK) {
2579 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2580 "Failed to create repeating distances table");
2581 return ARCHIVE_FATAL;
2582 }
2583
2584 return ARCHIVE_OK;
2167}
2168
2169/* Parses the block header, verifies its CRC byte, and saves the header
2170 * fields inside the `hdr` pointer. */
2171static int parse_block_header(struct archive_read* a, const uint8_t* p,
2585}
2586
2587/* Parses the block header, verifies its CRC byte, and saves the header
2588 * fields inside the `hdr` pointer. */
2589static int parse_block_header(struct archive_read* a, const uint8_t* p,
2172 ssize_t* block_size, struct compressed_block_header* hdr)
2590 ssize_t* block_size, struct compressed_block_header* hdr)
2173{
2591{
2174 memcpy(hdr, p, sizeof(struct compressed_block_header));
2592 memcpy(hdr, p, sizeof(struct compressed_block_header));
2175
2593
2176 if(bf_byte_count(hdr) > 2) {
2177 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2178 "Unsupported block header size (was %d, max is 2)",
2179 bf_byte_count(hdr));
2180 return ARCHIVE_FATAL;
2181 }
2594 if(bf_byte_count(hdr) > 2) {
2595 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2596 "Unsupported block header size (was %d, max is 2)",
2597 bf_byte_count(hdr));
2598 return ARCHIVE_FATAL;
2599 }
2182
2600
2183 /* This should probably use bit reader interface in order to be more
2184 * future-proof. */
2185 *block_size = 0;
2186 switch(bf_byte_count(hdr)) {
2187 /* 1-byte block size */
2188 case 0:
2189 *block_size = *(const uint8_t*) &p[2];
2190 break;
2601 /* This should probably use bit reader interface in order to be more
2602 * future-proof. */
2603 *block_size = 0;
2604 switch(bf_byte_count(hdr)) {
2605 /* 1-byte block size */
2606 case 0:
2607 *block_size = *(const uint8_t*) &p[2];
2608 break;
2191
2609
2192 /* 2-byte block size */
2193 case 1:
2194 *block_size = archive_le16dec(&p[2]);
2195 break;
2610 /* 2-byte block size */
2611 case 1:
2612 *block_size = archive_le16dec(&p[2]);
2613 break;
2196
2614
2197 /* 3-byte block size */
2198 case 2:
2199 *block_size = archive_le32dec(&p[2]);
2200 *block_size &= 0x00FFFFFF;
2201 break;
2615 /* 3-byte block size */
2616 case 2:
2617 *block_size = archive_le32dec(&p[2]);
2618 *block_size &= 0x00FFFFFF;
2619 break;
2202
2620
2203 /* Other block sizes are not supported. This case is not reached,
2204 * because we have an 'if' guard before the switch that makes sure
2205 * of it. */
2206 default:
2207 return ARCHIVE_FATAL;
2208 }
2621 /* Other block sizes are not supported. This case is not
2622 * reached, because we have an 'if' guard before the switch
2623 * that makes sure of it. */
2624 default:
2625 return ARCHIVE_FATAL;
2626 }
2209
2627
2210 /* Verify the block header checksum. 0x5A is a magic value and is always
2211 * constant. */
2212 uint8_t calculated_cksum = 0x5A
2213 ^ (uint8_t) hdr->block_flags_u8
2214 ^ (uint8_t) *block_size
2215 ^ (uint8_t) (*block_size >> 8)
2216 ^ (uint8_t) (*block_size >> 16);
2628 /* Verify the block header checksum. 0x5A is a magic value and is
2629 * always * constant. */
2630 uint8_t calculated_cksum = 0x5A
2631 ^ (uint8_t) hdr->block_flags_u8
2632 ^ (uint8_t) *block_size
2633 ^ (uint8_t) (*block_size >> 8)
2634 ^ (uint8_t) (*block_size >> 16);
2217
2635
2218 if(calculated_cksum != hdr->block_cksum) {
2219 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2220 "Block checksum error: got 0x%02x, expected 0x%02x",
2221 hdr->block_cksum, calculated_cksum);
2636 if(calculated_cksum != hdr->block_cksum) {
2637 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2638 "Block checksum error: got 0x%x, expected 0x%x",
2639 hdr->block_cksum, calculated_cksum);
2222
2640
2223 return ARCHIVE_FATAL;
2224 }
2641 return ARCHIVE_FATAL;
2642 }
2225
2643
2226 return ARCHIVE_OK;
2644 return ARCHIVE_OK;
2227}
2228
2229/* Convenience function used during filter processing. */
2230static int parse_filter_data(struct rar5* rar, const uint8_t* p,
2645}
2646
2647/* Convenience function used during filter processing. */
2648static int parse_filter_data(struct rar5* rar, const uint8_t* p,
2231 uint32_t* filter_data)
2649 uint32_t* filter_data)
2232{
2650{
2233 int i, bytes;
2234 uint32_t data = 0;
2651 int i, bytes;
2652 uint32_t data = 0;
2235
2653
2236 if(ARCHIVE_OK != read_consume_bits(rar, p, 2, &bytes))
2237 return ARCHIVE_EOF;
2654 if(ARCHIVE_OK != read_consume_bits(rar, p, 2, &bytes))
2655 return ARCHIVE_EOF;
2238
2656
2239 bytes++;
2657 bytes++;
2240
2658
2241 for(i = 0; i < bytes; i++) {
2242 uint16_t byte;
2659 for(i = 0; i < bytes; i++) {
2660 uint16_t byte;
2243
2661
2244 if(ARCHIVE_OK != read_bits_16(rar, p, &byte)) {
2245 return ARCHIVE_EOF;
2246 }
2662 if(ARCHIVE_OK != read_bits_16(rar, p, &byte)) {
2663 return ARCHIVE_EOF;
2664 }
2247
2665
2248 data += (byte >> 8) << (i * 8);
2249 skip_bits(rar, 8);
2250 }
2666 /* Cast to uint32_t will ensure the shift operation will not
2667 * produce undefined result. */
2668 data += ((uint32_t) byte >> 8) << (i * 8);
2669 skip_bits(rar, 8);
2670 }
2251
2671
2252 *filter_data = data;
2253 return ARCHIVE_OK;
2672 *filter_data = data;
2673 return ARCHIVE_OK;
2254}
2255
2256/* Function is used during sanity checking. */
2257static int is_valid_filter_block_start(struct rar5* rar,
2674}
2675
2676/* Function is used during sanity checking. */
2677static int is_valid_filter_block_start(struct rar5* rar,
2258 uint32_t start)
2678 uint32_t start)
2259{
2679{
2260 const int64_t block_start = (ssize_t) start + rar->cstate.write_ptr;
2261 const int64_t last_bs = rar->cstate.last_block_start;
2262 const ssize_t last_bl = rar->cstate.last_block_length;
2680 const int64_t block_start = (ssize_t) start + rar->cstate.write_ptr;
2681 const int64_t last_bs = rar->cstate.last_block_start;
2682 const ssize_t last_bl = rar->cstate.last_block_length;
2263
2683
2264 if(last_bs == 0 || last_bl == 0) {
2265 /* We didn't have any filters yet, so accept this offset. */
2266 return 1;
2267 }
2684 if(last_bs == 0 || last_bl == 0) {
2685 /* We didn't have any filters yet, so accept this offset. */
2686 return 1;
2687 }
2268
2688
2269 if(block_start >= last_bs + last_bl) {
2270 /* Current offset is bigger than last block's end offset, so
2271 * accept current offset. */
2272 return 1;
2273 }
2689 if(block_start >= last_bs + last_bl) {
2690 /* Current offset is bigger than last block's end offset, so
2691 * accept current offset. */
2692 return 1;
2693 }
2274
2694
2275 /* Any other case is not a normal situation and we should fail. */
2276 return 0;
2695 /* Any other case is not a normal situation and we should fail. */
2696 return 0;
2277}
2278
2279/* The function will create a new filter, read its parameters from the input
2280 * stream and add it to the filter collection. */
2281static int parse_filter(struct archive_read* ar, const uint8_t* p) {
2697}
2698
2699/* The function will create a new filter, read its parameters from the input
2700 * stream and add it to the filter collection. */
2701static int parse_filter(struct archive_read* ar, const uint8_t* p) {
2282 uint32_t block_start, block_length;
2283 uint16_t filter_type;
2284 struct rar5* rar = get_context(ar);
2702 uint32_t block_start, block_length;
2703 uint16_t filter_type;
2704 struct rar5* rar = get_context(ar);
2285
2705
2286 /* Read the parameters from the input stream. */
2287 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_start))
2288 return ARCHIVE_EOF;
2706 /* Read the parameters from the input stream. */
2707 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_start))
2708 return ARCHIVE_EOF;
2289
2709
2290 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_length))
2291 return ARCHIVE_EOF;
2710 if(ARCHIVE_OK != parse_filter_data(rar, p, &block_length))
2711 return ARCHIVE_EOF;
2292
2712
2293 if(ARCHIVE_OK != read_bits_16(rar, p, &filter_type))
2294 return ARCHIVE_EOF;
2713 if(ARCHIVE_OK != read_bits_16(rar, p, &filter_type))
2714 return ARCHIVE_EOF;
2295
2715
2296 filter_type >>= 13;
2297 skip_bits(rar, 3);
2716 filter_type >>= 13;
2717 skip_bits(rar, 3);
2298
2718
2299 /* Perform some sanity checks on this filter parameters. Note that we
2300 * allow only DELTA, E8/E9 and ARM filters here, because rest of filters
2301 * are not used in RARv5. */
2719 /* Perform some sanity checks on this filter parameters. Note that we
2720 * allow only DELTA, E8/E9 and ARM filters here, because rest of
2721 * filters are not used in RARv5. */
2302
2722
2303 if(block_length < 4 ||
2304 block_length > 0x400000 ||
2305 filter_type > FILTER_ARM ||
2306 !is_valid_filter_block_start(rar, block_start))
2307 {
2308 archive_set_error(&ar->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Invalid "
2309 "filter encountered");
2310 return ARCHIVE_FATAL;
2311 }
2723 if(block_length < 4 ||
2724 block_length > 0x400000 ||
2725 filter_type > FILTER_ARM ||
2726 !is_valid_filter_block_start(rar, block_start))
2727 {
2728 archive_set_error(&ar->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2729 "Invalid filter encountered");
2730 return ARCHIVE_FATAL;
2731 }
2312
2732
2313 /* Allocate a new filter. */
2314 struct filter_info* filt = add_new_filter(rar);
2315 if(filt == NULL) {
2316 archive_set_error(&ar->archive, ENOMEM, "Can't allocate memory for a "
2317 "filter descriptor.");
2318 return ARCHIVE_FATAL;
2319 }
2733 /* Allocate a new filter. */
2734 struct filter_info* filt = add_new_filter(rar);
2735 if(filt == NULL) {
2736 archive_set_error(&ar->archive, ENOMEM,
2737 "Can't allocate memory for a filter descriptor.");
2738 return ARCHIVE_FATAL;
2739 }
2320
2740
2321 filt->type = filter_type;
2322 filt->block_start = rar->cstate.write_ptr + block_start;
2323 filt->block_length = block_length;
2741 filt->type = filter_type;
2742 filt->block_start = rar->cstate.write_ptr + block_start;
2743 filt->block_length = block_length;
2324
2744
2325 rar->cstate.last_block_start = filt->block_start;
2326 rar->cstate.last_block_length = filt->block_length;
2745 rar->cstate.last_block_start = filt->block_start;
2746 rar->cstate.last_block_length = filt->block_length;
2327
2747
2328 /* Read some more data in case this is a DELTA filter. Other filter types
2329 * don't require any additional data over what was already read. */
2330 if(filter_type == FILTER_DELTA) {
2331 int channels;
2748 /* Read some more data in case this is a DELTA filter. Other filter
2749 * types don't require any additional data over what was already
2750 * read. */
2751 if(filter_type == FILTER_DELTA) {
2752 int channels;
2332
2753
2333 if(ARCHIVE_OK != read_consume_bits(rar, p, 5, &channels))
2334 return ARCHIVE_EOF;
2754 if(ARCHIVE_OK != read_consume_bits(rar, p, 5, &channels))
2755 return ARCHIVE_EOF;
2335
2756
2336 filt->channels = channels + 1;
2337 }
2757 filt->channels = channels + 1;
2758 }
2338
2759
2339 return ARCHIVE_OK;
2760 return ARCHIVE_OK;
2340}
2341
2342static int decode_code_length(struct rar5* rar, const uint8_t* p,
2761}
2762
2763static int decode_code_length(struct rar5* rar, const uint8_t* p,
2343 uint16_t code)
2764 uint16_t code)
2344{
2765{
2345 int lbits, length = 2;
2346 if(code < 8) {
2347 lbits = 0;
2348 length += code;
2349 } else {
2350 lbits = code / 4 - 1;
2351 length += (4 | (code & 3)) << lbits;
2352 }
2766 int lbits, length = 2;
2767 if(code < 8) {
2768 lbits = 0;
2769 length += code;
2770 } else {
2771 lbits = code / 4 - 1;
2772 length += (4 | (code & 3)) << lbits;
2773 }
2353
2774
2354 if(lbits > 0) {
2355 int add;
2775 if(lbits > 0) {
2776 int add;
2356
2777
2357 if(ARCHIVE_OK != read_consume_bits(rar, p, lbits, &add))
2358 return -1;
2778 if(ARCHIVE_OK != read_consume_bits(rar, p, lbits, &add))
2779 return -1;
2359
2780
2360 length += add;
2361 }
2781 length += add;
2782 }
2362
2783
2363 return length;
2784 return length;
2364}
2365
2366static int copy_string(struct archive_read* a, int len, int dist) {
2785}
2786
2787static int copy_string(struct archive_read* a, int len, int dist) {
2367 struct rar5* rar = get_context(a);
2368 const int cmask = (int)rar->cstate.window_mask;
2369 const int64_t write_ptr = rar->cstate.write_ptr + rar->cstate.solid_offset;
2370 int i;
2788 struct rar5* rar = get_context(a);
2789 const uint64_t cmask = rar->cstate.window_mask;
2790 const uint64_t write_ptr = rar->cstate.write_ptr +
2791 rar->cstate.solid_offset;
2792 int i;
2371
2793
2372 /* The unpacker spends most of the time in this function. It would be
2373 * a good idea to introduce some optimizations here.
2374 *
2375 * Just remember that this loop treats buffers that overlap differently
2376 * than buffers that do not overlap. This is why a simple memcpy(3) call
2377 * will not be enough. */
2794 if (rar->cstate.window_buf == NULL)
2795 return ARCHIVE_FATAL;
2378
2796
2379 for(i = 0; i < len; i++) {
2380 const ssize_t write_idx = (write_ptr + i) & cmask;
2381 const ssize_t read_idx = (write_ptr + i - dist) & cmask;
2382 rar->cstate.window_buf[write_idx] = rar->cstate.window_buf[read_idx];
2383 }
2797 /* The unpacker spends most of the time in this function. It would be
2798 * a good idea to introduce some optimizations here.
2799 *
2800 * Just remember that this loop treats buffers that overlap differently
2801 * than buffers that do not overlap. This is why a simple memcpy(3)
2802 * call will not be enough. */
2384
2803
2385 rar->cstate.write_ptr += len;
2386 return ARCHIVE_OK;
2804 for(i = 0; i < len; i++) {
2805 const ssize_t write_idx = (write_ptr + i) & cmask;
2806 const ssize_t read_idx = (write_ptr + i - dist) & cmask;
2807 rar->cstate.window_buf[write_idx] =
2808 rar->cstate.window_buf[read_idx];
2809 }
2810
2811 rar->cstate.write_ptr += len;
2812 return ARCHIVE_OK;
2387}
2388
2389static int do_uncompress_block(struct archive_read* a, const uint8_t* p) {
2813}
2814
2815static int do_uncompress_block(struct archive_read* a, const uint8_t* p) {
2390 struct rar5* rar = get_context(a);
2391 uint16_t num;
2392 int ret;
2816 struct rar5* rar = get_context(a);
2817 uint16_t num;
2818 int ret;
2393
2819
2394 const int cmask = (int)rar->cstate.window_mask;
2395 const struct compressed_block_header* hdr = &rar->last_block_hdr;
2396 const uint8_t bit_size = 1 + bf_bit_size(hdr);
2820 const uint64_t cmask = rar->cstate.window_mask;
2821 const struct compressed_block_header* hdr = &rar->last_block_hdr;
2822 const uint8_t bit_size = 1 + bf_bit_size(hdr);
2397
2823
2398 while(1) {
2399 if(rar->cstate.write_ptr - rar->cstate.last_write_ptr >
2400 (rar->cstate.window_size >> 1)) {
2824 while(1) {
2825 if(rar->cstate.write_ptr - rar->cstate.last_write_ptr >
2826 (rar->cstate.window_size >> 1)) {
2827 /* Don't allow growing data by more than half of the
2828 * window size at a time. In such case, break the loop;
2829 * next call to this function will continue processing
2830 * from this moment. */
2831 break;
2832 }
2401
2833
2402 /* Don't allow growing data by more than half of the window size
2403 * at a time. In such case, break the loop; next call to this
2404 * function will continue processing from this moment. */
2834 if(rar->bits.in_addr > rar->cstate.cur_block_size - 1 ||
2835 (rar->bits.in_addr == rar->cstate.cur_block_size - 1 &&
2836 rar->bits.bit_addr >= bit_size))
2837 {
2838 /* If the program counter is here, it means the
2839 * function has finished processing the block. */
2840 rar->cstate.block_parsing_finished = 1;
2841 break;
2842 }
2405
2843
2406 break;
2407 }
2844 /* Decode the next literal. */
2845 if(ARCHIVE_OK != decode_number(a, &rar->cstate.ld, p, &num)) {
2846 return ARCHIVE_EOF;
2847 }
2408
2848
2409 if(rar->bits.in_addr > rar->cstate.cur_block_size - 1 ||
2410 (rar->bits.in_addr == rar->cstate.cur_block_size - 1 &&
2411 rar->bits.bit_addr >= bit_size))
2412 {
2413 /* If the program counter is here, it means the function has
2414 * finished processing the block. */
2415 rar->cstate.block_parsing_finished = 1;
2416 break;
2417 }
2849 /* Num holds a decompression literal, or 'command code'.
2850 *
2851 * - Values lower than 256 are just bytes. Those codes
2852 * can be stored in the output buffer directly.
2853 *
2854 * - Code 256 defines a new filter, which is later used to
2855 * ransform the data block accordingly to the filter type.
2856 * The data block needs to be fully uncompressed first.
2857 *
2858 * - Code bigger than 257 and smaller than 262 define
2859 * a repetition pattern that should be copied from
2860 * an already uncompressed chunk of data.
2861 */
2418
2862
2419 /* Decode the next literal. */
2420 if(ARCHIVE_OK != decode_number(a, &rar->cstate.ld, p, &num)) {
2421 return ARCHIVE_EOF;
2422 }
2863 if(num < 256) {
2864 /* Directly store the byte. */
2865 int64_t write_idx = rar->cstate.solid_offset +
2866 rar->cstate.write_ptr++;
2423
2867
2424 /* Num holds a decompression literal, or 'command code'.
2425 *
2426 * - Values lower than 256 are just bytes. Those codes can be stored
2427 * in the output buffer directly.
2428 *
2429 * - Code 256 defines a new filter, which is later used to transform
2430 * the data block accordingly to the filter type. The data block
2431 * needs to be fully uncompressed first.
2432 *
2433 * - Code bigger than 257 and smaller than 262 define a repetition
2434 * pattern that should be copied from an already uncompressed chunk
2435 * of data.
2436 */
2868 rar->cstate.window_buf[write_idx & cmask] =
2869 (uint8_t) num;
2870 continue;
2871 } else if(num >= 262) {
2872 uint16_t dist_slot;
2873 int len = decode_code_length(rar, p, num - 262),
2874 dbits,
2875 dist = 1;
2437
2876
2438 if(num < 256) {
2439 /* Directly store the byte. */
2877 if(len == -1) {
2878 archive_set_error(&a->archive,
2879 ARCHIVE_ERRNO_PROGRAMMER,
2880 "Failed to decode the code length");
2440
2881
2441 int64_t write_idx = rar->cstate.solid_offset +
2442 rar->cstate.write_ptr++;
2882 return ARCHIVE_FATAL;
2883 }
2443
2884
2444 rar->cstate.window_buf[write_idx & cmask] = (uint8_t) num;
2445 continue;
2446 } else if(num >= 262) {
2447 uint16_t dist_slot;
2448 int len = decode_code_length(rar, p, num - 262),
2449 dbits,
2450 dist = 1;
2885 if(ARCHIVE_OK != decode_number(a, &rar->cstate.dd, p,
2886 &dist_slot))
2887 {
2888 archive_set_error(&a->archive,
2889 ARCHIVE_ERRNO_PROGRAMMER,
2890 "Failed to decode the distance slot");
2451
2891
2452 if(len == -1) {
2453 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2454 "Failed to decode the code length");
2892 return ARCHIVE_FATAL;
2893 }
2455
2894
2456 return ARCHIVE_FATAL;
2457 }
2895 if(dist_slot < 4) {
2896 dbits = 0;
2897 dist += dist_slot;
2898 } else {
2899 dbits = dist_slot / 2 - 1;
2458
2900
2459 if(ARCHIVE_OK != decode_number(a, &rar->cstate.dd, p, &dist_slot))
2460 {
2461 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2462 "Failed to decode the distance slot");
2901 /* Cast to uint32_t will make sure the shift
2902 * left operation won't produce undefined
2903 * result. Then, the uint32_t type will
2904 * be implicitly casted to int. */
2905 dist += (uint32_t) (2 |
2906 (dist_slot & 1)) << dbits;
2907 }
2463
2908
2464 return ARCHIVE_FATAL;
2465 }
2909 if(dbits > 0) {
2910 if(dbits >= 4) {
2911 uint32_t add = 0;
2912 uint16_t low_dist;
2466
2913
2467 if(dist_slot < 4) {
2468 dbits = 0;
2469 dist += dist_slot;
2470 } else {
2471 dbits = dist_slot / 2 - 1;
2472 dist += (2 | (dist_slot & 1)) << dbits;
2473 }
2914 if(dbits > 4) {
2915 if(ARCHIVE_OK != read_bits_32(
2916 rar, p, &add)) {
2917 /* Return EOF if we
2918 * can't read more
2919 * data. */
2920 return ARCHIVE_EOF;
2921 }
2474
2922
2475 if(dbits > 0) {
2476 if(dbits >= 4) {
2477 uint32_t add = 0;
2478 uint16_t low_dist;
2923 skip_bits(rar, dbits - 4);
2924 add = (add >> (
2925 36 - dbits)) << 4;
2926 dist += add;
2927 }
2479
2928
2480 if(dbits > 4) {
2481 if(ARCHIVE_OK != read_bits_32(rar, p, &add)) {
2482 /* Return EOF if we can't read more data. */
2483 return ARCHIVE_EOF;
2484 }
2929 if(ARCHIVE_OK != decode_number(a,
2930 &rar->cstate.ldd, p, &low_dist))
2931 {
2932 archive_set_error(&a->archive,
2933 ARCHIVE_ERRNO_PROGRAMMER,
2934 "Failed to decode the "
2935 "distance slot");
2485
2936
2486 skip_bits(rar, dbits - 4);
2487 add = (add >> (36 - dbits)) << 4;
2488 dist += add;
2489 }
2937 return ARCHIVE_FATAL;
2938 }
2490
2939
2491 if(ARCHIVE_OK != decode_number(a, &rar->cstate.ldd, p,
2492 &low_dist))
2493 {
2494 archive_set_error(&a->archive,
2495 ARCHIVE_ERRNO_PROGRAMMER,
2496 "Failed to decode the distance slot");
2940 if(dist >= INT_MAX - low_dist - 1) {
2941 /* This only happens in
2942 * invalid archives. */
2943 archive_set_error(&a->archive,
2944 ARCHIVE_ERRNO_FILE_FORMAT,
2945 "Distance pointer "
2946 "overflow");
2947 return ARCHIVE_FATAL;
2948 }
2497
2949
2498 return ARCHIVE_FATAL;
2499 }
2950 dist += low_dist;
2951 } else {
2952 /* dbits is one of [0,1,2,3] */
2953 int add;
2500
2954
2501 dist += low_dist;
2502 } else {
2503 /* dbits is one of [0,1,2,3] */
2504 int add;
2955 if(ARCHIVE_OK != read_consume_bits(rar,
2956 p, dbits, &add)) {
2957 /* Return EOF if we can't read
2958 * more data. */
2959 return ARCHIVE_EOF;
2960 }
2505
2961
2506 if(ARCHIVE_OK != read_consume_bits(rar, p, dbits, &add)) {
2507 /* Return EOF if we can't read more data. */
2508 return ARCHIVE_EOF;
2509 }
2962 dist += add;
2963 }
2964 }
2510
2965
2511 dist += add;
2512 }
2513 }
2966 if(dist > 0x100) {
2967 len++;
2514
2968
2515 if(dist > 0x100) {
2516 len++;
2969 if(dist > 0x2000) {
2970 len++;
2517
2971
2518 if(dist > 0x2000) {
2519 len++;
2972 if(dist > 0x40000) {
2973 len++;
2974 }
2975 }
2976 }
2520
2977
2521 if(dist > 0x40000) {
2522 len++;
2523 }
2524 }
2525 }
2978 dist_cache_push(rar, dist);
2979 rar->cstate.last_len = len;
2526
2980
2527 dist_cache_push(rar, dist);
2528 rar->cstate.last_len = len;
2981 if(ARCHIVE_OK != copy_string(a, len, dist))
2982 return ARCHIVE_FATAL;
2529
2983
2530 if(ARCHIVE_OK != copy_string(a, len, dist))
2531 return ARCHIVE_FATAL;
2984 continue;
2985 } else if(num == 256) {
2986 /* Create a filter. */
2987 ret = parse_filter(a, p);
2988 if(ret != ARCHIVE_OK)
2989 return ret;
2532
2990
2533 continue;
2534 } else if(num == 256) {
2535 /* Create a filter. */
2536 ret = parse_filter(a, p);
2537 if(ret != ARCHIVE_OK)
2538 return ret;
2991 continue;
2992 } else if(num == 257) {
2993 if(rar->cstate.last_len != 0) {
2994 if(ARCHIVE_OK != copy_string(a,
2995 rar->cstate.last_len,
2996 rar->cstate.dist_cache[0]))
2997 {
2998 return ARCHIVE_FATAL;
2999 }
3000 }
2539
3001
2540 continue;
2541 } else if(num == 257) {
2542 if(rar->cstate.last_len != 0) {
2543 if(ARCHIVE_OK != copy_string(a, rar->cstate.last_len,
2544 rar->cstate.dist_cache[0]))
2545 {
2546 return ARCHIVE_FATAL;
2547 }
2548 }
3002 continue;
3003 } else if(num < 262) {
3004 const int idx = num - 258;
3005 const int dist = dist_cache_touch(rar, idx);
2549
3006
2550 continue;
2551 } else if(num < 262) {
2552 const int idx = num - 258;
2553 const int dist = dist_cache_touch(rar, idx);
3007 uint16_t len_slot;
3008 int len;
2554
3009
2555 uint16_t len_slot;
2556 int len;
3010 if(ARCHIVE_OK != decode_number(a, &rar->cstate.rd, p,
3011 &len_slot)) {
3012 return ARCHIVE_FATAL;
3013 }
2557
3014
2558 if(ARCHIVE_OK != decode_number(a, &rar->cstate.rd, p, &len_slot)) {
2559 return ARCHIVE_FATAL;
2560 }
3015 len = decode_code_length(rar, p, len_slot);
3016 rar->cstate.last_len = len;
2561
3017
2562 len = decode_code_length(rar, p, len_slot);
2563 rar->cstate.last_len = len;
3018 if(ARCHIVE_OK != copy_string(a, len, dist))
3019 return ARCHIVE_FATAL;
2564
3020
2565 if(ARCHIVE_OK != copy_string(a, len, dist))
2566 return ARCHIVE_FATAL;
3021 continue;
3022 }
2567
3023
2568 continue;
2569 }
3024 /* The program counter shouldn't reach here. */
3025 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3026 "Unsupported block code: 0x%x", num);
2570
3027
2571 /* The program counter shouldn't reach here. */
2572 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2573 "Unsupported block code: 0x%02x", num);
3028 return ARCHIVE_FATAL;
3029 }
2574
3030
2575 return ARCHIVE_FATAL;
2576 }
2577
2578 return ARCHIVE_OK;
3031 return ARCHIVE_OK;
2579}
2580
2581/* Binary search for the RARv5 signature. */
2582static int scan_for_signature(struct archive_read* a) {
3032}
3033
3034/* Binary search for the RARv5 signature. */
3035static int scan_for_signature(struct archive_read* a) {
2583 const uint8_t* p;
2584 const int chunk_size = 512;
2585 ssize_t i;
3036 const uint8_t* p;
3037 const int chunk_size = 512;
3038 ssize_t i;
2586
3039
2587 /* If we're here, it means we're on an 'unknown territory' data.
2588 * There's no indication what kind of data we're reading here. It could be
2589 * some text comment, any kind of binary data, digital sign, dragons, etc.
2590 *
2591 * We want to find a valid RARv5 magic header inside this unknown data. */
3040 /* If we're here, it means we're on an 'unknown territory' data.
3041 * There's no indication what kind of data we're reading here.
3042 * It could be some text comment, any kind of binary data,
3043 * digital sign, dragons, etc.
3044 *
3045 * We want to find a valid RARv5 magic header inside this unknown
3046 * data. */
2592
3047
2593 /* Is it possible in libarchive to just skip everything until the
2594 * end of the file? If so, it would be a better approach than the
2595 * current implementation of this function. */
3048 /* Is it possible in libarchive to just skip everything until the
3049 * end of the file? If so, it would be a better approach than the
3050 * current implementation of this function. */
2596
3051
2597 while(1) {
2598 if(!read_ahead(a, chunk_size, &p))
2599 return ARCHIVE_EOF;
3052 while(1) {
3053 if(!read_ahead(a, chunk_size, &p))
3054 return ARCHIVE_EOF;
2600
3055
2601 for(i = 0; i < chunk_size - rar5_signature_size; i++) {
2602 if(memcmp(&p[i], rar5_signature, rar5_signature_size) == 0) {
2603 /* Consume the number of bytes we've used to search for the
2604 * signature, as well as the number of bytes used by the
2605 * signature itself. After this we should be standing on a
2606 * valid base block header. */
2607 (void) consume(a, i + rar5_signature_size);
2608 return ARCHIVE_OK;
2609 }
2610 }
3056 for(i = 0; i < chunk_size - rar5_signature_size; i++) {
3057 if(memcmp(&p[i], rar5_signature,
3058 rar5_signature_size) == 0) {
3059 /* Consume the number of bytes we've used to
3060 * search for the signature, as well as the
3061 * number of bytes used by the signature
3062 * itself. After this we should be standing
3063 * on a valid base block header. */
3064 (void) consume(a, i + rar5_signature_size);
3065 return ARCHIVE_OK;
3066 }
3067 }
2611
3068
2612 consume(a, chunk_size);
2613 }
3069 consume(a, chunk_size);
3070 }
2614
3071
2615 return ARCHIVE_FATAL;
3072 return ARCHIVE_FATAL;
2616}
2617
2618/* This function will switch the multivolume archive file to another file,
2619 * i.e. from part03 to part 04. */
2620static int advance_multivolume(struct archive_read* a) {
3073}
3074
3075/* This function will switch the multivolume archive file to another file,
3076 * i.e. from part03 to part 04. */
3077static int advance_multivolume(struct archive_read* a) {
2621 int lret;
2622 struct rar5* rar = get_context(a);
3078 int lret;
3079 struct rar5* rar = get_context(a);
2623
3080
2624 /* A small state machine that will skip unnecessary data, needed to
2625 * switch from one multivolume to another. Such skipping is needed if
2626 * we want to be an stream-oriented (instead of file-oriented)
2627 * unpacker.
2628 *
2629 * The state machine starts with `rar->main.endarc` == 0. It also
2630 * assumes that current stream pointer points to some base block header.
2631 *
2632 * The `endarc` field is being set when the base block parsing function
2633 * encounters the 'end of archive' marker.
2634 */
3081 /* A small state machine that will skip unnecessary data, needed to
3082 * switch from one multivolume to another. Such skipping is needed if
3083 * we want to be an stream-oriented (instead of file-oriented)
3084 * unpacker.
3085 *
3086 * The state machine starts with `rar->main.endarc` == 0. It also
3087 * assumes that current stream pointer points to some base block
3088 * header.
3089 *
3090 * The `endarc` field is being set when the base block parsing
3091 * function encounters the 'end of archive' marker.
3092 */
2635
3093
2636 while(1) {
2637 if(rar->main.endarc == 1) {
2638 rar->main.endarc = 0;
2639 while(ARCHIVE_RETRY == skip_base_block(a));
2640 break;
2641 } else {
2642 /* Skip current base block. In order to properly skip it,
2643 * we really need to simply parse it and discard the results. */
3094 while(1) {
3095 if(rar->main.endarc == 1) {
3096 int looping = 1;
2644
3097
2645 lret = skip_base_block(a);
3098 rar->main.endarc = 0;
2646
3099
2647 /* The `skip_base_block` function tells us if we should continue
2648 * with skipping, or we should stop skipping. We're trying to skip
2649 * everything up to a base FILE block. */
3100 while(looping) {
3101 lret = skip_base_block(a);
3102 switch(lret) {
3103 case ARCHIVE_RETRY:
3104 /* Continue looping. */
3105 break;
3106 case ARCHIVE_OK:
3107 /* Break loop. */
3108 looping = 0;
3109 break;
3110 default:
3111 /* Forward any errors to the
3112 * caller. */
3113 return lret;
3114 }
3115 }
2650
3116
2651 if(lret != ARCHIVE_RETRY) {
2652 /* If there was an error during skipping, or we have just
2653 * skipped a FILE base block... */
3117 break;
3118 } else {
3119 /* Skip current base block. In order to properly skip
3120 * it, we really need to simply parse it and discard
3121 * the results. */
2654
3122
2655 if(rar->main.endarc == 0) {
2656 return lret;
2657 } else {
2658 continue;
2659 }
2660 }
2661 }
2662 }
3123 lret = skip_base_block(a);
3124 if(lret == ARCHIVE_FATAL || lret == ARCHIVE_FAILED)
3125 return lret;
2663
3126
2664 return ARCHIVE_OK;
3127 /* The `skip_base_block` function tells us if we
3128 * should continue with skipping, or we should stop
3129 * skipping. We're trying to skip everything up to
3130 * a base FILE block. */
3131
3132 if(lret != ARCHIVE_RETRY) {
3133 /* If there was an error during skipping, or we
3134 * have just skipped a FILE base block... */
3135
3136 if(rar->main.endarc == 0) {
3137 return lret;
3138 } else {
3139 continue;
3140 }
3141 }
3142 }
3143 }
3144
3145 return ARCHIVE_OK;
2665}
2666
2667/* Merges the partial block from the first multivolume archive file, and
2668 * partial block from the second multivolume archive file. The result is
2669 * a chunk of memory containing the whole block, and the stream pointer
2670 * is advanced to the next block in the second multivolume archive file. */
2671static int merge_block(struct archive_read* a, ssize_t block_size,
3146}
3147
3148/* Merges the partial block from the first multivolume archive file, and
3149 * partial block from the second multivolume archive file. The result is
3150 * a chunk of memory containing the whole block, and the stream pointer
3151 * is advanced to the next block in the second multivolume archive file. */
3152static int merge_block(struct archive_read* a, ssize_t block_size,
2672 const uint8_t** p)
3153 const uint8_t** p)
2673{
3154{
2674 struct rar5* rar = get_context(a);
2675 ssize_t cur_block_size, partial_offset = 0;
2676 const uint8_t* lp;
2677 int ret;
3155 struct rar5* rar = get_context(a);
3156 ssize_t cur_block_size, partial_offset = 0;
3157 const uint8_t* lp;
3158 int ret;
2678
3159
2679 /* Set a flag that we're in the switching mode. */
2680 rar->cstate.switch_multivolume = 1;
3160 if(rar->merge_mode) {
3161 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3162 "Recursive merge is not allowed");
2681
3163
2682 /* Reallocate the memory which will hold the whole block. */
2683 if(rar->vol.push_buf)
2684 free((void*) rar->vol.push_buf);
3164 return ARCHIVE_FATAL;
3165 }
2685
3166
2686 /* Increasing the allocation block by 8 is due to bit reading functions,
2687 * which are using additional 2 or 4 bytes. Allocating the block size
2688 * by exact value would make bit reader perform reads from invalid memory
2689 * block when reading the last byte from the buffer. */
2690 rar->vol.push_buf = malloc(block_size + 8);
2691 if(!rar->vol.push_buf) {
2692 archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for a "
2693 "merge block buffer.");
2694 return ARCHIVE_FATAL;
2695 }
3167 /* Set a flag that we're in the switching mode. */
3168 rar->cstate.switch_multivolume = 1;
2696
3169
2697 /* Valgrind complains if the extension block for bit reader is not
2698 * initialized, so initialize it. */
2699 memset(&rar->vol.push_buf[block_size], 0, 8);
3170 /* Reallocate the memory which will hold the whole block. */
3171 if(rar->vol.push_buf)
3172 free((void*) rar->vol.push_buf);
2700
3173
2701 /* A single block can span across multiple multivolume archive files,
2702 * so we use a loop here. This loop will consume enough multivolume
2703 * archive files until the whole block is read. */
3174 /* Increasing the allocation block by 8 is due to bit reading functions,
3175 * which are using additional 2 or 4 bytes. Allocating the block size
3176 * by exact value would make bit reader perform reads from invalid
3177 * memory block when reading the last byte from the buffer. */
3178 rar->vol.push_buf = malloc(block_size + 8);
3179 if(!rar->vol.push_buf) {
3180 archive_set_error(&a->archive, ENOMEM,
3181 "Can't allocate memory for a merge block buffer.");
3182 return ARCHIVE_FATAL;
3183 }
2704
3184
2705 while(1) {
2706 /* Get the size of current block chunk in this multivolume archive
2707 * file and read it. */
2708 cur_block_size =
2709 rar5_min(rar->file.bytes_remaining, block_size - partial_offset);
3185 /* Valgrind complains if the extension block for bit reader is not
3186 * initialized, so initialize it. */
3187 memset(&rar->vol.push_buf[block_size], 0, 8);
2710
3188
2711 if(cur_block_size == 0) {
2712 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2713 "Encountered block size == 0 during block merge");
2714 return ARCHIVE_FATAL;
2715 }
3189 /* A single block can span across multiple multivolume archive files,
3190 * so we use a loop here. This loop will consume enough multivolume
3191 * archive files until the whole block is read. */
2716
3192
2717 if(!read_ahead(a, cur_block_size, &lp))
2718 return ARCHIVE_EOF;
3193 while(1) {
3194 /* Get the size of current block chunk in this multivolume
3195 * archive file and read it. */
3196 cur_block_size = rar5_min(rar->file.bytes_remaining,
3197 block_size - partial_offset);
2719
3198
2720 /* Sanity check; there should never be a situation where this function
2721 * reads more data than the block's size. */
2722 if(partial_offset + cur_block_size > block_size) {
2723 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2724 "Consumed too much data when merging blocks.");
2725 return ARCHIVE_FATAL;
2726 }
3199 if(cur_block_size == 0) {
3200 archive_set_error(&a->archive,
3201 ARCHIVE_ERRNO_FILE_FORMAT,
3202 "Encountered block size == 0 during block merge");
3203 return ARCHIVE_FATAL;
3204 }
2727
3205
2728 /* Merge previous block chunk with current block chunk, or create
2729 * first block chunk if this is our first iteration. */
2730 memcpy(&rar->vol.push_buf[partial_offset], lp, cur_block_size);
3206 if(!read_ahead(a, cur_block_size, &lp))
3207 return ARCHIVE_EOF;
2731
3208
2732 /* Advance the stream read pointer by this block chunk size. */
2733 if(ARCHIVE_OK != consume(a, cur_block_size))
2734 return ARCHIVE_EOF;
3209 /* Sanity check; there should never be a situation where this
3210 * function reads more data than the block's size. */
3211 if(partial_offset + cur_block_size > block_size) {
3212 archive_set_error(&a->archive,
3213 ARCHIVE_ERRNO_PROGRAMMER,
3214 "Consumed too much data when merging blocks.");
3215 return ARCHIVE_FATAL;
3216 }
2735
3217
2736 /* Update the pointers. `partial_offset` contains information about
2737 * the sum of merged block chunks. */
2738 partial_offset += cur_block_size;
2739 rar->file.bytes_remaining -= cur_block_size;
3218 /* Merge previous block chunk with current block chunk,
3219 * or create first block chunk if this is our first
3220 * iteration. */
3221 memcpy(&rar->vol.push_buf[partial_offset], lp, cur_block_size);
2740
3222
2741 /* If `partial_offset` is the same as `block_size`, this means we've
2742 * merged all block chunks and we have a valid full block. */
2743 if(partial_offset == block_size) {
2744 break;
2745 }
3223 /* Advance the stream read pointer by this block chunk size. */
3224 if(ARCHIVE_OK != consume(a, cur_block_size))
3225 return ARCHIVE_EOF;
2746
3226
2747 /* If we don't have any bytes to read, this means we should switch
2748 * to another multivolume archive file. */
2749 if(rar->file.bytes_remaining == 0) {
2750 ret = advance_multivolume(a);
2751 if(ret != ARCHIVE_OK)
2752 return ret;
2753 }
2754 }
3227 /* Update the pointers. `partial_offset` contains information
3228 * about the sum of merged block chunks. */
3229 partial_offset += cur_block_size;
3230 rar->file.bytes_remaining -= cur_block_size;
2755
3231
2756 *p = rar->vol.push_buf;
3232 /* If `partial_offset` is the same as `block_size`, this means
3233 * we've merged all block chunks and we have a valid full
3234 * block. */
3235 if(partial_offset == block_size) {
3236 break;
3237 }
2757
3238
2758 /* If we're here, we can resume unpacking by processing the block pointed
2759 * to by the `*p` memory pointer. */
3239 /* If we don't have any bytes to read, this means we should
3240 * switch to another multivolume archive file. */
3241 if(rar->file.bytes_remaining == 0) {
3242 rar->merge_mode++;
3243 ret = advance_multivolume(a);
3244 rar->merge_mode--;
3245 if(ret != ARCHIVE_OK) {
3246 return ret;
3247 }
3248 }
3249 }
2760
3250
2761 return ARCHIVE_OK;
3251 *p = rar->vol.push_buf;
3252
3253 /* If we're here, we can resume unpacking by processing the block
3254 * pointed to by the `*p` memory pointer. */
3255
3256 return ARCHIVE_OK;
2762}
2763
2764static int process_block(struct archive_read* a) {
3257}
3258
3259static int process_block(struct archive_read* a) {
2765 const uint8_t* p;
2766 struct rar5* rar = get_context(a);
2767 int ret;
3260 const uint8_t* p;
3261 struct rar5* rar = get_context(a);
3262 int ret;
2768
3263
2769 /* If we don't have any data to be processed, this most probably means
2770 * we need to switch to the next volume. */
2771 if(rar->main.volume && rar->file.bytes_remaining == 0) {
2772 ret = advance_multivolume(a);
2773 if(ret != ARCHIVE_OK)
2774 return ret;
2775 }
3264 /* If we don't have any data to be processed, this most probably means
3265 * we need to switch to the next volume. */
3266 if(rar->main.volume && rar->file.bytes_remaining == 0) {
3267 ret = advance_multivolume(a);
3268 if(ret != ARCHIVE_OK)
3269 return ret;
3270 }
2776
3271
2777 if(rar->cstate.block_parsing_finished) {
2778 ssize_t block_size;
3272 if(rar->cstate.block_parsing_finished) {
3273 ssize_t block_size;
2779
3274
2780 rar->cstate.block_parsing_finished = 0;
3275 /* The header size won't be bigger than 6 bytes. */
3276 if(!read_ahead(a, 6, &p)) {
3277 /* Failed to prefetch data block header. */
3278 return ARCHIVE_EOF;
3279 }
2781
3280
2782 /* The header size won't be bigger than 6 bytes. */
2783 if(!read_ahead(a, 6, &p)) {
2784 /* Failed to prefetch data block header. */
2785 return ARCHIVE_EOF;
2786 }
3281 /*
3282 * Read block_size by parsing block header. Validate the header
3283 * by calculating CRC byte stored inside the header. Size of
3284 * the header is not constant (block size can be stored either
3285 * in 1 or 2 bytes), that's why block size is left out from the
3286 * `compressed_block_header` structure and returned by
3287 * `parse_block_header` as the second argument. */
2787
3288
2788 /*
2789 * Read block_size by parsing block header. Validate the header by
2790 * calculating CRC byte stored inside the header. Size of the header is
2791 * not constant (block size can be stored either in 1 or 2 bytes),
2792 * that's why block size is left out from the `compressed_block_header`
2793 * structure and returned by `parse_block_header` as the second
2794 * argument. */
3289 ret = parse_block_header(a, p, &block_size,
3290 &rar->last_block_hdr);
3291 if(ret != ARCHIVE_OK) {
3292 return ret;
3293 }
2795
3294
2796 ret = parse_block_header(a, p, &block_size, &rar->last_block_hdr);
2797 if(ret != ARCHIVE_OK)
2798 return ret;
3295 /* Skip block header. Next data is huffman tables,
3296 * if present. */
3297 ssize_t to_skip = sizeof(struct compressed_block_header) +
3298 bf_byte_count(&rar->last_block_hdr) + 1;
2799
3299
2800 /* Skip block header. Next data is huffman tables, if present. */
2801 ssize_t to_skip = sizeof(struct compressed_block_header) +
2802 bf_byte_count(&rar->last_block_hdr) + 1;
3300 if(ARCHIVE_OK != consume(a, to_skip))
3301 return ARCHIVE_EOF;
2803
3302
2804 if(ARCHIVE_OK != consume(a, to_skip))
2805 return ARCHIVE_EOF;
3303 rar->file.bytes_remaining -= to_skip;
2806
3304
2807 rar->file.bytes_remaining -= to_skip;
3305 /* The block size gives information about the whole block size,
3306 * but the block could be stored in split form when using
3307 * multi-volume archives. In this case, the block size will be
3308 * bigger than the actual data stored in this file. Remaining
3309 * part of the data will be in another file. */
2808
3310
2809 /* The block size gives information about the whole block size, but
2810 * the block could be stored in split form when using multi-volume
2811 * archives. In this case, the block size will be bigger than the
2812 * actual data stored in this file. Remaining part of the data will
2813 * be in another file. */
3311 ssize_t cur_block_size =
3312 rar5_min(rar->file.bytes_remaining, block_size);
2814
3313
2815 ssize_t cur_block_size =
2816 rar5_min(rar->file.bytes_remaining, block_size);
3314 if(block_size > rar->file.bytes_remaining) {
3315 /* If current blocks' size is bigger than our data
3316 * size, this means we have a multivolume archive.
3317 * In this case, skip all base headers until the end
3318 * of the file, proceed to next "partXXX.rar" volume,
3319 * find its signature, skip all headers up to the first
3320 * FILE base header, and continue from there.
3321 *
3322 * Note that `merge_block` will update the `rar`
3323 * context structure quite extensively. */
2817
3324
2818 if(block_size > rar->file.bytes_remaining) {
2819 /* If current blocks' size is bigger than our data size, this
2820 * means we have a multivolume archive. In this case, skip
2821 * all base headers until the end of the file, proceed to next
2822 * "partXXX.rar" volume, find its signature, skip all headers up
2823 * to the first FILE base header, and continue from there.
2824 *
2825 * Note that `merge_block` will update the `rar` context structure
2826 * quite extensively. */
3325 ret = merge_block(a, block_size, &p);
3326 if(ret != ARCHIVE_OK) {
3327 return ret;
3328 }
2827
3329
2828 ret = merge_block(a, block_size, &p);
2829 if(ret != ARCHIVE_OK) {
2830 return ret;
2831 }
3330 cur_block_size = block_size;
2832
3331
2833 cur_block_size = block_size;
3332 /* Current stream pointer should be now directly
3333 * *after* the block that spanned through multiple
3334 * archive files. `p` pointer should have the data of
3335 * the *whole* block (merged from partial blocks
3336 * stored in multiple archives files). */
3337 } else {
3338 rar->cstate.switch_multivolume = 0;
2834
3339
2835 /* Current stream pointer should be now directly *after* the
2836 * block that spanned through multiple archive files. `p` pointer
2837 * should have the data of the *whole* block (merged from
2838 * partial blocks stored in multiple archives files). */
2839 } else {
2840 rar->cstate.switch_multivolume = 0;
3340 /* Read the whole block size into memory. This can take
3341 * up to 8 megabytes of memory in theoretical cases.
3342 * Might be worth to optimize this and use a standard
3343 * chunk of 4kb's. */
3344 if(!read_ahead(a, 4 + cur_block_size, &p)) {
3345 /* Failed to prefetch block data. */
3346 return ARCHIVE_EOF;
3347 }
3348 }
2841
3349
2842 /* Read the whole block size into memory. This can take up to
2843 * 8 megabytes of memory in theoretical cases. Might be worth to
2844 * optimize this and use a standard chunk of 4kb's. */
3350 rar->cstate.block_buf = p;
3351 rar->cstate.cur_block_size = cur_block_size;
3352 rar->cstate.block_parsing_finished = 0;
2845
3353
2846 if(!read_ahead(a, 4 + cur_block_size, &p)) {
2847 /* Failed to prefetch block data. */
2848 return ARCHIVE_EOF;
2849 }
2850 }
3354 rar->bits.in_addr = 0;
3355 rar->bits.bit_addr = 0;
2851
3356
2852 rar->cstate.block_buf = p;
2853 rar->cstate.cur_block_size = cur_block_size;
3357 if(bf_is_table_present(&rar->last_block_hdr)) {
3358 /* Load Huffman tables. */
3359 ret = parse_tables(a, rar, p);
3360 if(ret != ARCHIVE_OK) {
3361 /* Error during decompression of Huffman
3362 * tables. */
3363 return ret;
3364 }
3365 }
3366 } else {
3367 /* Block parsing not finished, reuse previous memory buffer. */
3368 p = rar->cstate.block_buf;
3369 }
2854
3370
2855 rar->bits.in_addr = 0;
2856 rar->bits.bit_addr = 0;
3371 /* Uncompress the block, or a part of it, depending on how many bytes
3372 * will be generated by uncompressing the block.
3373 *
3374 * In case too many bytes will be generated, calling this function
3375 * again will resume the uncompression operation. */
3376 ret = do_uncompress_block(a, p);
3377 if(ret != ARCHIVE_OK) {
3378 return ret;
3379 }
2857
3380
2858 if(bf_is_table_present(&rar->last_block_hdr)) {
2859 /* Load Huffman tables. */
2860 ret = parse_tables(a, rar, p);
2861 if(ret != ARCHIVE_OK) {
2862 /* Error during decompression of Huffman tables. */
2863 return ret;
2864 }
2865 }
2866 } else {
2867 p = rar->cstate.block_buf;
2868 }
3381 if(rar->cstate.block_parsing_finished &&
3382 rar->cstate.switch_multivolume == 0 &&
3383 rar->cstate.cur_block_size > 0)
3384 {
3385 /* If we're processing a normal block, consume the whole
3386 * block. We can do this because we've already read the whole
3387 * block to memory. */
3388 if(ARCHIVE_OK != consume(a, rar->cstate.cur_block_size))
3389 return ARCHIVE_FATAL;
2869
3390
2870 /* Uncompress the block, or a part of it, depending on how many bytes
2871 * will be generated by uncompressing the block.
2872 *
2873 * In case too many bytes will be generated, calling this function again
2874 * will resume the uncompression operation. */
2875 ret = do_uncompress_block(a, p);
2876 if(ret != ARCHIVE_OK) {
2877 return ret;
2878 }
3391 rar->file.bytes_remaining -= rar->cstate.cur_block_size;
3392 } else if(rar->cstate.switch_multivolume) {
3393 /* Don't consume the block if we're doing multivolume
3394 * processing. The volume switching function will consume
3395 * the proper count of bytes instead. */
3396 rar->cstate.switch_multivolume = 0;
3397 }
2879
3398
2880 if(rar->cstate.block_parsing_finished &&
2881 rar->cstate.switch_multivolume == 0 &&
2882 rar->cstate.cur_block_size > 0)
2883 {
2884 /* If we're processing a normal block, consume the whole block. We
2885 * can do this because we've already read the whole block to memory.
2886 */
2887 if(ARCHIVE_OK != consume(a, rar->cstate.cur_block_size))
2888 return ARCHIVE_FATAL;
2889
2890 rar->file.bytes_remaining -= rar->cstate.cur_block_size;
2891 } else if(rar->cstate.switch_multivolume) {
2892 /* Don't consume the block if we're doing multivolume processing.
2893 * The volume switching function will consume the proper count of
2894 * bytes instead. */
2895
2896 rar->cstate.switch_multivolume = 0;
2897 }
2898
2899 return ARCHIVE_OK;
3399 return ARCHIVE_OK;
2900}
2901
2902/* Pops the `buf`, `size` and `offset` from the "data ready" stack.
2903 *
2904 * Returns ARCHIVE_OK when those arguments can be used, ARCHIVE_RETRY
2905 * when there is no data on the stack. */
2906static int use_data(struct rar5* rar, const void** buf, size_t* size,
3400}
3401
3402/* Pops the `buf`, `size` and `offset` from the "data ready" stack.
3403 *
3404 * Returns ARCHIVE_OK when those arguments can be used, ARCHIVE_RETRY
3405 * when there is no data on the stack. */
3406static int use_data(struct rar5* rar, const void** buf, size_t* size,
2907 int64_t* offset)
3407 int64_t* offset)
2908{
3408{
2909 int i;
3409 int i;
2910
3410
2911 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
2912 struct data_ready *d = &rar->cstate.dready[i];
3411 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
3412 struct data_ready *d = &rar->cstate.dready[i];
2913
3413
2914 if(d->used) {
2915 if(buf) *buf = d->buf;
2916 if(size) *size = d->size;
2917 if(offset) *offset = d->offset;
3414 if(d->used) {
3415 if(buf) *buf = d->buf;
3416 if(size) *size = d->size;
3417 if(offset) *offset = d->offset;
2918
3418
2919 d->used = 0;
2920 return ARCHIVE_OK;
2921 }
2922 }
3419 d->used = 0;
3420 return ARCHIVE_OK;
3421 }
3422 }
2923
3423
2924 return ARCHIVE_RETRY;
3424 return ARCHIVE_RETRY;
2925}
2926
2927/* Pushes the `buf`, `size` and `offset` arguments to the rar->cstate.dready
2928 * FIFO stack. Those values will be popped from this stack by the `use_data`
2929 * function. */
2930static int push_data_ready(struct archive_read* a, struct rar5* rar,
3425}
3426
3427/* Pushes the `buf`, `size` and `offset` arguments to the rar->cstate.dready
3428 * FIFO stack. Those values will be popped from this stack by the `use_data`
3429 * function. */
3430static int push_data_ready(struct archive_read* a, struct rar5* rar,
2931 const uint8_t* buf, size_t size, int64_t offset)
3431 const uint8_t* buf, size_t size, int64_t offset)
2932{
3432{
2933 int i;
3433 int i;
2934
3434
2935 /* Don't push if we're in skip mode. This is needed because solid
2936 * streams need full processing even if we're skipping data. After fully
2937 * processing the stream, we need to discard the generated bytes, because
2938 * we're interested only in the side effect: building up the internal
2939 * window circular buffer. This window buffer will be used later during
2940 * unpacking of requested data. */
2941 if(rar->skip_mode)
2942 return ARCHIVE_OK;
3435 /* Don't push if we're in skip mode. This is needed because solid
3436 * streams need full processing even if we're skipping data. After
3437 * fully processing the stream, we need to discard the generated bytes,
3438 * because we're interested only in the side effect: building up the
3439 * internal window circular buffer. This window buffer will be used
3440 * later during unpacking of requested data. */
3441 if(rar->skip_mode)
3442 return ARCHIVE_OK;
2943
3443
2944 /* Sanity check. */
2945 if(offset != rar->file.last_offset + rar->file.last_size) {
2946 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "Sanity "
2947 "check error: output stream is not continuous");
2948 return ARCHIVE_FATAL;
2949 }
3444 /* Sanity check. */
3445 if(offset != rar->file.last_offset + rar->file.last_size) {
3446 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3447 "Sanity check error: output stream is not continuous");
3448 return ARCHIVE_FATAL;
3449 }
2950
3450
2951 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
2952 struct data_ready* d = &rar->cstate.dready[i];
2953 if(!d->used) {
2954 d->used = 1;
2955 d->buf = buf;
2956 d->size = size;
2957 d->offset = offset;
3451 for(i = 0; i < rar5_countof(rar->cstate.dready); i++) {
3452 struct data_ready* d = &rar->cstate.dready[i];
3453 if(!d->used) {
3454 d->used = 1;
3455 d->buf = buf;
3456 d->size = size;
3457 d->offset = offset;
2958
3458
2959 /* These fields are used only in sanity checking. */
2960 rar->file.last_offset = offset;
2961 rar->file.last_size = size;
3459 /* These fields are used only in sanity checking. */
3460 rar->file.last_offset = offset;
3461 rar->file.last_size = size;
2962
3462
2963 /* Calculate the checksum of this new block before submitting
2964 * data to libarchive's engine. */
2965 update_crc(rar, d->buf, d->size);
3463 /* Calculate the checksum of this new block before
3464 * submitting data to libarchive's engine. */
3465 update_crc(rar, d->buf, d->size);
2966
3466
2967 return ARCHIVE_OK;
2968 }
2969 }
3467 return ARCHIVE_OK;
3468 }
3469 }
2970
3470
2971 /* Program counter will reach this code if the `rar->cstate.data_ready`
2972 * stack will be filled up so that no new entries will be allowed. The
2973 * code shouldn't allow such situation to occur. So we treat this case
2974 * as an internal error. */
3471 /* Program counter will reach this code if the `rar->cstate.data_ready`
3472 * stack will be filled up so that no new entries will be allowed. The
3473 * code shouldn't allow such situation to occur. So we treat this case
3474 * as an internal error. */
2975
3475
2976 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "Error: "
2977 "premature end of data_ready stack");
2978 return ARCHIVE_FATAL;
3476 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3477 "Error: premature end of data_ready stack");
3478 return ARCHIVE_FATAL;
2979}
2980
2981/* This function uncompresses the data that is stored in the <FILE> base
2982 * block.
2983 *
2984 * The FILE base block looks like this:
2985 *
2986 * <header><huffman tables><block_1><block_2>...<block_n>

--- 29 unchanged lines hidden (view full) ---

3016 *
3017 * The RAR compressor creates those literals and the actual efficiency of
3018 * compression depends on what those literals are. The literals can also
3019 * be seen as a kind of a non-turing-complete virtual machine that simply
3020 * tells the decompressor what it should do.
3021 * */
3022
3023static int do_uncompress_file(struct archive_read* a) {
3479}
3480
3481/* This function uncompresses the data that is stored in the <FILE> base
3482 * block.
3483 *
3484 * The FILE base block looks like this:
3485 *
3486 * <header><huffman tables><block_1><block_2>...<block_n>

--- 29 unchanged lines hidden (view full) ---

3516 *
3517 * The RAR compressor creates those literals and the actual efficiency of
3518 * compression depends on what those literals are. The literals can also
3519 * be seen as a kind of a non-turing-complete virtual machine that simply
3520 * tells the decompressor what it should do.
3521 * */
3522
3523static int do_uncompress_file(struct archive_read* a) {
3024 struct rar5* rar = get_context(a);
3025 int ret;
3026 int64_t max_end_pos;
3524 struct rar5* rar = get_context(a);
3525 int ret;
3526 int64_t max_end_pos;
3027
3527
3028 if(!rar->cstate.initialized) {
3029 /* Don't perform full context reinitialization if we're processing
3030 * a solid archive. */
3031 if(!rar->main.solid || !rar->cstate.window_buf) {
3032 init_unpack(rar);
3033 }
3528 if(!rar->cstate.initialized) {
3529 /* Don't perform full context reinitialization if we're
3530 * processing a solid archive. */
3531 if(!rar->main.solid || !rar->cstate.window_buf) {
3532 init_unpack(rar);
3533 }
3034
3534
3035 rar->cstate.initialized = 1;
3036 }
3535 rar->cstate.initialized = 1;
3536 }
3037
3537
3038 if(rar->cstate.all_filters_applied == 1) {
3039 /* We use while(1) here, but standard case allows for just 1 iteration.
3040 * The loop will iterate if process_block() didn't generate any data at
3041 * all. This can happen if the block contains only filter definitions
3042 * (this is common in big files). */
3538 if(rar->cstate.all_filters_applied == 1) {
3539 /* We use while(1) here, but standard case allows for just 1
3540 * iteration. The loop will iterate if process_block() didn't
3541 * generate any data at all. This can happen if the block
3542 * contains only filter definitions (this is common in big
3543 * files). */
3544 while(1) {
3545 ret = process_block(a);
3546 if(ret == ARCHIVE_EOF || ret == ARCHIVE_FATAL)
3547 return ret;
3043
3548
3044 while(1) {
3045 ret = process_block(a);
3046 if(ret == ARCHIVE_EOF || ret == ARCHIVE_FATAL)
3047 return ret;
3549 if(rar->cstate.last_write_ptr ==
3550 rar->cstate.write_ptr) {
3551 /* The block didn't generate any new data,
3552 * so just process a new block. */
3553 continue;
3554 }
3048
3555
3049 if(rar->cstate.last_write_ptr == rar->cstate.write_ptr) {
3050 /* The block didn't generate any new data, so just process
3051 * a new block. */
3052 continue;
3053 }
3556 /* The block has generated some new data, so break
3557 * the loop. */
3558 break;
3559 }
3560 }
3054
3561
3055 /* The block has generated some new data, so break the loop. */
3056 break;
3057 }
3058 }
3562 /* Try to run filters. If filters won't be applied, it means that
3563 * insufficient data was generated. */
3564 ret = apply_filters(a);
3565 if(ret == ARCHIVE_RETRY) {
3566 return ARCHIVE_OK;
3567 } else if(ret == ARCHIVE_FATAL) {
3568 return ARCHIVE_FATAL;
3569 }
3059
3570
3060 /* Try to run filters. If filters won't be applied, it means that
3061 * insufficient data was generated. */
3062 ret = apply_filters(a);
3063 if(ret == ARCHIVE_RETRY) {
3064 return ARCHIVE_OK;
3065 } else if(ret == ARCHIVE_FATAL) {
3066 return ARCHIVE_FATAL;
3067 }
3571 /* If apply_filters() will return ARCHIVE_OK, we can continue here. */
3068
3572
3069 /* If apply_filters() will return ARCHIVE_OK, we can continue here. */
3573 if(cdeque_size(&rar->cstate.filters) > 0) {
3574 /* Check if we can write something before hitting first
3575 * filter. */
3576 struct filter_info* flt;
3070
3577
3071 if(cdeque_size(&rar->cstate.filters) > 0) {
3072 /* Check if we can write something before hitting first filter. */
3073 struct filter_info* flt;
3578 /* Get the block_start offset from the first filter. */
3579 if(CDE_OK != cdeque_front(&rar->cstate.filters,
3580 cdeque_filter_p(&flt)))
3581 {
3582 archive_set_error(&a->archive,
3583 ARCHIVE_ERRNO_PROGRAMMER,
3584 "Can't read first filter");
3585 return ARCHIVE_FATAL;
3586 }
3074
3587
3075 /* Get the block_start offset from the first filter. */
3076 if(CDE_OK != cdeque_front(&rar->cstate.filters, cdeque_filter_p(&flt)))
3077 {
3078 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3079 "Can't read first filter");
3080 return ARCHIVE_FATAL;
3081 }
3588 max_end_pos = rar5_min(flt->block_start,
3589 rar->cstate.write_ptr);
3590 } else {
3591 /* There are no filters defined, or all filters were applied.
3592 * This means we can just store the data without any
3593 * postprocessing. */
3594 max_end_pos = rar->cstate.write_ptr;
3595 }
3082
3596
3083 max_end_pos = rar5_min(flt->block_start, rar->cstate.write_ptr);
3084 } else {
3085 /* There are no filters defined, or all filters were applied. This
3086 * means we can just store the data without any postprocessing. */
3087 max_end_pos = rar->cstate.write_ptr;
3088 }
3597 if(max_end_pos == rar->cstate.last_write_ptr) {
3598 /* We can't write anything yet. The block uncompression
3599 * function did not generate enough data, and no filter can be
3600 * applied. At the same time we don't have any data that can be
3601 * stored without filter postprocessing. This means we need to
3602 * wait for more data to be generated, so we can apply the
3603 * filters.
3604 *
3605 * Signal the caller that we need more data to be able to do
3606 * anything.
3607 */
3608 return ARCHIVE_RETRY;
3609 } else {
3610 /* We can write the data before hitting the first filter.
3611 * So let's do it. The push_window_data() function will
3612 * effectively return the selected data block to the user
3613 * application. */
3614 push_window_data(a, rar, rar->cstate.last_write_ptr,
3615 max_end_pos);
3616 rar->cstate.last_write_ptr = max_end_pos;
3617 }
3089
3618
3090 if(max_end_pos == rar->cstate.last_write_ptr) {
3091 /* We can't write anything yet. The block uncompression function did
3092 * not generate enough data, and no filter can be applied. At the same
3093 * time we don't have any data that can be stored without filter
3094 * postprocessing. This means we need to wait for more data to be
3095 * generated, so we can apply the filters.
3096 *
3097 * Signal the caller that we need more data to be able to do anything.
3098 */
3099 return ARCHIVE_RETRY;
3100 } else {
3101 /* We can write the data before hitting the first filter. So let's
3102 * do it. The push_window_data() function will effectively return
3103 * the selected data block to the user application. */
3104 push_window_data(a, rar, rar->cstate.last_write_ptr, max_end_pos);
3105 rar->cstate.last_write_ptr = max_end_pos;
3106 }
3107
3108 return ARCHIVE_OK;
3619 return ARCHIVE_OK;
3109}
3110
3111static int uncompress_file(struct archive_read* a) {
3620}
3621
3622static int uncompress_file(struct archive_read* a) {
3112 int ret;
3623 int ret;
3113
3624
3114 while(1) {
3115 /* Sometimes the uncompression function will return a 'retry' signal.
3116 * If this will happen, we have to retry the function. */
3117 ret = do_uncompress_file(a);
3118 if(ret != ARCHIVE_RETRY)
3119 return ret;
3120 }
3625 while(1) {
3626 /* Sometimes the uncompression function will return a
3627 * 'retry' signal. If this will happen, we have to retry
3628 * the function. */
3629 ret = do_uncompress_file(a);
3630 if(ret != ARCHIVE_RETRY)
3631 return ret;
3632 }
3121}
3122
3123
3124static int do_unstore_file(struct archive_read* a,
3633}
3634
3635
3636static int do_unstore_file(struct archive_read* a,
3125 struct rar5* rar,
3126 const void** buf,
3127 size_t* size,
3128 int64_t* offset)
3637 struct rar5* rar, const void** buf, size_t* size, int64_t* offset)
3129{
3638{
3130 const uint8_t* p;
3639 const uint8_t* p;
3131
3640
3132 if(rar->file.bytes_remaining == 0 && rar->main.volume > 0 &&
3133 rar->generic.split_after > 0)
3134 {
3135 int ret;
3641 if(rar->file.bytes_remaining == 0 && rar->main.volume > 0 &&
3642 rar->generic.split_after > 0)
3643 {
3644 int ret;
3136
3645
3137 rar->cstate.switch_multivolume = 1;
3138 ret = advance_multivolume(a);
3139 rar->cstate.switch_multivolume = 0;
3646 rar->cstate.switch_multivolume = 1;
3647 ret = advance_multivolume(a);
3648 rar->cstate.switch_multivolume = 0;
3140
3649
3141 if(ret != ARCHIVE_OK) {
3142 /* Failed to advance to next multivolume archive file. */
3143 return ret;
3144 }
3145 }
3650 if(ret != ARCHIVE_OK) {
3651 /* Failed to advance to next multivolume archive
3652 * file. */
3653 return ret;
3654 }
3655 }
3146
3656
3147 size_t to_read = rar5_min(rar->file.bytes_remaining, 64 * 1024);
3148 if(to_read == 0) {
3149 return ARCHIVE_EOF;
3150 }
3657 size_t to_read = rar5_min(rar->file.bytes_remaining, 64 * 1024);
3658 if(to_read == 0) {
3659 return ARCHIVE_EOF;
3660 }
3151
3661
3152 if(!read_ahead(a, to_read, &p)) {
3153 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "I/O error "
3154 "when unstoring file");
3155 return ARCHIVE_FATAL;
3156 }
3662 if(!read_ahead(a, to_read, &p)) {
3663 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3664 "I/O error when unstoring file");
3665 return ARCHIVE_FATAL;
3666 }
3157
3667
3158 if(ARCHIVE_OK != consume(a, to_read)) {
3159 return ARCHIVE_EOF;
3160 }
3668 if(ARCHIVE_OK != consume(a, to_read)) {
3669 return ARCHIVE_EOF;
3670 }
3161
3671
3162 if(buf) *buf = p;
3163 if(size) *size = to_read;
3164 if(offset) *offset = rar->cstate.last_unstore_ptr;
3672 if(buf) *buf = p;
3673 if(size) *size = to_read;
3674 if(offset) *offset = rar->cstate.last_unstore_ptr;
3165
3675
3166 rar->file.bytes_remaining -= to_read;
3167 rar->cstate.last_unstore_ptr += to_read;
3676 rar->file.bytes_remaining -= to_read;
3677 rar->cstate.last_unstore_ptr += to_read;
3168
3678
3169 update_crc(rar, p, to_read);
3170 return ARCHIVE_OK;
3679 update_crc(rar, p, to_read);
3680 return ARCHIVE_OK;
3171}
3172
3173static int do_unpack(struct archive_read* a, struct rar5* rar,
3681}
3682
3683static int do_unpack(struct archive_read* a, struct rar5* rar,
3174 const void** buf, size_t* size, int64_t* offset)
3684 const void** buf, size_t* size, int64_t* offset)
3175{
3685{
3176 enum COMPRESSION_METHOD {
3177 STORE = 0, FASTEST = 1, FAST = 2, NORMAL = 3, GOOD = 4, BEST = 5
3178 };
3686 enum COMPRESSION_METHOD {
3687 STORE = 0, FASTEST = 1, FAST = 2, NORMAL = 3, GOOD = 4,
3688 BEST = 5
3689 };
3179
3690
3180 if(rar->file.service > 0) {
3181 return do_unstore_file(a, rar, buf, size, offset);
3182 } else {
3183 switch(rar->cstate.method) {
3184 case STORE:
3185 return do_unstore_file(a, rar, buf, size, offset);
3186 case FASTEST:
3187 /* fallthrough */
3188 case FAST:
3189 /* fallthrough */
3190 case NORMAL:
3191 /* fallthrough */
3192 case GOOD:
3193 /* fallthrough */
3194 case BEST:
3195 return uncompress_file(a);
3196 default:
3197 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3198 "Compression method not supported: 0x%08x",
3199 rar->cstate.method);
3691 if(rar->file.service > 0) {
3692 return do_unstore_file(a, rar, buf, size, offset);
3693 } else {
3694 switch(rar->cstate.method) {
3695 case STORE:
3696 return do_unstore_file(a, rar, buf, size,
3697 offset);
3698 case FASTEST:
3699 /* fallthrough */
3700 case FAST:
3701 /* fallthrough */
3702 case NORMAL:
3703 /* fallthrough */
3704 case GOOD:
3705 /* fallthrough */
3706 case BEST:
3707 return uncompress_file(a);
3708 default:
3709 archive_set_error(&a->archive,
3710 ARCHIVE_ERRNO_FILE_FORMAT,
3711 "Compression method not supported: 0x%x",
3712 rar->cstate.method);
3200
3713
3201 return ARCHIVE_FATAL;
3202 }
3203 }
3714 return ARCHIVE_FATAL;
3715 }
3716 }
3204
3205#if !defined WIN32
3717
3718#if !defined WIN32
3206 /* Not reached. */
3207 return ARCHIVE_OK;
3719 /* Not reached. */
3720 return ARCHIVE_OK;
3208#endif
3209}
3210
3211static int verify_checksums(struct archive_read* a) {
3721#endif
3722}
3723
3724static int verify_checksums(struct archive_read* a) {
3212 int verify_crc;
3213 struct rar5* rar = get_context(a);
3725 int verify_crc;
3726 struct rar5* rar = get_context(a);
3214
3727
3215 /* Check checksums only when actually unpacking the data. There's no need
3216 * to calculate checksum when we're skipping data in solid archives
3217 * (skipping in solid archives is the same thing as unpacking compressed
3218 * data and discarding the result). */
3728 /* Check checksums only when actually unpacking the data. There's no
3729 * need to calculate checksum when we're skipping data in solid archives
3730 * (skipping in solid archives is the same thing as unpacking compressed
3731 * data and discarding the result). */
3219
3732
3220 if(!rar->skip_mode) {
3221 /* Always check checksums if we're not in skip mode */
3222 verify_crc = 1;
3223 } else {
3224 /* We can override the logic above with a compile-time option
3225 * NO_CRC_ON_SOLID_SKIP. This option is used during debugging, and it
3226 * will check checksums of unpacked data even when we're skipping it.
3227 */
3733 if(!rar->skip_mode) {
3734 /* Always check checksums if we're not in skip mode */
3735 verify_crc = 1;
3736 } else {
3737 /* We can override the logic above with a compile-time option
3738 * NO_CRC_ON_SOLID_SKIP. This option is used during debugging,
3739 * and it will check checksums of unpacked data even when
3740 * we're skipping it. */
3228
3229#if defined CHECK_CRC_ON_SOLID_SKIP
3741
3742#if defined CHECK_CRC_ON_SOLID_SKIP
3230 /* Debug case */
3231 verify_crc = 1;
3743 /* Debug case */
3744 verify_crc = 1;
3232#else
3745#else
3233 /* Normal case */
3234 verify_crc = 0;
3746 /* Normal case */
3747 verify_crc = 0;
3235#endif
3748#endif
3236 }
3749 }
3237
3750
3238 if(verify_crc) {
3239 /* During unpacking, on each unpacked block we're calling the
3240 * update_crc() function. Since we are here, the unpacking process is
3241 * already over and we can check if calculated checksum (CRC32 or
3242 * BLAKE2sp) is the same as what is stored in the archive.
3243 */
3244 if(rar->file.stored_crc32 > 0) {
3245 /* Check CRC32 only when the file contains a CRC32 value for this
3246 * file. */
3751 if(verify_crc) {
3752 /* During unpacking, on each unpacked block we're calling the
3753 * update_crc() function. Since we are here, the unpacking
3754 * process is already over and we can check if calculated
3755 * checksum (CRC32 or BLAKE2sp) is the same as what is stored
3756 * in the archive. */
3757 if(rar->file.stored_crc32 > 0) {
3758 /* Check CRC32 only when the file contains a CRC32
3759 * value for this file. */
3247
3760
3248 if(rar->file.calculated_crc32 != rar->file.stored_crc32) {
3249 /* Checksums do not match; the unpacked file is corrupted. */
3761 if(rar->file.calculated_crc32 !=
3762 rar->file.stored_crc32) {
3763 /* Checksums do not match; the unpacked file
3764 * is corrupted. */
3250
3765
3251 DEBUG_CODE {
3252 printf("Checksum error: CRC32 (was: %08x, expected: %08x)\n",
3253 rar->file.calculated_crc32, rar->file.stored_crc32);
3254 }
3766 DEBUG_CODE {
3767 printf("Checksum error: CRC32 "
3768 "(was: %08x, expected: %08x)\n",
3769 rar->file.calculated_crc32,
3770 rar->file.stored_crc32);
3771 }
3255
3256#ifndef DONT_FAIL_ON_CRC_ERROR
3772
3773#ifndef DONT_FAIL_ON_CRC_ERROR
3257 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3258 "Checksum error: CRC32");
3259 return ARCHIVE_FATAL;
3774 archive_set_error(&a->archive,
3775 ARCHIVE_ERRNO_FILE_FORMAT,
3776 "Checksum error: CRC32");
3777 return ARCHIVE_FATAL;
3260#endif
3778#endif
3261 } else {
3262 DEBUG_CODE {
3263 printf("Checksum OK: CRC32 (%08x/%08x)\n",
3264 rar->file.stored_crc32,
3265 rar->file.calculated_crc32);
3266 }
3267 }
3268 }
3779 } else {
3780 DEBUG_CODE {
3781 printf("Checksum OK: CRC32 "
3782 "(%08x/%08x)\n",
3783 rar->file.stored_crc32,
3784 rar->file.calculated_crc32);
3785 }
3786 }
3787 }
3269
3788
3270 if(rar->file.has_blake2 > 0) {
3271 /* BLAKE2sp is an optional checksum algorithm that is added to
3272 * RARv5 archives when using the `-htb` switch during creation of
3273 * archive.
3274 *
3275 * We now finalize the hash calculation by calling the `final`
3276 * function. This will generate the final hash value we can use to
3277 * compare it with the BLAKE2sp checksum that is stored in the
3278 * archive.
3279 *
3280 * The return value of this `final` function is not very helpful,
3281 * as it guards only against improper use. This is why we're
3282 * explicitly ignoring it. */
3789 if(rar->file.has_blake2 > 0) {
3790 /* BLAKE2sp is an optional checksum algorithm that is
3791 * added to RARv5 archives when using the `-htb` switch
3792 * during creation of archive.
3793 *
3794 * We now finalize the hash calculation by calling the
3795 * `final` function. This will generate the final hash
3796 * value we can use to compare it with the BLAKE2sp
3797 * checksum that is stored in the archive.
3798 *
3799 * The return value of this `final` function is not
3800 * very helpful, as it guards only against improper use.
3801 * This is why we're explicitly ignoring it. */
3283
3802
3284 uint8_t b2_buf[32];
3285 (void) blake2sp_final(&rar->file.b2state, b2_buf, 32);
3803 uint8_t b2_buf[32];
3804 (void) blake2sp_final(&rar->file.b2state, b2_buf, 32);
3286
3805
3287 if(memcmp(&rar->file.blake2sp, b2_buf, 32) != 0) {
3806 if(memcmp(&rar->file.blake2sp, b2_buf, 32) != 0) {
3288#ifndef DONT_FAIL_ON_CRC_ERROR
3807#ifndef DONT_FAIL_ON_CRC_ERROR
3289 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3290 "Checksum error: BLAKE2");
3808 archive_set_error(&a->archive,
3809 ARCHIVE_ERRNO_FILE_FORMAT,
3810 "Checksum error: BLAKE2");
3291
3811
3292 return ARCHIVE_FATAL;
3812 return ARCHIVE_FATAL;
3293#endif
3813#endif
3294 }
3295 }
3296 }
3814 }
3815 }
3816 }
3297
3817
3298 /* Finalization for this file has been successfully completed. */
3299 return ARCHIVE_OK;
3818 /* Finalization for this file has been successfully completed. */
3819 return ARCHIVE_OK;
3300}
3301
3302static int verify_global_checksums(struct archive_read* a) {
3820}
3821
3822static int verify_global_checksums(struct archive_read* a) {
3303 return verify_checksums(a);
3823 return verify_checksums(a);
3304}
3305
3306static int rar5_read_data(struct archive_read *a, const void **buff,
3824}
3825
3826static int rar5_read_data(struct archive_read *a, const void **buff,
3307 size_t *size, int64_t *offset) {
3308 int ret;
3309 struct rar5* rar = get_context(a);
3827 size_t *size, int64_t *offset) {
3828 int ret;
3829 struct rar5* rar = get_context(a);
3310
3830
3311 if(!rar->skip_mode && (rar->cstate.last_write_ptr > rar->file.unpacked_size)) {
3312 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3313 "Unpacker has written too many bytes");
3314 return ARCHIVE_FATAL;
3315 }
3831 if(rar->file.dir > 0) {
3832 /* Don't process any data if this file entry was declared
3833 * as a directory. This is needed, because entries marked as
3834 * directory doesn't have any dictionary buffer allocated, so
3835 * it's impossible to perform any decompression. */
3836 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3837 "Can't decompress an entry marked as a directory");
3838 return ARCHIVE_FAILED;
3839 }
3316
3840
3317 ret = use_data(rar, buff, size, offset);
3318 if(ret == ARCHIVE_OK) {
3319 return ret;
3320 }
3841 if(!rar->skip_mode && (rar->cstate.last_write_ptr > rar->file.unpacked_size)) {
3842 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
3843 "Unpacker has written too many bytes");
3844 return ARCHIVE_FATAL;
3845 }
3321
3846
3322 if(rar->file.eof == 1) {
3323 return ARCHIVE_EOF;
3324 }
3847 ret = use_data(rar, buff, size, offset);
3848 if(ret == ARCHIVE_OK) {
3849 return ret;
3850 }
3325
3851
3326 ret = do_unpack(a, rar, buff, size, offset);
3327 if(ret != ARCHIVE_OK) {
3328 return ret;
3329 }
3852 if(rar->file.eof == 1) {
3853 return ARCHIVE_EOF;
3854 }
3330
3855
3331 if(rar->file.bytes_remaining == 0 &&
3332 rar->cstate.last_write_ptr == rar->file.unpacked_size)
3333 {
3334 /* If all bytes of current file were processed, run finalization.
3335 *
3336 * Finalization will check checksum against proper values. If
3337 * some of the checksums will not match, we'll return an error
3338 * value in the last `archive_read_data` call to signal an error
3339 * to the user. */
3856 ret = do_unpack(a, rar, buff, size, offset);
3857 if(ret != ARCHIVE_OK) {
3858 return ret;
3859 }
3340
3860
3341 rar->file.eof = 1;
3342 return verify_global_checksums(a);
3343 }
3861 if(rar->file.bytes_remaining == 0 &&
3862 rar->cstate.last_write_ptr == rar->file.unpacked_size)
3863 {
3864 /* If all bytes of current file were processed, run
3865 * finalization.
3866 *
3867 * Finalization will check checksum against proper values. If
3868 * some of the checksums will not match, we'll return an error
3869 * value in the last `archive_read_data` call to signal an error
3870 * to the user. */
3344
3871
3345 return ARCHIVE_OK;
3872 rar->file.eof = 1;
3873 return verify_global_checksums(a);
3874 }
3875
3876 return ARCHIVE_OK;
3346}
3347
3348static int rar5_read_data_skip(struct archive_read *a) {
3877}
3878
3879static int rar5_read_data_skip(struct archive_read *a) {
3349 struct rar5* rar = get_context(a);
3880 struct rar5* rar = get_context(a);
3350
3881
3351 if(rar->main.solid) {
3352 /* In solid archives, instead of skipping the data, we need to extract
3353 * it, and dispose the result. The side effect of this operation will
3354 * be setting up the initial window buffer state needed to be able to
3355 * extract the selected file. */
3882 if(rar->main.solid) {
3883 /* In solid archives, instead of skipping the data, we need to
3884 * extract it, and dispose the result. The side effect of this
3885 * operation will be setting up the initial window buffer state
3886 * needed to be able to extract the selected file. */
3356
3887
3357 int ret;
3888 int ret;
3358
3889
3359 /* Make sure to process all blocks in the compressed stream. */
3360 while(rar->file.bytes_remaining > 0) {
3361 /* Setting the "skip mode" will allow us to skip checksum checks
3362 * during data skipping. Checking the checksum of skipped data
3363 * isn't really necessary and it's only slowing things down.
3364 *
3365 * This is incremented instead of setting to 1 because this data
3366 * skipping function can be called recursively. */
3367 rar->skip_mode++;
3890 /* Make sure to process all blocks in the compressed stream. */
3891 while(rar->file.bytes_remaining > 0) {
3892 /* Setting the "skip mode" will allow us to skip
3893 * checksum checks during data skipping. Checking the
3894 * checksum of skipped data isn't really necessary and
3895 * it's only slowing things down.
3896 *
3897 * This is incremented instead of setting to 1 because
3898 * this data skipping function can be called
3899 * recursively. */
3900 rar->skip_mode++;
3368
3901
3369 /* We're disposing 1 block of data, so we use triple NULLs in
3370 * arguments.
3371 */
3372 ret = rar5_read_data(a, NULL, NULL, NULL);
3902 /* We're disposing 1 block of data, so we use triple
3903 * NULLs in arguments. */
3904 ret = rar5_read_data(a, NULL, NULL, NULL);
3373
3905
3374 /* Turn off "skip mode". */
3375 rar->skip_mode--;
3906 /* Turn off "skip mode". */
3907 rar->skip_mode--;
3376
3908
3377 if(ret < 0) {
3378 /* Propagate any potential error conditions to the caller. */
3379 return ret;
3380 }
3381 }
3382 } else {
3383 /* In standard archives, we can just jump over the compressed stream.
3384 * Each file in non-solid archives starts from an empty window buffer.
3385 */
3909 if(ret < 0) {
3910 /* Propagate any potential error conditions
3911 * to the caller. */
3912 return ret;
3913 }
3914 }
3915 } else {
3916 /* In standard archives, we can just jump over the compressed
3917 * stream. Each file in non-solid archives starts from an empty
3918 * window buffer. */
3386
3919
3387 if(ARCHIVE_OK != consume(a, rar->file.bytes_remaining)) {
3388 return ARCHIVE_FATAL;
3389 }
3920 if(ARCHIVE_OK != consume(a, rar->file.bytes_remaining)) {
3921 return ARCHIVE_FATAL;
3922 }
3390
3923
3391 rar->file.bytes_remaining = 0;
3392 }
3924 rar->file.bytes_remaining = 0;
3925 }
3393
3926
3394 return ARCHIVE_OK;
3927 return ARCHIVE_OK;
3395}
3396
3397static int64_t rar5_seek_data(struct archive_read *a, int64_t offset,
3928}
3929
3930static int64_t rar5_seek_data(struct archive_read *a, int64_t offset,
3398 int whence)
3931 int whence)
3399{
3932{
3400 (void) a;
3401 (void) offset;
3402 (void) whence;
3933 (void) a;
3934 (void) offset;
3935 (void) whence;
3403
3936
3404 /* We're a streaming unpacker, and we don't support seeking. */
3937 /* We're a streaming unpacker, and we don't support seeking. */
3405
3938
3406 return ARCHIVE_FATAL;
3939 return ARCHIVE_FATAL;
3407}
3408
3409static int rar5_cleanup(struct archive_read *a) {
3940}
3941
3942static int rar5_cleanup(struct archive_read *a) {
3410 struct rar5* rar = get_context(a);
3943 struct rar5* rar = get_context(a);
3411
3944
3412 free(rar->cstate.window_buf);
3945 free(rar->cstate.window_buf);
3946 free(rar->cstate.filtered_buf);
3413
3947
3414 free(rar->cstate.filtered_buf);
3948 free(rar->vol.push_buf);
3415
3949
3416 free(rar->vol.push_buf);
3950 free_filters(rar);
3951 cdeque_free(&rar->cstate.filters);
3417
3952
3418 free_filters(rar);
3419 cdeque_free(&rar->cstate.filters);
3953 free(rar);
3954 a->format->data = NULL;
3420
3955
3421 free(rar);
3422 a->format->data = NULL;
3423
3424 return ARCHIVE_OK;
3956 return ARCHIVE_OK;
3425}
3426
3427static int rar5_capabilities(struct archive_read * a) {
3957}
3958
3959static int rar5_capabilities(struct archive_read * a) {
3428 (void) a;
3429 return 0;
3960 (void) a;
3961 return 0;
3430}
3431
3432static int rar5_has_encrypted_entries(struct archive_read *_a) {
3962}
3963
3964static int rar5_has_encrypted_entries(struct archive_read *_a) {
3433 (void) _a;
3965 (void) _a;
3434
3966
3435 /* Unsupported for now. */
3436 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
3967 /* Unsupported for now. */
3968 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
3437}
3438
3439static int rar5_init(struct rar5* rar) {
3969}
3970
3971static int rar5_init(struct rar5* rar) {
3440 ssize_t i;
3972 ssize_t i;
3441
3973
3442 memset(rar, 0, sizeof(struct rar5));
3974 memset(rar, 0, sizeof(struct rar5));
3443
3975
3444 /* Decrypt the magic signature pattern. Check the comment near the
3445 * `rar5_signature` symbol to read the rationale behind this. */
3976 /* Decrypt the magic signature pattern. Check the comment near the
3977 * `rar5_signature` symbol to read the rationale behind this. */
3446
3978
3447 if(rar5_signature[0] == 243) {
3448 for(i = 0; i < rar5_signature_size; i++) {
3449 rar5_signature[i] ^= 0xA1;
3450 }
3451 }
3979 if(rar5_signature[0] == 243) {
3980 for(i = 0; i < rar5_signature_size; i++) {
3981 rar5_signature[i] ^= 0xA1;
3982 }
3983 }
3452
3984
3453 if(CDE_OK != cdeque_init(&rar->cstate.filters, 8192))
3454 return ARCHIVE_FATAL;
3985 if(CDE_OK != cdeque_init(&rar->cstate.filters, 8192))
3986 return ARCHIVE_FATAL;
3455
3987
3456 return ARCHIVE_OK;
3988 return ARCHIVE_OK;
3457}
3458
3459int archive_read_support_format_rar5(struct archive *_a) {
3989}
3990
3991int archive_read_support_format_rar5(struct archive *_a) {
3460 struct archive_read* ar;
3461 int ret;
3462 struct rar5* rar;
3992 struct archive_read* ar;
3993 int ret;
3994 struct rar5* rar;
3463
3995
3464 if(ARCHIVE_OK != (ret = get_archive_read(_a, &ar)))
3465 return ret;
3996 if(ARCHIVE_OK != (ret = get_archive_read(_a, &ar)))
3997 return ret;
3466
3998
3467 rar = malloc(sizeof(*rar));
3468 if(rar == NULL) {
3469 archive_set_error(&ar->archive, ENOMEM, "Can't allocate rar5 data");
3470 return ARCHIVE_FATAL;
3471 }
3999 rar = malloc(sizeof(*rar));
4000 if(rar == NULL) {
4001 archive_set_error(&ar->archive, ENOMEM,
4002 "Can't allocate rar5 data");
4003 return ARCHIVE_FATAL;
4004 }
3472
4005
3473 if(ARCHIVE_OK != rar5_init(rar)) {
3474 archive_set_error(&ar->archive, ENOMEM, "Can't allocate rar5 filter "
3475 "buffer");
3476 return ARCHIVE_FATAL;
3477 }
4006 if(ARCHIVE_OK != rar5_init(rar)) {
4007 archive_set_error(&ar->archive, ENOMEM,
4008 "Can't allocate rar5 filter buffer");
4009 return ARCHIVE_FATAL;
4010 }
3478
4011
3479 ret = __archive_read_register_format(ar,
3480 rar,
3481 "rar5",
3482 rar5_bid,
3483 rar5_options,
3484 rar5_read_header,
3485 rar5_read_data,
3486 rar5_read_data_skip,
3487 rar5_seek_data,
3488 rar5_cleanup,
3489 rar5_capabilities,
3490 rar5_has_encrypted_entries);
4012 ret = __archive_read_register_format(ar,
4013 rar,
4014 "rar5",
4015 rar5_bid,
4016 rar5_options,
4017 rar5_read_header,
4018 rar5_read_data,
4019 rar5_read_data_skip,
4020 rar5_seek_data,
4021 rar5_cleanup,
4022 rar5_capabilities,
4023 rar5_has_encrypted_entries);
3491
4024
3492 if(ret != ARCHIVE_OK) {
3493 (void) rar5_cleanup(ar);
3494 }
4025 if(ret != ARCHIVE_OK) {
4026 (void) rar5_cleanup(ar);
4027 }
3495
4028
3496 return ret;
4029 return ret;
3497}
4030}