1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_encoder.c
4 /// \brief Encodes .xz Streams
6 // Copyright (C) 2007-2008 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 "stream_encoder.h"
21 #include "block_encoder.h"
22 #include "index_encoder.h"
36 lzma_next_coder block_encoder;
38 /// Options for the Block encoder
39 lzma_block block_options;
41 /// Index encoder. This is separate from Block encoder, because this
42 /// doesn't take much memory, and when encoding multiple Streams
43 /// with the same encoding options we avoid reallocating memory.
44 lzma_next_coder index_encoder;
46 /// Index to hold sizes of the Blocks
49 /// Read position in buffer[]
52 /// Total number of bytes in buffer[]
55 /// Buffer to hold Stream Header, Block Header, and Stream Footer.
56 /// Block Header has biggest maximum size.
57 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
62 block_encoder_init(lzma_coder *coder, lzma_allocator *allocator)
64 // Prepare the Block options. Even though Block encoder doesn't need
65 // compressed_size, uncompressed_size, and header_size to be
66 // initialized, it is a good idea to do it here, because this way
67 // we catch if someone gave us Filter ID that cannot be used in
69 coder->block_options.compressed_size = LZMA_VLI_UNKNOWN;
70 coder->block_options.uncompressed_size = LZMA_VLI_UNKNOWN;
72 return_if_error(lzma_block_header_size(&coder->block_options));
74 // Initialize the actual Block encoder.
75 return lzma_block_encoder_init(&coder->block_encoder, allocator,
76 &coder->block_options);
81 stream_encode(lzma_coder *coder, lzma_allocator *allocator,
82 const uint8_t *restrict in, size_t *restrict in_pos,
83 size_t in_size, uint8_t *restrict out,
84 size_t *restrict out_pos, size_t out_size, lzma_action action)
87 while (*out_pos < out_size)
88 switch (coder->sequence) {
89 case SEQ_STREAM_HEADER:
90 case SEQ_BLOCK_HEADER:
91 case SEQ_STREAM_FOOTER:
92 lzma_bufcpy(coder->buffer, &coder->buffer_pos,
93 coder->buffer_size, out, out_pos, out_size);
94 if (coder->buffer_pos < coder->buffer_size)
97 if (coder->sequence == SEQ_STREAM_FOOTER)
98 return LZMA_STREAM_END;
100 coder->buffer_pos = 0;
104 case SEQ_BLOCK_INIT: {
105 if (*in_pos == in_size) {
106 // If we are requested to flush or finish the current
107 // Block, return LZMA_STREAM_END immediatelly since
108 // there's nothing to do.
109 if (action != LZMA_FINISH)
110 return action == LZMA_RUN
111 ? LZMA_OK : LZMA_STREAM_END;
113 // The application had used LZMA_FULL_FLUSH to finish
114 // the previous Block, but now wants to finish without
115 // encoding new data, or it is simply creating an
116 // empty Stream with no Blocks.
118 // Initialize the Index encoder, and continue to
119 // actually encoding the Index.
120 return_if_error(lzma_index_encoder_init(
121 &coder->index_encoder, allocator,
123 coder->sequence = SEQ_INDEX_ENCODE;
127 // Initialize the Block encoder except if this is the first
128 // Block, because stream_encoder_init() has already
130 if (lzma_index_count(coder->index) != 0)
131 return_if_error(block_encoder_init(coder, allocator));
133 // Encode the Block Header. This shouldn't fail since we have
134 // already initialized the Block encoder.
135 if (lzma_block_header_encode(&coder->block_options,
136 coder->buffer) != LZMA_OK)
137 return LZMA_PROG_ERROR;
139 coder->buffer_size = coder->block_options.header_size;
140 coder->sequence = SEQ_BLOCK_HEADER;
144 case SEQ_BLOCK_ENCODE: {
145 static const lzma_action convert[4] = {
152 const lzma_ret ret = coder->block_encoder.code(
153 coder->block_encoder.coder, allocator,
155 out, out_pos, out_size, convert[action]);
156 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
159 // Add a new Index Record.
160 const lzma_vli unpadded_size = lzma_block_unpadded_size(
161 &coder->block_options);
162 assert(unpadded_size != 0);
163 return_if_error(lzma_index_append(coder->index, allocator,
165 coder->block_options.uncompressed_size));
167 coder->sequence = SEQ_BLOCK_INIT;
171 case SEQ_INDEX_ENCODE: {
172 // Call the Index encoder. It doesn't take any input, so
173 // those pointers can be NULL.
174 const lzma_ret ret = coder->index_encoder.code(
175 coder->index_encoder.coder, allocator,
177 out, out_pos, out_size, LZMA_RUN);
178 if (ret != LZMA_STREAM_END)
181 // Encode the Stream Footer into coder->buffer.
182 const lzma_stream_flags stream_flags = {
184 .backward_size = lzma_index_size(coder->index),
185 .check = coder->block_options.check,
188 if (lzma_stream_footer_encode(&stream_flags, coder->buffer)
190 return LZMA_PROG_ERROR;
192 coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
193 coder->sequence = SEQ_STREAM_FOOTER;
199 return LZMA_PROG_ERROR;
207 stream_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
209 lzma_next_end(&coder->block_encoder, allocator);
210 lzma_next_end(&coder->index_encoder, allocator);
211 lzma_index_end(coder->index, allocator);
212 lzma_free(coder, allocator);
218 lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
219 const lzma_filter *filters, lzma_check check)
221 lzma_next_coder_init(lzma_stream_encoder_init, next, allocator);
224 return LZMA_PROG_ERROR;
226 if (next->coder == NULL) {
227 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
228 if (next->coder == NULL)
229 return LZMA_MEM_ERROR;
231 next->code = &stream_encode;
232 next->end = &stream_encoder_end;
234 next->coder->block_encoder = LZMA_NEXT_CODER_INIT;
235 next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
236 next->coder->index = NULL;
239 // Basic initializations
240 next->coder->sequence = SEQ_STREAM_HEADER;
241 next->coder->block_options.version = 0;
242 next->coder->block_options.check = check;
243 next->coder->block_options.filters = (lzma_filter *)(filters);
245 // Initialize the Index
246 next->coder->index = lzma_index_init(next->coder->index, allocator);
247 if (next->coder->index == NULL)
248 return LZMA_MEM_ERROR;
250 // Encode the Stream Header
251 lzma_stream_flags stream_flags = {
255 return_if_error(lzma_stream_header_encode(
256 &stream_flags, next->coder->buffer));
258 next->coder->buffer_pos = 0;
259 next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
261 // Initialize the Block encoder. This way we detect if the given
262 // filters are supported by the current liblzma build, and the
263 // application doesn't need to keep the filters structure available
264 // unless it is going to use LZMA_FULL_FLUSH.
265 return block_encoder_init(next->coder, allocator);
269 extern LZMA_API(lzma_ret)
270 lzma_stream_encoder(lzma_stream *strm,
271 const lzma_filter *filters, lzma_check check)
273 lzma_next_strm_init(lzma_stream_encoder_init, strm, filters, check);
275 strm->internal->supported_actions[LZMA_RUN] = true;
276 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
277 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
278 strm->internal->supported_actions[LZMA_FINISH] = true;