2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file block_header_decoder.c
|
2008-12-27 17:27:49 +00:00
|
|
|
/// \brief Decodes Block Header from .xz files
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "check.h"
|
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_ret)
|
2008-12-27 17:27:49 +00:00
|
|
|
lzma_block_header_decode(lzma_block *block,
|
2012-07-17 15:19:59 +00:00
|
|
|
const lzma_allocator *allocator, const uint8_t *in)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-06-18 15:02:10 +00:00
|
|
|
// NOTE: We consider the header to be corrupt not only when the
|
|
|
|
// CRC32 doesn't match, but also when variable-length integers
|
2008-08-28 19:53:15 +00:00
|
|
|
// are invalid or over 63 bits, or if the header is too small
|
2008-06-18 15:02:10 +00:00
|
|
|
// to contain the claimed information.
|
|
|
|
|
2022-12-08 15:30:09 +00:00
|
|
|
// Catch unexpected NULL pointers.
|
|
|
|
if (block == NULL || block->filters == NULL || in == NULL)
|
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Initialize the filter options array. This way the caller can
|
|
|
|
// safely free() the options even if an error occurs in this function.
|
2008-11-19 18:46:52 +00:00
|
|
|
for (size_t i = 0; i <= LZMA_FILTERS_MAX; ++i) {
|
2008-12-27 17:27:49 +00:00
|
|
|
block->filters[i].id = LZMA_VLI_UNKNOWN;
|
|
|
|
block->filters[i].options = NULL;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 19:03:30 +00:00
|
|
|
// Versions 0 and 1 are supported. If a newer version was specified,
|
|
|
|
// we need to downgrade it.
|
|
|
|
if (block->version > 1)
|
|
|
|
block->version = 1;
|
|
|
|
|
|
|
|
// This isn't a Block Header option, but since the decompressor will
|
|
|
|
// read it if version >= 1, it's better to initialize it here than
|
|
|
|
// to expect the caller to do it since in almost all cases this
|
|
|
|
// should be false.
|
|
|
|
block->ignore_check = false;
|
2008-12-27 17:27:49 +00:00
|
|
|
|
2008-11-19 18:46:52 +00:00
|
|
|
// Validate Block Header Size and Check type. The caller must have
|
|
|
|
// already set these, so it is a programming error if this test fails.
|
2008-12-27 17:27:49 +00:00
|
|
|
if (lzma_block_header_size_decode(in[0]) != block->header_size
|
|
|
|
|| (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
|
2008-06-18 15:02:10 +00:00
|
|
|
return LZMA_PROG_ERROR;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Exclude the CRC32 field.
|
2008-12-27 17:27:49 +00:00
|
|
|
const size_t in_size = block->header_size - 4;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Verify CRC32
|
2019-12-30 22:41:28 +00:00
|
|
|
if (lzma_crc32(in, in_size, 0) != read32le(in + in_size)) {
|
2018-10-26 19:49:10 +00:00
|
|
|
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
2008-06-18 15:02:10 +00:00
|
|
|
return LZMA_DATA_ERROR;
|
2018-10-26 19:49:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Check for unsupported flags.
|
|
|
|
if (in[1] & 0x3C)
|
2008-09-13 09:10:43 +00:00
|
|
|
return LZMA_OPTIONS_ERROR;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Start after the Block Header Size and Block Flags fields.
|
|
|
|
size_t in_pos = 2;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Compressed Size
|
|
|
|
if (in[1] & 0x40) {
|
2008-12-27 17:27:49 +00:00
|
|
|
return_if_error(lzma_vli_decode(&block->compressed_size,
|
2008-06-18 15:02:10 +00:00
|
|
|
NULL, in, &in_pos, in_size));
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-11-19 18:46:52 +00:00
|
|
|
// Validate Compressed Size. This checks that it isn't zero
|
|
|
|
// and that the total size of the Block is a valid VLI.
|
2008-12-27 17:27:49 +00:00
|
|
|
if (lzma_block_unpadded_size(block) == 0)
|
2007-12-08 22:42:33 +00:00
|
|
|
return LZMA_DATA_ERROR;
|
2008-06-18 15:02:10 +00:00
|
|
|
} else {
|
2008-12-27 17:27:49 +00:00
|
|
|
block->compressed_size = LZMA_VLI_UNKNOWN;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Uncompressed Size
|
|
|
|
if (in[1] & 0x80)
|
2008-12-27 17:27:49 +00:00
|
|
|
return_if_error(lzma_vli_decode(&block->uncompressed_size,
|
2008-06-18 15:02:10 +00:00
|
|
|
NULL, in, &in_pos, in_size));
|
|
|
|
else
|
2008-12-27 17:27:49 +00:00
|
|
|
block->uncompressed_size = LZMA_VLI_UNKNOWN;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
// Filter Flags
|
2019-06-23 18:38:56 +00:00
|
|
|
const size_t filter_count = (in[1] & 3U) + 1;
|
2008-06-18 15:02:10 +00:00
|
|
|
for (size_t i = 0; i < filter_count; ++i) {
|
|
|
|
const lzma_ret ret = lzma_filter_flags_decode(
|
2008-12-27 17:27:49 +00:00
|
|
|
&block->filters[i], allocator,
|
2008-06-18 15:02:10 +00:00
|
|
|
in, &in_pos, in_size);
|
|
|
|
if (ret != LZMA_OK) {
|
2022-11-26 20:21:13 +00:00
|
|
|
lzma_filters_free(block->filters, allocator);
|
2008-06-18 15:02:10 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Padding
|
|
|
|
while (in_pos < in_size) {
|
|
|
|
if (in[in_pos++] != 0x00) {
|
2022-11-26 20:21:13 +00:00
|
|
|
lzma_filters_free(block->filters, allocator);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Possibly some new field present so use
|
2008-09-13 09:10:43 +00:00
|
|
|
// LZMA_OPTIONS_ERROR instead of LZMA_DATA_ERROR.
|
|
|
|
return LZMA_OPTIONS_ERROR;
|
2008-06-18 15:02:10 +00:00
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|