diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2009-01-26 14:34:10 +0200 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2009-01-26 14:34:10 +0200 |
commit | 667481f1aad34e1ed15738e7913a9c7e256b4cf5 (patch) | |
tree | dc5b1b272885a07b6b0a6c7c94561c3924e3d811 /src/liblzma/common | |
parent | Add more sanity checks to lzma_stream_buffer_decode(). (diff) | |
download | xz-667481f1aad34e1ed15738e7913a9c7e256b4cf5.tar.xz |
Add lzma_block_buffer_decode().
Diffstat (limited to '')
-rw-r--r-- | src/liblzma/common/Makefile.am | 1 | ||||
-rw-r--r-- | src/liblzma/common/block_buffer_decoder.c | 87 |
2 files changed, 88 insertions, 0 deletions
diff --git a/src/liblzma/common/Makefile.am b/src/liblzma/common/Makefile.am index e8ff480f..1496b2d0 100644 --- a/src/liblzma/common/Makefile.am +++ b/src/liblzma/common/Makefile.am @@ -61,6 +61,7 @@ libcommon_la_SOURCES += \ alone_decoder.c \ alone_decoder.h \ auto_decoder.c \ + block_buffer_decoder.c \ block_decoder.c \ block_decoder.h \ block_header_decoder.c \ diff --git a/src/liblzma/common/block_buffer_decoder.c b/src/liblzma/common/block_buffer_decoder.c new file mode 100644 index 00000000..ec24e44b --- /dev/null +++ b/src/liblzma/common/block_buffer_decoder.c @@ -0,0 +1,87 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file block_buffer_decoder.c +/// \brief Single-call .xz Block decoder +// +// Copyright (C) 2009 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" + + +extern LZMA_API lzma_ret +lzma_block_buffer_decode(lzma_block *block, lzma_allocator *allocator, + const uint8_t *in, size_t *in_pos, size_t in_size, + uint8_t *out, size_t *out_pos, size_t out_size) +{ + if (in_pos == NULL || (in == NULL && *in_pos != in_size) + || *in_pos > in_size || out_pos == NULL + || (out == NULL && *out_pos != out_size) + || *out_pos > out_size) + return LZMA_PROG_ERROR; + + // Initialize the Block decoder. + lzma_next_coder block_decoder = LZMA_NEXT_CODER_INIT; + lzma_ret ret = lzma_block_decoder_init( + &block_decoder, allocator, block); + + if (ret == LZMA_OK) { + // Save the positions so that we can restore them in case + // an error occurs. + const size_t in_start = *in_pos; + const size_t out_start = *out_pos; + + // Do the actual decoding. + ret = block_decoder.code(block_decoder.coder, allocator, + in, in_pos, in_size, out, out_pos, out_size, + LZMA_FINISH); + + if (ret == LZMA_STREAM_END) { + ret = LZMA_OK; + } else { + // Something went wrong, restore the positions. + *in_pos = in_start; + *out_pos = out_start; + + if (ret == LZMA_OK) { + // Either the input was truncated or the + // output buffer was too small. + assert(*in_pos == in_size + || *out_pos == out_size); + + // If all the input was consumed, then the + // input is truncated, even if the output + // buffer is also full. This is because + // processing the last byte of the Block + // never produces output. + // + // NOTE: This assumption may break when new + // filters are added, if the end marker of + // the filter doesn't consume at least one + // complete byte. + if (*in_pos == in_size) + ret = LZMA_DATA_ERROR; + else + ret = LZMA_BUF_ERROR; + } + } + } + + // Free the decoder memory. This needs to be done even if + // initialization fails, because the internal API doesn't + // require the initialization function to free its memory on error. + lzma_next_end(&block_decoder, allocator); + + return ret; +} |