1 /* flacdiff - Displays where two FLAC streams differ
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 <string.h>
26 #include "FLAC++/decoder.h"
27 #include "share/compat.h"
28 
29 #ifdef _MSC_VER
30 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
31 #pragma warning ( disable : 4800 )
32 #endif
33 
34 class AutoFILE {
35 protected:
36 	::FILE *f_;
37 public:
AutoFILE(const char * path,const char * mode)38 	inline AutoFILE(const char *path, const char *mode): f_(::fopen(path, mode)) { }
~AutoFILE()39 	inline virtual ~AutoFILE() { if (f_) (void)::fclose(f_); }
40 
operator bool() const41 	inline operator bool() const { return 0 != f_; }
operator const::FILE*() const42 	inline operator const ::FILE *() const { return f_; }
operator ::FILE*()43 	inline operator ::FILE *() { return f_; }
44 private:
45 	AutoFILE();
46 	AutoFILE(const AutoFILE &);
47 	void operator=(const AutoFILE &);
48 };
49 
50 class Decoder: public FLAC::Decoder::Stream {
51 public:
Decoder(AutoFILE & f,FLAC__off_t tgt)52 	Decoder(AutoFILE &f, FLAC__off_t tgt): tgtpos_((FLAC__uint64)tgt), curpos_(0), go_(true), err_(false), frame_(), f_(f) { memset(&frame_, 0, sizeof(::FLAC__Frame)); }
53 	FLAC__uint64 tgtpos_, curpos_;
54 	bool go_, err_;
55 	::FLAC__Frame frame_;
56 protected:
57 	AutoFILE &f_;
58 	// from FLAC::Decoder::Stream
read_callback(FLAC__byte buffer[],size_t * bytes)59 	virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes)
60 	{
61 		*bytes = fread(buffer, 1, *bytes, f_);
62 		if(ferror((FILE*)f_))
63 			return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
64 		else if(*bytes == 0 && feof((FILE*)f_))
65 			return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
66 		else
67 			return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
68 	}
69 
tell_callback(FLAC__uint64 * absolute_byte_offset)70 	virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset)
71 	{
72 		FLAC__off_t off = ftello(f_);
73 		if(off < 0)
74 			return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
75 		*absolute_byte_offset = off;
76 		return ::FLAC__STREAM_DECODER_TELL_STATUS_OK;
77 	}
78 
eof_callback()79 	virtual bool eof_callback()
80 	{
81 		return (bool)feof((FILE*)f_);
82 	}
83 
write_callback(const::FLAC__Frame * frame,const FLAC__int32 * const[])84 	virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const /*buffer*/[])
85 	{
86 		FLAC__uint64 pos;
87 		if(!get_decode_position(&pos)) {
88 			go_ = false;
89 			err_ = true;
90 			return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
91 		}
92 		if(pos > tgtpos_) {
93 			go_ = false;
94 			frame_ = *frame;
95 		}
96 		else
97 			curpos_ = pos;
98 		return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
99 	}
100 
error_callback(::FLAC__StreamDecoderErrorStatus status)101 	virtual void error_callback(::FLAC__StreamDecoderErrorStatus status)
102 	{
103 		fprintf(stderr, "got error %d:%s\n", status, ::FLAC__StreamDecoderErrorStatusString[status]);
104 		go_ = false;
105 		err_ = true;
106 	}
107 };
108 
show_diff(AutoFILE & f1,AutoFILE & f2,FLAC__off_t off)109 static bool show_diff(AutoFILE &f1, AutoFILE &f2, FLAC__off_t off)
110 {
111 	Decoder d1(f1, off), d2(f2, off);
112 	if(!d1) {
113 		fprintf(stderr, "ERROR: setting up decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
114 		return false;
115 	}
116 	if(!d2) {
117 		fprintf(stderr, "ERROR: setting up decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
118 		return false;
119 	}
120 	::FLAC__StreamDecoderInitStatus is;
121 	if((is = d1.init()) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
122 		fprintf(stderr, "ERROR: initializing decoder1, status=%s state=%s\n", FLAC__StreamDecoderInitStatusString[is], d1.get_state().resolved_as_cstring(d1));
123 		return false;
124 	}
125 	if((is = d2.init()) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
126 		fprintf(stderr, "ERROR: initializing decoder2, status=%s state=%s\n", FLAC__StreamDecoderInitStatusString[is], d2.get_state().resolved_as_cstring(d2));
127 		return false;
128 	}
129 	if(!d1.process_until_end_of_metadata()) {
130 		fprintf(stderr, "ERROR: skipping metadata in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
131 		return false;
132 	}
133 	if(!d2.process_until_end_of_metadata()) {
134 		fprintf(stderr, "ERROR: skipping metadata in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
135 		return false;
136 	}
137 	while(d1.go_ && d2.go_) {
138 		if(!d1.process_single()) {
139 			fprintf(stderr, "ERROR: decoding frame in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
140 			return false;
141 		}
142 		if(!d2.process_single()) {
143 			fprintf(stderr, "ERROR: decoding frame in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
144 			return false;
145 		}
146 	}
147 	if(d1.err_) {
148 		fprintf(stderr, "ERROR: got err_ in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
149 		return false;
150 	}
151 	if(d2.err_) {
152 		fprintf(stderr, "ERROR: got err_ in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
153 		return false;
154 	}
155 	if(d1.go_ != d2.go_) {
156 		fprintf(stderr, "ERROR: d1.go_(%s) != d2.go_(%s)\n", d1.go_?"true":"false", d2.go_?"true":"false");
157 		return false;
158 	}
159 	fprintf(stdout, "pos1 = %" PRIu64 "  blocksize=%u sample#%" PRIu64 " frame#%" PRIu64 "\n", d1.curpos_, d1.frame_.header.blocksize, d1.frame_.header.number.sample_number, d1.frame_.header.number.sample_number / d1.frame_.header.blocksize);
160 	fprintf(stdout, "pos2 = %" PRIu64 "  blocksize=%u sample#%" PRIu64 " frame#%" PRIu64 "\n", d2.curpos_, d2.frame_.header.blocksize, d2.frame_.header.number.sample_number, d2.frame_.header.number.sample_number / d2.frame_.header.blocksize);
161 
162 	return true;
163 }
164 
get_diff_offset(AutoFILE & f1,AutoFILE & f2)165 static FLAC__off_t get_diff_offset(AutoFILE &f1, AutoFILE &f2)
166 {
167 	FLAC__off_t off = 0;
168 	while(1) {
169 		if(feof((FILE*)f1) && feof((FILE*)f2)) {
170 			fprintf(stderr, "ERROR: files are identical\n");
171 			return -1;
172 		}
173 		if(feof((FILE*)f1)) {
174 			fprintf(stderr, "ERROR: file1 EOF\n");
175 			return -1;
176 		}
177 		if(feof((FILE*)f2)) {
178 			fprintf(stderr, "ERROR: file2 EOF\n");
179 			return -1;
180 		}
181 		if(fgetc(f1) != fgetc(f2))
182 			return off;
183 		off++;
184 	}
185 }
186 
run(const char * fn1,const char * fn2)187 static bool run(const char *fn1, const char *fn2)
188 {
189 	FLAC__off_t off;
190 	AutoFILE f1(fn1, "rb"), f2(fn2, "rb");
191 
192 	if(!f1) {
193 		flac_fprintf(stderr, "ERROR: opening %s for reading\n", fn1);
194 		return false;
195 	}
196 	if(!f2) {
197 		flac_fprintf(stderr, "ERROR: opening %s for reading\n", fn2);
198 		return false;
199 	}
200 
201 	if((off = get_diff_offset(f1, f2)) < 0)
202 		return false;
203 
204 	fprintf(stdout, "got diff offset = %" PRId64 "\n", off);
205 
206 	return show_diff(f1, f2, off);
207 }
208 
main(int argc,char * argv[])209 int main(int argc, char *argv[])
210 {
211 	const char *usage = "usage: flacdiff flacfile1 flacfile2\n";
212 
213 #ifdef _WIN32
214 	if (get_utf8_argv(&argc, &argv) != 0) {
215 		fprintf(stderr, "ERROR: failed to convert command line parameters to UTF-8\n");
216 		return 1;
217 	}
218 #endif
219 
220 	if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
221 		printf(usage);
222 		return 0;
223 	}
224 	else if(argc != 3) {
225 		fprintf(stderr, usage);
226 		return 255;
227 	}
228 
229 	return run(argv[1], argv[2])? 0 : 1;
230 }
231