diff options
Diffstat (limited to 'contrib/libarchive/libarchive/archive_read_support_format_rar5.c')
-rw-r--r-- | contrib/libarchive/libarchive/archive_read_support_format_rar5.c | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/contrib/libarchive/libarchive/archive_read_support_format_rar5.c b/contrib/libarchive/libarchive/archive_read_support_format_rar5.c index 7681c9486147..9314f7a9f9c3 100644 --- a/contrib/libarchive/libarchive/archive_read_support_format_rar5.c +++ b/contrib/libarchive/libarchive/archive_read_support_format_rar5.c @@ -24,6 +24,7 @@ */ #include "archive_platform.h" +#include "archive_endian.h" #ifdef HAVE_ERRNO_H #include <errno.h> @@ -225,18 +226,17 @@ struct bit_reader { int in_addr; /* Current byte pointer. */ }; -/* RARv5 block header structure. */ +/* RARv5 block header structure. Use bf_* functions to get values from + * block_flags_u8 field. I.e. bf_byte_count, etc. */ struct compressed_block_header { - union { - struct { - uint8_t bit_size : 3; - uint8_t byte_count : 3; - uint8_t is_last_block : 1; - uint8_t is_table_present : 1; - } block_flags; - uint8_t block_flags_u8; - }; - + /* block_flags_u8 contain fields encoded in little-endian bitfield: + * + * - table present flag (shr 7, and 1), + * - last block flag (shr 6, and 1), + * - byte_count (shr 3, and 7), + * - bit_size (shr 0, and 7). + */ + uint8_t block_flags_u8; uint8_t block_cksum; }; @@ -429,26 +429,35 @@ static void cdeque_free(struct cdeque* d) { d->cap_mask = 0; } +static inline +uint8_t bf_bit_size(const struct compressed_block_header* hdr) { + return hdr->block_flags_u8 & 7; +} + +static inline +uint8_t bf_byte_count(const struct compressed_block_header* hdr) { + return (hdr->block_flags_u8 >> 3) & 7; +} + +static inline +uint8_t bf_is_table_present(const struct compressed_block_header* hdr) { + return (hdr->block_flags_u8 >> 7) & 1; +} + static inline struct rar5* get_context(struct archive_read* a) { return (struct rar5*) a->format->data; } -// TODO: make sure these functions return a little endian number - /* Convenience functions used by filter implementations. */ static uint32_t read_filter_data(struct rar5* rar, uint32_t offset) { - uint32_t* dptr = (uint32_t*) &rar->cstate.window_buf[offset]; - // TODO: bswap if big endian - return *dptr; + return archive_le32dec(&rar->cstate.window_buf[offset]); } static void write_filter_data(struct rar5* rar, uint32_t offset, uint32_t value) { - uint32_t* dptr = (uint32_t*) &rar->cstate.filtered_buf[offset]; - // TODO: bswap if big endian - *dptr = value; + archive_le32enc(&rar->cstate.filtered_buf[offset], value); } static void circular_memcpy(uint8_t* dst, uint8_t* window, const int mask, @@ -995,8 +1004,7 @@ static int read_u32(struct archive_read* a, uint32_t* pvalue) { if(!read_ahead(a, 4, &p)) return 0; - *pvalue = *(const uint32_t*)p; - + *pvalue = archive_le32dec(p); return ARCHIVE_OK == consume(a, 4) ? 1 : 0; } @@ -1005,8 +1013,7 @@ static int read_u64(struct archive_read* a, uint64_t* pvalue) { if(!read_ahead(a, 8, &p)) return 0; - *pvalue = *(const uint64_t*)p; - + *pvalue = archive_le64dec(p); return ARCHIVE_OK == consume(a, 8) ? 1 : 0; } @@ -1936,7 +1943,7 @@ static int create_decode_tables(uint8_t* bit_length, dist = bit_field - table->decode_len[cur_len - 1]; dist >>= (16 - cur_len); - pos = table->decode_pos[cur_len] + dist; + pos = table->decode_pos[cur_len & 15] + dist; if(cur_len < rar5_countof(table->decode_pos) && pos < size) { table->quick_num[code] = table->decode_num[pos]; } else { @@ -2159,17 +2166,17 @@ static int parse_block_header(struct archive_read* a, const uint8_t* p, { memcpy(hdr, p, sizeof(struct compressed_block_header)); - if(hdr->block_flags.byte_count > 2) { + if(bf_byte_count(hdr) > 2) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Unsupported block header size (was %d, max is 2)", - hdr->block_flags.byte_count); + bf_byte_count(hdr)); return ARCHIVE_FATAL; } /* This should probably use bit reader interface in order to be more * future-proof. */ *block_size = 0; - switch(hdr->block_flags.byte_count) { + switch(bf_byte_count(hdr)) { /* 1-byte block size */ case 0: *block_size = *(const uint8_t*) &p[2]; @@ -2177,12 +2184,12 @@ static int parse_block_header(struct archive_read* a, const uint8_t* p, /* 2-byte block size */ case 1: - *block_size = *(const uint16_t*) &p[2]; + *block_size = archive_le16dec(&p[2]); break; /* 3-byte block size */ case 2: - *block_size = *(const uint32_t*) &p[2]; + *block_size = archive_le32dec(&p[2]); *block_size &= 0x00FFFFFF; break; @@ -2379,7 +2386,7 @@ static int do_uncompress_block(struct archive_read* a, const uint8_t* p) { const int cmask = rar->cstate.window_mask; const struct compressed_block_header* hdr = &rar->last_block_hdr; - const uint8_t bit_size = 1 + hdr->block_flags.bit_size; + const uint8_t bit_size = 1 + bf_bit_size(hdr); while(1) { if(rar->cstate.write_ptr - rar->cstate.last_write_ptr > @@ -2777,7 +2784,7 @@ static int process_block(struct archive_read* a) { /* Skip block header. Next data is huffman tables, if present. */ ssize_t to_skip = sizeof(struct compressed_block_header) + - rar->last_block_hdr.block_flags.byte_count + 1; + bf_byte_count(&rar->last_block_hdr) + 1; if(ARCHIVE_OK != consume(a, to_skip)) return ARCHIVE_EOF; @@ -2833,7 +2840,7 @@ static int process_block(struct archive_read* a) { rar->bits.in_addr = 0; rar->bits.bit_addr = 0; - if(rar->last_block_hdr.block_flags.is_table_present) { + if(bf_is_table_present(&rar->last_block_hdr)) { /* Load Huffman tables. */ ret = parse_tables(a, rar, p); if(ret != ARCHIVE_OK) { |