SkMD5: cleanup header and minor refactor
authorhalcanary <halcanary@google.com>
Fri, 22 Apr 2016 17:40:49 +0000 (10:40 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 22 Apr 2016 17:40:49 +0000 (10:40 -0700)
Also: I now define a non-virtual function in terms of a final virtual
function.  This reduces the number of actual functions while adding
no overhead.

GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1911363002

Review URL: https://codereview.chromium.org/1911363002

src/core/SkMD5.cpp
src/core/SkMD5.h

index 725ae55..5c86522 100644 (file)
@@ -8,7 +8,11 @@
  * http://www.ietf.org/rfc/rfc1321.txt
  */
 
-#include "SkTypes.h"
+//The following macros can be defined to affect the MD5 code generated.
+//SK_MD5_CLEAR_DATA causes all intermediate state to be overwritten with 0's.
+//SK_CPU_LENDIAN allows 32 bit <=> 8 bit conversions without copies (if alligned).
+//SK_CPU_FAST_UNALIGNED_ACCESS allows 32 bit <=> 8 bit conversions without copies if SK_CPU_LENDIAN.
+
 #include "SkMD5.h"
 #include <string.h>
 
@@ -32,7 +36,8 @@ SkMD5::SkMD5() : byteCount(0) {
     this->state[3] = 0x10325476;
 }
 
-void SkMD5::update(const uint8_t* input, size_t inputLength) {
+bool SkMD5::write(const void* buf, size_t inputLength) {
+    const uint8_t* input = reinterpret_cast<const uint8_t*>(buf);
     unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F);
     unsigned int bufferAvailable = 64 - bufferIndex;
 
@@ -58,6 +63,7 @@ void SkMD5::update(const uint8_t* input, size_t inputLength) {
     memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIndex);
 
     this->byteCount += inputLength;
+    return true;
 }
 
 void SkMD5::finish(Digest& digest) {
index ed55793..a6d9b9f 100644 (file)
@@ -8,54 +8,37 @@
 #ifndef SkMD5_DEFINED
 #define SkMD5_DEFINED
 
-#include "SkTypes.h"
-#include "SkEndian.h"
 #include "SkStream.h"
 
-//The following macros can be defined to affect the MD5 code generated.
-//SK_MD5_CLEAR_DATA causes all intermediate state to be overwritten with 0's.
-//SK_CPU_LENDIAN allows 32 bit <=> 8 bit conversions without copies (if alligned).
-//SK_CPU_FAST_UNALIGNED_ACCESS allows 32 bit <=> 8 bit conversions without copies if SK_CPU_LENDIAN.
-
+/* Calculate a 128-bit MD5 message-digest of the bytes sent to this stream. */
 class SkMD5 : public SkWStream {
 public:
     SkMD5();
 
     /** Processes input, adding it to the digest.
-     *  Note that this treats the buffer as a series of uint8_t values.
-     */
-    bool write(const void* buffer, size_t size) override {
-        this->update(reinterpret_cast<const uint8_t*>(buffer), size);
-        return true;
-    }
+        Calling this after finish is undefined.  */
+    bool write(const void* buffer, size_t size) final;
 
-    size_t bytesWritten() const override { return SkToSizeT(this->byteCount); }
+    size_t bytesWritten() const final { return SkToSizeT(this->byteCount); }
 
-    /** Processes input, adding it to the digest. Calling this after finish is undefined. */
-    void update(const uint8_t* input, size_t length);
+    /** Alias for write() */
+    void update(const uint8_t* b, size_t l) { (void)this->write(b, l); }
 
     struct Digest {
         uint8_t data[16];
         bool operator ==(Digest const& other) const {
             return 0 == memcmp(data, other.data, sizeof(data));
         }
-        bool operator !=(Digest const& other) const {
-            return 0 != memcmp(data, other.data, sizeof(data));
-        }
+        bool operator !=(Digest const& other) const { return !(*this == other); }
     };
 
     /** Computes and returns the digest. */
     void finish(Digest& digest);
 
 private:
-    // number of bytes, modulo 2^64
-    uint64_t byteCount;
-
-    // state (ABCD)
-    uint32_t state[4];
-
-    // input buffer
-    uint8_t buffer[64];
+    uint64_t byteCount;  // number of bytes, modulo 2^64
+    uint32_t state[4];   // state (ABCD)
+    uint8_t buffer[64];  // input buffer
 };
 
 #endif