]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/filter_flags_encoder.c
Sort of garbage collection commit. :-| Many things are still
[icculus/xz.git] / src / liblzma / common / filter_flags_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       filter_flags_encoder.c
4 /// \brief      Decodes a Filter Flags field
5 //
6 //  Copyright (C) 2007 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_filter_flags_size(uint32_t *size, const lzma_filter *filter)
25 {
26         return_if_error(lzma_properties_size(size, filter));
27
28         // lzma_properties_size() validates the Filter ID as a side-effect,
29         // so we know that it is a valid VLI.
30         *size += lzma_vli_size(filter->id) + lzma_vli_size(*size);
31
32         return LZMA_OK;
33 }
34
35
36 extern LZMA_API lzma_ret
37 lzma_filter_flags_encode(const lzma_filter *filter,
38                 uint8_t *out, size_t *out_pos, size_t out_size)
39 {
40         // Filter ID
41         if (filter->id >= LZMA_FILTER_RESERVED_START)
42                 return LZMA_HEADER_ERROR;
43
44         return_if_error(lzma_vli_encode(filter->id, NULL,
45                         out, out_pos, out_size));
46
47         // Size of Properties
48         uint32_t props_size;
49         return_if_error(lzma_properties_size(&props_size, filter));
50         return_if_error(lzma_vli_encode(props_size, NULL,
51                         out, out_pos, out_size));
52
53         // Filter Properties
54         if (out_size - *out_pos < props_size)
55                 return LZMA_PROG_ERROR;
56
57         return_if_error(lzma_properties_encode(filter, out + *out_pos));
58
59         *out_pos += props_size;
60
61         return LZMA_OK;
62 }