1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_decoder.c
4 /// \brief Decodes .lzma Blocks
6 // Copyright (C) 2007 Lasse Collin
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "block_decoder.h"
21 #include "block_private.h"
22 #include "filter_decoder.h"
33 /// The filters in the chain; initialized with lzma_raw_decoder_init().
36 /// Decoding options; we also write Compressed Size and Uncompressed
37 /// Size back to this structure when the encoding has been finished.
40 /// Compressed Size calculated while encoding
41 lzma_vli compressed_size;
43 /// Uncompressed Size calculated while encoding
44 lzma_vli uncompressed_size;
46 /// Maximum allowed Compressed Size; this takes into account the
47 /// size of the Block Header and Check fields when Compressed Size
49 lzma_vli compressed_limit;
51 /// Position when reading the Check field
54 /// Check of the uncompressed data
55 lzma_check_state check;
60 block_decode(lzma_coder *coder, lzma_allocator *allocator,
61 const uint8_t *restrict in, size_t *restrict in_pos,
62 size_t in_size, uint8_t *restrict out,
63 size_t *restrict out_pos, size_t out_size, lzma_action action)
65 switch (coder->sequence) {
67 const size_t in_start = *in_pos;
68 const size_t out_start = *out_pos;
70 const lzma_ret ret = coder->next.code(coder->next.coder,
71 allocator, in, in_pos, in_size,
72 out, out_pos, out_size, action);
74 const size_t in_used = *in_pos - in_start;
75 const size_t out_used = *out_pos - out_start;
77 // NOTE: We compare to compressed_limit here, which prevents
78 // the total size of the Block growing past LZMA_VLI_VALUE_MAX.
79 if (update_size(&coder->compressed_size, in_used,
80 coder->compressed_limit)
81 || update_size(&coder->uncompressed_size,
83 coder->options->uncompressed_size))
84 return LZMA_DATA_ERROR;
86 lzma_check_update(&coder->check, coder->options->check,
87 out + out_start, out_used);
89 if (ret != LZMA_STREAM_END)
92 coder->sequence = SEQ_PADDING;
98 // Compressed Data is padded to a multiple of four bytes.
99 while (coder->compressed_size & 3) {
100 if (*in_pos >= in_size)
103 if (in[(*in_pos)++] != 0x00)
104 return LZMA_DATA_ERROR;
106 if (update_size(&coder->compressed_size, 1,
107 coder->compressed_limit))
108 return LZMA_DATA_ERROR;
111 // Compressed and Uncompressed Sizes are now at their final
112 // values. Verify that they match the values given to us.
113 if (!is_size_valid(coder->compressed_size,
114 coder->options->compressed_size)
115 || !is_size_valid(coder->uncompressed_size,
116 coder->options->uncompressed_size))
117 return LZMA_DATA_ERROR;
119 // Copy the values into coder->options. The caller
120 // may use this information to construct Index.
121 coder->options->compressed_size = coder->compressed_size;
122 coder->options->uncompressed_size = coder->uncompressed_size;
124 if (coder->options->check == LZMA_CHECK_NONE)
125 return LZMA_STREAM_END;
127 lzma_check_finish(&coder->check, coder->options->check);
128 coder->sequence = SEQ_CHECK;
133 const bool chksup = lzma_check_is_supported(
134 coder->options->check);
136 while (*in_pos < in_size) {
137 // coder->check.buffer[] may be uninitialized when
138 // the Check ID is not supported.
139 if (chksup && coder->check.buffer.u8[coder->check_pos]
142 return LZMA_DATA_ERROR;
147 if (++coder->check_pos == lzma_check_size(
148 coder->options->check))
149 return LZMA_STREAM_END;
156 return LZMA_PROG_ERROR;
161 block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
163 lzma_next_end(&coder->next, allocator);
164 lzma_free(coder, allocator);
170 lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
173 lzma_next_coder_init(lzma_block_decoder_init, next, allocator);
175 // While lzma_block_total_size_get() is meant to calculate the Total
176 // Size, it also validates the options excluding the filters.
177 if (lzma_block_total_size_get(options) == 0)
178 return LZMA_PROG_ERROR;
180 // options->check is used for array indexing so we need to know that
181 // it is in the valid range.
182 if ((unsigned)(options->check) > LZMA_CHECK_ID_MAX)
183 return LZMA_PROG_ERROR;
185 // Allocate and initialize *next->coder if needed.
186 if (next->coder == NULL) {
187 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
188 if (next->coder == NULL)
189 return LZMA_MEM_ERROR;
191 next->code = &block_decode;
192 next->end = &block_decoder_end;
193 next->coder->next = LZMA_NEXT_CODER_INIT;
196 // Basic initializations
197 next->coder->sequence = SEQ_CODE;
198 next->coder->options = options;
199 next->coder->compressed_size = 0;
200 next->coder->uncompressed_size = 0;
202 // If Compressed Size is not known, we calculate the maximum allowed
203 // value so that Total Size of the Block still is a valid VLI and
204 // a multiple of four.
205 next->coder->compressed_limit
206 = options->compressed_size == LZMA_VLI_VALUE_UNKNOWN
207 ? (LZMA_VLI_VALUE_MAX & ~LZMA_VLI_C(3))
208 - options->header_size
209 - lzma_check_size(options->check)
210 : options->compressed_size;
212 // Initialize the check. It's caller's problem if the Check ID is not
213 // supported, and the Block decoder cannot verify the Check field.
214 // Caller can test lzma_checks[options->check].
215 next->coder->check_pos = 0;
216 lzma_check_init(&next->coder->check, options->check);
218 // Initialize the filter chain.
219 return lzma_raw_decoder_init(&next->coder->next, allocator,
224 extern LZMA_API lzma_ret
225 lzma_block_decoder(lzma_stream *strm, lzma_block *options)
227 lzma_next_strm_init(lzma_block_decoder_init, strm, options);
229 strm->internal->supported_actions[LZMA_RUN] = true;
230 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;