Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed bug in Endian.h and added testing

  • Loading branch information...
commit e2c59ceea0ebd5e798fd5e72ba857f8a124b47c3 1 parent f5cf444
authored February 09, 2012
41  util/Endian.h
@@ -15,6 +15,7 @@
15 15
 #define ENDIAN_H
16 16
 
17 17
 #include <stdint.h>
  18
+#include <stdbool.h>
18 19
 
19 20
 // ntohs() ntohl()
20 21
 #ifdef WIN32
@@ -23,6 +24,17 @@
23 24
     #include <arpa/inet.h>
24 25
 #endif
25 26
 
  27
+
  28
+static inline bool Endian_checkIsBigEndian()
  29
+{
  30
+    union {
  31
+        uint32_t i;
  32
+        char c[4];
  33
+    } bint = {0x01020304};
  34
+
  35
+    return bint.c[0] == 1; 
  36
+}
  37
+
26 38
 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN)
27 39
     #if (__BYTE_ORDER == __BIG_ENDIAN)
28 40
         #define Endian_isBigEndian() 1
@@ -30,15 +42,7 @@
30 42
         #define Endian_isBigEndian() 0
31 43
     #endif
32 44
 #else
33  
-    static uint32_t Endian_isBigEndian()
34  
-    {
35  
-        union {
36  
-            uint32_t i;
37  
-            char c[4];
38  
-        } bint = {0x01020304};
39  
-
40  
-        return bint.c[0] == 1; 
41  
-    }
  45
+    #define Endian_isBigEndian() Endian_checkIsBigEndian()
42 46
 #endif
43 47
 
44 48
 
@@ -53,12 +57,20 @@
53 57
 
54 58
 #ifdef htobe64
55 59
     #define Endian_hostToBigEndian64(x) htobe64(x)
  60
+    #define Endian_hostToBigEndian64_uses "htobe64"
56 61
 #else
57  
-    #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__bswap_64)
58  
-        #define Endian_hostToBigEndian64(x) __bswap_64(x)
59  
-    #elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN)
60  
-        #define Endian_hostToBigEndian64(x) (x)
61  
-    #else
  62
+    #if defined(__BYTE_ORDER)
  63
+        #if defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) && defined(__bswap_64)
  64
+            #define Endian_hostToBigEndian64(x) __bswap_64(x)
  65
+            #define Endian_hostToBigEndian64_uses "__bswap_64"
  66
+        #elif defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
  67
+            #define Endian_hostToBigEndian64(x) (x)
  68
+            #define Endian_hostToBigEndian64_uses "nop"
  69
+        #endif
  70
+    #endif
  71
+
  72
+    #ifndef Endian_hostToBigEndian64
  73
+        
62 74
         static inline uint64_t Endian_hostToBigEndian64(uint64_t input)
63 75
         {
64 76
             if (Endian_isBigEndian()) {
@@ -67,6 +79,7 @@
67 79
                 return input;
68 80
             }
69 81
         }
  82
+        #define Endian_hostToBigEndian64_uses "function"
70 83
     #endif
71 84
 #endif
72 85
 #define Endian_bigEndianToHost64(x) Endian_hostToBigEndian64(x)
49  util/test/Endian_test.c
... ...
@@ -0,0 +1,49 @@
  1
+/*
  2
+ * You may redistribute this program and/or modify it under the terms of
  3
+ * the GNU General Public License as published by the Free Software Foundation,
  4
+ * either version 3 of the License, or (at your option) any later version.
  5
+ *
  6
+ * This program is distributed in the hope that it will be useful,
  7
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9
+ * GNU General Public License for more details.
  10
+ *
  11
+ * You should have received a copy of the GNU General Public License
  12
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13
+ */
  14
+#include "crypto/Crypto.h"
  15
+#include "util/Endian.h"
  16
+
  17
+#include <stdio.h>
  18
+#include <assert.h>
  19
+
  20
+void printInfo()
  21
+{
  22
+    #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN)
  23
+        printf("This machine is defined as %s\n",
  24
+               Endian_isBigEndian() ? "big endian" : "little endian");
  25
+    #endif
  26
+
  27
+    printf("According to testing this machine is %s\n",
  28
+           Endian_checkIsBigEndian() ? "big endian" : "little endian");
  29
+
  30
+    printf("Endian_hostToBigEndian64() uses " Endian_hostToBigEndian64_uses "\n");
  31
+}
  32
+
  33
+int main()
  34
+{
  35
+    printInfo();
  36
+
  37
+    volatile uint32_t a =    0x01234567;
  38
+    volatile uint32_t a_be = 0x67452301;
  39
+
  40
+    volatile uint64_t b =    0x0123456789abcdef;
  41
+    volatile uint64_t b_be = 0xefcdab8967452301;
  42
+
  43
+    volatile uint16_t c =    0xcabe;
  44
+    volatile uint16_t c_be = 0xbeca;
  45
+
  46
+    assert(a == Endian_bigEndianToHost32(a_be));
  47
+    assert(b == Endian_bigEndianToHost64(b_be));
  48
+    assert(c == Endian_bigEndianToHost16(c_be));
  49
+}

0 notes on commit e2c59ce

Please sign in to comment.
Something went wrong with that request. Please try again.