]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/xz/src/liblzma/common/filter_encoder.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / xz / src / liblzma / common / filter_encoder.c
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 "subblock_encoder.h"
18 #include "simple_encoder.h"
19 #include "delta_encoder.h"
20
21
22 typedef struct {
23         /// Filter ID
24         lzma_vli id;
25
26         /// Initializes the filter encoder and calls lzma_next_filter_init()
27         /// for filters + 1.
28         lzma_init_function init;
29
30         /// Calculates memory usage of the encoder. If the options are
31         /// invalid, UINT64_MAX is returned.
32         uint64_t (*memusage)(const void *options);
33
34         /// Calculates the minimum sane size for Blocks (or other types of
35         /// chunks) to which the input data can be split to make
36         /// multithreaded encoding possible. If this is NULL, it is assumed
37         /// that the encoder is fast enough with single thread.
38         lzma_vli (*chunk_size)(const void *options);
39
40         /// Tells the size of the Filter Properties field. If options are
41         /// invalid, UINT32_MAX is returned. If this is NULL, props_size_fixed
42         /// is used.
43         lzma_ret (*props_size_get)(uint32_t *size, const void *options);
44         uint32_t props_size_fixed;
45
46         /// Encodes Filter Properties.
47         ///
48         /// \return     - LZMA_OK: Properties encoded successfully.
49         ///             - LZMA_OPTIONS_ERROR: Unsupported options
50         ///             - LZMA_PROG_ERROR: Invalid options or not enough
51         ///               output space
52         lzma_ret (*props_encode)(const void *options, uint8_t *out);
53
54 } lzma_filter_encoder;
55
56
57 static const lzma_filter_encoder encoders[] = {
58 #ifdef HAVE_ENCODER_LZMA1
59         {
60                 .id = LZMA_FILTER_LZMA1,
61                 .init = &lzma_lzma_encoder_init,
62                 .memusage = &lzma_lzma_encoder_memusage,
63                 .chunk_size = NULL, // FIXME
64                 .props_size_get = NULL,
65                 .props_size_fixed = 5,
66                 .props_encode = &lzma_lzma_props_encode,
67         },
68 #endif
69 #ifdef HAVE_ENCODER_LZMA2
70         {
71                 .id = LZMA_FILTER_LZMA2,
72                 .init = &lzma_lzma2_encoder_init,
73                 .memusage = &lzma_lzma2_encoder_memusage,
74                 .chunk_size = NULL, // FIXME
75                 .props_size_get = NULL,
76                 .props_size_fixed = 1,
77                 .props_encode = &lzma_lzma2_props_encode,
78         },
79 #endif
80 #ifdef HAVE_ENCODER_SUBBLOCK
81         {
82                 .id = LZMA_FILTER_SUBBLOCK,
83                 .init = &lzma_subblock_encoder_init,
84 //              .memusage = &lzma_subblock_encoder_memusage,
85                 .chunk_size = NULL,
86                 .props_size_get = NULL,
87                 .props_size_fixed = 0,
88                 .props_encode = NULL,
89         },
90 #endif
91 #ifdef HAVE_ENCODER_X86
92         {
93                 .id = LZMA_FILTER_X86,
94                 .init = &lzma_simple_x86_encoder_init,
95                 .memusage = NULL,
96                 .chunk_size = NULL,
97                 .props_size_get = &lzma_simple_props_size,
98                 .props_encode = &lzma_simple_props_encode,
99         },
100 #endif
101 #ifdef HAVE_ENCODER_POWERPC
102         {
103                 .id = LZMA_FILTER_POWERPC,
104                 .init = &lzma_simple_powerpc_encoder_init,
105                 .memusage = NULL,
106                 .chunk_size = NULL,
107                 .props_size_get = &lzma_simple_props_size,
108                 .props_encode = &lzma_simple_props_encode,
109         },
110 #endif
111 #ifdef HAVE_ENCODER_IA64
112         {
113                 .id = LZMA_FILTER_IA64,
114                 .init = &lzma_simple_ia64_encoder_init,
115                 .memusage = NULL,
116                 .chunk_size = NULL,
117                 .props_size_get = &lzma_simple_props_size,
118                 .props_encode = &lzma_simple_props_encode,
119         },
120 #endif
121 #ifdef HAVE_ENCODER_ARM
122         {
123                 .id = LZMA_FILTER_ARM,
124                 .init = &lzma_simple_arm_encoder_init,
125                 .memusage = NULL,
126                 .chunk_size = NULL,
127                 .props_size_get = &lzma_simple_props_size,
128                 .props_encode = &lzma_simple_props_encode,
129         },
130 #endif
131 #ifdef HAVE_ENCODER_ARMTHUMB
132         {
133                 .id = LZMA_FILTER_ARMTHUMB,
134                 .init = &lzma_simple_armthumb_encoder_init,
135                 .memusage = NULL,
136                 .chunk_size = NULL,
137                 .props_size_get = &lzma_simple_props_size,
138                 .props_encode = &lzma_simple_props_encode,
139         },
140 #endif
141 #ifdef HAVE_ENCODER_SPARC
142         {
143                 .id = LZMA_FILTER_SPARC,
144                 .init = &lzma_simple_sparc_encoder_init,
145                 .memusage = NULL,
146                 .chunk_size = NULL,
147                 .props_size_get = &lzma_simple_props_size,
148                 .props_encode = &lzma_simple_props_encode,
149         },
150 #endif
151 #ifdef HAVE_ENCODER_DELTA
152         {
153                 .id = LZMA_FILTER_DELTA,
154                 .init = &lzma_delta_encoder_init,
155                 .memusage = &lzma_delta_coder_memusage,
156                 .chunk_size = NULL,
157                 .props_size_get = NULL,
158                 .props_size_fixed = 1,
159                 .props_encode = &lzma_delta_props_encode,
160         },
161 #endif
162 };
163
164
165 static const lzma_filter_encoder *
166 encoder_find(lzma_vli id)
167 {
168         for (size_t i = 0; i < ARRAY_SIZE(encoders); ++i)
169                 if (encoders[i].id == id)
170                         return encoders + i;
171
172         return NULL;
173 }
174
175
176 extern LZMA_API(lzma_bool)
177 lzma_filter_encoder_is_supported(lzma_vli id)
178 {
179         return encoder_find(id) != NULL;
180 }
181
182
183 extern LZMA_API(lzma_ret)
184 lzma_filters_update(lzma_stream *strm, const lzma_filter *filters)
185 {
186         if (strm->internal->next.update == NULL)
187                 return LZMA_PROG_ERROR;
188
189         // Validate the filter chain.
190         if (lzma_raw_encoder_memusage(filters) == UINT64_MAX)
191                 return LZMA_OPTIONS_ERROR;
192
193         // The actual filter chain in the encoder is reversed. Some things
194         // still want the normal order chain, so we provide both.
195         size_t count = 1;
196         while (filters[count].id != LZMA_VLI_UNKNOWN)
197                 ++count;
198
199         lzma_filter reversed_filters[LZMA_FILTERS_MAX + 1];
200         for (size_t i = 0; i < count; ++i)
201                 reversed_filters[count - i - 1] = filters[i];
202
203         reversed_filters[count].id = LZMA_VLI_UNKNOWN;
204
205         return strm->internal->next.update(strm->internal->next.coder,
206                         strm->allocator, filters, reversed_filters);
207 }
208
209
210 extern lzma_ret
211 lzma_raw_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
212                 const lzma_filter *options)
213 {
214         return lzma_raw_coder_init(next, allocator,
215                         options, (lzma_filter_find)(&encoder_find), true);
216 }
217
218
219 extern LZMA_API(lzma_ret)
220 lzma_raw_encoder(lzma_stream *strm, const lzma_filter *options)
221 {
222         lzma_next_strm_init(lzma_raw_coder_init, strm, options,
223                         (lzma_filter_find)(&encoder_find), true);
224
225         strm->internal->supported_actions[LZMA_RUN] = true;
226         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
227         strm->internal->supported_actions[LZMA_FINISH] = true;
228
229         return LZMA_OK;
230 }
231
232
233 extern LZMA_API(uint64_t)
234 lzma_raw_encoder_memusage(const lzma_filter *filters)
235 {
236         return lzma_raw_coder_memusage(
237                         (lzma_filter_find)(&encoder_find), filters);
238 }
239
240
241 extern LZMA_API(lzma_vli)
242 lzma_chunk_size(const lzma_filter *filters)
243 {
244         lzma_vli max = 0;
245
246         for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
247                 const lzma_filter_encoder *const fe
248                                 = encoder_find(filters[i].id);
249                 if (fe->chunk_size != NULL) {
250                         const lzma_vli size
251                                         = fe->chunk_size(filters[i].options);
252                         if (size == LZMA_VLI_UNKNOWN)
253                                 return LZMA_VLI_UNKNOWN;
254
255                         if (size > max)
256                                 max = size;
257                 }
258         }
259
260         return max;
261 }
262
263
264 extern LZMA_API(lzma_ret)
265 lzma_properties_size(uint32_t *size, const lzma_filter *filter)
266 {
267         const lzma_filter_encoder *const fe = encoder_find(filter->id);
268         if (fe == NULL) {
269                 // Unknown filter - if the Filter ID is a proper VLI,
270                 // return LZMA_OPTIONS_ERROR instead of LZMA_PROG_ERROR,
271                 // because it's possible that we just don't have support
272                 // compiled in for the requested filter.
273                 return filter->id <= LZMA_VLI_MAX
274                                 ? LZMA_OPTIONS_ERROR : LZMA_PROG_ERROR;
275         }
276
277         if (fe->props_size_get == NULL) {
278                 // No props_size_get() function, use props_size_fixed.
279                 *size = fe->props_size_fixed;
280                 return LZMA_OK;
281         }
282
283         return fe->props_size_get(size, filter->options);
284 }
285
286
287 extern LZMA_API(lzma_ret)
288 lzma_properties_encode(const lzma_filter *filter, uint8_t *props)
289 {
290         const lzma_filter_encoder *const fe = encoder_find(filter->id);
291         if (fe == NULL)
292                 return LZMA_PROG_ERROR;
293
294         if (fe->props_encode == NULL)
295                 return LZMA_OK;
296
297         return fe->props_encode(filter->options, props);
298 }