11341Sstevel///////////////////////////////////////////////////////////////////////////////
21341Sstevel//
31341Sstevel/// \file       block_buffer_decoder.c
41341Sstevel/// \brief      Single-call .xz Block decoder
51341Sstevel//
61341Sstevel//  Author:     Lasse Collin
71341Sstevel//
81341Sstevel//  This file has been put into the public domain.
91341Sstevel//  You can do whatever you want with this file.
101341Sstevel//
111341Sstevel///////////////////////////////////////////////////////////////////////////////
121341Sstevel
131341Sstevel#include "block_decoder.h"
141341Sstevel
151341Sstevel
161341Sstevelextern LZMA_API(lzma_ret)
171341Sstevellzma_block_buffer_decode(lzma_block *block, const lzma_allocator *allocator,
181341Sstevel		const uint8_t *in, size_t *in_pos, size_t in_size,
191341Sstevel		uint8_t *out, size_t *out_pos, size_t out_size)
201341Sstevel{
211341Sstevel	if (in_pos == NULL || (in == NULL && *in_pos != in_size)
221341Sstevel			|| *in_pos > in_size || out_pos == NULL
231341Sstevel			|| (out == NULL && *out_pos != out_size)
241341Sstevel			|| *out_pos > out_size)
251341Sstevel		return LZMA_PROG_ERROR;
261341Sstevel
271341Sstevel	// Initialize the Block decoder.
281341Sstevel	lzma_next_coder block_decoder = LZMA_NEXT_CODER_INIT;
291341Sstevel	lzma_ret ret = lzma_block_decoder_init(
301341Sstevel			&block_decoder, allocator, block);
311341Sstevel
321341Sstevel	if (ret == LZMA_OK) {
331341Sstevel		// Save the positions so that we can restore them in case
341341Sstevel		// an error occurs.
351341Sstevel		const size_t in_start = *in_pos;
361341Sstevel		const size_t out_start = *out_pos;
371341Sstevel
381341Sstevel		// Do the actual decoding.
391341Sstevel		ret = block_decoder.code(block_decoder.coder, allocator,
401341Sstevel				in, in_pos, in_size, out, out_pos, out_size,
411341Sstevel				LZMA_FINISH);
421341Sstevel
431341Sstevel		if (ret == LZMA_STREAM_END) {
441341Sstevel			ret = LZMA_OK;
451341Sstevel		} else {
461341Sstevel			if (ret == LZMA_OK) {
471341Sstevel				// Either the input was truncated or the
481341Sstevel				// output buffer was too small.
491341Sstevel				assert(*in_pos == in_size
501341Sstevel						|| *out_pos == out_size);
511341Sstevel
521341Sstevel				// If all the input was consumed, then the
531341Sstevel				// input is truncated, even if the output
541341Sstevel				// buffer is also full. This is because
551341Sstevel				// processing the last byte of the Block
561341Sstevel				// never produces output.
571341Sstevel				//
581341Sstevel				// NOTE: This assumption may break when new
591341Sstevel				// filters are added, if the end marker of
601341Sstevel				// the filter doesn't consume at least one
611341Sstevel				// complete byte.
621341Sstevel				if (*in_pos == in_size)
631341Sstevel					ret = LZMA_DATA_ERROR;
641341Sstevel				else
651341Sstevel					ret = LZMA_BUF_ERROR;
661341Sstevel			}
671341Sstevel
681341Sstevel			// Restore the positions.
691341Sstevel			*in_pos = in_start;
701341Sstevel			*out_pos = out_start;
711341Sstevel		}
721341Sstevel	}
731341Sstevel
741341Sstevel	// Free the decoder memory. This needs to be done even if
751341Sstevel	// initialization fails, because the internal API doesn't
761341Sstevel	// require the initialization function to free its memory on error.
771341Sstevel	lzma_next_end(&block_decoder, allocator);
781341Sstevel
791341Sstevel	return ret;
801341Sstevel}
811341Sstevel