]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_header_decoder.c
096d4620e1d029e0be51ed1a4293eeeace0d6d13
[icculus/xz.git] / src / liblzma / common / block_header_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_header_decoder.c
4 /// \brief      Decodes Block Header from .xz files
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "common.h"
21 #include "check.h"
22
23
24 static void
25 free_properties(lzma_block *block, lzma_allocator *allocator)
26 {
27         // Free allocated filter options. The last array member is not
28         // touched after the initialization in the beginning of
29         // lzma_block_header_decode(), so we don't need to touch that here.
30         for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i) {
31                 lzma_free(block->filters[i].options, allocator);
32                 block->filters[i].id = LZMA_VLI_UNKNOWN;
33                 block->filters[i].options = NULL;
34         }
35
36         return;
37 }
38
39
40 extern LZMA_API(lzma_ret)
41 lzma_block_header_decode(lzma_block *block,
42                 lzma_allocator *allocator, const uint8_t *in)
43 {
44         // NOTE: We consider the header to be corrupt not only when the
45         // CRC32 doesn't match, but also when variable-length integers
46         // are invalid or over 63 bits, or if the header is too small
47         // to contain the claimed information.
48
49         // Initialize the filter options array. This way the caller can
50         // safely free() the options even if an error occurs in this function.
51         for (size_t i = 0; i <= LZMA_FILTERS_MAX; ++i) {
52                 block->filters[i].id = LZMA_VLI_UNKNOWN;
53                 block->filters[i].options = NULL;
54         }
55
56         // Always zero for now.
57         block->version = 0;
58
59         // Validate Block Header Size and Check type. The caller must have
60         // already set these, so it is a programming error if this test fails.
61         if (lzma_block_header_size_decode(in[0]) != block->header_size
62                         || (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
63                 return LZMA_PROG_ERROR;
64
65         // Exclude the CRC32 field.
66         const size_t in_size = block->header_size - 4;
67
68         // Verify CRC32
69         if (lzma_crc32(in, in_size, 0) != integer_read_32(in + in_size))
70                 return LZMA_DATA_ERROR;
71
72         // Check for unsupported flags.
73         if (in[1] & 0x3C)
74                 return LZMA_OPTIONS_ERROR;
75
76         // Start after the Block Header Size and Block Flags fields.
77         size_t in_pos = 2;
78
79         // Compressed Size
80         if (in[1] & 0x40) {
81                 return_if_error(lzma_vli_decode(&block->compressed_size,
82                                 NULL, in, &in_pos, in_size));
83
84                 // Validate Compressed Size. This checks that it isn't zero
85                 // and that the total size of the Block is a valid VLI.
86                 if (lzma_block_unpadded_size(block) == 0)
87                         return LZMA_DATA_ERROR;
88         } else {
89                 block->compressed_size = LZMA_VLI_UNKNOWN;
90         }
91
92         // Uncompressed Size
93         if (in[1] & 0x80)
94                 return_if_error(lzma_vli_decode(&block->uncompressed_size,
95                                 NULL, in, &in_pos, in_size));
96         else
97                 block->uncompressed_size = LZMA_VLI_UNKNOWN;
98
99         // Filter Flags
100         const size_t filter_count = (in[1] & 3) + 1;
101         for (size_t i = 0; i < filter_count; ++i) {
102                 const lzma_ret ret = lzma_filter_flags_decode(
103                                 &block->filters[i], allocator,
104                                 in, &in_pos, in_size);
105                 if (ret != LZMA_OK) {
106                         free_properties(block, allocator);
107                         return ret;
108                 }
109         }
110
111         // Padding
112         while (in_pos < in_size) {
113                 if (in[in_pos++] != 0x00) {
114                         free_properties(block, allocator);
115
116                         // Possibly some new field present so use
117                         // LZMA_OPTIONS_ERROR instead of LZMA_DATA_ERROR.
118                         return LZMA_OPTIONS_ERROR;
119                 }
120         }
121
122         return LZMA_OK;
123 }