1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_header_decoder.c
4 /// \brief Decodes Block Header from .xz files
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
18 free_properties(lzma_block
*block
, 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
;
33 extern LZMA_API(lzma_ret
)
34 lzma_block_header_decode(lzma_block
*block
,
35 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 // Always zero for now.
52 // Validate Block Header Size and Check type. The caller must have
53 // already set these, so it is a programming error if this test fails.
54 if (lzma_block_header_size_decode(in
[0]) != block
->header_size
55 || (unsigned int)(block
->check
) > LZMA_CHECK_ID_MAX
)
56 return LZMA_PROG_ERROR
;
58 // Exclude the CRC32 field.
59 const size_t in_size
= block
->header_size
- 4;
62 if (lzma_crc32(in
, in_size
, 0) != unaligned_read32le(in
+ in_size
))
63 return LZMA_DATA_ERROR
;
65 // Check for unsupported flags.
67 return LZMA_OPTIONS_ERROR
;
69 // Start after the Block Header Size and Block Flags fields.
74 return_if_error(lzma_vli_decode(&block
->compressed_size
,
75 NULL
, in
, &in_pos
, in_size
));
77 // Validate Compressed Size. This checks that it isn't zero
78 // and that the total size of the Block is a valid VLI.
79 if (lzma_block_unpadded_size(block
) == 0)
80 return LZMA_DATA_ERROR
;
82 block
->compressed_size
= LZMA_VLI_UNKNOWN
;
87 return_if_error(lzma_vli_decode(&block
->uncompressed_size
,
88 NULL
, in
, &in_pos
, in_size
));
90 block
->uncompressed_size
= LZMA_VLI_UNKNOWN
;
93 const size_t filter_count
= (in
[1] & 3) + 1;
94 for (size_t i
= 0; i
< filter_count
; ++i
) {
95 const lzma_ret ret
= lzma_filter_flags_decode(
96 &block
->filters
[i
], allocator
,
97 in
, &in_pos
, in_size
);
99 free_properties(block
, allocator
);
105 while (in_pos
< in_size
) {
106 if (in
[in_pos
++] != 0x00) {
107 free_properties(block
, allocator
);
109 // Possibly some new field present so use
110 // LZMA_OPTIONS_ERROR instead of LZMA_DATA_ERROR.
111 return LZMA_OPTIONS_ERROR
;