diff options
author | Eric Christopher <echristo@gmail.com> | 2013-05-24 23:08:17 +0000 |
---|---|---|
committer | Eric Christopher <echristo@gmail.com> | 2013-05-24 23:08:17 +0000 |
commit | f7306f224e7f85c2690256636613422c4a7b8230 (patch) | |
tree | 8ed035cec51604f65c10bee3f312106e8600d230 /lib | |
parent | 80d10ded8cd4f34b87d82b03d6f63328ea337b26 (diff) | |
download | external_llvm-f7306f224e7f85c2690256636613422c4a7b8230.zip external_llvm-f7306f224e7f85c2690256636613422c4a7b8230.tar.gz external_llvm-f7306f224e7f85c2690256636613422c4a7b8230.tar.bz2 |
ArrayRef-ize MD5 and clean up a few variable names.
Add a stringize method to make dumping a bit easier, and add a testcase
exercising a few different paths.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182692 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Support/MD5.cpp | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/lib/Support/MD5.cpp b/lib/Support/MD5.cpp index 6dfbe7a..6cd040b 100644 --- a/lib/Support/MD5.cpp +++ b/lib/Support/MD5.cpp @@ -37,7 +37,10 @@ * compile-time configuration. */ +#include "llvm/ADT/ArrayRef.h" +#include "llvm/Support/Format.h" #include "llvm/Support/MD5.h" +#include "llvm/Support/raw_ostream.h" #include <cstring> // The basic MD5 functions. @@ -69,12 +72,13 @@ namespace llvm { /// \brief This processes one or more 64-byte data blocks, but does NOT update ///the bit counters. There are no alignment requirements. -void *MD5::body(void *data, unsigned long size) { - unsigned char *ptr; +const unsigned char *MD5::body(ArrayRef<unsigned char> Data) { + const unsigned char *ptr; MD5_u32plus a, b, c, d; MD5_u32plus saved_a, saved_b, saved_c, saved_d; + unsigned long Size = Data.size(); - ptr = (unsigned char *)data; + ptr = Data.data(); a = this->a; b = this->b; @@ -165,7 +169,7 @@ void *MD5::body(void *data, unsigned long size) { d += saved_d; ptr += 64; - } while (size -= 64); + } while (Size -= 64); this->a = a; this->b = b; @@ -180,42 +184,44 @@ MD5::MD5() } /// Incrementally add \p size of \p data to the hash. -void MD5::Update(void *data, unsigned long size) { +void MD5::update(ArrayRef<unsigned char> Data) { MD5_u32plus saved_lo; unsigned long used, free; + const unsigned char *Ptr = Data.data(); + unsigned long Size = Data.size(); saved_lo = lo; - if ((lo = (saved_lo + size) & 0x1fffffff) < saved_lo) + if ((lo = (saved_lo + Size) & 0x1fffffff) < saved_lo) hi++; - hi += size >> 29; + hi += Size >> 29; used = saved_lo & 0x3f; if (used) { free = 64 - used; - if (size < free) { - memcpy(&buffer[used], data, size); + if (Size < free) { + memcpy(&buffer[used], Ptr, Size); return; } - memcpy(&buffer[used], data, free); - data = (unsigned char *)data + free; - size -= free; - body(buffer, 64); + memcpy(&buffer[used], Ptr, free); + Ptr = Ptr + free; + Size -= free; + body(ArrayRef<unsigned char>(buffer, 64)); } - if (size >= 64) { - data = body(data, size & ~(unsigned long) 0x3f); - size &= 0x3f; + if (Size >= 64) { + Ptr = body(ArrayRef<unsigned char>(Ptr, Size & ~(unsigned long) 0x3f)); + Size &= 0x3f; } - memcpy(buffer, data, size); + memcpy(buffer, Ptr, Size); } /// \brief Finish the hash and place the resulting hash into \p result. /// \param result is assumed to be a minimum of 16-bytes in size. -void MD5::Final(unsigned char *result) { +void MD5::final(MD5Result &result) { unsigned long used, free; used = lo & 0x3f; @@ -226,7 +232,7 @@ void MD5::Final(unsigned char *result) { if (free < 8) { memset(&buffer[used], 0, free); - body(buffer, 64); + body(ArrayRef<unsigned char>(buffer, 64)); used = 0; free = 64; } @@ -243,7 +249,7 @@ void MD5::Final(unsigned char *result) { buffer[62] = hi >> 16; buffer[63] = hi >> 24; - body(buffer, 64); + body(ArrayRef<unsigned char>(buffer, 64)); result[0] = a; result[1] = a >> 8; @@ -263,4 +269,10 @@ void MD5::Final(unsigned char *result) { result[15] = d >> 24; } +void MD5::stringifyResult(MD5Result &result, SmallString<32> &Str) { + raw_svector_ostream Res(Str); + for (int i = 0; i < 16; ++i) + Res << format("%.2x", result[i]); +} + } |