replace local_strtoull with _strtoui64 in windows
[platform/upstream/flac.git] / src / metaflac / options.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "options.h"
24 #include "usage.h"
25 #include "utils.h"
26 #include "FLAC/assert.h"
27 #include "share/alloc.h"
28 #include "share/compat.h"
29 #include "share/grabbag/replaygain.h"
30 #include <ctype.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 /*
36    share__getopt format struct; note we don't use short options so we just
37    set the 'val' field to 0 everywhere to indicate a valid option.
38 */
39 struct share__option long_options_[] = {
40         /* global options */
41         { "preserve-modtime", 0, 0, 0 },
42         { "with-filename", 0, 0, 0 },
43         { "no-filename", 0, 0, 0 },
44         { "no-utf8-convert", 0, 0, 0 },
45         { "dont-use-padding", 0, 0, 0 },
46         { "no-cued-seekpoints", 0, 0, 0 },
47         /* shorthand operations */
48         { "show-md5sum", 0, 0, 0 },
49         { "show-min-blocksize", 0, 0, 0 },
50         { "show-max-blocksize", 0, 0, 0 },
51         { "show-min-framesize", 0, 0, 0 },
52         { "show-max-framesize", 0, 0, 0 },
53         { "show-sample-rate", 0, 0, 0 },
54         { "show-channels", 0, 0, 0 },
55         { "show-bps", 0, 0, 0 },
56         { "show-total-samples", 0, 0, 0 },
57         { "set-md5sum", 1, 0, 0 }, /* undocumented */
58         { "set-min-blocksize", 1, 0, 0 }, /* undocumented */
59         { "set-max-blocksize", 1, 0, 0 }, /* undocumented */
60         { "set-min-framesize", 1, 0, 0 }, /* undocumented */
61         { "set-max-framesize", 1, 0, 0 }, /* undocumented */
62         { "set-sample-rate", 1, 0, 0 }, /* undocumented */
63         { "set-channels", 1, 0, 0 }, /* undocumented */
64         { "set-bps", 1, 0, 0 }, /* undocumented */
65         { "set-total-samples", 1, 0, 0 }, /* undocumented */ /* WATCHOUT: used by test/test_flac.sh on windows */
66         { "show-vendor-tag", 0, 0, 0 },
67         { "show-tag", 1, 0, 0 },
68         { "remove-all-tags", 0, 0, 0 },
69         { "remove-tag", 1, 0, 0 },
70         { "remove-first-tag", 1, 0, 0 },
71         { "set-tag", 1, 0, 0 },
72         { "set-tag-from-file", 1, 0, 0 },
73         { "import-tags-from", 1, 0, 0 },
74         { "export-tags-to", 1, 0, 0 },
75         { "import-cuesheet-from", 1, 0, 0 },
76         { "export-cuesheet-to", 1, 0, 0 },
77         { "import-picture-from", 1, 0, 0 },
78         { "export-picture-to", 1, 0, 0 },
79         { "add-seekpoint", 1, 0, 0 },
80         { "add-replay-gain", 0, 0, 0 },
81         { "remove-replay-gain", 0, 0, 0 },
82         { "add-padding", 1, 0, 0 },
83         /* major operations */
84         { "help", 0, 0, 0 },
85         { "version", 0, 0, 0 },
86         { "list", 0, 0, 0 },
87         { "append", 0, 0, 0 },
88         { "remove", 0, 0, 0 },
89         { "remove-all", 0, 0, 0 },
90         { "merge-padding", 0, 0, 0 },
91         { "sort-padding", 0, 0, 0 },
92         /* major operation arguments */
93         { "block-number", 1, 0, 0 },
94         { "block-type", 1, 0, 0 },
95         { "except-block-type", 1, 0, 0 },
96         { "data-format", 1, 0, 0 },
97         { "application-data-format", 1, 0, 0 },
98         { "from-file", 1, 0, 0 },
99         {0, 0, 0, 0}
100 };
101
102 static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options);
103 static void append_new_operation(CommandLineOptions *options, Operation operation);
104 static void append_new_argument(CommandLineOptions *options, Argument argument);
105 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
106 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
107 static Argument *find_argument(CommandLineOptions *options, ArgumentType type);
108 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
109 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
110 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
111 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
112 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
113 static FLAC__bool parse_string(const char *src, char **dest);
114 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
115 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
116 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
117 static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out);
118 static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out);
119 static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out);
120 static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out);
121 static void undocumented_warning(const char *opt);
122
123
124 void init_options(CommandLineOptions *options)
125 {
126         options->preserve_modtime = false;
127
128         /* '2' is a hack to mean "use default if not forced on command line" */
129         FLAC__ASSERT(true != 2);
130         options->prefix_with_filename = 2;
131
132         options->utf8_convert = true;
133         options->use_padding = true;
134         options->cued_seekpoints = true;
135         options->show_long_help = false;
136         options->show_version = false;
137         options->application_data_format_is_hexdump = false;
138
139         options->ops.operations = 0;
140         options->ops.num_operations = 0;
141         options->ops.capacity = 0;
142
143         options->args.arguments = 0;
144         options->args.num_arguments = 0;
145         options->args.capacity = 0;
146
147         options->args.checks.num_shorthand_ops = 0;
148         options->args.checks.num_major_ops = 0;
149         options->args.checks.has_block_type = false;
150         options->args.checks.has_except_block_type = false;
151
152         options->num_files = 0;
153         options->filenames = 0;
154 }
155
156 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
157 {
158         int ret;
159         int option_index = 1;
160         FLAC__bool had_error = false;
161
162         while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
163                 switch (ret) {
164                         case 0:
165                                 had_error |= !parse_option(option_index, share__optarg, options);
166                                 break;
167                         case '?':
168                         case ':':
169                                 had_error = true;
170                                 break;
171                         default:
172                                 FLAC__ASSERT(0);
173                                 break;
174                 }
175         }
176
177         if(options->prefix_with_filename == 2)
178                 options->prefix_with_filename = (argc - share__optind > 1);
179
180         if(share__optind >= argc && !options->show_long_help && !options->show_version) {
181                 fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
182                 fprintf(stderr,"       metaflac cannot be used as a pipe\n");
183                 had_error = true;
184         }
185
186         options->num_files = argc - share__optind;
187
188         if(options->num_files > 0) {
189                 unsigned i = 0;
190                 if(0 == (options->filenames = safe_malloc_mul_2op_(sizeof(char*), /*times*/options->num_files)))
191                         die("out of memory allocating space for file names list");
192                 while(share__optind < argc)
193                         options->filenames[i++] = local_strdup(argv[share__optind++]);
194         }
195
196         if(options->args.checks.num_major_ops > 0) {
197                 if(options->args.checks.num_major_ops > 1) {
198                         fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
199                         had_error = true;
200                 }
201                 else if(options->args.checks.num_shorthand_ops > 0) {
202                         fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
203                         had_error = true;
204                 }
205         }
206
207         /* check for only one FLAC file used with certain options */
208         if(options->num_files > 1) {
209                 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
210                         fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from'\n");
211                         had_error = true;
212                 }
213                 if(0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) {
214                         fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-cuesheet-to'\n");
215                         had_error = true;
216                 }
217                 if(0 != find_shorthand_operation(options, OP__EXPORT_PICTURE_TO)) {
218                         fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-picture-to'\n");
219                         had_error = true;
220                 }
221                 if(
222                         0 != find_shorthand_operation(options, OP__IMPORT_VC_FROM) &&
223                         0 == strcmp(find_shorthand_operation(options, OP__IMPORT_VC_FROM)->argument.filename.value, "-")
224                 ) {
225                         fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-tags-from=-'\n");
226                         had_error = true;
227                 }
228         }
229
230         if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
231                 fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
232                 had_error = true;
233         }
234
235         if(had_error)
236                 short_usage(0);
237
238         /*
239          * We need to create an OP__ADD_SEEKPOINT operation if there is
240          * not one already, and --import-cuesheet-from was specified but
241          * --no-cued-seekpoints was not:
242          */
243         if(options->cued_seekpoints) {
244                 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
245                 if(0 != op) {
246                         Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
247                         if(0 == op2)
248                                 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
249                         op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
250                 }
251         }
252
253         return had_error;
254 }
255
256 void free_options(CommandLineOptions *options)
257 {
258         unsigned i;
259         Operation *op;
260         Argument *arg;
261
262         FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
263         FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
264
265         for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
266                 switch(op->type) {
267                         case OP__SHOW_VC_FIELD:
268                         case OP__REMOVE_VC_FIELD:
269                         case OP__REMOVE_VC_FIRSTFIELD:
270                                 if(0 != op->argument.vc_field_name.value)
271                                         free(op->argument.vc_field_name.value);
272                                 break;
273                         case OP__SET_VC_FIELD:
274                                 if(0 != op->argument.vc_field.field)
275                                         free(op->argument.vc_field.field);
276                                 if(0 != op->argument.vc_field.field_name)
277                                         free(op->argument.vc_field.field_name);
278                                 if(0 != op->argument.vc_field.field_value)
279                                         free(op->argument.vc_field.field_value);
280                                 break;
281                         case OP__IMPORT_VC_FROM:
282                         case OP__EXPORT_VC_TO:
283                         case OP__EXPORT_CUESHEET_TO:
284                                 if(0 != op->argument.filename.value)
285                                         free(op->argument.filename.value);
286                                 break;
287                         case OP__IMPORT_CUESHEET_FROM:
288                                 if(0 != op->argument.import_cuesheet_from.filename)
289                                         free(op->argument.import_cuesheet_from.filename);
290                                 break;
291                         case OP__IMPORT_PICTURE_FROM:
292                                 if(0 != op->argument.specification.value)
293                                         free(op->argument.specification.value);
294                                 break;
295                         case OP__EXPORT_PICTURE_TO:
296                                 if(0 != op->argument.export_picture_to.filename)
297                                         free(op->argument.export_picture_to.filename);
298                                 break;
299                         case OP__ADD_SEEKPOINT:
300                                 if(0 != op->argument.add_seekpoint.specification)
301                                         free(op->argument.add_seekpoint.specification);
302                                 break;
303                         default:
304                                 break;
305                 }
306         }
307
308         for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
309                 switch(arg->type) {
310                         case ARG__BLOCK_NUMBER:
311                                 if(0 != arg->value.block_number.entries)
312                                         free(arg->value.block_number.entries);
313                                 break;
314                         case ARG__BLOCK_TYPE:
315                         case ARG__EXCEPT_BLOCK_TYPE:
316                                 if(0 != arg->value.block_type.entries)
317                                         free(arg->value.block_type.entries);
318                                 break;
319                         case ARG__FROM_FILE:
320                                 if(0 != arg->value.from_file.file_name)
321                                         free(arg->value.from_file.file_name);
322                                 break;
323                         default:
324                                 break;
325                 }
326         }
327
328         if(0 != options->ops.operations)
329                 free(options->ops.operations);
330
331         if(0 != options->args.arguments)
332                 free(options->args.arguments);
333
334         if(0 != options->filenames) {
335                 for(i = 0; i < options->num_files; i++) {
336                         if(0 != options->filenames[i])
337                                 free(options->filenames[i]);
338                 }
339                 free(options->filenames);
340         }
341 }
342
343 /*
344  * local routines
345  */
346
347 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
348 {
349         const char *opt = long_options_[option_index].name;
350         Operation *op;
351         Argument *arg;
352         FLAC__bool ok = true;
353
354         if(0 == strcmp(opt, "preserve-modtime")) {
355                 options->preserve_modtime = true;
356         }
357         else if(0 == strcmp(opt, "with-filename")) {
358                 options->prefix_with_filename = true;
359         }
360         else if(0 == strcmp(opt, "no-filename")) {
361                 options->prefix_with_filename = false;
362         }
363         else if(0 == strcmp(opt, "no-utf8-convert")) {
364                 options->utf8_convert = false;
365         }
366         else if(0 == strcmp(opt, "dont-use-padding")) {
367                 options->use_padding = false;
368         }
369         else if(0 == strcmp(opt, "no-cued-seekpoints")) {
370                 options->cued_seekpoints = false;
371         }
372         else if(0 == strcmp(opt, "show-md5sum")) {
373                 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
374         }
375         else if(0 == strcmp(opt, "show-min-blocksize")) {
376                 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
377         }
378         else if(0 == strcmp(opt, "show-max-blocksize")) {
379                 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
380         }
381         else if(0 == strcmp(opt, "show-min-framesize")) {
382                 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
383         }
384         else if(0 == strcmp(opt, "show-max-framesize")) {
385                 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
386         }
387         else if(0 == strcmp(opt, "show-sample-rate")) {
388                 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
389         }
390         else if(0 == strcmp(opt, "show-channels")) {
391                 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
392         }
393         else if(0 == strcmp(opt, "show-bps")) {
394                 (void) append_shorthand_operation(options, OP__SHOW_BPS);
395         }
396         else if(0 == strcmp(opt, "show-total-samples")) {
397                 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
398         }
399         else if(0 == strcmp(opt, "set-md5sum")) {
400                 op = append_shorthand_operation(options, OP__SET_MD5SUM);
401                 FLAC__ASSERT(0 != option_argument);
402                 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
403                         fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
404                         ok = false;
405                 }
406                 else
407                         undocumented_warning(opt);
408         }
409         else if(0 == strcmp(opt, "set-min-blocksize")) {
410                 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
411                 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
412                         fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
413                         ok = false;
414                 }
415                 else
416                         undocumented_warning(opt);
417         }
418         else if(0 == strcmp(opt, "set-max-blocksize")) {
419                 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
420                 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
421                         fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
422                         ok = false;
423                 }
424                 else
425                         undocumented_warning(opt);
426         }
427         else if(0 == strcmp(opt, "set-min-framesize")) {
428                 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
429                 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
430                         fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
431                         ok = false;
432                 }
433                 else
434                         undocumented_warning(opt);
435         }
436         else if(0 == strcmp(opt, "set-max-framesize")) {
437                 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
438                 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
439                         fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
440                         ok = false;
441                 }
442                 else
443                         undocumented_warning(opt);
444         }
445         else if(0 == strcmp(opt, "set-sample-rate")) {
446                 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
447                 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
448                         fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
449                         ok = false;
450                 }
451                 else
452                         undocumented_warning(opt);
453         }
454         else if(0 == strcmp(opt, "set-channels")) {
455                 op = append_shorthand_operation(options, OP__SET_CHANNELS);
456                 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
457                         fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
458                         ok = false;
459                 }
460                 else
461                         undocumented_warning(opt);
462         }
463         else if(0 == strcmp(opt, "set-bps")) {
464                 op = append_shorthand_operation(options, OP__SET_BPS);
465                 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BITS_PER_SAMPLE || op->argument.streaminfo_uint32.value > FLAC__MAX_BITS_PER_SAMPLE) {
466                         fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
467                         ok = false;
468                 }
469                 else
470                         undocumented_warning(opt);
471         }
472         else if(0 == strcmp(opt, "set-total-samples")) {
473                 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
474                 if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (((FLAC__uint64)1)<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
475                         fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
476                         ok = false;
477                 }
478                 else
479                         undocumented_warning(opt);
480         }
481         else if(0 == strcmp(opt, "show-vendor-tag")) {
482                 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
483         }
484         else if(0 == strcmp(opt, "show-tag")) {
485                 const char *violation;
486                 op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
487                 FLAC__ASSERT(0 != option_argument);
488                 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
489                         FLAC__ASSERT(0 != violation);
490                         fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
491                         ok = false;
492                 }
493         }
494         else if(0 == strcmp(opt, "remove-all-tags")) {
495                 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
496         }
497         else if(0 == strcmp(opt, "remove-tag")) {
498                 const char *violation;
499                 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
500                 FLAC__ASSERT(0 != option_argument);
501                 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
502                         FLAC__ASSERT(0 != violation);
503                         fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
504                         ok = false;
505                 }
506         }
507         else if(0 == strcmp(opt, "remove-first-tag")) {
508                 const char *violation;
509                 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
510                 FLAC__ASSERT(0 != option_argument);
511                 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
512                         FLAC__ASSERT(0 != violation);
513                         fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
514                         ok = false;
515                 }
516         }
517         else if(0 == strcmp(opt, "set-tag")) {
518                 const char *violation;
519                 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
520                 FLAC__ASSERT(0 != option_argument);
521                 op->argument.vc_field.field_value_from_file = false;
522                 if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
523                         FLAC__ASSERT(0 != violation);
524                         fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
525                         ok = false;
526                 }
527         }
528         else if(0 == strcmp(opt, "set-tag-from-file")) {
529                 const char *violation;
530                 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
531                 FLAC__ASSERT(0 != option_argument);
532                 op->argument.vc_field.field_value_from_file = true;
533                 if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
534                         FLAC__ASSERT(0 != violation);
535                         fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
536                         ok = false;
537                 }
538         }
539         else if(0 == strcmp(opt, "import-tags-from")) {
540                 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
541                 FLAC__ASSERT(0 != option_argument);
542                 if(!parse_string(option_argument, &(op->argument.filename.value))) {
543                         fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
544                         ok = false;
545                 }
546         }
547         else if(0 == strcmp(opt, "export-tags-to")) {
548                 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
549                 FLAC__ASSERT(0 != option_argument);
550                 if(!parse_string(option_argument, &(op->argument.filename.value))) {
551                         fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
552                         ok = false;
553                 }
554         }
555         else if(0 == strcmp(opt, "import-cuesheet-from")) {
556                 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
557                         fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
558                         ok = false;
559                 }
560                 op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
561                 FLAC__ASSERT(0 != option_argument);
562                 if(!parse_string(option_argument, &(op->argument.import_cuesheet_from.filename))) {
563                         fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
564                         ok = false;
565                 }
566         }
567         else if(0 == strcmp(opt, "export-cuesheet-to")) {
568                 op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
569                 FLAC__ASSERT(0 != option_argument);
570                 if(!parse_string(option_argument, &(op->argument.filename.value))) {
571                         fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
572                         ok = false;
573                 }
574         }
575         else if(0 == strcmp(opt, "import-picture-from")) {
576                 op = append_shorthand_operation(options, OP__IMPORT_PICTURE_FROM);
577                 FLAC__ASSERT(0 != option_argument);
578                 if(!parse_string(option_argument, &(op->argument.specification.value))) {
579                         fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
580                         ok = false;
581                 }
582         }
583         else if(0 == strcmp(opt, "export-picture-to")) {
584                 const Argument *arg = find_argument(options, ARG__BLOCK_NUMBER);
585                 op = append_shorthand_operation(options, OP__EXPORT_PICTURE_TO);
586                 FLAC__ASSERT(0 != option_argument);
587                 if(!parse_string(option_argument, &(op->argument.export_picture_to.filename))) {
588                         fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
589                         ok = false;
590                 }
591                 op->argument.export_picture_to.block_number_link = arg? &(arg->value.block_number) : 0;
592         }
593         else if(0 == strcmp(opt, "add-seekpoint")) {
594                 const char *violation;
595                 char *spec;
596                 FLAC__ASSERT(0 != option_argument);
597                 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
598                         FLAC__ASSERT(0 != violation);
599                         fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n       %s\n", opt, option_argument, violation);
600                         ok = false;
601                 }
602                 else {
603                         op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
604                         if(0 == op)
605                                 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
606                         local_strcat(&(op->argument.add_seekpoint.specification), spec);
607                         local_strcat(&(op->argument.add_seekpoint.specification), ";");
608                         free(spec);
609                 }
610         }
611         else if(0 == strcmp(opt, "add-replay-gain")) {
612                 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
613         }
614         else if(0 == strcmp(opt, "remove-replay-gain")) {
615                 const FLAC__byte * const tags[5] = {
616                         GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS,
617                         GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN,
618                         GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK,
619                         GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN,
620                         GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK
621                 };
622                 size_t i;
623                 for(i = 0; i < sizeof(tags)/sizeof(tags[0]); i++) {
624                         op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
625                         op->argument.vc_field_name.value = local_strdup((const char *)tags[i]);
626                 }
627         }
628         else if(0 == strcmp(opt, "add-padding")) {
629                 op = append_shorthand_operation(options, OP__ADD_PADDING);
630                 FLAC__ASSERT(0 != option_argument);
631                 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
632                         fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
633                         ok = false;
634                 }
635         }
636         else if(0 == strcmp(opt, "help")) {
637                 options->show_long_help = true;
638         }
639         else if(0 == strcmp(opt, "version")) {
640                 options->show_version = true;
641         }
642         else if(0 == strcmp(opt, "list")) {
643                 (void) append_major_operation(options, OP__LIST);
644         }
645         else if(0 == strcmp(opt, "append")) {
646                 (void) append_major_operation(options, OP__APPEND);
647         }
648         else if(0 == strcmp(opt, "remove")) {
649                 (void) append_major_operation(options, OP__REMOVE);
650         }
651         else if(0 == strcmp(opt, "remove-all")) {
652                 (void) append_major_operation(options, OP__REMOVE_ALL);
653         }
654         else if(0 == strcmp(opt, "merge-padding")) {
655                 (void) append_major_operation(options, OP__MERGE_PADDING);
656         }
657         else if(0 == strcmp(opt, "sort-padding")) {
658                 (void) append_major_operation(options, OP__SORT_PADDING);
659         }
660         else if(0 == strcmp(opt, "block-number")) {
661                 arg = append_argument(options, ARG__BLOCK_NUMBER);
662                 FLAC__ASSERT(0 != option_argument);
663                 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
664                         fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
665                         ok = false;
666                 }
667         }
668         else if(0 == strcmp(opt, "block-type")) {
669                 arg = append_argument(options, ARG__BLOCK_TYPE);
670                 FLAC__ASSERT(0 != option_argument);
671                 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
672                         fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
673                         ok = false;
674                 }
675                 options->args.checks.has_block_type = true;
676         }
677         else if(0 == strcmp(opt, "except-block-type")) {
678                 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
679                 FLAC__ASSERT(0 != option_argument);
680                 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
681                         fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
682                         ok = false;
683                 }
684                 options->args.checks.has_except_block_type = true;
685         }
686         else if(0 == strcmp(opt, "data-format")) {
687                 arg = append_argument(options, ARG__DATA_FORMAT);
688                 FLAC__ASSERT(0 != option_argument);
689                 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
690                         fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
691                         ok = false;
692                 }
693         }
694         else if(0 == strcmp(opt, "application-data-format")) {
695                 FLAC__ASSERT(0 != option_argument);
696                 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
697                         fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
698                         ok = false;
699                 }
700         }
701         else if(0 == strcmp(opt, "from-file")) {
702                 arg = append_argument(options, ARG__FROM_FILE);
703                 FLAC__ASSERT(0 != option_argument);
704                 arg->value.from_file.file_name = local_strdup(option_argument);
705         }
706         else {
707                 FLAC__ASSERT(0);
708         }
709
710         return ok;
711 }
712
713 void append_new_operation(CommandLineOptions *options, Operation operation)
714 {
715         if(options->ops.capacity == 0) {
716                 options->ops.capacity = 50;
717                 if(0 == (options->ops.operations = malloc(sizeof(Operation) * options->ops.capacity)))
718                         die("out of memory allocating space for option list");
719                 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
720         }
721         if(options->ops.capacity <= options->ops.num_operations) {
722                 unsigned original_capacity = options->ops.capacity;
723                 if(options->ops.capacity > UINT32_MAX / 2) /* overflow check */
724                         die("out of memory allocating space for option list");
725                 options->ops.capacity *= 2;
726                 if(0 == (options->ops.operations = safe_realloc_mul_2op_(options->ops.operations, sizeof(Operation), /*times*/options->ops.capacity)))
727                         die("out of memory allocating space for option list");
728                 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
729         }
730
731         options->ops.operations[options->ops.num_operations++] = operation;
732 }
733
734 void append_new_argument(CommandLineOptions *options, Argument argument)
735 {
736         if(options->args.capacity == 0) {
737                 options->args.capacity = 50;
738                 if(0 == (options->args.arguments = malloc(sizeof(Argument) * options->args.capacity)))
739                         die("out of memory allocating space for option list");
740                 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
741         }
742         if(options->args.capacity <= options->args.num_arguments) {
743                 unsigned original_capacity = options->args.capacity;
744                 if(options->args.capacity > UINT32_MAX / 2) /* overflow check */
745                         die("out of memory allocating space for option list");
746                 options->args.capacity *= 2;
747                 if(0 == (options->args.arguments = safe_realloc_mul_2op_(options->args.arguments, sizeof(Argument), /*times*/options->args.capacity)))
748                         die("out of memory allocating space for option list");
749                 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
750         }
751
752         options->args.arguments[options->args.num_arguments++] = argument;
753 }
754
755 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
756 {
757         Operation op;
758         memset(&op, 0, sizeof(op));
759         op.type = type;
760         append_new_operation(options, op);
761         options->args.checks.num_major_ops++;
762         return options->ops.operations + (options->ops.num_operations - 1);
763 }
764
765 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
766 {
767         Operation op;
768         memset(&op, 0, sizeof(op));
769         op.type = type;
770         append_new_operation(options, op);
771         options->args.checks.num_shorthand_ops++;
772         return options->ops.operations + (options->ops.num_operations - 1);
773 }
774
775 Argument *find_argument(CommandLineOptions *options, ArgumentType type)
776 {
777         unsigned i;
778         for(i = 0; i < options->args.num_arguments; i++)
779                 if(options->args.arguments[i].type == type)
780                         return &options->args.arguments[i];
781         return 0;
782 }
783
784 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
785 {
786         unsigned i;
787         for(i = 0; i < options->ops.num_operations; i++)
788                 if(options->ops.operations[i].type == type)
789                         return &options->ops.operations[i];
790         return 0;
791 }
792
793 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
794 {
795         Argument arg;
796         memset(&arg, 0, sizeof(arg));
797         arg.type = type;
798         append_new_argument(options, arg);
799         return options->args.arguments + (options->args.num_arguments - 1);
800 }
801
802 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
803 {
804         unsigned i, d;
805         int c;
806         FLAC__ASSERT(0 != src);
807         if(strlen(src) != 32)
808                 return false;
809         /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
810         for(i = 0; i < 16; i++) {
811                 c = (int)(*src++);
812                 if(isdigit(c))
813                         d = (unsigned)(c - '0');
814                 else if(c >= 'a' && c <= 'f')
815                         d = (unsigned)(c - 'a') + 10u;
816                 else if(c >= 'A' && c <= 'F')
817                         d = (unsigned)(c - 'A') + 10u;
818                 else
819                         return false;
820                 d <<= 4;
821                 c = (int)(*src++);
822                 if(isdigit(c))
823                         d |= (unsigned)(c - '0');
824                 else if(c >= 'a' && c <= 'f')
825                         d |= (unsigned)(c - 'a') + 10u;
826                 else if(c >= 'A' && c <= 'F')
827                         d |= (unsigned)(c - 'A') + 10u;
828                 else
829                         return false;
830                 dest[i] = (FLAC__byte)d;
831         }
832         return true;
833 }
834
835 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
836 {
837         FLAC__ASSERT(0 != src);
838         if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
839                 return false;
840         *dest = strtoul(src, 0, 10);
841         return true;
842 }
843
844 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
845 {
846         FLAC__ASSERT(0 != src);
847         if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
848                 return false;
849         *dest = strtoull(src, 0, 10);
850         return true;
851 }
852
853 FLAC__bool parse_string(const char *src, char **dest)
854 {
855         if(0 == src || strlen(src) == 0)
856                 return false;
857         *dest = strdup(src);
858         return true;
859 }
860
861 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
862 {
863         static const char * const violations[] = {
864                 "field name contains invalid character"
865         };
866
867         char *q, *s;
868
869         s = local_strdup(field_ref);
870
871         for(q = s; *q; q++) {
872                 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
873                         free(s);
874                         *violation = violations[0];
875                         return false;
876                 }
877         }
878
879         *name = s;
880
881         return true;
882 }
883
884 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
885 {
886         static const char *garbled_ = "garbled specification";
887         const unsigned n = strlen(in);
888
889         FLAC__ASSERT(0 != in);
890         FLAC__ASSERT(0 != out);
891
892         if(n == 0) {
893                 *violation = "specification is empty";
894                 return false;
895         }
896
897         if(n > strspn(in, "0123456789.Xsx")) {
898                 *violation = "specification contains invalid character";
899                 return false;
900         }
901
902         if(in[n-1] == 'X') {
903                 if(n > 1) {
904                         *violation = garbled_;
905                         return false;
906                 }
907         }
908         else if(in[n-1] == 's') {
909                 if(n-1 > strspn(in, "0123456789.")) {
910                         *violation = garbled_;
911                         return false;
912                 }
913         }
914         else if(in[n-1] == 'x') {
915                 if(n-1 > strspn(in, "0123456789")) {
916                         *violation = garbled_;
917                         return false;
918                 }
919         }
920         else {
921                 if(n > strspn(in, "0123456789")) {
922                         *violation = garbled_;
923                         return false;
924                 }
925         }
926
927         *out = local_strdup(in);
928         return true;
929 }
930
931 FLAC__bool parse_add_padding(const char *in, unsigned *out)
932 {
933         FLAC__ASSERT(0 != in);
934         FLAC__ASSERT(0 != out);
935         *out = (unsigned)strtoul(in, 0, 10);
936         return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
937 }
938
939 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
940 {
941         char *p, *q, *s, *end;
942         long i;
943         unsigned entry;
944
945         if(*in == '\0')
946                 return false;
947
948         s = local_strdup(in);
949
950         /* first count the entries */
951         for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
952                 ;
953
954         /* make space */
955         FLAC__ASSERT(out->num_entries > 0);
956         if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(unsigned), /*times*/out->num_entries)))
957                 die("out of memory allocating space for option list");
958
959         /* load 'em up */
960         entry = 0;
961         q = s;
962         while(q) {
963                 FLAC__ASSERT(entry < out->num_entries);
964                 if(0 != (p = strchr(q, ',')))
965                         *p++ = '\0';
966                 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
967                         free(s);
968                         return false;
969                 }
970                 out->entries[entry++] = (unsigned)i;
971                 q = p;
972         }
973         FLAC__ASSERT(entry == out->num_entries);
974
975         free(s);
976         return true;
977 }
978
979 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
980 {
981         char *p, *q, *r, *s;
982         unsigned entry;
983
984         if(*in == '\0')
985                 return false;
986
987         s = local_strdup(in);
988
989         /* first count the entries */
990         for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
991                 ;
992
993         /* make space */
994         FLAC__ASSERT(out->num_entries > 0);
995         if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(Argument_BlockTypeEntry), /*times*/out->num_entries)))
996                 die("out of memory allocating space for option list");
997
998         /* load 'em up */
999         entry = 0;
1000         q = s;
1001         while(q) {
1002                 FLAC__ASSERT(entry < out->num_entries);
1003                 if(0 != (p = strchr(q, ',')))
1004                         *p++ = 0;
1005                 r = strchr(q, ':');
1006                 if(r)
1007                         *r++ = '\0';
1008                 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
1009                         free(s);
1010                         return false;
1011                 }
1012                 if(0 == strcmp(q, "STREAMINFO")) {
1013                         out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
1014                 }
1015                 else if(0 == strcmp(q, "PADDING")) {
1016                         out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
1017                 }
1018                 else if(0 == strcmp(q, "APPLICATION")) {
1019                         out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
1020                         out->entries[entry].filter_application_by_id = (0 != r);
1021                         if(0 != r) {
1022                                 if(strlen(r) == sizeof (out->entries[entry].application_id)) {
1023                                         memcpy(out->entries[entry].application_id, r, sizeof (out->entries[entry].application_id));
1024                                 }
1025                                 else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
1026                                         FLAC__uint32 x = strtoul(r+2, 0, 16);
1027                                         out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
1028                                         out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
1029                                         out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
1030                                         out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
1031                                 }
1032                                 else {
1033                                         free(s);
1034                                         return false;
1035                                 }
1036                         }
1037                         entry++;
1038                 }
1039                 else if(0 == strcmp(q, "SEEKTABLE")) {
1040                         out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
1041                 }
1042                 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
1043                         out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1044                 }
1045                 else if(0 == strcmp(q, "CUESHEET")) {
1046                         out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
1047                 }
1048                 else if(0 == strcmp(q, "PICTURE")) {
1049                         out->entries[entry++].type = FLAC__METADATA_TYPE_PICTURE;
1050                 }
1051                 else {
1052                         free(s);
1053                         return false;
1054                 }
1055                 q = p;
1056         }
1057         FLAC__ASSERT(entry == out->num_entries);
1058
1059         free(s);
1060         return true;
1061 }
1062
1063 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1064 {
1065         if(0 == strcmp(in, "binary"))
1066                 out->is_binary = true;
1067         else if(0 == strcmp(in, "text"))
1068                 out->is_binary = false;
1069         else
1070                 return false;
1071         return true;
1072 }
1073
1074 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1075 {
1076         if(0 == strcmp(in, "hexdump"))
1077                 *out = true;
1078         else if(0 == strcmp(in, "text"))
1079                 *out = false;
1080         else
1081                 return false;
1082         return true;
1083 }
1084
1085 void undocumented_warning(const char *opt)
1086 {
1087         fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n         only for repairing a damaged STREAMINFO block\n", opt);
1088 }