1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2023 Xiph.Org Foundation
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "options.h"
25 #include "usage.h"
26 #include "utils.h"
27 #include "FLAC/assert.h"
28 #include "share/alloc.h"
29 #include "share/compat.h"
30 #include "share/grabbag/replaygain.h"
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 /*
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.
39 */
40 struct share__option long_options_[] = {
41 /* global 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-all-tags", 0, 0, 0 },
69 { "show-tag", 1, 0, 0 },
70 { "remove-all-tags", 0, 0, 0 },
71 { "remove-all-tags-except", 1, 0, 0 },
72 { "remove-tag", 1, 0, 0 },
73 { "remove-first-tag", 1, 0, 0 },
74 { "set-tag", 1, 0, 0 },
75 { "set-tag-from-file", 1, 0, 0 },
76 { "import-tags-from", 1, 0, 0 },
77 { "export-tags-to", 1, 0, 0 },
78 { "import-cuesheet-from", 1, 0, 0 },
79 { "export-cuesheet-to", 1, 0, 0 },
80 { "import-picture-from", 1, 0, 0 },
81 { "export-picture-to", 1, 0, 0 },
82 { "add-seekpoint", 1, 0, 0 },
83 { "add-replay-gain", 0, 0, 0 },
84 { "scan-replay-gain", 0, 0, 0 },
85 { "remove-replay-gain", 0, 0, 0 },
86 { "add-padding", 1, 0, 0 },
87 /* major operations */
88 { "help", 0, 0, 0 },
89 { "version", 0, 0, 0 },
90 { "list", 0, 0, 0 },
91 { "append", 0, 0, 0 },
92 { "remove", 0, 0, 0 },
93 { "remove-all", 0, 0, 0 },
94 { "merge-padding", 0, 0, 0 },
95 { "sort-padding", 0, 0, 0 },
96 /* major operation arguments */
97 { "block-number", 1, 0, 0 },
98 { "block-type", 1, 0, 0 },
99 { "except-block-type", 1, 0, 0 },
100 { "data-format", 1, 0, 0 },
101 { "application-data-format", 1, 0, 0 },
102 { "from-file", 1, 0, 0 },
103 {0, 0, 0, 0}
104 };
105
106 static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options);
107 static void append_new_operation(CommandLineOptions *options, Operation operation);
108 static void append_new_argument(CommandLineOptions *options, Argument argument);
109 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
110 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
111 static Argument *find_argument(CommandLineOptions *options, ArgumentType type);
112 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
113 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
114 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
115 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
116 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
117 static FLAC__bool parse_string(const char *src, char **dest);
118 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
119 static FLAC__bool parse_vorbis_comment_field_names(const char *field_ref, char **names, const char **violation);
120 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
121 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
122 static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out);
123 static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out);
124 static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out);
125 static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out);
126 static void undocumented_warning(const char *opt);
127
128
init_options(CommandLineOptions * options)129 void init_options(CommandLineOptions *options)
130 {
131 options->preserve_modtime = false;
132
133 /* '2' is a hack to mean "use default if not forced on command line" */
134 FLAC__ASSERT(true != 2);
135 options->prefix_with_filename = 2;
136
137 options->utf8_convert = true;
138 options->use_padding = true;
139 options->cued_seekpoints = true;
140 options->show_long_help = false;
141 options->show_version = false;
142 options->data_format_is_binary = false;
143 options->data_format_is_binary_headerless = false;
144 options->application_data_format_is_hexdump = false;
145
146 options->ops.operations = 0;
147 options->ops.num_operations = 0;
148 options->ops.capacity = 0;
149
150 options->args.arguments = 0;
151 options->args.num_arguments = 0;
152 options->args.capacity = 0;
153
154 options->args.checks.num_shorthand_ops = 0;
155 options->args.checks.num_major_ops = 0;
156 options->args.checks.has_block_type = false;
157 options->args.checks.has_except_block_type = false;
158
159 options->num_files = 0;
160 options->filenames = 0;
161 }
162
parse_options(int argc,char * argv[],CommandLineOptions * options)163 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
164 {
165 int ret;
166 int option_index = 1;
167 FLAC__bool had_error = false;
168
169 while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
170 switch (ret) {
171 case 0:
172 had_error |= !parse_option(option_index, share__optarg, options);
173 break;
174 case '?':
175 case ':':
176 had_error = true;
177 break;
178 default:
179 FLAC__ASSERT(0);
180 break;
181 }
182 }
183
184 if(options->prefix_with_filename == 2)
185 options->prefix_with_filename = (argc - share__optind > 1);
186
187 if(share__optind >= argc && !options->show_long_help && !options->show_version) {
188 flac_fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
189 flac_fprintf(stderr," metaflac cannot be used as a pipe\n");
190 had_error = true;
191 }
192
193 options->num_files = argc - share__optind;
194
195 if(options->num_files > 0) {
196 unsigned i = 0;
197 if(0 == (options->filenames = safe_malloc_mul_2op_(sizeof(char*), /*times*/options->num_files)))
198 die("out of memory allocating space for file names list");
199 while(share__optind < argc)
200 options->filenames[i++] = local_strdup(argv[share__optind++]);
201 }
202
203 if(options->args.checks.num_major_ops > 0) {
204 if(options->args.checks.num_major_ops > 1) {
205 flac_fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
206 had_error = true;
207 }
208 else if(options->args.checks.num_shorthand_ops > 0) {
209 flac_fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
210 had_error = true;
211 }
212 }
213
214 /* check for only one FLAC file used with certain options */
215 if(!had_error && options->num_files > 1) {
216 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
217 flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from'\n");
218 had_error = true;
219 }
220 if(0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) {
221 flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-cuesheet-to'\n");
222 had_error = true;
223 }
224 if(0 != find_shorthand_operation(options, OP__EXPORT_PICTURE_TO)) {
225 flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-picture-to'\n");
226 had_error = true;
227 }
228 if(
229 0 != find_shorthand_operation(options, OP__IMPORT_VC_FROM) &&
230 0 == strcmp(find_shorthand_operation(options, OP__IMPORT_VC_FROM)->argument.filename.value, "-")
231 ) {
232 flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-tags-from=-'\n");
233 had_error = true;
234 }
235 }
236
237 if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
238 flac_fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
239 had_error = true;
240 }
241
242 if(had_error)
243 short_usage(0);
244
245 /*
246 * We need to create an OP__ADD_SEEKPOINT operation if there is
247 * not one already, and --import-cuesheet-from was specified but
248 * --no-cued-seekpoints was not:
249 */
250 if(options->cued_seekpoints) {
251 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
252 if(0 != op) {
253 Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
254 if(0 == op2)
255 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
256 op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
257 }
258 }
259
260 return had_error;
261 }
262
free_options(CommandLineOptions * options)263 void free_options(CommandLineOptions *options)
264 {
265 unsigned i;
266 Operation *op;
267 Argument *arg;
268
269 FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
270 FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
271
272 for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
273 switch(op->type) {
274 case OP__SHOW_VC_FIELD:
275 case OP__REMOVE_VC_FIELD:
276 case OP__REMOVE_VC_FIRSTFIELD:
277 case OP__REMOVE_VC_ALL_EXCEPT:
278 if(0 != op->argument.vc_field_name.value)
279 free(op->argument.vc_field_name.value);
280 break;
281 case OP__SET_VC_FIELD:
282 if(0 != op->argument.vc_field.field)
283 free(op->argument.vc_field.field);
284 if(0 != op->argument.vc_field.field_name)
285 free(op->argument.vc_field.field_name);
286 if(0 != op->argument.vc_field.field_value)
287 free(op->argument.vc_field.field_value);
288 break;
289 case OP__IMPORT_VC_FROM:
290 case OP__EXPORT_VC_TO:
291 case OP__EXPORT_CUESHEET_TO:
292 if(0 != op->argument.filename.value)
293 free(op->argument.filename.value);
294 break;
295 case OP__IMPORT_CUESHEET_FROM:
296 if(0 != op->argument.import_cuesheet_from.filename)
297 free(op->argument.import_cuesheet_from.filename);
298 break;
299 case OP__IMPORT_PICTURE_FROM:
300 if(0 != op->argument.specification.value)
301 free(op->argument.specification.value);
302 break;
303 case OP__EXPORT_PICTURE_TO:
304 if(0 != op->argument.export_picture_to.filename)
305 free(op->argument.export_picture_to.filename);
306 break;
307 case OP__ADD_SEEKPOINT:
308 if(0 != op->argument.add_seekpoint.specification)
309 free(op->argument.add_seekpoint.specification);
310 break;
311 default:
312 break;
313 }
314 }
315
316 for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
317 switch(arg->type) {
318 case ARG__BLOCK_NUMBER:
319 if(0 != arg->value.block_number.entries)
320 free(arg->value.block_number.entries);
321 break;
322 case ARG__BLOCK_TYPE:
323 case ARG__EXCEPT_BLOCK_TYPE:
324 if(0 != arg->value.block_type.entries)
325 free(arg->value.block_type.entries);
326 break;
327 case ARG__FROM_FILE:
328 if(0 != arg->value.from_file.file_name)
329 free(arg->value.from_file.file_name);
330 break;
331 default:
332 break;
333 }
334 }
335
336 if(0 != options->ops.operations)
337 free(options->ops.operations);
338
339 if(0 != options->args.arguments)
340 free(options->args.arguments);
341
342 if(0 != options->filenames) {
343 for(i = 0; i < options->num_files; i++) {
344 if(0 != options->filenames[i])
345 free(options->filenames[i]);
346 }
347 free(options->filenames);
348 }
349 }
350
351 /*
352 * local routines
353 */
354
parse_option(int option_index,const char * option_argument,CommandLineOptions * options)355 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
356 {
357 const char *opt = long_options_[option_index].name;
358 Operation *op;
359 Argument *arg;
360 FLAC__bool ok = true;
361
362 if(0 == strcmp(opt, "preserve-modtime")) {
363 options->preserve_modtime = true;
364 }
365 else if(0 == strcmp(opt, "with-filename")) {
366 options->prefix_with_filename = true;
367 }
368 else if(0 == strcmp(opt, "no-filename")) {
369 options->prefix_with_filename = false;
370 }
371 else if(0 == strcmp(opt, "no-utf8-convert")) {
372 options->utf8_convert = false;
373 }
374 else if(0 == strcmp(opt, "dont-use-padding")) {
375 options->use_padding = false;
376 }
377 else if(0 == strcmp(opt, "no-cued-seekpoints")) {
378 options->cued_seekpoints = false;
379 }
380 else if(0 == strcmp(opt, "show-md5sum")) {
381 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
382 }
383 else if(0 == strcmp(opt, "show-min-blocksize")) {
384 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
385 }
386 else if(0 == strcmp(opt, "show-max-blocksize")) {
387 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
388 }
389 else if(0 == strcmp(opt, "show-min-framesize")) {
390 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
391 }
392 else if(0 == strcmp(opt, "show-max-framesize")) {
393 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
394 }
395 else if(0 == strcmp(opt, "show-sample-rate")) {
396 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
397 }
398 else if(0 == strcmp(opt, "show-channels")) {
399 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
400 }
401 else if(0 == strcmp(opt, "show-bps")) {
402 (void) append_shorthand_operation(options, OP__SHOW_BPS);
403 }
404 else if(0 == strcmp(opt, "show-total-samples")) {
405 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
406 }
407 else if(0 == strcmp(opt, "set-md5sum")) {
408 op = append_shorthand_operation(options, OP__SET_MD5SUM);
409 FLAC__ASSERT(0 != option_argument);
410 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
411 flac_fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
412 ok = false;
413 }
414 else
415 undocumented_warning(opt);
416 }
417 else if(0 == strcmp(opt, "set-min-blocksize")) {
418 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
419 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) {
420 flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
421 ok = false;
422 }
423 else
424 undocumented_warning(opt);
425 }
426 else if(0 == strcmp(opt, "set-max-blocksize")) {
427 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
428 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) {
429 flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
430 ok = false;
431 }
432 else
433 undocumented_warning(opt);
434 }
435 else if(0 == strcmp(opt, "set-min-framesize")) {
436 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
437 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
438 flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
439 ok = false;
440 }
441 else
442 undocumented_warning(opt);
443 }
444 else if(0 == strcmp(opt, "set-max-framesize")) {
445 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
446 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
447 flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
448 ok = false;
449 }
450 else
451 undocumented_warning(opt);
452 }
453 else if(0 == strcmp(opt, "set-sample-rate")) {
454 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
455 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
456 flac_fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
457 ok = false;
458 }
459 else
460 undocumented_warning(opt);
461 }
462 else if(0 == strcmp(opt, "set-channels")) {
463 op = append_shorthand_operation(options, OP__SET_CHANNELS);
464 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
465 flac_fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
466 ok = false;
467 }
468 else
469 undocumented_warning(opt);
470 }
471 else if(0 == strcmp(opt, "set-bps")) {
472 op = append_shorthand_operation(options, OP__SET_BPS);
473 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) {
474 flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
475 ok = false;
476 }
477 else
478 undocumented_warning(opt);
479 }
480 else if(0 == strcmp(opt, "set-total-samples")) {
481 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
482 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)) {
483 flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
484 ok = false;
485 }
486 else
487 undocumented_warning(opt);
488 }
489 else if(0 == strcmp(opt, "show-vendor-tag")) {
490 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
491 }
492 else if(0 == strcmp(opt, "show-tag")) {
493 const char *violation;
494 op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
495 FLAC__ASSERT(0 != option_argument);
496 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
497 FLAC__ASSERT(0 != violation);
498 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
499 ok = false;
500 }
501 }
502 else if(0 == strcmp(opt, "show-all-tags")) {
503 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
504 parse_string("-",&op->argument.filename.value);
505 }
506 else if(0 == strcmp(opt, "remove-all-tags")) {
507 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
508 }
509 else if(0 == strcmp(opt, "remove-all-tags-except")) {
510 const char *violation;
511 op = append_shorthand_operation(options, OP__REMOVE_VC_ALL_EXCEPT);
512 FLAC__ASSERT(0 != option_argument);
513 if(!parse_vorbis_comment_field_names(option_argument, &(op->argument.vc_field_name.value), &violation)) {
514 FLAC__ASSERT(0 != violation);
515 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
516 ok = false;
517 }
518 }
519 else if(0 == strcmp(opt, "remove-tag")) {
520 const char *violation;
521 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
522 FLAC__ASSERT(0 != option_argument);
523 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
524 FLAC__ASSERT(0 != violation);
525 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
526 ok = false;
527 }
528 }
529 else if(0 == strcmp(opt, "remove-first-tag")) {
530 const char *violation;
531 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
532 FLAC__ASSERT(0 != option_argument);
533 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
534 FLAC__ASSERT(0 != violation);
535 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
536 ok = false;
537 }
538 }
539 else if(0 == strcmp(opt, "set-tag")) {
540 const char *violation;
541 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
542 FLAC__ASSERT(0 != option_argument);
543 op->argument.vc_field.field_value_from_file = false;
544 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)) {
545 FLAC__ASSERT(0 != violation);
546 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
547 ok = false;
548 }
549 }
550 else if(0 == strcmp(opt, "set-tag-from-file")) {
551 const char *violation;
552 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
553 FLAC__ASSERT(0 != option_argument);
554 op->argument.vc_field.field_value_from_file = true;
555 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)) {
556 FLAC__ASSERT(0 != violation);
557 flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
558 ok = false;
559 }
560 }
561 else if(0 == strcmp(opt, "import-tags-from")) {
562 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
563 FLAC__ASSERT(0 != option_argument);
564 if(!parse_string(option_argument, &(op->argument.filename.value))) {
565 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
566 ok = false;
567 }
568 }
569 else if(0 == strcmp(opt, "export-tags-to")) {
570 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
571 FLAC__ASSERT(0 != option_argument);
572 if(!parse_string(option_argument, &(op->argument.filename.value))) {
573 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
574 ok = false;
575 }
576 }
577 else if(0 == strcmp(opt, "import-cuesheet-from")) {
578 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
579 flac_fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
580 ok = false;
581 }
582 op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
583 FLAC__ASSERT(0 != option_argument);
584 if(!parse_string(option_argument, &(op->argument.import_cuesheet_from.filename))) {
585 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
586 ok = false;
587 }
588 }
589 else if(0 == strcmp(opt, "export-cuesheet-to")) {
590 op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
591 FLAC__ASSERT(0 != option_argument);
592 if(!parse_string(option_argument, &(op->argument.filename.value))) {
593 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
594 ok = false;
595 }
596 }
597 else if(0 == strcmp(opt, "import-picture-from")) {
598 op = append_shorthand_operation(options, OP__IMPORT_PICTURE_FROM);
599 FLAC__ASSERT(0 != option_argument);
600 if(!parse_string(option_argument, &(op->argument.specification.value))) {
601 flac_fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
602 ok = false;
603 }
604 }
605 else if(0 == strcmp(opt, "export-picture-to")) {
606 arg = find_argument(options, ARG__BLOCK_NUMBER);
607 op = append_shorthand_operation(options, OP__EXPORT_PICTURE_TO);
608 FLAC__ASSERT(0 != option_argument);
609 if(!parse_string(option_argument, &(op->argument.export_picture_to.filename))) {
610 flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
611 ok = false;
612 }
613 op->argument.export_picture_to.block_number_link = arg? &(arg->value.block_number) : 0;
614 }
615 else if(0 == strcmp(opt, "add-seekpoint")) {
616 const char *violation;
617 char *spec;
618 FLAC__ASSERT(0 != option_argument);
619 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
620 FLAC__ASSERT(0 != violation);
621 flac_fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation);
622 ok = false;
623 }
624 else {
625 op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
626 if(0 == op)
627 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
628 local_strcat(&(op->argument.add_seekpoint.specification), spec);
629 local_strcat(&(op->argument.add_seekpoint.specification), ";");
630 free(spec);
631 }
632 }
633 else if(0 == strcmp(opt, "add-replay-gain")) {
634 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
635 }
636 else if(0 == strcmp(opt, "scan-replay-gain")) {
637 (void) append_shorthand_operation(options, OP__SCAN_REPLAY_GAIN);
638 }
639 else if(0 == strcmp(opt, "remove-replay-gain")) {
640 const FLAC__byte * const tags[5] = {
641 GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS,
642 GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN,
643 GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK,
644 GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN,
645 GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK
646 };
647 size_t i;
648 for(i = 0; i < sizeof(tags)/sizeof(tags[0]); i++) {
649 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
650 op->argument.vc_field_name.value = local_strdup((const char *)tags[i]);
651 }
652 }
653 else if(0 == strcmp(opt, "add-padding")) {
654 op = append_shorthand_operation(options, OP__ADD_PADDING);
655 FLAC__ASSERT(0 != option_argument);
656 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
657 flac_fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
658 ok = false;
659 }
660 }
661 else if(0 == strcmp(opt, "help")) {
662 options->show_long_help = true;
663 }
664 else if(0 == strcmp(opt, "version")) {
665 options->show_version = true;
666 }
667 else if(0 == strcmp(opt, "list")) {
668 (void) append_major_operation(options, OP__LIST);
669 }
670 else if(0 == strcmp(opt, "append")) {
671 (void) append_major_operation(options, OP__APPEND);
672 }
673 else if(0 == strcmp(opt, "remove")) {
674 (void) append_major_operation(options, OP__REMOVE);
675 }
676 else if(0 == strcmp(opt, "remove-all")) {
677 (void) append_major_operation(options, OP__REMOVE_ALL);
678 }
679 else if(0 == strcmp(opt, "merge-padding")) {
680 (void) append_major_operation(options, OP__MERGE_PADDING);
681 }
682 else if(0 == strcmp(opt, "sort-padding")) {
683 (void) append_major_operation(options, OP__SORT_PADDING);
684 }
685 else if(0 == strcmp(opt, "block-number")) {
686 arg = append_argument(options, ARG__BLOCK_NUMBER);
687 FLAC__ASSERT(0 != option_argument);
688 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
689 flac_fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
690 ok = false;
691 }
692 }
693 else if(0 == strcmp(opt, "block-type")) {
694 arg = append_argument(options, ARG__BLOCK_TYPE);
695 FLAC__ASSERT(0 != option_argument);
696 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
697 flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
698 ok = false;
699 }
700 options->args.checks.has_block_type = true;
701 }
702 else if(0 == strcmp(opt, "except-block-type")) {
703 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
704 FLAC__ASSERT(0 != option_argument);
705 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
706 flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
707 ok = false;
708 }
709 options->args.checks.has_except_block_type = true;
710 }
711 else if(0 == strcmp(opt, "data-format")) {
712 arg = append_argument(options, ARG__DATA_FORMAT);
713 FLAC__ASSERT(0 != option_argument);
714 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
715 flac_fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
716 ok = false;
717 }
718 options->data_format_is_binary = arg->value.data_format.is_binary;
719 options->data_format_is_binary_headerless = arg->value.data_format.is_headerless;
720 }
721 else if(0 == strcmp(opt, "application-data-format")) {
722 FLAC__ASSERT(0 != option_argument);
723 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
724 flac_fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
725 ok = false;
726 }
727 }
728 else if(0 == strcmp(opt, "from-file")) {
729 arg = append_argument(options, ARG__FROM_FILE);
730 FLAC__ASSERT(0 != option_argument);
731 arg->value.from_file.file_name = local_strdup(option_argument);
732 }
733 else {
734 FLAC__ASSERT(0);
735 }
736
737 return ok;
738 }
739
append_new_operation(CommandLineOptions * options,Operation operation)740 void append_new_operation(CommandLineOptions *options, Operation operation)
741 {
742 if(options->ops.capacity == 0) {
743 options->ops.capacity = 50;
744 if(0 == (options->ops.operations = malloc(sizeof(Operation) * options->ops.capacity)))
745 die("out of memory allocating space for option list");
746 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
747 }
748 if(options->ops.capacity <= options->ops.num_operations) {
749 unsigned original_capacity = options->ops.capacity;
750 if(options->ops.capacity > UINT32_MAX / 2) /* overflow check */
751 die("out of memory allocating space for option list");
752 options->ops.capacity *= 2;
753 if(0 == (options->ops.operations = safe_realloc_mul_2op_(options->ops.operations, sizeof(Operation), /*times*/options->ops.capacity)))
754 die("out of memory allocating space for option list");
755 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
756 }
757
758 options->ops.operations[options->ops.num_operations++] = operation;
759 }
760
append_new_argument(CommandLineOptions * options,Argument argument)761 void append_new_argument(CommandLineOptions *options, Argument argument)
762 {
763 if(options->args.capacity == 0) {
764 options->args.capacity = 50;
765 if(0 == (options->args.arguments = malloc(sizeof(Argument) * options->args.capacity)))
766 die("out of memory allocating space for option list");
767 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
768 }
769 if(options->args.capacity <= options->args.num_arguments) {
770 unsigned original_capacity = options->args.capacity;
771 if(options->args.capacity > UINT32_MAX / 2) /* overflow check */
772 die("out of memory allocating space for option list");
773 options->args.capacity *= 2;
774 if(0 == (options->args.arguments = safe_realloc_mul_2op_(options->args.arguments, sizeof(Argument), /*times*/options->args.capacity)))
775 die("out of memory allocating space for option list");
776 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
777 }
778
779 options->args.arguments[options->args.num_arguments++] = argument;
780 }
781
append_major_operation(CommandLineOptions * options,OperationType type)782 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
783 {
784 Operation op;
785 memset(&op, 0, sizeof(op));
786 op.type = type;
787 append_new_operation(options, op);
788 options->args.checks.num_major_ops++;
789 return options->ops.operations + (options->ops.num_operations - 1);
790 }
791
append_shorthand_operation(CommandLineOptions * options,OperationType type)792 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
793 {
794 Operation op;
795 memset(&op, 0, sizeof(op));
796 op.type = type;
797 append_new_operation(options, op);
798 options->args.checks.num_shorthand_ops++;
799 return options->ops.operations + (options->ops.num_operations - 1);
800 }
801
find_argument(CommandLineOptions * options,ArgumentType type)802 Argument *find_argument(CommandLineOptions *options, ArgumentType type)
803 {
804 unsigned i;
805 for(i = 0; i < options->args.num_arguments; i++)
806 if(options->args.arguments[i].type == type)
807 return &options->args.arguments[i];
808 return 0;
809 }
810
find_shorthand_operation(CommandLineOptions * options,OperationType type)811 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
812 {
813 unsigned i;
814 for(i = 0; i < options->ops.num_operations; i++)
815 if(options->ops.operations[i].type == type)
816 return &options->ops.operations[i];
817 return 0;
818 }
819
append_argument(CommandLineOptions * options,ArgumentType type)820 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
821 {
822 Argument arg;
823 memset(&arg, 0, sizeof(arg));
824 arg.type = type;
825 append_new_argument(options, arg);
826 return options->args.arguments + (options->args.num_arguments - 1);
827 }
828
parse_md5(const char * src,FLAC__byte dest[16])829 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
830 {
831 unsigned i, d;
832 int c;
833 FLAC__ASSERT(0 != src);
834 if(strlen(src) != 32)
835 return false;
836 /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
837 for(i = 0; i < 16; i++) {
838 c = (int)(*src++);
839 if(isdigit(c))
840 d = (unsigned)(c - '0');
841 else if(c >= 'a' && c <= 'f')
842 d = (unsigned)(c - 'a') + 10u;
843 else if(c >= 'A' && c <= 'F')
844 d = (unsigned)(c - 'A') + 10u;
845 else
846 return false;
847 d <<= 4;
848 c = (int)(*src++);
849 if(isdigit(c))
850 d |= (unsigned)(c - '0');
851 else if(c >= 'a' && c <= 'f')
852 d |= (unsigned)(c - 'a') + 10u;
853 else if(c >= 'A' && c <= 'F')
854 d |= (unsigned)(c - 'A') + 10u;
855 else
856 return false;
857 dest[i] = (FLAC__byte)d;
858 }
859 return true;
860 }
861
parse_uint32(const char * src,FLAC__uint32 * dest)862 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
863 {
864 FLAC__ASSERT(0 != src);
865 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
866 return false;
867 *dest = strtoul(src, 0, 10);
868 return true;
869 }
870
parse_uint64(const char * src,FLAC__uint64 * dest)871 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
872 {
873 FLAC__ASSERT(0 != src);
874 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
875 return false;
876 *dest = strtoull(src, 0, 10);
877 return true;
878 }
879
parse_string(const char * src,char ** dest)880 FLAC__bool parse_string(const char *src, char **dest)
881 {
882 if(0 == src || strlen(src) == 0)
883 return false;
884 *dest = strdup(src);
885 return true;
886 }
887
parse_vorbis_comment_field_name(const char * field_ref,char ** name,const char ** violation)888 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
889 {
890 static const char * const violations[] = {
891 "field name contains invalid character"
892 };
893
894 char *q, *s;
895
896 s = local_strdup(field_ref);
897
898 for(q = s; *q; q++) {
899 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
900 free(s);
901 *violation = violations[0];
902 return false;
903 }
904 }
905
906 *name = s;
907
908 return true;
909 }
910
parse_vorbis_comment_field_names(const char * field_ref,char ** names,const char ** violation)911 FLAC__bool parse_vorbis_comment_field_names(const char *field_ref, char **names, const char **violation)
912 {
913 static const char * const violations[] = {
914 "field name contains invalid character"
915 };
916
917 char *q, *s;
918
919 s = local_strdup(field_ref);
920
921 for(q = s; *q; q++) {
922 if(*q < 0x20 || *q > 0x7d) {
923 free(s);
924 *violation = violations[0];
925 return false;
926 }
927 }
928
929 *names = s;
930
931 return true;
932 }
933
parse_add_seekpoint(const char * in,char ** out,const char ** violation)934 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
935 {
936 static const char *garbled_ = "garbled specification";
937 const unsigned n = strlen(in);
938
939 FLAC__ASSERT(0 != in);
940 FLAC__ASSERT(0 != out);
941
942 if(n == 0) {
943 *violation = "specification is empty";
944 return false;
945 }
946
947 if(n > strspn(in, "0123456789.Xsx")) {
948 *violation = "specification contains invalid character";
949 return false;
950 }
951
952 if(in[n-1] == 'X') {
953 if(n > 1) {
954 *violation = garbled_;
955 return false;
956 }
957 }
958 else if(in[n-1] == 's') {
959 if(n-1 > strspn(in, "0123456789.")) {
960 *violation = garbled_;
961 return false;
962 }
963 }
964 else if(in[n-1] == 'x') {
965 if(n-1 > strspn(in, "0123456789")) {
966 *violation = garbled_;
967 return false;
968 }
969 }
970 else {
971 if(n > strspn(in, "0123456789")) {
972 *violation = garbled_;
973 return false;
974 }
975 }
976
977 *out = local_strdup(in);
978 return true;
979 }
980
parse_add_padding(const char * in,unsigned * out)981 FLAC__bool parse_add_padding(const char *in, unsigned *out)
982 {
983 FLAC__ASSERT(0 != in);
984 FLAC__ASSERT(0 != out);
985 *out = (unsigned)strtoul(in, 0, 10);
986 return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
987 }
988
parse_block_number(const char * in,Argument_BlockNumber * out)989 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
990 {
991 char *p, *q, *s, *end;
992 long i;
993 unsigned entry;
994
995 if(*in == '\0')
996 return false;
997
998 s = local_strdup(in);
999
1000 /* first count the entries */
1001 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
1002 ;
1003
1004 /* make space */
1005 FLAC__ASSERT(out->num_entries > 0);
1006 if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(unsigned), /*times*/out->num_entries)))
1007 die("out of memory allocating space for option list");
1008
1009 /* load 'em up */
1010 entry = 0;
1011 q = s;
1012 while(q) {
1013 FLAC__ASSERT(entry < out->num_entries);
1014 if(0 != (p = strchr(q, ',')))
1015 *p++ = '\0';
1016 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
1017 free(s);
1018 return false;
1019 }
1020 out->entries[entry++] = (unsigned)i;
1021 q = p;
1022 }
1023 FLAC__ASSERT(entry == out->num_entries);
1024
1025 free(s);
1026 return true;
1027 }
1028
parse_block_type(const char * in,Argument_BlockType * out)1029 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
1030 {
1031 char *p, *q, *r, *s;
1032 unsigned entry;
1033
1034 if(*in == '\0')
1035 return false;
1036
1037 s = local_strdup(in);
1038
1039 /* first count the entries */
1040 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
1041 ;
1042
1043 /* make space */
1044 FLAC__ASSERT(out->num_entries > 0);
1045 if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(Argument_BlockTypeEntry), /*times*/out->num_entries)))
1046 die("out of memory allocating space for option list");
1047
1048 /* load 'em up */
1049 entry = 0;
1050 q = s;
1051 while(q) {
1052 FLAC__ASSERT(entry < out->num_entries);
1053 if(0 != (p = strchr(q, ',')))
1054 *p++ = 0;
1055 r = strchr(q, ':');
1056 if(r)
1057 *r++ = '\0';
1058 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
1059 free(s);
1060 return false;
1061 }
1062 if(0 == strcmp(q, "STREAMINFO")) {
1063 out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
1064 }
1065 else if(0 == strcmp(q, "PADDING")) {
1066 out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
1067 }
1068 else if(0 == strcmp(q, "APPLICATION")) {
1069 out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
1070 out->entries[entry].filter_application_by_id = (0 != r);
1071 if(0 != r) {
1072 if(strlen(r) == sizeof (out->entries[entry].application_id)) {
1073 memcpy(out->entries[entry].application_id, r, sizeof (out->entries[entry].application_id));
1074 }
1075 else if(strlen(r) == 10 && FLAC__STRNCASECMP(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
1076 FLAC__uint32 x = strtoul(r+2, 0, 16);
1077 out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
1078 out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
1079 out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
1080 out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
1081 }
1082 else {
1083 free(s);
1084 return false;
1085 }
1086 }
1087 entry++;
1088 }
1089 else if(0 == strcmp(q, "SEEKTABLE")) {
1090 out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
1091 }
1092 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
1093 out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1094 }
1095 else if(0 == strcmp(q, "CUESHEET")) {
1096 out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
1097 }
1098 else if(0 == strcmp(q, "PICTURE")) {
1099 out->entries[entry++].type = FLAC__METADATA_TYPE_PICTURE;
1100 }
1101 else {
1102 free(s);
1103 return false;
1104 }
1105 q = p;
1106 }
1107 FLAC__ASSERT(entry == out->num_entries);
1108
1109 free(s);
1110 return true;
1111 }
1112
parse_data_format(const char * in,Argument_DataFormat * out)1113 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1114 {
1115 if(0 == strcmp(in, "binary-headerless")) {
1116 out->is_binary = false;
1117 out->is_headerless = true;
1118 }
1119 else if(0 == strcmp(in, "binary")) {
1120 out->is_binary = true;
1121 out->is_headerless = false;
1122 }
1123 else if(0 == strcmp(in, "text")) {
1124 out->is_binary = false;
1125 out->is_headerless = false;
1126 }
1127 else
1128 return false;
1129 return true;
1130 }
1131
parse_application_data_format(const char * in,FLAC__bool * out)1132 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1133 {
1134 if(0 == strcmp(in, "hexdump"))
1135 *out = true;
1136 else if(0 == strcmp(in, "text"))
1137 *out = false;
1138 else
1139 return false;
1140 return true;
1141 }
1142
undocumented_warning(const char * opt)1143 void undocumented_warning(const char *opt)
1144 {
1145 flac_fprintf(stderr, "WARNING: undocumented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt);
1146 }
1147