From b8be14bd7bbbb5befe3a5d90f31c8cc2b2373b3f Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Wed, 15 Jul 2020 10:48:08 +0200 Subject: [PATCH] shared --- src/utils/md5.cpp | 64 +++++++------- src/utils/md5.h | 2 +- src/utils/smallut.cpp | 197 ++++++++++++++++++------------------------ src/utils/smallut.h | 10 +-- 4 files changed, 124 insertions(+), 149 deletions(-) diff --git a/src/utils/md5.cpp b/src/utils/md5.cpp index 10e8e81e..4f9dd938 100644 --- a/src/utils/md5.cpp +++ b/src/utils/md5.cpp @@ -27,27 +27,27 @@ #include -#define PUT_BIT_LE(i, cp, value) do { \ - (cp)[i] = (uint8_t)(((value) >> 8 * i) & 0xFF); \ -} while (0) +#define PUT_BIT_LE(i, cp, value) do { \ + (cp)[i] = (uint8_t)(((value) >> 8 * i) & 0xFF); \ + } while (0) -#define PUT_64BIT_LE(cp, value) do { \ - PUT_BIT_LE(7, cp, value); \ - PUT_BIT_LE(6, cp, value); \ - PUT_BIT_LE(5, cp, value); \ - PUT_BIT_LE(4, cp, value); \ - PUT_BIT_LE(3, cp, value); \ - PUT_BIT_LE(2, cp, value); \ - PUT_BIT_LE(1, cp, value); \ - PUT_BIT_LE(0, cp, value); \ -} while (0) +#define PUT_64BIT_LE(cp, value) do { \ + PUT_BIT_LE(7, cp, value); \ + PUT_BIT_LE(6, cp, value); \ + PUT_BIT_LE(5, cp, value); \ + PUT_BIT_LE(4, cp, value); \ + PUT_BIT_LE(3, cp, value); \ + PUT_BIT_LE(2, cp, value); \ + PUT_BIT_LE(1, cp, value); \ + PUT_BIT_LE(0, cp, value); \ + } while (0) -#define PUT_32BIT_LE(cp, value) do { \ - PUT_BIT_LE(3, cp, value); \ - PUT_BIT_LE(2, cp, value); \ - PUT_BIT_LE(1, cp, value); \ - PUT_BIT_LE(0, cp, value); \ -} while (0) +#define PUT_32BIT_LE(cp, value) do { \ + PUT_BIT_LE(3, cp, value); \ + PUT_BIT_LE(2, cp, value); \ + PUT_BIT_LE(1, cp, value); \ + PUT_BIT_LE(0, cp, value); \ + } while (0) static uint8_t PADDING[MD5_BLOCK_LENGTH] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -145,7 +145,7 @@ MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx) #define F4(x, y, z) (y ^ (x | ~z)) /* This is the central step in the MD5 algorithm. */ -#define MD5STEP(f, w, x, y, z, data, s) \ +#define MD5STEP(f, w, x, y, z, data, s) \ ( w += f(x, y, z) + data, w = w<>(32-s), w += x ) /* @@ -256,14 +256,14 @@ void MD5Final(string &digest, MD5_CTX *context) { unsigned char d[16]; MD5Final (d, context); - digest.assign((const char *)d, 16); + digest.assign(reinterpret_cast(d), 16); } string& MD5String(const string& data, string& digest) { MD5_CTX ctx; MD5Init(&ctx); - MD5Update(&ctx, (const unsigned char*)data.c_str(), data.length()); + MD5Update(&ctx, reinterpret_cast(data.c_str()), data.length()); MD5Final(digest, &ctx); return digest; } @@ -273,10 +273,10 @@ string& MD5HexPrint(const string& digest, string &out) out.erase(); out.reserve(33); static const char hex[]="0123456789abcdef"; - auto hash = (const unsigned char *)digest.c_str(); + auto hash = reinterpret_cast(digest.c_str()); for (int i = 0; i < 16; i++) { - out.append(1, hex[hash[i] >> 4]); - out.append(1, hex[hash[i] & 0x0f]); + out.append(1, hex[hash[i] >> 4]); + out.append(1, hex[hash[i] & 0x0f]); } return out; } @@ -285,15 +285,15 @@ 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); + 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, static_cast(val)); } return digest; } diff --git a/src/utils/md5.h b/src/utils/md5.h index 1eea1f82..b9532442 100644 --- a/src/utils/md5.h +++ b/src/utils/md5.h @@ -36,7 +36,7 @@ void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]); #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& MD5HexPrint(const std::string& digest, std::string& out); extern std::string& MD5HexScan(const std::string& xdigest, std::string& digest); #endif /* _MD5_H_ */ diff --git a/src/utils/smallut.cpp b/src/utils/smallut.cpp index bad53bf8..6c54e412 100644 --- a/src/utils/smallut.cpp +++ b/src/utils/smallut.cpp @@ -15,9 +15,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA */ -#include -#include -#include +#include +#include +#include +#include #ifdef _WIN32 // needed for localtime_r under mingw? @@ -27,11 +28,11 @@ #endif /* _MSC_VER */ #endif /* _WIN32 */ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include // Older compilers don't support stdc++ regex, but Windows does not // have the Linux one. Have a simple class to solve the simple cases. @@ -46,6 +47,7 @@ #include #include #include +#include #include #include @@ -71,18 +73,18 @@ int stringicmp(const string& s1, const string& s2) ++it2; } return size1 == size2 ? 0 : -1; - } else { - while (it2 != s2.end()) { - c1 = ::toupper(*it1); - c2 = ::toupper(*it2); - if (c1 != c2) { - return c1 > c2 ? 1 : -1; - } - ++it1; - ++it2; - } - return size1 == size2 ? 0 : 1; } + + while (it2 != s2.end()) { + c1 = ::toupper(*it1); + c2 = ::toupper(*it2); + if (c1 != c2) { + return c1 > c2 ? 1 : -1; + } + ++it1; + ++it2; + } + return size1 == size2 ? 0 : 1; } void stringtolower(string& io) { @@ -150,17 +152,17 @@ int stringlowercmp(const string& s1, const string& s2) ++it2; } return size1 == size2 ? 0 : -1; - } else { - while (it2 != s2.end()) { - c2 = ::tolower(*it2); - if (*it1 != c2) { - return *it1 > c2 ? 1 : -1; - } - ++it1; - ++it2; - } - return size1 == size2 ? 0 : 1; } + + while (it2 != s2.end()) { + c2 = ::tolower(*it2); + if (*it1 != c2) { + return *it1 > c2 ? 1 : -1; + } + ++it1; + ++it2; + } + return size1 == size2 ? 0 : 1; } // s1 is already uppercase @@ -181,42 +183,29 @@ int stringuppercmp(const string& s1, const string& s2) ++it2; } return size1 == size2 ? 0 : -1; - } else { - while (it2 != s2.end()) { - c2 = ::toupper(*it2); - if (*it1 != c2) { - return *it1 > c2 ? 1 : -1; - } - ++it1; - ++it2; - } - return size1 == size2 ? 0 : 1; } + + while (it2 != s2.end()) { + c2 = ::toupper(*it2); + if (*it1 != c2) { + return *it1 > c2 ? 1 : -1; + } + ++it1; + ++it2; + } + return size1 == size2 ? 0 : 1; } bool beginswith(const std::string& big, const std::string& small) { - if (big.compare(0, small.size(), small)) { - return false; - } - return true; + return big.compare(0, small.size(), small) == 0; } // Compare charset names, removing the more common spelling variations bool samecharset(const string& cs1, const string& cs2) { - string mcs1, mcs2; - // Remove all - and _, turn to lowecase - for (unsigned int i = 0; i < cs1.length(); i++) { - if (cs1[i] != '_' && cs1[i] != '-') { - mcs1 += ::tolower(cs1[i]); - } - } - for (unsigned int i = 0; i < cs2.length(); i++) { - if (cs2[i] != '_' && cs2[i] != '-') { - mcs2 += ::tolower(cs2[i]); - } - } + auto mcs1 = std::accumulate(cs1.begin(), cs1.end(), "", [](const char* m, char i) { return (i != '_' && i != '-') ? m + ::tolower(i) : m; }); + auto mcs2 = std::accumulate(cs2.begin(), cs2.end(), "", [](const char* m, char i) { return (i != '_' && i != '-') ? m + ::tolower(i) : m; }); return mcs1 == mcs2; } @@ -227,8 +216,8 @@ template bool stringToStrings(const string& s, T& tokens, tokens.clear(); enum states {SPACE, TOKEN, INQUOTE, ESCAPE}; states state = SPACE; - for (unsigned int i = 0; i < s.length(); i++) { - switch (s[i]) { + for (char i : s) { + switch (i) { case '"': switch (state) { case SPACE: @@ -279,13 +268,13 @@ template bool stringToStrings(const string& s, T& tokens, continue; case INQUOTE: case ESCAPE: - current += s[i]; + current += i; continue; } break; default: - if (!addseps.empty() && addseps.find(s[i]) != string::npos) { + if (!addseps.empty() && addseps.find(i) != string::npos) { switch (state) { case ESCAPE: state = INQUOTE; @@ -293,12 +282,12 @@ template bool stringToStrings(const string& s, T& tokens, case INQUOTE: break; case SPACE: - tokens.insert(tokens.end(), string(1, s[i])); + tokens.insert(tokens.end(), string(1, i)); continue; case TOKEN: tokens.insert(tokens.end(), current); current.erase(); - tokens.insert(tokens.end(), string(1, s[i])); + tokens.insert(tokens.end(), string(1, i)); state = SPACE; continue; } @@ -313,7 +302,7 @@ template bool stringToStrings(const string& s, T& tokens, case INQUOTE: break; } - current += s[i]; + current += i; } } switch (state) { @@ -340,7 +329,7 @@ template bool stringToStrings > template void stringsToString(const T& tokens, string& s) { - for (typename T::const_iterator it = tokens.begin(); + for (auto it = tokens.begin(); it != tokens.end(); it++) { bool hasblanks = false; if (it->find_first_of(" \t\n") != string::npos) { @@ -385,7 +374,7 @@ template void stringsToCSV(const T& tokens, string& s, char sep) { s.erase(); - for (typename T::const_iterator it = tokens.begin(); + for (auto it = tokens.begin(); it != tokens.end(); it++) { bool needquotes = false; if (it->empty() || @@ -433,10 +422,11 @@ void stringToTokens(const string& str, vector& tokens, if (pos == string::npos) { tokens.push_back(str.substr(startPos)); break; - } else if (pos == startPos) { + } + if (pos == startPos) { // Dont' push empty tokens after first if (tokens.empty()) { - tokens.push_back(string()); + tokens.emplace_back(); } startPos = ++pos; } else { @@ -461,9 +451,10 @@ void stringSplitString(const string& str, vector& tokens, if (pos == string::npos) { tokens.push_back(str.substr(startPos)); break; - } else if (pos == startPos) { + } + if (pos == startPos) { // Initial or consecutive separators - tokens.push_back(string()); + tokens.emplace_back(); } else { tokens.push_back(str.substr(startPos, pos - startPos)); } @@ -478,12 +469,9 @@ bool stringToBool(const string& s) } if (isdigit(s[0])) { int val = atoi(s.c_str()); - return val ? true : false; + return val != 0; } - if (s.find_first_of("yYtT") == 0) { - return true; - } - return false; + return s.find_first_of("yYtT") == 0; } void trimstring(string& s, const char *ws) @@ -572,13 +560,13 @@ string truncate_to_word(const string& input, string::size_type maxlen) string escapeHtml(const string& in) { string out; - for (string::size_type pos = 0; pos < in.length(); pos++) { - switch(in.at(pos)) { + for (char pos : in) { + switch(pos) { case '<': out += "<"; break; case '>': out += ">"; break; case '&': out += "&"; break; case '"': out += """; break; - default: out += in.at(pos); break; + default: out += pos; break; } } return out; @@ -588,8 +576,8 @@ string escapeShell(const string& in) { string out; out += "\""; - for (string::size_type pos = 0; pos < in.length(); pos++) { - switch (in.at(pos)) { + for (char pos : in) { + switch (pos) { case '$': out += "\\$"; break; @@ -606,7 +594,7 @@ string escapeShell(const string& in) out += "\\\\"; break; default: - out += in.at(pos); + out += pos; } } out += "\""; @@ -619,8 +607,8 @@ string makeCString(const string& in) { string out; out += "\""; - for (string::size_type pos = 0; pos < in.length(); pos++) { - switch (in.at(pos)) { + for (char pos : in) { + switch (pos) { case '"': out += "\\\""; break; @@ -634,7 +622,7 @@ string makeCString(const string& in) out += "\\\\"; break; default: - out += in.at(pos); + out += pos; } } out += "\""; @@ -684,13 +672,13 @@ bool pcSubst(const string& in, string& out, const map& subs) out += '%'; continue; } - string key = ""; + string key; if (in[i] == '(') { if (++i == in.size()) { out += string("%("); break; } - string::size_type j = in.find_first_of(")", i); + string::size_type j = in.find_first_of(')', i); if (j == string::npos) { // ??concatenate remaining part and stop out += in.substr(i - 2); @@ -733,7 +721,6 @@ void ulltodecstr(uint64_t val, string& buf) } while (val); buf.assign(&rbuf[idx+1]); - return; } void lltodecstr(int64_t val, string& buf) @@ -760,7 +747,6 @@ void lltodecstr(int64_t val, string& buf) rbuf[idx--] = '-'; } buf.assign(&rbuf[idx+1]); - return; } string lltodecstr(int64_t val) @@ -809,9 +795,9 @@ string breakIntoLines(const string& in, unsigned int ll, while (query.length() > 0) { string ss = query.substr(0, ll); if (ss.length() == ll) { - string::size_type pos = ss.find_last_of(" "); + string::size_type pos = ss.find_last_of(' '); if (pos == string::npos) { - pos = query.find_first_of(" "); + pos = query.find_first_of(' '); if (pos != string::npos) { ss = query.substr(0, pos + 1); } else { @@ -1080,7 +1066,7 @@ secondelt: // Empty part means today IF other part is period, else means // forever (stays at 0) - time_t now = time(0); + time_t now = time(nullptr); struct tm *tmnow = gmtime(&now); if ((!hasp1 && !hasd1) && hasp2) { d1.y1 = 1900 + tmnow->tm_year; @@ -1161,7 +1147,7 @@ std::string hexprint(const std::string& in, char separ) string out; out.reserve(separ ? (3 *in.size()) : (2 * in.size())); static const char hex[]="0123456789abcdef"; - auto cp = (const unsigned char *)in.c_str(); + auto cp = reinterpret_cast(in.c_str()); for (unsigned int i = 0; i < in.size(); i++) { out.append(1, hex[cp[i] >> 4]); out.append(1, hex[cp[i] & 0x0f]); @@ -1269,12 +1255,12 @@ string localelang() { const char *lang = getenv("LANG"); - if (lang == 0 || *lang == 0 || !strcmp(lang, "C") || + if (lang == nullptr || *lang == 0 || !strcmp(lang, "C") || !strcmp(lang, "POSIX")) { return "en"; } string locale(lang); - string::size_type under = locale.find_first_of("_"); + string::size_type under = locale.find_first_of('_'); if (under == string::npos) { return locale; } @@ -1316,13 +1302,11 @@ string SimpleRegexp::getMatch(const string&, int i) const class SimpleRegexp::Internal { public: Internal(const string& exp, int flags, int nm) : nmatch(nm) { - if (regcomp(&expr, exp.c_str(), REG_EXTENDED | - ((flags&SRE_ICASE) ? REG_ICASE : 0) | - ((flags&SRE_NOSUB) ? REG_NOSUB : 0)) == 0) { - ok = true; - } else { - ok = false; - } + ok = regcomp(&expr, exp.c_str(), REG_EXTENDED | + + ((flags & SRE_ICASE) ? REG_ICASE : 0) | + + ((flags & SRE_NOSUB) ? REG_NOSUB : 0)) == 0; matches.resize(nmatch+1); } ~Internal() { @@ -1338,11 +1322,7 @@ bool SimpleRegexp::simpleMatch(const string& val) const { if (!ok()) return false; - if (regexec(&m->expr, val.c_str(), m->nmatch+1, &m->matches[0], 0) == 0) { - return true; - } else { - return false; - } + return regexec(&m->expr, val.c_str(), m->nmatch + 1, &m->matches[0], 0) == 0; } string SimpleRegexp::getMatch(const string& val, int i) const @@ -1424,13 +1404,8 @@ unsigned int stringToFlags(const vector& flags, stringToTokens(input, toks, sep); for (auto& tok: toks) { trimstring(tok); - for (auto& flag : flags) { - if (!tok.compare(flag.yesname)) { - /* Note: we don't break: the same name could conceivably - set several flags. */ - out |= flag.value; - } - } + out += std::accumulate(flags.begin(), flags.end(), out, + [&](int o, CharFlags flag){ return tok == flag.yesname ? o | flag.value : o; }); } return out; } diff --git a/src/utils/smallut.h b/src/utils/smallut.h index afe18a4a..4b4df678 100644 --- a/src/utils/smallut.h +++ b/src/utils/smallut.h @@ -57,9 +57,9 @@ struct StringIcmpPred { const std::string& m_s1; }; -extern int stringlowercmp(const std::string& alreadylower, +extern int stringlowercmp(const std::string& s1, const std::string& s2); -extern int stringuppercmp(const std::string& alreadyupper, +extern int stringuppercmp(const std::string& s1, const std::string& s2); extern void stringtolower(std::string& io); @@ -163,7 +163,7 @@ extern void neutchars(const std::string& str, std::string& out, /** Turn string into something that won't be expanded by a shell. In practise * quote with double-quotes and escape $`\ */ -extern std::string escapeShell(const std::string& str); +extern std::string escapeShell(const std::string& in); /** Truncate a string to a given maxlength, avoiding cutting off midword * if reasonably possible. */ @@ -221,7 +221,7 @@ public: bool simpleMatch(const std::string& val) const; /// After simpleMatch success, get nth submatch, 0 is the whole /// match, 1 first parentheses, etc. - std::string getMatch(const std::string& val, int matchidx) const; + std::string getMatch(const std::string& val, int i) const; /// Calls simpleMatch() bool operator() (const std::string& val) const; /// Check after construction @@ -249,7 +249,7 @@ struct CharFlags { /// Translate a bitfield into string description extern std::string flagsToString(const std::vector&, - unsigned int flags); + unsigned int val); /// Translate a value into a name extern std::string valToString(const std::vector&, unsigned int val);