1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_encoder.c
4 /// \brief Encodes .xz Streams
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 "stream_encoder.h"
14 #include "block_encoder.h"
15 #include "index_encoder.h"
29 lzma_next_coder block_encoder;
31 /// Options for the Block encoder
32 lzma_block block_options;
34 /// Index encoder. This is separate from Block encoder, because this
35 /// doesn't take much memory, and when encoding multiple Streams
36 /// with the same encoding options we avoid reallocating memory.
37 lzma_next_coder index_encoder;
39 /// Index to hold sizes of the Blocks
42 /// Read position in buffer[]
45 /// Total number of bytes in buffer[]
48 /// Buffer to hold Stream Header, Block Header, and Stream Footer.
49 /// Block Header has biggest maximum size.
50 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
55 block_encoder_init(lzma_coder *coder, lzma_allocator *allocator)
57 // Prepare the Block options. Even though Block encoder doesn't need
58 // compressed_size, uncompressed_size, and header_size to be
59 // initialized, it is a good idea to do it here, because this way
60 // we catch if someone gave us Filter ID that cannot be used in
62 coder->block_options.compressed_size = LZMA_VLI_UNKNOWN;
63 coder->block_options.uncompressed_size = LZMA_VLI_UNKNOWN;
65 return_if_error(lzma_block_header_size(&coder->block_options));
67 // Initialize the actual Block encoder.
68 return lzma_block_encoder_init(&coder->block_encoder, allocator,
69 &coder->block_options);
74 stream_encode(lzma_coder *coder, lzma_allocator *allocator,
75 const uint8_t *restrict in, size_t *restrict in_pos,
76 size_t in_size, uint8_t *restrict out,
77 size_t *restrict out_pos, size_t out_size, lzma_action action)
80 while (*out_pos < out_size)
81 switch (coder->sequence) {
82 case SEQ_STREAM_HEADER:
83 case SEQ_BLOCK_HEADER:
84 case SEQ_STREAM_FOOTER:
85 lzma_bufcpy(coder->buffer, &coder->buffer_pos,
86 coder->buffer_size, out, out_pos, out_size);
87 if (coder->buffer_pos < coder->buffer_size)
90 if (coder->sequence == SEQ_STREAM_FOOTER)
91 return LZMA_STREAM_END;
93 coder->buffer_pos = 0;
97 case SEQ_BLOCK_INIT: {
98 if (*in_pos == in_size) {
99 // If we are requested to flush or finish the current
100 // Block, return LZMA_STREAM_END immediatelly since
101 // there's nothing to do.
102 if (action != LZMA_FINISH)
103 return action == LZMA_RUN
104 ? LZMA_OK : LZMA_STREAM_END;
106 // The application had used LZMA_FULL_FLUSH to finish
107 // the previous Block, but now wants to finish without
108 // encoding new data, or it is simply creating an
109 // empty Stream with no Blocks.
111 // Initialize the Index encoder, and continue to
112 // actually encoding the Index.
113 return_if_error(lzma_index_encoder_init(
114 &coder->index_encoder, allocator,
116 coder->sequence = SEQ_INDEX_ENCODE;
120 // Initialize the Block encoder except if this is the first
121 // Block, because stream_encoder_init() has already
123 if (lzma_index_count(coder->index) != 0)
124 return_if_error(block_encoder_init(coder, allocator));
126 // Encode the Block Header. This shouldn't fail since we have
127 // already initialized the Block encoder.
128 if (lzma_block_header_encode(&coder->block_options,
129 coder->buffer) != LZMA_OK)
130 return LZMA_PROG_ERROR;
132 coder->buffer_size = coder->block_options.header_size;
133 coder->sequence = SEQ_BLOCK_HEADER;
137 case SEQ_BLOCK_ENCODE: {
138 static const lzma_action convert[4] = {
145 const lzma_ret ret = coder->block_encoder.code(
146 coder->block_encoder.coder, allocator,
148 out, out_pos, out_size, convert[action]);
149 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
152 // Add a new Index Record.
153 const lzma_vli unpadded_size = lzma_block_unpadded_size(
154 &coder->block_options);
155 assert(unpadded_size != 0);
156 return_if_error(lzma_index_append(coder->index, allocator,
158 coder->block_options.uncompressed_size));
160 coder->sequence = SEQ_BLOCK_INIT;
164 case SEQ_INDEX_ENCODE: {
165 // Call the Index encoder. It doesn't take any input, so
166 // those pointers can be NULL.
167 const lzma_ret ret = coder->index_encoder.code(
168 coder->index_encoder.coder, allocator,
170 out, out_pos, out_size, LZMA_RUN);
171 if (ret != LZMA_STREAM_END)
174 // Encode the Stream Footer into coder->buffer.
175 const lzma_stream_flags stream_flags = {
177 .backward_size = lzma_index_size(coder->index),
178 .check = coder->block_options.check,
181 if (lzma_stream_footer_encode(&stream_flags, coder->buffer)
183 return LZMA_PROG_ERROR;
185 coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
186 coder->sequence = SEQ_STREAM_FOOTER;
192 return LZMA_PROG_ERROR;
200 stream_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
202 lzma_next_end(&coder->block_encoder, allocator);
203 lzma_next_end(&coder->index_encoder, allocator);
204 lzma_index_end(coder->index, allocator);
205 lzma_free(coder, allocator);
211 lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
212 const lzma_filter *filters, lzma_check check)
214 lzma_next_coder_init(lzma_stream_encoder_init, next, allocator);
217 return LZMA_PROG_ERROR;
219 if (next->coder == NULL) {
220 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
221 if (next->coder == NULL)
222 return LZMA_MEM_ERROR;
224 next->code = &stream_encode;
225 next->end = &stream_encoder_end;
227 next->coder->block_encoder = LZMA_NEXT_CODER_INIT;
228 next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
229 next->coder->index = NULL;
232 // Basic initializations
233 next->coder->sequence = SEQ_STREAM_HEADER;
234 next->coder->block_options.version = 0;
235 next->coder->block_options.check = check;
236 next->coder->block_options.filters = (lzma_filter *)(filters);
238 // Initialize the Index
239 next->coder->index = lzma_index_init(next->coder->index, allocator);
240 if (next->coder->index == NULL)
241 return LZMA_MEM_ERROR;
243 // Encode the Stream Header
244 lzma_stream_flags stream_flags = {
248 return_if_error(lzma_stream_header_encode(
249 &stream_flags, next->coder->buffer));
251 next->coder->buffer_pos = 0;
252 next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
254 // Initialize the Block encoder. This way we detect if the given
255 // filters are supported by the current liblzma build, and the
256 // application doesn't need to keep the filters structure available
257 // unless it is going to use LZMA_FULL_FLUSH.
258 return block_encoder_init(next->coder, allocator);
262 extern LZMA_API(lzma_ret)
263 lzma_stream_encoder(lzma_stream *strm,
264 const lzma_filter *filters, lzma_check check)
266 lzma_next_strm_init(lzma_stream_encoder_init, strm, filters, check);
268 strm->internal->supported_actions[LZMA_RUN] = true;
269 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
270 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
271 strm->internal->supported_actions[LZMA_FINISH] = true;