1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004,2005,2006 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", 1, 0, 0 },
76 { "add-seekpoint", 1, 0, 0 },
77 { "add-replay-gain", 0, 0, 0 },
78 { "remove-replay-gain", 0, 0, 0 },
79 { "add-padding", 1, 0, 0 },
80 /* major operations */
82 { "version", 0, 0, 0 },
84 { "append", 0, 0, 0 },
85 { "remove", 0, 0, 0 },
86 { "remove-all", 0, 0, 0 },
87 { "merge-padding", 0, 0, 0 },
88 { "sort-padding", 0, 0, 0 },
89 /* major operation arguments */
90 { "block-number", 1, 0, 0 },
91 { "block-type", 1, 0, 0 },
92 { "except-block-type", 1, 0, 0 },
93 { "data-format", 1, 0, 0 },
94 { "application-data-format", 1, 0, 0 },
95 { "from-file", 1, 0, 0 },
99 static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options);
100 static void append_new_operation(CommandLineOptions *options, Operation operation);
101 static void append_new_argument(CommandLineOptions *options, Argument argument);
102 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
103 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
104 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
105 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
106 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
107 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
108 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
109 static FLAC__bool parse_string(const char *src, char **dest);
110 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
111 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
112 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
113 static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out);
114 static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out);
115 static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out);
116 static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out);
117 static void undocumented_warning(const char *opt);
120 void init_options(CommandLineOptions *options)
122 options->preserve_modtime = false;
124 /* '2' is a hack to mean "use default if not forced on command line" */
125 FLAC__ASSERT(true != 2);
126 options->prefix_with_filename = 2;
128 options->utf8_convert = true;
129 options->use_padding = true;
130 options->cued_seekpoints = true;
131 options->show_long_help = false;
132 options->show_version = false;
133 options->application_data_format_is_hexdump = false;
135 options->ops.operations = 0;
136 options->ops.num_operations = 0;
137 options->ops.capacity = 0;
139 options->args.arguments = 0;
140 options->args.num_arguments = 0;
141 options->args.capacity = 0;
143 options->args.checks.num_shorthand_ops = 0;
144 options->args.checks.num_major_ops = 0;
145 options->args.checks.has_block_type = false;
146 options->args.checks.has_except_block_type = false;
148 options->num_files = 0;
149 options->filenames = 0;
152 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
155 int option_index = 1;
156 FLAC__bool had_error = false;
158 while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
161 had_error |= !parse_option(option_index, share__optarg, options);
173 if(options->prefix_with_filename == 2)
174 options->prefix_with_filename = (argc - share__optind > 1);
176 if(share__optind >= argc && !options->show_long_help && !options->show_version) {
177 fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
178 fprintf(stderr," metaflac cannot be used as a pipe\n");
182 options->num_files = argc - share__optind;
184 if(options->num_files > 0) {
186 if(0 == (options->filenames = (char**)malloc(sizeof(char*) * options->num_files)))
187 die("out of memory allocating space for file names list");
188 while(share__optind < argc)
189 options->filenames[i++] = local_strdup(argv[share__optind++]);
192 if(options->args.checks.num_major_ops > 0) {
193 if(options->args.checks.num_major_ops > 1) {
194 fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
197 else if(options->args.checks.num_shorthand_ops > 0) {
198 fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
203 /* check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to */
204 if((0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) || 0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) && options->num_files > 1) {
205 fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from' or '--export-cuesheet-to'\n");
209 if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
210 fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
218 * We need to create an OP__ADD_SEEKPOINT operation if there is
219 * not one already, and --import-cuesheet-from was specified but
220 * --no-cued-seekpoints was not:
222 if(options->cued_seekpoints) {
223 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
225 Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
227 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
228 op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
235 void free_options(CommandLineOptions *options)
241 FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
242 FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
244 for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
246 case OP__SHOW_VC_FIELD:
247 case OP__REMOVE_VC_FIELD:
248 case OP__REMOVE_VC_FIRSTFIELD:
249 if(0 != op->argument.vc_field_name.value)
250 free(op->argument.vc_field_name.value);
252 case OP__SET_VC_FIELD:
253 if(0 != op->argument.vc_field.field)
254 free(op->argument.vc_field.field);
255 if(0 != op->argument.vc_field.field_name)
256 free(op->argument.vc_field.field_name);
257 if(0 != op->argument.vc_field.field_value)
258 free(op->argument.vc_field.field_value);
260 case OP__IMPORT_VC_FROM:
261 case OP__EXPORT_VC_TO:
262 case OP__EXPORT_CUESHEET_TO:
263 if(0 != op->argument.filename.value)
264 free(op->argument.filename.value);
266 case OP__IMPORT_CUESHEET_FROM:
267 if(0 != op->argument.import_cuesheet_from.filename)
268 free(op->argument.import_cuesheet_from.filename);
270 case OP__IMPORT_PICTURE:
271 if(0 != op->argument.specification.value)
272 free(op->argument.specification.value);
274 case OP__ADD_SEEKPOINT:
275 if(0 != op->argument.add_seekpoint.specification)
276 free(op->argument.add_seekpoint.specification);
283 for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
285 case ARG__BLOCK_NUMBER:
286 if(0 != arg->value.block_number.entries)
287 free(arg->value.block_number.entries);
289 case ARG__BLOCK_TYPE:
290 case ARG__EXCEPT_BLOCK_TYPE:
291 if(0 != arg->value.block_type.entries)
292 free(arg->value.block_type.entries);
295 if(0 != arg->value.from_file.file_name)
296 free(arg->value.from_file.file_name);
303 if(0 != options->ops.operations)
304 free(options->ops.operations);
306 if(0 != options->args.arguments)
307 free(options->args.arguments);
309 if(0 != options->filenames) {
310 for(i = 0; i < options->num_files; i++) {
311 if(0 != options->filenames[i])
312 free(options->filenames[i]);
314 free(options->filenames);
322 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
324 const char *opt = long_options_[option_index].name;
327 FLAC__bool ok = true;
329 if(0 == strcmp(opt, "preserve-modtime")) {
330 options->preserve_modtime = true;
332 else if(0 == strcmp(opt, "with-filename")) {
333 options->prefix_with_filename = true;
335 else if(0 == strcmp(opt, "no-filename")) {
336 options->prefix_with_filename = false;
338 else if(0 == strcmp(opt, "no-utf8-convert")) {
339 options->utf8_convert = false;
341 else if(0 == strcmp(opt, "dont-use-padding")) {
342 options->use_padding = false;
344 else if(0 == strcmp(opt, "no-cued-seekpoints")) {
345 options->cued_seekpoints = false;
347 else if(0 == strcmp(opt, "show-md5sum")) {
348 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
350 else if(0 == strcmp(opt, "show-min-blocksize")) {
351 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
353 else if(0 == strcmp(opt, "show-max-blocksize")) {
354 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
356 else if(0 == strcmp(opt, "show-min-framesize")) {
357 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
359 else if(0 == strcmp(opt, "show-max-framesize")) {
360 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
362 else if(0 == strcmp(opt, "show-sample-rate")) {
363 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
365 else if(0 == strcmp(opt, "show-channels")) {
366 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
368 else if(0 == strcmp(opt, "show-bps")) {
369 (void) append_shorthand_operation(options, OP__SHOW_BPS);
371 else if(0 == strcmp(opt, "show-total-samples")) {
372 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
374 else if(0 == strcmp(opt, "set-md5sum")) {
375 op = append_shorthand_operation(options, OP__SET_MD5SUM);
376 FLAC__ASSERT(0 != option_argument);
377 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
378 fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
382 undocumented_warning(opt);
384 else if(0 == strcmp(opt, "set-min-blocksize")) {
385 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
386 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) {
387 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
391 undocumented_warning(opt);
393 else if(0 == strcmp(opt, "set-max-blocksize")) {
394 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
395 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) {
396 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
400 undocumented_warning(opt);
402 else if(0 == strcmp(opt, "set-min-framesize")) {
403 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
404 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
405 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
409 undocumented_warning(opt);
411 else if(0 == strcmp(opt, "set-max-framesize")) {
412 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
413 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
414 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
418 undocumented_warning(opt);
420 else if(0 == strcmp(opt, "set-sample-rate")) {
421 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
422 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
423 fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
427 undocumented_warning(opt);
429 else if(0 == strcmp(opt, "set-channels")) {
430 op = append_shorthand_operation(options, OP__SET_CHANNELS);
431 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
432 fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
436 undocumented_warning(opt);
438 else if(0 == strcmp(opt, "set-bps")) {
439 op = append_shorthand_operation(options, OP__SET_BPS);
440 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) {
441 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
445 undocumented_warning(opt);
447 else if(0 == strcmp(opt, "set-total-samples")) {
448 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
449 if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
450 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
454 undocumented_warning(opt);
456 else if(0 == strcmp(opt, "show-vendor-tag")) {
457 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
459 else if(0 == strcmp(opt, "show-tag")) {
460 const char *violation;
461 op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
462 FLAC__ASSERT(0 != option_argument);
463 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
464 FLAC__ASSERT(0 != violation);
465 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
469 else if(0 == strcmp(opt, "remove-all-tags")) {
470 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
472 else if(0 == strcmp(opt, "remove-tag")) {
473 const char *violation;
474 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
475 FLAC__ASSERT(0 != option_argument);
476 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
477 FLAC__ASSERT(0 != violation);
478 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
482 else if(0 == strcmp(opt, "remove-first-tag")) {
483 const char *violation;
484 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
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, "set-tag")) {
493 const char *violation;
494 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
495 FLAC__ASSERT(0 != option_argument);
496 op->argument.vc_field.field_value_from_file = false;
497 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)) {
498 FLAC__ASSERT(0 != violation);
499 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
503 else if(0 == strcmp(opt, "set-tag-from-file")) {
504 const char *violation;
505 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
506 FLAC__ASSERT(0 != option_argument);
507 op->argument.vc_field.field_value_from_file = true;
508 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)) {
509 FLAC__ASSERT(0 != violation);
510 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
514 else if(0 == strcmp(opt, "import-tags-from")) {
515 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
516 FLAC__ASSERT(0 != option_argument);
517 if(!parse_string(option_argument, &(op->argument.filename.value))) {
518 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
522 else if(0 == strcmp(opt, "export-tags-to")) {
523 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
524 FLAC__ASSERT(0 != option_argument);
525 if(!parse_string(option_argument, &(op->argument.filename.value))) {
526 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
530 else if(0 == strcmp(opt, "import-cuesheet-from")) {
531 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
532 fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
535 op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
536 FLAC__ASSERT(0 != option_argument);
537 if(!parse_string(option_argument, &(op->argument.import_cuesheet_from.filename))) {
538 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
542 else if(0 == strcmp(opt, "export-cuesheet-to")) {
543 op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
544 FLAC__ASSERT(0 != option_argument);
545 if(!parse_string(option_argument, &(op->argument.filename.value))) {
546 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
550 else if(0 == strcmp(opt, "import-picture")) {
551 op = append_shorthand_operation(options, OP__IMPORT_PICTURE);
552 FLAC__ASSERT(0 != option_argument);
553 if(!parse_string(option_argument, &(op->argument.specification.value))) {
554 fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
558 else if(0 == strcmp(opt, "add-seekpoint")) {
559 const char *violation;
561 FLAC__ASSERT(0 != option_argument);
562 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
563 FLAC__ASSERT(0 != violation);
564 fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation);
568 op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
570 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
571 local_strcat(&(op->argument.add_seekpoint.specification), spec);
572 local_strcat(&(op->argument.add_seekpoint.specification), ";");
576 else if(0 == strcmp(opt, "add-replay-gain")) {
577 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
579 else if(0 == strcmp(opt, "remove-replay-gain")) {
580 const FLAC__byte * const tags[5] = {
581 GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS,
582 GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN,
583 GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK,
584 GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN,
585 GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK
588 for(i = 0; i < sizeof(tags)/sizeof(tags[0]); i++) {
589 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
590 op->argument.vc_field_name.value = local_strdup((const char *)tags[i]);
593 else if(0 == strcmp(opt, "add-padding")) {
594 op = append_shorthand_operation(options, OP__ADD_PADDING);
595 FLAC__ASSERT(0 != option_argument);
596 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
597 fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
601 else if(0 == strcmp(opt, "help")) {
602 options->show_long_help = true;
604 else if(0 == strcmp(opt, "version")) {
605 options->show_version = true;
607 else if(0 == strcmp(opt, "list")) {
608 (void) append_major_operation(options, OP__LIST);
610 else if(0 == strcmp(opt, "append")) {
611 (void) append_major_operation(options, OP__APPEND);
613 else if(0 == strcmp(opt, "remove")) {
614 (void) append_major_operation(options, OP__REMOVE);
616 else if(0 == strcmp(opt, "remove-all")) {
617 (void) append_major_operation(options, OP__REMOVE_ALL);
619 else if(0 == strcmp(opt, "merge-padding")) {
620 (void) append_major_operation(options, OP__MERGE_PADDING);
622 else if(0 == strcmp(opt, "sort-padding")) {
623 (void) append_major_operation(options, OP__SORT_PADDING);
625 else if(0 == strcmp(opt, "block-number")) {
626 arg = append_argument(options, ARG__BLOCK_NUMBER);
627 FLAC__ASSERT(0 != option_argument);
628 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
629 fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
633 else if(0 == strcmp(opt, "block-type")) {
634 arg = append_argument(options, ARG__BLOCK_TYPE);
635 FLAC__ASSERT(0 != option_argument);
636 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
637 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
640 options->args.checks.has_block_type = true;
642 else if(0 == strcmp(opt, "except-block-type")) {
643 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
644 FLAC__ASSERT(0 != option_argument);
645 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
646 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
649 options->args.checks.has_except_block_type = true;
651 else if(0 == strcmp(opt, "data-format")) {
652 arg = append_argument(options, ARG__DATA_FORMAT);
653 FLAC__ASSERT(0 != option_argument);
654 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
655 fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
659 else if(0 == strcmp(opt, "application-data-format")) {
660 FLAC__ASSERT(0 != option_argument);
661 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
662 fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
666 else if(0 == strcmp(opt, "from-file")) {
667 arg = append_argument(options, ARG__FROM_FILE);
668 FLAC__ASSERT(0 != option_argument);
669 arg->value.from_file.file_name = local_strdup(option_argument);
678 void append_new_operation(CommandLineOptions *options, Operation operation)
680 if(options->ops.capacity == 0) {
681 options->ops.capacity = 50;
682 if(0 == (options->ops.operations = (Operation*)malloc(sizeof(Operation) * options->ops.capacity)))
683 die("out of memory allocating space for option list");
684 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
686 if(options->ops.capacity <= options->ops.num_operations) {
687 unsigned original_capacity = options->ops.capacity;
688 options->ops.capacity *= 4;
689 if(0 == (options->ops.operations = (Operation*)realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
690 die("out of memory allocating space for option list");
691 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
694 options->ops.operations[options->ops.num_operations++] = operation;
697 void append_new_argument(CommandLineOptions *options, Argument argument)
699 if(options->args.capacity == 0) {
700 options->args.capacity = 50;
701 if(0 == (options->args.arguments = (Argument*)malloc(sizeof(Argument) * options->args.capacity)))
702 die("out of memory allocating space for option list");
703 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
705 if(options->args.capacity <= options->args.num_arguments) {
706 unsigned original_capacity = options->args.capacity;
707 options->args.capacity *= 4;
708 if(0 == (options->args.arguments = (Argument*)realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
709 die("out of memory allocating space for option list");
710 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
713 options->args.arguments[options->args.num_arguments++] = argument;
716 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
719 memset(&op, 0, sizeof(op));
721 append_new_operation(options, op);
722 options->args.checks.num_major_ops++;
723 return options->ops.operations + (options->ops.num_operations - 1);
726 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
729 memset(&op, 0, sizeof(op));
731 append_new_operation(options, op);
732 options->args.checks.num_shorthand_ops++;
733 return options->ops.operations + (options->ops.num_operations - 1);
736 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
739 for(i = 0; i < options->ops.num_operations; i++)
740 if(options->ops.operations[i].type == type)
741 return &options->ops.operations[i];
745 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
748 memset(&arg, 0, sizeof(arg));
750 append_new_argument(options, arg);
751 return options->args.arguments + (options->args.num_arguments - 1);
754 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
758 FLAC__ASSERT(0 != src);
759 if(strlen(src) != 32)
761 /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
762 for(i = 0; i < 16; i++) {
765 d = (unsigned)(c - '0');
766 else if(c >= 'a' && c <= 'f')
767 d = (unsigned)(c - 'a') + 10u;
768 else if(c >= 'A' && c <= 'F')
769 d = (unsigned)(c - 'A') + 10u;
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;
782 dest[i] = (FLAC__byte)d;
787 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
789 FLAC__ASSERT(0 != src);
790 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
792 *dest = strtoul(src, 0, 10);
796 /* There's no stroull() in MSVC6 so we just write a specialized one */
797 static FLAC__uint64 local__strtoull(const char *src)
799 FLAC__uint64 ret = 0;
801 FLAC__ASSERT(0 != src);
802 while(0 != (c = *src++)) {
805 ret = (ret * 10) + c;
812 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
814 FLAC__ASSERT(0 != src);
815 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
817 *dest = local__strtoull(src);
821 FLAC__bool parse_string(const char *src, char **dest)
823 if(0 == src || strlen(src) == 0)
829 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
831 static const char * const violations[] = {
832 "field name contains invalid character"
837 s = local_strdup(field_ref);
839 for(q = s; *q; q++) {
840 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
842 *violation = violations[0];
852 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
854 static const char *garbled_ = "garbled specification";
855 const unsigned n = strlen(in);
857 FLAC__ASSERT(0 != in);
858 FLAC__ASSERT(0 != out);
861 *violation = "specification is empty";
865 if(n > strspn(in, "0123456789.Xsx")) {
866 *violation = "specification contains invalid character";
872 *violation = garbled_;
876 else if(in[n-1] == 's') {
877 if(n-1 > strspn(in, "0123456789.")) {
878 *violation = garbled_;
882 else if(in[n-1] == 'x') {
883 if(n-1 > strspn(in, "0123456789")) {
884 *violation = garbled_;
889 if(n > strspn(in, "0123456789")) {
890 *violation = garbled_;
895 *out = local_strdup(in);
899 FLAC__bool parse_add_padding(const char *in, unsigned *out)
901 FLAC__ASSERT(0 != in);
902 FLAC__ASSERT(0 != out);
903 *out = (unsigned)strtoul(in, 0, 10);
904 return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
907 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
909 char *p, *q, *s, *end;
916 s = local_strdup(in);
918 /* first count the entries */
919 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
923 FLAC__ASSERT(out->num_entries > 0);
924 if(0 == (out->entries = (unsigned*)malloc(sizeof(unsigned) * out->num_entries)))
925 die("out of memory allocating space for option list");
931 FLAC__ASSERT(entry < out->num_entries);
932 if(0 != (p = strchr(q, ',')))
934 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
938 out->entries[entry++] = (unsigned)i;
941 FLAC__ASSERT(entry == out->num_entries);
947 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
955 s = local_strdup(in);
957 /* first count the entries */
958 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
962 FLAC__ASSERT(out->num_entries > 0);
963 if(0 == (out->entries = (Argument_BlockTypeEntry*)malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
964 die("out of memory allocating space for option list");
970 FLAC__ASSERT(entry < out->num_entries);
971 if(0 != (p = strchr(q, ',')))
976 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
980 if(0 == strcmp(q, "STREAMINFO")) {
981 out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
983 else if(0 == strcmp(q, "PADDING")) {
984 out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
986 else if(0 == strcmp(q, "APPLICATION")) {
987 out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
988 out->entries[entry].filter_application_by_id = (0 != r);
991 strcpy(out->entries[entry].application_id, r);
993 else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
994 FLAC__uint32 x = strtoul(r+2, 0, 16);
995 out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
996 out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
997 out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
998 out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
1007 else if(0 == strcmp(q, "SEEKTABLE")) {
1008 out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
1010 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
1011 out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1013 else if(0 == strcmp(q, "CUESHEET")) {
1014 out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
1016 else if(0 == strcmp(q, "PICTURE")) {
1017 out->entries[entry++].type = FLAC__METADATA_TYPE_PICTURE;
1025 FLAC__ASSERT(entry == out->num_entries);
1031 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1033 if(0 == strcmp(in, "binary"))
1034 out->is_binary = true;
1035 else if(0 == strcmp(in, "text"))
1036 out->is_binary = false;
1042 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1044 if(0 == strcmp(in, "hexdump"))
1046 else if(0 == strcmp(in, "text"))
1053 void undocumented_warning(const char *opt)
1055 fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt);