1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       block_buffer_decoder.c
4207753Smm/// \brief      Single-call .xz Block decoder
5207753Smm//
6207753Smm//  Author:     Lasse Collin
7207753Smm//
8207753Smm//  This file has been put into the public domain.
9207753Smm//  You can do whatever you want with this file.
10207753Smm//
11207753Smm///////////////////////////////////////////////////////////////////////////////
12207753Smm
13207753Smm#include "block_decoder.h"
14207753Smm
15207753Smm
16207753Smmextern LZMA_API(lzma_ret)
17207753Smmlzma_block_buffer_decode(lzma_block *block, lzma_allocator *allocator,
18207753Smm		const uint8_t *in, size_t *in_pos, size_t in_size,
19207753Smm		uint8_t *out, size_t *out_pos, size_t out_size)
20207753Smm{
21207753Smm	if (in_pos == NULL || (in == NULL && *in_pos != in_size)
22207753Smm			|| *in_pos > in_size || out_pos == NULL
23207753Smm			|| (out == NULL && *out_pos != out_size)
24207753Smm			|| *out_pos > out_size)
25207753Smm		return LZMA_PROG_ERROR;
26207753Smm
27207753Smm	// Initialize the Block decoder.
28207753Smm	lzma_next_coder block_decoder = LZMA_NEXT_CODER_INIT;
29207753Smm	lzma_ret ret = lzma_block_decoder_init(
30207753Smm			&block_decoder, allocator, block);
31207753Smm
32207753Smm	if (ret == LZMA_OK) {
33207753Smm		// Save the positions so that we can restore them in case
34207753Smm		// an error occurs.
35207753Smm		const size_t in_start = *in_pos;
36207753Smm		const size_t out_start = *out_pos;
37207753Smm
38207753Smm		// Do the actual decoding.
39207753Smm		ret = block_decoder.code(block_decoder.coder, allocator,
40207753Smm				in, in_pos, in_size, out, out_pos, out_size,
41207753Smm				LZMA_FINISH);
42207753Smm
43207753Smm		if (ret == LZMA_STREAM_END) {
44207753Smm			ret = LZMA_OK;
45207753Smm		} else {
46207753Smm			if (ret == LZMA_OK) {
47207753Smm				// Either the input was truncated or the
48207753Smm				// output buffer was too small.
49207753Smm				assert(*in_pos == in_size
50207753Smm						|| *out_pos == out_size);
51207753Smm
52207753Smm				// If all the input was consumed, then the
53207753Smm				// input is truncated, even if the output
54207753Smm				// buffer is also full. This is because
55207753Smm				// processing the last byte of the Block
56207753Smm				// never produces output.
57207753Smm				//
58207753Smm				// NOTE: This assumption may break when new
59207753Smm				// filters are added, if the end marker of
60207753Smm				// the filter doesn't consume at least one
61207753Smm				// complete byte.
62207753Smm				if (*in_pos == in_size)
63207753Smm					ret = LZMA_DATA_ERROR;
64207753Smm				else
65207753Smm					ret = LZMA_BUF_ERROR;
66207753Smm			}
67207753Smm
68207753Smm			// Restore the positions.
69207753Smm			*in_pos = in_start;
70207753Smm			*out_pos = out_start;
71207753Smm		}
72207753Smm	}
73207753Smm
74207753Smm	// Free the decoder memory. This needs to be done even if
75207753Smm	// initialization fails, because the internal API doesn't
76207753Smm	// require the initialization function to free its memory on error.
77207753Smm	lzma_next_end(&block_decoder, allocator);
78207753Smm
79207753Smm	return ret;
80207753Smm}
81