Print this page
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474

@@ -1,12 +1,10 @@
 /*
- * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
-#pragma ident   "%Z%%M% %I%     %E% SMI"
-
 /*
  * MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
  */
 
 /*

@@ -36,10 +34,14 @@
 #include <sys/sunddi.h>
 #else
 #include <strings.h>
 #endif /* _KERNEL */
 
+#if defined(__i386) || defined(__amd64)
+#define UNALIGNED_POINTERS_PERMITTED
+#endif
+
 #include <sys/md4.h>
 
 /*
  * Constants for MD4Transform routine.
  */

@@ -260,38 +262,44 @@
 /*
  * Encodes input (uint32_t) into output (unsigned char). Assumes len is
  * a multiple of 4.
  */
 static void
-Encode(output, input, len)
-        unsigned char *output;
-        uint32_t *input;
-        unsigned int len;
+Encode(unsigned char *output, uint32_t *input, unsigned int len)
 {
         unsigned int i, j;
 
         for (i = 0, j = 0; j < len; i++, j += 4) {
+#if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED)
+                *(uint32_t *)&output[j] = input[i];
+#else
+                /* endian-independent code */
                 output[j] = (unsigned char)(input[i] & 0xff);
                 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
                 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
                 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
+#endif  /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */
         }
 }
 
 /*
  * Decodes input (unsigned char) into output (uint32_t). Assumes len is
  * a multiple of 4.
  */
 static void
-Decode(output, input, len)
-        uint32_t *output;
-        unsigned char *input;
-        unsigned int len;
+Decode(uint32_t *output, unsigned char *input, unsigned int len)
 {
         unsigned int i, j;
 
-        for (i = 0, j = 0; j < len; i++, j += 4)
+        for (i = 0, j = 0; j < len; i++, j += 4) {
+#if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED)
+                output[i] = *(uint32_t *)&input[j];
+#else
+                /* endian-independent code */
                 output[i] = ((uint32_t)input[j]) |
                         (((uint32_t)input[j+1]) << 8) |
                         (((uint32_t)input[j+2]) << 16) |
                         (((uint32_t)input[j+3]) << 24);
+#endif  /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */
+        }
+
 }