1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file filter_decoder.c
4 /// \brief Filter ID mapping to filter-specific functions
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
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"
27 /// Initializes the filter encoder and calls lzma_next_filter_init()
29 lzma_init_function init;
31 /// Calculates memory usage of the encoder. If the options are
32 /// invalid, UINT64_MAX is returned.
33 uint64_t (*memusage)(const void *options);
35 /// Decodes Filter Properties.
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);
43 } lzma_filter_decoder;
46 static const lzma_filter_decoder decoders[] = {
47 #ifdef HAVE_DECODER_LZMA1
49 .id = LZMA_FILTER_LZMA1,
50 .init = &lzma_lzma_decoder_init,
51 .memusage = &lzma_lzma_decoder_memusage,
52 .props_decode = &lzma_lzma_props_decode,
55 #ifdef HAVE_DECODER_LZMA2
57 .id = LZMA_FILTER_LZMA2,
58 .init = &lzma_lzma2_decoder_init,
59 .memusage = &lzma_lzma2_decoder_memusage,
60 .props_decode = &lzma_lzma2_props_decode,
63 #ifdef HAVE_DECODER_SUBBLOCK
65 .id = LZMA_FILTER_SUBBLOCK,
66 .init = &lzma_subblock_decoder_init,
67 // .memusage = &lzma_subblock_decoder_memusage,
71 .id = LZMA_FILTER_SUBBLOCK_HELPER,
72 .init = &lzma_subblock_decoder_helper_init,
77 #ifdef HAVE_DECODER_X86
79 .id = LZMA_FILTER_X86,
80 .init = &lzma_simple_x86_decoder_init,
82 .props_decode = &lzma_simple_props_decode,
85 #ifdef HAVE_DECODER_POWERPC
87 .id = LZMA_FILTER_POWERPC,
88 .init = &lzma_simple_powerpc_decoder_init,
90 .props_decode = &lzma_simple_props_decode,
93 #ifdef HAVE_DECODER_IA64
95 .id = LZMA_FILTER_IA64,
96 .init = &lzma_simple_ia64_decoder_init,
98 .props_decode = &lzma_simple_props_decode,
101 #ifdef HAVE_DECODER_ARM
103 .id = LZMA_FILTER_ARM,
104 .init = &lzma_simple_arm_decoder_init,
106 .props_decode = &lzma_simple_props_decode,
109 #ifdef HAVE_DECODER_ARMTHUMB
111 .id = LZMA_FILTER_ARMTHUMB,
112 .init = &lzma_simple_armthumb_decoder_init,
114 .props_decode = &lzma_simple_props_decode,
117 #ifdef HAVE_DECODER_SPARC
119 .id = LZMA_FILTER_SPARC,
120 .init = &lzma_simple_sparc_decoder_init,
122 .props_decode = &lzma_simple_props_decode,
125 #ifdef HAVE_DECODER_DELTA
127 .id = LZMA_FILTER_DELTA,
128 .init = &lzma_delta_decoder_init,
129 .memusage = &lzma_delta_coder_memusage,
130 .props_decode = &lzma_delta_props_decode,
136 static const lzma_filter_decoder *
137 decoder_find(lzma_vli id)
139 for (size_t i = 0; i < ARRAY_SIZE(decoders); ++i)
140 if (decoders[i].id == id)
147 extern LZMA_API(lzma_bool)
148 lzma_filter_decoder_is_supported(lzma_vli id)
150 return decoder_find(id) != NULL;
155 lzma_raw_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
156 const lzma_filter *options)
158 return lzma_raw_coder_init(next, allocator,
159 options, (lzma_filter_find)(&decoder_find), false);
163 extern LZMA_API(lzma_ret)
164 lzma_raw_decoder(lzma_stream *strm, const lzma_filter *options)
166 lzma_next_strm_init(lzma_raw_decoder_init, strm, options);
168 strm->internal->supported_actions[LZMA_RUN] = true;
169 strm->internal->supported_actions[LZMA_FINISH] = true;
175 extern LZMA_API(uint64_t)
176 lzma_raw_decoder_memusage(const lzma_filter *filters)
178 return lzma_raw_coder_memusage(
179 (lzma_filter_find)(&decoder_find), filters);
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)
187 // Make it always NULL so that the caller can always safely free() it.
188 filter->options = NULL;
190 const lzma_filter_decoder *const fd = decoder_find(filter->id);
192 return LZMA_OPTIONS_ERROR;
194 if (fd->props_decode == NULL)
195 return props_size == 0 ? LZMA_OK : LZMA_OPTIONS_ERROR;
197 return fd->props_decode(
198 &filter->options, allocator, props, props_size);