paragraph for gnunet devs that don't know how to use the web
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file util/common_endian.c
21  * @brief endian conversion helpers
22  * @author Christian Grothoff
23  * @author Gabor X Toth
24  */
25
26 #include "platform.h"
27 #include "gnunet_crypto_lib.h"
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-common-endian",__VA_ARGS__)
30
31
32 uint64_t
33 GNUNET_htonll (uint64_t n)
34 {
35 #if __BYTE_ORDER == __BIG_ENDIAN
36   return n;
37 #elif __BYTE_ORDER == __LITTLE_ENDIAN
38   return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32);
39 #else
40   #error byteorder undefined
41 #endif
42 }
43
44
45 uint64_t
46 GNUNET_ntohll (uint64_t n)
47 {
48 #if __BYTE_ORDER == __BIG_ENDIAN
49   return n;
50 #elif __BYTE_ORDER == __LITTLE_ENDIAN
51   return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32);
52 #else
53   #error byteorder undefined
54 #endif
55 }
56
57
58 /**
59  * Convert double to network-byte-order.
60  * @param d the value in network byte order
61  * @return the same value in host byte order
62  */
63 double
64 GNUNET_hton_double (double d)
65 {
66   double res;
67   uint64_t *in = (uint64_t *) &d;
68   uint64_t *out = (uint64_t *) &res;
69
70   out[0] = GNUNET_htonll(in[0]);
71
72   return res;
73 }
74
75
76 /**
77  * Convert double to host-byte-order
78  * @param d the value in network byte order
79  * @return the same value in host byte order
80  */
81 double
82 GNUNET_ntoh_double (double d)
83 {
84   double res;
85   uint64_t *in = (uint64_t *) &d;
86   uint64_t *out = (uint64_t *) &res;
87
88   out[0] = GNUNET_ntohll(in[0]);
89
90   return res;
91 }
92
93
94 /* end of common_endian.c */