summaryrefslogtreecommitdiff
path: root/library/binary.cpp
blob: 409b024a1be0cd10473b5f50b2dc920cefb27def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#include "binary.h"
#include <boost/scoped_array.hpp>
#include "../shared/fileIO.h"


bool FreeFileSync::filesHaveSameContent(const Zstring& filename1, const Zstring& filename2, CompareCallback& callback)
{
    const size_t BUFFER_SIZE = 512 * 1024; //512 kb seems to be the perfect buffer size
    static boost::scoped_array<unsigned char> memory1(new unsigned char[BUFFER_SIZE]);
    static boost::scoped_array<unsigned char> memory2(new unsigned char[BUFFER_SIZE]);

    FileInput file1(filename1); //throw FileError()
    FileInput file2(filename2); //throw FileError()

    wxLongLong bytesCompared;
    do
    {
        const size_t length1 = file1.read(memory1.get(), BUFFER_SIZE); //returns actual number of bytes read; throw FileError()
        const size_t length2 = file2.read(memory2.get(), BUFFER_SIZE); //

        if (length1 != length2 || ::memcmp(memory1.get(), memory2.get(), length1) != 0)
            return false;

        bytesCompared += length1 * 2;

        //send progress updates
        callback.updateCompareStatus(bytesCompared);
    }
    while (!file1.eof());

    if (!file1.eof()) //highly unlikely, but theoretically possible! (but then again, not in this context where both files have same size...)
        return false;

    return true;
}
bgstack15