diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2008-09-12 22:41:40 +0300 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2008-09-12 22:41:40 +0300 |
commit | 320601b2c7b08fc7da9da18d5bf7c3c1a189b080 (patch) | |
tree | 3846af51e6904d900e5cd8337baf0728a42a07fe /src/liblzma/common | |
parent | Simplified debug/known_sizes.c to match the relaxed (diff) | |
download | xz-320601b2c7b08fc7da9da18d5bf7c3c1a189b080.tar.xz |
Improved the Stream Flags handling API.
Diffstat (limited to 'src/liblzma/common')
-rw-r--r-- | src/liblzma/common/stream_decoder.c | 5 | ||||
-rw-r--r-- | src/liblzma/common/stream_encoder.c | 2 | ||||
-rw-r--r-- | src/liblzma/common/stream_flags_common.c | 28 | ||||
-rw-r--r-- | src/liblzma/common/stream_flags_common.h | 9 | ||||
-rw-r--r-- | src/liblzma/common/stream_flags_decoder.c | 3 | ||||
-rw-r--r-- | src/liblzma/common/stream_flags_encoder.c | 10 |
6 files changed, 43 insertions, 14 deletions
diff --git a/src/liblzma/common/stream_decoder.c b/src/liblzma/common/stream_decoder.c index 884c4e9d..5360d87c 100644 --- a/src/liblzma/common/stream_decoder.c +++ b/src/liblzma/common/stream_decoder.c @@ -294,9 +294,8 @@ stream_decode(lzma_coder *coder, lzma_allocator *allocator, // Compare that the Stream Flags fields are identical in // both Stream Header and Stream Footer. - if (!lzma_stream_flags_equal(&coder->stream_flags, - &footer_flags)) - return LZMA_DATA_ERROR; + return_if_error(lzma_stream_flags_compare( + &coder->stream_flags, &footer_flags)); if (!coder->concatenated) return LZMA_STREAM_END; diff --git a/src/liblzma/common/stream_encoder.c b/src/liblzma/common/stream_encoder.c index b21ee652..8748f52e 100644 --- a/src/liblzma/common/stream_encoder.c +++ b/src/liblzma/common/stream_encoder.c @@ -180,6 +180,7 @@ stream_encode(lzma_coder *coder, lzma_allocator *allocator, // Encode the Stream Footer into coder->buffer. const lzma_stream_flags stream_flags = { + .version = 0, .backward_size = lzma_index_size(coder->index), .check = coder->block_options.check, }; @@ -247,6 +248,7 @@ lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator, // Encode the Stream Header lzma_stream_flags stream_flags = { + .version = 0, .check = check, }; return_if_error(lzma_stream_header_encode( diff --git a/src/liblzma/common/stream_flags_common.c b/src/liblzma/common/stream_flags_common.c index c44b3ff2..aaa9fe02 100644 --- a/src/liblzma/common/stream_flags_common.c +++ b/src/liblzma/common/stream_flags_common.c @@ -24,17 +24,31 @@ const uint8_t lzma_header_magic[6] = { 0xFF, 0x4C, 0x5A, 0x4D, 0x41, 0x00 }; const uint8_t lzma_footer_magic[2] = { 0x59, 0x5A }; -extern LZMA_API lzma_bool -lzma_stream_flags_equal(const lzma_stream_flags *a, const lzma_stream_flags *b) +extern LZMA_API lzma_ret +lzma_stream_flags_compare( + const lzma_stream_flags *a, const lzma_stream_flags *b) { + // We can compare only version 0 structures. + if (a->version != 0 || b->version != 0) + return LZMA_HEADER_ERROR; + + // Check type + if ((unsigned int)(a->check) > LZMA_CHECK_ID_MAX + || (unsigned int)(b->check) > LZMA_CHECK_ID_MAX) + return LZMA_PROG_ERROR; + if (a->check != b->check) - return false; + return LZMA_DATA_ERROR; // Backward Sizes are compared only if they are known in both. if (a->backward_size != LZMA_VLI_VALUE_UNKNOWN - && b->backward_size != LZMA_VLI_VALUE_UNKNOWN - && a->backward_size != b->backward_size) - return false; + && b->backward_size != LZMA_VLI_VALUE_UNKNOWN) { + if (!is_backward_size_valid(a) || !is_backward_size_valid(b)) + return LZMA_PROG_ERROR; + + if (a->backward_size != b->backward_size) + return LZMA_DATA_ERROR; + } - return true; + return LZMA_OK; } diff --git a/src/liblzma/common/stream_flags_common.h b/src/liblzma/common/stream_flags_common.h index 6e57857b..f422b02a 100644 --- a/src/liblzma/common/stream_flags_common.h +++ b/src/liblzma/common/stream_flags_common.h @@ -28,4 +28,13 @@ extern const uint8_t lzma_header_magic[6]; extern const uint8_t lzma_footer_magic[2]; + +static inline bool +is_backward_size_valid(const lzma_stream_flags *options) +{ + return options->backward_size >= LZMA_BACKWARD_SIZE_MIN + && options->backward_size <= LZMA_BACKWARD_SIZE_MAX + && (options->backward_size & 3) == 0; +} + #endif diff --git a/src/liblzma/common/stream_flags_decoder.c b/src/liblzma/common/stream_flags_decoder.c index ccc1539d..a54ce813 100644 --- a/src/liblzma/common/stream_flags_decoder.c +++ b/src/liblzma/common/stream_flags_decoder.c @@ -27,6 +27,7 @@ stream_flags_decode(lzma_stream_flags *options, const uint8_t *in) if (in[0] != 0x00 || (in[1] & 0xF0)) return true; + options->version = 0; options->check = in[1] & 0x0F; return false; @@ -53,7 +54,7 @@ lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in) return LZMA_HEADER_ERROR; // Set Backward Size to indicate unknown value. That way - // lzma_stream_flags_equal can be used to compare Stream Header + // lzma_stream_flags_compare() can be used to compare Stream Header // and Stream Footer while keeping it useful also for comparing // two Stream Footers. options->backward_size = LZMA_VLI_VALUE_UNKNOWN; diff --git a/src/liblzma/common/stream_flags_encoder.c b/src/liblzma/common/stream_flags_encoder.c index 1d736a8a..88ed81e0 100644 --- a/src/liblzma/common/stream_flags_encoder.c +++ b/src/liblzma/common/stream_flags_encoder.c @@ -39,6 +39,9 @@ lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out) assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE + 4 == LZMA_STREAM_HEADER_SIZE); + if (options->version != 0) + return LZMA_HEADER_ERROR; + // Magic memcpy(out, lzma_header_magic, sizeof(lzma_header_magic)); @@ -63,10 +66,11 @@ lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out) assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic) == LZMA_STREAM_HEADER_SIZE); + if (options->version != 0) + return LZMA_HEADER_ERROR; + // Backward Size - if (options->backward_size < LZMA_BACKWARD_SIZE_MIN - || options->backward_size > LZMA_BACKWARD_SIZE_MAX - || (options->backward_size & 3)) + if (!is_backward_size_valid(options)) return LZMA_PROG_ERROR; integer_write_32(out + 4, options->backward_size / 4 - 1); |