1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file filter_buffer_encoder.c
4 /// \brief Single-call raw encoding
6 // Copyright (C) 2009 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "filter_encoder.h"
23 extern LZMA_API lzma_ret
24 lzma_raw_buffer_encode(const lzma_filter *filters, lzma_allocator *allocator,
25 const uint8_t *in, size_t in_size, uint8_t *out,
26 size_t *out_pos, size_t out_size)
28 // Validate what isn't validated later in filter_common.c.
29 if ((in == NULL && in_size != 0) || out == NULL
30 || out_pos == NULL || *out_pos > out_size)
31 return LZMA_PROG_ERROR;
33 // Initialize the encoder
34 lzma_next_coder next = LZMA_NEXT_CODER_INIT;
35 return_if_error(lzma_raw_encoder_init(&next, allocator, filters));
37 // Store the output position so that we can restore it if
38 // something goes wrong.
39 const size_t out_start = *out_pos;
41 // Do the actual encoding and free coder's memory.
43 lzma_ret ret = next.code(next.coder, allocator, in, &in_pos, in_size,
44 out, out_pos, out_size, LZMA_FINISH);
45 lzma_next_end(&next, allocator);
47 if (ret == LZMA_STREAM_END) {
51 // Output buffer was too small.
52 assert(*out_pos == out_size);
56 // Restore the output position.