1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_encoder.c
4 /// \brief Encodes .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_encoder.h"
14 #include "filter_encoder.h"
19 /// The filters in the chain; initialized with lzma_raw_decoder_init().
22 /// Encoding options; we also write Unpadded Size, Compressed Size,
23 /// and Uncompressed Size back to this structure when the encoding
24 /// has been finished.
33 /// Compressed Size calculated while encoding
34 lzma_vli compressed_size;
36 /// Uncompressed Size calculated while encoding
37 lzma_vli uncompressed_size;
39 /// Position in the Check field
42 /// Check of the uncompressed data
43 lzma_check_state check;
48 block_encode(lzma_coder *coder, lzma_allocator *allocator,
49 const uint8_t *restrict in, size_t *restrict in_pos,
50 size_t in_size, uint8_t *restrict out,
51 size_t *restrict out_pos, size_t out_size, lzma_action action)
53 // Check that our amount of input stays in proper limits.
54 if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
55 return LZMA_DATA_ERROR;
57 switch (coder->sequence) {
59 const size_t in_start = *in_pos;
60 const size_t out_start = *out_pos;
62 const lzma_ret ret = coder->next.code(coder->next.coder,
63 allocator, in, in_pos, in_size,
64 out, out_pos, out_size, action);
66 const size_t in_used = *in_pos - in_start;
67 const size_t out_used = *out_pos - out_start;
69 if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
70 return LZMA_DATA_ERROR;
72 coder->compressed_size += out_used;
74 // No need to check for overflow because we have already
75 // checked it at the beginning of this function.
76 coder->uncompressed_size += in_used;
78 lzma_check_update(&coder->check, coder->block->check,
79 in + in_start, in_used);
81 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
84 assert(*in_pos == in_size);
85 assert(action == LZMA_FINISH);
87 // Copy the values into coder->block. The caller
88 // may use this information to construct Index.
89 coder->block->compressed_size = coder->compressed_size;
90 coder->block->uncompressed_size = coder->uncompressed_size;
92 coder->sequence = SEQ_PADDING;
98 // Pad Compressed Data to a multiple of four bytes. We can
99 // use coder->compressed_size for this since we don't need
100 // it for anything else anymore.
101 while (coder->compressed_size & 3) {
102 if (*out_pos >= out_size)
105 out[*out_pos] = 0x00;
107 ++coder->compressed_size;
110 if (coder->block->check == LZMA_CHECK_NONE)
111 return LZMA_STREAM_END;
113 lzma_check_finish(&coder->check, coder->block->check);
115 coder->sequence = SEQ_CHECK;
120 const uint32_t check_size
121 = lzma_check_size(coder->block->check);
123 while (*out_pos < out_size) {
124 out[*out_pos] = coder->check.buffer.u8[coder->pos];
127 if (++coder->pos == check_size)
128 return LZMA_STREAM_END;
135 return LZMA_PROG_ERROR;
140 block_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
142 lzma_next_end(&coder->next, allocator);
143 lzma_free(coder, allocator);
149 lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
152 lzma_next_coder_init(lzma_block_encoder_init, next, allocator);
154 if (block->version != 0)
155 return LZMA_OPTIONS_ERROR;
157 // If the Check ID is not supported, we cannot calculate the check and
158 // thus not create a proper Block.
159 if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
160 return LZMA_PROG_ERROR;
162 if (!lzma_check_is_supported(block->check))
163 return LZMA_UNSUPPORTED_CHECK;
165 // Allocate and initialize *next->coder if needed.
166 if (next->coder == NULL) {
167 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
168 if (next->coder == NULL)
169 return LZMA_MEM_ERROR;
171 next->code = &block_encode;
172 next->end = &block_encoder_end;
173 next->coder->next = LZMA_NEXT_CODER_INIT;
176 // Basic initializations
177 next->coder->sequence = SEQ_CODE;
178 next->coder->block = block;
179 next->coder->compressed_size = 0;
180 next->coder->uncompressed_size = 0;
181 next->coder->pos = 0;
183 // Initialize the check
184 lzma_check_init(&next->coder->check, block->check);
186 // Initialize the requested filters.
187 return lzma_raw_encoder_init(&next->coder->next, allocator,
192 extern LZMA_API(lzma_ret)
193 lzma_block_encoder(lzma_stream *strm, lzma_block *block)
195 lzma_next_strm_init(lzma_block_encoder_init, strm, block);
197 strm->internal->supported_actions[LZMA_RUN] = true;
198 strm->internal->supported_actions[LZMA_FINISH] = true;