Refactor handling of input files
[xapian.git] / xapian-applications / omega / md5wrap.cc
bloba3cd9fba5de84c94fe3b167ed21c5a1a714fc659
1 /** @file md5wrap.cc
2 * @brief wrapper functions to allow easy use of MD5 from C++.
3 */
4 /* Copyright (C) 2006,2010,2012,2013,2015,2018 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
23 #include "md5wrap.h"
25 #include <cerrno>
26 #include <string>
28 #include "safeunistd.h"
30 #include "md5.h"
32 using namespace std;
34 bool
35 md5_fd(int fd, string& md5)
37 MD5Context md5_ctx;
38 MD5Init(&md5_ctx);
40 unsigned char blk[4096];
42 while (true) {
43 int c = read(fd, blk, sizeof(blk));
44 if (c == 0) break;
45 if (c < 0) {
46 if (errno == EINTR) continue;
47 return false;
49 MD5Update(&md5_ctx, blk, c);
52 MD5Final(blk, &md5_ctx);
53 md5.assign(reinterpret_cast<const char*>(blk), 16);
55 return true;
58 void
59 md5_block(const char* p, size_t len, string& md5)
61 unsigned char blk[16];
62 MD5Context md5_ctx;
64 MD5Init(&md5_ctx);
65 MD5Update(&md5_ctx, reinterpret_cast<const unsigned char*>(p), len);
66 MD5Final(blk, &md5_ctx);
67 md5.assign(reinterpret_cast<const char*>(blk), 16);