Browse Source

further simplify endianness handling

Julius Pfrommer 8 years ago
parent
commit
3a8af99c5a
5 changed files with 29 additions and 38 deletions
  1. 0 5
      doc/building.rst
  2. 11 12
      include/ua_config.h.in
  3. 8 0
      include/ua_constants.h
  4. 9 16
      src/ua_types_encoding_binary.c
  5. 1 5
      src/ua_util.h

+ 0 - 5
doc/building.rst

@@ -105,11 +105,6 @@ Build Options
 Further options that are not inherited from the CMake configuration are defined
 Further options that are not inherited from the CMake configuration are defined
 in ua_config.h. Usually there is no need to adjust them.
 in ua_config.h. Usually there is no need to adjust them.
 
 
-**UA_NON_LITTLEENDIAN_ARCHITECTURE**
-   Big-endian or mixed endian platform
-**UA_MIXED_ENDIAN**
-   Mixed-endian platform (e.g., ARM7TDMI)
-
 UA_BUILD_* group
 UA_BUILD_* group
 ~~~~~~~~~~~~~
 ~~~~~~~~~~~~~
 
 

+ 11 - 12
include/ua_config.h.in

@@ -82,7 +82,8 @@
 /**
 /**
  * Integer Endianness
  * Integer Endianness
  * ------------------ */
  * ------------------ */
-#if defined(__LITTLE_ENDIAN__) || defined(_WIN32)
+#if defined(_WIN32) || (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
+                        && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
 # define htole16(x) (x)
 # define htole16(x) (x)
 # define htole32(x) (x)
 # define htole32(x) (x)
 # define htole64(x) (x)
 # define htole64(x) (x)
@@ -145,17 +146,15 @@
      !defined(__ORDER_LITTLE_ENDIAN__) || !defined(__ORDER_BIG_ENDIAN__)
      !defined(__ORDER_LITTLE_ENDIAN__) || !defined(__ORDER_BIG_ENDIAN__)
 #  define UA_ENCODING_FLOAT_GENERIC
 #  define UA_ENCODING_FLOAT_GENERIC
 #  warning Unknown float representation. Use a slow manual IEEE 754 conversion.
 #  warning Unknown float representation. Use a slow manual IEEE 754 conversion.
-# else
-#  if !(((__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
-        ((__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)))
-#   if ((__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)) || \
-       ((__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__))
-#    define UA_ENCODING_FLOAT_SWAP
-#   else
-#    define UA_ENCODING_FLOAT_GENERIC
-#    warning Unknown float representation. Use a slow manual IEEE 754 conversion.
-#   endif
-#  endif
+/* Replace UA_ENCODING_FLOAT_GENERIC with the following section (and adjust
+   accordingly to your archtecture) if little-endian IEEE 754 can be achieved
+   with a simple byte-swap. For example for middle-endian encoding on some
+   ARM CPUs. */
+/* #define UA_ENCODING_FLOAT_SWAP */
+/* #define UA_swap32(u32) ((u32 >> 24) | ((u32 << 8) & 0x00FF0000) | ((u32 >> 8) & 0x0000FF00) | (u32 << 24)) */
+/* #define UA_swap64(u64) ((u64 >> 56) | ((u64 << 40) & 0x00FF000000000000) | ((u64 << 24) & 0x0000FF0000000000) | \ */
+/*                        ((u64 << 8) & 0x000000FF00000000) | ((u64 >> 8) & 0x00000000FF000000) |                 \ */
+/*                        ((u64 >> 24) & 0x0000000000FF0000) | ((u64 >> 40) & 0x000000000000FF00) | (u64 << 56)) */
 # endif
 # endif
 #endif
 #endif
 
 

+ 8 - 0
include/ua_constants.h

