Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Unified Diff: third_party/libvpx/source/libvpx/md5_utils.c

Issue 1158913006: Move libvpx from DEPS to src (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add DEPS file with #include paths Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/libvpx/source/libvpx/md5_utils.c
diff --git a/base/md5.cc b/third_party/libvpx/source/libvpx/md5_utils.c
similarity index 56%
copy from base/md5.cc
copy to third_party/libvpx/source/libvpx/md5_utils.c
index 72c774d35546ae0fa4c5798174a95861d5460eaf..8fb26e2084b2331e4314576dcdd365f332ab801c 100644
--- a/base/md5.cc
+++ b/third_party/libvpx/source/libvpx/md5_utils.c
@@ -1,9 +1,3 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// The original file was copied from sqlite, and was in the public domain.
-
/*
* This code implements the MD5 message-digest algorithm.
* The algorithm is due to Ron Rivest. This code was
@@ -19,34 +13,131 @@
* MD5Context structure, pass it to MD5Init, call MD5Update as
* needed on buffers full of bytes, and then call MD5Final, which
* will fill a supplied 16-byte array with the digest.
+ *
+ * Changed so as no longer to depend on Colin Plumb's `usual.h' header
+ * definitions
+ * - Ian Jackson <ian@chiark.greenend.org.uk>.
+ * Still in the public domain.
*/
-#include "base/md5.h"
+#include <string.h> /* for memcpy() */
+
+#include "md5_utils.h"
-#include <stddef.h>
+void
+byteSwap(UWORD32 *buf, unsigned words) {
+ md5byte *p;
-namespace {
+ /* Only swap bytes for big endian machines */
+ int i = 1;
-struct Context {
- uint32_t buf[4];
- uint32_t bits[2];
- uint8_t in[64];
-};
+ if (*(char *)&i == 1)
+ return;
+
+ p = (md5byte *)buf;
+
+ do {
+ *buf++ = (UWORD32)((unsigned)p[3] << 8 | p[2]) << 16 |
+ ((unsigned)p[1] << 8 | p[0]);
+ p += 4;
+ } while (--words);
+}
/*
- * Note: this code is harmless on little-endian machines.
+ * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
+ * initialization constants.
*/
-void byteReverse(uint8_t* buf, unsigned longs) {
- do {
- uint32_t temp = static_cast<uint32_t>(
- static_cast<unsigned>(buf[3]) << 8 |
- buf[2]) << 16 |
- (static_cast<unsigned>(buf[1]) << 8 | buf[0]);
- *reinterpret_cast<uint32_t*>(buf) = temp;
- buf += 4;
- } while (--longs);
+void
+MD5Init(struct MD5Context *ctx) {
+ ctx->buf[0] = 0x67452301;
+ ctx->buf[1] = 0xefcdab89;
+ ctx->buf[2] = 0x98badcfe;
+ ctx->buf[3] = 0x10325476;
+
+ ctx->bytes[0] = 0;
+ ctx->bytes[1] = 0;
+}
+
+/*
+ * Update context to reflect the concatenation of another buffer full
+ * of bytes.
+ */
+void
+MD5Update(struct MD5Context *ctx, md5byte const *buf, unsigned len) {
+ UWORD32 t;
+
+ /* Update byte count */
+
+ t = ctx->bytes[0];
+
+ if ((ctx->bytes[0] = t + len) < t)
+ ctx->bytes[1]++; /* Carry from low to high */
+
+ t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
+
+ if (t > len) {
+ memcpy((md5byte *)ctx->in + 64 - t, buf, len);
+ return;
+ }
+
+ /* First chunk is an odd size */
+ memcpy((md5byte *)ctx->in + 64 - t, buf, t);
+ byteSwap(ctx->in, 16);
+ MD5Transform(ctx->buf, ctx->in);
+ buf += t;
+ len -= t;
+
+ /* Process data in 64-byte chunks */
+ while (len >= 64) {
+ memcpy(ctx->in, buf, 64);
+ byteSwap(ctx->in, 16);
+ MD5Transform(ctx->buf, ctx->in);
+ buf += 64;
+ len -= 64;
+ }
+
+ /* Handle any remaining bytes of data. */
+ memcpy(ctx->in, buf, len);
+}
+
+/*
+ * Final wrapup - pad to 64-byte boundary with the bit pattern
+ * 1 0* (64-bit count of bits processed, MSB-first)
+ */
+void
+MD5Final(md5byte digest[16], struct MD5Context *ctx) {
+ int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
+ md5byte *p = (md5byte *)ctx->in + count;
+
+ /* Set the first char of padding to 0x80. There is always room. */
+ *p++ = 0x80;
+
+ /* Bytes of padding needed to make 56 bytes (-8..55) */
+ count = 56 - 1 - count;
+
+ if (count < 0) { /* Padding forces an extra block */
+ memset(p, 0, count + 8);
+ byteSwap(ctx->in, 16);
+ MD5Transform(ctx->buf, ctx->in);
+ p = (md5byte *)ctx->in;
+ count = 56;
+ }
+
+ memset(p, 0, count);
+ byteSwap(ctx->in, 14);
+
+ /* Append length in bits and transform */
+ ctx->in[14] = ctx->bytes[0] << 3;
+ ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
+ MD5Transform(ctx->buf, ctx->in);
+
+ byteSwap(ctx->buf, 4);
+ memcpy(digest, ctx->buf, 16);
+ memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
}
+#ifndef ASM_MD5
+
/* The four core functions - F1 is optimized somewhat */
/* #define F1(x, y, z) (x & y | ~x & z) */
@@ -56,16 +147,17 @@ void byteReverse(uint8_t* buf, unsigned longs) {
#define F4(x, y, z) (y ^ (x | ~z))
/* This is the central step in the MD5 algorithm. */
-#define MD5STEP(f, w, x, y, z, data, s) \
- (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
+#define MD5STEP(f,w,x,y,z,in,s) \
+ (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
-void MD5Transform(uint32_t buf[4], const uint32_t in[16]) {
- uint32_t a, b, c, d;
+void
+MD5Transform(UWORD32 buf[4], UWORD32 const in[16]) {
+ register UWORD32 a, b, c, d;
a = buf[0];
b = buf[1];
@@ -146,154 +238,4 @@ void MD5Transform(uint32_t buf[4], const uint32_t in[16]) {
buf[3] += d;
}
-} // namespace
-
-namespace base {
-
-/*
- * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
- * initialization constants.
- */
-void MD5Init(MD5Context* context) {
- struct Context* ctx = reinterpret_cast<struct Context*>(context);
- ctx->buf[0] = 0x67452301;
- ctx->buf[1] = 0xefcdab89;
- ctx->buf[2] = 0x98badcfe;
- ctx->buf[3] = 0x10325476;
- ctx->bits[0] = 0;
- ctx->bits[1] = 0;
-}
-
-/*
- * Update context to reflect the concatenation of another buffer full
- * of bytes.
- */
-void MD5Update(MD5Context* context, const StringPiece& data) {
- struct Context* ctx = reinterpret_cast<struct Context*>(context);
- const uint8_t* buf = reinterpret_cast<const uint8_t*>(data.data());
- size_t len = data.size();
-
- /* Update bitcount */
-
- uint32_t t = ctx->bits[0];
- if ((ctx->bits[0] = t + (static_cast<uint32_t>(len) << 3)) < t)
- ctx->bits[1]++; /* Carry from low to high */
- ctx->bits[1] += static_cast<uint32_t>(len >> 29);
-
- t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
-
- /* Handle any leading odd-sized chunks */
-
- if (t) {
- uint8_t* p = static_cast<uint8_t*>(ctx->in + t);
-
- t = 64 - t;
- if (len < t) {
- memcpy(p, buf, len);
- return;
- }
- memcpy(p, buf, t);
- byteReverse(ctx->in, 16);
- MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in));
- buf += t;
- len -= t;
- }
-
- /* Process data in 64-byte chunks */
-
- while (len >= 64) {
- memcpy(ctx->in, buf, 64);
- byteReverse(ctx->in, 16);
- MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in));
- buf += 64;
- len -= 64;
- }
-
- /* Handle any remaining bytes of data. */
-
- memcpy(ctx->in, buf, len);
-}
-
-/*
- * Final wrapup - pad to 64-byte boundary with the bit pattern
- * 1 0* (64-bit count of bits processed, MSB-first)
- */
-void MD5Final(MD5Digest* digest, MD5Context* context) {
- struct Context* ctx = reinterpret_cast<struct Context*>(context);
- unsigned count;
- uint8_t* p;
-
- /* Compute number of bytes mod 64 */
- count = (ctx->bits[0] >> 3) & 0x3F;
-
- /* Set the first char of padding to 0x80. This is safe since there is
- always at least one byte free */
- p = ctx->in + count;
- *p++ = 0x80;
-
- /* Bytes of padding needed to make 64 bytes */
- count = 64 - 1 - count;
-
- /* Pad out to 56 mod 64 */
- if (count < 8) {
- /* Two lots of padding: Pad the first block to 64 bytes */
- memset(p, 0, count);
- byteReverse(ctx->in, 16);
- MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in));
-
- /* Now fill the next block with 56 bytes */
- memset(ctx->in, 0, 56);
- } else {
- /* Pad block to 56 bytes */
- memset(p, 0, count - 8);
- }
- byteReverse(ctx->in, 14);
-
- /* Append length in bits and transform */
- memcpy(&ctx->in[14 * sizeof(ctx->bits[0])], &ctx->bits[0],
- sizeof(ctx->bits[0]));
- memcpy(&ctx->in[15 * sizeof(ctx->bits[1])], &ctx->bits[1],
- sizeof(ctx->bits[1]));
-
- MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in));
- byteReverse(reinterpret_cast<uint8_t*>(ctx->buf), 4);
- memcpy(digest->a, ctx->buf, 16);
- memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
-}
-
-void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) {
- /* MD5Final mutates the MD5Context*. Make a copy for generating the
- intermediate value. */
- MD5Context context_copy;
- memcpy(&context_copy, context, sizeof(context_copy));
- MD5Final(digest, &context_copy);
-}
-
-std::string MD5DigestToBase16(const MD5Digest& digest) {
- static char const zEncode[] = "0123456789abcdef";
-
- std::string ret;
- ret.resize(32);
-
- for (int i = 0, j = 0; i < 16; i++, j += 2) {
- uint8_t a = digest.a[i];
- ret[j] = zEncode[(a >> 4) & 0xf];
- ret[j + 1] = zEncode[a & 0xf];
- }
- return ret;
-}
-
-void MD5Sum(const void* data, size_t length, MD5Digest* digest) {
- MD5Context ctx;
- MD5Init(&ctx);
- MD5Update(&ctx, StringPiece(reinterpret_cast<const char*>(data), length));
- MD5Final(digest, &ctx);
-}
-
-std::string MD5String(const StringPiece& str) {
- MD5Digest digest;
- MD5Sum(str.data(), str.length(), &digest);
- return MD5DigestToBase16(digest);
-}
-
-} // namespace base
+#endif

Powered by Google App Engine
This is Rietveld 408576698