1 /* iffscan - Simple AIFF/RIFF chunk scanner
2  * Copyright (C) 2007-2009  Josh Coalson
3  * Copyright (C) 2011-2016  Xiph.Org Foundation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "share/compat.h"
28 #include "foreign_metadata.h"
29 
unpack32be_(const FLAC__byte * b)30 static FLAC__uint32 unpack32be_(const FLAC__byte *b)
31 {
32 	return ((FLAC__uint32)b[0]<<24) + ((FLAC__uint32)b[1]<<16) + ((FLAC__uint32)b[2]<<8) + (FLAC__uint32)b[3];
33 }
34 
unpack32le_(const FLAC__byte * b)35 static FLAC__uint32 unpack32le_(const FLAC__byte *b)
36 {
37 	return (FLAC__uint32)b[0] + ((FLAC__uint32)b[1]<<8) + ((FLAC__uint32)b[2]<<16) + ((FLAC__uint32)b[3]<<24);
38 }
39 
unpack64le_(const FLAC__byte * b)40 static FLAC__uint64 unpack64le_(const FLAC__byte *b)
41 {
42 	return (FLAC__uint64)b[0] + ((FLAC__uint64)b[1]<<8) + ((FLAC__uint64)b[2]<<16) + ((FLAC__uint64)b[3]<<24) + ((FLAC__uint64)b[4]<<32) + ((FLAC__uint64)b[5]<<40) + ((FLAC__uint64)b[6]<<48) + ((FLAC__uint64)b[7]<<56);
43 }
44 
unpack32_(const FLAC__byte * b,foreign_block_type_t type)45 static FLAC__uint32 unpack32_(const FLAC__byte *b, foreign_block_type_t type)
46 {
47 	if(type == FOREIGN_BLOCK_TYPE__AIFF)
48 		return unpack32be_(b);
49 	else
50 		return unpack32le_(b);
51 }
52 
main(int argc,char * argv[])53 int main(int argc, char *argv[])
54 {
55 	FILE *f;
56 	char buf[36];
57 	foreign_metadata_t *fm;
58 	const char *fn, *error;
59 	size_t i;
60 	FLAC__uint32 size;
61 
62 #ifdef _WIN32
63 	if (get_utf8_argv(&argc, &argv) != 0) {
64 		fprintf(stderr, "ERROR: failed to convert command line parameters to UTF-8\n");
65 		return 1;
66 	}
67 #endif
68 
69 	if(argc != 2) {
70 		flac_fprintf(stderr, "usage: %s { file.wav | file.aif }\n", argv[0]);
71 		return 1;
72 	}
73 	fn = argv[1];
74 	if(0 == (f = flac_fopen(fn, "rb")) || fread(buf, 1, 4, f) != 4) {
75 		flac_fprintf(stderr, "ERROR opening %s for reading\n", fn);
76 		return 1;
77 	}
78 	fclose(f);
79 	if(0 == (fm = flac__foreign_metadata_new(memcmp(buf, "RIFF", 4) && memcmp(buf, "RF64", 4)? FOREIGN_BLOCK_TYPE__AIFF : FOREIGN_BLOCK_TYPE__RIFF))) {
80 		flac_fprintf(stderr, "ERROR: out of memory\n");
81 		return 1;
82 	}
83 	if(fm->type == FOREIGN_BLOCK_TYPE__AIFF) {
84 		if(!flac__foreign_metadata_read_from_aiff(fm, fn, &error)) {
85 			flac_fprintf(stderr, "ERROR reading chunks from %s: %s\n", fn, error);
86 			return 1;
87 		}
88 	}
89 	else {
90 		if(!flac__foreign_metadata_read_from_wave(fm, fn, &error)) {
91 			flac_fprintf(stderr, "ERROR reading chunks from %s: %s\n", fn, error);
92 			return 1;
93 		}
94 	}
95 	if(0 == (f = flac_fopen(fn, "rb"))) {
96 		flac_fprintf(stderr, "ERROR opening %s for reading\n", fn);
97 		return 1;
98 	}
99 	for(i = 0; i < fm->num_blocks; i++) {
100 		if(fseeko(f, fm->blocks[i].offset, SEEK_SET) < 0) {
101 			flac_fprintf(stderr, "ERROR seeking in %s\n", fn);
102 			return 1;
103 		}
104 		if(fread(buf, 1, i==0?12:8, f) != (i==0?12:8)) {
105 			flac_fprintf(stderr, "ERROR reading %s\n", fn);
106 			return 1;
107 		}
108 		size = unpack32_((FLAC__byte*)buf+4, fm->type);
109 		printf("block:[%c%c%c%c] size=%08x=(%10u)", buf[0], buf[1], buf[2], buf[3], size, size);
110 		if(i == 0)
111 			printf(" type:[%c%c%c%c]", buf[8], buf[9], buf[10], buf[11]);
112 		else if(fm->type == FOREIGN_BLOCK_TYPE__AIFF && i == fm->audio_block)
113 			printf(" offset size=%08x=(%10u)", fm->ssnd_offset_size, fm->ssnd_offset_size);
114 		else if(fm->type == FOREIGN_BLOCK_TYPE__RIFF && i == 1 && !memcmp(buf, "ds64", 4)) {
115 			if(fread(buf+8, 1, 36-8, f) != 36-8) {
116 				flac_fprintf(stderr, "ERROR reading %s\n", fn);
117 				return 1;
118 			}
119 			printf("\n    RIFF size=%016" PRIx64 "=(%20" PRIu64 ")", unpack64le_((FLAC__byte*)buf+8), unpack64le_((FLAC__byte*)buf+8));
120 			printf("\n    data size=%016" PRIx64 "=(%20" PRIu64 ")", unpack64le_((FLAC__byte*)buf+16), unpack64le_((FLAC__byte*)buf+16));
121 			printf("\n    sample count=%016" PRIx64 "=(%20" PRIu64 ")", unpack64le_((FLAC__byte*)buf+24), unpack64le_((FLAC__byte*)buf+24));
122 			printf("\n    table size=%08x=(%10u)", unpack32le_((FLAC__byte*)buf+32), unpack32le_((FLAC__byte*)buf+32));
123 		}
124 		printf("\n");
125 	}
126 	fclose(f);
127 	flac__foreign_metadata_delete(fm);
128 	return 0;
129 }
130