]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/xz/src/liblzma/subblock/subblock_decoder_helper.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / xz / src / liblzma / subblock / subblock_decoder_helper.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       subblock_decoder_helper.c
4 /// \brief      Helper filter for the Subblock decoder
5 ///
6 /// This filter is used to indicate End of Input for subfilters needing it.
7 //
8 //  Author:     Lasse Collin
9 //
10 //  This file has been put into the public domain.
11 //  You can do whatever you want with this file.
12 //
13 ///////////////////////////////////////////////////////////////////////////////
14
15 #include "subblock_decoder_helper.h"
16
17
18 struct lzma_coder_s {
19         const lzma_options_subblock_helper *options;
20 };
21
22
23 static lzma_ret
24 helper_decode(lzma_coder *coder,
25                 lzma_allocator *allocator lzma_attribute((unused)),
26                 const uint8_t *restrict in, size_t *restrict in_pos,
27                 size_t in_size, uint8_t *restrict out,
28                 size_t *restrict out_pos, size_t out_size,
29                 lzma_action action lzma_attribute((unused)))
30 {
31         // If end_was_reached is true, we cannot have any input.
32         assert(!coder->options->end_was_reached || *in_pos == in_size);
33
34         // We can safely copy as much as possible, because we are never
35         // given more data than a single Subblock Data field.
36         lzma_bufcpy(in, in_pos, in_size, out, out_pos, out_size);
37
38         // Return LZMA_STREAM_END when instructed so by the Subblock decoder.
39         return coder->options->end_was_reached ? LZMA_STREAM_END : LZMA_OK;
40 }
41
42
43 static void
44 helper_end(lzma_coder *coder, lzma_allocator *allocator)
45 {
46         lzma_free(coder, allocator);
47         return;
48 }
49
50
51 extern lzma_ret
52 lzma_subblock_decoder_helper_init(lzma_next_coder *next,
53                 lzma_allocator *allocator, const lzma_filter_info *filters)
54 {
55         // This is always the last filter in the chain.
56         assert(filters[1].init == NULL);
57
58         if (next->coder == NULL) {
59                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
60                 if (next->coder == NULL)
61                         return LZMA_MEM_ERROR;
62
63                 next->code = &helper_decode;
64                 next->end = &helper_end;
65         }
66
67         next->coder->options = filters[0].options;
68
69         return LZMA_OK;
70 }