• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001-2009  Josh Coalson
3  * Copyright (C) 2011-2022  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 "operations.h"
25 #include "usage.h"
26 #include "utils.h"
27 #include "FLAC/assert.h"
28 #include "FLAC/metadata.h"
29 #include "share/alloc.h"
30 #include "share/grabbag.h"
31 #include "share/compat.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "operations_shorthand.h"
36 
37 static void show_version(void);
38 static FLAC__bool do_major_operation(const CommandLineOptions *options);
39 static FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options);
40 static FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
41 static FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
42 static FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
43 static FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
44 static FLAC__bool do_shorthand_operations(const CommandLineOptions *options);
45 static FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options);
46 static FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert);
47 static FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime, FLAC__bool scan);
48 static FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write);
49 
50 static FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number);
51 static void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application);
52 
53 /* from operations_shorthand_seektable.c */
54 extern FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write);
55 
56 /* from operations_shorthand_streaminfo.c */
57 extern FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
58 
59 /* from operations_shorthand_vorbiscomment.c */
60 extern FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw);
61 
62 /* from operations_shorthand_cuesheet.c */
63 extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
64 
65 /* from operations_shorthand_picture.c */
66 extern FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
67 
68 
do_operations(const CommandLineOptions * options)69 FLAC__bool do_operations(const CommandLineOptions *options)
70 {
71 	FLAC__bool ok = true;
72 
73 	if(options->show_long_help) {
74 		long_usage(0);
75 	}
76 	if(options->show_version) {
77 		show_version();
78 	}
79 	else if(options->args.checks.num_major_ops > 0) {
80 		FLAC__ASSERT(options->args.checks.num_shorthand_ops == 0);
81 		FLAC__ASSERT(options->args.checks.num_major_ops == 1);
82 		FLAC__ASSERT(options->args.checks.num_major_ops == options->ops.num_operations);
83 		ok = do_major_operation(options);
84 	}
85 	else if(options->args.checks.num_shorthand_ops > 0) {
86 		FLAC__ASSERT(options->args.checks.num_shorthand_ops == options->ops.num_operations);
87 		ok = do_shorthand_operations(options);
88 	}
89 
90 	return ok;
91 }
92 
93 /*
94  * local routines
95  */
96 
show_version(void)97 void show_version(void)
98 {
99 	printf("metaflac %s\n", FLAC__VERSION_STRING);
100 }
101 
do_major_operation(const CommandLineOptions * options)102 FLAC__bool do_major_operation(const CommandLineOptions *options)
103 {
104 	unsigned i;
105 	FLAC__bool ok = true;
106 
107 	/* to die after first error,     v---  add '&& ok' here */
108 	for(i = 0; i < options->num_files; i++)
109 		ok &= do_major_operation_on_file(options->filenames[i], options);
110 
111 	return ok;
112 }
113 
do_major_operation_on_file(const char * filename,const CommandLineOptions * options)114 FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options)
115 {
116 	FLAC__bool ok = true, needs_write = false, is_ogg = false;
117 	FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
118 
119 	if(0 == chain)
120 		die("out of memory allocating chain");
121 
122 	/*@@@@ lame way of guessing the file type */
123 	if(strlen(filename) >= 4 && (0 == strcmp(filename+strlen(filename)-4, ".oga") || 0 == strcmp(filename+strlen(filename)-4, ".ogg")))
124 		is_ogg = true;
125 
126 	if(! (is_ogg? FLAC__metadata_chain_read_ogg(chain, filename) : FLAC__metadata_chain_read(chain, filename)) ) {
127 		print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
128 		FLAC__metadata_chain_delete(chain);
129 		return false;
130 	}
131 
132 	switch(options->ops.operations[0].type) {
133 		case OP__LIST:
134 			ok = do_major_operation__list(options->prefix_with_filename? filename : 0, chain, options);
135 			break;
136 		case OP__APPEND:
137 			ok = do_major_operation__append(chain, options);
138 			needs_write = true;
139 			break;
140 		case OP__REMOVE:
141 			ok = do_major_operation__remove(chain, options);
142 			needs_write = true;
143 			break;
144 		case OP__REMOVE_ALL:
145 			ok = do_major_operation__remove_all(chain, options);
146 			needs_write = true;
147 			break;
148 		case OP__MERGE_PADDING:
149 			FLAC__metadata_chain_merge_padding(chain);
150 			needs_write = true;
151 			break;
152 		case OP__SORT_PADDING:
153 			FLAC__metadata_chain_sort_padding(chain);
154 			needs_write = true;
155 			break;
156 		default:
157 			FLAC__ASSERT(0);
158 			return false;
159 	}
160 
161 	if(ok && needs_write) {
162 		if(options->use_padding)
163 			FLAC__metadata_chain_sort_padding(chain);
164 		ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime);
165 		if(!ok)
166 			print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
167 	}
168 
169 	FLAC__metadata_chain_delete(chain);
170 
171 	return ok;
172 }
173 
do_major_operation__list(const char * filename,FLAC__Metadata_Chain * chain,const CommandLineOptions * options)174 FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
175 {
176 	FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
177 	FLAC__StreamMetadata *block;
178 	FLAC__bool ok = true;
179 	unsigned block_number;
180 
181 	if(0 == iterator)
182 		die("out of memory allocating iterator");
183 
184 	FLAC__metadata_iterator_init(iterator, chain);
185 
186 	block_number = 0;
187 	do {
188 		block = FLAC__metadata_iterator_get_block(iterator);
189 		ok &= (0 != block);
190 		if(!ok)
191 			flac_fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
192 		else if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number))
193 			write_metadata(filename, block, block_number, !options->utf8_convert, options->application_data_format_is_hexdump);
194 		block_number++;
195 	} while(ok && FLAC__metadata_iterator_next(iterator));
196 
197 	FLAC__metadata_iterator_delete(iterator);
198 
199 	return ok;
200 }
201 
do_major_operation__append(FLAC__Metadata_Chain * chain,const CommandLineOptions * options)202 FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
203 {
204 	(void) chain, (void) options;
205 	flac_fprintf(stderr, "ERROR: --append not implemented yet\n");
206 	return false;
207 }
208 
do_major_operation__remove(FLAC__Metadata_Chain * chain,const CommandLineOptions * options)209 FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
210 {
211 	FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
212 	FLAC__bool ok = true;
213 	unsigned block_number;
214 
215 	if(0 == iterator)
216 		die("out of memory allocating iterator");
217 
218 	FLAC__metadata_iterator_init(iterator, chain);
219 
220 	block_number = 0;
221 	while(ok && FLAC__metadata_iterator_next(iterator)) {
222 		block_number++;
223 		if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number)) {
224 			ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
225 			if(options->use_padding)
226 				ok &= FLAC__metadata_iterator_next(iterator);
227 		}
228 	}
229 
230 	FLAC__metadata_iterator_delete(iterator);
231 
232 	return ok;
233 }
234 
do_major_operation__remove_all(FLAC__Metadata_Chain * chain,const CommandLineOptions * options)235 FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
236 {
237 	FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
238 	FLAC__bool ok = true;
239 
240 	if(0 == iterator)
241 		die("out of memory allocating iterator");
242 
243 	FLAC__metadata_iterator_init(iterator, chain);
244 
245 	while(ok && FLAC__metadata_iterator_next(iterator)) {
246 		ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
247 		if(options->use_padding)
248 			ok &= FLAC__metadata_iterator_next(iterator);
249 	}
250 
251 	FLAC__metadata_iterator_delete(iterator);
252 
253 	return ok;
254 }
255 
do_shorthand_operations(const CommandLineOptions * options)256 FLAC__bool do_shorthand_operations(const CommandLineOptions *options)
257 {
258 	unsigned i;
259 	FLAC__bool ok = true;
260 
261 	/* to die after first error,     v---  add '&& ok' here */
262 	for(i = 0; i < options->num_files; i++)
263 		ok &= do_shorthand_operations_on_file(options->filenames[i], options);
264 
265 	/* check if OP__ADD_REPLAY_GAIN requested */
266 	if(ok && options->num_files > 0) {
267 		for(i = 0; i < options->ops.num_operations; i++) {
268 			if(options->ops.operations[i].type == OP__ADD_REPLAY_GAIN)
269 				ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime, false);
270 			else if(options->ops.operations[i].type == OP__SCAN_REPLAY_GAIN)
271 				ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime, true);
272 		}
273 	}
274 
275 	return ok;
276 }
277 
do_shorthand_operations_on_file(const char * filename,const CommandLineOptions * options)278 FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options)
279 {
280 	unsigned i;
281 	FLAC__bool ok = true, needs_write = false, use_padding = options->use_padding;
282 	FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
283 
284 	if(0 == chain)
285 		die("out of memory allocating chain");
286 
287 	if(!FLAC__metadata_chain_read(chain, filename)) {
288 		print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
289 		ok = false;
290 		goto cleanup;
291 	}
292 
293 	for(i = 0; i < options->ops.num_operations && ok; i++) {
294 		/*
295 		 * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
296 		 * --add-seekpoint and --import-cuesheet-from are used.
297 		 */
298 		if(options->ops.operations[i].type != OP__ADD_SEEKPOINT)
299 			ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
300 
301 		/* The following seems counterintuitive but the meaning
302 		 * of 'use_padding' is 'try to keep the overall metadata
303 		 * to its original size, adding or truncating extra
304 		 * padding if necessary' which is why we need to turn it
305 		 * off in this case.  If we don't, the extra padding block
306 		 * will just be truncated.
307 		 */
308 		if(options->ops.operations[i].type == OP__ADD_PADDING)
309 			use_padding = false;
310 	}
311 
312 	/*
313 	 * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
314 	 * --add-seekpoint and --import-cuesheet-from are used.
315 	 */
316 	for(i = 0; i < options->ops.num_operations && ok; i++) {
317 		if(options->ops.operations[i].type == OP__ADD_SEEKPOINT)
318 			ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
319 	}
320 
321 	if(ok && needs_write) {
322 		if(use_padding)
323 			FLAC__metadata_chain_sort_padding(chain);
324 		ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime);
325 		if(!ok)
326 			print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
327 	}
328 
329   cleanup :
330 	FLAC__metadata_chain_delete(chain);
331 
332 	return ok;
333 }
334 
do_shorthand_operation(const char * filename,FLAC__bool prefix_with_filename,FLAC__Metadata_Chain * chain,const Operation * operation,FLAC__bool * needs_write,FLAC__bool utf8_convert)335 FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert)
336 {
337 	FLAC__bool ok = true;
338 
339 	switch(operation->type) {
340 		case OP__SHOW_MD5SUM:
341 		case OP__SHOW_MIN_BLOCKSIZE:
342 		case OP__SHOW_MAX_BLOCKSIZE:
343 		case OP__SHOW_MIN_FRAMESIZE:
344 		case OP__SHOW_MAX_FRAMESIZE:
345 		case OP__SHOW_SAMPLE_RATE:
346 		case OP__SHOW_CHANNELS:
347 		case OP__SHOW_BPS:
348 		case OP__SHOW_TOTAL_SAMPLES:
349 		case OP__SET_MD5SUM:
350 		case OP__SET_MIN_BLOCKSIZE:
351 		case OP__SET_MAX_BLOCKSIZE:
352 		case OP__SET_MIN_FRAMESIZE:
353 		case OP__SET_MAX_FRAMESIZE:
354 		case OP__SET_SAMPLE_RATE:
355 		case OP__SET_CHANNELS:
356 		case OP__SET_BPS:
357 		case OP__SET_TOTAL_SAMPLES:
358 			ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write);
359 			break;
360 		case OP__SHOW_VC_VENDOR:
361 		case OP__SHOW_VC_FIELD:
362 		case OP__REMOVE_VC_ALL:
363 		case OP__REMOVE_VC_ALL_EXCEPT:
364 		case OP__REMOVE_VC_FIELD:
365 		case OP__REMOVE_VC_FIRSTFIELD:
366 		case OP__SET_VC_FIELD:
367 		case OP__IMPORT_VC_FROM:
368 		case OP__EXPORT_VC_TO:
369 			ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert);
370 			break;
371 		case OP__IMPORT_CUESHEET_FROM:
372 		case OP__EXPORT_CUESHEET_TO:
373 			ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
374 			break;
375 		case OP__IMPORT_PICTURE_FROM:
376 		case OP__EXPORT_PICTURE_TO:
377 			ok = do_shorthand_operation__picture(filename, chain, operation, needs_write);
378 			break;
379 		case OP__ADD_SEEKPOINT:
380 			ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
381 			break;
382 		case OP__ADD_REPLAY_GAIN:
383 		case OP__SCAN_REPLAY_GAIN:
384 			/* these commands are always executed last */
385 			ok = true;
386 			break;
387 		case OP__ADD_PADDING:
388 			ok = do_shorthand_operation__add_padding(filename, chain, operation->argument.add_padding.length, needs_write);
389 			break;
390 		default:
391 			ok = false;
392 			FLAC__ASSERT(0);
393 			break;
394 	};
395 
396 	return ok;
397 }
398 
do_shorthand_operation__add_replay_gain(char ** filenames,unsigned num_files,FLAC__bool preserve_modtime,FLAC__bool scan)399 FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime, FLAC__bool scan)
400 {
401 	FLAC__StreamMetadata streaminfo;
402 	float *title_gains = 0, *title_peaks = 0;
403 	float album_gain, album_peak;
404 	unsigned sample_rate = 0;
405 	unsigned bits_per_sample = 0;
406 	unsigned channels = 0;
407 	unsigned i;
408 	const char *error;
409 	FLAC__bool first = true;
410 
411 	FLAC__ASSERT(num_files > 0);
412 
413 	for(i = 0; i < num_files; i++) {
414 		FLAC__ASSERT(0 != filenames[i]);
415 		if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) {
416 			flac_fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
417 			return false;
418 		}
419 		if(first) {
420 			first = false;
421 			sample_rate = streaminfo.data.stream_info.sample_rate;
422 			bits_per_sample = streaminfo.data.stream_info.bits_per_sample;
423 			channels = streaminfo.data.stream_info.channels;
424 		}
425 		else {
426 			if(sample_rate != streaminfo.data.stream_info.sample_rate) {
427 				flac_fprintf(stderr, "%s: ERROR: sample rate of %u Hz does not match previous files' %u Hz\n", filenames[i], streaminfo.data.stream_info.sample_rate, sample_rate);
428 				return false;
429 			}
430 			if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) {
431 				flac_fprintf(stderr, "%s: ERROR: resolution of %u bps does not match previous files' %u bps\n", filenames[i], streaminfo.data.stream_info.bits_per_sample, bits_per_sample);
432 				return false;
433 			}
434 			if(channels != streaminfo.data.stream_info.channels) {
435 				flac_fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels);
436 				return false;
437 			}
438 		}
439 		if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
440 			flac_fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate);
441 			return false;
442 		}
443 		if(channels != 1 && channels != 2) {
444 			flac_fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels);
445 			return false;
446 		}
447 	}
448 	FLAC__ASSERT(bits_per_sample >= FLAC__MIN_BITS_PER_SAMPLE && bits_per_sample <= FLAC__MAX_BITS_PER_SAMPLE);
449 
450 	if(!grabbag__replaygain_init(sample_rate)) {
451 		FLAC__ASSERT(0);
452 		/* double protection */
453 		flac_fprintf(stderr, "internal error\n");
454 		return false;
455 	}
456 
457 	if(
458 		0 == (title_gains = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files)) ||
459 		0 == (title_peaks = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files))
460 	)
461 		die("out of memory allocating space for title gains/peaks");
462 
463 	for(i = 0; i < num_files; i++) {
464 		if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) {
465 			flac_fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
466 			free(title_gains);
467 			free(title_peaks);
468 			return false;
469 		}
470 	}
471 	grabbag__replaygain_get_album(&album_gain, &album_peak);
472 
473 	for(i = 0; i < num_files; i++) {
474 		if(!scan) {
475 			if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
476 				flac_fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
477 				free(title_gains);
478 				free(title_peaks);
479 				return false;
480 			}
481 		} else {
482 			flac_fprintf(stdout, "%s: %f %f %f %f\n", filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i]);
483 		}
484 	}
485 
486 	free(title_gains);
487 	free(title_peaks);
488 	return true;
489 }
490 
do_shorthand_operation__add_padding(const char * filename,FLAC__Metadata_Chain * chain,unsigned length,FLAC__bool * needs_write)491 FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write)
492 {
493 	FLAC__StreamMetadata *padding = 0;
494 	FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
495 
496 	if(0 == iterator)
497 		die("out of memory allocating iterator");
498 
499 	FLAC__metadata_iterator_init(iterator, chain);
500 
501 	while(FLAC__metadata_iterator_next(iterator))
502 		;
503 
504 	padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
505 	if(0 == padding)
506 		die("out of memory allocating PADDING block");
507 
508 	padding->length = length;
509 
510 	if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) {
511 		print_error_with_chain_status(chain, "%s: ERROR: adding new PADDING block to metadata", filename);
512 		FLAC__metadata_object_delete(padding);
513 		FLAC__metadata_iterator_delete(iterator);
514 		return false;
515 	}
516 
517 	FLAC__metadata_iterator_delete(iterator);
518 	*needs_write = true;
519 	return true;
520 }
521 
passes_filter(const CommandLineOptions * options,const FLAC__StreamMetadata * block,unsigned block_number)522 FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number)
523 {
524 	unsigned i, j;
525 	FLAC__bool matches_number = false, matches_type = false;
526 	FLAC__bool has_block_number_arg = false;
527 
528 	for(i = 0; i < options->args.num_arguments; i++) {
529 		if(options->args.arguments[i].type == ARG__BLOCK_TYPE || options->args.arguments[i].type == ARG__EXCEPT_BLOCK_TYPE) {
530 			for(j = 0; j < options->args.arguments[i].value.block_type.num_entries; j++) {
531 				if(options->args.arguments[i].value.block_type.entries[j].type == block->type) {
532 					if(block->type != FLAC__METADATA_TYPE_APPLICATION || !options->args.arguments[i].value.block_type.entries[j].filter_application_by_id || 0 == memcmp(options->args.arguments[i].value.block_type.entries[j].application_id, block->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
533 						matches_type = true;
534 				}
535 			}
536 		}
537 		else if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) {
538 			has_block_number_arg = true;
539 			for(j = 0; j < options->args.arguments[i].value.block_number.num_entries; j++) {
540 				if(options->args.arguments[i].value.block_number.entries[j] == block_number)
541 					matches_number = true;
542 			}
543 		}
544 	}
545 
546 	if(!has_block_number_arg)
547 		matches_number = true;
548 
549 	if(options->args.checks.has_block_type) {
550 		FLAC__ASSERT(!options->args.checks.has_except_block_type);
551 	}
552 	else if(options->args.checks.has_except_block_type)
553 		matches_type = !matches_type;
554 	else
555 		matches_type = true;
556 
557 	return matches_number && matches_type;
558 }
559 
write_metadata(const char * filename,FLAC__StreamMetadata * block,unsigned block_number,FLAC__bool raw,FLAC__bool hexdump_application)560 void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application)
561 {
562 	unsigned i, j;
563 
564 /*@@@ yuck, should do this with a varargs function or something: */
565 #define PPR if(filename) { if(raw) printf("%s:",filename); else flac_printf("%s:",filename); }
566 	PPR; printf("METADATA block #%u\n", block_number);
567 	PPR; printf("  type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
568 	PPR; printf("  is last: %s\n", block->is_last? "true":"false");
569 	PPR; printf("  length: %u\n", block->length);
570 
571 	switch(block->type) {
572 		case FLAC__METADATA_TYPE_STREAMINFO:
573 			PPR; printf("  minimum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
574 			PPR; printf("  maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize);
575 			PPR; printf("  minimum framesize: %u bytes\n", block->data.stream_info.min_framesize);
576 			PPR; printf("  maximum framesize: %u bytes\n", block->data.stream_info.max_framesize);
577 			PPR; printf("  sample_rate: %u Hz\n", block->data.stream_info.sample_rate);
578 			PPR; printf("  channels: %u\n", block->data.stream_info.channels);
579 			PPR; printf("  bits-per-sample: %u\n", block->data.stream_info.bits_per_sample);
580 			PPR; printf("  total samples: %" PRIu64 "\n", block->data.stream_info.total_samples);
581 			PPR; printf("  MD5 signature: ");
582 			for(i = 0; i < 16; i++) {
583 				printf("%02x", (unsigned)block->data.stream_info.md5sum[i]);
584 			}
585 			printf("\n");
586 			break;
587 		case FLAC__METADATA_TYPE_PADDING:
588 			/* nothing to print */
589 			break;
590 		case FLAC__METADATA_TYPE_APPLICATION:
591 			PPR; printf("  application ID: ");
592 			for(i = 0; i < 4; i++)
593 				printf("%02x", block->data.application.id[i]);
594 			printf("\n");
595 			PPR; printf("  data contents:\n");
596 			if(0 != block->data.application.data) {
597 				if(hexdump_application)
598 					hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, "    ");
599 				else
600 					(void) local_fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
601 			}
602 			break;
603 		case FLAC__METADATA_TYPE_SEEKTABLE:
604 			PPR; printf("  seek points: %u\n", block->data.seek_table.num_points);
605 			for(i = 0; i < block->data.seek_table.num_points; i++) {
606 				if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
607 					PPR; printf("    point %u: sample_number=%" PRIu64 ", stream_offset=%" PRIu64 ", frame_samples=%u\n", i, block->data.seek_table.points[i].sample_number, block->data.seek_table.points[i].stream_offset, block->data.seek_table.points[i].frame_samples);
608 				}
609 				else {
610 					PPR; printf("    point %u: PLACEHOLDER\n", i);
611 				}
612 			}
613 			break;
614 		case FLAC__METADATA_TYPE_VORBIS_COMMENT:
615 			PPR; printf("  vendor string: ");
616 			write_vc_field(0, &block->data.vorbis_comment.vendor_string, raw, stdout);
617 			PPR; printf("  comments: %u\n", block->data.vorbis_comment.num_comments);
618 			for(i = 0; i < block->data.vorbis_comment.num_comments; i++) {
619 				PPR; printf("    comment[%u]: ", i);
620 				write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout);
621 			}
622 			break;
623 		case FLAC__METADATA_TYPE_CUESHEET:
624 			PPR; printf("  media catalog number: %s\n", block->data.cue_sheet.media_catalog_number);
625 			PPR; printf("  lead-in: %" PRIu64 "\n", block->data.cue_sheet.lead_in);
626 			PPR; printf("  is CD: %s\n", block->data.cue_sheet.is_cd? "true":"false");
627 			PPR; printf("  number of tracks: %u\n", block->data.cue_sheet.num_tracks);
628 			for(i = 0; i < block->data.cue_sheet.num_tracks; i++) {
629 				const FLAC__StreamMetadata_CueSheet_Track *track = block->data.cue_sheet.tracks+i;
630 				const FLAC__bool is_last = (i == block->data.cue_sheet.num_tracks-1);
631 				const FLAC__bool is_leadout = is_last && track->num_indices == 0;
632 				PPR; printf("    track[%u]\n", i);
633 				PPR; printf("      offset: %" PRIu64 "\n", track->offset);
634 				if(is_last) {
635 					PPR; printf("      number: %u (%s)\n", (unsigned)track->number, is_leadout? "LEAD-OUT" : "INVALID");
636 				}
637 				else {
638 					PPR; printf("      number: %u\n", (unsigned)track->number);
639 				}
640 				if(!is_leadout) {
641 					PPR; printf("      ISRC: %s\n", track->isrc);
642 					PPR; printf("      type: %s\n", track->type == 1? "DATA" : "AUDIO");
643 					PPR; printf("      pre-emphasis: %s\n", track->pre_emphasis? "true":"false");
644 					PPR; printf("      number of index points: %u\n", track->num_indices);
645 					for(j = 0; j < track->num_indices; j++) {
646 						const FLAC__StreamMetadata_CueSheet_Index *indx = track->indices+j;
647 						PPR; printf("        index[%u]\n", j);
648 						PPR; printf("          offset: %" PRIu64 "\n", indx->offset);
649 						PPR; printf("          number: %u\n", (unsigned)indx->number);
650 					}
651 				}
652 			}
653 			break;
654 		case FLAC__METADATA_TYPE_PICTURE:
655 			PPR; printf("  type: %u (%s)\n", block->data.picture.type, block->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED? FLAC__StreamMetadata_Picture_TypeString[block->data.picture.type] : "UNDEFINED");
656 			PPR; printf("  MIME type: %s\n", block->data.picture.mime_type);
657 			PPR; printf("  description: %s\n", block->data.picture.description);
658 			PPR; printf("  width: %u\n", (unsigned)block->data.picture.width);
659 			PPR; printf("  height: %u\n", (unsigned)block->data.picture.height);
660 			PPR; printf("  depth: %u\n", (unsigned)block->data.picture.depth);
661 			PPR; printf("  colors: %u%s\n", (unsigned)block->data.picture.colors, block->data.picture.colors? "" : " (unindexed)");
662 			PPR; printf("  data length: %u\n", (unsigned)block->data.picture.data_length);
663 			PPR; printf("  data:\n");
664 			if(0 != block->data.picture.data)
665 				hexdump(filename, block->data.picture.data, block->data.picture.data_length, "    ");
666 			break;
667 		default:
668 			PPR; printf("  data contents:\n");
669 			if(0 != block->data.unknown.data)
670 				hexdump(filename, block->data.unknown.data, block->length, "    ");
671 			break;
672 	}
673 #undef PPR
674 }
675