1 /* host2net.h - Endian conversion macros
2 * Copyright (C) 1998, 2014, 2015 Werner Koch
4 * This file is part of GnuPG.
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of either
9 * - the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
15 * - the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * or both in parallel, as here.
21 * This file is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <https://www.gnu.org/licenses/>.
30 #ifndef GNUPG_COMMON_HOST2NET_H
31 #define GNUPG_COMMON_HOST2NET_H
35 #define ulongtobuf( p, a ) do { \
36 ((byte*)p)[0] = a >> 24; \
37 ((byte*)p)[1] = a >> 16; \
38 ((byte*)p)[2] = a >> 8; \
41 #define ushorttobuf( p, a ) do { \
42 ((byte*)p)[0] = a >> 8; \
47 static inline unsigned long
48 buf16_to_ulong (const void *buffer)
50 const unsigned char *p = buffer;
52 return (((unsigned long)p[0] << 8) | p[1]);
55 static inline unsigned int
56 buf16_to_uint (const void *buffer)
58 const unsigned char *p = buffer;
60 return (((unsigned int)p[0] << 8) | p[1]);
63 static inline unsigned short
64 buf16_to_ushort (const void *buffer)
66 const unsigned char *p = buffer;
68 return (((unsigned short)p[0] << 8) | p[1]);
72 buf16_to_u16 (const void *buffer)
74 const unsigned char *p = buffer;
76 return (((u16)p[0] << 8) | p[1]);
80 buf32_to_size_t (const void *buffer)
82 const unsigned char *p = buffer;
84 return (((size_t)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
87 static inline unsigned long
88 buf32_to_ulong (const void *buffer)
90 const unsigned char *p = buffer;
92 return (((unsigned long)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
95 static inline unsigned int
96 buf32_to_uint (const void *buffer)
98 const unsigned char *p = buffer;
100 return (((unsigned int)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
104 buf32_to_u32 (const void *buffer)
106 const unsigned char *p = buffer;
108 return (((u32)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
112 #endif /*GNUPG_COMMON_HOST2NET_H*/