]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_encoder.c
89fc2f9dc7a8857760f905f4a5d13c67939f698c
[icculus/xz.git] / src / liblzma / common / stream_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_encoder.c
4 /// \brief      Encodes .xz Streams
5 //
6 //  Copyright (C) 2007-2008 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "stream_encoder.h"
21 #include "block_encoder.h"
22 #include "index_encoder.h"
23
24
25 struct lzma_coder_s {
26         enum {
27                 SEQ_STREAM_HEADER,
28                 SEQ_BLOCK_INIT,
29                 SEQ_BLOCK_HEADER,
30                 SEQ_BLOCK_ENCODE,
31                 SEQ_INDEX_ENCODE,
32                 SEQ_STREAM_FOOTER,
33         } sequence;
34
35         /// Block
36         lzma_next_coder block_encoder;
37
38         /// Options for the Block encoder
39         lzma_block block_options;
40
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;
45
46         /// Index to hold sizes of the Blocks
47         lzma_index *index;
48
49         /// Read position in buffer[]
50         size_t buffer_pos;
51
52         /// Total number of bytes in buffer[]
53         size_t buffer_size;
54
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];
58 };
59
60
61 static lzma_ret
62 block_encoder_init(lzma_coder *coder, lzma_allocator *allocator)
63 {
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
68         // Blocks/Streams.
69         coder->block_options.compressed_size = LZMA_VLI_UNKNOWN;
70         coder->block_options.uncompressed_size = LZMA_VLI_UNKNOWN;
71
72         return_if_error(lzma_block_header_size(&coder->block_options));
73
74         // Initialize the actual Block encoder.
75         return lzma_block_encoder_init(&coder->block_encoder, allocator,
76                         &coder->block_options);
77 }
78
79
80 static lzma_ret
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)
85 {
86         // Main loop
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)
95                         return LZMA_OK;
96
97                 if (coder->sequence == SEQ_STREAM_FOOTER)
98                         return LZMA_STREAM_END;
99
100                 coder->buffer_pos = 0;
101                 ++coder->sequence;
102                 break;
103
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;
112
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.
117                         //
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,
122                                         coder->index));
123                         coder->sequence = SEQ_INDEX_ENCODE;
124                         break;
125                 }
126
127                 // Initialize the Block encoder except if this is the first
128                 // Block, because stream_encoder_init() has already
129                 // initialized it.
130                 if (lzma_index_count(coder->index) != 0)
131                         return_if_error(block_encoder_init(coder, allocator));
132
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;
138
139                 coder->buffer_size = coder->block_options.header_size;
140                 coder->sequence = SEQ_BLOCK_HEADER;
141                 break;
142         }
143
144         case SEQ_BLOCK_ENCODE: {
145                 static const lzma_action convert[4] = {
146                         LZMA_RUN,
147                         LZMA_SYNC_FLUSH,
148                         LZMA_FINISH,
149                         LZMA_FINISH,
150                 };
151
152                 const lzma_ret ret = coder->block_encoder.code(
153                                 coder->block_encoder.coder, allocator,
154                                 in, in_pos, in_size,
155                                 out, out_pos, out_size, convert[action]);
156                 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
157                         return ret;
158
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,
164                                 unpadded_size,
165                                 coder->block_options.uncompressed_size));
166
167                 coder->sequence = SEQ_BLOCK_INIT;
168                 break;
169         }
170
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,
176                                 NULL, NULL, 0,
177                                 out, out_pos, out_size, LZMA_RUN);
178                 if (ret != LZMA_STREAM_END)
179                         return ret;
180
181                 // Encode the Stream Footer into coder->buffer.
182                 const lzma_stream_flags stream_flags = {
183                         .version = 0,
184                         .backward_size = lzma_index_size(coder->index),
185                         .check = coder->block_options.check,
186                 };
187
188                 if (lzma_stream_footer_encode(&stream_flags, coder->buffer)
189                                 != LZMA_OK)
190                         return LZMA_PROG_ERROR;
191
192                 coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
193                 coder->sequence = SEQ_STREAM_FOOTER;
194                 break;
195         }
196
197         default:
198                 assert(0);
199                 return LZMA_PROG_ERROR;
200         }
201
202         return LZMA_OK;
203 }
204
205
206 static void
207 stream_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
208 {
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);
213         return;
214 }
215
216
217 extern lzma_ret
218 lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
219                 const lzma_filter *filters, lzma_check check)
220 {
221         lzma_next_coder_init(lzma_stream_encoder_init, next, allocator);
222
223         if (filters == NULL)
224                 return LZMA_PROG_ERROR;
225
226         if (next->coder == NULL) {
227                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
228                 if (next->coder == NULL)
229                         return LZMA_MEM_ERROR;
230
231                 next->code = &stream_encode;
232                 next->end = &stream_encoder_end;
233
234                 next->coder->block_encoder = LZMA_NEXT_CODER_INIT;
235                 next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
236                 next->coder->index = NULL;
237         }
238
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);
244
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;
249
250         // Encode the Stream Header
251         lzma_stream_flags stream_flags = {
252                 .version = 0,
253                 .check = check,
254         };
255         return_if_error(lzma_stream_header_encode(
256                         &stream_flags, next->coder->buffer));
257
258         next->coder->buffer_pos = 0;
259         next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
260
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);
266 }
267
268
269 extern LZMA_API(lzma_ret)
270 lzma_stream_encoder(lzma_stream *strm,
271                 const lzma_filter *filters, lzma_check check)
272 {
273         lzma_next_strm_init(lzma_stream_encoder_init, strm, filters, check);
274
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;
279
280         return LZMA_OK;
281 }