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 "simple_decoder.h"
18 #include "delta_decoder.h"
25 /// Initializes the filter encoder and calls lzma_next_filter_init()
27 lzma_init_function init;
29 /// Calculates memory usage of the encoder. If the options are
30 /// invalid, UINT64_MAX is returned.
31 uint64_t (*memusage)(const void *options);
33 /// Decodes Filter Properties.
35 /// \return - LZMA_OK: Properties decoded successfully.
36 /// - LZMA_OPTIONS_ERROR: Unsupported properties
37 /// - LZMA_MEM_ERROR: Memory allocation failed.
38 lzma_ret (*props_decode)(
39 void **options, const lzma_allocator *allocator,
40 const uint8_t *props, size_t props_size);
42 } lzma_filter_decoder;
45 static const lzma_filter_decoder decoders[] = {
46 #ifdef HAVE_DECODER_LZMA1
48 .id = LZMA_FILTER_LZMA1,
49 .init = &lzma_lzma_decoder_init,
50 .memusage = &lzma_lzma_decoder_memusage,
51 .props_decode = &lzma_lzma_props_decode,
54 #ifdef HAVE_DECODER_LZMA2
56 .id = LZMA_FILTER_LZMA2,
57 .init = &lzma_lzma2_decoder_init,
58 .memusage = &lzma_lzma2_decoder_memusage,
59 .props_decode = &lzma_lzma2_props_decode,
62 #ifdef HAVE_DECODER_X86
64 .id = LZMA_FILTER_X86,
65 .init = &lzma_simple_x86_decoder_init,
67 .props_decode = &lzma_simple_props_decode,
70 #ifdef HAVE_DECODER_POWERPC
72 .id = LZMA_FILTER_POWERPC,
73 .init = &lzma_simple_powerpc_decoder_init,
75 .props_decode = &lzma_simple_props_decode,
78 #ifdef HAVE_DECODER_IA64
80 .id = LZMA_FILTER_IA64,
81 .init = &lzma_simple_ia64_decoder_init,
83 .props_decode = &lzma_simple_props_decode,
86 #ifdef HAVE_DECODER_ARM
88 .id = LZMA_FILTER_ARM,
89 .init = &lzma_simple_arm_decoder_init,
91 .props_decode = &lzma_simple_props_decode,
94 #ifdef HAVE_DECODER_ARMTHUMB
96 .id = LZMA_FILTER_ARMTHUMB,
97 .init = &lzma_simple_armthumb_decoder_init,
99 .props_decode = &lzma_simple_props_decode,
102 #ifdef HAVE_DECODER_SPARC
104 .id = LZMA_FILTER_SPARC,
105 .init = &lzma_simple_sparc_decoder_init,
107 .props_decode = &lzma_simple_props_decode,
110 #ifdef HAVE_DECODER_DELTA
112 .id = LZMA_FILTER_DELTA,
113 .init = &lzma_delta_decoder_init,
114 .memusage = &lzma_delta_coder_memusage,
115 .props_decode = &lzma_delta_props_decode,
121 static const lzma_filter_decoder *
122 decoder_find(lzma_vli id)
124 for (size_t i = 0; i < ARRAY_SIZE(decoders); ++i)
125 if (decoders[i].id == id)
132 extern LZMA_API(lzma_bool)
133 lzma_filter_decoder_is_supported(lzma_vli id)
135 return decoder_find(id) != NULL;
140 lzma_raw_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
141 const lzma_filter *options)
143 return lzma_raw_coder_init(next, allocator,
144 options, (lzma_filter_find)(&decoder_find), false);
148 extern LZMA_API(lzma_ret)
149 lzma_raw_decoder(lzma_stream *strm, const lzma_filter *options)
151 lzma_next_strm_init(lzma_raw_decoder_init, strm, options);
153 strm->internal->supported_actions[LZMA_RUN] = true;
154 strm->internal->supported_actions[LZMA_FINISH] = true;
160 extern LZMA_API(uint64_t)
161 lzma_raw_decoder_memusage(const lzma_filter *filters)
163 return lzma_raw_coder_memusage(
164 (lzma_filter_find)(&decoder_find), filters);
168 extern LZMA_API(lzma_ret)
169 lzma_properties_decode(lzma_filter *filter, const lzma_allocator *allocator,
170 const uint8_t *props, size_t props_size)
172 // Make it always NULL so that the caller can always safely free() it.
173 filter->options = NULL;
175 const lzma_filter_decoder *const fd = decoder_find(filter->id);
177 return LZMA_OPTIONS_ERROR;
179 if (fd->props_decode == NULL)
180 return props_size == 0 ? LZMA_OK : LZMA_OPTIONS_ERROR;
182 return fd->props_decode(
183 &filter->options, allocator, props, props_size);