Fix a couple of Windows 2Gig file size issues.
[flac.git] / src / utils / flacdiff / main.cpp
blobd4c8f0666d2ce45eee2db487cd80e6ac2033fb4a
1 /* flacdiff - Displays where two FLAC streams differ
2 * Copyright (C) 2007,2008,2009 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <stdio.h>
24 #include <string.h>
25 #include "FLAC++/decoder.h"
26 #include "share/compat.h"
28 #ifdef _MSC_VER
29 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
30 #pragma warning ( disable : 4800 )
31 #endif
33 class AutoFILE {
34 protected:
35 ::FILE *f_;
36 public:
37 inline AutoFILE(const char *path, const char *mode): f_(::fopen(path, mode)) { }
38 inline virtual ~AutoFILE() { if (f_) (void)::fclose(f_); }
40 inline operator bool() const { return 0 != f_; }
41 inline operator const ::FILE *() const { return f_; }
42 inline operator ::FILE *() { return f_; }
43 private:
44 AutoFILE();
45 AutoFILE(const AutoFILE &);
46 void operator=(const AutoFILE &);
49 class Decoder: public FLAC::Decoder::Stream {
50 public:
51 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)); }
52 FLAC__uint64 tgtpos_, curpos_;
53 bool go_, err_;
54 ::FLAC__Frame frame_;
55 protected:
56 AutoFILE &f_;
57 // from FLAC::Decoder::Stream
58 virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes)
60 *bytes = fread(buffer, 1, *bytes, f_);
61 if(ferror((FILE*)f_))
62 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
63 else if(*bytes == 0 && feof((FILE*)f_))
64 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
65 else
66 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
69 virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset)
71 FLAC__off_t off = ftello(f_);
72 if(off < 0)
73 return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
74 *absolute_byte_offset = off;
75 return ::FLAC__STREAM_DECODER_TELL_STATUS_OK;
78 virtual bool eof_callback()
80 return (bool)feof((FILE*)f_);
83 virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const /*buffer*/[])
85 FLAC__uint64 pos;
86 if(!get_decode_position(&pos)) {
87 go_ = false;
88 err_ = true;
89 return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
91 if(pos > tgtpos_) {
92 go_ = false;
93 frame_ = *frame;
95 else
96 curpos_ = pos;
97 return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
100 virtual void error_callback(::FLAC__StreamDecoderErrorStatus status)
102 fprintf(stderr, "got error %d:%s\n", status, ::FLAC__StreamDecoderErrorStatusString[status]);
103 go_ = false;
104 err_ = true;
108 static bool show_diff(AutoFILE &f1, AutoFILE &f2, FLAC__off_t off)
110 Decoder d1(f1, off), d2(f2, off);
111 if(!d1) {
112 fprintf(stderr, "ERROR: setting up decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
113 return false;
115 if(!d2) {
116 fprintf(stderr, "ERROR: setting up decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
117 return false;
119 ::FLAC__StreamDecoderInitStatus is;
120 if((is = d1.init()) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
121 fprintf(stderr, "ERROR: initializing decoder1, status=%s state=%s\n", FLAC__StreamDecoderInitStatusString[is], d1.get_state().resolved_as_cstring(d1));
122 return false;
124 if((is = d2.init()) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
125 fprintf(stderr, "ERROR: initializing decoder2, status=%s state=%s\n", FLAC__StreamDecoderInitStatusString[is], d2.get_state().resolved_as_cstring(d2));
126 return false;
128 if(!d1.process_until_end_of_metadata()) {
129 fprintf(stderr, "ERROR: skipping metadata in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
130 return false;
132 if(!d2.process_until_end_of_metadata()) {
133 fprintf(stderr, "ERROR: skipping metadata in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
134 return false;
136 while(d1.go_ && d2.go_) {
137 if(!d1.process_single()) {
138 fprintf(stderr, "ERROR: decoding frame in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
139 return false;
141 if(!d2.process_single()) {
142 fprintf(stderr, "ERROR: decoding frame in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
143 return false;
146 if(d1.err_) {
147 fprintf(stderr, "ERROR: got err_ in decoder1, state=%s\n", d1.get_state().resolved_as_cstring(d1));
148 return false;
150 if(d2.err_) {
151 fprintf(stderr, "ERROR: got err_ in decoder2, state=%s\n", d2.get_state().resolved_as_cstring(d2));
152 return false;
154 if(d1.go_ != d2.go_) {
155 fprintf(stderr, "ERROR: d1.go_(%s) != d2.go_(%s)\n", d1.go_?"true":"false", d2.go_?"true":"false");
156 return false;
158 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);
159 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 return true;
164 static FLAC__off_t get_diff_offset(AutoFILE &f1, AutoFILE &f2)
166 FLAC__off_t off = 0;
167 while(1) {
168 if(feof((FILE*)f1) && feof((FILE*)f1)) {
169 fprintf(stderr, "ERROR: files are identical\n");
170 return -1;
172 if(feof((FILE*)f1)) {
173 fprintf(stderr, "ERROR: file1 EOF\n");
174 return -1;
176 if(feof((FILE*)f2)) {
177 fprintf(stderr, "ERROR: file2 EOF\n");
178 return -1;
180 if(fgetc(f1) != fgetc(f2))
181 return off;
182 off++;
186 static bool run(const char *fn1, const char *fn2)
188 FLAC__off_t off;
189 AutoFILE f1(fn1, "rb"), f2(fn2, "rb");
191 if(!f1) {
192 fprintf(stderr, "ERROR: opening %s for reading\n", fn1);
193 return false;
195 if(!f2) {
196 fprintf(stderr, "ERROR: opening %s for reading\n", fn2);
197 return false;
200 if((off = get_diff_offset(f1, f2)) < 0)
201 return false;
203 fprintf(stdout, "got diff offset = %" PRId64 "\n", off);
205 return show_diff(f1, f2, off);
208 int main(int argc, char *argv[])
210 const char *usage = "usage: flacdiff flacfile1 flacfile2\n";
212 if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
213 printf(usage);
214 return 0;
216 else if(argc != 3) {
217 fprintf(stderr, usage);
218 return 255;
221 return run(argv[1], argv[2])? 0 : 1;