xref: /dragonfly/contrib/xz/src/liblzma/common/filter_buffer_encoder.c (revision 4381ed9d7ee193d719c4e4a94a9d267d177981c1)
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       filter_buffer_encoder.c
4 /// \brief      Single-call raw encoding
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "filter_encoder.h"
14 
15 
16 extern LZMA_API(lzma_ret)
lzma_raw_buffer_encode(const lzma_filter * filters,const lzma_allocator * allocator,const uint8_t * in,size_t in_size,uint8_t * out,size_t * out_pos,size_t out_size)17 lzma_raw_buffer_encode(
18                     const lzma_filter *filters, const lzma_allocator *allocator,
19                     const uint8_t *in, size_t in_size,
20                     uint8_t *out, size_t *out_pos, size_t out_size)
21 {
22           // Validate what isn't validated later in filter_common.c.
23           if ((in == NULL && in_size != 0) || out == NULL
24                               || out_pos == NULL || *out_pos > out_size)
25                     return LZMA_PROG_ERROR;
26 
27           // Initialize the encoder
28           lzma_next_coder next = LZMA_NEXT_CODER_INIT;
29           return_if_error(lzma_raw_encoder_init(&next, allocator, filters));
30 
31           // Store the output position so that we can restore it if
32           // something goes wrong.
33           const size_t out_start = *out_pos;
34 
35           // Do the actual encoding and free coder's memory.
36           size_t in_pos = 0;
37           lzma_ret ret = next.code(next.coder, allocator, in, &in_pos, in_size,
38                               out, out_pos, out_size, LZMA_FINISH);
39           lzma_next_end(&next, allocator);
40 
41           if (ret == LZMA_STREAM_END) {
42                     ret = LZMA_OK;
43           } else {
44                     if (ret == LZMA_OK) {
45                               // Output buffer was too small.
46                               assert(*out_pos == out_size);
47                               ret = LZMA_BUF_ERROR;
48                     }
49 
50                     // Restore the output position.
51                     *out_pos = out_start;
52           }
53 
54           return ret;
55 }
56