From 1d62b50c7e1e85663ba618e2274629d3f2c51c23 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Tue, 6 Nov 2018 09:46:05 +0100 Subject: [PATCH] rearrange md5 code for sharing. update shared smallut --- src/utils/md5.cpp | 59 +++++++++++++++++++++-- src/utils/md5.h | 9 +++- src/utils/md5ut.cpp | 108 ++---------------------------------------- src/utils/md5ut.h | 14 ++---- src/utils/smallut.cpp | 15 +++--- src/utils/smallut.h | 5 +- 6 files changed, 80 insertions(+), 130 deletions(-) diff --git a/src/utils/md5.cpp b/src/utils/md5.cpp index ccfd797a..8ff37027 100644 --- a/src/utils/md5.cpp +++ b/src/utils/md5.cpp @@ -17,9 +17,6 @@ * will fill a supplied 16-byte array with the digest. */ -#include "autoconfig.h" -//#include - #include "md5.h" #include @@ -47,6 +44,9 @@ static uint8_t PADDING[MD5_BLOCK_LENGTH] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +static void MD5Pad(MD5_CTX *); +static void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]); + /* * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious * initialization constants. @@ -103,7 +103,7 @@ MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len) * Pad pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ -void +static void MD5Pad(MD5_CTX *ctx) { uint8_t count[8]; @@ -155,7 +155,7 @@ MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx) * reflect the addition of 16 longwords of new data. MD5Update blocks * the data and converts bytes into longwords for this routine. */ -void +static void MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH]) { uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4]; @@ -250,3 +250,52 @@ MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH]) state[2] += c; state[3] += d; } + + +// C++ utilities +using std::string; + +void MD5Final(string &digest, MD5_CTX *context) +{ + unsigned char d[16]; + MD5Final (d, context); + digest.assign((const char *)d, 16); +} + +string& MD5String(const string& data, string& digest) +{ + MD5_CTX ctx; + MD5Init(&ctx); + MD5Update(&ctx, (const unsigned char*)data.c_str(), data.length()); + MD5Final(digest, &ctx); + return digest; +} + +string& MD5HexPrint(const string& digest, string &out) +{ + out.erase(); + out.reserve(33); + static const char hex[]="0123456789abcdef"; + const unsigned char *hash = (const unsigned char *)digest.c_str(); + for (int i = 0; i < 16; i++) { + out.append(1, hex[hash[i] >> 4]); + out.append(1, hex[hash[i] & 0x0f]); + } + return out; +} +string& MD5HexScan(const string& xdigest, string& digest) +{ + digest.erase(); + if (xdigest.length() != 32) { + return digest; + } + for (unsigned int i = 0; i < 16; i++) { + unsigned int val; + if (sscanf(xdigest.c_str() + 2*i, "%2x", &val) != 1) { + digest.erase(); + return digest; + } + digest.append(1, (unsigned char)val); + } + return digest; +} diff --git a/src/utils/md5.h b/src/utils/md5.h index fcd66036..d03702e0 100644 --- a/src/utils/md5.h +++ b/src/utils/md5.h @@ -30,8 +30,13 @@ typedef struct MD5Context { void MD5Init(MD5_CTX *); void MD5Update(MD5_CTX *, const uint8_t *, size_t); -void MD5Pad(MD5_CTX *); void MD5Final(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *); -void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]); + +/** md5 c++ utility wrappers */ +#include +extern void MD5Final(std::string& digest, MD5_CTX *); +extern std::string& MD5String(const std::string& data, std::string& digest); +extern std::string& MD5HexPrint(const std::string& digest, std::string& xdigest); +extern std::string& MD5HexScan(const std::string& xdigest, std::string& digest); #endif /* _MD5_H_ */ diff --git a/src/utils/md5ut.cpp b/src/utils/md5ut.cpp index 6966080c..de8a16d7 100644 --- a/src/utils/md5ut.cpp +++ b/src/utils/md5ut.cpp @@ -14,8 +14,6 @@ * Free Software Foundation, Inc., * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#ifndef TEST_MD5 #include "autoconfig.h" #include @@ -23,54 +21,10 @@ #include "md5ut.h" -/* Convenience utilities */ - -void MD5Final(string &digest, MD5_CTX *context) -{ - unsigned char d[16]; - MD5Final (d, context); - digest.assign((const char *)d, 16); -} - -string& MD5String(const string& data, string& digest) -{ - MD5_CTX ctx; - MD5Init(&ctx); - MD5Update(&ctx, (const unsigned char*)data.c_str(), data.length()); - MD5Final(digest, &ctx); - return digest; -} - -string& MD5HexPrint(const string& digest, string &out) -{ - out.erase(); - out.reserve(33); - static const char hex[]="0123456789abcdef"; - const unsigned char *hash = (const unsigned char *)digest.c_str(); - for (int i = 0; i < 16; i++) { - out.append(1, hex[hash[i] >> 4]); - out.append(1, hex[hash[i] & 0x0f]); - } - return out; -} -string& MD5HexScan(const string& xdigest, string& digest) -{ - digest.erase(); - if (xdigest.length() != 32) { - return digest; - } - for (unsigned int i = 0; i < 16; i++) { - unsigned int val; - if (sscanf(xdigest.c_str() + 2*i, "%2x", &val) != 1) { - digest.erase(); - return digest; - } - digest.append(1, (unsigned char)val); - } - return digest; -} - #include "readfile.h" + +using namespace std; + class FileScanMd5 : public FileScanDo { public: FileScanMd5(string& d) : digest(d) {} @@ -87,6 +41,7 @@ public: string &digest; MD5_CTX ctx; }; + bool MD5File(const string& filename, string &digest, string *reason) { FileScanMd5 md5er(digest); @@ -96,58 +51,3 @@ bool MD5File(const string& filename, string &digest, string *reason) MD5Final(md5er.digest, &md5er.ctx); return true; } - -#else // TEST_MD5 - -// Test driver -#include -#include - -#include -#include -#include "md5ut.h" - -using namespace std; - -static const char *thisprog; -static char usage [] = -"trmd5 filename\n\n" -; -static void -Usage(void) -{ - fprintf(stderr, "%s: usage:\n%s", thisprog, usage); - exit(1); -} - -int main(int argc, const char **argv) -{ - thisprog = argv[0]; - argc--; argv++; - - if (argc != 1) - Usage(); - string filename = *argv++;argc--; - - string reason, digest; - if (!MD5File(filename, digest, &reason)) { - cerr << reason << endl; - exit(1); - } else { - string hex; - cout << "MD5 (" << filename << ") = " << MD5HexPrint(digest, hex) << endl; - - string digest1; - MD5HexScan(hex, digest1); - if (digest1.compare(digest)) { - cout << "MD5HexScan Failure" << endl; - cout << MD5HexPrint(digest, hex) << " " << digest.length() << " -> " - << MD5HexPrint(digest1, hex) << " " << digest1.length() << endl; - exit(1); - } - - } - exit(0); -} - -#endif diff --git a/src/utils/md5ut.h b/src/utils/md5ut.h index f1e5c81f..e625830e 100644 --- a/src/utils/md5ut.h +++ b/src/utils/md5ut.h @@ -17,17 +17,11 @@ #ifndef _MD5UT_H_ #define _MD5UT_H_ -#include - #include "md5.h" -/** md5 utility wrappers */ -#include -using std::string; -extern void MD5Final(string& digest, MD5_CTX *); -extern bool MD5File(const string& filename, string& digest, string *reason); -extern string& MD5String(const string& data, string& digest); -extern string& MD5HexPrint(const string& digest, string& xdigest); -extern string& MD5HexScan(const string& xdigest, string& digest); +/** md5 utility: compute file md5 */ + +extern bool MD5File(const std::string& filename, std::string& digest, + std::string *reason); #endif /* _MD5UT_H_ */ diff --git a/src/utils/smallut.cpp b/src/utils/smallut.cpp index e9b8ac0f..d6e0e547 100644 --- a/src/utils/smallut.cpp +++ b/src/utils/smallut.cpp @@ -477,13 +477,13 @@ void ltrimstring(string& s, const char *ws) } // Remove some chars and replace them with spaces -string neutchars(const string& str, const string& chars) +string neutchars(const string& str, const string& chars, char rep) { string out; - neutchars(str, out, chars); + neutchars(str, out, chars, rep); return out; } -void neutchars(const string& str, string& out, const string& chars) +void neutchars(const string& str, string& out, const string& chars, char rep) { string::size_type startPos, pos; @@ -498,7 +498,7 @@ void neutchars(const string& str, string& out, const string& chars) if (pos == string::npos) { out += str.substr(startPos); } else { - out += str.substr(startPos, pos - startPos) + " "; + out += str.substr(startPos, pos - startPos) + rep; } } } @@ -1237,9 +1237,10 @@ class SimpleRegexp::Internal { public: Internal(const string& exp, int flags, int nm) : expr(exp, - basic_regex::flag_type(regex_constants::extended | - ((flags&SRE_ICASE) ? int(regex_constants::icase) : 0) | - ((flags&SRE_NOSUB) ? int(regex_constants::nosubs) : 0) + basic_regex::flag_type( + regex_constants::extended | + ((flags&SRE_ICASE) ? int(regex_constants::icase) : 0) | + ((flags&SRE_NOSUB) ? int(regex_constants::nosubs) : 0) )), ok(true), nmatch(nm) { } std::regex expr; diff --git a/src/utils/smallut.h b/src/utils/smallut.h index 1ab958c0..0946398c 100644 --- a/src/utils/smallut.h +++ b/src/utils/smallut.h @@ -152,9 +152,10 @@ extern std::string escapeHtml(const std::string& in); extern std::string makeCString(const std::string& in); /** Replace some chars with spaces (ie: newline chars). */ -extern std::string neutchars(const std::string& str, const std::string& chars); +extern std::string neutchars(const std::string& str, const std::string& chars, + char rep = ' '); extern void neutchars(const std::string& str, std::string& out, - const std::string& chars); + const std::string& chars, char rep = ' '); /** Turn string into something that won't be expanded by a shell. In practise * quote with double-quotes and escape $`\ */