1 /* Copyright (C) 2015 J.F.Dockes
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the
14 * Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17 #include "autoconfig.h"
18
19 #include <stdio.h>
20 #include <string.h>
21
22 #include "md5ut.h"
23 #include "readfile.h"
24
25 using namespace std;
26
27 // Quite incredibly if this class is named FileScanMd5 like the
28 // different one in readfile.cpp, the vtables get mixed up and mh_xslt
29 // crashes while calling a virtual function (gcc 6.3 and 7.3)
30 class FileScanMd5loc : public FileScanDo {
31 public:
FileScanMd5loc(string & d)32 FileScanMd5loc(string& d) : digest(d) {}
init(int64_t,string *)33 virtual bool init(int64_t, string *)
34 {
35 MD5Init(&ctx);
36 return true;
37 }
data(const char * buf,int cnt,string *)38 virtual bool data(const char *buf, int cnt, string*)
39 {
40 MD5Update(&ctx, (const unsigned char*)buf, cnt);
41 return true;
42 }
43 string &digest;
44 MD5_CTX ctx;
45 };
46
MD5File(const string & filename,string & digest,string * reason)47 bool MD5File(const string& filename, string &digest, string *reason)
48 {
49 FileScanMd5loc md5er(digest);
50 if (!file_scan(filename, &md5er, reason))
51 return false;
52 // We happen to know that digest and md5er.digest are the same object
53 MD5Final(md5er.digest, &md5er.ctx);
54 return true;
55 }
56