[NFC][Support] Move `MD5` members in `InternalState`.

This prepares an update to follow other hashes.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D108388
This commit is contained in:
Alexandre Rames 2021-08-19 10:09:53 -07:00
parent 6c1436a9b0
commit 10a126325d
2 changed files with 49 additions and 46 deletions

View File

@ -39,18 +39,6 @@ template <unsigned N> class SmallString;
template <typename T> class ArrayRef; template <typename T> class ArrayRef;
class MD5 { class MD5 {
// Any 32-bit or wider unsigned integer data type will do.
typedef uint32_t MD5_u32plus;
MD5_u32plus a = 0x67452301;
MD5_u32plus b = 0xefcdab89;
MD5_u32plus c = 0x98badcfe;
MD5_u32plus d = 0x10325476;
MD5_u32plus hi = 0;
MD5_u32plus lo = 0;
uint8_t buffer[64];
MD5_u32plus block[16];
public: public:
struct MD5Result { struct MD5Result {
std::array<uint8_t, 16> Bytes; std::array<uint8_t, 16> Bytes;
@ -98,6 +86,21 @@ public:
static std::array<uint8_t, 16> hash(ArrayRef<uint8_t> Data); static std::array<uint8_t, 16> hash(ArrayRef<uint8_t> Data);
private: private:
// Any 32-bit or wider unsigned integer data type will do.
typedef uint32_t MD5_u32plus;
// Internal State
struct {
MD5_u32plus a = 0x67452301;
MD5_u32plus b = 0xefcdab89;
MD5_u32plus c = 0x98badcfe;
MD5_u32plus d = 0x10325476;
MD5_u32plus hi = 0;
MD5_u32plus lo = 0;
uint8_t buffer[64];
MD5_u32plus block[16];
} InternalState;
const uint8_t *body(ArrayRef<uint8_t> Data); const uint8_t *body(ArrayRef<uint8_t> Data);
}; };

View File

@ -67,11 +67,11 @@
// SET reads 4 input bytes in little-endian byte order and stores them // SET reads 4 input bytes in little-endian byte order and stores them
// in a properly aligned word in host byte order. // in a properly aligned word in host byte order.
#define SET(n) \ #define SET(n) \
(block[(n)] = \ (InternalState.block[(n)] = (MD5_u32plus)ptr[(n)*4] | \
(MD5_u32plus) ptr[(n) * 4] | ((MD5_u32plus) ptr[(n) * 4 + 1] << 8) | \ ((MD5_u32plus)ptr[(n)*4 + 1] << 8) | \
((MD5_u32plus) ptr[(n) * 4 + 2] << 16) | \ ((MD5_u32plus)ptr[(n)*4 + 2] << 16) | \
((MD5_u32plus) ptr[(n) * 4 + 3] << 24)) ((MD5_u32plus)ptr[(n)*4 + 3] << 24))
#define GET(n) (block[(n)]) #define GET(n) (InternalState.block[(n)])
using namespace llvm; using namespace llvm;
@ -85,10 +85,10 @@ const uint8_t *MD5::body(ArrayRef<uint8_t> Data) {
ptr = Data.data(); ptr = Data.data();
a = this->a; a = InternalState.a;
b = this->b; b = InternalState.b;
c = this->c; c = InternalState.c;
d = this->d; d = InternalState.d;
do { do {
saved_a = a; saved_a = a;
@ -176,10 +176,10 @@ const uint8_t *MD5::body(ArrayRef<uint8_t> Data) {
ptr += 64; ptr += 64;
} while (Size -= 64); } while (Size -= 64);
this->a = a; InternalState.a = a;
this->b = b; InternalState.b = b;
this->c = c; InternalState.c = c;
this->d = d; InternalState.d = d;
return ptr; return ptr;
} }
@ -193,10 +193,10 @@ void MD5::update(ArrayRef<uint8_t> Data) {
const uint8_t *Ptr = Data.data(); const uint8_t *Ptr = Data.data();
unsigned long Size = Data.size(); unsigned long Size = Data.size();
saved_lo = lo; saved_lo = InternalState.lo;
if ((lo = (saved_lo + Size) & 0x1fffffff) < saved_lo) if ((InternalState.lo = (saved_lo + Size) & 0x1fffffff) < saved_lo)
hi++; InternalState.hi++;
hi += Size >> 29; InternalState.hi += Size >> 29;
used = saved_lo & 0x3f; used = saved_lo & 0x3f;
@ -204,14 +204,14 @@ void MD5::update(ArrayRef<uint8_t> Data) {
free = 64 - used; free = 64 - used;
if (Size < free) { if (Size < free) {
memcpy(&buffer[used], Ptr, Size); memcpy(&InternalState.buffer[used], Ptr, Size);
return; return;
} }
memcpy(&buffer[used], Ptr, free); memcpy(&InternalState.buffer[used], Ptr, free);
Ptr = Ptr + free; Ptr = Ptr + free;
Size -= free; Size -= free;
body(makeArrayRef(buffer, 64)); body(makeArrayRef(InternalState.buffer, 64));
} }
if (Size >= 64) { if (Size >= 64) {
@ -219,7 +219,7 @@ void MD5::update(ArrayRef<uint8_t> Data) {
Size &= 0x3f; Size &= 0x3f;
} }
memcpy(buffer, Ptr, Size); memcpy(InternalState.buffer, Ptr, Size);
} }
/// Add the bytes in the StringRef \p Str to the hash. /// Add the bytes in the StringRef \p Str to the hash.
@ -235,31 +235,31 @@ void MD5::update(StringRef Str) {
void MD5::final(MD5Result &Result) { void MD5::final(MD5Result &Result) {
unsigned long used, free; unsigned long used, free;
used = lo & 0x3f; used = InternalState.lo & 0x3f;
buffer[used++] = 0x80; InternalState.buffer[used++] = 0x80;
free = 64 - used; free = 64 - used;
if (free < 8) { if (free < 8) {
memset(&buffer[used], 0, free); memset(&InternalState.buffer[used], 0, free);
body(makeArrayRef(buffer, 64)); body(makeArrayRef(InternalState.buffer, 64));
used = 0; used = 0;
free = 64; free = 64;
} }
memset(&buffer[used], 0, free - 8); memset(&InternalState.buffer[used], 0, free - 8);
lo <<= 3; InternalState.lo <<= 3;
support::endian::write32le(&buffer[56], lo); support::endian::write32le(&InternalState.buffer[56], InternalState.lo);
support::endian::write32le(&buffer[60], hi); support::endian::write32le(&InternalState.buffer[60], InternalState.hi);
body(makeArrayRef(buffer, 64)); body(makeArrayRef(InternalState.buffer, 64));
support::endian::write32le(&Result[0], a); support::endian::write32le(&Result[0], InternalState.a);
support::endian::write32le(&Result[4], b); support::endian::write32le(&Result[4], InternalState.b);
support::endian::write32le(&Result[8], c); support::endian::write32le(&Result[8], InternalState.c);
support::endian::write32le(&Result[12], d); support::endian::write32le(&Result[12], InternalState.d);
} }
SmallString<32> MD5::MD5Result::digest() const { SmallString<32> MD5::MD5Result::digest() const {