Path: blob/master/Utilities/cmliblzma/liblzma/common/block_buffer_decoder.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file block_buffer_decoder.c5/// \brief Single-call .xz Block decoder6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "block_decoder.h"121314extern LZMA_API(lzma_ret)15lzma_block_buffer_decode(lzma_block *block, const lzma_allocator *allocator,16const uint8_t *in, size_t *in_pos, size_t in_size,17uint8_t *out, size_t *out_pos, size_t out_size)18{19if (in_pos == NULL || (in == NULL && *in_pos != in_size)20|| *in_pos > in_size || out_pos == NULL21|| (out == NULL && *out_pos != out_size)22|| *out_pos > out_size)23return LZMA_PROG_ERROR;2425// Initialize the Block decoder.26lzma_next_coder block_decoder = LZMA_NEXT_CODER_INIT;27lzma_ret ret = lzma_block_decoder_init(28&block_decoder, allocator, block);2930if (ret == LZMA_OK) {31// Save the positions so that we can restore them in case32// an error occurs.33const size_t in_start = *in_pos;34const size_t out_start = *out_pos;3536// Do the actual decoding.37ret = block_decoder.code(block_decoder.coder, allocator,38in, in_pos, in_size, out, out_pos, out_size,39LZMA_FINISH);4041if (ret == LZMA_STREAM_END) {42ret = LZMA_OK;43} else {44if (ret == LZMA_OK) {45// Either the input was truncated or the46// output buffer was too small.47assert(*in_pos == in_size48|| *out_pos == out_size);4950// If all the input was consumed, then the51// input is truncated, even if the output52// buffer is also full. This is because53// processing the last byte of the Block54// never produces output.55//56// NOTE: This assumption may break when new57// filters are added, if the end marker of58// the filter doesn't consume at least one59// complete byte.60if (*in_pos == in_size)61ret = LZMA_DATA_ERROR;62else63ret = LZMA_BUF_ERROR;64}6566// Restore the positions.67*in_pos = in_start;68*out_pos = out_start;69}70}7172// Free the decoder memory. This needs to be done even if73// initialization fails, because the internal API doesn't74// require the initialization function to free its memory on error.75lzma_next_end(&block_decoder, allocator);7677return ret;78}798081