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