1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003 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 */
59 { "show-vc-vendor", 0, 0, 0 },
60 { "show-vc-field", 1, 0, 0 },
61 { "remove-vc-all", 0, 0, 0 },
62 { "remove-vc-field", 1, 0, 0 },
63 { "remove-vc-firstfield", 1, 0, 0 },
64 { "set-vc-field", 1, 0, 0 },
65 { "import-vc-from", 1, 0, 0 },
66 { "export-vc-to", 1, 0, 0 },
67 { "import-cuesheet-from", 1, 0, 0 },
68 { "export-cuesheet-to", 1, 0, 0 },
69 { "add-seekpoint", 1, 0, 0 },
70 { "add-replay-gain", 0, 0, 0 },
71 { "add-padding", 1, 0, 0 },
72 /* major operations */
74 { "version", 0, 0, 0 },
76 { "append", 0, 0, 0 },
77 { "remove", 0, 0, 0 },
78 { "remove-all", 0, 0, 0 },
79 { "merge-padding", 0, 0, 0 },
80 { "sort-padding", 0, 0, 0 },
81 /* major operation arguments */
82 { "block-number", 1, 0, 0 },
83 { "block-type", 1, 0, 0 },
84 { "except-block-type", 1, 0, 0 },
85 { "data-format", 1, 0, 0 },
86 { "application-data-format", 1, 0, 0 },
87 { "from-file", 1, 0, 0 },
91 static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options);
92 static void append_new_operation(CommandLineOptions *options, Operation operation);
93 static void append_new_argument(CommandLineOptions *options, Argument argument);
94 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
95 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
96 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
97 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
98 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
99 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
100 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
101 static FLAC__bool parse_filename(const char *src, char **dest);
102 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
103 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
104 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
105 static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out);
106 static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out);
107 static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out);
108 static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out);
109 static void undocumented_warning(const char *opt);
112 void init_options(CommandLineOptions *options)
114 options->preserve_modtime = false;
116 /* '2' is a hack to mean "use default if not forced on command line" */
117 FLAC__ASSERT(true != 2);
118 options->prefix_with_filename = 2;
120 options->utf8_convert = true;
121 options->use_padding = true;
122 options->cued_seekpoints = true;
123 options->show_long_help = false;
124 options->show_version = false;
125 options->application_data_format_is_hexdump = false;
127 options->ops.operations = 0;
128 options->ops.num_operations = 0;
129 options->ops.capacity = 0;
131 options->args.arguments = 0;
132 options->args.num_arguments = 0;
133 options->args.capacity = 0;
135 options->args.checks.num_shorthand_ops = 0;
136 options->args.checks.num_major_ops = 0;
137 options->args.checks.has_block_type = false;
138 options->args.checks.has_except_block_type = false;
140 options->num_files = 0;
141 options->filenames = 0;
144 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
147 int option_index = 1;
148 FLAC__bool had_error = false;
150 while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
153 had_error |= !parse_option(option_index, share__optarg, options);
165 if(options->prefix_with_filename == 2)
166 options->prefix_with_filename = (argc - share__optind > 1);
168 if(share__optind >= argc && !options->show_long_help && !options->show_version) {
169 fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
170 fprintf(stderr," metaflac cannot be used as a pipe\n");
174 options->num_files = argc - share__optind;
176 if(options->num_files > 0) {
178 if(0 == (options->filenames = malloc(sizeof(char *) * options->num_files)))
179 die("out of memory allocating space for file names list");
180 while(share__optind < argc)
181 options->filenames[i++] = local_strdup(argv[share__optind++]);
184 if(options->args.checks.num_major_ops > 0) {
185 if(options->args.checks.num_major_ops > 1) {
186 fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
189 else if(options->args.checks.num_shorthand_ops > 0) {
190 fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
195 /* check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to */
196 if((0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) || 0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) && options->num_files > 1) {
197 fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from' or '--export-cuesheet-to'\n");
201 if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
202 fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
210 * We need to create an OP__ADD_SEEKPOINT operation if there is
211 * not one already, and --import-cuesheet-from was specified but
212 * --no-cued-seekpoints was not:
214 if(options->cued_seekpoints) {
215 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
217 Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
219 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
220 op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
227 void free_options(CommandLineOptions *options)
233 FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
234 FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
236 for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
238 case OP__SHOW_VC_FIELD:
239 case OP__REMOVE_VC_FIELD:
240 case OP__REMOVE_VC_FIRSTFIELD:
241 if(0 != op->argument.vc_field_name.value)
242 free(op->argument.vc_field_name.value);
244 case OP__SET_VC_FIELD:
245 if(0 != op->argument.vc_field.field)
246 free(op->argument.vc_field.field);
247 if(0 != op->argument.vc_field.field_name)
248 free(op->argument.vc_field.field_name);
249 if(0 != op->argument.vc_field.field_value)
250 free(op->argument.vc_field.field_value);
252 case OP__IMPORT_VC_FROM:
253 case OP__EXPORT_VC_TO:
254 case OP__EXPORT_CUESHEET_TO:
255 if(0 != op->argument.filename.value)
256 free(op->argument.filename.value);
258 case OP__IMPORT_CUESHEET_FROM:
259 if(0 != op->argument.import_cuesheet_from.filename)
260 free(op->argument.import_cuesheet_from.filename);
262 case OP__ADD_SEEKPOINT:
263 if(0 != op->argument.add_seekpoint.specification)
264 free(op->argument.add_seekpoint.specification);
271 for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
273 case ARG__BLOCK_NUMBER:
274 if(0 != arg->value.block_number.entries)
275 free(arg->value.block_number.entries);
277 case ARG__BLOCK_TYPE:
278 case ARG__EXCEPT_BLOCK_TYPE:
279 if(0 != arg->value.block_type.entries)
280 free(arg->value.block_type.entries);
283 if(0 != arg->value.from_file.file_name)
284 free(arg->value.from_file.file_name);
291 if(0 != options->ops.operations)
292 free(options->ops.operations);
294 if(0 != options->args.arguments)
295 free(options->args.arguments);
297 if(0 != options->filenames) {
298 for(i = 0; i < options->num_files; i++) {
299 if(0 != options->filenames[i])
300 free(options->filenames[i]);
302 free(options->filenames);
310 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
312 const char *opt = long_options_[option_index].name;
315 FLAC__bool ok = true;
317 if(0 == strcmp(opt, "preserve-modtime")) {
318 options->preserve_modtime = true;
320 else if(0 == strcmp(opt, "with-filename")) {
321 options->prefix_with_filename = true;
323 else if(0 == strcmp(opt, "no-filename")) {
324 options->prefix_with_filename = false;
326 else if(0 == strcmp(opt, "no-utf8-convert")) {
327 options->utf8_convert = false;
329 else if(0 == strcmp(opt, "dont-use-padding")) {
330 options->use_padding = false;
332 else if(0 == strcmp(opt, "no-cued-seekpoints")) {
333 options->cued_seekpoints = false;
335 else if(0 == strcmp(opt, "show-md5sum")) {
336 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
338 else if(0 == strcmp(opt, "show-min-blocksize")) {
339 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
341 else if(0 == strcmp(opt, "show-max-blocksize")) {
342 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
344 else if(0 == strcmp(opt, "show-min-framesize")) {
345 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
347 else if(0 == strcmp(opt, "show-max-framesize")) {
348 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
350 else if(0 == strcmp(opt, "show-sample-rate")) {
351 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
353 else if(0 == strcmp(opt, "show-channels")) {
354 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
356 else if(0 == strcmp(opt, "show-bps")) {
357 (void) append_shorthand_operation(options, OP__SHOW_BPS);
359 else if(0 == strcmp(opt, "show-total-samples")) {
360 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
362 else if(0 == strcmp(opt, "set-md5sum")) {
363 op = append_shorthand_operation(options, OP__SET_MD5SUM);
364 FLAC__ASSERT(0 != option_argument);
365 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
366 fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
370 undocumented_warning(opt);
372 else if(0 == strcmp(opt, "set-min-blocksize")) {
373 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
374 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) {
375 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
379 undocumented_warning(opt);
381 else if(0 == strcmp(opt, "set-max-blocksize")) {
382 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
383 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) {
384 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
388 undocumented_warning(opt);
390 else if(0 == strcmp(opt, "set-min-framesize")) {
391 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
392 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
393 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
397 undocumented_warning(opt);
399 else if(0 == strcmp(opt, "set-max-framesize")) {
400 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
401 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
402 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
406 undocumented_warning(opt);
408 else if(0 == strcmp(opt, "set-sample-rate")) {
409 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
410 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
411 fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
415 undocumented_warning(opt);
417 else if(0 == strcmp(opt, "set-channels")) {
418 op = append_shorthand_operation(options, OP__SET_CHANNELS);
419 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
420 fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
424 undocumented_warning(opt);
426 else if(0 == strcmp(opt, "set-bps")) {
427 op = append_shorthand_operation(options, OP__SET_BPS);
428 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) {
429 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
433 undocumented_warning(opt);
435 else if(0 == strcmp(opt, "set-total-samples")) {
436 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
437 if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
438 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
442 undocumented_warning(opt);
444 else if(0 == strcmp(opt, "show-vc-vendor")) {
445 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
447 else if(0 == strcmp(opt, "show-vc-field")) {
448 const char *violation;
449 op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
450 FLAC__ASSERT(0 != option_argument);
451 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
452 FLAC__ASSERT(0 != violation);
453 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
457 else if(0 == strcmp(opt, "remove-vc-all")) {
458 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
460 else if(0 == strcmp(opt, "remove-vc-field")) {
461 const char *violation;
462 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
463 FLAC__ASSERT(0 != option_argument);
464 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
465 FLAC__ASSERT(0 != violation);
466 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
470 else if(0 == strcmp(opt, "remove-vc-firstfield")) {
471 const char *violation;
472 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
473 FLAC__ASSERT(0 != option_argument);
474 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
475 FLAC__ASSERT(0 != violation);
476 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
480 else if(0 == strcmp(opt, "set-vc-field")) {
481 const char *violation;
482 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
483 FLAC__ASSERT(0 != option_argument);
484 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)) {
485 FLAC__ASSERT(0 != violation);
486 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
490 else if(0 == strcmp(opt, "import-vc-from")) {
491 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
492 FLAC__ASSERT(0 != option_argument);
493 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
494 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
498 else if(0 == strcmp(opt, "export-vc-to")) {
499 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
500 FLAC__ASSERT(0 != option_argument);
501 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
502 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
506 else if(0 == strcmp(opt, "import-cuesheet-from")) {
507 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
508 fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
511 op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
512 FLAC__ASSERT(0 != option_argument);
513 if(!parse_filename(option_argument, &(op->argument.import_cuesheet_from.filename))) {
514 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
518 else if(0 == strcmp(opt, "export-cuesheet-to")) {
519 op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
520 FLAC__ASSERT(0 != option_argument);
521 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
522 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
526 else if(0 == strcmp(opt, "add-seekpoint")) {
527 const char *violation;
529 FLAC__ASSERT(0 != option_argument);
530 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
531 FLAC__ASSERT(0 != violation);
532 fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation);
535 op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
537 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
538 local_strcat(&(op->argument.add_seekpoint.specification), spec);
539 local_strcat(&(op->argument.add_seekpoint.specification), ";");
542 else if(0 == strcmp(opt, "add-replay-gain")) {
543 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
545 else if(0 == strcmp(opt, "add-padding")) {
546 op = append_shorthand_operation(options, OP__ADD_PADDING);
547 FLAC__ASSERT(0 != option_argument);
548 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
549 fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
553 else if(0 == strcmp(opt, "help")) {
554 options->show_long_help = true;
556 else if(0 == strcmp(opt, "version")) {
557 options->show_version = true;
559 else if(0 == strcmp(opt, "list")) {
560 (void) append_major_operation(options, OP__LIST);
562 else if(0 == strcmp(opt, "append")) {
563 (void) append_major_operation(options, OP__APPEND);
565 else if(0 == strcmp(opt, "remove")) {
566 (void) append_major_operation(options, OP__REMOVE);
568 else if(0 == strcmp(opt, "remove-all")) {
569 (void) append_major_operation(options, OP__REMOVE_ALL);
571 else if(0 == strcmp(opt, "merge-padding")) {
572 (void) append_major_operation(options, OP__MERGE_PADDING);
574 else if(0 == strcmp(opt, "sort-padding")) {
575 (void) append_major_operation(options, OP__SORT_PADDING);
577 else if(0 == strcmp(opt, "block-number")) {
578 arg = append_argument(options, ARG__BLOCK_NUMBER);
579 FLAC__ASSERT(0 != option_argument);
580 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
581 fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
585 else if(0 == strcmp(opt, "block-type")) {
586 arg = append_argument(options, ARG__BLOCK_TYPE);
587 FLAC__ASSERT(0 != option_argument);
588 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
589 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
592 options->args.checks.has_block_type = true;
594 else if(0 == strcmp(opt, "except-block-type")) {
595 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
596 FLAC__ASSERT(0 != option_argument);
597 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
598 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
601 options->args.checks.has_except_block_type = true;
603 else if(0 == strcmp(opt, "data-format")) {
604 arg = append_argument(options, ARG__DATA_FORMAT);
605 FLAC__ASSERT(0 != option_argument);
606 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
607 fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
611 else if(0 == strcmp(opt, "application-data-format")) {
612 FLAC__ASSERT(0 != option_argument);
613 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
614 fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
618 else if(0 == strcmp(opt, "from-file")) {
619 arg = append_argument(options, ARG__FROM_FILE);
620 FLAC__ASSERT(0 != option_argument);
621 arg->value.from_file.file_name = local_strdup(option_argument);
630 void append_new_operation(CommandLineOptions *options, Operation operation)
632 if(options->ops.capacity == 0) {
633 options->ops.capacity = 50;
634 if(0 == (options->ops.operations = malloc(sizeof(Operation) * options->ops.capacity)))
635 die("out of memory allocating space for option list");
636 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
638 if(options->ops.capacity <= options->ops.num_operations) {
639 unsigned original_capacity = options->ops.capacity;
640 options->ops.capacity *= 4;
641 if(0 == (options->ops.operations = realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
642 die("out of memory allocating space for option list");
643 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
646 options->ops.operations[options->ops.num_operations++] = operation;
649 void append_new_argument(CommandLineOptions *options, Argument argument)
651 if(options->args.capacity == 0) {
652 options->args.capacity = 50;
653 if(0 == (options->args.arguments = malloc(sizeof(Argument) * options->args.capacity)))
654 die("out of memory allocating space for option list");
655 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
657 if(options->args.capacity <= options->args.num_arguments) {
658 unsigned original_capacity = options->args.capacity;
659 options->args.capacity *= 4;
660 if(0 == (options->args.arguments = realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
661 die("out of memory allocating space for option list");
662 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
665 options->args.arguments[options->args.num_arguments++] = argument;
668 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
671 memset(&op, 0, sizeof(op));
673 append_new_operation(options, op);
674 options->args.checks.num_major_ops++;
675 return options->ops.operations + (options->ops.num_operations - 1);
678 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
681 memset(&op, 0, sizeof(op));
683 append_new_operation(options, op);
684 options->args.checks.num_shorthand_ops++;
685 return options->ops.operations + (options->ops.num_operations - 1);
688 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
691 for(i = 0; i < options->ops.num_operations; i++)
692 if(options->ops.operations[i].type == type)
693 return &options->ops.operations[i];
697 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
700 memset(&arg, 0, sizeof(arg));
702 append_new_argument(options, arg);
703 return options->args.arguments + (options->args.num_arguments - 1);
706 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
710 FLAC__ASSERT(0 != src);
711 if(strlen(src) != 32)
713 /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
714 for(i = 0; i < 16; i++) {
717 d = (unsigned)(c - '0');
718 else if(c >= 'a' && c <= 'f')
719 d = (unsigned)(c - 'a') + 10u;
720 else if(c >= 'A' && c <= 'F')
721 d = (unsigned)(c - 'A') + 10u;
727 d |= (unsigned)(c - '0');
728 else if(c >= 'a' && c <= 'f')
729 d |= (unsigned)(c - 'a') + 10u;
730 else if(c >= 'A' && c <= 'F')
731 d |= (unsigned)(c - 'A') + 10u;
734 dest[i] = (FLAC__byte)d;
739 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
741 FLAC__ASSERT(0 != src);
742 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
744 *dest = strtoul(src, 0, 10);
748 /* There's no stroull() in MSVC6 so we just write a specialized one */
749 static FLAC__uint64 local__strtoull(const char *src)
751 FLAC__uint64 ret = 0;
753 FLAC__ASSERT(0 != src);
754 while(0 != (c = *src++)) {
757 ret = (ret * 10) + c;
764 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
766 FLAC__ASSERT(0 != src);
767 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
769 *dest = local__strtoull(src);
773 FLAC__bool parse_filename(const char *src, char **dest)
775 if(0 == src || strlen(src) == 0)
781 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
783 static const char * const violations[] = {
784 "field name contains invalid character"
789 s = local_strdup(field_ref);
791 for(q = s; *q; q++) {
792 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
794 *violation = violations[0];
804 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
806 static const char *garbled_ = "garbled specification";
807 const unsigned n = strlen(in);
809 FLAC__ASSERT(0 != in);
810 FLAC__ASSERT(0 != out);
813 *violation = "specification is empty";
817 if(n > strspn(in, "0123456789.Xsx")) {
818 *violation = "specification contains invalid character";
824 *violation = garbled_;
828 else if(in[n-1] == 's') {
829 if(n-1 > strspn(in, "0123456789.")) {
830 *violation = garbled_;
834 else if(in[n-1] == 'x') {
835 if(n-1 > strspn(in, "0123456789")) {
836 *violation = garbled_;
841 if(n > strspn(in, "0123456789")) {
842 *violation = garbled_;
847 *out = local_strdup(in);
851 FLAC__bool parse_add_padding(const char *in, unsigned *out)
853 FLAC__ASSERT(0 != in);
854 FLAC__ASSERT(0 != out);
855 *out = (unsigned)strtoul(in, 0, 10);
856 return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
859 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
861 char *p, *q, *s, *end;
868 s = local_strdup(in);
870 /* first count the entries */
871 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
875 FLAC__ASSERT(out->num_entries > 0);
876 if(0 == (out->entries = malloc(sizeof(unsigned) * out->num_entries)))
877 die("out of memory allocating space for option list");
883 FLAC__ASSERT(entry < out->num_entries);
884 if(0 != (p = strchr(q, ',')))
886 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
890 out->entries[entry++] = (unsigned)i;
893 FLAC__ASSERT(entry == out->num_entries);
899 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
907 s = local_strdup(in);
909 /* first count the entries */
910 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
914 FLAC__ASSERT(out->num_entries > 0);
915 if(0 == (out->entries = malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
916 die("out of memory allocating space for option list");
922 FLAC__ASSERT(entry < out->num_entries);
923 if(0 != (p = strchr(q, ',')))
928 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
932 if(0 == strcmp(q, "STREAMINFO")) {
933 out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
935 else if(0 == strcmp(q, "PADDING")) {
936 out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
938 else if(0 == strcmp(q, "APPLICATION")) {
939 out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
940 out->entries[entry].filter_application_by_id = (0 != r);
943 strcpy(out->entries[entry].application_id, r);
945 else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
946 FLAC__uint32 x = strtoul(r+2, 0, 16);
947 out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
948 out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
949 out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
950 out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
959 else if(0 == strcmp(q, "SEEKTABLE")) {
960 out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
962 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
963 out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
965 else if(0 == strcmp(q, "CUESHEET")) {
966 out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
974 FLAC__ASSERT(entry == out->num_entries);
980 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
982 if(0 == strcmp(in, "binary"))
983 out->is_binary = true;
984 else if(0 == strcmp(in, "text"))
985 out->is_binary = false;
991 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
993 if(0 == strcmp(in, "hexdump"))
995 else if(0 == strcmp(in, "text"))
1002 void undocumented_warning(const char *opt)
1004 fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt);