2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file block_decoder.c
|
2008-12-27 18:27:49 +01:00
|
|
|
/// \brief Decodes .xz Blocks
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
// Copyright (C) 2007 Lasse Collin
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "block_decoder.h"
|
2008-08-28 21:53:15 +02:00
|
|
|
#include "filter_decoder.h"
|
2007-12-08 23:42:33 +01:00
|
|
|
#include "check.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct lzma_coder_s {
|
|
|
|
enum {
|
|
|
|
SEQ_CODE,
|
|
|
|
SEQ_PADDING,
|
2008-06-18 17:02:10 +02:00
|
|
|
SEQ_CHECK,
|
2007-12-08 23:42:33 +01:00
|
|
|
} sequence;
|
|
|
|
|
|
|
|
/// The filters in the chain; initialized with lzma_raw_decoder_init().
|
|
|
|
lzma_next_coder next;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
/// Decoding options; we also write Compressed Size and Uncompressed
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Size back to this structure when the decoding has been finished.
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_block *block;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Compressed Size calculated while decoding
|
2007-12-08 23:42:33 +01:00
|
|
|
lzma_vli compressed_size;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Uncompressed Size calculated while decoding
|
2007-12-08 23:42:33 +01:00
|
|
|
lzma_vli uncompressed_size;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
/// Maximum allowed Compressed Size; this takes into account the
|
|
|
|
/// size of the Block Header and Check fields when Compressed Size
|
|
|
|
/// is unknown.
|
|
|
|
lzma_vli compressed_limit;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
/// Position when reading the Check field
|
|
|
|
size_t check_pos;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
/// Check of the uncompressed data
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_check_state check;
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-09-09 23:27:02 +02:00
|
|
|
static inline bool
|
|
|
|
update_size(lzma_vli *size, lzma_vli add, lzma_vli limit)
|
|
|
|
{
|
2008-09-13 11:10:43 +02:00
|
|
|
if (limit > LZMA_VLI_MAX)
|
|
|
|
limit = LZMA_VLI_MAX;
|
2008-09-09 23:27:02 +02:00
|
|
|
|
|
|
|
if (limit < *size || limit - *size < add)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
*size += add;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
is_size_valid(lzma_vli size, lzma_vli reference)
|
|
|
|
{
|
2008-09-13 11:10:43 +02:00
|
|
|
return reference == LZMA_VLI_UNKNOWN || reference == size;
|
2008-09-09 23:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
static lzma_ret
|
|
|
|
block_decode(lzma_coder *coder, lzma_allocator *allocator,
|
|
|
|
const uint8_t *restrict in, size_t *restrict in_pos,
|
|
|
|
size_t in_size, uint8_t *restrict out,
|
|
|
|
size_t *restrict out_pos, size_t out_size, lzma_action action)
|
|
|
|
{
|
|
|
|
switch (coder->sequence) {
|
|
|
|
case SEQ_CODE: {
|
|
|
|
const size_t in_start = *in_pos;
|
|
|
|
const size_t out_start = *out_pos;
|
|
|
|
|
2008-01-08 22:11:59 +01:00
|
|
|
const lzma_ret ret = coder->next.code(coder->next.coder,
|
2007-12-08 23:42:33 +01:00
|
|
|
allocator, in, in_pos, in_size,
|
|
|
|
out, out_pos, out_size, action);
|
|
|
|
|
|
|
|
const size_t in_used = *in_pos - in_start;
|
|
|
|
const size_t out_used = *out_pos - out_start;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// NOTE: We compare to compressed_limit here, which prevents
|
2008-09-13 11:10:43 +02:00
|
|
|
// the total size of the Block growing past LZMA_VLI_MAX.
|
2008-06-18 17:02:10 +02:00
|
|
|
if (update_size(&coder->compressed_size, in_used,
|
|
|
|
coder->compressed_limit)
|
2007-12-08 23:42:33 +01:00
|
|
|
|| update_size(&coder->uncompressed_size,
|
2008-06-18 17:02:10 +02:00
|
|
|
out_used,
|
2008-12-27 18:27:49 +01:00
|
|
|
coder->block->uncompressed_size))
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_check_update(&coder->check, coder->block->check,
|
2007-12-08 23:42:33 +01:00
|
|
|
out + out_start, out_used);
|
|
|
|
|
|
|
|
if (ret != LZMA_STREAM_END)
|
|
|
|
return ret;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Compressed and Uncompressed Sizes are now at their final
|
|
|
|
// values. Verify that they match the values given to us.
|
|
|
|
if (!is_size_valid(coder->compressed_size,
|
2008-12-27 18:27:49 +01:00
|
|
|
coder->block->compressed_size)
|
2008-11-19 19:46:52 +01:00
|
|
|
|| !is_size_valid(coder->uncompressed_size,
|
2008-12-27 18:27:49 +01:00
|
|
|
coder->block->uncompressed_size))
|
2008-11-19 19:46:52 +01:00
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
// Copy the values into coder->block. The caller
|
2008-11-19 19:46:52 +01:00
|
|
|
// may use this information to construct Index.
|
2008-12-27 18:27:49 +01:00
|
|
|
coder->block->compressed_size = coder->compressed_size;
|
|
|
|
coder->block->uncompressed_size = coder->uncompressed_size;
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
coder->sequence = SEQ_PADDING;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Fall through
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
case SEQ_PADDING:
|
2008-08-28 21:53:15 +02:00
|
|
|
// Compressed Data is padded to a multiple of four bytes.
|
2008-06-18 17:02:10 +02:00
|
|
|
while (coder->compressed_size & 3) {
|
2009-01-25 00:35:56 +01:00
|
|
|
if (*in_pos >= in_size)
|
|
|
|
return LZMA_OK;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// We use compressed_size here just get the Padding
|
|
|
|
// right. The actual Compressed Size was stored to
|
2008-12-27 18:27:49 +01:00
|
|
|
// coder->block already, and won't be modified by
|
2008-11-19 19:46:52 +01:00
|
|
|
// us anymore.
|
|
|
|
++coder->compressed_size;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
if (in[(*in_pos)++] != 0x00)
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
if (coder->block->check == LZMA_CHECK_NONE)
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_STREAM_END;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_check_finish(&coder->check, coder->block->check);
|
2008-06-18 17:02:10 +02:00
|
|
|
coder->sequence = SEQ_CHECK;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Fall through
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
case SEQ_CHECK: {
|
|
|
|
const bool chksup = lzma_check_is_supported(
|
2008-12-27 18:27:49 +01:00
|
|
|
coder->block->check);
|
2008-08-28 21:53:15 +02:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
while (*in_pos < in_size) {
|
2008-08-28 21:53:15 +02:00
|
|
|
// coder->check.buffer[] may be uninitialized when
|
|
|
|
// the Check ID is not supported.
|
|
|
|
if (chksup && coder->check.buffer.u8[coder->check_pos]
|
|
|
|
!= in[*in_pos]) {
|
|
|
|
++*in_pos;
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_DATA_ERROR;
|
2008-08-28 21:53:15 +02:00
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
++*in_pos;
|
|
|
|
|
|
|
|
if (++coder->check_pos == lzma_check_size(
|
2008-12-27 18:27:49 +01:00
|
|
|
coder->block->check))
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_STREAM_END;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_OK;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
2008-08-28 21:53:15 +02:00
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_PROG_ERROR;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
|
|
|
|
{
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_next_end(&coder->next, allocator);
|
2007-12-08 23:42:33 +01:00
|
|
|
lzma_free(coder, allocator);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
extern lzma_ret
|
|
|
|
lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_block *block)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_next_coder_init(lzma_block_decoder_init, next, allocator);
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Validate the options. lzma_block_unpadded_size() does that for us
|
|
|
|
// except for Uncompressed Size and filters. Filters are validated
|
|
|
|
// by the raw decoder.
|
2008-12-27 18:27:49 +01:00
|
|
|
if (lzma_block_unpadded_size(block) == 0
|
|
|
|
|| !lzma_vli_is_valid(block->uncompressed_size))
|
2008-08-28 21:53:15 +02:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Allocate and initialize *next->coder if needed.
|
2007-12-08 23:42:33 +01:00
|
|
|
if (next->coder == NULL) {
|
|
|
|
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
|
|
|
if (next->coder == NULL)
|
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
|
|
|
next->code = &block_decode;
|
|
|
|
next->end = &block_decoder_end;
|
|
|
|
next->coder->next = LZMA_NEXT_CODER_INIT;
|
|
|
|
}
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Basic initializations
|
2008-01-08 21:58:42 +01:00
|
|
|
next->coder->sequence = SEQ_CODE;
|
2008-12-27 18:27:49 +01:00
|
|
|
next->coder->block = block;
|
2007-12-08 23:42:33 +01:00
|
|
|
next->coder->compressed_size = 0;
|
|
|
|
next->coder->uncompressed_size = 0;
|
2008-06-18 17:02:10 +02:00
|
|
|
|
|
|
|
// If Compressed Size is not known, we calculate the maximum allowed
|
2008-11-19 19:46:52 +01:00
|
|
|
// value so that encoded size of the Block (including Block Padding)
|
|
|
|
// is still a valid VLI and a multiple of four.
|
2008-06-18 17:02:10 +02:00
|
|
|
next->coder->compressed_limit
|
2008-12-27 18:27:49 +01:00
|
|
|
= block->compressed_size == LZMA_VLI_UNKNOWN
|
2008-09-13 11:10:43 +02:00
|
|
|
? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
|
2008-12-27 18:27:49 +01:00
|
|
|
- block->header_size
|
|
|
|
- lzma_check_size(block->check)
|
|
|
|
: block->compressed_size;
|
2008-06-18 17:02:10 +02:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
// Initialize the check. It's caller's problem if the Check ID is not
|
|
|
|
// supported, and the Block decoder cannot verify the Check field.
|
2008-12-27 18:27:49 +01:00
|
|
|
// Caller can test lzma_check_is_supported(block->check).
|
2008-06-18 17:02:10 +02:00
|
|
|
next->coder->check_pos = 0;
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_check_init(&next->coder->check, block->check);
|
2008-01-08 21:58:42 +01:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
// Initialize the filter chain.
|
2008-01-08 21:58:42 +01:00
|
|
|
return lzma_raw_decoder_init(&next->coder->next, allocator,
|
2008-12-27 18:27:49 +01:00
|
|
|
block->filters);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern LZMA_API lzma_ret
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_block_decoder(lzma_stream *strm, lzma_block *block)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_next_strm_init(lzma_block_decoder_init, strm, block);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
strm->internal->supported_actions[LZMA_RUN] = true;
|
2008-09-04 09:39:15 +02:00
|
|
|
strm->internal->supported_actions[LZMA_FINISH] = true;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|