Bug 798172 - part 3 - convert SHA1.cpp to use Endian.h; r=Waldo
authorNathan Froyd <froydnj@mozilla.com>
Wed, 30 Jan 2013 16:47:00 -0500
changeset 138545 f9878fed7d54bd5fd7ab2bf32400610a1c11bc03
parent 138544 ea8ae9085ea16cf6931c7496f7a89b835dc52557
child 138546 fa1191b1d320dfa0cfd400b65d4808ebd91d5ea2
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs798172
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 798172 - part 3 - convert SHA1.cpp to use Endian.h; r=Waldo
mfbt/SHA1.cpp
--- a/mfbt/SHA1.cpp
+++ b/mfbt/SHA1.cpp
@@ -1,48 +1,28 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/Assertions.h"
+#include "mozilla/Endian.h"
 #include "mozilla/SHA1.h"
 
 #include <string.h>
 
-// FIXME: We should probably create a more complete mfbt/Endian.h. This assumes
-// that any compiler that doesn't define these macros is little endian.
-#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
-#  if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-#    define MOZ_IS_LITTLE_ENDIAN
-#  endif
-#else
-#  define MOZ_IS_LITTLE_ENDIAN
-#endif
-
+using mozilla::NativeEndian;
 using mozilla::SHA1Sum;
 
 static inline uint32_t
 SHA_ROTL(uint32_t t, uint32_t n)
 {
   MOZ_ASSERT(n < 32);
   return (t << n) | (t >> (32 - n));
 }
 
-static inline unsigned
-SHA_HTONL(unsigned x)
-{
-#ifdef MOZ_IS_LITTLE_ENDIAN
-  const unsigned int mask = 0x00FF00FF;
-  x = (x << 16) | (x >> 16);
-  return ((x & mask) << 8) | ((x >> 8) & mask);
-#else
-  return x;
-#endif
-}
-
 static void
 shaCompress(volatile unsigned* X, const uint32_t* datain);
 
 #define SHA_F1(X, Y, Z) ((((Y) ^ (Z)) & (X)) ^ (Z))
 #define SHA_F2(X, Y, Z) ((X) ^ (Y) ^ (Z))
 #define SHA_F3(X, Y, Z) (((X) & (Y)) | ((Z) & ((X) | (Y))))
 #define SHA_F4(X, Y, Z) ((X) ^ (Y) ^ (Z))
 
@@ -155,26 +135,26 @@ SHA1Sum::finish(SHA1Sum::Hash& hashOut)
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
 
   /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length in bits. */
   update(bulk_pad, (((55 + 64) - lenB) & 63) + 1);
   MOZ_ASSERT((uint32_t(size) & 63) == 56);
 
   /* Convert size from bytes to bits. */
   size2 <<= 3;
-  u.w[14] = SHA_HTONL(uint32_t(size2 >> 32));
-  u.w[15] = SHA_HTONL(uint32_t(size2));
+  u.w[14] = NativeEndian::swapToBigEndian(uint32_t(size2 >> 32));
+  u.w[15] = NativeEndian::swapToBigEndian(uint32_t(size2));
   shaCompress(&H[H2X], u.w);
 
   /* Output hash. */
-  u.w[0] = SHA_HTONL(H[0]);
-  u.w[1] = SHA_HTONL(H[1]);
-  u.w[2] = SHA_HTONL(H[2]);
-  u.w[3] = SHA_HTONL(H[3]);
-  u.w[4] = SHA_HTONL(H[4]);
+  u.w[0] = NativeEndian::swapToBigEndian(H[0]);
+  u.w[1] = NativeEndian::swapToBigEndian(H[1]);
+  u.w[2] = NativeEndian::swapToBigEndian(H[2]);
+  u.w[3] = NativeEndian::swapToBigEndian(H[3]);
+  u.w[4] = NativeEndian::swapToBigEndian(H[4]);
   memcpy(hashOut, u.w, 20);
   mDone = true;
 }
 
 /*
  *  SHA: Compression function, unrolled.
  *
  * Some operations in shaCompress are done as 5 groups of 16 operations.
@@ -238,17 +218,17 @@ shaCompress(volatile unsigned *X, const 
   a = SHA_ROTL(b, 5) + SHA_F1(c, d, e) + a + XW(n) + K0; c = SHA_ROTL(c, 30)
 #define SHA_RND2(a, b, c, d, e, n) \
   a = SHA_ROTL(b, 5) + SHA_F2(c, d, e) + a + XW(n) + K1; c = SHA_ROTL(c, 30)
 #define SHA_RND3(a, b, c, d, e, n) \
   a = SHA_ROTL(b, 5) + SHA_F3(c, d, e) + a + XW(n) + K2; c = SHA_ROTL(c, 30)
 #define SHA_RND4(a, b, c, d, e, n) \
   a = SHA_ROTL(b ,5) + SHA_F4(c, d, e) + a + XW(n) + K3; c = SHA_ROTL(c, 30)
 
-#define LOAD(n) XW(n) = SHA_HTONL(inbuf[n])
+#define LOAD(n) XW(n) = NativeEndian::swapToBigEndian(inbuf[n])
 
   A = XH(0);
   B = XH(1);
   C = XH(2);
   D = XH(3);
   E = XH(4);
 
   LOAD(0);		   SHA_RND1(E,A,B,C,D, 0);