diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ReadHex.C b/src/OpenFOAM/db/IOstreams/Sstreams/ReadHex.C index 3d41d59066..a599df56ea 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ReadHex.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ReadHex.C @@ -36,10 +36,11 @@ Description template T Foam::ReadHex(ISstream& is) { - // Takes into account that 'a' (or 'A') is 10 - static const int alphaOffset = toupper('A') - 10; - // Takes into account that '0' is 0 - static const int zeroOffset = int('0'); + // The char '0' == 0 + static constexpr int offsetZero = int('0'); + + // The char 'A' (or 'a') == 10 + static constexpr int offsetAlpha = int('A') - 10; char c = 0; @@ -63,11 +64,11 @@ T Foam::ReadHex(ISstream& is) if (isdigit(c)) { - result += int(c) - zeroOffset; + result += int(c) - offsetZero; } else { - result += toupper(c) - alphaOffset; + result += toupper(c) - offsetAlpha; } } while (is.get(c)); diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ReadHex.H b/src/OpenFOAM/db/IOstreams/Sstreams/ReadHex.H index 3646642897..d98d48aec6 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ReadHex.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ReadHex.H @@ -34,8 +34,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef ReadHex_H -#define ReadHex_H +#ifndef Foam_ReadHex_H +#define Foam_ReadHex_H #include "ISstream.H" @@ -44,7 +44,7 @@ SourceFiles namespace Foam { -//- Read a hex label from an input stream +//- Read a hex integer from an input stream template T ReadHex(ISstream& is); diff --git a/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H b/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H index 763745aa25..1268d7a844 100644 --- a/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H +++ b/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2019-2021 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,8 +32,8 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef OSHA1stream_H -#define OSHA1stream_H +#ifndef Foam_OSHA1stream_H +#define Foam_OSHA1stream_H #include "OSstream.H" #include "SHA1.H" @@ -63,10 +63,17 @@ class osha1stream protected: + //- Handle overflow + virtual int overflow(int c = EOF) + { + if (c != EOF) sha1_.append(c); + return c; + } + //- Put sequence of characters virtual std::streamsize xsputn(const char* s, std::streamsize n) { - sha1_.append(s, n); + if (n) sha1_.append(s, n); return n; } @@ -76,7 +83,7 @@ class osha1stream sha1buf() = default; //- Full access to the sha1 - inline SHA1& sha1() + SHA1& sha1() noexcept { return sha1_; } @@ -108,7 +115,7 @@ public: } //- Full access to the sha1 - SHA1& sha1() + SHA1& sha1() noexcept { return buf_.sha1(); } @@ -146,7 +153,7 @@ public: // Member Functions //- Full access to the sha1 - SHA1& sha1() + SHA1& sha1() noexcept { return stream_.sha1(); } diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H index 9d9e617a48..6c43f06e06 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -42,8 +42,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef SHA1_H -#define SHA1_H +#ifndef Foam_SHA1_H +#define Foam_SHA1_H #include #include @@ -98,13 +98,13 @@ public: // Constructors - //- Construct null + //- Default construct inline SHA1(); - //- Construct null and append initial string + //- Default construct and append initial string content inline explicit SHA1(const char* str); - //- Construct null and append initial std::string + //- Default construct and append initial string content inline explicit SHA1(const std::string& str); @@ -113,17 +113,20 @@ public: //- Reset the hashed data before appending more void clear() noexcept; - //- Append data for processing - inline SHA1& append(const char* str); - - //- Append data for processing - inline SHA1& append(const char* data, size_t len); + //- Append single character + inline void append(char c); //- Append string for processing - inline SHA1& append(const std::string& str); + inline void append(const char* str); + + //- Append data for processing + inline void append(const char* data, size_t len); + + //- Append string for processing + inline void append(const std::string& str); //- Append substring for processing - inline SHA1& append + inline void append ( const std::string& str, size_t pos, diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C index 6704ced21f..dd0da5a0ba 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -40,7 +40,7 @@ static const char hexChars[] = "0123456789abcdef"; static constexpr int offsetZero = int('0'); // The char 'A' (or 'a') == 10 -static constexpr int offsetUpper = int('A') - 10; +static constexpr int offsetAlpha = int('A') - 10; // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // @@ -70,12 +70,42 @@ static unsigned char readHexDigit(Istream& is) << exit(FatalIOError); } - return toupper(c) - offsetUpper; + return toupper(c) - offsetAlpha; } } // End namespace Foam +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +bool Foam::SHA1Digest::isEqual(const char* hexdigits, std::size_t len) const +{ + // Skip possible '_' prefix + if (*hexdigits == '_') + { + ++hexdigits; + --len; + } + + // Incorrect length - can never match + if (len != 2*dig_.size()) + { + return false; + } + + for (const auto& byteVal : dig_) + { + const char upp = hexChars[((byteVal >> 4) & 0xF)]; + const char low = hexChars[(byteVal & 0xF)]; + + if (upp != *hexdigits++) return false; + if (low != *hexdigits++) return false; + } + + return true; +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::SHA1Digest::SHA1Digest() @@ -113,10 +143,25 @@ bool Foam::SHA1Digest::empty() const } +Foam::Istream& Foam::SHA1Digest::read(Istream& is) +{ + for (auto& byteVal : dig_) + { + const unsigned char upp = readHexDigit(is); + const unsigned char low = readHexDigit(is); + + byteVal = (upp << 4) + low; + } + + is.check(FUNCTION_NAME); + return is; +} + + std::string Foam::SHA1Digest::str(const bool prefixed) const { std::string buf; - unsigned nChar = 0; + std::size_t nChar = 0; if (prefixed) { @@ -138,21 +183,6 @@ std::string Foam::SHA1Digest::str(const bool prefixed) const } -Foam::Istream& Foam::SHA1Digest::read(Istream& is) -{ - for (auto& byteVal : dig_) - { - const unsigned char upp = readHexDigit(is); - const unsigned char low = readHexDigit(is); - - byteVal = (upp << 4) + low; - } - - is.check(FUNCTION_NAME); - return is; -} - - Foam::Ostream& Foam::SHA1Digest::write(Ostream& os, const bool prefixed) const { if (prefixed) @@ -187,29 +217,7 @@ bool Foam::SHA1Digest::operator==(const std::string& hexdigits) const return empty(); } - // Skip possible '_' prefix - unsigned nChar = 0; - if (hexdigits[0] == '_') - { - ++nChar; - } - - // Incorrect length - can never match - if (hexdigits.size() != nChar + 2*dig_.size()) - { - return false; - } - - for (const auto& byteVal : dig_) - { - const char upp = hexChars[((byteVal >> 4) & 0xF)]; // Upper nibble - const char low = hexChars[(byteVal & 0xF)]; // Lower nibble - - if (upp != hexdigits[nChar++]) return false; - if (low != hexdigits[nChar++]) return false; - } - - return true; + return isEqual(hexdigits.data(), hexdigits.length()); } @@ -221,47 +229,25 @@ bool Foam::SHA1Digest::operator==(const char* hexdigits) const return empty(); } - // Skip possible '_' prefix - unsigned nChar = 0; - if (hexdigits[0] == '_') - { - ++nChar; - } - - // Incorrect length - can never match - if (strlen(hexdigits) != nChar + 2*dig_.size()) - { - return false; - } - - for (const auto& byteVal : dig_) - { - const char upp = hexChars[((byteVal >> 4) & 0xF)]; - const char low = hexChars[(byteVal & 0xF)]; - - if (upp != hexdigits[nChar++]) return false; - if (low != hexdigits[nChar++]) return false; - } - - return true; + return isEqual(hexdigits, std::char_traits::length(hexdigits)); } bool Foam::SHA1Digest::operator!=(const SHA1Digest& rhs) const { - return !operator==(rhs); + return !this->operator==(rhs); } -bool Foam::SHA1Digest::operator!=(const std::string& rhs) const +bool Foam::SHA1Digest::operator!=(const std::string& hexdigits) const { - return !operator==(rhs); + return !this->operator==(hexdigits); } -bool Foam::SHA1Digest::operator!=(const char* rhs) const +bool Foam::SHA1Digest::operator!=(const char* hexdigits) const { - return !operator==(rhs); + return !this->operator==(hexdigits); } @@ -275,7 +261,8 @@ Foam::Istream& Foam::operator>>(Istream& is, SHA1Digest& dig) Foam::Ostream& Foam::operator<<(Ostream& os, const SHA1Digest& dig) { - return dig.write(os); + // Write with prefixed = false + return dig.write(os, false); } diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H index 7b8c8041bc..f7456ef1b2 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H @@ -38,8 +38,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef SHA1Digest_H -#define SHA1Digest_H +#ifndef Foam_SHA1Digest_H +#define Foam_SHA1Digest_H #include #include @@ -74,13 +74,15 @@ class SHA1Digest return dig_.data(); } + //- Byte-wise compare digest contents + bool isEqual(const char* hexdigits, std::size_t len) const; + // Permit SHA1 to calculate the digest friend class SHA1; public: - // Static Data Members //- A null digest (ie, all zero) @@ -89,7 +91,7 @@ public: // Constructors - //- Construct a zero digest + //- Default construct a zero digest SHA1Digest(); //- Read construct a digest @@ -120,7 +122,7 @@ public: // Member Operators //- Equality operator - bool operator==(const SHA1Digest&) const; + bool operator==(const SHA1Digest& rhs) const; //- Compare to (40-byte) text representation (eg, from sha1sum) // An %empty string is equivalent to @@ -136,7 +138,7 @@ public: //- Inequality operator - bool operator!=(const SHA1Digest&) const; + bool operator!=(const SHA1Digest& rhs) const; //- Inequality operator bool operator!=(const std::string& hexdigits) const; @@ -149,7 +151,7 @@ public: // IOstream Operators //- Read (40-byte) text representation (ignoring leading '_' prefix) -Istream& operator>>(Istream&is , SHA1Digest& dig); +Istream& operator>>(Istream& is, SHA1Digest& dig); //- Write (40-byte) text representation, unquoted and without prefix Ostream& operator<<(Ostream& os, const SHA1Digest& dig); diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H index b04b3b6161..3c5a61bc7e 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2019-2021 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,8 +26,6 @@ License \*---------------------------------------------------------------------------*/ -#include "SHA1.H" - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // inline Foam::SHA1::SHA1() @@ -52,31 +50,34 @@ inline Foam::SHA1::SHA1(const std::string& str) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -inline Foam::SHA1& Foam::SHA1::append(const char* data, size_t len) +inline void Foam::SHA1::append(char c) { - processBytes(data, len); - return *this; + processBytes(&c, 1); } -inline Foam::SHA1& Foam::SHA1::append(const char* str) +inline void Foam::SHA1::append(const char* data, size_t len) +{ + processBytes(data, len); +} + + +inline void Foam::SHA1::append(const char* str) { if (str && *str) { processBytes(str, std::char_traits::length(str)); } - return *this; } -inline Foam::SHA1& Foam::SHA1::append(const std::string& str) +inline void Foam::SHA1::append(const std::string& str) { processBytes(str.data(), str.length()); - return *this; } -inline Foam::SHA1& Foam::SHA1::append +inline void Foam::SHA1::append ( const std::string& str, size_t pos, @@ -92,8 +93,6 @@ inline Foam::SHA1& Foam::SHA1::append processBytes(str.data() + pos, len); } - - return *this; } @@ -175,15 +174,15 @@ inline bool Foam::SHA1::operator!=(const SHA1Digest& rhs) const } -inline bool Foam::SHA1::operator!=(const std::string& rhs) const +inline bool Foam::SHA1::operator!=(const std::string& hexdigits) const { - return !this->operator==(rhs); + return !this->operator==(hexdigits); } -inline bool Foam::SHA1::operator!=(const char* rhs) const +inline bool Foam::SHA1::operator!=(const char* hexdigits) const { - return !this->operator==(rhs); + return !this->operator==(hexdigits); }