From 203b008eb220208981902e0db541c02d1c1c9f5e Mon Sep 17 00:00:00 2001 From: Jia Tan Date: Wed, 17 Aug 2022 17:59:51 +0800 Subject: liblzma: Replaced hardcoded 0x0 index indicator byte with macro --- src/liblzma/common/index.h | 3 +++ src/liblzma/common/index_decoder.c | 2 +- src/liblzma/common/index_encoder.c | 2 +- src/liblzma/common/index_hash.c | 2 +- src/liblzma/common/stream_decoder.c | 3 ++- src/liblzma/common/stream_decoder_mt.c | 2 +- 6 files changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/liblzma/common/index.h b/src/liblzma/common/index.h index 64e97247..ea396714 100644 --- a/src/liblzma/common/index.h +++ b/src/liblzma/common/index.h @@ -22,6 +22,9 @@ /// Maximum Unpadded Size #define UNPADDED_SIZE_MAX (LZMA_VLI_MAX & ~LZMA_VLI_C(3)) +/// Index Indicator based on xz specification +#define INDEX_INDICATOR 0 + /// Get the size of the Index Padding field. This is needed by Index encoder /// and decoder, but applications should have no use for this. diff --git a/src/liblzma/common/index_decoder.c b/src/liblzma/common/index_decoder.c index b2689885..8622b2f0 100644 --- a/src/liblzma/common/index_decoder.c +++ b/src/liblzma/common/index_decoder.c @@ -80,7 +80,7 @@ index_decode(void *coder_ptr, const lzma_allocator *allocator, // format". One could argue that the application should // verify the Index Indicator before trying to decode the // Index, but well, I suppose it is simpler this way. - if (in[(*in_pos)++] != 0x00) + if (in[(*in_pos)++] != INDEX_INDICATOR) return LZMA_DATA_ERROR; coder->sequence = SEQ_COUNT; diff --git a/src/liblzma/common/index_encoder.c b/src/liblzma/common/index_encoder.c index ac97d0ce..c7cafb72 100644 --- a/src/liblzma/common/index_encoder.c +++ b/src/liblzma/common/index_encoder.c @@ -65,7 +65,7 @@ index_encode(void *coder_ptr, while (*out_pos < out_size) switch (coder->sequence) { case SEQ_INDICATOR: - out[*out_pos] = 0x00; + out[*out_pos] = INDEX_INDICATOR; ++*out_pos; coder->sequence = SEQ_COUNT; break; diff --git a/src/liblzma/common/index_hash.c b/src/liblzma/common/index_hash.c index 34df85d7..c3c56674 100644 --- a/src/liblzma/common/index_hash.c +++ b/src/liblzma/common/index_hash.c @@ -190,7 +190,7 @@ lzma_index_hash_decode(lzma_index_hash *index_hash, const uint8_t *in, switch (index_hash->sequence) { case SEQ_BLOCK: // Check the Index Indicator is present. - if (in[(*in_pos)++] != 0x00) + if (in[(*in_pos)++] != INDEX_INDICATOR) return LZMA_DATA_ERROR; index_hash->sequence = SEQ_COUNT; diff --git a/src/liblzma/common/stream_decoder.c b/src/liblzma/common/stream_decoder.c index dcf7c149..64283812 100644 --- a/src/liblzma/common/stream_decoder.c +++ b/src/liblzma/common/stream_decoder.c @@ -12,6 +12,7 @@ #include "stream_decoder.h" #include "block_decoder.h" +#include "index.h" typedef struct { @@ -164,7 +165,7 @@ stream_decode(void *coder_ptr, const lzma_allocator *allocator, if (coder->pos == 0) { // Detect if it's Index. - if (in[*in_pos] == 0x00) { + if (in[*in_pos] == INDEX_INDICATOR) { coder->sequence = SEQ_INDEX; break; } diff --git a/src/liblzma/common/stream_decoder_mt.c b/src/liblzma/common/stream_decoder_mt.c index 5733c764..fd5cd7fd 100644 --- a/src/liblzma/common/stream_decoder_mt.c +++ b/src/liblzma/common/stream_decoder_mt.c @@ -887,7 +887,7 @@ decode_block_header(struct lzma_stream_coder *coder, if (coder->pos == 0) { // Detect if it's Index. - if (in[*in_pos] == 0x00) + if (in[*in_pos] == INDEX_INDICATOR) return LZMA_INDEX_DETECTED; // Calculate the size of the Block Header. Note that -- cgit v1.2.3 From f16e12d5e755d371247202fcccbcccd1ec16b2cf Mon Sep 17 00:00:00 2001 From: Jia Tan Date: Wed, 17 Aug 2022 20:20:16 +0800 Subject: liblzma: Add NULL check to lzma_index_hash_append. This is for consistency with lzma_index_append. --- src/liblzma/common/index_hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/liblzma/common/index_hash.c b/src/liblzma/common/index_hash.c index c3c56674..f55f7bc8 100644 --- a/src/liblzma/common/index_hash.c +++ b/src/liblzma/common/index_hash.c @@ -145,7 +145,7 @@ lzma_index_hash_append(lzma_index_hash *index_hash, lzma_vli unpadded_size, lzma_vli uncompressed_size) { // Validate the arguments. - if (index_hash->sequence != SEQ_BLOCK + if (index_hash == NULL || index_hash->sequence != SEQ_BLOCK || unpadded_size < UNPADDED_SIZE_MIN || unpadded_size > UNPADDED_SIZE_MAX || uncompressed_size > LZMA_VLI_MAX) -- cgit v1.2.3 From 84f9687cbae972c2c342e10bf69f8ec8f70ae111 Mon Sep 17 00:00:00 2001 From: Jia Tan Date: Thu, 5 Jan 2023 20:57:25 +0800 Subject: liblzma: Remove common.h include from common/index.h. common/index.h is needed by liblzma internally and tests. common.h will include and define many things that are not needed by the tests. Also, this prevents include order problems because common.h will redefine LZMA_API resulting in a warning. --- src/liblzma/common/index.c | 1 + src/liblzma/common/index.h | 9 +++++++-- src/liblzma/common/index_decoder.h | 1 + src/liblzma/common/stream_buffer_encoder.c | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/liblzma/common/index.c b/src/liblzma/common/index.c index 24ec3c10..97cc9f95 100644 --- a/src/liblzma/common/index.c +++ b/src/liblzma/common/index.c @@ -10,6 +10,7 @@ // /////////////////////////////////////////////////////////////////////////////// +#include "common.h" #include "index.h" #include "stream_flags_common.h" diff --git a/src/liblzma/common/index.h b/src/liblzma/common/index.h index ea396714..031efcc7 100644 --- a/src/liblzma/common/index.h +++ b/src/liblzma/common/index.h @@ -2,6 +2,13 @@ // /// \file index.h /// \brief Handling of Index +/// \note This header file does not include common.h or lzma.h because +/// this file is needed by both liblzma internally and by the +/// tests. Including common.h will include and define many things +/// the tests do not need and prevents issues with header file +/// include order. This way, if lzma.h or common.h are not +/// included before this file it will break on every OS instead +/// of causing more subtle errors. // // Author: Lasse Collin // @@ -13,8 +20,6 @@ #ifndef LZMA_INDEX_H #define LZMA_INDEX_H -#include "common.h" - /// Minimum Unpadded Size #define UNPADDED_SIZE_MIN LZMA_VLI_C(5) diff --git a/src/liblzma/common/index_decoder.h b/src/liblzma/common/index_decoder.h index 1af433b5..3fec4833 100644 --- a/src/liblzma/common/index_decoder.h +++ b/src/liblzma/common/index_decoder.h @@ -13,6 +13,7 @@ #ifndef LZMA_INDEX_DECODER_H #define LZMA_INDEX_DECODER_H +#include "common.h" #include "index.h" diff --git a/src/liblzma/common/stream_buffer_encoder.c b/src/liblzma/common/stream_buffer_encoder.c index af49554a..73157590 100644 --- a/src/liblzma/common/stream_buffer_encoder.c +++ b/src/liblzma/common/stream_buffer_encoder.c @@ -10,6 +10,7 @@ // /////////////////////////////////////////////////////////////////////////////// +#include "common.h" #include "index.h" -- cgit v1.2.3