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 "filter_encoder.h"
25 /// The maximum size of a single Block is limited by the maximum size of
26 /// a Stream, which is 2^63 - 1 bytes (i.e. LZMA_VLI_MAX). We could
27 /// take into account the headers etc. to determine the exact maximum size
28 /// of the Compressed Data field, but the complexity would give us nothing
29 /// useful. Instead, limit the size of Compressed Data so that even with
30 /// biggest possible Block Header and Check fields the total encoded size of
31 /// the Block stays as valid VLI. This way we don't produce incorrect output
32 /// if someone will really try creating a Block of 8 EiB.
34 /// ~LZMA_VLI_C(3) is to guarantee that if we need padding at the end of
35 /// the Compressed Data field, it will still stay in the proper limit.
36 #define COMPRESSED_SIZE_MAX ((LZMA_VLI_MAX - LZMA_BLOCK_HEADER_SIZE_MAX \
37 - LZMA_CHECK_SIZE_MAX) & ~LZMA_VLI_C(3))
41 /// The filters in the chain; initialized with lzma_raw_decoder_init().
44 /// Encoding options; we also write Unpadded Size, Compressed Size,
45 /// and Uncompressed Size back to this structure when the encoding
46 /// has been finished.
55 /// Compressed Size calculated while encoding
56 lzma_vli compressed_size;
58 /// Uncompressed Size calculated while encoding
59 lzma_vli uncompressed_size;
61 /// Position in Block Padding and the Check fields
64 /// Check of the uncompressed data
65 lzma_check_state check;
70 block_encode(lzma_coder *coder, lzma_allocator *allocator,
71 const uint8_t *restrict in, size_t *restrict in_pos,
72 size_t in_size, uint8_t *restrict out,
73 size_t *restrict out_pos, size_t out_size, lzma_action action)
75 // Check that our amount of input stays in proper limits.
76 if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
77 return LZMA_PROG_ERROR;
79 switch (coder->sequence) {
81 const size_t in_start = *in_pos;
82 const size_t out_start = *out_pos;
84 const lzma_ret ret = coder->next.code(coder->next.coder,
85 allocator, in, in_pos, in_size,
86 out, out_pos, out_size, action);
88 const size_t in_used = *in_pos - in_start;
89 const size_t out_used = *out_pos - out_start;
91 if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
92 return LZMA_DATA_ERROR;
94 coder->compressed_size += out_used;
96 // No need to check for overflow because we have already
97 // checked it at the beginning of this function.
98 coder->uncompressed_size += in_used;
100 lzma_check_update(&coder->check, coder->options->check,
101 in + in_start, in_used);
103 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
106 assert(*in_pos == in_size);
107 assert(action == LZMA_FINISH);
109 // Copy the values into coder->options. The caller
110 // may use this information to construct Index.
111 coder->options->compressed_size = coder->compressed_size;
112 coder->options->uncompressed_size = coder->uncompressed_size;
114 coder->sequence = SEQ_PADDING;
120 // Pad Compressed Data to a multiple of four bytes.
121 while ((coder->compressed_size + coder->pos) & 3) {
122 if (*out_pos >= out_size)
125 out[*out_pos] = 0x00;
130 if (coder->options->check == LZMA_CHECK_NONE)
131 return LZMA_STREAM_END;
133 lzma_check_finish(&coder->check, coder->options->check);
136 coder->sequence = SEQ_CHECK;
141 const uint32_t check_size
142 = lzma_check_size(coder->options->check);
144 while (*out_pos < out_size) {
145 out[*out_pos] = coder->check.buffer.u8[coder->pos];
148 if (++coder->pos == check_size)
149 return LZMA_STREAM_END;
156 return LZMA_PROG_ERROR;
161 block_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
163 lzma_next_end(&coder->next, allocator);
164 lzma_free(coder, allocator);
170 lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
173 lzma_next_coder_init(lzma_block_encoder_init, next, allocator);
175 // If the Check ID is not supported, we cannot calculate the check and
176 // thus not create a proper Block.
177 if ((unsigned)(options->check) > LZMA_CHECK_ID_MAX)
178 return LZMA_PROG_ERROR;
180 if (!lzma_check_is_supported(options->check))
181 return LZMA_UNSUPPORTED_CHECK;
183 // Allocate and initialize *next->coder if needed.
184 if (next->coder == NULL) {
185 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
186 if (next->coder == NULL)
187 return LZMA_MEM_ERROR;
189 next->code = &block_encode;
190 next->end = &block_encoder_end;
191 next->coder->next = LZMA_NEXT_CODER_INIT;
194 // Basic initializations
195 next->coder->sequence = SEQ_CODE;
196 next->coder->options = options;
197 next->coder->compressed_size = 0;
198 next->coder->uncompressed_size = 0;
199 next->coder->pos = 0;
201 // Initialize the check
202 lzma_check_init(&next->coder->check, options->check);
204 // Initialize the requested filters.
205 return lzma_raw_encoder_init(&next->coder->next, allocator,
210 extern LZMA_API lzma_ret
211 lzma_block_encoder(lzma_stream *strm, lzma_block *options)
213 lzma_next_strm_init(lzma_block_encoder_init, strm, options);
215 strm->internal->supported_actions[LZMA_RUN] = true;
216 strm->internal->supported_actions[LZMA_FINISH] = true;