1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Parser for filter-specific options
6 // Copyright (C) 2007 Lasse Collin
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
35 const name_id_map *map;
41 /// Parses option=value pairs that are separated with colons, semicolons,
42 /// or commas: opt=val:opt=val;opt=val,opt=val
44 /// Each option is a string, that is converted to an integer using the
45 /// index where the option string is in the array.
47 /// Value can be either a number with minimum and maximum value limit, or
48 /// a string-id map mapping a list of possible string values to integers.
50 /// When parsing both option and value succeed, a filter-specific function
51 /// is called, which should update the given value to filter-specific
52 /// options structure.
54 /// \param str String containing the options from the command line
55 /// \param opts Filter-specific option map
56 /// \param set Filter-specific function to update filter_options
57 /// \param filter_options Pointer to filter-specific options structure
59 /// \return Returns only if no errors occur.
62 parse_options(const char *str, const option_map *opts,
63 void (*set)(void *filter_options,
64 uint32_t key, uint64_t value),
67 if (str == NULL || str[0] == '\0')
70 char *s = xstrdup(str);
74 char *split = strchr(name, ',');
78 char *value = strchr(name, '=');
82 if (value == NULL || value[0] == '\0') {
83 errmsg(V_ERROR, _("%s: Options must be `name=value' "
84 "pairs separated with commas"), str);
88 // Look for the option name from the option map.
90 for (size_t i = 0; opts[i].name != NULL; ++i) {
91 if (strcmp(name, opts[i].name) != 0)
94 if (opts[i].map == NULL) {
95 // value is an integer.
96 const uint64_t v = str_to_uint64(name, value,
97 opts[i].min, opts[i].max);
98 set(filter_options, i, v);
100 // value is a string which we should map
103 for (j = 0; opts[i].map[j].name != NULL; ++j) {
104 if (strcmp(opts[i].map[j].name, value)
109 if (opts[i].map[j].name == NULL) {
110 errmsg(V_ERROR, _("%s: Invalid option "
115 set(filter_options, i, opts[i].map[j].id);
123 errmsg(V_ERROR, _("%s: Invalid option name"), name);
150 set_subblock(void *options, uint32_t key, uint64_t value)
152 lzma_options_subblock *opt = options;
156 opt->subblock_data_size = value;
164 opt->alignment = value;
170 extern lzma_options_subblock *
171 parse_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)
210 lzma_options_delta *opt = options;
219 extern lzma_options_delta *
220 parse_options_delta(const char *str)
222 static const option_map opts[] = {
223 { "dist", NULL, LZMA_DELTA_DIST_MIN,
224 LZMA_DELTA_DIST_MAX },
228 lzma_options_delta *options = xmalloc(sizeof(lzma_options_subblock));
229 *options = (lzma_options_delta){
230 // It's hard to give a useful default for this.
231 .type = LZMA_DELTA_TYPE_BYTE,
232 .dist = LZMA_DELTA_DIST_MIN,
235 parse_options(str, opts, &set_delta, options);
258 set_lzma(void *options, uint32_t key, uint64_t value)
260 lzma_options_lzma *opt = options;
264 opt->dict_size = value;
284 opt->nice_len = value;
298 extern lzma_options_lzma *
299 parse_options_lzma(const char *str)
301 static const name_id_map modes[] = {
302 { "fast", LZMA_MODE_FAST },
303 { "normal", LZMA_MODE_NORMAL },
307 static const name_id_map mfs[] = {
308 { "hc3", LZMA_MF_HC3 },
309 { "hc4", LZMA_MF_HC4 },
310 { "bt2", LZMA_MF_BT2 },
311 { "bt3", LZMA_MF_BT3 },
312 { "bt4", LZMA_MF_BT4 },
316 static const option_map opts[] = {
317 { "dict", NULL, LZMA_DICT_SIZE_MIN,
318 (UINT32_C(1) << 30) + (UINT32_C(1) << 29) },
319 { "lc", NULL, LZMA_LCLP_MIN, LZMA_LCLP_MAX },
320 { "lp", NULL, LZMA_LCLP_MIN, LZMA_LCLP_MAX },
321 { "pb", NULL, LZMA_PB_MIN, LZMA_PB_MAX },
322 { "mode", modes, 0, 0 },
323 { "nice", NULL, 2, 273 },
325 { "depth", NULL, 0, UINT32_MAX },
329 // TODO There should be a way to take some preset as the base for
331 lzma_options_lzma *options = xmalloc(sizeof(lzma_options_lzma));
332 *options = (lzma_options_lzma){
333 .dict_size = LZMA_DICT_SIZE_DEFAULT,
335 .preset_dict_size = 0,
336 .lc = LZMA_LC_DEFAULT,
337 .lp = LZMA_LP_DEFAULT,
338 .pb = LZMA_PB_DEFAULT,
340 .mode = LZMA_MODE_NORMAL,
346 parse_options(str, opts, &set_lzma, options);
348 if (options->lc + options->lp > LZMA_LCLP_MAX) {
349 errmsg(V_ERROR, "The sum of lc and lp must be at "
354 const uint32_t nice_len_min = options->mf & 0x0F;
355 if (options->nice_len < nice_len_min) {
356 errmsg(V_ERROR, "The selected match finder requires at "
357 "least nice=%" PRIu32, nice_len_min);