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_encoder.h"
14 #include "filter_common.h"
15 #include "lzma_encoder.h"
16 #include "lzma2_encoder.h"
17 #include "simple_encoder.h"
18 #include "delta_encoder.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 /// Calculates the recommended Uncompressed Size for .xz Blocks to
34 /// which the input data can be split to make multithreaded
35 /// encoding possible. If this is NULL, it is assumed that
36 /// the encoder is fast enough with single thread. If the options
37 /// are invalid, UINT64_MAX is returned.
38 uint64_t (*block_size
)(const void *options
);
40 /// Tells the size of the Filter Properties field. If options are
41 /// invalid, LZMA_OPTIONS_ERROR is returned and size is set to
43 lzma_ret (*props_size_get
)(uint32_t *size
, const void *options
);
45 /// Some filters will always have the same size Filter Properties
46 /// field. If props_size_get is NULL, this value is used.
47 uint32_t props_size_fixed
;
49 /// Encodes Filter Properties.
51 /// \return - LZMA_OK: Properties encoded successfully.
52 /// - LZMA_OPTIONS_ERROR: Unsupported options
53 /// - LZMA_PROG_ERROR: Invalid options or not enough
55 lzma_ret (*props_encode
)(const void *options
, uint8_t *out
);
57 } lzma_filter_encoder
;
60 static const lzma_filter_encoder encoders
[] = {
61 #ifdef HAVE_ENCODER_LZMA1
63 .id
= LZMA_FILTER_LZMA1
,
64 .init
= &lzma_lzma_encoder_init
,
65 .memusage
= &lzma_lzma_encoder_memusage
,
66 .block_size
= NULL
, // Not needed for LZMA1
67 .props_size_get
= NULL
,
68 .props_size_fixed
= 5,
69 .props_encode
= &lzma_lzma_props_encode
,
72 .id
= LZMA_FILTER_LZMA1EXT
,
73 .init
= &lzma_lzma_encoder_init
,
74 .memusage
= &lzma_lzma_encoder_memusage
,
75 .block_size
= NULL
, // Not needed for LZMA1
76 .props_size_get
= NULL
,
77 .props_size_fixed
= 5,
78 .props_encode
= &lzma_lzma_props_encode
,
81 #ifdef HAVE_ENCODER_LZMA2
83 .id
= LZMA_FILTER_LZMA2
,
84 .init
= &lzma_lzma2_encoder_init
,
85 .memusage
= &lzma_lzma2_encoder_memusage
,
86 .block_size
= &lzma_lzma2_block_size
,
87 .props_size_get
= NULL
,
88 .props_size_fixed
= 1,
89 .props_encode
= &lzma_lzma2_props_encode
,
92 #ifdef HAVE_ENCODER_X86
94 .id
= LZMA_FILTER_X86
,
95 .init
= &lzma_simple_x86_encoder_init
,
98 .props_size_get
= &lzma_simple_props_size
,
99 .props_encode
= &lzma_simple_props_encode
,
102 #ifdef HAVE_ENCODER_POWERPC
104 .id
= LZMA_FILTER_POWERPC
,
105 .init
= &lzma_simple_powerpc_encoder_init
,
108 .props_size_get
= &lzma_simple_props_size
,
109 .props_encode
= &lzma_simple_props_encode
,
112 #ifdef HAVE_ENCODER_IA64
114 .id
= LZMA_FILTER_IA64
,
115 .init
= &lzma_simple_ia64_encoder_init
,
118 .props_size_get
= &lzma_simple_props_size
,
119 .props_encode
= &lzma_simple_props_encode
,
122 #ifdef HAVE_ENCODER_ARM
124 .id
= LZMA_FILTER_ARM
,
125 .init
= &lzma_simple_arm_encoder_init
,
128 .props_size_get
= &lzma_simple_props_size
,
129 .props_encode
= &lzma_simple_props_encode
,
132 #ifdef HAVE_ENCODER_ARMTHUMB
134 .id
= LZMA_FILTER_ARMTHUMB
,
135 .init
= &lzma_simple_armthumb_encoder_init
,
138 .props_size_get
= &lzma_simple_props_size
,
139 .props_encode
= &lzma_simple_props_encode
,
142 #ifdef HAVE_ENCODER_ARM64
144 .id
= LZMA_FILTER_ARM64
,
145 .init
= &lzma_simple_arm64_encoder_init
,
148 .props_size_get
= &lzma_simple_props_size
,
149 .props_encode
= &lzma_simple_props_encode
,
152 #ifdef HAVE_ENCODER_SPARC
154 .id
= LZMA_FILTER_SPARC
,
155 .init
= &lzma_simple_sparc_encoder_init
,
158 .props_size_get
= &lzma_simple_props_size
,
159 .props_encode
= &lzma_simple_props_encode
,
162 #ifdef HAVE_ENCODER_RISCV
164 .id
= LZMA_FILTER_RISCV
,
165 .init
= &lzma_simple_riscv_encoder_init
,
168 .props_size_get
= &lzma_simple_props_size
,
169 .props_encode
= &lzma_simple_props_encode
,
172 #ifdef HAVE_ENCODER_DELTA
174 .id
= LZMA_FILTER_DELTA
,
175 .init
= &lzma_delta_encoder_init
,
176 .memusage
= &lzma_delta_coder_memusage
,
178 .props_size_get
= NULL
,
179 .props_size_fixed
= 1,
180 .props_encode
= &lzma_delta_props_encode
,
186 static const lzma_filter_encoder
*
187 encoder_find(lzma_vli id
)
189 for (size_t i
= 0; i
< ARRAY_SIZE(encoders
); ++i
)
190 if (encoders
[i
].id
== id
)
197 extern LZMA_API(lzma_bool
)
198 lzma_filter_encoder_is_supported(lzma_vli id
)
200 return encoder_find(id
) != NULL
;
204 extern LZMA_API(lzma_ret
)
205 lzma_filters_update(lzma_stream
*strm
, const lzma_filter
*filters
)
207 if (strm
->internal
->next
.update
== NULL
)
208 return LZMA_PROG_ERROR
;
210 // Validate the filter chain.
211 if (lzma_raw_encoder_memusage(filters
) == UINT64_MAX
)
212 return LZMA_OPTIONS_ERROR
;
214 // The actual filter chain in the encoder is reversed. Some things
215 // still want the normal order chain, so we provide both.
217 while (filters
[count
].id
!= LZMA_VLI_UNKNOWN
)
220 lzma_filter reversed_filters
[LZMA_FILTERS_MAX
+ 1];
221 for (size_t i
= 0; i
< count
; ++i
)
222 reversed_filters
[count
- i
- 1] = filters
[i
];
224 reversed_filters
[count
].id
= LZMA_VLI_UNKNOWN
;
226 return strm
->internal
->next
.update(strm
->internal
->next
.coder
,
227 strm
->allocator
, filters
, reversed_filters
);
232 lzma_raw_encoder_init(lzma_next_coder
*next
, const lzma_allocator
*allocator
,
233 const lzma_filter
*filters
)
235 return lzma_raw_coder_init(next
, allocator
,
236 filters
, (lzma_filter_find
)(&encoder_find
), true);
240 extern LZMA_API(lzma_ret
)
241 lzma_raw_encoder(lzma_stream
*strm
, const lzma_filter
*filters
)
243 lzma_next_strm_init(lzma_raw_coder_init
, strm
, filters
,
244 (lzma_filter_find
)(&encoder_find
), true);
246 strm
->internal
->supported_actions
[LZMA_RUN
] = true;
247 strm
->internal
->supported_actions
[LZMA_SYNC_FLUSH
] = true;
248 strm
->internal
->supported_actions
[LZMA_FINISH
] = true;
254 extern LZMA_API(uint64_t)
255 lzma_raw_encoder_memusage(const lzma_filter
*filters
)
257 return lzma_raw_coder_memusage(
258 (lzma_filter_find
)(&encoder_find
), filters
);
262 extern LZMA_API(uint64_t)
263 lzma_mt_block_size(const lzma_filter
*filters
)
270 for (size_t i
= 0; filters
[i
].id
!= LZMA_VLI_UNKNOWN
; ++i
) {
271 const lzma_filter_encoder
*const fe
272 = encoder_find(filters
[i
].id
);
276 if (fe
->block_size
!= NULL
) {
278 = fe
->block_size(filters
[i
].options
);
284 return max
== 0 ? UINT64_MAX
: max
;
288 extern LZMA_API(lzma_ret
)
289 lzma_properties_size(uint32_t *size
, const lzma_filter
*filter
)
291 const lzma_filter_encoder
*const fe
= encoder_find(filter
->id
);
293 // Unknown filter - if the Filter ID is a proper VLI,
294 // return LZMA_OPTIONS_ERROR instead of LZMA_PROG_ERROR,
295 // because it's possible that we just don't have support
296 // compiled in for the requested filter.
297 return filter
->id
<= LZMA_VLI_MAX
298 ? LZMA_OPTIONS_ERROR
: LZMA_PROG_ERROR
;
301 if (fe
->props_size_get
== NULL
) {
302 // No props_size_get() function, use props_size_fixed.
303 *size
= fe
->props_size_fixed
;
307 return fe
->props_size_get(size
, filter
->options
);
311 extern LZMA_API(lzma_ret
)
312 lzma_properties_encode(const lzma_filter
*filter
, uint8_t *props
)
314 const lzma_filter_encoder
*const fe
= encoder_find(filter
->id
);
316 return LZMA_PROG_ERROR
;
318 if (fe
->props_encode
== NULL
)
321 return fe
->props_encode(filter
->options
, props
);