]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/lib/isccc/include/isccc/util.h
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / lib / isccc / include / isccc / util.h
1 /*
2  * Portions Copyright (C) 2004, 2005  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.4.18.2 2005/04/29 00:17:14 marka Exp $ */
20
21 #ifndef ISCCC_UTIL_H
22 #define ISCCC_UTIL_H 1
23
24 #include <isc/util.h>
25
26 /*! \file
27  * \brief
28  * Macros for dealing with unaligned numbers.
29  *
30  * \note no side effects are allowed when invoking these macros!
31  */
32
33 #define GET8(v, w) \
34         do { \
35                 v = *w; \
36                 w++; \
37         } while (0)
38
39 #define GET16(v, w) \
40         do { \
41                 v = (unsigned int)w[0] << 8; \
42                 v |= (unsigned int)w[1]; \
43                 w += 2; \
44         } while (0)
45
46 #define GET24(v, w) \
47         do { \
48                 v = (unsigned int)w[0] << 16; \
49                 v |= (unsigned int)w[1] << 8; \
50                 v |= (unsigned int)w[2]; \
51                 w += 3; \
52         } while (0)
53
54 #define GET32(v, w) \
55         do { \
56                 v = (unsigned int)w[0] << 24; \
57                 v |= (unsigned int)w[1] << 16; \
58                 v |= (unsigned int)w[2] << 8; \
59                 v |= (unsigned int)w[3]; \
60                 w += 4; \
61         } while (0)
62
63 #define GET64(v, w) \
64         do { \
65                 v = (isc_uint64_t)w[0] << 56; \
66                 v |= (isc_uint64_t)w[1] << 48; \
67                 v |= (isc_uint64_t)w[2] << 40; \
68                 v |= (isc_uint64_t)w[3] << 32; \
69                 v |= (isc_uint64_t)w[4] << 24; \
70                 v |= (isc_uint64_t)w[5] << 16; \
71                 v |= (isc_uint64_t)w[6] << 8; \
72                 v |= (isc_uint64_t)w[7]; \
73                 w += 8; \
74         } while (0)
75
76 #define GETC16(v, w, d) \
77         do { \
78                 GET8(v, w); \
79                 if (v == 0) \
80                         d = ISCCC_TRUE; \
81                 else { \
82                         d = ISCCC_FALSE; \
83                         if (v == 255) \
84                                 GET16(v, w); \
85                 } \
86         } while (0)
87
88 #define GETC32(v, w) \
89         do { \
90                 GET24(v, w); \
91                 if (v == 0xffffffu) \
92                         GET32(v, w); \
93         } while (0)
94
95 #define GET_OFFSET(v, w)                GET32(v, w)
96
97 #define GET_MEM(v, c, w) \
98         do { \
99                 memcpy(v, w, c); \
100                 w += c; \
101         } while (0)
102
103 #define GET_TYPE(v, w) \
104         do { \
105                 GET8(v, w); \
106                 if (v > 127) { \
107                         if (v < 255) \
108                                 v = ((v & 0x7f) << 16) | ISCCC_RDATATYPE_SIG; \
109                         else \
110                                 GET32(v, w); \
111                 } \
112         } while (0)
113
114 #define PUT8(v, w) \
115         do { \
116                 *w = (v & 0x000000ffU); \
117                 w++; \
118         } while (0)
119
120 #define PUT16(v, w) \
121         do { \
122                 w[0] = (v & 0x0000ff00U) >> 8; \
123                 w[1] = (v & 0x000000ffU); \
124                 w += 2; \
125         } while (0)
126
127 #define PUT24(v, w) \
128         do { \
129                 w[0] = (v & 0x00ff0000U) >> 16; \
130                 w[1] = (v & 0x0000ff00U) >> 8; \
131                 w[2] = (v & 0x000000ffU); \
132                 w += 3; \
133         } while (0)
134
135 #define PUT32(v, w) \
136         do { \
137                 w[0] = (v & 0xff000000U) >> 24; \
138                 w[1] = (v & 0x00ff0000U) >> 16; \
139                 w[2] = (v & 0x0000ff00U) >> 8; \
140                 w[3] = (v & 0x000000ffU); \
141                 w += 4; \
142         } while (0)
143
144 #define PUT64(v, w) \
145         do { \
146                 w[0] = (v & 0xff00000000000000ULL) >> 56; \
147                 w[1] = (v & 0x00ff000000000000ULL) >> 48; \
148                 w[2] = (v & 0x0000ff0000000000ULL) >> 40; \
149                 w[3] = (v & 0x000000ff00000000ULL) >> 32; \
150                 w[4] = (v & 0x00000000ff000000ULL) >> 24; \
151                 w[5] = (v & 0x0000000000ff0000ULL) >> 16; \
152                 w[6] = (v & 0x000000000000ff00ULL) >> 8; \
153                 w[7] = (v & 0x00000000000000ffULL); \
154                 w += 8; \
155         } while (0)
156
157 #define PUTC16(v, w) \
158         do { \
159                 if (v > 0 && v < 255) \
160                         PUT8(v, w); \
161                 else { \
162                         PUT8(255, w); \
163                         PUT16(v, w); \
164                 } \
165         } while (0)
166
167 #define PUTC32(v, w) \
168         do { \
169                 if (v < 0xffffffU) \
170                         PUT24(v, w); \
171                 else { \
172                         PUT24(0xffffffU, w); \
173                         PUT32(v, w); \
174                 } \
175         } while (0)
176
177 #define PUT_OFFSET(v, w)                PUT32(v, w)
178
179 #include <string.h>
180
181 #define PUT_MEM(s, c, w) \
182         do { \
183                 memcpy(w, s, c); \
184                 w += c; \
185         } while (0)
186
187 /*
188  * Regions.
189  */
190 #define REGION_SIZE(r)          ((unsigned int)((r).rend - (r).rstart))
191 #define REGION_EMPTY(r)         ((r).rstart == (r).rend)
192 #define REGION_FROMSTRING(r, s) do { \
193         (r).rstart = (unsigned char *)s; \
194         (r).rend = (r).rstart + strlen(s); \
195 } while (0)
196
197 /*%
198  * Use this to remove the const qualifier of a variable to assign it to
199  * a non-const variable or pass it as a non-const function argument ...
200  * but only when you are sure it won't then be changed!
201  * This is necessary to sometimes shut up some compilers
202  * (as with gcc -Wcast-qual) when there is just no other good way to avoid the
203  * situation.
204  */
205 #define DE_CONST(konst, var) \
206         do { \
207                 union { const void *k; void *v; } _u; \
208                 _u.k = konst; \
209                 var = _u.v; \
210         } while (0)
211
212 #endif /* ISCCC_UTIL_H */