Import of bind-9.3.2-P1
[dragonfly.git] / contrib / bind-9.3 / lib / isccc / include / isccc / util.h
1 /*
2  * Portions Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (C) 2001  Internet Software Consortium.
4  * Portions Copyright (C) 2001  Nominum, Inc.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12  * OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY
13  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 /* $Id: util.h,v 1.3.206.1 2004/03/06 08:15:23 marka Exp $ */
20
21 #ifndef ISCCC_UTIL_H
22 #define ISCCC_UTIL_H 1
23
24 #include <isc/util.h>
25
26 /*
27  * Macros for dealing with unaligned numbers.
28  *
29  * Note: no side effects are allowed when invoking these macros!
30  */
31
32 #define GET8(v, w) \
33         do { \
34                 v = *w; \
35                 w++; \
36         } while (0)
37
38 #define GET16(v, w) \
39         do { \
40                 v = (unsigned int)w[0] << 8; \
41                 v |= (unsigned int)w[1]; \
42                 w += 2; \
43         } while (0)
44
45 #define GET24(v, w) \
46         do { \
47                 v = (unsigned int)w[0] << 16; \
48                 v |= (unsigned int)w[1] << 8; \
49                 v |= (unsigned int)w[2]; \
50                 w += 3; \
51         } while (0)
52
53 #define GET32(v, w) \
54         do { \
55                 v = (unsigned int)w[0] << 24; \
56                 v |= (unsigned int)w[1] << 16; \
57                 v |= (unsigned int)w[2] << 8; \
58                 v |= (unsigned int)w[3]; \
59                 w += 4; \
60         } while (0)
61
62 #define GET64(v, w) \
63         do { \
64                 v = (isc_uint64_t)w[0] << 56; \
65                 v |= (isc_uint64_t)w[1] << 48; \
66                 v |= (isc_uint64_t)w[2] << 40; \
67                 v |= (isc_uint64_t)w[3] << 32; \
68                 v |= (isc_uint64_t)w[4] << 24; \
69                 v |= (isc_uint64_t)w[5] << 16; \
70                 v |= (isc_uint64_t)w[6] << 8; \
71                 v |= (isc_uint64_t)w[7]; \
72                 w += 8; \
73         } while (0)
74
75 #define GETC16(v, w, d) \
76         do { \
77                 GET8(v, w); \
78                 if (v == 0) \
79                         d = ISCCC_TRUE; \
80                 else { \
81                         d = ISCCC_FALSE; \
82                         if (v == 255) \
83                                 GET16(v, w); \
84                 } \
85         } while (0)
86
87 #define GETC32(v, w) \
88         do { \
89                 GET24(v, w); \
90                 if (v == 0xffffffu) \
91                         GET32(v, w); \
92         } while (0)
93
94 #define GET_OFFSET(v, w)                GET32(v, w)
95
96 #define GET_MEM(v, c, w) \
97         do { \
98                 memcpy(v, w, c); \
99                 w += c; \
100         } while (0)
101
102 #define GET_TYPE(v, w) \
103         do { \
104                 GET8(v, w); \
105                 if (v > 127) { \
106                         if (v < 255) \
107                                 v = ((v & 0x7f) << 16) | ISCCC_RDATATYPE_SIG; \
108                         else \
109                                 GET32(v, w); \
110                 } \
111         } while (0)
112
113 #define PUT8(v, w) \
114         do { \
115                 *w = (v & 0x000000ffU); \
116                 w++; \
117         } while (0)
118
119 #define PUT16(v, w) \
120         do { \
121                 w[0] = (v & 0x0000ff00U) >> 8; \
122                 w[1] = (v & 0x000000ffU); \
123                 w += 2; \
124         } while (0)
125
126 #define PUT24(v, w) \
127         do { \
128                 w[0] = (v & 0x00ff0000U) >> 16; \
129                 w[1] = (v & 0x0000ff00U) >> 8; \
130                 w[2] = (v & 0x000000ffU); \
131                 w += 3; \
132         } while (0)
133
134 #define PUT32(v, w) \
135         do { \
136                 w[0] = (v & 0xff000000U) >> 24; \
137                 w[1] = (v & 0x00ff0000U) >> 16; \
138                 w[2] = (v & 0x0000ff00U) >> 8; \
139                 w[3] = (v & 0x000000ffU); \
140                 w += 4; \
141         } while (0)
142
143 #define PUT64(v, w) \
144         do { \
145                 w[0] = (v & 0xff00000000000000ULL) >> 56; \
146                 w[1] = (v & 0x00ff000000000000ULL) >> 48; \
147                 w[2] = (v & 0x0000ff0000000000ULL) >> 40; \
148                 w[3] = (v & 0x000000ff00000000ULL) >> 32; \
149                 w[4] = (v & 0x00000000ff000000ULL) >> 24; \
150                 w[5] = (v & 0x0000000000ff0000ULL) >> 16; \
151                 w[6] = (v & 0x000000000000ff00ULL) >> 8; \
152                 w[7] = (v & 0x00000000000000ffULL); \
153                 w += 8; \
154         } while (0)
155
156 #define PUTC16(v, w) \
157         do { \
158                 if (v > 0 && v < 255) \
159                         PUT8(v, w); \
160                 else { \
161                         PUT8(255, w); \
162                         PUT16(v, w); \
163                 } \
164         } while (0)
165
166 #define PUTC32(v, w) \
167         do { \
168                 if (v < 0xffffffU) \
169                         PUT24(v, w); \
170                 else { \
171                         PUT24(0xffffffU, w); \
172                         PUT32(v, w); \
173                 } \
174         } while (0)
175
176 #define PUT_OFFSET(v, w)                PUT32(v, w)
177
178 #include <string.h>
179
180 #define PUT_MEM(s, c, w) \
181         do { \
182                 memcpy(w, s, c); \
183                 w += c; \
184         } while (0)
185
186 /*
187  * Regions.
188  */
189 #define REGION_SIZE(r)          ((unsigned int)((r).rend - (r).rstart))
190 #define REGION_EMPTY(r)         ((r).rstart == (r).rend)
191 #define REGION_FROMSTRING(r, s) do { \
192         (r).rstart = (unsigned char *)s; \
193         (r).rend = (r).rstart + strlen(s); \
194 } while (0)
195
196 /*
197  * Use this to remove the const qualifier of a variable to assign it to
198  * a non-const variable or pass it as a non-const function argument ...
199  * but only when you are sure it won't then be changed!
200  * This is necessary to sometimes shut up some compilers
201  * (as with gcc -Wcast-qual) when there is just no other good way to avoid the
202  * situation.
203  */
204 #define DE_CONST(konst, var) \
205         do { \
206                 union { const void *k; void *v; } _u; \
207                 _u.k = konst; \
208                 var = _u.v; \
209         } while (0)
210
211 #endif /* ISCCC_UTIL_H */