1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_decoder.c
4 /// \brief Decodes .xz Blocks
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 ///////////////////////////////////////////////////////////////////////////////
13 #include "block_decoder.h"
14 #include "filter_decoder.h"
25 /// The filters in the chain; initialized with lzma_raw_decoder_init().
28 /// Decoding options; we also write Compressed Size and Uncompressed
29 /// Size back to this structure when the decoding has been finished.
32 /// Compressed Size calculated while decoding
33 lzma_vli compressed_size
;
35 /// Uncompressed Size calculated while decoding
36 lzma_vli uncompressed_size
;
38 /// Maximum allowed Compressed Size; this takes into account the
39 /// size of the Block Header and Check fields when Compressed Size
41 lzma_vli compressed_limit
;
43 /// Position when reading the Check field
46 /// Check of the uncompressed data
47 lzma_check_state check
;
49 /// True if the integrity check won't be calculated and verified.
55 update_size(lzma_vli
*size
, lzma_vli add
, lzma_vli limit
)
57 if (limit
> LZMA_VLI_MAX
)
60 if (limit
< *size
|| limit
- *size
< add
)
70 is_size_valid(lzma_vli size
, lzma_vli reference
)
72 return reference
== LZMA_VLI_UNKNOWN
|| reference
== size
;
77 block_decode(void *coder_ptr
, const lzma_allocator
*allocator
,
78 const uint8_t *restrict in
, size_t *restrict in_pos
,
79 size_t in_size
, uint8_t *restrict out
,
80 size_t *restrict out_pos
, size_t out_size
, lzma_action action
)
82 lzma_block_coder
*coder
= coder_ptr
;
84 switch (coder
->sequence
) {
86 const size_t in_start
= *in_pos
;
87 const size_t out_start
= *out_pos
;
89 const lzma_ret ret
= coder
->next
.code(coder
->next
.coder
,
90 allocator
, in
, in_pos
, in_size
,
91 out
, out_pos
, out_size
, action
);
93 const size_t in_used
= *in_pos
- in_start
;
94 const size_t out_used
= *out_pos
- out_start
;
96 // NOTE: We compare to compressed_limit here, which prevents
97 // the total size of the Block growing past LZMA_VLI_MAX.
98 if (update_size(&coder
->compressed_size
, in_used
,
99 coder
->compressed_limit
)
100 || update_size(&coder
->uncompressed_size
,
102 coder
->block
->uncompressed_size
))
103 return LZMA_DATA_ERROR
;
105 if (!coder
->ignore_check
)
106 lzma_check_update(&coder
->check
, coder
->block
->check
,
107 out
+ out_start
, out_used
);
109 if (ret
!= LZMA_STREAM_END
)
112 // Compressed and Uncompressed Sizes are now at their final
113 // values. Verify that they match the values given to us.
114 if (!is_size_valid(coder
->compressed_size
,
115 coder
->block
->compressed_size
)
116 || !is_size_valid(coder
->uncompressed_size
,
117 coder
->block
->uncompressed_size
))
118 return LZMA_DATA_ERROR
;
120 // Copy the values into coder->block. The caller
121 // may use this information to construct Index.
122 coder
->block
->compressed_size
= coder
->compressed_size
;
123 coder
->block
->uncompressed_size
= coder
->uncompressed_size
;
125 coder
->sequence
= SEQ_PADDING
;
131 // Compressed Data is padded to a multiple of four bytes.
132 while (coder
->compressed_size
& 3) {
133 if (*in_pos
>= in_size
)
136 // We use compressed_size here just get the Padding
137 // right. The actual Compressed Size was stored to
138 // coder->block already, and won't be modified by
140 ++coder
->compressed_size
;
142 if (in
[(*in_pos
)++] != 0x00)
143 return LZMA_DATA_ERROR
;
146 if (coder
->block
->check
== LZMA_CHECK_NONE
)
147 return LZMA_STREAM_END
;
149 if (!coder
->ignore_check
)
150 lzma_check_finish(&coder
->check
, coder
->block
->check
);
152 coder
->sequence
= SEQ_CHECK
;
157 const size_t check_size
= lzma_check_size(coder
->block
->check
);
158 lzma_bufcpy(in
, in_pos
, in_size
, coder
->block
->raw_check
,
159 &coder
->check_pos
, check_size
);
160 if (coder
->check_pos
< check_size
)
163 // Validate the Check only if we support it.
164 // coder->check.buffer may be uninitialized
165 // when the Check ID is not supported.
166 if (!coder
->ignore_check
167 && lzma_check_is_supported(coder
->block
->check
)
168 && memcmp(coder
->block
->raw_check
,
169 coder
->check
.buffer
.u8
,
171 return LZMA_DATA_ERROR
;
173 return LZMA_STREAM_END
;
177 return LZMA_PROG_ERROR
;
182 block_decoder_end(void *coder_ptr
, const lzma_allocator
*allocator
)
184 lzma_block_coder
*coder
= coder_ptr
;
185 lzma_next_end(&coder
->next
, allocator
);
186 lzma_free(coder
, allocator
);
192 lzma_block_decoder_init(lzma_next_coder
*next
, const lzma_allocator
*allocator
,
195 lzma_next_coder_init(&lzma_block_decoder_init
, next
, allocator
);
197 // Validate the options. lzma_block_unpadded_size() does that for us
198 // except for Uncompressed Size and filters. Filters are validated
199 // by the raw decoder.
200 if (lzma_block_unpadded_size(block
) == 0
201 || !lzma_vli_is_valid(block
->uncompressed_size
))
202 return LZMA_PROG_ERROR
;
204 // Allocate *next->coder if needed.
205 lzma_block_coder
*coder
= next
->coder
;
207 coder
= lzma_alloc(sizeof(lzma_block_coder
), allocator
);
209 return LZMA_MEM_ERROR
;
212 next
->code
= &block_decode
;
213 next
->end
= &block_decoder_end
;
214 coder
->next
= LZMA_NEXT_CODER_INIT
;
217 // Basic initializations
218 coder
->sequence
= SEQ_CODE
;
219 coder
->block
= block
;
220 coder
->compressed_size
= 0;
221 coder
->uncompressed_size
= 0;
223 // If Compressed Size is not known, we calculate the maximum allowed
224 // value so that encoded size of the Block (including Block Padding)
225 // is still a valid VLI and a multiple of four.
226 coder
->compressed_limit
227 = block
->compressed_size
== LZMA_VLI_UNKNOWN
228 ? (LZMA_VLI_MAX
& ~LZMA_VLI_C(3))
230 - lzma_check_size(block
->check
)
231 : block
->compressed_size
;
233 // Initialize the check. It's caller's problem if the Check ID is not
234 // supported, and the Block decoder cannot verify the Check field.
235 // Caller can test lzma_check_is_supported(block->check).
236 coder
->check_pos
= 0;
237 lzma_check_init(&coder
->check
, block
->check
);
239 coder
->ignore_check
= block
->version
>= 1
240 ? block
->ignore_check
: false;
242 // Initialize the filter chain.
243 return lzma_raw_decoder_init(&coder
->next
, allocator
,
248 extern LZMA_API(lzma_ret
)
249 lzma_block_decoder(lzma_stream
*strm
, lzma_block
*block
)
251 lzma_next_strm_init(lzma_block_decoder_init
, strm
, block
);
253 strm
->internal
->supported_actions
[LZMA_RUN
] = true;
254 strm
->internal
->supported_actions
[LZMA_FINISH
] = true;