@@ -65,6 +65,14 @@ typedef enum {
 #define UA_ACCESSLEVELMASK_HISTORYREAD 0x4
 #define UA_ACCESSLEVELMASK_HISTORYREAD 0x4
 #define UA_ACCESSLEVELMASK_HISTORYWRITE 0x08
 #define UA_ACCESSLEVELMASK_HISTORYWRITE 0x08
 #define UA_ACCESSLEVELMASK_SEMANTICCHANGE 0x10
 #define UA_ACCESSLEVELMASK_SEMANTICCHANGE 0x10
+
+/**
+* Encoding Offsets
+* ----------------
+* Subtract from the typeid of the encoding nodeids to get to the type
+* definition. */
+#define UA_ENCODINGOFFSET_XML 1
+#define UA_ENCODINGOFFSET_BINARY 2
     
     
 /**
 /**
  * .. _statuscodes:
  * .. _statuscodes:

+ 9 - 16
src/ua_types_encoding_binary.c

@@ -1,6 +1,6 @@
-#include "ua_types_encoding_binary.h"
-#include "ua_types_generated.h"
 #include "ua_util.h"
 #include "ua_util.h"
+#include "ua_types_generated.h"
+#include "ua_types_encoding_binary.h"
 
 
 /* All de- and encoding functions have the same signature up to the pointer type.
 /* All de- and encoding functions have the same signature up to the pointer type.
    So we can use a jump-table to switch into member types. */
    So we can use a jump-table to switch into member types. */
@@ -97,8 +97,7 @@ UInt16_encodeBinary(UA_UInt16 const *src, bufpos pos, bufend end) {
         return UA_STATUSCODE_BADENCODINGERROR;
         return UA_STATUSCODE_BADENCODINGERROR;
 #ifndef UA_ENCODING_INTEGER_GENERIC
 #ifndef UA_ENCODING_INTEGER_GENERIC
     UA_UInt16 le_uint16 = htole16(*src);
     UA_UInt16 le_uint16 = htole16(*src);
-    src = &le_uint16;
-    memcpy(*pos, src, sizeof(UA_UInt16));
+    memcpy(*pos, &le_uint16, sizeof(UA_UInt16));
 #else
 #else
     UA_encode16(*src, *pos);
     UA_encode16(*src, *pos);
 #endif
 #endif
@@ -137,8 +136,7 @@ UInt32_encodeBinary(UA_UInt32 const *src, bufpos pos, bufend end) {
         return UA_STATUSCODE_BADENCODINGERROR;
         return UA_STATUSCODE_BADENCODINGERROR;
 #ifndef UA_ENCODING_INTEGER_GENERIC
 #ifndef UA_ENCODING_INTEGER_GENERIC
     UA_UInt32 le_uint32 = htole32(*src);
     UA_UInt32 le_uint32 = htole32(*src);
-    src = &le_uint32;
-    memcpy(*pos, src, sizeof(UA_UInt32));
+    memcpy(*pos, &le_uint32, sizeof(UA_UInt32));
 #else
 #else
     UA_encode32(*src, *pos);
     UA_encode32(*src, *pos);
 #endif
 #endif
@@ -187,8 +185,7 @@ UInt64_encodeBinary(UA_UInt64 const *src, bufpos pos, bufend end) {
         return UA_STATUSCODE_BADENCODINGERROR;
         return UA_STATUSCODE_BADENCODINGERROR;
 #ifndef UA_ENCODING_INTEGER_GENERIC
 #ifndef UA_ENCODING_INTEGER_GENERIC
     UA_UInt64 le_uint64 = htole64(*src);
     UA_UInt64 le_uint64 = htole64(*src);
-    src = &le_uint64;
-    memcpy(*pos, src, sizeof(UA_UInt64));
+    memcpy(*pos, &le_uint64, sizeof(UA_UInt64));
 #else
 #else
     UA_encode64(*src, *pos);
     UA_encode64(*src, *pos);
 #endif
 #endif
@@ -243,15 +240,11 @@ DateTime_decodeBinary(bufpos pos, bufend end, UA_DateTime *dst) {
 #else
 #else
 
 
 #ifdef UA_ENCODING_FLOAT_SWAP
 #ifdef UA_ENCODING_FLOAT_SWAP
-#define swap32(u32) ((u32 >> 24) | ((u32 << 8) & 0x00FF0000) | ((u32 >> 8) & 0x0000FF00) | (u32 << 24))
-#define swap64(u64) ((u64 >> 56) | ((u64 << 40) & 0x00FF000000000000) | ((u64 << 24) & 0x0000FF0000000000) | \
-                     ((u64 << 8) & 0x000000FF00000000) | ((u64 >> 8) & 0x00000000FF000000) |                 \
-                     ((u64 >> 24) & 0x0000000000FF0000) | ((u64 >> 40) & 0x000000000000FF00) | (u64 << 56))
 
 
 static UA_StatusCode
 static UA_StatusCode
 Float_encodeBinary(UA_Float const *src, bufpos pos, bufend end) {
 Float_encodeBinary(UA_Float const *src, bufpos pos, bufend end) {
     const UA_UInt32 *f = (const UA_UInt32*)src;
     const UA_UInt32 *f = (const UA_UInt32*)src;
-    UA_UInt32 encoded = swap32(*f);
+    UA_UInt32 encoded = UA_swap32(*f);
     return UInt32_encodeBinary(&encoded, pos, end);
     return UInt32_encodeBinary(&encoded, pos, end);
 }
 }
 
 
@@ -260,7 +253,7 @@ static UA_StatusCode Float_decodeBinary(bufpos pos, bufend end, UA_Float *dst) {
     UA_StatusCode retval = UInt32_decodeBinary(pos, end, &decoded);
     UA_StatusCode retval = UInt32_decodeBinary(pos, end, &decoded);
     if(retval != UA_STATUSCODE_GOOD)
     if(retval != UA_STATUSCODE_GOOD)
         return retval;
         return retval;
-    decoded = swap32(decoded);
+    decoded = UA_swap32(decoded);
     *dst = *(UA_Float*)decoded;
     *dst = *(UA_Float*)decoded;
     return UA_STATUSCODE_GOOD;
     return UA_STATUSCODE_GOOD;
 }
 }
@@ -268,7 +261,7 @@ static UA_StatusCode Float_decodeBinary(bufpos pos, bufend end, UA_Float *dst) {
 static UA_StatusCode
 static UA_StatusCode
 Double_encodeBinary(UA_Double const *src, bufpos pos, bufend end) {
 Double_encodeBinary(UA_Double const *src, bufpos pos, bufend end) {
     const UA_UInt64 *f = (const UA_UInt64*)src;
     const UA_UInt64 *f = (const UA_UInt64*)src;
-    UA_UInt64 encoded = swap64(*f);
+    UA_UInt64 encoded = UA_swap64(*f);
     return UInt64_encodeBinary(&encoded, pos, end);
     return UInt64_encodeBinary(&encoded, pos, end);
 }
 }
 
 
@@ -277,7 +270,7 @@ static UA_StatusCode Double_decodeBinary(bufpos pos, bufend end, UA_Double *dst)
     UA_StatusCode retval = UInt64_decodeBinary(pos, end, &decoded);
     UA_StatusCode retval = UInt64_decodeBinary(pos, end, &decoded);
     if(retval != UA_STATUSCODE_GOOD)
     if(retval != UA_STATUSCODE_GOOD)
         return retval;
         return retval;
-    decoded = swap64(decoded);
+    decoded = UA_swap64(decoded);
     *dst = *(UA_Double*)decoded;
     *dst = *(UA_Double*)decoded;
     return UA_STATUSCODE_GOOD;
     return UA_STATUSCODE_GOOD;
 }
 }

+ 1 - 5
src/ua_util.h

@@ -3,11 +3,7 @@
 
 
 #include "ua_config.h"
 #include "ua_config.h"
 
 
-/* Subtract from nodeids to get from the encoding to the content */
-#define UA_ENCODINGOFFSET_XML 1
-#define UA_ENCODINGOFFSET_BINARY 2
-
-#include <assert.h> // assert
+#include <assert.h>
 #define UA_assert(ignore) assert(ignore)
 #define UA_assert(ignore) assert(ignore)
 
 
 /*********************/
 /*********************/