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"),
89 // Look for the option name from the option map.
91 for (size_t i = 0; opts[i].name != NULL; ++i) {
92 if (strcmp(name, opts[i].name) != 0)
95 if (opts[i].map == NULL) {
96 // value is an integer.
97 const uint64_t v = str_to_uint64(name, value,
98 opts[i].min, opts[i].max);
99 set(filter_options, i, v);
101 // value is a string which we should map
104 for (j = 0; opts[i].map[j].name != NULL; ++j) {
105 if (strcmp(opts[i].map[j].name, value)
110 if (opts[i].map[j].name == NULL) {
111 errmsg(V_ERROR, _("%s: Invalid option "
116 set(filter_options, i, opts[i].map[j].id);
124 errmsg(V_ERROR, _("%s: Invalid option name"), name);
151 set_subblock(void *options, uint32_t key, uint64_t value)
153 lzma_options_subblock *opt = options;
157 opt->subblock_data_size = value;
165 opt->alignment = value;
171 extern lzma_options_subblock *
172 parse_options_subblock(const char *str)
174 static const option_map opts[] = {
175 { "size", NULL, LZMA_SUBBLOCK_DATA_SIZE_MIN,
176 LZMA_SUBBLOCK_DATA_SIZE_MAX },
177 { "rle", NULL, LZMA_SUBBLOCK_RLE_OFF,
178 LZMA_SUBBLOCK_RLE_MAX },
179 { "align",NULL, LZMA_SUBBLOCK_ALIGNMENT_MIN,
180 LZMA_SUBBLOCK_ALIGNMENT_MAX },
184 lzma_options_subblock *options
185 = xmalloc(sizeof(lzma_options_subblock));
186 *options = (lzma_options_subblock){
187 .allow_subfilters = false,
188 .alignment = LZMA_SUBBLOCK_ALIGNMENT_DEFAULT,
189 .subblock_data_size = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT,
190 .rle = LZMA_SUBBLOCK_RLE_OFF,
193 parse_options(str, opts, &set_subblock, options);
209 set_delta(void *options, uint32_t key, uint64_t value)
211 lzma_options_delta *opt = options;
214 opt->distance = value;
220 extern lzma_options_delta *
221 parse_options_delta(const char *str)
223 static const option_map opts[] = {
224 { "distance", NULL, LZMA_DELTA_DISTANCE_MIN,
225 LZMA_DELTA_DISTANCE_MAX },
229 lzma_options_delta *options = xmalloc(sizeof(lzma_options_subblock));
230 *options = (lzma_options_delta){
231 // It's hard to give a useful default for this.
232 .distance = LZMA_DELTA_DISTANCE_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->dictionary_size = value;
268 opt->literal_context_bits = value;
272 opt->literal_pos_bits = value;
276 opt->pos_bits = value;
284 opt->fast_bytes = value;
288 opt->match_finder = value;
292 opt->match_finder_cycles = 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 { "best", LZMA_MODE_BEST },
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_DICTIONARY_SIZE_MIN,
318 LZMA_DICTIONARY_SIZE_MAX },
319 { "lc", NULL, LZMA_LITERAL_CONTEXT_BITS_MIN,
320 LZMA_LITERAL_CONTEXT_BITS_MAX },
321 { "lp", NULL, LZMA_LITERAL_POS_BITS_MIN,
322 LZMA_LITERAL_POS_BITS_MAX },
323 { "pb", NULL, LZMA_POS_BITS_MIN, LZMA_POS_BITS_MAX },
324 { "mode", modes, 0, 0 },
325 { "fb", NULL, LZMA_FAST_BYTES_MIN, LZMA_FAST_BYTES_MAX },
327 { "mc", NULL, 0, UINT32_MAX },
331 lzma_options_lzma *options = xmalloc(sizeof(lzma_options_lzma));
332 *options = (lzma_options_lzma){
333 .dictionary_size = LZMA_DICTIONARY_SIZE_DEFAULT,
334 .literal_context_bits = LZMA_LITERAL_CONTEXT_BITS_DEFAULT,
335 .literal_pos_bits = LZMA_LITERAL_POS_BITS_DEFAULT,
336 .pos_bits = LZMA_POS_BITS_DEFAULT,
337 .mode = LZMA_MODE_BEST,
338 .fast_bytes = LZMA_FAST_BYTES_DEFAULT,
339 .match_finder = LZMA_MF_BT4,
340 .match_finder_cycles = 0,
343 parse_options(str, opts, &set_lzma, options);