]> icculus.org git repositories - icculus/xz.git/blob - src/lzma/options.c
Some API changes, bug fixes, cleanups etc.
[icculus/xz.git] / src / lzma / options.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       options.c
4 /// \brief      Parser for filter-specific options
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "private.h"
21
22
23 ///////////////////
24 // Generic stuff //
25 ///////////////////
26
27 typedef struct {
28         const char *name;
29         uint64_t id;
30 } name_id_map;
31
32
33 typedef struct {
34         const char *name;
35         const name_id_map *map;
36         uint64_t min;
37         uint64_t max;
38 } option_map;
39
40
41 /// Parses option=value pairs that are separated with colons, semicolons,
42 /// or commas: opt=val:opt=val;opt=val,opt=val
43 ///
44 /// Each option is a string, that is converted to an integer using the
45 /// index where the option string is in the array.
46 ///
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.
49 ///
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.
53 ///
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
58 ///
59 /// \return     Returns only if no errors occur.
60 ///
61 static void
62 parse_options(const char *str, const option_map *opts,
63                 void (*set)(void *filter_options,
64                         uint32_t key, uint64_t value),
65                 void *filter_options)
66 {
67         if (str == NULL || str[0] == '\0')
68                 return;
69
70         char *s = xstrdup(str);
71         char *name = s;
72
73         while (true) {
74                 char *split = strchr(name, ',');
75                 if (split != NULL)
76                         *split = '\0';
77
78                 char *value = strchr(name, '=');
79                 if (value != NULL)
80                         *value++ = '\0';
81
82                 if (value == NULL || value[0] == '\0') {
83                         errmsg(V_ERROR, _("%s: Options must be `name=value' "
84                                         "pairs separated with commas"), str);
85                         my_exit(ERROR);
86                 }
87
88                 // Look for the option name from the option map.
89                 bool found = false;
90                 for (size_t i = 0; opts[i].name != NULL; ++i) {
91                         if (strcmp(name, opts[i].name) != 0)
92                                 continue;
93
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);
99                         } else {
100                                 // value is a string which we should map
101                                 // to an integer.
102                                 size_t j;
103                                 for (j = 0; opts[i].map[j].name != NULL; ++j) {
104                                         if (strcmp(opts[i].map[j].name, value)
105                                                         == 0)
106                                                 break;
107                                 }
108
109                                 if (opts[i].map[j].name == NULL) {
110                                         errmsg(V_ERROR, _("%s: Invalid option "
111                                                         "value"), value);
112                                         my_exit(ERROR);
113                                 }
114
115                                 set(filter_options, i, opts[i].map[j].id);
116                         }
117
118                         found = true;
119                         break;
120                 }
121
122                 if (!found) {
123                         errmsg(V_ERROR, _("%s: Invalid option name"), name);
124                         my_exit(ERROR);
125                 }
126
127                 if (split == NULL)
128                         break;
129
130                 name = split + 1;
131         }
132
133         free(s);
134         return;
135 }
136
137
138 //////////////
139 // Subblock //
140 //////////////
141
142 enum {
143         OPT_SIZE,
144         OPT_RLE,
145         OPT_ALIGN,
146 };
147
148
149 static void
150 set_subblock(void *options, uint32_t key, uint64_t value)
151 {
152         lzma_options_subblock *opt = options;
153
154         switch (key) {
155         case OPT_SIZE:
156                 opt->subblock_data_size = value;
157                 break;
158
159         case OPT_RLE:
160                 opt->rle = value;
161                 break;
162
163         case OPT_ALIGN:
164                 opt->alignment = value;
165                 break;
166         }
167 }
168
169
170 extern lzma_options_subblock *
171 parse_options_subblock(const char *str)
172 {
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 },
180                 { NULL,   NULL,   0, 0 }
181         };
182
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,
190         };
191
192         parse_options(str, opts, &set_subblock, options);
193
194         return options;
195 }
196
197
198 ///////////
199 // Delta //
200 ///////////
201
202 enum {
203         OPT_DIST,
204 };
205
206
207 static void
208 set_delta(void *options, uint32_t key, uint64_t value)
209 {
210         lzma_options_delta *opt = options;
211         switch (key) {
212         case OPT_DIST:
213                 opt->dist = value;
214                 break;
215         }
216 }
217
218
219 extern lzma_options_delta *
220 parse_options_delta(const char *str)
221 {
222         static const option_map opts[] = {
223                 { "dist",     NULL,  LZMA_DELTA_DIST_MIN,
224                                      LZMA_DELTA_DIST_MAX },
225                 { NULL,       NULL,  0, 0 }
226         };
227
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,
233         };
234
235         parse_options(str, opts, &set_delta, options);
236
237         return options;
238 }
239
240
241 //////////
242 // LZMA //
243 //////////
244
245 enum {
246         OPT_DICT,
247         OPT_LC,
248         OPT_LP,
249         OPT_PB,
250         OPT_MODE,
251         OPT_NICE,
252         OPT_MF,
253         OPT_DEPTH,
254 };
255
256
257 static void
258 set_lzma(void *options, uint32_t key, uint64_t value)
259 {
260         lzma_options_lzma *opt = options;
261
262         switch (key) {
263         case OPT_DICT:
264                 opt->dict_size = value;
265                 break;
266
267         case OPT_LC:
268                 opt->lc = value;
269                 break;
270
271         case OPT_LP:
272                 opt->lp = value;
273                 break;
274
275         case OPT_PB:
276                 opt->pb = value;
277                 break;
278
279         case OPT_MODE:
280                 opt->mode = value;
281                 break;
282
283         case OPT_NICE:
284                 opt->nice_len = value;
285                 break;
286
287         case OPT_MF:
288                 opt->mf = value;
289                 break;
290
291         case OPT_DEPTH:
292                 opt->depth = value;
293                 break;
294         }
295 }
296
297
298 extern lzma_options_lzma *
299 parse_options_lzma(const char *str)
300 {
301         static const name_id_map modes[] = {
302                 { "fast",   LZMA_MODE_FAST },
303                 { "normal", LZMA_MODE_NORMAL },
304                 { NULL,     0 }
305         };
306
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 },
313                 { NULL,  0 }
314         };
315
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 },
324                 { "mf",     mfs,    0, 0 },
325                 { "depth",  NULL,   0, UINT32_MAX },
326                 { NULL,     NULL,   0, 0 }
327         };
328
329         // TODO There should be a way to take some preset as the base for
330         // custom settings.
331         lzma_options_lzma *options = xmalloc(sizeof(lzma_options_lzma));
332         *options = (lzma_options_lzma){
333                 .dict_size = LZMA_DICT_SIZE_DEFAULT,
334                 .preset_dict =  NULL,
335                 .preset_dict_size = 0,
336                 .lc = LZMA_LC_DEFAULT,
337                 .lp = LZMA_LP_DEFAULT,
338                 .pb = LZMA_PB_DEFAULT,
339                 .persistent = false,
340                 .mode = LZMA_MODE_NORMAL,
341                 .nice_len = 64,
342                 .mf = LZMA_MF_BT4,
343                 .depth = 0,
344         };
345
346         parse_options(str, opts, &set_lzma, options);
347
348         if (options->lc + options->lp > LZMA_LCLP_MAX) {
349                 errmsg(V_ERROR, "The sum of lc and lp must be at "
350                                 "maximum of 4");
351                 exit(ERROR);
352         }
353
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);
358                 exit(ERROR);
359         }
360
361         return options;
362 }