1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_encoder.c
4 /// \brief Encodes .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_encoder.h"
21 #include "block_private.h"
22 #include "raw_encoder.h"
27 /// The filters in the chain; initialized with lzma_raw_decoder_init().
30 /// Encoding options; we also write Total Size, Compressed Size, and
31 /// Uncompressed Size back to this structure when the encoding has
33 lzma_options_block *options;
39 SEQ_UNCOMPRESSED_SIZE,
44 /// Position in .header and .check.
47 /// Check of the uncompressed data
50 /// Total Size calculated while encoding
53 /// Compressed Size calculated while encoding
54 lzma_vli compressed_size;
56 /// Uncompressed Size calculated while encoding
57 lzma_vli uncompressed_size;
59 /// Maximum allowed total_size
62 /// Maximum allowed uncompressed_size
63 lzma_vli uncompressed_limit;
65 /// Backward Size - This is a copy of total_size right before
66 /// the Backward Size field.
67 lzma_vli backward_size;
72 block_encode(lzma_coder *coder, lzma_allocator *allocator,
73 const uint8_t *restrict in, size_t *restrict in_pos,
74 size_t in_size, uint8_t *restrict out,
75 size_t *restrict out_pos, size_t out_size, lzma_action action)
77 // Check that our amount of input stays in proper limits.
78 if (coder->options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN) {
79 if (action == LZMA_FINISH) {
80 if (coder->options->uncompressed_size
81 - coder->uncompressed_size
82 != (lzma_vli)(in_size - *in_pos))
83 return LZMA_DATA_ERROR;
85 if (coder->options->uncompressed_size
86 - coder->uncompressed_size
87 < (lzma_vli)(in_size - *in_pos))
88 return LZMA_DATA_ERROR;
90 } else if (LZMA_VLI_VALUE_MAX - coder->uncompressed_size
91 < (lzma_vli)(in_size - *in_pos)) {
92 return LZMA_DATA_ERROR;
96 while (*out_pos < out_size
97 && (*in_pos < in_size || action != LZMA_RUN))
98 switch (coder->sequence) {
100 const size_t in_start = *in_pos;
101 const size_t out_start = *out_pos;
103 const lzma_ret ret = coder->next.code(coder->next.coder,
104 allocator, in, in_pos, in_size,
105 out, out_pos, out_size, action);
107 const size_t in_used = *in_pos - in_start;
108 const size_t out_used = *out_pos - out_start;
110 if (update_size(&coder->total_size, out_used,
112 || update_size(&coder->compressed_size,
114 coder->options->compressed_size))
115 return LZMA_DATA_ERROR;
117 // No need to check for overflow because we have already
118 // checked it at the beginning of this function.
119 coder->uncompressed_size += in_used;
121 lzma_check_update(&coder->check, coder->options->check,
122 in + in_start, in_used);
124 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
127 assert(*in_pos == in_size);
129 // Compressed and Uncompressed Sizes are now at their final
130 // values. Verify that they match the values give to us.
131 if (!is_size_valid(coder->compressed_size,
132 coder->options->compressed_size)
133 || !is_size_valid(coder->uncompressed_size,
134 coder->options->uncompressed_size))
135 return LZMA_DATA_ERROR;
137 coder->sequence = SEQ_CHECK_FINISH;
141 case SEQ_CHECK_FINISH:
142 if (coder->options->check == LZMA_CHECK_NONE) {
143 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
147 lzma_check_finish(&coder->check, coder->options->check);
148 coder->sequence = SEQ_CHECK_COPY;
153 assert(lzma_check_sizes[coder->options->check] > 0);
155 switch (coder->options->check) {
156 case LZMA_CHECK_CRC32:
157 out[*out_pos] = coder->check.crc32 >> (coder->pos * 8);
160 case LZMA_CHECK_CRC64:
161 out[*out_pos] = coder->check.crc64 >> (coder->pos * 8);
164 case LZMA_CHECK_SHA256:
165 out[*out_pos] = coder->check.sha256.buffer[coder->pos];
170 return LZMA_PROG_ERROR;
175 if (update_size(&coder->total_size, 1, coder->total_limit))
176 return LZMA_DATA_ERROR;
178 if (++coder->pos == lzma_check_sizes[coder->options->check]) {
180 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
185 case SEQ_UNCOMPRESSED_SIZE:
186 if (coder->options->has_uncompressed_size_in_footer) {
187 const size_t out_start = *out_pos;
189 const lzma_ret ret = lzma_vli_encode(
190 coder->uncompressed_size,
192 out, out_pos, out_size);
194 // Updating the size this way instead of doing in a
195 // single chunk using lzma_vli_size(), because this
196 // way we detect when exactly we are going out of
198 if (update_size(&coder->total_size,
199 *out_pos - out_start,
201 return LZMA_DATA_ERROR;
203 if (ret != LZMA_STREAM_END)
209 coder->backward_size = coder->total_size;
210 coder->sequence = SEQ_BACKWARD_SIZE;
213 case SEQ_BACKWARD_SIZE:
214 if (coder->options->has_backward_size) {
215 const size_t out_start = *out_pos;
217 const lzma_ret ret = lzma_vli_encode(
218 coder->backward_size, &coder->pos, 1,
219 out, out_pos, out_size);
221 if (update_size(&coder->total_size,
222 *out_pos - out_start,
224 return LZMA_DATA_ERROR;
226 if (ret != LZMA_STREAM_END)
230 coder->sequence = SEQ_PADDING;
234 if (coder->options->handle_padding) {
235 assert(coder->options->total_size
236 != LZMA_VLI_VALUE_UNKNOWN);
238 if (coder->total_size < coder->options->total_size) {
239 out[*out_pos] = 0x00;
242 if (update_size(&coder->total_size, 1,
244 return LZMA_DATA_ERROR;
250 // Now also Total Size is known. Verify it.
251 if (!is_size_valid(coder->total_size,
252 coder->options->total_size))
253 return LZMA_DATA_ERROR;
255 // Copy the values into coder->options. The caller
256 // may use this information to construct Index.
257 coder->options->total_size = coder->total_size;
258 coder->options->compressed_size = coder->compressed_size;
259 coder->options->uncompressed_size = coder->uncompressed_size;
261 return LZMA_STREAM_END;
264 return LZMA_PROG_ERROR;
272 block_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
274 lzma_next_coder_end(&coder->next, allocator);
275 lzma_free(coder, allocator);
281 block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
282 lzma_options_block *options)
284 // Validate some options.
285 if (validate_options_1(options) || validate_options_2(options)
286 || (options->handle_padding && options->total_size
287 == LZMA_VLI_VALUE_UNKNOWN))
288 return LZMA_PROG_ERROR;
290 // Allocate and initialize *next->coder if needed.
291 if (next->coder == NULL) {
292 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
293 if (next->coder == NULL)
294 return LZMA_MEM_ERROR;
296 next->code = &block_encode;
297 next->end = &block_encoder_end;
298 next->coder->next = LZMA_NEXT_CODER_INIT;
301 // Initialize the check.
302 return_if_error(lzma_check_init(&next->coder->check, options->check));
304 // If End of Payload Marker is not used and Uncompressed Size is zero,
305 // Compressed Data is empty. That is, we don't call the encoder at all.
306 // We initialize it though; it allows detecting invalid options.
307 if (!options->has_eopm && options->uncompressed_size == 0) {
308 // Also Compressed Size must be zero if it has been
310 if (!is_size_valid(0, options->compressed_size))
311 return LZMA_PROG_ERROR;
313 next->coder->sequence = SEQ_CHECK_FINISH;
315 next->coder->sequence = SEQ_CODE;
318 // Other initializations
319 next->coder->options = options;
320 next->coder->pos = 0;
321 next->coder->total_size = options->header_size;
322 next->coder->compressed_size = 0;
323 next->coder->uncompressed_size = 0;
324 next->coder->total_limit
325 = MIN(options->total_size, options->total_limit);
326 next->coder->uncompressed_limit = MIN(options->uncompressed_size,
327 options->uncompressed_limit);
329 // Initialize the requested filters.
330 return lzma_raw_encoder_init(&next->coder->next, allocator,
331 options->filters, options->has_eopm
332 ? LZMA_VLI_VALUE_UNKNOWN
333 : options->uncompressed_size,
339 lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
340 lzma_options_block *options)
342 lzma_next_coder_init(block_encoder_init, next, allocator, options);
346 extern LZMA_API lzma_ret
347 lzma_block_encoder(lzma_stream *strm, lzma_options_block *options)
349 lzma_next_strm_init(strm, block_encoder_init, options);
351 strm->internal->supported_actions[LZMA_RUN] = true;
352 strm->internal->supported_actions[LZMA_FINISH] = true;