util.h

Go to the documentation of this file.
00001 /*
00002  * Portions Copyright (C) 2004-2007, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Portions Copyright (C) 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
00010  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00011  * OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY
00012  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00013  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00014  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00015  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00016  *
00017  * Portions Copyright (C) 2001  Nominum, Inc.
00018  *
00019  * Permission to use, copy, modify, and/or distribute this software for any
00020  * purpose with or without fee is hereby granted, provided that the above
00021  * copyright notice and this permission notice appear in all copies.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
00024  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00025  * OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY
00026  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00027  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00028  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00029  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00030  */
00031 
00032 /* $Id: util.h,v 1.11 2007/08/28 07:20:43 tbox Exp $ */
00033 
00034 #ifndef ISCCC_UTIL_H
00035 #define ISCCC_UTIL_H 1
00036 
00037 #include <isc/util.h>
00038 
00039 /*! \file isccc/util.h
00040  * \brief
00041  * Macros for dealing with unaligned numbers.
00042  *
00043  * \note no side effects are allowed when invoking these macros!
00044  */
00045 
00046 #define GET8(v, w) \
00047         do { \
00048                 v = *w; \
00049                 w++; \
00050         } while (0)
00051 
00052 #define GET16(v, w) \
00053         do { \
00054                 v = (unsigned int)w[0] << 8; \
00055                 v |= (unsigned int)w[1]; \
00056                 w += 2; \
00057         } while (0)
00058 
00059 #define GET24(v, w) \
00060         do { \
00061                 v = (unsigned int)w[0] << 16; \
00062                 v |= (unsigned int)w[1] << 8; \
00063                 v |= (unsigned int)w[2]; \
00064                 w += 3; \
00065         } while (0)
00066 
00067 #define GET32(v, w) \
00068         do { \
00069                 v = (unsigned int)w[0] << 24; \
00070                 v |= (unsigned int)w[1] << 16; \
00071                 v |= (unsigned int)w[2] << 8; \
00072                 v |= (unsigned int)w[3]; \
00073                 w += 4; \
00074         } while (0)
00075 
00076 #define GET64(v, w) \
00077         do { \
00078                 v = (isc_uint64_t)w[0] << 56; \
00079                 v |= (isc_uint64_t)w[1] << 48; \
00080                 v |= (isc_uint64_t)w[2] << 40; \
00081                 v |= (isc_uint64_t)w[3] << 32; \
00082                 v |= (isc_uint64_t)w[4] << 24; \
00083                 v |= (isc_uint64_t)w[5] << 16; \
00084                 v |= (isc_uint64_t)w[6] << 8; \
00085                 v |= (isc_uint64_t)w[7]; \
00086                 w += 8; \
00087         } while (0)
00088 
00089 #define GETC16(v, w, d) \
00090         do { \
00091                 GET8(v, w); \
00092                 if (v == 0) \
00093                         d = ISCCC_TRUE; \
00094                 else { \
00095                         d = ISCCC_FALSE; \
00096                         if (v == 255) \
00097                                 GET16(v, w); \
00098                 } \
00099         } while (0)
00100 
00101 #define GETC32(v, w) \
00102         do { \
00103                 GET24(v, w); \
00104                 if (v == 0xffffffu) \
00105                         GET32(v, w); \
00106         } while (0)
00107 
00108 #define GET_OFFSET(v, w)                GET32(v, w)
00109 
00110 #define GET_MEM(v, c, w) \
00111         do { \
00112                 memmove(v, w, c); \
00113                 w += c; \
00114         } while (0)
00115 
00116 #define GET_TYPE(v, w) \
00117         do { \
00118                 GET8(v, w); \
00119                 if (v > 127) { \
00120                         if (v < 255) \
00121                                 v = ((v & 0x7f) << 16) | ISCCC_RDATATYPE_SIG; \
00122                         else \
00123                                 GET32(v, w); \
00124                 } \
00125         } while (0)
00126 
00127 #define PUT8(v, w) \
00128         do { \
00129                 *w = (v & 0x000000ffU); \
00130                 w++; \
00131         } while (0)
00132 
00133 #define PUT16(v, w) \
00134         do { \
00135                 w[0] = (v & 0x0000ff00U) >> 8; \
00136                 w[1] = (v & 0x000000ffU); \
00137                 w += 2; \
00138         } while (0)
00139 
00140 #define PUT24(v, w) \
00141         do { \
00142                 w[0] = (v & 0x00ff0000U) >> 16; \
00143                 w[1] = (v & 0x0000ff00U) >> 8; \
00144                 w[2] = (v & 0x000000ffU); \
00145                 w += 3; \
00146         } while (0)
00147 
00148 #define PUT32(v, w) \
00149         do { \
00150                 w[0] = (v & 0xff000000U) >> 24; \
00151                 w[1] = (v & 0x00ff0000U) >> 16; \
00152                 w[2] = (v & 0x0000ff00U) >> 8; \
00153                 w[3] = (v & 0x000000ffU); \
00154                 w += 4; \
00155         } while (0)
00156 
00157 #define PUT64(v, w) \
00158         do { \
00159                 w[0] = (v & 0xff00000000000000ULL) >> 56; \
00160                 w[1] = (v & 0x00ff000000000000ULL) >> 48; \
00161                 w[2] = (v & 0x0000ff0000000000ULL) >> 40; \
00162                 w[3] = (v & 0x000000ff00000000ULL) >> 32; \
00163                 w[4] = (v & 0x00000000ff000000ULL) >> 24; \
00164                 w[5] = (v & 0x0000000000ff0000ULL) >> 16; \
00165                 w[6] = (v & 0x000000000000ff00ULL) >> 8; \
00166                 w[7] = (v & 0x00000000000000ffULL); \
00167                 w += 8; \
00168         } while (0)
00169 
00170 #define PUTC16(v, w) \
00171         do { \
00172                 if (v > 0 && v < 255) \
00173                         PUT8(v, w); \
00174                 else { \
00175                         PUT8(255, w); \
00176                         PUT16(v, w); \
00177                 } \
00178         } while (0)
00179 
00180 #define PUTC32(v, w) \
00181         do { \
00182                 if (v < 0xffffffU) \
00183                         PUT24(v, w); \
00184                 else { \
00185                         PUT24(0xffffffU, w); \
00186                         PUT32(v, w); \
00187                 } \
00188         } while (0)
00189 
00190 #define PUT_OFFSET(v, w)                PUT32(v, w)
00191 
00192 #include <string.h>
00193 
00194 #define PUT_MEM(s, c, w) \
00195         do { \
00196                 memmove(w, s, c); \
00197                 w += c; \
00198         } while (0)
00199 
00200 /*
00201  * Regions.
00202  */
00203 #define REGION_SIZE(r)          ((unsigned int)((r).rend - (r).rstart))
00204 #define REGION_EMPTY(r)         ((r).rstart == (r).rend)
00205 #define REGION_FROMSTRING(r, s) do { \
00206         (r).rstart = (unsigned char *)s; \
00207         (r).rend = (r).rstart + strlen(s); \
00208 } while (0)
00209 
00210 /*%
00211  * Use this to remove the const qualifier of a variable to assign it to
00212  * a non-const variable or pass it as a non-const function argument ...
00213  * but only when you are sure it won't then be changed!
00214  * This is necessary to sometimes shut up some compilers
00215  * (as with gcc -Wcast-qual) when there is just no other good way to avoid the
00216  * situation.
00217  */
00218 #define DE_CONST(konst, var) \
00219         do { \
00220                 union { const void *k; void *v; } _u; \
00221                 _u.k = konst; \
00222                 var = _u.v; \
00223         } while (0)
00224 
00225 #endif /* ISCCC_UTIL_H */

Generated on Tue Apr 28 17:41:04 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha