]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_header_decoder.c
Renamed constants:
[icculus/xz.git] / src / liblzma / common / block_header_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_header_decoder.c
4 /// \brief      Decodes Block Header from .lzma 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 *options, 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_BLOCK_FILTERS_MAX; ++i) {
31                 lzma_free(options->filters[i].options, allocator);
32                 options->filters[i].id = LZMA_VLI_UNKNOWN;
33                 options->filters[i].options = NULL;
34         }
35
36         return;
37 }
38
39
40 extern LZMA_API lzma_ret
41 lzma_block_header_decode(lzma_block *options,
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_BLOCK_FILTERS_MAX; ++i) {
52                 options->filters[i].id = LZMA_VLI_UNKNOWN;
53                 options->filters[i].options = NULL;
54         }
55
56         size_t in_size = options->header_size;
57
58         // Validate. The caller must have set options->header_size with
59         // lzma_block_header_size_decode() macro, so it is a programming error
60         // if these tests fail.
61         if (in_size < LZMA_BLOCK_HEADER_SIZE_MIN
62                         || in_size > LZMA_BLOCK_HEADER_SIZE_MAX
63                         || (in_size & 3)
64                         || lzma_block_header_size_decode(in[0]) != in_size)
65                 return LZMA_PROG_ERROR;
66
67         // Exclude the CRC32 field.
68         in_size -= 4;
69
70         // Verify CRC32
71         if (lzma_crc32(in, in_size, 0) != integer_read_32(in + in_size))
72                 return LZMA_DATA_ERROR;
73
74         // Check for unsupported flags.
75         if (in[1] & 0x3C)
76                 return LZMA_OPTIONS_ERROR;
77
78         // Start after the Block Header Size and Block Flags fields.
79         size_t in_pos = 2;
80
81         // Compressed Size
82         if (in[1] & 0x40) {
83                 return_if_error(lzma_vli_decode(&options->compressed_size,
84                                 NULL, in, &in_pos, in_size));
85
86                 if (options->compressed_size > LZMA_VLI_MAX / 4 - 1)
87                         return LZMA_DATA_ERROR;
88
89                 options->compressed_size = (options->compressed_size + 1) * 4;
90
91                 // Check that Total Size (that is, size of
92                 // Block Header + Compressed Data + Check) is
93                 // representable as a VLI.
94                 if (lzma_block_total_size_get(options) == 0)
95                         return LZMA_DATA_ERROR;
96         } else {
97                 options->compressed_size = LZMA_VLI_UNKNOWN;
98         }
99
100         // Uncompressed Size
101         if (in[1] & 0x80)
102                 return_if_error(lzma_vli_decode(&options->uncompressed_size,
103                                 NULL, in, &in_pos, in_size));
104         else
105                 options->uncompressed_size = LZMA_VLI_UNKNOWN;
106
107         // Filter Flags
108         const size_t filter_count = (in[1] & 3) + 1;
109         for (size_t i = 0; i < filter_count; ++i) {
110                 const lzma_ret ret = lzma_filter_flags_decode(
111                                 &options->filters[i], allocator,
112                                 in, &in_pos, in_size);
113                 if (ret != LZMA_OK) {
114                         free_properties(options, allocator);
115                         return ret;
116                 }
117         }
118
119         // Padding
120         while (in_pos < in_size) {
121                 if (in[in_pos++] != 0x00) {
122                         free_properties(options, allocator);
123
124                         // Possibly some new field present so use
125                         // LZMA_OPTIONS_ERROR instead of LZMA_DATA_ERROR.
126                         return LZMA_OPTIONS_ERROR;
127                 }
128         }
129
130         return LZMA_OK;
131 }