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.19 2002/12/11 07:13:51 guy Exp $ (LBL) 22 */ 23 24 /* Network to host order macros */ 25 26 #ifdef LBL_ALIGN 27 /* 28 * The processor doesn't natively handle unaligned loads. 29 */ 30 #ifdef HAVE___ATTRIBUTE__ 31 /* 32 * We have __attribute__; we assume that means we have __attribute__((packed)). 33 * Declare packed structures containing a u_int16_t and a u_int32_t, 34 * cast the pointer to point to one of those, and fetch through it; 35 * the GCC manual doesn't appear to explicitly say that 36 * __attribute__((packed)) causes the compiler to generate unaligned-safe 37 * code, but it apppears to do so. 38 * 39 * We do this in case the compiler can generate, for this instruction set, 40 * better code to do an unaligned load and pass stuff to "ntohs()" or 41 * "ntohl()" than the code to fetch the bytes one at a time and 42 * assemble them. (That might not be the case on a little-endian platform, 43 * where "ntohs()" and "ntohl()" might not be done inline.) 44 */ 45 typedef struct { 46 u_int16_t val; 47 } __attribute__((packed)) unaligned_u_int16_t; 48 49 typedef struct { 50 u_int32_t val; 51 } __attribute__((packed)) unaligned_u_int32_t; 52 53 #define EXTRACT_16BITS(p) \ 54 ((u_int16_t)ntohs(((const unaligned_u_int16_t *)(p))->val)) 55 #define EXTRACT_32BITS(p) \ 56 ((u_int32_t)ntohl(((const unaligned_u_int32_t *)(p))->val)) 57 #else /* HAVE___ATTRIBUTE__ */ 58 /* 59 * We don't have __attribute__, so do unaligned loads of big-endian 60 * quantities the hard way - fetch the bytes one at a time and 61 * assemble them. 62 */ 63 #define EXTRACT_16BITS(p) \ 64 ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \ 65 (u_int16_t)*((const u_int8_t *)(p) + 1))) 66 #define EXTRACT_32BITS(p) \ 67 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \ 68 (u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \ 69 (u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \ 70 (u_int32_t)*((const u_int8_t *)(p) + 3))) 71 #endif /* HAVE___ATTRIBUTE__ */ 72 #else /* LBL_ALIGN */ 73 /* 74 * The processor natively handles unaligned loads, so we can just 75 * cast the pointer and fetch through it. 76 */ 77 #define EXTRACT_16BITS(p) \ 78 ((u_int16_t)ntohs(*(const u_int16_t *)(p))) 79 #define EXTRACT_32BITS(p) \ 80 ((u_int32_t)ntohl(*(const u_int32_t *)(p))) 81 #endif /* LBL_ALIGN */ 82 83 #define EXTRACT_24BITS(p) \ 84 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \ 85 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \ 86 (u_int32_t)*((const u_int8_t *)(p) + 2))) 87 88 /* Little endian protocol host order macros */ 89 90 #define EXTRACT_LE_8BITS(p) (*(p)) 91 #define EXTRACT_LE_16BITS(p) \ 92 ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \ 93 (u_int16_t)*((const u_int8_t *)(p) + 0))) 94 #define EXTRACT_LE_32BITS(p) \ 95 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \ 96 (u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \ 97 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \ 98 (u_int32_t)*((const u_int8_t *)(p) + 0))) 99