GNUnet  0.10.x
common_endian.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
28 #include "platform.h"
29 #include "gnunet_crypto_lib.h"
30 
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-common-endian",__VA_ARGS__)
32 
33 
34 uint64_t
35 GNUNET_htonll (uint64_t n)
36 {
37 #if __BYTE_ORDER == __BIG_ENDIAN
38  return n;
39 #elif __BYTE_ORDER == __LITTLE_ENDIAN
40  return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32);
41 #else
42  #error byteorder undefined
43 #endif
44 }
45 
46 
47 uint64_t
48 GNUNET_ntohll (uint64_t n)
49 {
50 #if __BYTE_ORDER == __BIG_ENDIAN
51  return n;
52 #elif __BYTE_ORDER == __LITTLE_ENDIAN
53  return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32);
54 #else
55  #error byteorder undefined
56 #endif
57 }
58 
59 
65 double
67 {
68  double res;
69  uint64_t *in = (uint64_t *) &d;
70  uint64_t *out = (uint64_t *) &res;
71 
72  out[0] = GNUNET_htonll(in[0]);
73 
74  return res;
75 }
76 
77 
83 double
85 {
86  double res;
87  uint64_t *in = (uint64_t *) &d;
88  uint64_t *out = (uint64_t *) &res;
89 
90  out[0] = GNUNET_ntohll(in[0]);
91 
92  return res;
93 }
94 
95 
96 /* end of common_endian.c */
double GNUNET_ntoh_double(double d)
Convert double to host-byte-order.
Definition: common_endian.c:84
double GNUNET_hton_double(double d)
Convert double to network-byte-order.
Definition: common_endian.c:66
cryptographic primitives for GNUnet
static int res
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:48