stream_decoder.c (292588) | stream_decoder.c (312518) |
---|---|
1/////////////////////////////////////////////////////////////////////////////// 2// 3/// \file stream_decoder.c 4/// \brief Decodes .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_decoder.h" 14#include "block_decoder.h" 15 16 | 1/////////////////////////////////////////////////////////////////////////////// 2// 3/// \file stream_decoder.c 4/// \brief Decodes .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_decoder.h" 14#include "block_decoder.h" 15 16 |
17struct lzma_coder_s { | 17typedef struct { |
18 enum { 19 SEQ_STREAM_HEADER, 20 SEQ_BLOCK_HEADER, 21 SEQ_BLOCK, 22 SEQ_INDEX, 23 SEQ_STREAM_FOOTER, 24 SEQ_STREAM_PADDING, 25 } sequence; --- 49 unchanged lines hidden (view full) --- 75 bool first_stream; 76 77 /// Write position in buffer[] and position in Stream Padding 78 size_t pos; 79 80 /// Buffer to hold Stream Header, Block Header, and Stream Footer. 81 /// Block Header has biggest maximum size. 82 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX]; | 18 enum { 19 SEQ_STREAM_HEADER, 20 SEQ_BLOCK_HEADER, 21 SEQ_BLOCK, 22 SEQ_INDEX, 23 SEQ_STREAM_FOOTER, 24 SEQ_STREAM_PADDING, 25 } sequence; --- 49 unchanged lines hidden (view full) --- 75 bool first_stream; 76 77 /// Write position in buffer[] and position in Stream Padding 78 size_t pos; 79 80 /// Buffer to hold Stream Header, Block Header, and Stream Footer. 81 /// Block Header has biggest maximum size. 82 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX]; |
83}; | 83} lzma_stream_coder; |
84 85 86static lzma_ret | 84 85 86static lzma_ret |
87stream_decoder_reset(lzma_coder *coder, const lzma_allocator *allocator) | 87stream_decoder_reset(lzma_stream_coder *coder, const lzma_allocator *allocator) |
88{ 89 // Initialize the Index hash used to verify the Index. 90 coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator); 91 if (coder->index_hash == NULL) 92 return LZMA_MEM_ERROR; 93 94 // Reset the rest of the variables. 95 coder->sequence = SEQ_STREAM_HEADER; 96 coder->pos = 0; 97 98 return LZMA_OK; 99} 100 101 102static lzma_ret | 88{ 89 // Initialize the Index hash used to verify the Index. 90 coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator); 91 if (coder->index_hash == NULL) 92 return LZMA_MEM_ERROR; 93 94 // Reset the rest of the variables. 95 coder->sequence = SEQ_STREAM_HEADER; 96 coder->pos = 0; 97 98 return LZMA_OK; 99} 100 101 102static lzma_ret |
103stream_decode(lzma_coder *coder, const lzma_allocator *allocator, | 103stream_decode(void *coder_ptr, const lzma_allocator *allocator, |
104 const uint8_t *restrict in, size_t *restrict in_pos, 105 size_t in_size, uint8_t *restrict out, 106 size_t *restrict out_pos, size_t out_size, lzma_action action) 107{ | 104 const uint8_t *restrict in, size_t *restrict in_pos, 105 size_t in_size, uint8_t *restrict out, 106 size_t *restrict out_pos, size_t out_size, lzma_action action) 107{ |
108 lzma_stream_coder *coder = coder_ptr; 109 |
|
108 // When decoding the actual Block, it may be able to produce more 109 // output even if we don't give it any new input. 110 while (true) 111 switch (coder->sequence) { 112 case SEQ_STREAM_HEADER: { 113 // Copy the Stream Header to the internal buffer. 114 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos, 115 LZMA_STREAM_HEADER_SIZE); --- 254 unchanged lines hidden (view full) --- 370 return LZMA_PROG_ERROR; 371 } 372 373 // Never reached 374} 375 376 377static void | 110 // When decoding the actual Block, it may be able to produce more 111 // output even if we don't give it any new input. 112 while (true) 113 switch (coder->sequence) { 114 case SEQ_STREAM_HEADER: { 115 // Copy the Stream Header to the internal buffer. 116 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos, 117 LZMA_STREAM_HEADER_SIZE); --- 254 unchanged lines hidden (view full) --- 372 return LZMA_PROG_ERROR; 373 } 374 375 // Never reached 376} 377 378 379static void |
378stream_decoder_end(lzma_coder *coder, const lzma_allocator *allocator) | 380stream_decoder_end(void *coder_ptr, const lzma_allocator *allocator) |
379{ | 381{ |
382 lzma_stream_coder *coder = coder_ptr; |
|
380 lzma_next_end(&coder->block_decoder, allocator); 381 lzma_index_hash_end(coder->index_hash, allocator); 382 lzma_free(coder, allocator); 383 return; 384} 385 386 387static lzma_check | 383 lzma_next_end(&coder->block_decoder, allocator); 384 lzma_index_hash_end(coder->index_hash, allocator); 385 lzma_free(coder, allocator); 386 return; 387} 388 389 390static lzma_check |
388stream_decoder_get_check(const lzma_coder *coder) | 391stream_decoder_get_check(const void *coder_ptr) |
389{ | 392{ |
393 const lzma_stream_coder *coder = coder_ptr; |
|
390 return coder->stream_flags.check; 391} 392 393 394static lzma_ret | 394 return coder->stream_flags.check; 395} 396 397 398static lzma_ret |
395stream_decoder_memconfig(lzma_coder *coder, uint64_t *memusage, | 399stream_decoder_memconfig(void *coder_ptr, uint64_t *memusage, |
396 uint64_t *old_memlimit, uint64_t new_memlimit) 397{ | 400 uint64_t *old_memlimit, uint64_t new_memlimit) 401{ |
402 lzma_stream_coder *coder = coder_ptr; 403 |
|
398 *memusage = coder->memusage; 399 *old_memlimit = coder->memlimit; 400 401 if (new_memlimit != 0) { 402 if (new_memlimit < coder->memusage) 403 return LZMA_MEMLIMIT_ERROR; 404 405 coder->memlimit = new_memlimit; --- 11 unchanged lines hidden (view full) --- 417 lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator); 418 419 if (memlimit == 0) 420 return LZMA_PROG_ERROR; 421 422 if (flags & ~LZMA_SUPPORTED_FLAGS) 423 return LZMA_OPTIONS_ERROR; 424 | 404 *memusage = coder->memusage; 405 *old_memlimit = coder->memlimit; 406 407 if (new_memlimit != 0) { 408 if (new_memlimit < coder->memusage) 409 return LZMA_MEMLIMIT_ERROR; 410 411 coder->memlimit = new_memlimit; --- 11 unchanged lines hidden (view full) --- 423 lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator); 424 425 if (memlimit == 0) 426 return LZMA_PROG_ERROR; 427 428 if (flags & ~LZMA_SUPPORTED_FLAGS) 429 return LZMA_OPTIONS_ERROR; 430 |
425 if (next->coder == NULL) { 426 next->coder = lzma_alloc(sizeof(lzma_coder), allocator); 427 if (next->coder == NULL) | 431 lzma_stream_coder *coder = next->coder; 432 if (coder == NULL) { 433 coder = lzma_alloc(sizeof(lzma_stream_coder), allocator); 434 if (coder == NULL) |
428 return LZMA_MEM_ERROR; 429 | 435 return LZMA_MEM_ERROR; 436 |
437 next->coder = coder; |
|
430 next->code = &stream_decode; 431 next->end = &stream_decoder_end; 432 next->get_check = &stream_decoder_get_check; 433 next->memconfig = &stream_decoder_memconfig; 434 | 438 next->code = &stream_decode; 439 next->end = &stream_decoder_end; 440 next->get_check = &stream_decoder_get_check; 441 next->memconfig = &stream_decoder_memconfig; 442 |
435 next->coder->block_decoder = LZMA_NEXT_CODER_INIT; 436 next->coder->index_hash = NULL; | 443 coder->block_decoder = LZMA_NEXT_CODER_INIT; 444 coder->index_hash = NULL; |
437 } 438 | 445 } 446 |
439 next->coder->memlimit = memlimit; 440 next->coder->memusage = LZMA_MEMUSAGE_BASE; 441 next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0; 442 next->coder->tell_unsupported_check | 447 coder->memlimit = memlimit; 448 coder->memusage = LZMA_MEMUSAGE_BASE; 449 coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0; 450 coder->tell_unsupported_check |
443 = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0; | 451 = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0; |
444 next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0; 445 next->coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0; 446 next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0; 447 next->coder->first_stream = true; | 452 coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0; 453 coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0; 454 coder->concatenated = (flags & LZMA_CONCATENATED) != 0; 455 coder->first_stream = true; |
448 | 456 |
449 return stream_decoder_reset(next->coder, allocator); | 457 return stream_decoder_reset(coder, allocator); |
450} 451 452 453extern LZMA_API(lzma_ret) 454lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags) 455{ 456 lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags); 457 458 strm->internal->supported_actions[LZMA_RUN] = true; 459 strm->internal->supported_actions[LZMA_FINISH] = true; 460 461 return LZMA_OK; 462} | 458} 459 460 461extern LZMA_API(lzma_ret) 462lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags) 463{ 464 lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags); 465 466 strm->internal->supported_actions[LZMA_RUN] = true; 467 strm->internal->supported_actions[LZMA_FINISH] = true; 468 469 return LZMA_OK; 470} |