[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;
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:
struct MD5Result {
std::array<uint8_t, 16> Bytes;
@ -98,6 +86,21 @@ public:
static std::array<uint8_t, 16> hash(ArrayRef<uint8_t> Data);
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);
};

View File

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