2 * Portions Copyright (C) 2004-2007, 2014 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Portions Copyright (C) 2001 Nominum, Inc.
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
24 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
26 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 /* $Id: util.h,v 1.11 2007/08/28 07:20:43 tbox Exp $ */
35 #define ISCCC_UTIL_H 1
39 /*! \file isccc/util.h
41 * Macros for dealing with unaligned numbers.
43 * \note no side effects are allowed when invoking these macros!
54 v = (unsigned int)w[0] << 8; \
55 v |= (unsigned int)w[1]; \
61 v = (unsigned int)w[0] << 16; \
62 v |= (unsigned int)w[1] << 8; \
63 v |= (unsigned int)w[2]; \
69 v = (unsigned int)w[0] << 24; \
70 v |= (unsigned int)w[1] << 16; \
71 v |= (unsigned int)w[2] << 8; \
72 v |= (unsigned int)w[3]; \
78 v = (isc_uint64_t)w[0] << 56; \
79 v |= (isc_uint64_t)w[1] << 48; \
80 v |= (isc_uint64_t)w[2] << 40; \
81 v |= (isc_uint64_t)w[3] << 32; \
82 v |= (isc_uint64_t)w[4] << 24; \
83 v |= (isc_uint64_t)w[5] << 16; \
84 v |= (isc_uint64_t)w[6] << 8; \
85 v |= (isc_uint64_t)w[7]; \
89 #define GETC16(v, w, d) \
101 #define GETC32(v, w) \
104 if (v == 0xffffffu) \
108 #define GET_OFFSET(v, w) GET32(v, w)
110 #define GET_MEM(v, c, w) \
116 #define GET_TYPE(v, w) \
121 v = ((v & 0x7f) << 16) | ISCCC_RDATATYPE_SIG; \
129 *w = (v & 0x000000ffU); \
133 #define PUT16(v, w) \
135 w[0] = (v & 0x0000ff00U) >> 8; \
136 w[1] = (v & 0x000000ffU); \
140 #define PUT24(v, w) \
142 w[0] = (v & 0x00ff0000U) >> 16; \
143 w[1] = (v & 0x0000ff00U) >> 8; \
144 w[2] = (v & 0x000000ffU); \
148 #define PUT32(v, w) \
150 w[0] = (v & 0xff000000U) >> 24; \
151 w[1] = (v & 0x00ff0000U) >> 16; \
152 w[2] = (v & 0x0000ff00U) >> 8; \
153 w[3] = (v & 0x000000ffU); \
157 #define PUT64(v, w) \
159 w[0] = (v & 0xff00000000000000ULL) >> 56; \
160 w[1] = (v & 0x00ff000000000000ULL) >> 48; \
161 w[2] = (v & 0x0000ff0000000000ULL) >> 40; \
162 w[3] = (v & 0x000000ff00000000ULL) >> 32; \
163 w[4] = (v & 0x00000000ff000000ULL) >> 24; \
164 w[5] = (v & 0x0000000000ff0000ULL) >> 16; \
165 w[6] = (v & 0x000000000000ff00ULL) >> 8; \
166 w[7] = (v & 0x00000000000000ffULL); \
170 #define PUTC16(v, w) \
172 if (v > 0 && v < 255) \
180 #define PUTC32(v, w) \
185 PUT24(0xffffffU, w); \
190 #define PUT_OFFSET(v, w) PUT32(v, w)
194 #define PUT_MEM(s, c, w) \
203 #define REGION_SIZE(r) ((unsigned int)((r).rend - (r).rstart))
204 #define REGION_EMPTY(r) ((r).rstart == (r).rend)
205 #define REGION_FROMSTRING(r, s) do { \
206 (r).rstart = (unsigned char *)s; \
207 (r).rend = (r).rstart + strlen(s); \
211 * Use this to remove the const qualifier of a variable to assign it to
212 * a non-const variable or pass it as a non-const function argument ...
213 * but only when you are sure it won't then be changed!
214 * This is necessary to sometimes shut up some compilers
215 * (as with gcc -Wcast-qual) when there is just no other good way to avoid the
218 #define DE_CONST(konst, var) \
220 union { const void *k; void *v; } _u; \
225 #endif /* ISCCC_UTIL_H */