1 /* 2 * Copyright (c) 1992, 1993, 1994, 1995, 1996 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 * 21 * @(#) Header: /tcpdump/master/tcpdump/extract.h,v 1.25 2006-01-30 16:20:07 hannes Exp (LBL) 22 */ 23 24 #ifdef __NetBSD__ 25 #include <string.h> 26 27 /* 28 * Do it the portable way and let the compiler optimize the code 29 */ 30 static inline uint16_t EXTRACT_16BITS(const void *p) 31 { 32 uint16_t t; 33 memcpy(&t, p, sizeof(t)); 34 return ntohs(t); 35 } 36 37 static inline uint32_t EXTRACT_24BITS(const void *p) 38 { 39 uint8_t t[3]; 40 memcpy(t, p, sizeof(t)); 41 return 42 ((uint32_t)t[0] << 16) | 43 ((uint32_t)t[1] << 8) | 44 t[2]; 45 } 46 47 static inline uint32_t EXTRACT_32BITS(const void *p) 48 { 49 uint32_t t; 50 memcpy(&t, p, sizeof(t)); 51 return ntohl(t); 52 } 53 54 static inline uint64_t EXTRACT_64BITS(const void *p) 55 { 56 uint32_t t[2]; 57 memcpy(&t[0], p, sizeof(t[0])); 58 memcpy(&t[1], (const uint8_t *)p + sizeof(t[0]), sizeof(t[1])); 59 return ((uint64_t)ntohl(t[0]) << 32) | ntohl(t[1]); 60 } 61 62 static inline uint8_t EXTRACT_LE_8BITS(const void *p) 63 { 64 uint8_t t[1]; 65 memcpy(t, p, sizeof(t)); 66 return t[0]; 67 } 68 69 static inline uint16_t EXTRACT_LE_16BITS(const void *p) 70 { 71 uint8_t t[2]; 72 memcpy(t, p, sizeof(t)); 73 return 74 ((uint16_t)t[1] << 8) | 75 t[0]; 76 } 77 78 static inline uint32_t EXTRACT_LE_24BITS(const void *p) 79 { 80 uint8_t t[3]; 81 memcpy(t, p, sizeof(t)); 82 return 83 ((uint32_t)t[2] << 16) | 84 ((uint32_t)t[1] << 8) | 85 t[0]; 86 } 87 88 static inline uint32_t EXTRACT_LE_32BITS(const void *p) 89 { 90 uint8_t t[4]; 91 memcpy(t, p, sizeof(t)); 92 return 93 ((uint32_t)t[3] << 24) | 94 ((uint32_t)t[2] << 16) | 95 ((uint32_t)t[1] << 8) | 96 t[0]; 97 } 98 99 static inline uint64_t EXTRACT_LE_64BITS(const void *p) 100 { 101 uint8_t t[8]; 102 memcpy(&t, p, sizeof(t)); 103 return 104 ((uint64_t)t[7] << 56) | 105 ((uint64_t)t[6] << 48) | 106 ((uint64_t)t[5] << 40) | 107 ((uint64_t)t[4] << 32) | 108 ((uint64_t)t[3] << 24) | 109 ((uint64_t)t[2] << 16) | 110 ((uint64_t)t[1] << 8) | 111 t[0]; 112 } 113 114 #else /* Fast & Loose */ 115 /* 116 * Macros to extract possibly-unaligned big-endian integral values. 117 */ 118 #ifdef LBL_ALIGN 119 /* 120 * The processor doesn't natively handle unaligned loads. 121 */ 122 #ifdef HAVE___ATTRIBUTE__ 123 /* 124 * We have __attribute__; we assume that means we have __attribute__((packed)). 125 * Declare packed structures containing a u_int16_t and a u_int32_t, 126 * cast the pointer to point to one of those, and fetch through it; 127 * the GCC manual doesn't appear to explicitly say that 128 * __attribute__((packed)) causes the compiler to generate unaligned-safe 129 * code, but it apppears to do so. 130 * 131 * We do this in case the compiler can generate, for this instruction set, 132 * better code to do an unaligned load and pass stuff to "ntohs()" or 133 * "ntohl()" than the code to fetch the bytes one at a time and 134 * assemble them. (That might not be the case on a little-endian platform, 135 * where "ntohs()" and "ntohl()" might not be done inline.) 136 */ 137 typedef struct { 138 u_int16_t val; 139 } __attribute__((packed)) unaligned_u_int16_t; 140 141 typedef struct { 142 u_int32_t val; 143 } __attribute__((packed)) unaligned_u_int32_t; 144 145 #define EXTRACT_16BITS(p) \ 146 ((u_int16_t)ntohs(((const unaligned_u_int16_t *)(p))->val)) 147 #define EXTRACT_32BITS(p) \ 148 ((u_int32_t)ntohl(((const unaligned_u_int32_t *)(p))->val)) 149 #define EXTRACT_64BITS(p) \ 150 ((u_int64_t)(((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 0)->val)) << 32 | \ 151 ((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 1)->val)) << 0)) 152 153 #else /* HAVE___ATTRIBUTE__ */ 154 /* 155 * We don't have __attribute__, so do unaligned loads of big-endian 156 * quantities the hard way - fetch the bytes one at a time and 157 * assemble them. 158 */ 159 #define EXTRACT_16BITS(p) \ 160 ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \ 161 (u_int16_t)*((const u_int8_t *)(p) + 1))) 162 #define EXTRACT_32BITS(p) \ 163 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \ 164 (u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \ 165 (u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \ 166 (u_int32_t)*((const u_int8_t *)(p) + 3))) 167 #define EXTRACT_64BITS(p) \ 168 ((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 0) << 56 | \ 169 (u_int64_t)*((const u_int8_t *)(p) + 1) << 48 | \ 170 (u_int64_t)*((const u_int8_t *)(p) + 2) << 40 | \ 171 (u_int64_t)*((const u_int8_t *)(p) + 3) << 32 | \ 172 (u_int64_t)*((const u_int8_t *)(p) + 4) << 24 | \ 173 (u_int64_t)*((const u_int8_t *)(p) + 5) << 16 | \ 174 (u_int64_t)*((const u_int8_t *)(p) + 6) << 8 | \ 175 (u_int64_t)*((const u_int8_t *)(p) + 7))) 176 #endif /* HAVE___ATTRIBUTE__ */ 177 #else /* LBL_ALIGN */ 178 /* 179 * The processor natively handles unaligned loads, so we can just 180 * cast the pointer and fetch through it. 181 */ 182 #define EXTRACT_16BITS(p) \ 183 ((u_int16_t)ntohs(*(const u_int16_t *)(p))) 184 #define EXTRACT_32BITS(p) \ 185 ((u_int32_t)ntohl(*(const u_int32_t *)(p))) 186 #define EXTRACT_64BITS(p) \ 187 ((u_int64_t)(((u_int64_t)ntohl(*((const u_int32_t *)(p) + 0))) << 32 | \ 188 ((u_int64_t)ntohl(*((const u_int32_t *)(p) + 1))) << 0)) 189 #endif /* LBL_ALIGN */ 190 191 #define EXTRACT_24BITS(p) \ 192 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \ 193 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \ 194 (u_int32_t)*((const u_int8_t *)(p) + 2))) 195 196 /* 197 * Macros to extract possibly-unaligned little-endian integral values. 198 * XXX - do loads on little-endian machines that support unaligned loads? 199 */ 200 #define EXTRACT_LE_8BITS(p) (*(p)) 201 #define EXTRACT_LE_16BITS(p) \ 202 ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \ 203 (u_int16_t)*((const u_int8_t *)(p) + 0))) 204 #define EXTRACT_LE_32BITS(p) \ 205 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \ 206 (u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \ 207 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \ 208 (u_int32_t)*((const u_int8_t *)(p) + 0))) 209 #define EXTRACT_LE_24BITS(p) \ 210 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \ 211 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \ 212 (u_int32_t)*((const u_int8_t *)(p) + 0))) 213 #define EXTRACT_LE_64BITS(p) \ 214 ((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 7) << 56 | \ 215 (u_int64_t)*((const u_int8_t *)(p) + 6) << 48 | \ 216 (u_int64_t)*((const u_int8_t *)(p) + 5) << 40 | \ 217 (u_int64_t)*((const u_int8_t *)(p) + 4) << 32 | \ 218 (u_int64_t)*((const u_int8_t *)(p) + 3) << 24 | \ 219 (u_int64_t)*((const u_int8_t *)(p) + 2) << 16 | \ 220 (u_int64_t)*((const u_int8_t *)(p) + 1) << 8 | \ 221 (u_int64_t)*((const u_int8_t *)(p) + 0))) 222 #endif /* __NetBSD__ */ 223