debian: Prepare for upload
[xz/debian.git] / src / liblzma / common / block_header_decoder.c
blob1dd982f6bd6896e69c8428734ef4915613ed23bf
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file block_header_decoder.c
4 /// \brief Decodes Block Header from .xz files
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "common.h"
14 #include "check.h"
17 static void
18 free_properties(lzma_block *block, const lzma_allocator *allocator)
20 // Free allocated filter options. The last array member is not
21 // touched after the initialization in the beginning of
22 // lzma_block_header_decode(), so we don't need to touch that here.
23 for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i) {
24 lzma_free(block->filters[i].options, allocator);
25 block->filters[i].id = LZMA_VLI_UNKNOWN;
26 block->filters[i].options = NULL;
29 return;
33 extern LZMA_API(lzma_ret)
34 lzma_block_header_decode(lzma_block *block,
35 const lzma_allocator *allocator, const uint8_t *in)
37 // NOTE: We consider the header to be corrupt not only when the
38 // CRC32 doesn't match, but also when variable-length integers
39 // are invalid or over 63 bits, or if the header is too small
40 // to contain the claimed information.
42 // Initialize the filter options array. This way the caller can
43 // safely free() the options even if an error occurs in this function.
44 for (size_t i = 0; i <= LZMA_FILTERS_MAX; ++i) {
45 block->filters[i].id = LZMA_VLI_UNKNOWN;
46 block->filters[i].options = NULL;
49 // Versions 0 and 1 are supported. If a newer version was specified,
50 // we need to downgrade it.
51 if (block->version > 1)
52 block->version = 1;
54 // This isn't a Block Header option, but since the decompressor will
55 // read it if version >= 1, it's better to initialize it here than
56 // to expect the caller to do it since in almost all cases this
57 // should be false.
58 block->ignore_check = false;
60 // Validate Block Header Size and Check type. The caller must have
61 // already set these, so it is a programming error if this test fails.
62 if (lzma_block_header_size_decode(in[0]) != block->header_size
63 || (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
64 return LZMA_PROG_ERROR;
66 // Exclude the CRC32 field.
67 const size_t in_size = block->header_size - 4;
69 // Verify CRC32
70 if (lzma_crc32(in, in_size, 0) != unaligned_read32le(in + in_size))
71 return LZMA_DATA_ERROR;
73 // Check for unsupported flags.
74 if (in[1] & 0x3C)
75 return LZMA_OPTIONS_ERROR;
77 // Start after the Block Header Size and Block Flags fields.
78 size_t in_pos = 2;
80 // Compressed Size
81 if (in[1] & 0x40) {
82 return_if_error(lzma_vli_decode(&block->compressed_size,
83 NULL, in, &in_pos, in_size));
85 // Validate Compressed Size. This checks that it isn't zero
86 // and that the total size of the Block is a valid VLI.
87 if (lzma_block_unpadded_size(block) == 0)
88 return LZMA_DATA_ERROR;
89 } else {
90 block->compressed_size = LZMA_VLI_UNKNOWN;
93 // Uncompressed Size
94 if (in[1] & 0x80)
95 return_if_error(lzma_vli_decode(&block->uncompressed_size,
96 NULL, in, &in_pos, in_size));
97 else
98 block->uncompressed_size = LZMA_VLI_UNKNOWN;
100 // Filter Flags
101 const size_t filter_count = (in[1] & 3) + 1;
102 for (size_t i = 0; i < filter_count; ++i) {
103 const lzma_ret ret = lzma_filter_flags_decode(
104 &block->filters[i], allocator,
105 in, &in_pos, in_size);
106 if (ret != LZMA_OK) {
107 free_properties(block, allocator);
108 return ret;
112 // Padding
113 while (in_pos < in_size) {
114 if (in[in_pos++] != 0x00) {
115 free_properties(block, allocator);
117 // Possibly some new field present so use
118 // LZMA_OPTIONS_ERROR instead of LZMA_DATA_ERROR.
119 return LZMA_OPTIONS_ERROR;
123 return LZMA_OK;