1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004,2005 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.
22 #include "FLAC/assert.h"
29 share__getopt format struct; note we don't use short options so we just
30 set the 'val' field to 0 everywhere to indicate a valid option.
32 struct share__option long_options_[] = {
34 { "preserve-modtime", 0, 0, 0 },
35 { "with-filename", 0, 0, 0 },
36 { "no-filename", 0, 0, 0 },
37 { "no-utf8-convert", 0, 0, 0 },
38 { "dont-use-padding", 0, 0, 0 },
39 { "no-cued-seekpoints", 0, 0, 0 },
40 /* shorthand operations */
41 { "show-md5sum", 0, 0, 0 },
42 { "show-min-blocksize", 0, 0, 0 },
43 { "show-max-blocksize", 0, 0, 0 },
44 { "show-min-framesize", 0, 0, 0 },
45 { "show-max-framesize", 0, 0, 0 },
46 { "show-sample-rate", 0, 0, 0 },
47 { "show-channels", 0, 0, 0 },
48 { "show-bps", 0, 0, 0 },
49 { "show-total-samples", 0, 0, 0 },
50 { "set-md5sum", 1, 0, 0 }, /* undocumented */
51 { "set-min-blocksize", 1, 0, 0 }, /* undocumented */
52 { "set-max-blocksize", 1, 0, 0 }, /* undocumented */
53 { "set-min-framesize", 1, 0, 0 }, /* undocumented */
54 { "set-max-framesize", 1, 0, 0 }, /* undocumented */
55 { "set-sample-rate", 1, 0, 0 }, /* undocumented */
56 { "set-channels", 1, 0, 0 }, /* undocumented */
57 { "set-bps", 1, 0, 0 }, /* undocumented */
58 { "set-total-samples", 1, 0, 0 }, /* undocumented */ /* WATCHOUT: used by test/test_flac.sh on windows */
59 { "show-vendor-tag", 0, 0, 0 },
60 { "show-tag", 1, 0, 0 },
61 { "remove-all-tags", 0, 0, 0 },
62 { "remove-tag", 1, 0, 0 },
63 { "remove-first-tag", 1, 0, 0 },
64 { "set-tag", 1, 0, 0 },
65 { "import-tags-from", 1, 0, 0 },
66 { "export-tags-to", 1, 0, 0 },
67 { "show-vc-vendor", 0, 0, 0 }, /* deprecated */
68 { "show-vc-field", 1, 0, 0 }, /* deprecated */
69 { "remove-vc-all", 0, 0, 0 }, /* deprecated */
70 { "remove-vc-field", 1, 0, 0 }, /* deprecated */
71 { "remove-vc-firstfield", 1, 0, 0 }, /* deprecated */
72 { "set-vc-field", 1, 0, 0 }, /* deprecated */
73 { "import-vc-from", 1, 0, 0 }, /* deprecated */
74 { "export-vc-to", 1, 0, 0 }, /* deprecated */
75 { "import-cuesheet-from", 1, 0, 0 },
76 { "export-cuesheet-to", 1, 0, 0 },
77 { "add-seekpoint", 1, 0, 0 },
78 { "add-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_filename(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__ADD_SEEKPOINT:
271 if(0 != op->argument.add_seekpoint.specification)
272 free(op->argument.add_seekpoint.specification);
279 for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
281 case ARG__BLOCK_NUMBER:
282 if(0 != arg->value.block_number.entries)
283 free(arg->value.block_number.entries);
285 case ARG__BLOCK_TYPE:
286 case ARG__EXCEPT_BLOCK_TYPE:
287 if(0 != arg->value.block_type.entries)
288 free(arg->value.block_type.entries);
291 if(0 != arg->value.from_file.file_name)
292 free(arg->value.from_file.file_name);
299 if(0 != options->ops.operations)
300 free(options->ops.operations);
302 if(0 != options->args.arguments)
303 free(options->args.arguments);
305 if(0 != options->filenames) {
306 for(i = 0; i < options->num_files; i++) {
307 if(0 != options->filenames[i])
308 free(options->filenames[i]);
310 free(options->filenames);
318 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
320 const char *opt = long_options_[option_index].name;
323 FLAC__bool ok = true;
325 if(0 == strcmp(opt, "preserve-modtime")) {
326 options->preserve_modtime = true;
328 else if(0 == strcmp(opt, "with-filename")) {
329 options->prefix_with_filename = true;
331 else if(0 == strcmp(opt, "no-filename")) {
332 options->prefix_with_filename = false;
334 else if(0 == strcmp(opt, "no-utf8-convert")) {
335 options->utf8_convert = false;
337 else if(0 == strcmp(opt, "dont-use-padding")) {
338 options->use_padding = false;
340 else if(0 == strcmp(opt, "no-cued-seekpoints")) {
341 options->cued_seekpoints = false;
343 else if(0 == strcmp(opt, "show-md5sum")) {
344 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
346 else if(0 == strcmp(opt, "show-min-blocksize")) {
347 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
349 else if(0 == strcmp(opt, "show-max-blocksize")) {
350 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
352 else if(0 == strcmp(opt, "show-min-framesize")) {
353 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
355 else if(0 == strcmp(opt, "show-max-framesize")) {
356 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
358 else if(0 == strcmp(opt, "show-sample-rate")) {
359 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
361 else if(0 == strcmp(opt, "show-channels")) {
362 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
364 else if(0 == strcmp(opt, "show-bps")) {
365 (void) append_shorthand_operation(options, OP__SHOW_BPS);
367 else if(0 == strcmp(opt, "show-total-samples")) {
368 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
370 else if(0 == strcmp(opt, "set-md5sum")) {
371 op = append_shorthand_operation(options, OP__SET_MD5SUM);
372 FLAC__ASSERT(0 != option_argument);
373 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
374 fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
378 undocumented_warning(opt);
380 else if(0 == strcmp(opt, "set-min-blocksize")) {
381 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
382 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) {
383 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
387 undocumented_warning(opt);
389 else if(0 == strcmp(opt, "set-max-blocksize")) {
390 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
391 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) {
392 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
396 undocumented_warning(opt);
398 else if(0 == strcmp(opt, "set-min-framesize")) {
399 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
400 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
401 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
405 undocumented_warning(opt);
407 else if(0 == strcmp(opt, "set-max-framesize")) {
408 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
409 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
410 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
414 undocumented_warning(opt);
416 else if(0 == strcmp(opt, "set-sample-rate")) {
417 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
418 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
419 fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
423 undocumented_warning(opt);
425 else if(0 == strcmp(opt, "set-channels")) {
426 op = append_shorthand_operation(options, OP__SET_CHANNELS);
427 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
428 fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
432 undocumented_warning(opt);
434 else if(0 == strcmp(opt, "set-bps")) {
435 op = append_shorthand_operation(options, OP__SET_BPS);
436 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) {
437 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
441 undocumented_warning(opt);
443 else if(0 == strcmp(opt, "set-total-samples")) {
444 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
445 if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
446 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
450 undocumented_warning(opt);
452 else if(0 == strcmp(opt, "show-vendor-tag") || 0 == strcmp(opt, "show-vc-vendor")) {
453 if(0 == strcmp(opt, "show-vc-vendor"))
454 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --show-vendor-tag\n", opt);
455 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
457 else if(0 == strcmp(opt, "show-tag") || 0 == strcmp(opt, "show-vc-field")) {
458 const char *violation;
459 if(0 == strcmp(opt, "show-vc-field"))
460 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --show-tag\n", opt);
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") || 0 == strcmp(opt, "remove-vc-all")) {
470 if(0 == strcmp(opt, "remove-vc-all"))
471 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-all-tags\n", opt);
472 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
474 else if(0 == strcmp(opt, "remove-tag") || 0 == strcmp(opt, "remove-vc-field")) {
475 const char *violation;
476 if(0 == strcmp(opt, "remove-vc-field"))
477 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-tag\n", opt);
478 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
479 FLAC__ASSERT(0 != option_argument);
480 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
481 FLAC__ASSERT(0 != violation);
482 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
486 else if(0 == strcmp(opt, "remove-first-tag") || 0 == strcmp(opt, "remove-vc-firstfield")) {
487 const char *violation;
488 if(0 == strcmp(opt, "remove-vc-firstfield"))
489 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-first-tag\n", opt);
490 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
491 FLAC__ASSERT(0 != option_argument);
492 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
493 FLAC__ASSERT(0 != violation);
494 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
498 else if(0 == strcmp(opt, "set-tag") || 0 == strcmp(opt, "set-vc-field")) {
499 const char *violation;
500 if(0 == strcmp(opt, "set-vc-field"))
501 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --set-tag\n", opt);
502 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
503 FLAC__ASSERT(0 != option_argument);
504 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)) {
505 FLAC__ASSERT(0 != violation);
506 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
510 else if(0 == strcmp(opt, "import-tags-from") || 0 == strcmp(opt, "import-vc-from")) {
511 if(0 == strcmp(opt, "import-vc-from"))
512 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --import-tags-from\n", opt);
513 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
514 FLAC__ASSERT(0 != option_argument);
515 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
516 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
520 else if(0 == strcmp(opt, "export-tags-to") || 0 == strcmp(opt, "export-vc-to")) {
521 if(0 == strcmp(opt, "export-vc-to"))
522 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --export-tags-to\n", opt);
523 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
524 FLAC__ASSERT(0 != option_argument);
525 if(!parse_filename(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_filename(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_filename(option_argument, &(op->argument.filename.value))) {
546 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
550 else if(0 == strcmp(opt, "add-seekpoint")) {
551 const char *violation;
553 FLAC__ASSERT(0 != option_argument);
554 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
555 FLAC__ASSERT(0 != violation);
556 fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation);
559 op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
561 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
562 local_strcat(&(op->argument.add_seekpoint.specification), spec);
563 local_strcat(&(op->argument.add_seekpoint.specification), ";");
566 else if(0 == strcmp(opt, "add-replay-gain")) {
567 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
569 else if(0 == strcmp(opt, "add-padding")) {
570 op = append_shorthand_operation(options, OP__ADD_PADDING);
571 FLAC__ASSERT(0 != option_argument);
572 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
573 fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
577 else if(0 == strcmp(opt, "help")) {
578 options->show_long_help = true;
580 else if(0 == strcmp(opt, "version")) {
581 options->show_version = true;
583 else if(0 == strcmp(opt, "list")) {
584 (void) append_major_operation(options, OP__LIST);
586 else if(0 == strcmp(opt, "append")) {
587 (void) append_major_operation(options, OP__APPEND);
589 else if(0 == strcmp(opt, "remove")) {
590 (void) append_major_operation(options, OP__REMOVE);
592 else if(0 == strcmp(opt, "remove-all")) {
593 (void) append_major_operation(options, OP__REMOVE_ALL);
595 else if(0 == strcmp(opt, "merge-padding")) {
596 (void) append_major_operation(options, OP__MERGE_PADDING);
598 else if(0 == strcmp(opt, "sort-padding")) {
599 (void) append_major_operation(options, OP__SORT_PADDING);
601 else if(0 == strcmp(opt, "block-number")) {
602 arg = append_argument(options, ARG__BLOCK_NUMBER);
603 FLAC__ASSERT(0 != option_argument);
604 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
605 fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
609 else if(0 == strcmp(opt, "block-type")) {
610 arg = append_argument(options, ARG__BLOCK_TYPE);
611 FLAC__ASSERT(0 != option_argument);
612 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
613 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
616 options->args.checks.has_block_type = true;
618 else if(0 == strcmp(opt, "except-block-type")) {
619 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
620 FLAC__ASSERT(0 != option_argument);
621 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
622 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
625 options->args.checks.has_except_block_type = true;
627 else if(0 == strcmp(opt, "data-format")) {
628 arg = append_argument(options, ARG__DATA_FORMAT);
629 FLAC__ASSERT(0 != option_argument);
630 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
631 fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
635 else if(0 == strcmp(opt, "application-data-format")) {
636 FLAC__ASSERT(0 != option_argument);
637 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
638 fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
642 else if(0 == strcmp(opt, "from-file")) {
643 arg = append_argument(options, ARG__FROM_FILE);
644 FLAC__ASSERT(0 != option_argument);
645 arg->value.from_file.file_name = local_strdup(option_argument);
654 void append_new_operation(CommandLineOptions *options, Operation operation)
656 if(options->ops.capacity == 0) {
657 options->ops.capacity = 50;
658 if(0 == (options->ops.operations = (Operation*)malloc(sizeof(Operation) * options->ops.capacity)))
659 die("out of memory allocating space for option list");
660 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
662 if(options->ops.capacity <= options->ops.num_operations) {
663 unsigned original_capacity = options->ops.capacity;
664 options->ops.capacity *= 4;
665 if(0 == (options->ops.operations = (Operation*)realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
666 die("out of memory allocating space for option list");
667 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
670 options->ops.operations[options->ops.num_operations++] = operation;
673 void append_new_argument(CommandLineOptions *options, Argument argument)
675 if(options->args.capacity == 0) {
676 options->args.capacity = 50;
677 if(0 == (options->args.arguments = (Argument*)malloc(sizeof(Argument) * options->args.capacity)))
678 die("out of memory allocating space for option list");
679 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
681 if(options->args.capacity <= options->args.num_arguments) {
682 unsigned original_capacity = options->args.capacity;
683 options->args.capacity *= 4;
684 if(0 == (options->args.arguments = (Argument*)realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
685 die("out of memory allocating space for option list");
686 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
689 options->args.arguments[options->args.num_arguments++] = argument;
692 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
695 memset(&op, 0, sizeof(op));
697 append_new_operation(options, op);
698 options->args.checks.num_major_ops++;
699 return options->ops.operations + (options->ops.num_operations - 1);
702 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
705 memset(&op, 0, sizeof(op));
707 append_new_operation(options, op);
708 options->args.checks.num_shorthand_ops++;
709 return options->ops.operations + (options->ops.num_operations - 1);
712 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
715 for(i = 0; i < options->ops.num_operations; i++)
716 if(options->ops.operations[i].type == type)
717 return &options->ops.operations[i];
721 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
724 memset(&arg, 0, sizeof(arg));
726 append_new_argument(options, arg);
727 return options->args.arguments + (options->args.num_arguments - 1);
730 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
734 FLAC__ASSERT(0 != src);
735 if(strlen(src) != 32)
737 /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
738 for(i = 0; i < 16; i++) {
741 d = (unsigned)(c - '0');
742 else if(c >= 'a' && c <= 'f')
743 d = (unsigned)(c - 'a') + 10u;
744 else if(c >= 'A' && c <= 'F')
745 d = (unsigned)(c - 'A') + 10u;
751 d |= (unsigned)(c - '0');
752 else if(c >= 'a' && c <= 'f')
753 d |= (unsigned)(c - 'a') + 10u;
754 else if(c >= 'A' && c <= 'F')
755 d |= (unsigned)(c - 'A') + 10u;
758 dest[i] = (FLAC__byte)d;
763 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
765 FLAC__ASSERT(0 != src);
766 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
768 *dest = strtoul(src, 0, 10);
772 /* There's no stroull() in MSVC6 so we just write a specialized one */
773 static FLAC__uint64 local__strtoull(const char *src)
775 FLAC__uint64 ret = 0;
777 FLAC__ASSERT(0 != src);
778 while(0 != (c = *src++)) {
781 ret = (ret * 10) + c;
788 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
790 FLAC__ASSERT(0 != src);
791 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
793 *dest = local__strtoull(src);
797 FLAC__bool parse_filename(const char *src, char **dest)
799 if(0 == src || strlen(src) == 0)
805 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
807 static const char * const violations[] = {
808 "field name contains invalid character"
813 s = local_strdup(field_ref);
815 for(q = s; *q; q++) {
816 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
818 *violation = violations[0];
828 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
830 static const char *garbled_ = "garbled specification";
831 const unsigned n = strlen(in);
833 FLAC__ASSERT(0 != in);
834 FLAC__ASSERT(0 != out);
837 *violation = "specification is empty";
841 if(n > strspn(in, "0123456789.Xsx")) {
842 *violation = "specification contains invalid character";
848 *violation = garbled_;
852 else if(in[n-1] == 's') {
853 if(n-1 > strspn(in, "0123456789.")) {
854 *violation = garbled_;
858 else if(in[n-1] == 'x') {
859 if(n-1 > strspn(in, "0123456789")) {
860 *violation = garbled_;
865 if(n > strspn(in, "0123456789")) {
866 *violation = garbled_;
871 *out = local_strdup(in);
875 FLAC__bool parse_add_padding(const char *in, unsigned *out)
877 FLAC__ASSERT(0 != in);
878 FLAC__ASSERT(0 != out);
879 *out = (unsigned)strtoul(in, 0, 10);
880 return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
883 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
885 char *p, *q, *s, *end;
892 s = local_strdup(in);
894 /* first count the entries */
895 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
899 FLAC__ASSERT(out->num_entries > 0);
900 if(0 == (out->entries = (unsigned*)malloc(sizeof(unsigned) * out->num_entries)))
901 die("out of memory allocating space for option list");
907 FLAC__ASSERT(entry < out->num_entries);
908 if(0 != (p = strchr(q, ',')))
910 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
914 out->entries[entry++] = (unsigned)i;
917 FLAC__ASSERT(entry == out->num_entries);
923 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
931 s = local_strdup(in);
933 /* first count the entries */
934 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
938 FLAC__ASSERT(out->num_entries > 0);
939 if(0 == (out->entries = (Argument_BlockTypeEntry*)malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
940 die("out of memory allocating space for option list");
946 FLAC__ASSERT(entry < out->num_entries);
947 if(0 != (p = strchr(q, ',')))
952 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
956 if(0 == strcmp(q, "STREAMINFO")) {
957 out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
959 else if(0 == strcmp(q, "PADDING")) {
960 out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
962 else if(0 == strcmp(q, "APPLICATION")) {
963 out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
964 out->entries[entry].filter_application_by_id = (0 != r);
967 strcpy(out->entries[entry].application_id, r);
969 else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
970 FLAC__uint32 x = strtoul(r+2, 0, 16);
971 out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
972 out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
973 out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
974 out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
983 else if(0 == strcmp(q, "SEEKTABLE")) {
984 out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
986 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
987 out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
989 else if(0 == strcmp(q, "CUESHEET")) {
990 out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
998 FLAC__ASSERT(entry == out->num_entries);
1004 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1006 if(0 == strcmp(in, "binary"))
1007 out->is_binary = true;
1008 else if(0 == strcmp(in, "text"))
1009 out->is_binary = false;
1015 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1017 if(0 == strcmp(in, "hexdump"))
1019 else if(0 == strcmp(in, "text"))
1026 void undocumented_warning(const char *opt)
1028 fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt);