X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/b80e619319b1def83d1e8b4f84042b661be1be7f..14957cd040308e3eeec43d26bae5d76da13fcd85:/wtf/MD5.cpp?ds=inline diff --git a/wtf/MD5.cpp b/wtf/MD5.cpp index cd1837a..07bbadd 100644 --- a/wtf/MD5.cpp +++ b/wtf/MD5.cpp @@ -54,6 +54,7 @@ #include "StringExtras.h" #include "text/CString.h" #endif +#include namespace WTF { @@ -67,14 +68,15 @@ static void expectMD5(CString input, CString expected) { MD5 md5; md5.addBytes(reinterpret_cast(input.data()), input.length()); - Vector digest = md5.checksum(); + Vector digest; + md5.checksum(digest); char* buf = 0; CString actual = CString::newUninitialized(32, buf); for (size_t i = 0; i < 16; i++) { snprintf(buf, 3, "%02x", digest.at(i)); buf += 2; } - ASSERT_WITH_MESSAGE(actual == expected, "input:%s[%d] actual:%s expected:%s", input.data(), input.length(), actual.data(), expected.data()); + ASSERT_WITH_MESSAGE(actual == expected, "input:%s[%lu] actual:%s expected:%s", input.data(), static_cast(input.length()), actual.data(), expected.data()); } static void testMD5() @@ -102,7 +104,7 @@ static void reverseBytes(uint8_t* buf, unsigned longs) do { uint32_t t = static_cast(buf[3] << 8 | buf[2]) << 16 | buf[1] << 8 | buf[0]; ASSERT_WITH_MESSAGE(!(reinterpret_cast(buf) % sizeof(t)), "alignment error of buf"); - *reinterpret_cast(buf) = t; + *reinterpret_cast_ptr(buf) = t; buf += 4; } while (--longs); } @@ -201,6 +203,7 @@ static void MD5Transform(uint32_t buf[4], const uint32_t in[16]) MD5::MD5() { + // FIXME: Move unit tests somewhere outside the constructor. See bug 55853. testMD5(); m_buf[0] = 0x67452301; m_buf[1] = 0xefcdab89; @@ -237,7 +240,7 @@ void MD5::addBytes(const uint8_t* input, size_t length) } memcpy(p, buf, t); reverseBytes(m_in, 16); - MD5Transform(m_buf, reinterpret_cast(m_in)); // m_in is 4-byte aligned. + MD5Transform(m_buf, reinterpret_cast_ptr(m_in)); // m_in is 4-byte aligned. buf += t; length -= t; } @@ -247,7 +250,7 @@ void MD5::addBytes(const uint8_t* input, size_t length) while (length >= 64) { memcpy(m_in, buf, 64); reverseBytes(m_in, 16); - MD5Transform(m_buf, reinterpret_cast(m_in)); // m_in is 4-byte aligned. + MD5Transform(m_buf, reinterpret_cast_ptr(m_in)); // m_in is 4-byte aligned. buf += 64; length -= 64; } @@ -256,7 +259,7 @@ void MD5::addBytes(const uint8_t* input, size_t length) memcpy(m_in, buf, length); } -Vector MD5::checksum() +void MD5::checksum(Vector& digest) { // Compute number of bytes mod 64 unsigned count = (m_bits[0] >> 3) & 0x3F; @@ -274,7 +277,7 @@ Vector MD5::checksum() // Two lots of padding: Pad the first block to 64 bytes memset(p, 0, count); reverseBytes(m_in, 16); - MD5Transform(m_buf, reinterpret_cast(m_in)); // m_in is 4-byte aligned. + MD5Transform(m_buf, reinterpret_cast_ptr(m_in)); // m_in is 4-byte aligned. // Now fill the next block with 56 bytes memset(m_in, 0, 56); @@ -286,19 +289,21 @@ Vector MD5::checksum() // Append length in bits and transform // m_in is 4-byte aligned. - (reinterpret_cast(m_in))[14] = m_bits[0]; - (reinterpret_cast(m_in))[15] = m_bits[1]; + (reinterpret_cast_ptr(m_in))[14] = m_bits[0]; + (reinterpret_cast_ptr(m_in))[15] = m_bits[1]; - MD5Transform(m_buf, reinterpret_cast(m_in)); + MD5Transform(m_buf, reinterpret_cast_ptr(m_in)); reverseBytes(reinterpret_cast(m_buf), 4); - Vector digest; + + // Now, m_buf contains checksum result. + if (!digest.isEmpty()) + digest.clear(); digest.append(reinterpret_cast(m_buf), 16); // In case it's sensitive memset(m_buf, 0, sizeof(m_buf)); memset(m_bits, 0, sizeof(m_bits)); memset(m_in, 0, sizeof(m_in)); - return digest; } } // namespace WTF