stream_encoder.c (292588) | stream_encoder.c (312518) |
---|---|
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 "block_encoder.h" 14#include "index_encoder.h" 15 16 | 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 "block_encoder.h" 14#include "index_encoder.h" 15 16 |
17struct lzma_coder_s { | 17typedef struct { |
18 enum { 19 SEQ_STREAM_HEADER, 20 SEQ_BLOCK_INIT, 21 SEQ_BLOCK_HEADER, 22 SEQ_BLOCK_ENCODE, 23 SEQ_INDEX_ENCODE, 24 SEQ_STREAM_FOOTER, 25 } sequence; --- 24 unchanged lines hidden (view full) --- 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]; | 18 enum { 19 SEQ_STREAM_HEADER, 20 SEQ_BLOCK_INIT, 21 SEQ_BLOCK_HEADER, 22 SEQ_BLOCK_ENCODE, 23 SEQ_INDEX_ENCODE, 24 SEQ_STREAM_FOOTER, 25 } sequence; --- 24 unchanged lines hidden (view full) --- 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}; | 58} lzma_stream_coder; |
59 60 61static lzma_ret | 59 60 61static lzma_ret |
62block_encoder_init(lzma_coder *coder, const lzma_allocator *allocator) | 62block_encoder_init(lzma_stream_coder *coder, const 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 80static lzma_ret | 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 80static lzma_ret |
81stream_encode(lzma_coder *coder, const lzma_allocator *allocator, | 81stream_encode(void *coder_ptr, const 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{ | 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 lzma_stream_coder *coder = coder_ptr; 87 |
|
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); --- 110 unchanged lines hidden (view full) --- 204 return LZMA_PROG_ERROR; 205 } 206 207 return LZMA_OK; 208} 209 210 211static void | 88 // Main loop 89 while (*out_pos < out_size) 90 switch (coder->sequence) { 91 case SEQ_STREAM_HEADER: 92 case SEQ_BLOCK_HEADER: 93 case SEQ_STREAM_FOOTER: 94 lzma_bufcpy(coder->buffer, &coder->buffer_pos, 95 coder->buffer_size, out, out_pos, out_size); --- 110 unchanged lines hidden (view full) --- 206 return LZMA_PROG_ERROR; 207 } 208 209 return LZMA_OK; 210} 211 212 213static void |
212stream_encoder_end(lzma_coder *coder, const lzma_allocator *allocator) | 214stream_encoder_end(void *coder_ptr, const lzma_allocator *allocator) |
213{ | 215{ |
216 lzma_stream_coder *coder = coder_ptr; 217 |
|
214 lzma_next_end(&coder->block_encoder, allocator); 215 lzma_next_end(&coder->index_encoder, allocator); 216 lzma_index_end(coder->index, allocator); 217 218 for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i) 219 lzma_free(coder->filters[i].options, allocator); 220 221 lzma_free(coder, allocator); 222 return; 223} 224 225 226static lzma_ret | 218 lzma_next_end(&coder->block_encoder, allocator); 219 lzma_next_end(&coder->index_encoder, allocator); 220 lzma_index_end(coder->index, allocator); 221 222 for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i) 223 lzma_free(coder->filters[i].options, allocator); 224 225 lzma_free(coder, allocator); 226 return; 227} 228 229 230static lzma_ret |
227stream_encoder_update(lzma_coder *coder, const lzma_allocator *allocator, | 231stream_encoder_update(void *coder_ptr, const lzma_allocator *allocator, |
228 const lzma_filter *filters, 229 const lzma_filter *reversed_filters) 230{ | 232 const lzma_filter *filters, 233 const lzma_filter *reversed_filters) 234{ |
235 lzma_stream_coder *coder = coder_ptr; 236 |
|
231 if (coder->sequence <= SEQ_BLOCK_INIT) { 232 // There is no incomplete Block waiting to be finished, 233 // thus we can change the whole filter chain. Start by 234 // trying to initialize the Block encoder with the new 235 // chain. This way we detect if the chain is valid. 236 coder->block_encoder_is_initialized = false; 237 coder->block_options.filters = (lzma_filter *)(filters); 238 const lzma_ret ret = block_encoder_init(coder, allocator); --- 27 unchanged lines hidden (view full) --- 266stream_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, 267 const lzma_filter *filters, lzma_check check) 268{ 269 lzma_next_coder_init(&stream_encoder_init, next, allocator); 270 271 if (filters == NULL) 272 return LZMA_PROG_ERROR; 273 | 237 if (coder->sequence <= SEQ_BLOCK_INIT) { 238 // There is no incomplete Block waiting to be finished, 239 // thus we can change the whole filter chain. Start by 240 // trying to initialize the Block encoder with the new 241 // chain. This way we detect if the chain is valid. 242 coder->block_encoder_is_initialized = false; 243 coder->block_options.filters = (lzma_filter *)(filters); 244 const lzma_ret ret = block_encoder_init(coder, allocator); --- 27 unchanged lines hidden (view full) --- 272stream_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, 273 const lzma_filter *filters, lzma_check check) 274{ 275 lzma_next_coder_init(&stream_encoder_init, next, allocator); 276 277 if (filters == NULL) 278 return LZMA_PROG_ERROR; 279 |
274 if (next->coder == NULL) { 275 next->coder = lzma_alloc(sizeof(lzma_coder), allocator); 276 if (next->coder == NULL) | 280 lzma_stream_coder *coder = next->coder; 281 282 if (coder == NULL) { 283 coder = lzma_alloc(sizeof(lzma_stream_coder), allocator); 284 if (coder == NULL) |
277 return LZMA_MEM_ERROR; 278 | 285 return LZMA_MEM_ERROR; 286 |
287 next->coder = coder; |
|
279 next->code = &stream_encode; 280 next->end = &stream_encoder_end; 281 next->update = &stream_encoder_update; 282 | 288 next->code = &stream_encode; 289 next->end = &stream_encoder_end; 290 next->update = &stream_encoder_update; 291 |
283 next->coder->filters[0].id = LZMA_VLI_UNKNOWN; 284 next->coder->block_encoder = LZMA_NEXT_CODER_INIT; 285 next->coder->index_encoder = LZMA_NEXT_CODER_INIT; 286 next->coder->index = NULL; | 292 coder->filters[0].id = LZMA_VLI_UNKNOWN; 293 coder->block_encoder = LZMA_NEXT_CODER_INIT; 294 coder->index_encoder = LZMA_NEXT_CODER_INIT; 295 coder->index = NULL; |
287 } 288 289 // Basic initializations | 296 } 297 298 // Basic initializations |
290 next->coder->sequence = SEQ_STREAM_HEADER; 291 next->coder->block_options.version = 0; 292 next->coder->block_options.check = check; | 299 coder->sequence = SEQ_STREAM_HEADER; 300 coder->block_options.version = 0; 301 coder->block_options.check = check; |
293 294 // Initialize the Index | 302 303 // Initialize the Index |
295 lzma_index_end(next->coder->index, allocator); 296 next->coder->index = lzma_index_init(allocator); 297 if (next->coder->index == NULL) | 304 lzma_index_end(coder->index, allocator); 305 coder->index = lzma_index_init(allocator); 306 if (coder->index == NULL) |
298 return LZMA_MEM_ERROR; 299 300 // Encode the Stream Header 301 lzma_stream_flags stream_flags = { 302 .version = 0, 303 .check = check, 304 }; 305 return_if_error(lzma_stream_header_encode( | 307 return LZMA_MEM_ERROR; 308 309 // Encode the Stream Header 310 lzma_stream_flags stream_flags = { 311 .version = 0, 312 .check = check, 313 }; 314 return_if_error(lzma_stream_header_encode( |
306 &stream_flags, next->coder->buffer)); | 315 &stream_flags, coder->buffer)); |
307 | 316 |
308 next->coder->buffer_pos = 0; 309 next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE; | 317 coder->buffer_pos = 0; 318 coder->buffer_size = LZMA_STREAM_HEADER_SIZE; |
310 311 // Initialize the Block encoder. This way we detect unsupported 312 // filter chains when initializing the Stream encoder instead of 313 // giving an error after Stream Header has already written out. | 319 320 // Initialize the Block encoder. This way we detect unsupported 321 // filter chains when initializing the Stream encoder instead of 322 // giving an error after Stream Header has already written out. |
314 return stream_encoder_update( 315 next->coder, allocator, filters, NULL); | 323 return stream_encoder_update(coder, allocator, filters, NULL); |
316} 317 318 319extern LZMA_API(lzma_ret) 320lzma_stream_encoder(lzma_stream *strm, 321 const lzma_filter *filters, lzma_check check) 322{ 323 lzma_next_strm_init(stream_encoder_init, strm, filters, check); 324 325 strm->internal->supported_actions[LZMA_RUN] = true; 326 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true; 327 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true; 328 strm->internal->supported_actions[LZMA_FULL_BARRIER] = true; 329 strm->internal->supported_actions[LZMA_FINISH] = true; 330 331 return LZMA_OK; 332} | 324} 325 326 327extern LZMA_API(lzma_ret) 328lzma_stream_encoder(lzma_stream *strm, 329 const lzma_filter *filters, lzma_check check) 330{ 331 lzma_next_strm_init(stream_encoder_init, strm, filters, check); 332 333 strm->internal->supported_actions[LZMA_RUN] = true; 334 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true; 335 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true; 336 strm->internal->supported_actions[LZMA_FULL_BARRIER] = true; 337 strm->internal->supported_actions[LZMA_FINISH] = true; 338 339 return LZMA_OK; 340} |