diff options
author | Gabor X Toth <*@tg-x.net> | 2015-05-07 12:15:24 +0000 |
---|---|---|
committer | Gabor X Toth <*@tg-x.net> | 2015-05-07 12:15:24 +0000 |
commit | 26dbd65eff8dc627697a1ee5c3b755c2951ac013 (patch) | |
tree | 386c078b0fda7d71867dff43f7711123d53c6428 /src/util | |
parent | 229083a5e71624479fdc44a14a264fe07077e699 (diff) | |
download | gnunet-26dbd65eff8dc627697a1ee5c3b755c2951ac013.tar.gz gnunet-26dbd65eff8dc627697a1ee5c3b755c2951ac013.zip |
add GNUNET_(hton|ntoh).*_signed() functions
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/common_endian.c | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/src/util/common_endian.c b/src/util/common_endian.c index 218875e0c..4a8a01664 100644 --- a/src/util/common_endian.c +++ b/src/util/common_endian.c | |||
@@ -22,6 +22,7 @@ | |||
22 | * @file util/common_endian.c | 22 | * @file util/common_endian.c |
23 | * @brief endian conversion helpers | 23 | * @brief endian conversion helpers |
24 | * @author Christian Grothoff | 24 | * @author Christian Grothoff |
25 | * @author Gabor X Toth | ||
25 | */ | 26 | */ |
26 | 27 | ||
27 | #include "platform.h" | 28 | #include "platform.h" |
@@ -29,25 +30,27 @@ | |||
29 | 30 | ||
30 | #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) | 31 | #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) |
31 | 32 | ||
33 | |||
32 | uint64_t | 34 | uint64_t |
33 | GNUNET_ntohll (uint64_t n) | 35 | GNUNET_htonll (uint64_t n) |
34 | { | 36 | { |
35 | #if __BYTE_ORDER == __BIG_ENDIAN | 37 | #if __BYTE_ORDER == __BIG_ENDIAN |
36 | return n; | 38 | return n; |
37 | #elif __BYTE_ORDER == __LITTLE_ENDIAN | 39 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
38 | return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32); | 40 | return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32); |
39 | #else | 41 | #else |
40 | #error byteorder undefined | 42 | #error byteorder undefined |
41 | #endif | 43 | #endif |
42 | } | 44 | } |
43 | 45 | ||
46 | |||
44 | uint64_t | 47 | uint64_t |
45 | GNUNET_htonll (uint64_t n) | 48 | GNUNET_ntohll (uint64_t n) |
46 | { | 49 | { |
47 | #if __BYTE_ORDER == __BIG_ENDIAN | 50 | #if __BYTE_ORDER == __BIG_ENDIAN |
48 | return n; | 51 | return n; |
49 | #elif __BYTE_ORDER == __LITTLE_ENDIAN | 52 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
50 | return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32); | 53 | return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32); |
51 | #else | 54 | #else |
52 | #error byteorder undefined | 55 | #error byteorder undefined |
53 | #endif | 56 | #endif |
@@ -90,5 +93,47 @@ GNUNET_ntoh_double (double d) | |||
90 | } | 93 | } |
91 | 94 | ||
92 | 95 | ||
96 | uint64_t | ||
97 | GNUNET_htonll_signed (int64_t n) | ||
98 | { | ||
99 | return GNUNET_htonll (n - INT64_MIN); | ||
100 | } | ||
101 | |||
102 | |||
103 | int64_t | ||
104 | GNUNET_ntohll_signed (uint64_t n) | ||
105 | { | ||
106 | return GNUNET_ntohll (n) + INT64_MIN; | ||
107 | } | ||
108 | |||
109 | |||
110 | uint32_t | ||
111 | GNUNET_htonl_signed (int32_t n) | ||
112 | { | ||
113 | return htonl (n - INT32_MIN); | ||
114 | } | ||
115 | |||
116 | |||
117 | int32_t | ||
118 | GNUNET_ntohl_signed (uint32_t n) | ||
119 | { | ||
120 | return ntohl (n) + INT32_MIN; | ||
121 | } | ||
122 | |||
123 | |||
124 | uint16_t | ||
125 | GNUNET_htons_signed (int16_t n) | ||
126 | { | ||
127 | return htons (n - INT16_MIN); | ||
128 | } | ||
129 | |||
130 | |||
131 | int16_t | ||
132 | GNUNET_ntohs_signed (uint16_t n) | ||
133 | { | ||
134 | return ntohs (n) + INT16_MIN; | ||
135 | } | ||
136 | |||
137 | |||
93 | 138 | ||
94 | /* end of common_endian.c */ | 139 | /* end of common_endian.c */ |