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