1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       filter_buffer_encoder.c
4207753Smm/// \brief      Single-call raw encoding
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 "filter_encoder.h"
14207753Smm
15207753Smm
16207753Smmextern LZMA_API(lzma_ret)
17292588Sdelphijlzma_raw_buffer_encode(
18292588Sdelphij		const lzma_filter *filters, const lzma_allocator *allocator,
19292588Sdelphij		const uint8_t *in, size_t in_size,
20292588Sdelphij		uint8_t *out, size_t *out_pos, size_t out_size)
21207753Smm{
22207753Smm	// Validate what isn't validated later in filter_common.c.
23207753Smm	if ((in == NULL && in_size != 0) || out == NULL
24207753Smm			|| out_pos == NULL || *out_pos > out_size)
25207753Smm		return LZMA_PROG_ERROR;
26207753Smm
27207753Smm	// Initialize the encoder
28207753Smm	lzma_next_coder next = LZMA_NEXT_CODER_INIT;
29207753Smm	return_if_error(lzma_raw_encoder_init(&next, allocator, filters));
30207753Smm
31207753Smm	// Store the output position so that we can restore it if
32207753Smm	// something goes wrong.
33207753Smm	const size_t out_start = *out_pos;
34207753Smm
35207753Smm	// Do the actual encoding and free coder's memory.
36207753Smm	size_t in_pos = 0;
37207753Smm	lzma_ret ret = next.code(next.coder, allocator, in, &in_pos, in_size,
38207753Smm			out, out_pos, out_size, LZMA_FINISH);
39207753Smm	lzma_next_end(&next, allocator);
40207753Smm
41207753Smm	if (ret == LZMA_STREAM_END) {
42207753Smm		ret = LZMA_OK;
43207753Smm	} else {
44207753Smm		if (ret == LZMA_OK) {
45207753Smm			// Output buffer was too small.
46207753Smm			assert(*out_pos == out_size);
47207753Smm			ret = LZMA_BUF_ERROR;
48207753Smm		}
49207753Smm
50207753Smm		// Restore the output position.
51207753Smm		*out_pos = out_start;
52207753Smm	}
53207753Smm
54207753Smm	return ret;
55207753Smm}
56