1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "FLAC/assert.h"
28 #include "share/alloc.h"
29 #include "share/compat.h"
30 #include "share/grabbag/replaygain.h"
37 share__getopt format struct; note we don't use short options so we just
38 set the 'val' field to 0 everywhere to indicate a valid option.
40 struct share__option long_options_[] = {
42 { "preserve-modtime", 0, 0, 0 },
43 { "with-filename", 0, 0, 0 },
44 { "no-filename", 0, 0, 0 },
45 { "no-utf8-convert", 0, 0, 0 },
46 { "dont-use-padding", 0, 0, 0 },
47 { "no-cued-seekpoints", 0, 0, 0 },
48 /* shorthand operations */
49 { "show-md5sum", 0, 0, 0 },
50 { "show-min-blocksize", 0, 0, 0 },
51 { "show-max-blocksize", 0, 0, 0 },
52 { "show-min-framesize", 0, 0, 0 },
53 { "show-max-framesize", 0, 0, 0 },
54 { "show-sample-rate", 0, 0, 0 },
55 { "show-channels", 0, 0, 0 },
56 { "show-bps", 0, 0, 0 },
57 { "show-total-samples", 0, 0, 0 },
58 { "set-md5sum", 1, 0, 0 }, /* undocumented */
59 { "set-min-blocksize", 1, 0, 0 }, /* undocumented */
60 { "set-max-blocksize", 1, 0, 0 }, /* undocumented */
61 { "set-min-framesize", 1, 0, 0 }, /* undocumented */
62 { "set-max-framesize", 1, 0, 0 }, /* undocumented */
63 { "set-sample-rate", 1, 0, 0 }, /* undocumented */
64 { "set-channels", 1, 0, 0 }, /* undocumented */
65 { "set-bps", 1, 0, 0 }, /* undocumented */
66 { "set-total-samples", 1, 0, 0 }, /* undocumented */ /* WATCHOUT: used by test/test_flac.sh on windows */
67 { "show-vendor-tag", 0, 0, 0 },
68 { "show-tag", 1, 0, 0 },
69 { "remove-all-tags", 0, 0, 0 },
70 { "remove-tag", 1, 0, 0 },
71 { "remove-first-tag", 1, 0, 0 },
72 { "set-tag", 1, 0, 0 },
73 { "set-tag-from-file", 1, 0, 0 },
74 { "import-tags-from", 1, 0, 0 },
75 { "export-tags-to", 1, 0, 0 },
76 { "import-cuesheet-from", 1, 0, 0 },
77 { "export-cuesheet-to", 1, 0, 0 },
78 { "import-picture-from", 1, 0, 0 },
79 { "export-picture-to", 1, 0, 0 },
80 { "add-seekpoint", 1, 0, 0 },
81 { "add-replay-gain", 0, 0, 0 },
82 { "remove-replay-gain", 0, 0, 0 },
83 { "add-padding", 1, 0, 0 },
84 /* major operations */
86 { "version", 0, 0, 0 },
88 { "append", 0, 0, 0 },
89 { "remove", 0, 0, 0 },
90 { "remove-all", 0, 0, 0 },
91 { "merge-padding", 0, 0, 0 },
92 { "sort-padding", 0, 0, 0 },
93 /* major operation arguments */
94 { "block-number", 1, 0, 0 },
95 { "block-type", 1, 0, 0 },
96 { "except-block-type", 1, 0, 0 },
97 { "data-format", 1, 0, 0 },
98 { "application-data-format", 1, 0, 0 },
99 { "from-file", 1, 0, 0 },
103 static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options);
104 static void append_new_operation(CommandLineOptions *options, Operation operation);
105 static void append_new_argument(CommandLineOptions *options, Argument argument);
106 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
107 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
108 static Argument *find_argument(CommandLineOptions *options, ArgumentType type);
109 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
110 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
111 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
112 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
113 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
114 static FLAC__bool parse_string(const char *src, char **dest);
115 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
116 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
117 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
118 static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out);
119 static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out);
120 static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out);
121 static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out);
122 static void undocumented_warning(const char *opt);
125 void init_options(CommandLineOptions *options)
127 options->preserve_modtime = false;
129 /* '2' is a hack to mean "use default if not forced on command line" */
130 FLAC__ASSERT(true != 2);
131 options->prefix_with_filename = 2;
133 options->utf8_convert = true;
134 options->use_padding = true;
135 options->cued_seekpoints = true;
136 options->show_long_help = false;
137 options->show_version = false;
138 options->application_data_format_is_hexdump = false;
140 options->ops.operations = 0;
141 options->ops.num_operations = 0;
142 options->ops.capacity = 0;
144 options->args.arguments = 0;
145 options->args.num_arguments = 0;
146 options->args.capacity = 0;
148 options->args.checks.num_shorthand_ops = 0;
149 options->args.checks.num_major_ops = 0;
150 options->args.checks.has_block_type = false;
151 options->args.checks.has_except_block_type = false;
153 options->num_files = 0;
154 options->filenames = 0;
157 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
160 int option_index = 1;
161 FLAC__bool had_error = false;
163 while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
166 had_error |= !parse_option(option_index, share__optarg, options);
178 if(options->prefix_with_filename == 2)
179 options->prefix_with_filename = (argc - share__optind > 1);
181 if(share__optind >= argc && !options->show_long_help && !options->show_version) {
182 flac_fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
183 flac_fprintf(stderr," metaflac cannot be used as a pipe\n");
187 options->num_files = argc - share__optind;
189 if(options->num_files > 0) {
191 if(0 == (options->filenames = safe_malloc_mul_2op_(sizeof(char*), /*times*/options->num_files)))
192 die("out of memory allocating space for file names list");
193 while(share__optind < argc)
194 options->filenames[i++] = local_strdup(argv[share__optind++]);
197 if(options->args.checks.num_major_ops > 0) {
198 if(options->args.checks.num_major_ops > 1) {
199 flac_fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
202 else if(options->args.checks.num_shorthand_ops > 0) {
203 flac_fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
208 /* check for only one FLAC file used with certain options */
209 if(options->num_files > 1) {
210 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
211 flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from'\n");
214 if(0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) {
215 flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-cuesheet-to'\n");
218 if(0 != find_shorthand_operation(options, OP__EXPORT_PICTURE_TO)) {
219 flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-picture-to'\n");
223 0 != find_shorthand_operation(options, OP__IMPORT_VC_FROM) &&
224 0 == strcmp(find_shorthand_operation(options, OP__IMPORT_VC_FROM)->argument.filename.value, "-")
226 flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-tags-from=-'\n");
231 if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
232 flac_fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
240 * We need to create an OP__ADD_SEEKPOINT operation if there is
241 * not one already, and --import-cuesheet-from was specified but
242 * --no-cued-seekpoints was not:
244 if(options->cued_seekpoints) {
245 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
247 Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
249 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
250 op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
257 void free_options(CommandLineOptions *options)
263 FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
264 FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
266 for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
268 case OP__SHOW_VC_FIELD:
269 case OP__REMOVE_VC_FIELD:
270 case OP__REMOVE_VC_FIRSTFIELD:
271 if(0 != op->argument.vc_field_name.value)
272 free(op->argument.vc_field_name.value);
274 case OP__SET_VC_FIELD:
275 if(0 != op->argument.vc_field.field)
276 free(op->argument.vc_field.field);
277 if(0 != op->argument.vc_field.field_name)
278 free(op->argument.vc_field.field_name);
279 if(0 != op->argument.vc_field.field_value)
280 free(op->argument.vc_field.field_value);
282 case OP__IMPORT_VC_FROM:
283 case OP__EXPORT_VC_TO:
284 case OP__EXPORT_CUESHEET_TO:
285 if(0 != op->argument.filename.value)
286 free(op->argument.filename.value);
288 case OP__IMPORT_CUESHEET_FROM:
289 if(0 != op->argument.import_cuesheet_from.filename)
290 free(op->argument.import_cuesheet_from.filename);
292 case OP__IMPORT_PICTURE_FROM:
293 if(0 != op->argument.specification.value)
294 free(op->argument.specification.value);
296 case OP__EXPORT_PICTURE_TO:
297 if(0 != op->argument.export_picture_to.filename)
298 free(op->argument.export_picture_to.filename);
300 case OP__ADD_SEEKPOINT:
301 if(0 != op->argument.add_seekpoint.specification)
302 free(op->argument.add_seekpoint.specification);
309 for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
311 case ARG__BLOCK_NUMBER:
312 if(0 != arg->value.block_number.entries)
313 free(arg->value.block_number.entries);
315 case ARG__BLOCK_TYPE:
316 case ARG__EXCEPT_BLOCK_TYPE:
317 if(0 != arg->value.block_type.entries)
318 free(arg->value.block_type.entries);
321 if(0 != arg->value.from_file.file_name)
322 free(arg->value.from_file.file_name);
329 if(0 != options->ops.operations)
330 free(options->ops.operations);
332 if(0 != options->args.arguments)
333 free(options->args.arguments);
335 if(0 != options->filenames) {
336 for(i = 0; i < options->num_files; i++) {
337 if(0 != options->filenames[i])
338 free(options->filenames[i]);
340 free(options->filenames);
348 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
350 const char *opt = long_options_[option_index].name;
353 FLAC__bool ok = true;
355 if(0 == strcmp(opt, "preserve-modtime")) {
356 options->preserve_modtime = true;
358 else if(0 == strcmp(opt, "with-filename")) {
359 options->prefix_with_filename = true;
361 else if(0 == strcmp(opt, "no-filename")) {
362 options->prefix_with_filename = false;
364 else if(0 == strcmp(opt, "no-utf8-convert")) {
365 options->utf8_convert = false;
367 else if(0 == strcmp(opt, "dont-use-padding")) {
368 options->use_padding = false;
370 else if(0 == strcmp(opt, "no-cued-seekpoints")) {
371 options->cued_seekpoints = false;
373 else if(0 == strcmp(opt, "show-md5sum")) {
374 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
376 else if(0 == strcmp(opt, "show-min-blocksize")) {
377 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
379 else if(0 == strcmp(opt, "show-max-blocksize")) {
380 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
382 else if(0 == strcmp(opt, "show-min-framesize")) {
383 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
385 else if(0 == strcmp(opt, "show-max-framesize")) {
386 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
388 else if(0 == strcmp(opt, "show-sample-rate")) {
389 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
391 else if(0 == strcmp(opt, "show-channels")) {
392 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
394 else if(0 == strcmp(opt, "show-bps")) {
395 (void) append_shorthand_operation(options, OP__SHOW_BPS);
397 else if(0 == strcmp(opt, "show-total-samples")) {
398 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
400 else if(0 == strcmp(opt, "set-md5sum")) {
401 op = append_shorthand_operation(options, OP__SET_MD5SUM);
402 FLAC__ASSERT(0 != option_argument);
403 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
404 flac_fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
408 undocumented_warning(opt);
410 else if(0 == strcmp(opt, "set-min-blocksize")) {
411 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
412 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) {
413 flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
417 undocumented_warning(opt);
419 else if(0 == strcmp(opt, "set-max-blocksize")) {
420 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
421 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) {
422 flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
426 undocumented_warning(opt);
428 else if(0 == strcmp(opt, "set-min-framesize")) {
429 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
430 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
431 flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
435 undocumented_warning(opt);
437 else if(0 == strcmp(opt, "set-max-framesize")) {
438 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
439 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
440 flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
444 undocumented_warning(opt);
446 else if(0 == strcmp(opt, "set-sample-rate")) {
447 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
448 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
449 flac_fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
453 undocumented_warning(opt);
455 else if(0 == strcmp(opt, "set-channels")) {
456 op = append_shorthand_operation(options, OP__SET_CHANNELS);
457 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
458 flac_fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
462 undocumented_warning(opt);
464 else if(0 == strcmp(opt, "set-bps")) {
465 op = append_shorthand_operation(options, OP__SET_BPS);
466 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) {
467 flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
471 undocumented_warning(opt);
473 else if(0 == strcmp(opt, "set-total-samples")) {
474 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
475 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)) {
476 flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
480 undocumented_warning(opt);
482 else if(0 == strcmp(opt, "show-vendor-tag")) {
483 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
485 else if(0 == strcmp(opt, "show-tag")) {
486 const char *violation;
487 op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
488 FLAC__ASSERT(0 != option_argument);
489 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
490 FLAC__ASSERT(0 != violation);
491 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
495 else if(0 == strcmp(opt, "remove-all-tags")) {
496 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
498 else if(0 == strcmp(opt, "remove-tag")) {
499 const char *violation;
500 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
501 FLAC__ASSERT(0 != option_argument);
502 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
503 FLAC__ASSERT(0 != violation);
504 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
508 else if(0 == strcmp(opt, "remove-first-tag")) {
509 const char *violation;
510 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
511 FLAC__ASSERT(0 != option_argument);
512 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
513 FLAC__ASSERT(0 != violation);
514 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
518 else if(0 == strcmp(opt, "set-tag")) {
519 const char *violation;
520 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
521 FLAC__ASSERT(0 != option_argument);
522 op->argument.vc_field.field_value_from_file = false;
523 if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
524 FLAC__ASSERT(0 != violation);
525 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
529 else if(0 == strcmp(opt, "set-tag-from-file")) {
530 const char *violation;
531 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
532 FLAC__ASSERT(0 != option_argument);
533 op->argument.vc_field.field_value_from_file = true;
534 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)) {
535 FLAC__ASSERT(0 != violation);
536 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
540 else if(0 == strcmp(opt, "import-tags-from")) {
541 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
542 FLAC__ASSERT(0 != option_argument);
543 if(!parse_string(option_argument, &(op->argument.filename.value))) {
544 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
548 else if(0 == strcmp(opt, "export-tags-to")) {
549 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
550 FLAC__ASSERT(0 != option_argument);
551 if(!parse_string(option_argument, &(op->argument.filename.value))) {
552 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
556 else if(0 == strcmp(opt, "import-cuesheet-from")) {
557 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
558 flac_fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
561 op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
562 FLAC__ASSERT(0 != option_argument);
563 if(!parse_string(option_argument, &(op->argument.import_cuesheet_from.filename))) {
564 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
568 else if(0 == strcmp(opt, "export-cuesheet-to")) {
569 op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
570 FLAC__ASSERT(0 != option_argument);
571 if(!parse_string(option_argument, &(op->argument.filename.value))) {
572 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
576 else if(0 == strcmp(opt, "import-picture-from")) {
577 op = append_shorthand_operation(options, OP__IMPORT_PICTURE_FROM);
578 FLAC__ASSERT(0 != option_argument);
579 if(!parse_string(option_argument, &(op->argument.specification.value))) {
580 flac_fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
584 else if(0 == strcmp(opt, "export-picture-to")) {
585 arg = find_argument(options, ARG__BLOCK_NUMBER);
586 op = append_shorthand_operation(options, OP__EXPORT_PICTURE_TO);
587 FLAC__ASSERT(0 != option_argument);
588 if(!parse_string(option_argument, &(op->argument.export_picture_to.filename))) {
589 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
592 op->argument.export_picture_to.block_number_link = arg? &(arg->value.block_number) : 0;
594 else if(0 == strcmp(opt, "add-seekpoint")) {
595 const char *violation;
597 FLAC__ASSERT(0 != option_argument);
598 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
599 FLAC__ASSERT(0 != violation);
600 flac_fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation);
604 op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
606 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
607 local_strcat(&(op->argument.add_seekpoint.specification), spec);
608 local_strcat(&(op->argument.add_seekpoint.specification), ";");
612 else if(0 == strcmp(opt, "add-replay-gain")) {
613 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
615 else if(0 == strcmp(opt, "remove-replay-gain")) {
616 const FLAC__byte * const tags[5] = {
617 GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS,
618 GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN,
619 GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK,
620 GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN,
621 GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK
624 for(i = 0; i < sizeof(tags)/sizeof(tags[0]); i++) {
625 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
626 op->argument.vc_field_name.value = local_strdup((const char *)tags[i]);
629 else if(0 == strcmp(opt, "add-padding")) {
630 op = append_shorthand_operation(options, OP__ADD_PADDING);
631 FLAC__ASSERT(0 != option_argument);
632 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
633 flac_fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
637 else if(0 == strcmp(opt, "help")) {
638 options->show_long_help = true;
640 else if(0 == strcmp(opt, "version")) {
641 options->show_version = true;
643 else if(0 == strcmp(opt, "list")) {
644 (void) append_major_operation(options, OP__LIST);
646 else if(0 == strcmp(opt, "append")) {
647 (void) append_major_operation(options, OP__APPEND);
649 else if(0 == strcmp(opt, "remove")) {
650 (void) append_major_operation(options, OP__REMOVE);
652 else if(0 == strcmp(opt, "remove-all")) {
653 (void) append_major_operation(options, OP__REMOVE_ALL);
655 else if(0 == strcmp(opt, "merge-padding")) {
656 (void) append_major_operation(options, OP__MERGE_PADDING);
658 else if(0 == strcmp(opt, "sort-padding")) {
659 (void) append_major_operation(options, OP__SORT_PADDING);
661 else if(0 == strcmp(opt, "block-number")) {
662 arg = append_argument(options, ARG__BLOCK_NUMBER);
663 FLAC__ASSERT(0 != option_argument);
664 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
665 flac_fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
669 else if(0 == strcmp(opt, "block-type")) {
670 arg = append_argument(options, ARG__BLOCK_TYPE);
671 FLAC__ASSERT(0 != option_argument);
672 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
673 flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
676 options->args.checks.has_block_type = true;
678 else if(0 == strcmp(opt, "except-block-type")) {
679 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
680 FLAC__ASSERT(0 != option_argument);
681 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
682 flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
685 options->args.checks.has_except_block_type = true;
687 else if(0 == strcmp(opt, "data-format")) {
688 arg = append_argument(options, ARG__DATA_FORMAT);
689 FLAC__ASSERT(0 != option_argument);
690 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
691 flac_fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
695 else if(0 == strcmp(opt, "application-data-format")) {
696 FLAC__ASSERT(0 != option_argument);
697 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
698 flac_fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
702 else if(0 == strcmp(opt, "from-file")) {
703 arg = append_argument(options, ARG__FROM_FILE);
704 FLAC__ASSERT(0 != option_argument);
705 arg->value.from_file.file_name = local_strdup(option_argument);
714 void append_new_operation(CommandLineOptions *options, Operation operation)
716 if(options->ops.capacity == 0) {
717 options->ops.capacity = 50;
718 if(0 == (options->ops.operations = malloc(sizeof(Operation) * options->ops.capacity)))
719 die("out of memory allocating space for option list");
720 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
722 if(options->ops.capacity <= options->ops.num_operations) {
723 unsigned original_capacity = options->ops.capacity;
724 if(options->ops.capacity > UINT32_MAX / 2) /* overflow check */
725 die("out of memory allocating space for option list");
726 options->ops.capacity *= 2;
727 if(0 == (options->ops.operations = safe_realloc_mul_2op_(options->ops.operations, sizeof(Operation), /*times*/options->ops.capacity)))
728 die("out of memory allocating space for option list");
729 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
732 options->ops.operations[options->ops.num_operations++] = operation;
735 void append_new_argument(CommandLineOptions *options, Argument argument)
737 if(options->args.capacity == 0) {
738 options->args.capacity = 50;
739 if(0 == (options->args.arguments = malloc(sizeof(Argument) * options->args.capacity)))
740 die("out of memory allocating space for option list");
741 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
743 if(options->args.capacity <= options->args.num_arguments) {
744 unsigned original_capacity = options->args.capacity;
745 if(options->args.capacity > UINT32_MAX / 2) /* overflow check */
746 die("out of memory allocating space for option list");
747 options->args.capacity *= 2;
748 if(0 == (options->args.arguments = safe_realloc_mul_2op_(options->args.arguments, sizeof(Argument), /*times*/options->args.capacity)))
749 die("out of memory allocating space for option list");
750 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
753 options->args.arguments[options->args.num_arguments++] = argument;
756 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
759 memset(&op, 0, sizeof(op));
761 append_new_operation(options, op);
762 options->args.checks.num_major_ops++;
763 return options->ops.operations + (options->ops.num_operations - 1);
766 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
769 memset(&op, 0, sizeof(op));
771 append_new_operation(options, op);
772 options->args.checks.num_shorthand_ops++;
773 return options->ops.operations + (options->ops.num_operations - 1);
776 Argument *find_argument(CommandLineOptions *options, ArgumentType type)
779 for(i = 0; i < options->args.num_arguments; i++)
780 if(options->args.arguments[i].type == type)
781 return &options->args.arguments[i];
785 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
788 for(i = 0; i < options->ops.num_operations; i++)
789 if(options->ops.operations[i].type == type)
790 return &options->ops.operations[i];
794 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
797 memset(&arg, 0, sizeof(arg));
799 append_new_argument(options, arg);
800 return options->args.arguments + (options->args.num_arguments - 1);
803 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
807 FLAC__ASSERT(0 != src);
808 if(strlen(src) != 32)
810 /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
811 for(i = 0; i < 16; i++) {
814 d = (unsigned)(c - '0');
815 else if(c >= 'a' && c <= 'f')
816 d = (unsigned)(c - 'a') + 10u;
817 else if(c >= 'A' && c <= 'F')
818 d = (unsigned)(c - 'A') + 10u;
824 d |= (unsigned)(c - '0');
825 else if(c >= 'a' && c <= 'f')
826 d |= (unsigned)(c - 'a') + 10u;
827 else if(c >= 'A' && c <= 'F')
828 d |= (unsigned)(c - 'A') + 10u;
831 dest[i] = (FLAC__byte)d;
836 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
838 FLAC__ASSERT(0 != src);
839 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
841 *dest = strtoul(src, 0, 10);
845 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
847 FLAC__ASSERT(0 != src);
848 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
850 *dest = strtoull(src, 0, 10);
854 FLAC__bool parse_string(const char *src, char **dest)
856 if(0 == src || strlen(src) == 0)
862 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
864 static const char * const violations[] = {
865 "field name contains invalid character"
870 s = local_strdup(field_ref);
872 for(q = s; *q; q++) {
873 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
875 *violation = violations[0];
885 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
887 static const char *garbled_ = "garbled specification";
888 const unsigned n = strlen(in);
890 FLAC__ASSERT(0 != in);
891 FLAC__ASSERT(0 != out);
894 *violation = "specification is empty";
898 if(n > strspn(in, "0123456789.Xsx")) {
899 *violation = "specification contains invalid character";
905 *violation = garbled_;
909 else if(in[n-1] == 's') {
910 if(n-1 > strspn(in, "0123456789.")) {
911 *violation = garbled_;
915 else if(in[n-1] == 'x') {
916 if(n-1 > strspn(in, "0123456789")) {
917 *violation = garbled_;
922 if(n > strspn(in, "0123456789")) {
923 *violation = garbled_;
928 *out = local_strdup(in);
932 FLAC__bool parse_add_padding(const char *in, unsigned *out)
934 FLAC__ASSERT(0 != in);
935 FLAC__ASSERT(0 != out);
936 *out = (unsigned)strtoul(in, 0, 10);
937 return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
940 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
942 char *p, *q, *s, *end;
949 s = local_strdup(in);
951 /* first count the entries */
952 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
956 FLAC__ASSERT(out->num_entries > 0);
957 if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(unsigned), /*times*/out->num_entries)))
958 die("out of memory allocating space for option list");
964 FLAC__ASSERT(entry < out->num_entries);
965 if(0 != (p = strchr(q, ',')))
967 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
971 out->entries[entry++] = (unsigned)i;
974 FLAC__ASSERT(entry == out->num_entries);
980 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
988 s = local_strdup(in);
990 /* first count the entries */
991 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
995 FLAC__ASSERT(out->num_entries > 0);
996 if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(Argument_BlockTypeEntry), /*times*/out->num_entries)))
997 die("out of memory allocating space for option list");
1003 FLAC__ASSERT(entry < out->num_entries);
1004 if(0 != (p = strchr(q, ',')))
1009 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
1013 if(0 == strcmp(q, "STREAMINFO")) {
1014 out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
1016 else if(0 == strcmp(q, "PADDING")) {
1017 out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
1019 else if(0 == strcmp(q, "APPLICATION")) {
1020 out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
1021 out->entries[entry].filter_application_by_id = (0 != r);
1023 if(strlen(r) == sizeof (out->entries[entry].application_id)) {
1024 memcpy(out->entries[entry].application_id, r, sizeof (out->entries[entry].application_id));
1026 else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
1027 FLAC__uint32 x = strtoul(r+2, 0, 16);
1028 out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
1029 out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
1030 out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
1031 out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
1040 else if(0 == strcmp(q, "SEEKTABLE")) {
1041 out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
1043 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
1044 out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1046 else if(0 == strcmp(q, "CUESHEET")) {
1047 out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
1049 else if(0 == strcmp(q, "PICTURE")) {
1050 out->entries[entry++].type = FLAC__METADATA_TYPE_PICTURE;
1058 FLAC__ASSERT(entry == out->num_entries);
1064 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1066 if(0 == strcmp(in, "binary"))
1067 out->is_binary = true;
1068 else if(0 == strcmp(in, "text"))
1069 out->is_binary = false;
1075 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1077 if(0 == strcmp(in, "hexdump"))
1079 else if(0 == strcmp(in, "text"))
1086 void undocumented_warning(const char *opt)
1088 flac_fprintf(stderr, "WARNING: undocumented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt);