chiark / gitweb /
dirmngr: New debug message on correctly initialized libdns.
[gnupg2.git] / common / host2net.h
1 /* host2net.h - Endian conversion macros
2  * Copyright (C) 1998, 2014, 2015  Werner Koch
3  *
4  * This file is part of GnuPG.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of either
8  *
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.
12  *
13  * or
14  *
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.
18  *
19  * or both in parallel, as here.
20  *
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.
25  *
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/>.
28  */
29
30 #ifndef GNUPG_COMMON_HOST2NET_H
31 #define GNUPG_COMMON_HOST2NET_H
32
33 #include "types.h"
34
35 #define ulongtobuf( p, a ) do {                           \
36                             ((byte*)p)[0] = a >> 24;    \
37                             ((byte*)p)[1] = a >> 16;    \
38                             ((byte*)p)[2] = a >>  8;    \
39                             ((byte*)p)[3] = a      ;    \
40                         } while(0)
41 #define ushorttobuf( p, a ) do {                           \
42                             ((byte*)p)[0] = a >>  8;    \
43                             ((byte*)p)[1] = a      ;    \
44                         } while(0)
45
46
47 static inline unsigned long
48 buf16_to_ulong (const void *buffer)
49 {
50   const unsigned char *p = buffer;
51
52   return (((unsigned long)p[0] << 8) | p[1]);
53 }
54
55 static inline unsigned int
56 buf16_to_uint (const void *buffer)
57 {
58   const unsigned char *p = buffer;
59
60   return (((unsigned int)p[0] << 8) | p[1]);
61 }
62
63 static inline unsigned short
64 buf16_to_ushort (const void *buffer)
65 {
66   const unsigned char *p = buffer;
67
68   return (((unsigned short)p[0] << 8) | p[1]);
69 }
70
71 static inline u16
72 buf16_to_u16 (const void *buffer)
73 {
74   const unsigned char *p = buffer;
75
76   return (((u16)p[0] << 8) | p[1]);
77 }
78
79 static inline size_t
80 buf32_to_size_t (const void *buffer)
81 {
82   const unsigned char *p = buffer;
83
84   return (((size_t)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
85 }
86
87 static inline unsigned long
88 buf32_to_ulong (const void *buffer)
89 {
90   const unsigned char *p = buffer;
91
92   return (((unsigned long)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
93 }
94
95 static inline unsigned int
96 buf32_to_uint (const void *buffer)
97 {
98   const unsigned char *p = buffer;
99
100   return (((unsigned int)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
101 }
102
103 static inline u32
104 buf32_to_u32 (const void *buffer)
105 {
106   const unsigned char *p = buffer;
107
108   return (((u32)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
109 }
110
111
112 #endif /*GNUPG_COMMON_HOST2NET_H*/