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