1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_encoder.c
4 /// \brief Encodes .lzma 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 "stream_flags_common.h"
22 #include "block_encoder.h"
23 #include "index_encoder.h"
37 lzma_next_coder block_encoder;
39 /// Options for the Block encoder
40 lzma_block block_options;
42 /// Index encoder. This is separate from Block encoder, because this
43 /// doesn't take much memory, and when encoding multiple Streams
44 /// with the same encoding options we avoid reallocating memory.
45 lzma_next_coder index_encoder;
47 /// Index to hold sizes of the Blocks
50 /// Read position in buffer[]
53 /// Total number of bytes in buffer[]
56 /// Buffer to hold Stream Header, Block Header, and Stream Footer.
57 /// Block Header has biggest maximum size.
58 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
63 block_encoder_init(lzma_coder *coder, lzma_allocator *allocator)
65 // Prepare the Block options. Even though Block encoder doesn't need
66 // compressed_size, uncompressed_size, and header_size to be
67 // initialized, it is a good idea to do it here, because this way
68 // we catch if someone gave us Filter ID that cannot be used in
70 coder->block_options.compressed_size = LZMA_VLI_VALUE_UNKNOWN;
71 coder->block_options.uncompressed_size = LZMA_VLI_VALUE_UNKNOWN;
73 return_if_error(lzma_block_header_size(&coder->block_options));
75 // Initialize the actual Block encoder.
76 return lzma_block_encoder_init(&coder->block_encoder, allocator,
77 &coder->block_options);
82 stream_encode(lzma_coder *coder, lzma_allocator *allocator,
83 const uint8_t *restrict in, size_t *restrict in_pos,
84 size_t in_size, uint8_t *restrict out,
85 size_t *restrict out_pos, size_t out_size, lzma_action action)
88 while (*out_pos < out_size)
89 switch (coder->sequence) {
90 case SEQ_STREAM_HEADER:
91 case SEQ_BLOCK_HEADER:
92 case SEQ_STREAM_FOOTER:
93 lzma_bufcpy(coder->buffer, &coder->buffer_pos,
94 coder->buffer_size, out, out_pos, out_size);
95 if (coder->buffer_pos < coder->buffer_size)
98 if (coder->sequence == SEQ_STREAM_FOOTER)
99 return LZMA_STREAM_END;
101 coder->buffer_pos = 0;
105 case SEQ_BLOCK_INIT: {
106 if (*in_pos == in_size) {
107 // If we are requested to flush or finish the current
108 // Block, return LZMA_STREAM_END immediatelly since
109 // there's nothing to do.
110 if (action != LZMA_FINISH)
111 return action == LZMA_RUN
112 ? LZMA_OK : LZMA_STREAM_END;
114 // The application had used LZMA_FULL_FLUSH to finish
115 // the previous Block, but now wants to finish without
116 // encoding new data, or it is simply creating an
117 // empty Stream with no Blocks.
119 // Initialize the Index encoder, and continue to
120 // actually encoding the Index.
121 return_if_error(lzma_index_encoder_init(
122 &coder->index_encoder, allocator,
124 coder->sequence = SEQ_INDEX_ENCODE;
128 // Initialize the Block encoder except if this is the first
129 // Block, because stream_encoder_init() has already
131 if (lzma_index_count(coder->index) != 0)
132 return_if_error(block_encoder_init(coder, allocator));
134 // Encode the Block Header. This shouldn't fail since we have
135 // already initialized the Block encoder.
136 if (lzma_block_header_encode(&coder->block_options,
137 coder->buffer) != LZMA_OK)
138 return LZMA_PROG_ERROR;
140 coder->buffer_size = coder->block_options.header_size;
141 coder->sequence = SEQ_BLOCK_HEADER;
145 case SEQ_BLOCK_ENCODE: {
146 static const lzma_action convert[4] = {
153 const lzma_ret ret = coder->block_encoder.code(
154 coder->block_encoder.coder, allocator,
156 out, out_pos, out_size, convert[action]);
157 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
160 // Add a new Index Record.
161 const lzma_vli total_size = lzma_block_total_size_get(
162 &coder->block_options);
163 assert(total_size != 0);
164 return_if_error(lzma_index_append(coder->index, allocator,
166 coder->block_options.uncompressed_size));
168 coder->sequence = SEQ_BLOCK_INIT;
172 case SEQ_INDEX_ENCODE: {
173 // Call the Index encoder. It doesn't take any input, so
174 // those pointers can be NULL.
175 const lzma_ret ret = coder->index_encoder.code(
176 coder->index_encoder.coder, allocator,
178 out, out_pos, out_size, LZMA_RUN);
179 if (ret != LZMA_STREAM_END)
182 // Encode the Stream Footer into coder->buffer.
183 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.check = check;
242 next->coder->block_options.filters = (lzma_filter *)(filters);
244 // Initialize the Index
245 next->coder->index = lzma_index_init(next->coder->index, allocator);
246 if (next->coder->index == NULL)
247 return LZMA_MEM_ERROR;
249 // Encode the Stream Header
250 lzma_stream_flags stream_flags = {
253 return_if_error(lzma_stream_header_encode(
254 &stream_flags, next->coder->buffer));
256 next->coder->buffer_pos = 0;
257 next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
259 // Initialize the Block encoder. This way we detect if the given
260 // filters are supported by the current liblzma build, and the
261 // application doesn't need to keep the filters structure available
262 // unless it is going to use LZMA_FULL_FLUSH.
263 return block_encoder_init(next->coder, allocator);
267 extern LZMA_API lzma_ret
268 lzma_stream_encoder(lzma_stream *strm,
269 const lzma_filter *filters, lzma_check check)
271 lzma_next_strm_init(lzma_stream_encoder_init, strm, filters, check);
273 strm->internal->supported_actions[LZMA_RUN] = true;
274 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
275 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
276 strm->internal->supported_actions[LZMA_FINISH] = true;