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