changeset 1566:62a7d617e1ce 0.5.1

Make md5sum and sha1sum work on big endian systems.
author Rob Landley <rob@landley.net>
date Wed, 19 Nov 2014 21:38:00 -0600
parents a3500bd8b322
children 73eb55d21cde
files toys/lsb/md5sum.c
diffstat 1 files changed, 18 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/toys/lsb/md5sum.c	Wed Nov 19 16:55:12 2014 -0600
+++ b/toys/lsb/md5sum.c	Wed Nov 19 21:38:00 2014 -0600
@@ -168,19 +168,21 @@
   j = TT.count & 63;
   TT.count += len;
 
-  // Enough data to process a frame?
-  if ((j + len) > 63) {
-    i = 64-j;
-    memcpy(TT.buffer.c + j, data, i);
+  for (;;) {
+    // Grab next chunk of data, return if it's not enough to process a frame
+    i = 64 - j;
+    if (i>len) i = len;
+    memcpy(TT.buffer.c+j, data, i);
+    if (j+i != 64) break;
+
+    // Process a frame
+    if (IS_BIG_ENDIAN)
+      for (j=0; j<16; j++) TT.buffer.i[j] = SWAP_LE32(TT.buffer.i[j]);
     transform();
-    for ( ; i + 63 < len; i += 64) {
-      memcpy(TT.buffer.c, data + i, 64);
-      transform();
-    }
-    j = 0;
-  } else i = 0;
-  // Grab remaining chunk
-  memcpy(TT.buffer.c + j, data + i, len - i);
+    j=0;
+    data += i;
+    len -= i;
+  }
 }
 
 // Callback for loopfiles()
@@ -221,16 +223,15 @@
     hash_update(&buf, 1, transform);
     buf = 0;
   } while ((TT.count & 63) != 56);
-  if (sha1) count=bswap_64(count);
-  for (i = 0; i < 8; i++) TT.buffer.c[56+i] = count >> (8*i);
-  transform();
+  count = sha1 ? SWAP_BE64(count) : SWAP_LE64(count);
+  hash_update((void *)&count, 8, transform);
 
   if (sha1)
     for (i = 0; i < 20; i++)
       printf("%02x", 255&(TT.state[i>>2] >> ((3-(i & 3)) * 8)));
-  else for (i=0; i<4; i++) printf("%08x", SWAP_BE32(TT.state[i]));
+  else for (i=0; i<4; i++) printf("%08x", bswap_32(TT.state[i]));
 
-  // Wipe variables.  Cryptographer paranoia.
+  // Wipe variables. Cryptographer paranoia.
   memset(&TT, 0, sizeof(TT));
 
   printf((toys.optflags & FLAG_b) ? "\n" : "  %s\n", name);