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_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"
19
20
21 typedef struct {
22 /// Filter ID
23 lzma_vli id;
24
25 /// Initializes the filter encoder and calls lzma_next_filter_init()
26 /// for filters + 1.
27 lzma_init_function init;
28
29 /// Calculates memory usage of the encoder. If the options are
30 /// invalid, UINT64_MAX is returned.
31 uint64_t (*memusage)(const void *options);
32
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);
39
40 /// Tells the size of the Filter Properties field. If options are
41 /// invalid, LZMA_OPTIONS_ERROR is returned and size is set to
42 /// UINT32_MAX.
43 lzma_ret (*props_size_get)(uint32_t *size, const void *options);
44
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;
48
49 /// Encodes Filter Properties.
50 ///
51 /// \return - LZMA_OK: Properties encoded successfully.
52 /// - LZMA_OPTIONS_ERROR: Unsupported options
53 /// - LZMA_PROG_ERROR: Invalid options or not enough
54 /// output space
55 lzma_ret (*props_encode)(const void *options, uint8_t *out);
56
57 } lzma_filter_encoder;
58
59
60 static const lzma_filter_encoder encoders[] = {
61 #ifdef HAVE_ENCODER_LZMA1
62 {
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,
70 },
71 {
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,
79 },
80 #endif
81 #ifdef HAVE_ENCODER_LZMA2
82 {
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,
90 },
91 #endif
92 #ifdef HAVE_ENCODER_X86
93 {
94 .id = LZMA_FILTER_X86,
95 .init = &lzma_simple_x86_encoder_init,
96 .memusage = NULL,
97 .block_size = NULL,
98 .props_size_get = &lzma_simple_props_size,
99 .props_encode = &lzma_simple_props_encode,
100 },
101 #endif
102 #ifdef HAVE_ENCODER_POWERPC
103 {
104 .id = LZMA_FILTER_POWERPC,
105 .init = &lzma_simple_powerpc_encoder_init,
106 .memusage = NULL,
107 .block_size = NULL,
108 .props_size_get = &lzma_simple_props_size,
109 .props_encode = &lzma_simple_props_encode,
110 },
111 #endif
112 #ifdef HAVE_ENCODER_IA64
113 {
114 .id = LZMA_FILTER_IA64,
115 .init = &lzma_simple_ia64_encoder_init,
116 .memusage = NULL,
117 .block_size = NULL,
118 .props_size_get = &lzma_simple_props_size,
119 .props_encode = &lzma_simple_props_encode,
120 },
121 #endif
122 #ifdef HAVE_ENCODER_ARM
123 {
124 .id = LZMA_FILTER_ARM,
125 .init = &lzma_simple_arm_encoder_init,
126 .memusage = NULL,
127 .block_size = NULL,
128 .props_size_get = &lzma_simple_props_size,
129 .props_encode = &lzma_simple_props_encode,
130 },
131 #endif
132 #ifdef HAVE_ENCODER_ARMTHUMB
133 {
134 .id = LZMA_FILTER_ARMTHUMB,
135 .init = &lzma_simple_armthumb_encoder_init,
136 .memusage = NULL,
137 .block_size = NULL,
138 .props_size_get = &lzma_simple_props_size,
139 .props_encode = &lzma_simple_props_encode,
140 },
141 #endif
142 #ifdef HAVE_ENCODER_ARM64
143 {
144 .id = LZMA_FILTER_ARM64,
145 .init = &lzma_simple_arm64_encoder_init,
146 .memusage = NULL,
147 .block_size = NULL,
148 .props_size_get = &lzma_simple_props_size,
149 .props_encode = &lzma_simple_props_encode,
150 },
151 #endif
152 #ifdef HAVE_ENCODER_SPARC
153 {
154 .id = LZMA_FILTER_SPARC,
155 .init = &lzma_simple_sparc_encoder_init,
156 .memusage = NULL,
157 .block_size = NULL,
158 .props_size_get = &lzma_simple_props_size,
159 .props_encode = &lzma_simple_props_encode,
160 },
161 #endif
162 #ifdef HAVE_ENCODER_DELTA
163 {
164 .id = LZMA_FILTER_DELTA,
165 .init = &lzma_delta_encoder_init,
166 .memusage = &lzma_delta_coder_memusage,
167 .block_size = NULL,
168 .props_size_get = NULL,
169 .props_size_fixed = 1,
170 .props_encode = &lzma_delta_props_encode,
171 },
172 #endif
173 };
174
175
176 static const lzma_filter_encoder *
encoder_find(lzma_vli id)177 encoder_find(lzma_vli id)
178 {
179 for (size_t i = 0; i < ARRAY_SIZE(encoders); ++i)
180 if (encoders[i].id == id)
181 return encoders + i;
182
183 return NULL;
184 }
185
186
187 extern LZMA_API(lzma_bool)
lzma_filter_encoder_is_supported(lzma_vli id)188 lzma_filter_encoder_is_supported(lzma_vli id)
189 {
190 return encoder_find(id) != NULL;
191 }
192
193
194 extern LZMA_API(lzma_ret)
lzma_filters_update(lzma_stream * strm,const lzma_filter * filters)195 lzma_filters_update(lzma_stream *strm, const lzma_filter *filters)
196 {
197 if (strm->internal->next.update == NULL)
198 return LZMA_PROG_ERROR;
199
200 // Validate the filter chain.
201 if (lzma_raw_encoder_memusage(filters) == UINT64_MAX)
202 return LZMA_OPTIONS_ERROR;
203
204 // The actual filter chain in the encoder is reversed. Some things
205 // still want the normal order chain, so we provide both.
206 size_t count = 1;
207 while (filters[count].id != LZMA_VLI_UNKNOWN)
208 ++count;
209
210 lzma_filter reversed_filters[LZMA_FILTERS_MAX + 1];
211 for (size_t i = 0; i < count; ++i)
212 reversed_filters[count - i - 1] = filters[i];
213
214 reversed_filters[count].id = LZMA_VLI_UNKNOWN;
215
216 return strm->internal->next.update(strm->internal->next.coder,
217 strm->allocator, filters, reversed_filters);
218 }
219
220
221 extern lzma_ret
lzma_raw_encoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter * options)222 lzma_raw_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
223 const lzma_filter *options)
224 {
225 return lzma_raw_coder_init(next, allocator,
226 options, (lzma_filter_find)(&encoder_find), true);
227 }
228
229
230 extern LZMA_API(lzma_ret)
lzma_raw_encoder(lzma_stream * strm,const lzma_filter * options)231 lzma_raw_encoder(lzma_stream *strm, const lzma_filter *options)
232 {
233 lzma_next_strm_init(lzma_raw_coder_init, strm, options,
234 (lzma_filter_find)(&encoder_find), true);
235
236 strm->internal->supported_actions[LZMA_RUN] = true;
237 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
238 strm->internal->supported_actions[LZMA_FINISH] = true;
239
240 return LZMA_OK;
241 }
242
243
244 extern LZMA_API(uint64_t)
lzma_raw_encoder_memusage(const lzma_filter * filters)245 lzma_raw_encoder_memusage(const lzma_filter *filters)
246 {
247 return lzma_raw_coder_memusage(
248 (lzma_filter_find)(&encoder_find), filters);
249 }
250
251
252 extern LZMA_API(uint64_t)
lzma_mt_block_size(const lzma_filter * filters)253 lzma_mt_block_size(const lzma_filter *filters)
254 {
255 if (filters == NULL)
256 return UINT64_MAX;
257
258 uint64_t max = 0;
259
260 for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
261 const lzma_filter_encoder *const fe
262 = encoder_find(filters[i].id);
263 if (fe == NULL)
264 return UINT64_MAX;
265
266 if (fe->block_size != NULL) {
267 const uint64_t size
268 = fe->block_size(filters[i].options);
269 if (size > max)
270 max = size;
271 }
272 }
273
274 return max == 0 ? UINT64_MAX : max;
275 }
276
277
278 extern LZMA_API(lzma_ret)
lzma_properties_size(uint32_t * size,const lzma_filter * filter)279 lzma_properties_size(uint32_t *size, const lzma_filter *filter)
280 {
281 const lzma_filter_encoder *const fe = encoder_find(filter->id);
282 if (fe == NULL) {
283 // Unknown filter - if the Filter ID is a proper VLI,
284 // return LZMA_OPTIONS_ERROR instead of LZMA_PROG_ERROR,
285 // because it's possible that we just don't have support
286 // compiled in for the requested filter.
287 return filter->id <= LZMA_VLI_MAX
288 ? LZMA_OPTIONS_ERROR : LZMA_PROG_ERROR;
289 }
290
291 if (fe->props_size_get == NULL) {
292 // No props_size_get() function, use props_size_fixed.
293 *size = fe->props_size_fixed;
294 return LZMA_OK;
295 }
296
297 return fe->props_size_get(size, filter->options);
298 }
299
300
301 extern LZMA_API(lzma_ret)
lzma_properties_encode(const lzma_filter * filter,uint8_t * props)302 lzma_properties_encode(const lzma_filter *filter, uint8_t *props)
303 {
304 const lzma_filter_encoder *const fe = encoder_find(filter->id);
305 if (fe == NULL)
306 return LZMA_PROG_ERROR;
307
308 if (fe->props_encode == NULL)
309 return LZMA_OK;
310
311 return fe->props_encode(filter->options, props);
312 }
313