extract.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1992, 1993, 1994, 1995, 1996
00003  *  The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that: (1) source code distributions
00007  * retain the above copyright notice and this paragraph in its entirety, (2)
00008  * distributions including binary code include the above copyright notice and
00009  * this paragraph in its entirety in the documentation or other materials
00010  * provided with the distribution, and (3) all advertising materials mentioning
00011  * features or use of this software display the following acknowledgement:
00012  * ``This product includes software developed by the University of California,
00013  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
00014  * the University nor the names of its contributors may be used to endorse
00015  * or promote products derived from this software without specific prior
00016  * written permission.
00017  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
00018  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020  *
00021  * @(#) $Header: /tcpdump/master/tcpdump/extract.h,v 1.24 2005/01/15 02:06:50 guy Exp $ (LBL)
00022  */
00023 
00024 /*
00025  * Macros to extract possibly-unaligned big-endian integral values.
00026  */
00027 #ifdef LBL_ALIGN
00028 /*
00029  * The processor doesn't natively handle unaligned loads.
00030  */
00031 #ifdef HAVE___ATTRIBUTE__
00032 /*
00033  * We have __attribute__; we assume that means we have __attribute__((packed)).
00034  * Declare packed structures containing a u_int16_t and a u_int32_t,
00035  * cast the pointer to point to one of those, and fetch through it;
00036  * the GCC manual doesn't appear to explicitly say that
00037  * __attribute__((packed)) causes the compiler to generate unaligned-safe
00038  * code, but it apppears to do so.
00039  *
00040  * We do this in case the compiler can generate, for this instruction set,
00041  * better code to do an unaligned load and pass stuff to "ntohs()" or
00042  * "ntohl()" than the code to fetch the bytes one at a time and
00043  * assemble them.  (That might not be the case on a little-endian platform,
00044  * where "ntohs()" and "ntohl()" might not be done inline.)
00045  */
00046 typedef struct {
00047   u_int16_t val;
00048 } __attribute__((packed)) unaligned_u_int16_t;
00049 
00050 typedef struct {
00051   u_int32_t val;
00052 } __attribute__((packed)) unaligned_u_int32_t;
00053 
00054 #define EXTRACT_16BITS(p) \
00055   ((u_int16_t)ntohs(((const unaligned_u_int16_t *)(p))->val))
00056 #define EXTRACT_32BITS(p) \
00057   ((u_int32_t)ntohl(((const unaligned_u_int32_t *)(p))->val))
00058 #define EXTRACT_64BITS(p) \
00059   ((u_int64_t)(((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 0)->val)) << 32 | \
00060          ((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 1)->val)) << 0))
00061 
00062 #else /* HAVE___ATTRIBUTE__ */
00063 /*
00064  * We don't have __attribute__, so do unaligned loads of big-endian
00065  * quantities the hard way - fetch the bytes one at a time and
00066  * assemble them.
00067  */
00068 #define EXTRACT_16BITS(p) \
00069   ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \
00070          (u_int16_t)*((const u_int8_t *)(p) + 1)))
00071 #define EXTRACT_32BITS(p) \
00072   ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \
00073          (u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \
00074          (u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \
00075          (u_int32_t)*((const u_int8_t *)(p) + 3)))
00076 #define EXTRACT_64BITS(p) \
00077   ((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 0) << 56 | \
00078          (u_int64_t)*((const u_int8_t *)(p) + 1) << 48 | \
00079          (u_int64_t)*((const u_int8_t *)(p) + 2) << 40 | \
00080          (u_int64_t)*((const u_int8_t *)(p) + 3) << 32 | \
00081                (u_int64_t)*((const u_int8_t *)(p) + 4) << 24 | \
00082          (u_int64_t)*((const u_int8_t *)(p) + 5) << 16 | \
00083          (u_int64_t)*((const u_int8_t *)(p) + 6) << 8 | \
00084          (u_int64_t)*((const u_int8_t *)(p) + 7)))
00085 #endif /* HAVE___ATTRIBUTE__ */
00086 #else /* LBL_ALIGN */
00087 /*
00088  * The processor natively handles unaligned loads, so we can just
00089  * cast the pointer and fetch through it.
00090  */
00091 #define EXTRACT_16BITS(p) \
00092   ((u_int16_t)ntohs(*(const u_int16_t *)(p)))
00093 #define EXTRACT_32BITS(p) \
00094   ((u_int32_t)ntohl(*(const u_int32_t *)(p)))
00095 #define EXTRACT_64BITS(p) \
00096   ((u_int64_t)(((u_int64_t)ntohl(*((const u_int32_t *)(p) + 0))) << 32 | \
00097          ((u_int64_t)ntohl(*((const u_int32_t *)(p) + 1))) << 0))
00098 #endif /* LBL_ALIGN */
00099 
00100 #define EXTRACT_24BITS(p) \
00101   ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \
00102          (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
00103          (u_int32_t)*((const u_int8_t *)(p) + 2)))
00104 
00105 /*
00106  * Macros to extract possibly-unaligned little-endian integral values.
00107  * XXX - do loads on little-endian machines that support unaligned loads?
00108  */
00109 #define EXTRACT_LE_8BITS(p) (*(p))
00110 #define EXTRACT_LE_16BITS(p) \
00111   ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \
00112          (u_int16_t)*((const u_int8_t *)(p) + 0)))
00113 #define EXTRACT_LE_32BITS(p) \
00114   ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \
00115          (u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \
00116          (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
00117          (u_int32_t)*((const u_int8_t *)(p) + 0)))
00118 #define EXTRACT_LE_64BITS(p) \
00119   ((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 7) << 56 | \
00120          (u_int64_t)*((const u_int8_t *)(p) + 6) << 48 | \
00121          (u_int64_t)*((const u_int8_t *)(p) + 5) << 40 | \
00122          (u_int64_t)*((const u_int8_t *)(p) + 4) << 32 | \
00123                (u_int64_t)*((const u_int8_t *)(p) + 3) << 24 | \
00124          (u_int64_t)*((const u_int8_t *)(p) + 2) << 16 | \
00125          (u_int64_t)*((const u_int8_t *)(p) + 1) << 8 | \
00126          (u_int64_t)*((const u_int8_t *)(p) + 0)))

Generated on Fri Jul 25 17:10:33 2008 for WifiScanner by  doxygen 1.5.5