]> icculus.org git repositories - icculus/xz.git/blob - src/xz/options.c
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / src / xz / options.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       options.c
4 /// \brief      Parser for filter-specific options
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 "private.h"
14
15
16 ///////////////////
17 // Generic stuff //
18 ///////////////////
19
20 typedef struct {
21         const char *name;
22         uint64_t id;
23 } name_id_map;
24
25
26 typedef struct {
27         const char *name;
28         const name_id_map *map;
29         uint64_t min;
30         uint64_t max;
31 } option_map;
32
33
34 /// Parses option=value pairs that are separated with colons, semicolons,
35 /// or commas: opt=val:opt=val;opt=val,opt=val
36 ///
37 /// Each option is a string, that is converted to an integer using the
38 /// index where the option string is in the array.
39 ///
40 /// Value can be either a number with minimum and maximum value limit, or
41 /// a string-id map mapping a list of possible string values to integers.
42 ///
43 /// When parsing both option and value succeed, a filter-specific function
44 /// is called, which should update the given value to filter-specific
45 /// options structure.
46 ///
47 /// \param      str     String containing the options from the command line
48 /// \param      opts    Filter-specific option map
49 /// \param      set     Filter-specific function to update filter_options
50 /// \param      filter_options  Pointer to filter-specific options structure
51 ///
52 /// \return     Returns only if no errors occur.
53 ///
54 static void
55 parse_options(const char *str, const option_map *opts,
56                 void (*set)(void *filter_options,
57                         uint32_t key, uint64_t value),
58                 void *filter_options)
59 {
60         if (str == NULL || str[0] == '\0')
61                 return;
62
63         char *s = xstrdup(str);
64         char *name = s;
65
66         while (true) {
67                 char *split = strchr(name, ',');
68                 if (split != NULL)
69                         *split = '\0';
70
71                 char *value = strchr(name, '=');
72                 if (value != NULL)
73                         *value++ = '\0';
74
75                 if (value == NULL || value[0] == '\0')
76                         message_fatal(_("%s: Options must be `name=value' "
77                                         "pairs separated with commas"), str);
78
79                 // Look for the option name from the option map.
80                 bool found = false;
81                 for (size_t i = 0; opts[i].name != NULL; ++i) {
82                         if (strcmp(name, opts[i].name) != 0)
83                                 continue;
84
85                         if (opts[i].map == NULL) {
86                                 // value is an integer.
87                                 const uint64_t v = str_to_uint64(name, value,
88                                                 opts[i].min, opts[i].max);
89                                 set(filter_options, i, v);
90                         } else {
91                                 // value is a string which we should map
92                                 // to an integer.
93                                 size_t j;
94                                 for (j = 0; opts[i].map[j].name != NULL; ++j) {
95                                         if (strcmp(opts[i].map[j].name, value)
96                                                         == 0)
97                                                 break;
98                                 }
99
100                                 if (opts[i].map[j].name == NULL)
101                                         message_fatal(_("%s: Invalid option "
102                                                         "value"), value);
103
104                                 set(filter_options, i, opts[i].map[j].id);
105                         }
106
107                         found = true;
108                         break;
109                 }
110
111                 if (!found)
112                         message_fatal(_("%s: Invalid option name"), name);
113
114                 if (split == NULL)
115                         break;
116
117                 name = split + 1;
118         }
119
120         free(s);
121         return;
122 }
123
124
125 //////////////
126 // Subblock //
127 //////////////
128
129 enum {
130         OPT_SIZE,
131         OPT_RLE,
132         OPT_ALIGN,
133 };
134
135
136 static void
137 set_subblock(void *options, uint32_t key, uint64_t value)
138 {
139         lzma_options_subblock *opt = options;
140
141         switch (key) {
142         case OPT_SIZE:
143                 opt->subblock_data_size = value;
144                 break;
145
146         case OPT_RLE:
147                 opt->rle = value;
148                 break;
149
150         case OPT_ALIGN:
151                 opt->alignment = value;
152                 break;
153         }
154 }
155
156
157 extern lzma_options_subblock *
158 options_subblock(const char *str)
159 {
160         static const option_map opts[] = {
161                 { "size", NULL,   LZMA_SUBBLOCK_DATA_SIZE_MIN,
162                                   LZMA_SUBBLOCK_DATA_SIZE_MAX },
163                 { "rle",  NULL,   LZMA_SUBBLOCK_RLE_OFF,
164                                   LZMA_SUBBLOCK_RLE_MAX },
165                 { "align",NULL,   LZMA_SUBBLOCK_ALIGNMENT_MIN,
166                                   LZMA_SUBBLOCK_ALIGNMENT_MAX },
167                 { NULL,   NULL,   0, 0 }
168         };
169
170         lzma_options_subblock *options
171                         = xmalloc(sizeof(lzma_options_subblock));
172         *options = (lzma_options_subblock){
173                 .allow_subfilters = false,
174                 .alignment = LZMA_SUBBLOCK_ALIGNMENT_DEFAULT,
175                 .subblock_data_size = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT,
176                 .rle = LZMA_SUBBLOCK_RLE_OFF,
177         };
178
179         parse_options(str, opts, &set_subblock, options);
180
181         return options;
182 }
183
184
185 ///////////
186 // Delta //
187 ///////////
188
189 enum {
190         OPT_DIST,
191 };
192
193
194 static void
195 set_delta(void *options, uint32_t key, uint64_t value)
196 {
197         lzma_options_delta *opt = options;
198         switch (key) {
199         case OPT_DIST:
200                 opt->dist = value;
201                 break;
202         }
203 }
204
205
206 extern lzma_options_delta *
207 options_delta(const char *str)
208 {
209         static const option_map opts[] = {
210                 { "dist",     NULL,  LZMA_DELTA_DIST_MIN,
211                                      LZMA_DELTA_DIST_MAX },
212                 { NULL,       NULL,  0, 0 }
213         };
214
215         lzma_options_delta *options = xmalloc(sizeof(lzma_options_delta));
216         *options = (lzma_options_delta){
217                 // It's hard to give a useful default for this.
218                 .type = LZMA_DELTA_TYPE_BYTE,
219                 .dist = LZMA_DELTA_DIST_MIN,
220         };
221
222         parse_options(str, opts, &set_delta, options);
223
224         return options;
225 }
226
227
228 //////////
229 // LZMA //
230 //////////
231
232 enum {
233         OPT_PRESET,
234         OPT_DICT,
235         OPT_LC,
236         OPT_LP,
237         OPT_PB,
238         OPT_MODE,
239         OPT_NICE,
240         OPT_MF,
241         OPT_DEPTH,
242 };
243
244
245 static void
246 set_lzma(void *options, uint32_t key, uint64_t value)
247 {
248         lzma_options_lzma *opt = options;
249
250         switch (key) {
251         case OPT_PRESET:
252                 if (lzma_lzma_preset(options, (uint32_t)(value)))
253                         message_fatal("LZMA1/LZMA2 preset %u is not supported",
254                                         (unsigned int)(value));
255                 break;
256
257         case OPT_DICT:
258                 opt->dict_size = value;
259                 break;
260
261         case OPT_LC:
262                 opt->lc = value;
263                 break;
264
265         case OPT_LP:
266                 opt->lp = value;
267                 break;
268
269         case OPT_PB:
270                 opt->pb = value;
271                 break;
272
273         case OPT_MODE:
274                 opt->mode = value;
275                 break;
276
277         case OPT_NICE:
278                 opt->nice_len = value;
279                 break;
280
281         case OPT_MF:
282                 opt->mf = value;
283                 break;
284
285         case OPT_DEPTH:
286                 opt->depth = value;
287                 break;
288         }
289 }
290
291
292 extern lzma_options_lzma *
293 options_lzma(const char *str)
294 {
295         static const name_id_map modes[] = {
296                 { "fast",   LZMA_MODE_FAST },
297                 { "normal", LZMA_MODE_NORMAL },
298                 { NULL,     0 }
299         };
300
301         static const name_id_map mfs[] = {
302                 { "hc3", LZMA_MF_HC3 },
303                 { "hc4", LZMA_MF_HC4 },
304                 { "bt2", LZMA_MF_BT2 },
305                 { "bt3", LZMA_MF_BT3 },
306                 { "bt4", LZMA_MF_BT4 },
307                 { NULL,  0 }
308         };
309
310         static const option_map opts[] = {
311                 { "preset", NULL,   0, 9 },
312                 { "dict",   NULL,   LZMA_DICT_SIZE_MIN,
313                                 (UINT32_C(1) << 30) + (UINT32_C(1) << 29) },
314                 { "lc",     NULL,   LZMA_LCLP_MIN, LZMA_LCLP_MAX },
315                 { "lp",     NULL,   LZMA_LCLP_MIN, LZMA_LCLP_MAX },
316                 { "pb",     NULL,   LZMA_PB_MIN, LZMA_PB_MAX },
317                 { "mode",   modes,  0, 0 },
318                 { "nice",   NULL,   2, 273 },
319                 { "mf",     mfs,    0, 0 },
320                 { "depth",  NULL,   0, UINT32_MAX },
321                 { NULL,     NULL,   0, 0 }
322         };
323
324         // TODO There should be a way to take some preset as the base for
325         // custom settings.
326         lzma_options_lzma *options = xmalloc(sizeof(lzma_options_lzma));
327         *options = (lzma_options_lzma){
328                 .dict_size = LZMA_DICT_SIZE_DEFAULT,
329                 .preset_dict =  NULL,
330                 .preset_dict_size = 0,
331                 .lc = LZMA_LC_DEFAULT,
332                 .lp = LZMA_LP_DEFAULT,
333                 .pb = LZMA_PB_DEFAULT,
334                 .persistent = false,
335                 .mode = LZMA_MODE_NORMAL,
336                 .nice_len = 64,
337                 .mf = LZMA_MF_BT4,
338                 .depth = 0,
339         };
340
341         parse_options(str, opts, &set_lzma, options);
342
343         if (options->lc + options->lp > LZMA_LCLP_MAX)
344                 message_fatal(_("The sum of lc and lp must be at "
345                                 "maximum of 4"));
346
347         const uint32_t nice_len_min = options->mf & 0x0F;
348         if (options->nice_len < nice_len_min)
349                 message_fatal(_("The selected match finder requires at "
350                                 "least nice=%" PRIu32), nice_len_min);
351
352         return options;
353 }