From 12326d715eafc241fcbd076d078cdc6171c98076 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 23 Jan 2024 06:49:16 +0100 Subject: Merging upstream version 0.8. Signed-off-by: Daniel Baumann --- lzip_decompress.c | 67 ++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 35 deletions(-) (limited to 'lzip_decompress.c') diff --git a/lzip_decompress.c b/lzip_decompress.c index 157a798..492523d 100644 --- a/lzip_decompress.c +++ b/lzip_decompress.c @@ -1,7 +1,7 @@ /* * LZIP decompressor * - * Copyright (C) 2016-2021 Antonio Diaz Diaz. + * Copyright (C) 2016-2024 Antonio Diaz Diaz. * * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ @@ -32,17 +32,17 @@ static inline State St_set_char(const State st) static inline State St_set_match(const State st) { - return ((st < 7) ? 7 : 10); + return (st < 7) ? 7 : 10; } static inline State St_set_rep(const State st) { - return ((st < 7) ? 8 : 11); + return (st < 7) ? 8 : 11; } static inline State St_set_short_rep(const State st) { - return ((st < 7) ? 9 : 11); + return (st < 7) ? 9 : 11; } @@ -189,12 +189,12 @@ static inline void CRC32_update_buf(uint32_t * const crc, STATIC_RW_DATA const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */ -typedef uint8_t Lzip_header[6]; /* 0-3 magic bytes */ +enum { Lh_size = 6 }; +typedef uint8_t Lzip_header[Lh_size]; /* 0-3 magic bytes */ /* 4 version */ /* 5 coded dictionary size */ -enum { Lh_size = 6 }; -static inline bool Lh_verify_magic(const Lzip_header data) +static inline bool Lh_check_magic(const Lzip_header data) { int i; @@ -205,34 +205,34 @@ static inline bool Lh_verify_magic(const Lzip_header data) } /* detect (truncated) header */ -static inline bool Lh_verify_prefix(const Lzip_header data, const int sz) +static inline bool Lh_check_prefix(const Lzip_header data, const int sz) { int i; for (i = 0; i < sz && i < 4; ++i) if (data[i] != lzip_magic[i]) return false; - return (sz > 0); + return sz > 0; } /* detect corrupt header */ -static inline bool Lh_verify_corrupt(const Lzip_header data) +static inline bool Lh_check_corrupt(const Lzip_header data) { int matches = 0; int i; for (i = 0; i < 4; ++i) if (data[i] == lzip_magic[i]) ++matches; - return (matches > 1 && matches < 4); + return matches > 1 && matches < 4; } -static inline bool Lh_verify_version(const Lzip_header data) +static inline bool Lh_check_version(const Lzip_header data) { - return (data[4] == 1); + return data[4] == 1; } static inline unsigned Lh_get_dictionary_size(const Lzip_header data) { - unsigned sz = (1 << (data[5] & 0x1F)); + unsigned sz = 1 << (data[5] & 0x1F); if (sz > min_dictionary_size) sz -= (sz / 16) * ((data[5] >> 5) & 7); @@ -240,11 +240,11 @@ static inline unsigned Lh_get_dictionary_size(const Lzip_header data) } -typedef uint8_t Lzip_trailer[20]; +enum { Lt_size = 20 }; +typedef uint8_t Lzip_trailer[Lt_size]; /* 0-3 CRC32 of the uncompressed data */ /* 4-11 size of the uncompressed data */ /* 12-19 member size including header and trailer */ -enum { Lt_size = 20 }; static inline unsigned Lt_get_data_crc(const Lzip_trailer data) { @@ -364,9 +364,9 @@ static inline void Rd_load(struct Range_decoder * const rdec) int i; rdec->code = 0; - for (i = 0; i < 5; ++i) - rdec->code = (rdec->code << 8) | Rd_get_byte(rdec); rdec->range = 0xFFFFFFFFU; + Rd_get_byte(rdec); /* discard first byte of the LZMA stream */ + for (i = 0; i < 4; ++i) rdec->code = (rdec->code << 8) | Rd_get_byte(rdec); } static inline void Rd_normalize(struct Range_decoder * const rdec) @@ -500,11 +500,12 @@ static inline unsigned Rd_decode_len(struct Range_decoder * const rdec, const int pos_state) { if (Rd_decode_bit(rdec, &lm->choice1) == 0) - return Rd_decode_tree3(rdec, lm->bm_low[pos_state]); + return min_match_len + + Rd_decode_tree3(rdec, lm->bm_low[pos_state]); if (Rd_decode_bit(rdec, &lm->choice2) == 0) - return len_low_symbols + + return min_match_len + len_low_symbols + Rd_decode_tree3(rdec, lm->bm_mid[pos_state]); - return len_low_symbols + len_mid_symbols + + return min_match_len + len_low_symbols + len_mid_symbols + Rd_decode_tree8(rdec, lm->bm_high); } @@ -668,7 +669,7 @@ LZd_data_position(const struct LZ_decoder * const d) } -static bool LZd_verify_trailer(struct LZ_decoder * const d) +static bool LZd_check_trailer(struct LZ_decoder * const d) { Lzip_trailer trailer; int i = 0; @@ -676,9 +677,9 @@ static bool LZd_verify_trailer(struct LZ_decoder * const d) while (i < Lt_size) trailer[i++] = Rd_get_byte(d->rdec); - return (Lt_get_data_crc(trailer) == LZd_crc(d) && + return Lt_get_data_crc(trailer) == LZd_crc(d) && Lt_get_data_size(trailer) == LZd_data_position(d) && - Lt_get_member_size(trailer) == Rd_member_position(d->rdec)); + Lt_get_member_size(trailer) == Rd_member_position(d->rdec); } @@ -736,11 +737,11 @@ static int LZd_decode_member(struct LZ_decoder * const d) rep0 = distance; } state = St_set_rep(state); - len = min_match_len + Rd_decode_len(rdec, &d->rep_len_model, pos_state); + len = Rd_decode_len(rdec, &d->rep_len_model, pos_state); } else { /* match */ unsigned distance; - len = min_match_len + Rd_decode_len(rdec, &d->match_len_model, pos_state); + len = Rd_decode_len(rdec, &d->match_len_model, pos_state); distance = Rd_decode_tree6(rdec, d->bm_dis_slot[get_len_state(len)]); if (distance >= start_dis_model) { const unsigned dis_slot = distance; @@ -760,15 +761,11 @@ static int LZd_decode_member(struct LZ_decoder * const d) if (d->write_error) return LZIP_WRITE_ERROR; if (len == min_match_len) { /* End Of Stream marker */ - if (LZd_verify_trailer(d)) + if (LZd_check_trailer(d)) return 0; else return LZIP_BAD_CRC; } - if (len == min_match_len + 1) { /* Sync Flush marker */ - Rd_load(rdec); - continue; - } return LZIP_BAD_DATA; /* unknown marker */ } } @@ -820,18 +817,18 @@ int lzip_decompress(unsigned char *inbuf, long in_len, if (Rd_finished(&rdec)) { /* End Of File */ if (first_member) retval = LZIP_HEADER1_EOF; - else if (Lh_verify_prefix(header, size)) + else if (Lh_check_prefix(header, size)) retval = LZIP_HEADER2_EOF; break; } - if (!Lh_verify_magic(header)) { + if (!Lh_check_magic(header)) { if (first_member) retval = LZIP_BAD_MAGIC1; - else if (Lh_verify_corrupt(header)) + else if (Lh_check_corrupt(header)) retval = LZIP_BAD_MAGIC2; break; } - if (!Lh_verify_version(header)) { + if (!Lh_check_version(header)) { retval = LZIP_BAD_VERSION; break; } -- cgit v1.2.3