]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/xz/src/liblzma/common/filter_decoder.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / xz / src / liblzma / common / filter_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       filter_decoder.c
4 /// \brief      Filter ID mapping to filter-specific functions
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_decoder.h"
14 #include "filter_common.h"
15 #include "lzma_decoder.h"
16 #include "lzma2_decoder.h"
17 #include "subblock_decoder.h"
18 #include "subblock_decoder_helper.h"
19 #include "simple_decoder.h"
20 #include "delta_decoder.h"
21
22
23 typedef struct {
24         /// Filter ID
25         lzma_vli id;
26
27         /// Initializes the filter encoder and calls lzma_next_filter_init()
28         /// for filters + 1.
29         lzma_init_function init;
30
31         /// Calculates memory usage of the encoder. If the options are
32         /// invalid, UINT64_MAX is returned.
33         uint64_t (*memusage)(const void *options);
34
35         /// Decodes Filter Properties.
36         ///
37         /// \return     - LZMA_OK: Properties decoded successfully.
38         ///             - LZMA_OPTIONS_ERROR: Unsupported properties
39         ///             - LZMA_MEM_ERROR: Memory allocation failed.
40         lzma_ret (*props_decode)(void **options, lzma_allocator *allocator,
41                         const uint8_t *props, size_t props_size);
42
43 } lzma_filter_decoder;
44
45
46 static const lzma_filter_decoder decoders[] = {
47 #ifdef HAVE_DECODER_LZMA1
48         {
49                 .id = LZMA_FILTER_LZMA1,
50                 .init = &lzma_lzma_decoder_init,
51                 .memusage = &lzma_lzma_decoder_memusage,
52                 .props_decode = &lzma_lzma_props_decode,
53         },
54 #endif
55 #ifdef HAVE_DECODER_LZMA2
56         {
57                 .id = LZMA_FILTER_LZMA2,
58                 .init = &lzma_lzma2_decoder_init,
59                 .memusage = &lzma_lzma2_decoder_memusage,
60                 .props_decode = &lzma_lzma2_props_decode,
61         },
62 #endif
63 #ifdef HAVE_DECODER_SUBBLOCK
64         {
65                 .id = LZMA_FILTER_SUBBLOCK,
66                 .init = &lzma_subblock_decoder_init,
67 //              .memusage = &lzma_subblock_decoder_memusage,
68                 .props_decode = NULL,
69         },
70         {
71                 .id = LZMA_FILTER_SUBBLOCK_HELPER,
72                 .init = &lzma_subblock_decoder_helper_init,
73                 .memusage = NULL,
74                 .props_decode = NULL,
75         },
76 #endif
77 #ifdef HAVE_DECODER_X86
78         {
79                 .id = LZMA_FILTER_X86,
80                 .init = &lzma_simple_x86_decoder_init,
81                 .memusage = NULL,
82                 .props_decode = &lzma_simple_props_decode,
83         },
84 #endif
85 #ifdef HAVE_DECODER_POWERPC
86         {
87                 .id = LZMA_FILTER_POWERPC,
88                 .init = &lzma_simple_powerpc_decoder_init,
89                 .memusage = NULL,
90                 .props_decode = &lzma_simple_props_decode,
91         },
92 #endif
93 #ifdef HAVE_DECODER_IA64
94         {
95                 .id = LZMA_FILTER_IA64,
96                 .init = &lzma_simple_ia64_decoder_init,
97                 .memusage = NULL,
98                 .props_decode = &lzma_simple_props_decode,
99         },
100 #endif
101 #ifdef HAVE_DECODER_ARM
102         {
103                 .id = LZMA_FILTER_ARM,
104                 .init = &lzma_simple_arm_decoder_init,
105                 .memusage = NULL,
106                 .props_decode = &lzma_simple_props_decode,
107         },
108 #endif
109 #ifdef HAVE_DECODER_ARMTHUMB
110         {
111                 .id = LZMA_FILTER_ARMTHUMB,
112                 .init = &lzma_simple_armthumb_decoder_init,
113                 .memusage = NULL,
114                 .props_decode = &lzma_simple_props_decode,
115         },
116 #endif
117 #ifdef HAVE_DECODER_SPARC
118         {
119                 .id = LZMA_FILTER_SPARC,
120                 .init = &lzma_simple_sparc_decoder_init,
121                 .memusage = NULL,
122                 .props_decode = &lzma_simple_props_decode,
123         },
124 #endif
125 #ifdef HAVE_DECODER_DELTA
126         {
127                 .id = LZMA_FILTER_DELTA,
128                 .init = &lzma_delta_decoder_init,
129                 .memusage = &lzma_delta_coder_memusage,
130                 .props_decode = &lzma_delta_props_decode,
131         },
132 #endif
133 };
134
135
136 static const lzma_filter_decoder *
137 decoder_find(lzma_vli id)
138 {
139         for (size_t i = 0; i < ARRAY_SIZE(decoders); ++i)
140                 if (decoders[i].id == id)
141                         return decoders + i;
142
143         return NULL;
144 }
145
146
147 extern LZMA_API(lzma_bool)
148 lzma_filter_decoder_is_supported(lzma_vli id)
149 {
150         return decoder_find(id) != NULL;
151 }
152
153
154 extern lzma_ret
155 lzma_raw_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
156                 const lzma_filter *options)
157 {
158         return lzma_raw_coder_init(next, allocator,
159                         options, (lzma_filter_find)(&decoder_find), false);
160 }
161
162
163 extern LZMA_API(lzma_ret)
164 lzma_raw_decoder(lzma_stream *strm, const lzma_filter *options)
165 {
166         lzma_next_strm_init(lzma_raw_decoder_init, strm, options);
167
168         strm->internal->supported_actions[LZMA_RUN] = true;
169         strm->internal->supported_actions[LZMA_FINISH] = true;
170
171         return LZMA_OK;
172 }
173
174
175 extern LZMA_API(uint64_t)
176 lzma_raw_decoder_memusage(const lzma_filter *filters)
177 {
178         return lzma_raw_coder_memusage(
179                         (lzma_filter_find)(&decoder_find), filters);
180 }
181
182
183 extern LZMA_API(lzma_ret)
184 lzma_properties_decode(lzma_filter *filter, lzma_allocator *allocator,
185                 const uint8_t *props, size_t props_size)
186 {
187         // Make it always NULL so that the caller can always safely free() it.
188         filter->options = NULL;
189
190         const lzma_filter_decoder *const fd = decoder_find(filter->id);
191         if (fd == NULL)
192                 return LZMA_OPTIONS_ERROR;
193
194         if (fd->props_decode == NULL)
195                 return props_size == 0 ? LZMA_OK : LZMA_OPTIONS_ERROR;
196
197         return fd->props_decode(
198                         &filter->options, allocator, props, props_size);
199 }