1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Parser for filter-specific options
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 ///////////////////////////////////////////////////////////////////////////////
28 const name_id_map *map;
34 /// Parses option=value pairs that are separated with colons, semicolons,
35 /// or commas: opt=val:opt=val;opt=val,opt=val
37 /// Each option is a string, that is converted to an integer using the
38 /// index where the option string is in the array.
41 /// - a string-id map mapping a list of possible string values to integers
42 /// (opts[i].map != NULL, opts[i].min and opts[i].max are ignored);
43 /// - a number with minimum and maximum value limit
44 /// (opts[i].map == NULL && opts[i].min != UINT64_MAX);
45 /// - a string that will be parsed by the filter-specific code
46 /// (opts[i].map == NULL && opts[i].min == UINT64_MAX, opts[i].max ignored)
48 /// When parsing both option and value succeed, a filter-specific function
49 /// is called, which should update the given value to filter-specific
50 /// options structure.
52 /// \param str String containing the options from the command line
53 /// \param opts Filter-specific option map
54 /// \param set Filter-specific function to update filter_options
55 /// \param filter_options Pointer to filter-specific options structure
57 /// \return Returns only if no errors occur.
60 parse_options(const char *str, const option_map *opts,
61 void (*set)(void *filter_options,
62 uint32_t key, uint64_t value, const char *valuestr),
65 if (str == NULL || str[0] == '\0')
68 char *s = xstrdup(str);
72 char *split = strchr(name, ',');
76 char *value = strchr(name, '=');
80 if (value == NULL || value[0] == '\0')
81 message_fatal(_("%s: Options must be `name=value' "
82 "pairs separated with commas"), str);
84 // Look for the option name from the option map.
86 for (size_t i = 0; opts[i].name != NULL; ++i) {
87 if (strcmp(name, opts[i].name) != 0)
90 if (opts[i].map != NULL) {
91 // value is a string which we should map
94 for (j = 0; opts[i].map[j].name != NULL; ++j) {
95 if (strcmp(opts[i].map[j].name, value)
100 if (opts[i].map[j].name == NULL)
101 message_fatal(_("%s: Invalid option "
104 set(filter_options, i, opts[i].map[j].id,
107 } else if (opts[i].min == UINT64_MAX) {
108 // value is a special string that will be
110 set(filter_options, i, 0, value);
113 // value is an integer.
114 const uint64_t v = str_to_uint64(name, value,
115 opts[i].min, opts[i].max);
116 set(filter_options, i, v, value);
124 message_fatal(_("%s: Invalid option name"), name);
149 set_subblock(void *options, uint32_t key, uint64_t value,
150 const char *valuestr lzma_attribute((unused)))
152 lzma_options_subblock *opt = options;
156 opt->subblock_data_size = value;
164 opt->alignment = value;
170 extern lzma_options_subblock *
171 options_subblock(const char *str)
173 static const option_map opts[] = {
174 { "size", NULL, LZMA_SUBBLOCK_DATA_SIZE_MIN,
175 LZMA_SUBBLOCK_DATA_SIZE_MAX },
176 { "rle", NULL, LZMA_SUBBLOCK_RLE_OFF,
177 LZMA_SUBBLOCK_RLE_MAX },
178 { "align",NULL, LZMA_SUBBLOCK_ALIGNMENT_MIN,
179 LZMA_SUBBLOCK_ALIGNMENT_MAX },
183 lzma_options_subblock *options
184 = xmalloc(sizeof(lzma_options_subblock));
185 *options = (lzma_options_subblock){
186 .allow_subfilters = false,
187 .alignment = LZMA_SUBBLOCK_ALIGNMENT_DEFAULT,
188 .subblock_data_size = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT,
189 .rle = LZMA_SUBBLOCK_RLE_OFF,
192 parse_options(str, opts, &set_subblock, options);
208 set_delta(void *options, uint32_t key, uint64_t value,
209 const char *valuestr lzma_attribute((unused)))
211 lzma_options_delta *opt = options;
220 extern lzma_options_delta *
221 options_delta(const char *str)
223 static const option_map opts[] = {
224 { "dist", NULL, LZMA_DELTA_DIST_MIN,
225 LZMA_DELTA_DIST_MAX },
229 lzma_options_delta *options = xmalloc(sizeof(lzma_options_delta));
230 *options = (lzma_options_delta){
231 // It's hard to give a useful default for this.
232 .type = LZMA_DELTA_TYPE_BYTE,
233 .dist = LZMA_DELTA_DIST_MIN,
236 parse_options(str, opts, &set_delta, options);
252 set_bcj(void *options, uint32_t key, uint64_t value,
253 const char *valuestr lzma_attribute((unused)))
255 lzma_options_bcj *opt = options;
257 case OPT_START_OFFSET:
258 opt->start_offset = value;
264 extern lzma_options_bcj *
265 options_bcj(const char *str)
267 static const option_map opts[] = {
268 { "start", NULL, 0, UINT32_MAX },
272 lzma_options_bcj *options = xmalloc(sizeof(lzma_options_bcj));
273 *options = (lzma_options_bcj){
277 parse_options(str, opts, &set_bcj, options);
300 static void lzma_attribute((noreturn))
301 error_lzma_preset(const char *valuestr)
303 message_fatal(_("Unsupported LZMA1/LZMA2 preset: %s"), valuestr);
308 set_lzma(void *options, uint32_t key, uint64_t value, const char *valuestr)
310 lzma_options_lzma *opt = options;
314 if (valuestr[0] < '0' || valuestr[0] > '9')
315 error_lzma_preset(valuestr);
317 uint32_t preset = valuestr[0] - '0';
319 // Currently only "e" is supported as a modifier,
320 // so keep this simple for now.
321 if (valuestr[1] != '\0') {
322 if (valuestr[1] == 'e')
323 preset |= LZMA_PRESET_EXTREME;
325 error_lzma_preset(valuestr);
327 if (valuestr[2] != '\0')
328 error_lzma_preset(valuestr);
331 if (lzma_lzma_preset(options, preset))
332 error_lzma_preset(valuestr);
338 opt->dict_size = value;
358 opt->nice_len = value;
372 extern lzma_options_lzma *
373 options_lzma(const char *str)
375 static const name_id_map modes[] = {
376 { "fast", LZMA_MODE_FAST },
377 { "normal", LZMA_MODE_NORMAL },
381 static const name_id_map mfs[] = {
382 { "hc3", LZMA_MF_HC3 },
383 { "hc4", LZMA_MF_HC4 },
384 { "bt2", LZMA_MF_BT2 },
385 { "bt3", LZMA_MF_BT3 },
386 { "bt4", LZMA_MF_BT4 },
390 static const option_map opts[] = {
391 { "preset", NULL, UINT64_MAX, 0 },
392 { "dict", NULL, LZMA_DICT_SIZE_MIN,
393 (UINT32_C(1) << 30) + (UINT32_C(1) << 29) },
394 { "lc", NULL, LZMA_LCLP_MIN, LZMA_LCLP_MAX },
395 { "lp", NULL, LZMA_LCLP_MIN, LZMA_LCLP_MAX },
396 { "pb", NULL, LZMA_PB_MIN, LZMA_PB_MAX },
397 { "mode", modes, 0, 0 },
398 { "nice", NULL, 2, 273 },
400 { "depth", NULL, 0, UINT32_MAX },
404 lzma_options_lzma *options = xmalloc(sizeof(lzma_options_lzma));
405 *options = (lzma_options_lzma){
406 .dict_size = LZMA_DICT_SIZE_DEFAULT,
408 .preset_dict_size = 0,
409 .lc = LZMA_LC_DEFAULT,
410 .lp = LZMA_LP_DEFAULT,
411 .pb = LZMA_PB_DEFAULT,
413 .mode = LZMA_MODE_NORMAL,
419 parse_options(str, opts, &set_lzma, options);
421 if (options->lc + options->lp > LZMA_LCLP_MAX)
422 message_fatal(_("The sum of lc and lp must be at "
425 const uint32_t nice_len_min = options->mf & 0x0F;
426 if (options->nice_len < nice_len_min)
427 message_fatal(_("The selected match finder requires at "
428 "least nice=%" PRIu32), nice_len_min);