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