]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/subblock/subblock_decoder_helper.c
Removed src/liblzma/common/sysdefs.h symlink, which was
[icculus/xz.git] / 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 //  Copyright (C) 2007 Lasse Collin
9 //
10 //  This library is free software; you can redistribute it and/or
11 //  modify it under the terms of the GNU Lesser General Public
12 //  License as published by the Free Software Foundation; either
13 //  version 2.1 of the License, or (at your option) any later version.
14 //
15 //  This library is distributed in the hope that it will be useful,
16 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 //  Lesser General Public License for more details.
19 //
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "subblock_decoder_helper.h"
23
24
25 struct lzma_coder_s {
26         const lzma_options_subblock_helper *options;
27 };
28
29
30 static lzma_ret
31 helper_decode(lzma_coder *coder,
32                 lzma_allocator *allocator lzma_attribute((unused)),
33                 const uint8_t *restrict in, size_t *restrict in_pos,
34                 size_t in_size, uint8_t *restrict out,
35                 size_t *restrict out_pos, size_t out_size,
36                 lzma_action action lzma_attribute((unused)))
37 {
38         // If end_was_reached is true, we cannot have any input.
39         assert(!coder->options->end_was_reached || *in_pos == in_size);
40
41         // We can safely copy as much as possible, because we are never
42         // given more data than a single Subblock Data field.
43         bufcpy(in, in_pos, in_size, out, out_pos, out_size);
44
45         // Return LZMA_STREAM_END when instructed so by the Subblock decoder.
46         return coder->options->end_was_reached ? LZMA_STREAM_END : LZMA_OK;
47 }
48
49
50 static void
51 helper_end(lzma_coder *coder, lzma_allocator *allocator)
52 {
53         lzma_free(coder, allocator);
54         return;
55 }
56
57
58 extern lzma_ret
59 lzma_subblock_decoder_helper_init(lzma_next_coder *next,
60                 lzma_allocator *allocator, const lzma_filter_info *filters)
61 {
62         // This is always the last filter in the chain.
63         assert(filters[1].init == NULL);
64
65         // We never know uncompressed size.
66         assert(filters[0].uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
67
68         if (next->coder == NULL) {
69                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
70                 if (next->coder == NULL)
71                         return LZMA_MEM_ERROR;
72                 
73                 next->code = &helper_decode;
74                 next->end = &helper_end;
75         }
76
77         next->coder->options = filters[0].options;
78
79         return LZMA_OK;
80 }