Merged from silc_1_0_branch (second merge).
[silc.git] / lib / silcutil / silctypes.h
index 7c6bdd514b29437993bd7fd99669e5f6436f9fd5..c763f8fa3b6be5fa1b6f613bbc0a926cdd6d2354 100644 (file)
 #endif
 /***/
 
-/* Define offsetof */
-#ifndef offsetof
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-#endif
+#define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 
 #if SILC_SIZEOF_SHORT > 2
 #error "size of the short must be 2 bytes"
@@ -219,8 +216,8 @@ typedef signed long SilcInt64;
 typedef unsigned long long SilcUInt64;
 typedef signed long long SilcInt64;
 #else
-typedef SilcUInt32 SilcUInt64; /* XXX Use Windows's own 64 bit types */
-typedef SilcInt32 SilcInt64;
+typedef unsigned __int64 SilcUInt64;
+typedef signed __int64 SilcInt64;
 #endif
 #else
 typedef SilcUInt32 SilcUInt64;
@@ -248,9 +245,9 @@ typedef SilcUInt32 * void *;
 
 /* Macros */
 
-#define GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24    \
-                   | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
-                   | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
+#define SILC_GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24       \
+                   | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)            \
+                   | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)             \
                    | ((SilcUInt32)(SilcUInt8)(cp)[3])
 
 /****d* silcutil/SILCTypes/SILC_GET16_MSB
@@ -266,8 +263,10 @@ typedef SilcUInt32 * void *;
  * SOURCE
  */
 #define SILC_GET16_MSB(l, cp)                          \
+do {                                                   \
        (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)     \
-           | ((SilcUInt32)(SilcUInt8)(cp)[1])
+           | ((SilcUInt32)(SilcUInt8)(cp)[1]);         \
+} while(0)
 /***/
 
 /****d* silcutil/SILCTypes/SILC_GET32_MSB
@@ -283,10 +282,12 @@ typedef SilcUInt32 * void *;
  * SOURCE
  */
 #define SILC_GET32_MSB(l, cp)                          \
+do {                                                   \
        (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24    \
            | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
            | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
-           | ((SilcUInt32)(SilcUInt8)(cp)[3])
+           | ((SilcUInt32)(SilcUInt8)(cp)[3]);         \
+} while(0)
 /***/
 
 /****d* silcutil/SILCTypes/SILC_GET64_MSB
@@ -301,9 +302,11 @@ typedef SilcUInt32 * void *;
  *
  * SOURCE
  */
-#define SILC_GET64_MSB(l, cp)                          \
-       (l) = ((((SilcUInt64)GET_WORD((cp))) << 32) |   \
-             ((SilcUInt64)GET_WORD((cp) + 4)))
+#define SILC_GET64_MSB(l, cp)                                  \
+do {                                                           \
+       (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) |      \
+             ((SilcUInt64)SILC_GET_WORD((cp) + 4)));           \
+} while(0)
 /***/
 
 /****d* silcutil/SILCTypes/SILC_GET16_LSB
@@ -319,8 +322,10 @@ typedef SilcUInt32 * void *;
  * SOURCE
  */
 #define SILC_GET16_LSB(l, cp)                          \
+do {                                                   \
        (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
-           | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)
+           | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8);    \
+} while(0)
 /***/
 
 /****d* silcutil/SILCTypes/SILC_GET32_LSB
@@ -336,10 +341,12 @@ typedef SilcUInt32 * void *;
  * SOURCE
  */
 #define SILC_GET32_LSB(l, cp)                          \
+do {                                                   \
        (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
            | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
            | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
-           | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
+           | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24);   \
+} while(0)
 
 /* Same as upper but XOR the result always. Special purpose macro. */
 #define SILC_GET32_X_LSB(l, cp)                                \
@@ -362,8 +369,10 @@ typedef SilcUInt32 * void *;
  * SOURCE
  */
 #define SILC_PUT16_MSB(l, cp)                  \
-       (cp)[0] = l >> 8;                       \
-       (cp)[1] = l;
+do {                                           \
+       (cp)[0] = (SilcUInt8)((l) >> 8);        \
+       (cp)[1] = (SilcUInt8)(l);               \
+} while(0)
 /***/
 
 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
@@ -379,10 +388,12 @@ typedef SilcUInt32 * void *;
  * SOURCE
  */
 #define SILC_PUT32_MSB(l, cp)                  \
-       (cp)[0] = l >> 24;                      \
-       (cp)[1] = l >> 16;                      \
-       (cp)[2] = l >> 8;                       \
-       (cp)[3] = l;
+do {                                           \
+       (cp)[0] = (SilcUInt8)((l) >> 24);       \
+       (cp)[1] = (SilcUInt8)((l) >> 16);       \
+       (cp)[2] = (SilcUInt8)((l) >> 8);        \
+       (cp)[3] = (SilcUInt8)(l);               \
+} while(0)
 /***/
 
 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
@@ -417,8 +428,10 @@ do {                                                               \
  * SOURCE
  */
 #define SILC_PUT16_LSB(l, cp)                  \
-       (cp)[0] = l;                            \
-       (cp)[1] = l >> 8;
+do  {                                          \
+       (cp)[0] = (SilcUInt8)(l);               \
+       (cp)[1] = (SilcUInt8)((l) >> 8);        \
+} while(0)
 /***/
 
 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
@@ -434,10 +447,48 @@ do {                                                              \
  * SOURCE
  */
 #define SILC_PUT32_LSB(l, cp)                  \
-       (cp)[0] = l;                            \
-       (cp)[1] = l >> 8;                       \
-       (cp)[2] = l >> 16;                      \
-       (cp)[3] = l >> 24;
+do {                                           \
+       (cp)[0] = (SilcUInt8)(l);               \
+       (cp)[1] = (SilcUInt8)((l) >> 8);        \
+       (cp)[2] = (SilcUInt8)((l) >> 16);       \
+       (cp)[3] = (SilcUInt8)((l) >> 24);       \
+} while(0)
+/***/
+
+/****d* silcutil/SILCTypes/SILC_SWAB_16
+ *
+ * NAME
+ *
+ *    #define SILC_SWAB_16 ...
+ *
+ * DESCRIPTION
+ *
+ *    Swabs 16-bit unsigned integer byte order.
+ *
+ * SOURCE
+ */
+#define SILC_SWAB_16(l)                                                \
+  ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) |        \
+               (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
+/***/
+
+/****d* silcutil/SILCTypes/SILC_SWAB_32
+ *
+ * NAME
+ *
+ *    #define SILC_SWAB_32 ...
+ *
+ * DESCRIPTION
+ *
+ *    Swabs 32-bit unsigned integer byte order.
+ *
+ * SOURCE
+ */
+#define SILC_SWAB_32(l)                                                        \
+  ((SilcUInt32)(((SilcUInt32)(l) & (SilcUInt32)0x000000FFUL) << 24) |  \
+               (((SilcUInt32)(l) & (SilcUInt32)0x0000FF00UL) << 8)  |  \
+               (((SilcUInt32)(l) & (SilcUInt32)0x00FF0000UL) >> 8)  |  \
+               (((SilcUInt32)(l) & (SilcUInt32)0xFF000000UL) >> 24))
 /***/
 
 #endif /* SILCTYPES_H */