1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson
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.
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.
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.
26 #include "FLAC/assert.h"
27 #include "share/grabbag/replaygain.h"
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.
37 struct share__option long_options_[] = {
39 { "preserve-modtime", 0, 0, 0 },
40 { "with-filename", 0, 0, 0 },
41 { "no-filename", 0, 0, 0 },
42 { "no-utf8-convert", 0, 0, 0 },
43 { "dont-use-padding", 0, 0, 0 },
44 { "no-cued-seekpoints", 0, 0, 0 },
45 /* shorthand operations */
46 { "show-md5sum", 0, 0, 0 },
47 { "show-min-blocksize", 0, 0, 0 },
48 { "show-max-blocksize", 0, 0, 0 },
49 { "show-min-framesize", 0, 0, 0 },
50 { "show-max-framesize", 0, 0, 0 },
51 { "show-sample-rate", 0, 0, 0 },
52 { "show-channels", 0, 0, 0 },
53 { "show-bps", 0, 0, 0 },
54 { "show-total-samples", 0, 0, 0 },
55 { "set-md5sum", 1, 0, 0 }, /* undocumented */
56 { "set-min-blocksize", 1, 0, 0 }, /* undocumented */
57 { "set-max-blocksize", 1, 0, 0 }, /* undocumented */
58 { "set-min-framesize", 1, 0, 0 }, /* undocumented */
59 { "set-max-framesize", 1, 0, 0 }, /* undocumented */
60 { "set-sample-rate", 1, 0, 0 }, /* undocumented */
61 { "set-channels", 1, 0, 0 }, /* undocumented */
62 { "set-bps", 1, 0, 0 }, /* undocumented */
63 { "set-total-samples", 1, 0, 0 }, /* undocumented */ /* WATCHOUT: used by test/test_flac.sh on windows */
64 { "show-vendor-tag", 0, 0, 0 },
65 { "show-tag", 1, 0, 0 },
66 { "remove-all-tags", 0, 0, 0 },
67 { "remove-tag", 1, 0, 0 },
68 { "remove-first-tag", 1, 0, 0 },
69 { "set-tag", 1, 0, 0 },
70 { "set-tag-from-file", 1, 0, 0 },
71 { "import-tags-from", 1, 0, 0 },
72 { "export-tags-to", 1, 0, 0 },
73 { "import-cuesheet-from", 1, 0, 0 },
74 { "export-cuesheet-to", 1, 0, 0 },
75 { "import-picture-from", 1, 0, 0 },
76 { "export-picture-to", 1, 0, 0 },
77 { "add-seekpoint", 1, 0, 0 },
78 { "add-replay-gain", 0, 0, 0 },
79 { "remove-replay-gain", 0, 0, 0 },
80 { "add-padding", 1, 0, 0 },
81 /* major operations */
83 { "version", 0, 0, 0 },
85 { "append", 0, 0, 0 },
86 { "remove", 0, 0, 0 },
87 { "remove-all", 0, 0, 0 },
88 { "merge-padding", 0, 0, 0 },
89 { "sort-padding", 0, 0, 0 },
90 /* major operation arguments */
91 { "block-number", 1, 0, 0 },
92 { "block-type", 1, 0, 0 },
93 { "except-block-type", 1, 0, 0 },
94 { "data-format", 1, 0, 0 },
95 { "application-data-format", 1, 0, 0 },
96 { "from-file", 1, 0, 0 },
100 static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options);
101 static void append_new_operation(CommandLineOptions *options, Operation operation);
102 static void append_new_argument(CommandLineOptions *options, Argument argument);
103 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
104 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
105 static Argument *find_argument(CommandLineOptions *options, ArgumentType type);
106 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
107 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
108 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
109 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
110 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
111 static FLAC__bool parse_string(const char *src, char **dest);
112 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
113 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
114 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
115 static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out);
116 static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out);
117 static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out);
118 static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out);
119 static void undocumented_warning(const char *opt);
122 void init_options(CommandLineOptions *options)
124 options->preserve_modtime = false;
126 /* '2' is a hack to mean "use default if not forced on command line" */
127 FLAC__ASSERT(true != 2);
128 options->prefix_with_filename = 2;
130 options->utf8_convert = true;
131 options->use_padding = true;
132 options->cued_seekpoints = true;
133 options->show_long_help = false;
134 options->show_version = false;
135 options->application_data_format_is_hexdump = false;
137 options->ops.operations = 0;
138 options->ops.num_operations = 0;
139 options->ops.capacity = 0;
141 options->args.arguments = 0;
142 options->args.num_arguments = 0;
143 options->args.capacity = 0;
145 options->args.checks.num_shorthand_ops = 0;
146 options->args.checks.num_major_ops = 0;
147 options->args.checks.has_block_type = false;
148 options->args.checks.has_except_block_type = false;
150 options->num_files = 0;
151 options->filenames = 0;
154 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
157 int option_index = 1;
158 FLAC__bool had_error = false;
160 while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
163 had_error |= !parse_option(option_index, share__optarg, options);
175 if(options->prefix_with_filename == 2)
176 options->prefix_with_filename = (argc - share__optind > 1);
178 if(share__optind >= argc && !options->show_long_help && !options->show_version) {
179 fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
180 fprintf(stderr," metaflac cannot be used as a pipe\n");
184 options->num_files = argc - share__optind;
186 if(options->num_files > 0) {
188 if(0 == (options->filenames = (char**)malloc(sizeof(char*) * options->num_files)))
189 die("out of memory allocating space for file names list");
190 while(share__optind < argc)
191 options->filenames[i++] = local_strdup(argv[share__optind++]);
194 if(options->args.checks.num_major_ops > 0) {
195 if(options->args.checks.num_major_ops > 1) {
196 fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
199 else if(options->args.checks.num_shorthand_ops > 0) {
200 fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
205 /* check for only one FLAC file used with certain options */
206 if(options->num_files > 1) {
207 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
208 fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from'\n");
211 if(0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) {
212 fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-cuesheet-to'\n");
215 if(0 != find_shorthand_operation(options, OP__EXPORT_PICTURE_TO)) {
216 fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-picture-to'\n");
220 0 != find_shorthand_operation(options, OP__IMPORT_VC_FROM) &&
221 0 == strcmp(find_shorthand_operation(options, OP__IMPORT_VC_FROM)->argument.filename.value, "-")
223 fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-tags-from=-'\n");
228 if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
229 fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
237 * We need to create an OP__ADD_SEEKPOINT operation if there is
238 * not one already, and --import-cuesheet-from was specified but
239 * --no-cued-seekpoints was not:
241 if(options->cued_seekpoints) {
242 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
244 Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
246 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
247 op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
254 void free_options(CommandLineOptions *options)
260 FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
261 FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
263 for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
265 case OP__SHOW_VC_FIELD:
266 case OP__REMOVE_VC_FIELD:
267 case OP__REMOVE_VC_FIRSTFIELD:
268 if(0 != op->argument.vc_field_name.value)
269 free(op->argument.vc_field_name.value);
271 case OP__SET_VC_FIELD:
272 if(0 != op->argument.vc_field.field)
273 free(op->argument.vc_field.field);
274 if(0 != op->argument.vc_field.field_name)
275 free(op->argument.vc_field.field_name);
276 if(0 != op->argument.vc_field.field_value)
277 free(op->argument.vc_field.field_value);
279 case OP__IMPORT_VC_FROM:
280 case OP__EXPORT_VC_TO:
281 case OP__EXPORT_CUESHEET_TO:
282 if(0 != op->argument.filename.value)
283 free(op->argument.filename.value);
285 case OP__IMPORT_CUESHEET_FROM:
286 if(0 != op->argument.import_cuesheet_from.filename)
287 free(op->argument.import_cuesheet_from.filename);
289 case OP__IMPORT_PICTURE_FROM:
290 if(0 != op->argument.specification.value)
291 free(op->argument.specification.value);
293 case OP__EXPORT_PICTURE_TO:
294 if(0 != op->argument.export_picture_to.filename)
295 free(op->argument.export_picture_to.filename);
297 case OP__ADD_SEEKPOINT:
298 if(0 != op->argument.add_seekpoint.specification)
299 free(op->argument.add_seekpoint.specification);
306 for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
308 case ARG__BLOCK_NUMBER:
309 if(0 != arg->value.block_number.entries)
310 free(arg->value.block_number.entries);
312 case ARG__BLOCK_TYPE:
313 case ARG__EXCEPT_BLOCK_TYPE:
314 if(0 != arg->value.block_type.entries)
315 free(arg->value.block_type.entries);
318 if(0 != arg->value.from_file.file_name)
319 free(arg->value.from_file.file_name);
326 if(0 != options->ops.operations)
327 free(options->ops.operations);
329 if(0 != options->args.arguments)
330 free(options->args.arguments);
332 if(0 != options->filenames) {
333 for(i = 0; i < options->num_files; i++) {
334 if(0 != options->filenames[i])
335 free(options->filenames[i]);
337 free(options->filenames);
345 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
347 const char *opt = long_options_[option_index].name;
350 FLAC__bool ok = true;
352 if(0 == strcmp(opt, "preserve-modtime")) {
353 options->preserve_modtime = true;
355 else if(0 == strcmp(opt, "with-filename")) {
356 options->prefix_with_filename = true;
358 else if(0 == strcmp(opt, "no-filename")) {
359 options->prefix_with_filename = false;
361 else if(0 == strcmp(opt, "no-utf8-convert")) {
362 options->utf8_convert = false;
364 else if(0 == strcmp(opt, "dont-use-padding")) {
365 options->use_padding = false;
367 else if(0 == strcmp(opt, "no-cued-seekpoints")) {
368 options->cued_seekpoints = false;
370 else if(0 == strcmp(opt, "show-md5sum")) {
371 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
373 else if(0 == strcmp(opt, "show-min-blocksize")) {
374 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
376 else if(0 == strcmp(opt, "show-max-blocksize")) {
377 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
379 else if(0 == strcmp(opt, "show-min-framesize")) {
380 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
382 else if(0 == strcmp(opt, "show-max-framesize")) {
383 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
385 else if(0 == strcmp(opt, "show-sample-rate")) {
386 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
388 else if(0 == strcmp(opt, "show-channels")) {
389 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
391 else if(0 == strcmp(opt, "show-bps")) {
392 (void) append_shorthand_operation(options, OP__SHOW_BPS);
394 else if(0 == strcmp(opt, "show-total-samples")) {
395 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
397 else if(0 == strcmp(opt, "set-md5sum")) {
398 op = append_shorthand_operation(options, OP__SET_MD5SUM);
399 FLAC__ASSERT(0 != option_argument);
400 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
401 fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
405 undocumented_warning(opt);
407 else if(0 == strcmp(opt, "set-min-blocksize")) {
408 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
409 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
410 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
414 undocumented_warning(opt);
416 else if(0 == strcmp(opt, "set-max-blocksize")) {
417 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
418 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
419 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
423 undocumented_warning(opt);
425 else if(0 == strcmp(opt, "set-min-framesize")) {
426 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
427 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
428 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
432 undocumented_warning(opt);
434 else if(0 == strcmp(opt, "set-max-framesize")) {
435 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
436 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
437 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
441 undocumented_warning(opt);
443 else if(0 == strcmp(opt, "set-sample-rate")) {
444 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
445 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
446 fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
450 undocumented_warning(opt);
452 else if(0 == strcmp(opt, "set-channels")) {
453 op = append_shorthand_operation(options, OP__SET_CHANNELS);
454 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
455 fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
459 undocumented_warning(opt);
461 else if(0 == strcmp(opt, "set-bps")) {
462 op = append_shorthand_operation(options, OP__SET_BPS);
463 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BITS_PER_SAMPLE || op->argument.streaminfo_uint32.value > FLAC__MAX_BITS_PER_SAMPLE) {
464 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
468 undocumented_warning(opt);
470 else if(0 == strcmp(opt, "set-total-samples")) {
471 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
472 if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (((FLAC__uint64)1)<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
473 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
477 undocumented_warning(opt);
479 else if(0 == strcmp(opt, "show-vendor-tag")) {
480 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
482 else if(0 == strcmp(opt, "show-tag")) {
483 const char *violation;
484 op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
485 FLAC__ASSERT(0 != option_argument);
486 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
487 FLAC__ASSERT(0 != violation);
488 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
492 else if(0 == strcmp(opt, "remove-all-tags")) {
493 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
495 else if(0 == strcmp(opt, "remove-tag")) {
496 const char *violation;
497 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
498 FLAC__ASSERT(0 != option_argument);
499 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
500 FLAC__ASSERT(0 != violation);
501 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
505 else if(0 == strcmp(opt, "remove-first-tag")) {
506 const char *violation;
507 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
508 FLAC__ASSERT(0 != option_argument);
509 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
510 FLAC__ASSERT(0 != violation);
511 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
515 else if(0 == strcmp(opt, "set-tag")) {
516 const char *violation;
517 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
518 FLAC__ASSERT(0 != option_argument);
519 op->argument.vc_field.field_value_from_file = false;
520 if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
521 FLAC__ASSERT(0 != violation);
522 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
526 else if(0 == strcmp(opt, "set-tag-from-file")) {
527 const char *violation;
528 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
529 FLAC__ASSERT(0 != option_argument);
530 op->argument.vc_field.field_value_from_file = true;
531 if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
532 FLAC__ASSERT(0 != violation);
533 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
537 else if(0 == strcmp(opt, "import-tags-from")) {
538 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
539 FLAC__ASSERT(0 != option_argument);
540 if(!parse_string(option_argument, &(op->argument.filename.value))) {
541 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
545 else if(0 == strcmp(opt, "export-tags-to")) {
546 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
547 FLAC__ASSERT(0 != option_argument);
548 if(!parse_string(option_argument, &(op->argument.filename.value))) {
549 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
553 else if(0 == strcmp(opt, "import-cuesheet-from")) {
554 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
555 fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
558 op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
559 FLAC__ASSERT(0 != option_argument);
560 if(!parse_string(option_argument, &(op->argument.import_cuesheet_from.filename))) {
561 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
565 else if(0 == strcmp(opt, "export-cuesheet-to")) {
566 op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
567 FLAC__ASSERT(0 != option_argument);
568 if(!parse_string(option_argument, &(op->argument.filename.value))) {
569 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
573 else if(0 == strcmp(opt, "import-picture-from")) {
574 op = append_shorthand_operation(options, OP__IMPORT_PICTURE_FROM);
575 FLAC__ASSERT(0 != option_argument);
576 if(!parse_string(option_argument, &(op->argument.specification.value))) {
577 fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
581 else if(0 == strcmp(opt, "export-picture-to")) {
582 const Argument *arg = find_argument(options, ARG__BLOCK_NUMBER);
583 op = append_shorthand_operation(options, OP__EXPORT_PICTURE_TO);
584 FLAC__ASSERT(0 != option_argument);
585 if(!parse_string(option_argument, &(op->argument.export_picture_to.filename))) {
586 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
589 op->argument.export_picture_to.block_number_link = arg? &(arg->value.block_number) : 0;
591 else if(0 == strcmp(opt, "add-seekpoint")) {
592 const char *violation;
594 FLAC__ASSERT(0 != option_argument);
595 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
596 FLAC__ASSERT(0 != violation);
597 fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation);
601 op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
603 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
604 local_strcat(&(op->argument.add_seekpoint.specification), spec);
605 local_strcat(&(op->argument.add_seekpoint.specification), ";");
609 else if(0 == strcmp(opt, "add-replay-gain")) {
610 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
612 else if(0 == strcmp(opt, "remove-replay-gain")) {
613 const FLAC__byte * const tags[5] = {
614 GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS,
615 GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN,
616 GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK,
617 GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN,
618 GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK
621 for(i = 0; i < sizeof(tags)/sizeof(tags[0]); i++) {
622 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
623 op->argument.vc_field_name.value = local_strdup((const char *)tags[i]);
626 else if(0 == strcmp(opt, "add-padding")) {
627 op = append_shorthand_operation(options, OP__ADD_PADDING);
628 FLAC__ASSERT(0 != option_argument);
629 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
630 fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
634 else if(0 == strcmp(opt, "help")) {
635 options->show_long_help = true;
637 else if(0 == strcmp(opt, "version")) {
638 options->show_version = true;
640 else if(0 == strcmp(opt, "list")) {
641 (void) append_major_operation(options, OP__LIST);
643 else if(0 == strcmp(opt, "append")) {
644 (void) append_major_operation(options, OP__APPEND);
646 else if(0 == strcmp(opt, "remove")) {
647 (void) append_major_operation(options, OP__REMOVE);
649 else if(0 == strcmp(opt, "remove-all")) {
650 (void) append_major_operation(options, OP__REMOVE_ALL);
652 else if(0 == strcmp(opt, "merge-padding")) {
653 (void) append_major_operation(options, OP__MERGE_PADDING);
655 else if(0 == strcmp(opt, "sort-padding")) {
656 (void) append_major_operation(options, OP__SORT_PADDING);
658 else if(0 == strcmp(opt, "block-number")) {
659 arg = append_argument(options, ARG__BLOCK_NUMBER);
660 FLAC__ASSERT(0 != option_argument);
661 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
662 fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
666 else if(0 == strcmp(opt, "block-type")) {
667 arg = append_argument(options, ARG__BLOCK_TYPE);
668 FLAC__ASSERT(0 != option_argument);
669 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
670 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
673 options->args.checks.has_block_type = true;
675 else if(0 == strcmp(opt, "except-block-type")) {
676 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
677 FLAC__ASSERT(0 != option_argument);
678 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
679 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
682 options->args.checks.has_except_block_type = true;
684 else if(0 == strcmp(opt, "data-format")) {
685 arg = append_argument(options, ARG__DATA_FORMAT);
686 FLAC__ASSERT(0 != option_argument);
687 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
688 fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
692 else if(0 == strcmp(opt, "application-data-format")) {
693 FLAC__ASSERT(0 != option_argument);
694 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
695 fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
699 else if(0 == strcmp(opt, "from-file")) {
700 arg = append_argument(options, ARG__FROM_FILE);
701 FLAC__ASSERT(0 != option_argument);
702 arg->value.from_file.file_name = local_strdup(option_argument);
711 void append_new_operation(CommandLineOptions *options, Operation operation)
713 if(options->ops.capacity == 0) {
714 options->ops.capacity = 50;
715 if(0 == (options->ops.operations = (Operation*)malloc(sizeof(Operation) * options->ops.capacity)))
716 die("out of memory allocating space for option list");
717 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
719 if(options->ops.capacity <= options->ops.num_operations) {
720 unsigned original_capacity = options->ops.capacity;
721 options->ops.capacity *= 4;
722 if(0 == (options->ops.operations = (Operation*)realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
723 die("out of memory allocating space for option list");
724 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
727 options->ops.operations[options->ops.num_operations++] = operation;
730 void append_new_argument(CommandLineOptions *options, Argument argument)
732 if(options->args.capacity == 0) {
733 options->args.capacity = 50;
734 if(0 == (options->args.arguments = (Argument*)malloc(sizeof(Argument) * options->args.capacity)))
735 die("out of memory allocating space for option list");
736 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
738 if(options->args.capacity <= options->args.num_arguments) {
739 unsigned original_capacity = options->args.capacity;
740 options->args.capacity *= 4;
741 if(0 == (options->args.arguments = (Argument*)realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
742 die("out of memory allocating space for option list");
743 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
746 options->args.arguments[options->args.num_arguments++] = argument;
749 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
752 memset(&op, 0, sizeof(op));
754 append_new_operation(options, op);
755 options->args.checks.num_major_ops++;
756 return options->ops.operations + (options->ops.num_operations - 1);
759 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
762 memset(&op, 0, sizeof(op));
764 append_new_operation(options, op);
765 options->args.checks.num_shorthand_ops++;
766 return options->ops.operations + (options->ops.num_operations - 1);
769 Argument *find_argument(CommandLineOptions *options, ArgumentType type)
772 for(i = 0; i < options->args.num_arguments; i++)
773 if(options->args.arguments[i].type == type)
774 return &options->args.arguments[i];
778 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
781 for(i = 0; i < options->ops.num_operations; i++)
782 if(options->ops.operations[i].type == type)
783 return &options->ops.operations[i];
787 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
790 memset(&arg, 0, sizeof(arg));
792 append_new_argument(options, arg);
793 return options->args.arguments + (options->args.num_arguments - 1);
796 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
800 FLAC__ASSERT(0 != src);
801 if(strlen(src) != 32)
803 /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
804 for(i = 0; i < 16; i++) {
807 d = (unsigned)(c - '0');
808 else if(c >= 'a' && c <= 'f')
809 d = (unsigned)(c - 'a') + 10u;
810 else if(c >= 'A' && c <= 'F')
811 d = (unsigned)(c - 'A') + 10u;
817 d |= (unsigned)(c - '0');
818 else if(c >= 'a' && c <= 'f')
819 d |= (unsigned)(c - 'a') + 10u;
820 else if(c >= 'A' && c <= 'F')
821 d |= (unsigned)(c - 'A') + 10u;
824 dest[i] = (FLAC__byte)d;
829 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
831 FLAC__ASSERT(0 != src);
832 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
834 *dest = strtoul(src, 0, 10);
839 /* There's no strtoull() in MSVC6 so we just write a specialized one */
840 static FLAC__uint64 local__strtoull(const char *src)
842 FLAC__uint64 ret = 0;
844 FLAC__ASSERT(0 != src);
845 while(0 != (c = *src++)) {
848 ret = (ret * 10) + c;
856 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
858 FLAC__ASSERT(0 != src);
859 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
862 *dest = local__strtoull(src);
864 *dest = strtoull(src, 0, 10);
869 FLAC__bool parse_string(const char *src, char **dest)
871 if(0 == src || strlen(src) == 0)
877 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
879 static const char * const violations[] = {
880 "field name contains invalid character"
885 s = local_strdup(field_ref);
887 for(q = s; *q; q++) {
888 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
890 *violation = violations[0];
900 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
902 static const char *garbled_ = "garbled specification";
903 const unsigned n = strlen(in);
905 FLAC__ASSERT(0 != in);
906 FLAC__ASSERT(0 != out);
909 *violation = "specification is empty";
913 if(n > strspn(in, "0123456789.Xsx")) {
914 *violation = "specification contains invalid character";
920 *violation = garbled_;
924 else if(in[n-1] == 's') {
925 if(n-1 > strspn(in, "0123456789.")) {
926 *violation = garbled_;
930 else if(in[n-1] == 'x') {
931 if(n-1 > strspn(in, "0123456789")) {
932 *violation = garbled_;
937 if(n > strspn(in, "0123456789")) {
938 *violation = garbled_;
943 *out = local_strdup(in);
947 FLAC__bool parse_add_padding(const char *in, unsigned *out)
949 FLAC__ASSERT(0 != in);
950 FLAC__ASSERT(0 != out);
951 *out = (unsigned)strtoul(in, 0, 10);
952 return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
955 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
957 char *p, *q, *s, *end;
964 s = local_strdup(in);
966 /* first count the entries */
967 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
971 FLAC__ASSERT(out->num_entries > 0);
972 if(0 == (out->entries = (unsigned*)malloc(sizeof(unsigned) * out->num_entries)))
973 die("out of memory allocating space for option list");
979 FLAC__ASSERT(entry < out->num_entries);
980 if(0 != (p = strchr(q, ',')))
982 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
986 out->entries[entry++] = (unsigned)i;
989 FLAC__ASSERT(entry == out->num_entries);
995 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
1003 s = local_strdup(in);
1005 /* first count the entries */
1006 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
1010 FLAC__ASSERT(out->num_entries > 0);
1011 if(0 == (out->entries = (Argument_BlockTypeEntry*)malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
1012 die("out of memory allocating space for option list");
1018 FLAC__ASSERT(entry < out->num_entries);
1019 if(0 != (p = strchr(q, ',')))
1024 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
1028 if(0 == strcmp(q, "STREAMINFO")) {
1029 out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
1031 else if(0 == strcmp(q, "PADDING")) {
1032 out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
1034 else if(0 == strcmp(q, "APPLICATION")) {
1035 out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
1036 out->entries[entry].filter_application_by_id = (0 != r);
1038 if(strlen(r) == 4) {
1039 strcpy(out->entries[entry].application_id, r);
1041 else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
1042 FLAC__uint32 x = strtoul(r+2, 0, 16);
1043 out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
1044 out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
1045 out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
1046 out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
1055 else if(0 == strcmp(q, "SEEKTABLE")) {
1056 out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
1058 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
1059 out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1061 else if(0 == strcmp(q, "CUESHEET")) {
1062 out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
1064 else if(0 == strcmp(q, "PICTURE")) {
1065 out->entries[entry++].type = FLAC__METADATA_TYPE_PICTURE;
1073 FLAC__ASSERT(entry == out->num_entries);
1079 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1081 if(0 == strcmp(in, "binary"))
1082 out->is_binary = true;
1083 else if(0 == strcmp(in, "text"))
1084 out->is_binary = false;
1090 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1092 if(0 == strcmp(in, "hexdump"))
1094 else if(0 == strcmp(in, "text"))
1101 void undocumented_warning(const char *opt)
1103 fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt);