glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / common_endian.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2003, 2004, 2006, 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15
16 /**
17  * @file util/common_endian.c
18  * @brief endian conversion helpers
19  * @author Christian Grothoff
20  * @author Gabor X Toth
21  */
22
23 #include "platform.h"
24 #include "gnunet_crypto_lib.h"
25
26 #define LOG(kind,...) GNUNET_log_from (kind, "util-common-endian",__VA_ARGS__)
27
28
29 uint64_t
30 GNUNET_htonll (uint64_t n)
31 {
32 #if __BYTE_ORDER == __BIG_ENDIAN
33   return n;
34 #elif __BYTE_ORDER == __LITTLE_ENDIAN
35   return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32);
36 #else
37   #error byteorder undefined
38 #endif
39 }
40
41
42 uint64_t
43 GNUNET_ntohll (uint64_t n)
44 {
45 #if __BYTE_ORDER == __BIG_ENDIAN
46   return n;
47 #elif __BYTE_ORDER == __LITTLE_ENDIAN
48   return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32);
49 #else
50   #error byteorder undefined
51 #endif
52 }
53
54
55 /**
56  * Convert double to network-byte-order.
57  * @param d the value in network byte order
58  * @return the same value in host byte order
59  */
60 double
61 GNUNET_hton_double (double d)
62 {
63   double res;
64   uint64_t *in = (uint64_t *) &d;
65   uint64_t *out = (uint64_t *) &res;
66
67   out[0] = GNUNET_htonll(in[0]);
68
69   return res;
70 }
71
72
73 /**
74  * Convert double to host-byte-order
75  * @param d the value in network byte order
76  * @return the same value in host byte order
77  */
78 double
79 GNUNET_ntoh_double (double d)
80 {
81   double res;
82   uint64_t *in = (uint64_t *) &d;
83   uint64_t *out = (uint64_t *) &res;
84
85   out[0] = GNUNET_ntohll(in[0]);
86
87   return res;
88 }
89
90
91 /* end of common_endian.c */