]> git.lyx.org Git - lyx.git/blob - src/support/checksum.cpp
Fix bug #12772
[lyx.git] / src / support / checksum.cpp
1 // -*- C++ -*-
2 /**
3  * \file checksum.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Yuriy Skalko
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13 #include "support/checksum.h"
14
15 #include <zlib.h>
16
17 namespace lyx {
18
19 namespace support {
20
21 unsigned long checksum(std::string const & s)
22 {
23         auto p = reinterpret_cast<unsigned char const *>(s.c_str());
24         return crc32(0, p, s.size());
25 }
26
27 unsigned long checksum(std::ifstream & ifs)
28 {
29         std::istreambuf_iterator<char> beg(ifs);
30         std::istreambuf_iterator<char> end;
31
32         unsigned long sum = 0;
33         for (auto & it = beg; beg != end; ++it) {
34                 unsigned char c = *it;
35                 sum = crc32(sum, &c, 1);
36         }
37         return sum;
38 }
39
40 unsigned long checksum(unsigned char const * beg, unsigned char const * end)
41 {
42         return crc32(0, beg, end - beg);
43 }
44
45 } // namespace support
46
47 } // namespace lyx