1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_encoder.c
4 /// \brief Encodes .xz 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"
26 /// The filters in the chain; initialized with lzma_raw_decoder_init().
29 /// Encoding options; we also write Unpadded Size, Compressed Size,
30 /// and Uncompressed Size back to this structure when the encoding
31 /// 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 /// Position in the Check field
49 /// Check of the uncompressed data
50 lzma_check_state check;
55 block_encode(lzma_coder *coder, lzma_allocator *allocator,
56 const uint8_t *restrict in, size_t *restrict in_pos,
57 size_t in_size, uint8_t *restrict out,
58 size_t *restrict out_pos, size_t out_size, lzma_action action)
60 // Check that our amount of input stays in proper limits.
61 if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
62 return LZMA_DATA_ERROR;
64 switch (coder->sequence) {
66 const size_t in_start = *in_pos;
67 const size_t out_start = *out_pos;
69 const lzma_ret ret = coder->next.code(coder->next.coder,
70 allocator, in, in_pos, in_size,
71 out, out_pos, out_size, action);
73 const size_t in_used = *in_pos - in_start;
74 const size_t out_used = *out_pos - out_start;
76 if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
77 return LZMA_DATA_ERROR;
79 coder->compressed_size += out_used;
81 // No need to check for overflow because we have already
82 // checked it at the beginning of this function.
83 coder->uncompressed_size += in_used;
85 lzma_check_update(&coder->check, coder->block->check,
86 in + in_start, in_used);
88 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
91 assert(*in_pos == in_size);
92 assert(action == LZMA_FINISH);
94 // Copy the values into coder->block. The caller
95 // may use this information to construct Index.
96 coder->block->compressed_size = coder->compressed_size;
97 coder->block->uncompressed_size = coder->uncompressed_size;
99 coder->sequence = SEQ_PADDING;
105 // Pad Compressed Data to a multiple of four bytes. We can
106 // use coder->compressed_size for this since we don't need
107 // it for anything else anymore.
108 while (coder->compressed_size & 3) {
109 if (*out_pos >= out_size)
112 out[*out_pos] = 0x00;
114 ++coder->compressed_size;
117 if (coder->block->check == LZMA_CHECK_NONE)
118 return LZMA_STREAM_END;
120 lzma_check_finish(&coder->check, coder->block->check);
122 coder->sequence = SEQ_CHECK;
127 const uint32_t check_size
128 = lzma_check_size(coder->block->check);
130 while (*out_pos < out_size) {
131 out[*out_pos] = coder->check.buffer.u8[coder->pos];
134 if (++coder->pos == check_size)
135 return LZMA_STREAM_END;
142 return LZMA_PROG_ERROR;
147 block_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
149 lzma_next_end(&coder->next, allocator);
150 lzma_free(coder, allocator);
156 lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
159 lzma_next_coder_init(lzma_block_encoder_init, next, allocator);
161 if (block->version != 0)
162 return LZMA_OPTIONS_ERROR;
164 // If the Check ID is not supported, we cannot calculate the check and
165 // thus not create a proper Block.
166 if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
167 return LZMA_PROG_ERROR;
169 if (!lzma_check_is_supported(block->check))
170 return LZMA_UNSUPPORTED_CHECK;
172 // Allocate and initialize *next->coder if needed.
173 if (next->coder == NULL) {
174 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
175 if (next->coder == NULL)
176 return LZMA_MEM_ERROR;
178 next->code = &block_encode;
179 next->end = &block_encoder_end;
180 next->coder->next = LZMA_NEXT_CODER_INIT;
183 // Basic initializations
184 next->coder->sequence = SEQ_CODE;
185 next->coder->block = block;
186 next->coder->compressed_size = 0;
187 next->coder->uncompressed_size = 0;
188 next->coder->pos = 0;
190 // Initialize the check
191 lzma_check_init(&next->coder->check, block->check);
193 // Initialize the requested filters.
194 return lzma_raw_encoder_init(&next->coder->next, allocator,
199 extern LZMA_API(lzma_ret)
200 lzma_block_encoder(lzma_stream *strm, lzma_block *block)
202 lzma_next_strm_init(lzma_block_encoder_init, strm, block);
204 strm->internal->supported_actions[LZMA_RUN] = true;
205 strm->internal->supported_actions[LZMA_FINISH] = true;