iso9660fs: initialize buffer cache
[minix.git] / external / public-domain / xz / dist / src / liblzma / common / block_header_decoder.c
blob2c9573ee204cae8a720049e8d54310bce9121e37
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, 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 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.
50 block->version = 0;
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;
61 // Verify CRC32
62 if (lzma_crc32(in, in_size, 0) != unaligned_read32le(in + in_size))
63 return LZMA_DATA_ERROR;
65 // Check for unsupported flags.
66 if (in[1] & 0x3C)
67 return LZMA_OPTIONS_ERROR;
69 // Start after the Block Header Size and Block Flags fields.
70 size_t in_pos = 2;
72 // Compressed Size
73 if (in[1] & 0x40) {
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;
81 } else {
82 block->compressed_size = LZMA_VLI_UNKNOWN;
85 // Uncompressed Size
86 if (in[1] & 0x80)
87 return_if_error(lzma_vli_decode(&block->uncompressed_size,
88 NULL, in, &in_pos, in_size));
89 else
90 block->uncompressed_size = LZMA_VLI_UNKNOWN;
92 // Filter Flags
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);
98 if (ret != LZMA_OK) {
99 free_properties(block, allocator);
100 return ret;
104 // Padding
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;
115 return LZMA_OK;