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
)(void **options
, lzma_allocator
*allocator
,
39 const uint8_t *props
, size_t props_size
);
41 } lzma_filter_decoder
;
44 static const lzma_filter_decoder decoders
[] = {
45 #ifdef HAVE_DECODER_LZMA1
47 .id
= LZMA_FILTER_LZMA1
,
48 .init
= &lzma_lzma_decoder_init
,
49 .memusage
= &lzma_lzma_decoder_memusage
,
50 .props_decode
= &lzma_lzma_props_decode
,
53 #ifdef HAVE_DECODER_LZMA2
55 .id
= LZMA_FILTER_LZMA2
,
56 .init
= &lzma_lzma2_decoder_init
,
57 .memusage
= &lzma_lzma2_decoder_memusage
,
58 .props_decode
= &lzma_lzma2_props_decode
,
61 #ifdef HAVE_DECODER_X86
63 .id
= LZMA_FILTER_X86
,
64 .init
= &lzma_simple_x86_decoder_init
,
66 .props_decode
= &lzma_simple_props_decode
,
69 #ifdef HAVE_DECODER_POWERPC
71 .id
= LZMA_FILTER_POWERPC
,
72 .init
= &lzma_simple_powerpc_decoder_init
,
74 .props_decode
= &lzma_simple_props_decode
,
77 #ifdef HAVE_DECODER_IA64
79 .id
= LZMA_FILTER_IA64
,
80 .init
= &lzma_simple_ia64_decoder_init
,
82 .props_decode
= &lzma_simple_props_decode
,
85 #ifdef HAVE_DECODER_ARM
87 .id
= LZMA_FILTER_ARM
,
88 .init
= &lzma_simple_arm_decoder_init
,
90 .props_decode
= &lzma_simple_props_decode
,
93 #ifdef HAVE_DECODER_ARMTHUMB
95 .id
= LZMA_FILTER_ARMTHUMB
,
96 .init
= &lzma_simple_armthumb_decoder_init
,
98 .props_decode
= &lzma_simple_props_decode
,
101 #ifdef HAVE_DECODER_SPARC
103 .id
= LZMA_FILTER_SPARC
,
104 .init
= &lzma_simple_sparc_decoder_init
,
106 .props_decode
= &lzma_simple_props_decode
,
109 #ifdef HAVE_DECODER_DELTA
111 .id
= LZMA_FILTER_DELTA
,
112 .init
= &lzma_delta_decoder_init
,
113 .memusage
= &lzma_delta_coder_memusage
,
114 .props_decode
= &lzma_delta_props_decode
,
120 static const lzma_filter_decoder
*
121 decoder_find(lzma_vli id
)
123 for (size_t i
= 0; i
< ARRAY_SIZE(decoders
); ++i
)
124 if (decoders
[i
].id
== id
)
131 extern LZMA_API(lzma_bool
)
132 lzma_filter_decoder_is_supported(lzma_vli id
)
134 return decoder_find(id
) != NULL
;
139 lzma_raw_decoder_init(lzma_next_coder
*next
, lzma_allocator
*allocator
,
140 const lzma_filter
*options
)
142 return lzma_raw_coder_init(next
, allocator
,
143 options
, (lzma_filter_find
)(&decoder_find
), false);
147 extern LZMA_API(lzma_ret
)
148 lzma_raw_decoder(lzma_stream
*strm
, const lzma_filter
*options
)
150 lzma_next_strm_init(lzma_raw_decoder_init
, strm
, options
);
152 strm
->internal
->supported_actions
[LZMA_RUN
] = true;
153 strm
->internal
->supported_actions
[LZMA_FINISH
] = true;
159 extern LZMA_API(uint64_t)
160 lzma_raw_decoder_memusage(const lzma_filter
*filters
)
162 return lzma_raw_coder_memusage(
163 (lzma_filter_find
)(&decoder_find
), filters
);
167 extern LZMA_API(lzma_ret
)
168 lzma_properties_decode(lzma_filter
*filter
, lzma_allocator
*allocator
,
169 const uint8_t *props
, size_t props_size
)
171 // Make it always NULL so that the caller can always safely free() it.
172 filter
->options
= NULL
;
174 const lzma_filter_decoder
*const fd
= decoder_find(filter
->id
);
176 return LZMA_OPTIONS_ERROR
;
178 if (fd
->props_decode
== NULL
)
179 return props_size
== 0 ? LZMA_OK
: LZMA_OPTIONS_ERROR
;
181 return fd
->props_decode(
182 &filter
->options
, allocator
, props
, props_size
);