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