]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_encoder.c
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / src / liblzma / common / stream_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_encoder.c
4 /// \brief      Encodes .xz Streams
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "stream_encoder.h"
14 #include "block_encoder.h"
15 #include "index_encoder.h"
16
17
18 struct lzma_coder_s {
19         enum {
20                 SEQ_STREAM_HEADER,
21                 SEQ_BLOCK_INIT,
22                 SEQ_BLOCK_HEADER,
23                 SEQ_BLOCK_ENCODE,
24                 SEQ_INDEX_ENCODE,
25                 SEQ_STREAM_FOOTER,
26         } sequence;
27
28         /// Block
29         lzma_next_coder block_encoder;
30
31         /// Options for the Block encoder
32         lzma_block block_options;
33
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;
38
39         /// Index to hold sizes of the Blocks
40         lzma_index *index;
41
42         /// Read position in buffer[]
43         size_t buffer_pos;
44
45         /// Total number of bytes in buffer[]
46         size_t buffer_size;
47
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];
51 };
52
53
54 static lzma_ret
55 block_encoder_init(lzma_coder *coder, lzma_allocator *allocator)
56 {
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
61         // Blocks/Streams.
62         coder->block_options.compressed_size = LZMA_VLI_UNKNOWN;
63         coder->block_options.uncompressed_size = LZMA_VLI_UNKNOWN;
64
65         return_if_error(lzma_block_header_size(&coder->block_options));
66
67         // Initialize the actual Block encoder.
68         return lzma_block_encoder_init(&coder->block_encoder, allocator,
69                         &coder->block_options);
70 }
71
72
73 static lzma_ret
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)
78 {
79         // Main loop
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)
88                         return LZMA_OK;
89
90                 if (coder->sequence == SEQ_STREAM_FOOTER)
91                         return LZMA_STREAM_END;
92
93                 coder->buffer_pos = 0;
94                 ++coder->sequence;
95                 break;
96
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;
105
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.
110                         //
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,
115                                         coder->index));
116                         coder->sequence = SEQ_INDEX_ENCODE;
117                         break;
118                 }
119
120                 // Initialize the Block encoder except if this is the first
121                 // Block, because stream_encoder_init() has already
122                 // initialized it.
123                 if (lzma_index_count(coder->index) != 0)
124                         return_if_error(block_encoder_init(coder, allocator));
125
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;
131
132                 coder->buffer_size = coder->block_options.header_size;
133                 coder->sequence = SEQ_BLOCK_HEADER;
134                 break;
135         }
136
137         case SEQ_BLOCK_ENCODE: {
138                 static const lzma_action convert[4] = {
139                         LZMA_RUN,
140                         LZMA_SYNC_FLUSH,
141                         LZMA_FINISH,
142                         LZMA_FINISH,
143                 };
144
145                 const lzma_ret ret = coder->block_encoder.code(
146                                 coder->block_encoder.coder, allocator,
147                                 in, in_pos, in_size,
148                                 out, out_pos, out_size, convert[action]);
149                 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
150                         return ret;
151
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,
157                                 unpadded_size,
158                                 coder->block_options.uncompressed_size));
159
160                 coder->sequence = SEQ_BLOCK_INIT;
161                 break;
162         }
163
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,
169                                 NULL, NULL, 0,
170                                 out, out_pos, out_size, LZMA_RUN);
171                 if (ret != LZMA_STREAM_END)
172                         return ret;
173
174                 // Encode the Stream Footer into coder->buffer.
175                 const lzma_stream_flags stream_flags = {
176                         .version = 0,
177                         .backward_size = lzma_index_size(coder->index),
178                         .check = coder->block_options.check,
179                 };
180
181                 if (lzma_stream_footer_encode(&stream_flags, coder->buffer)
182                                 != LZMA_OK)
183                         return LZMA_PROG_ERROR;
184
185                 coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
186                 coder->sequence = SEQ_STREAM_FOOTER;
187                 break;
188         }
189
190         default:
191                 assert(0);
192                 return LZMA_PROG_ERROR;
193         }
194
195         return LZMA_OK;
196 }
197
198
199 static void
200 stream_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
201 {
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);
206         return;
207 }
208
209
210 extern lzma_ret
211 lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
212                 const lzma_filter *filters, lzma_check check)
213 {
214         lzma_next_coder_init(lzma_stream_encoder_init, next, allocator);
215
216         if (filters == NULL)
217                 return LZMA_PROG_ERROR;
218
219         if (next->coder == NULL) {
220                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
221                 if (next->coder == NULL)
222                         return LZMA_MEM_ERROR;
223
224                 next->code = &stream_encode;
225                 next->end = &stream_encoder_end;
226
227                 next->coder->block_encoder = LZMA_NEXT_CODER_INIT;
228                 next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
229                 next->coder->index = NULL;
230         }
231
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);
237
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;
242
243         // Encode the Stream Header
244         lzma_stream_flags stream_flags = {
245                 .version = 0,
246                 .check = check,
247         };
248         return_if_error(lzma_stream_header_encode(
249                         &stream_flags, next->coder->buffer));
250
251         next->coder->buffer_pos = 0;
252         next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
253
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);
259 }
260
261
262 extern LZMA_API(lzma_ret)
263 lzma_stream_encoder(lzma_stream *strm,
264                 const lzma_filter *filters, lzma_check check)
265 {
266         lzma_next_strm_init(lzma_stream_encoder_init, strm, filters, check);
267
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;
272
273         return LZMA_OK;
274 }