]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/filter_buffer_encoder.c
Added initial version of raw buffer-to-buffer coding
[icculus/xz.git] / src / liblzma / common / filter_buffer_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       filter_buffer_encoder.c
4 /// \brief      Single-call raw encoding
5 //
6 //  Copyright (C) 2009 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "filter_encoder.h"
21
22
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)
27 {
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;
32
33         // Initialize the encoder
34         lzma_next_coder next = LZMA_NEXT_CODER_INIT;
35         return_if_error(lzma_raw_encoder_init(&next, allocator, filters));
36
37         // Store the output position so that we can restore it if
38         // something goes wrong.
39         const size_t out_start = *out_pos;
40
41         // Do the actual encoding and free coder's memory.
42         size_t in_pos = 0;
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);
46
47         if (ret == LZMA_STREAM_END) {
48                 ret = LZMA_OK;
49         } else {
50                 if (ret == LZMA_OK) {
51                         // Output buffer was too small.
52                         assert(*out_pos == out_size);
53                         ret = LZMA_BUF_ERROR;
54                 }
55
56                 // Restore the output position.
57                 *out_pos = out_start;
58         }
59
60         return ret;
61 }