Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / tcpdump / extract.h
blob834523eb6fba211ea131420719a54e04de160caf
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * @(#) Header: /tcpdump/master/tcpdump/extract.h,v 1.24 2005/01/15 02:06:50 guy Exp (LBL)
27 * Macros to extract possibly-unaligned big-endian integral values.
29 #ifdef LBL_ALIGN
31 * The processor doesn't natively handle unaligned loads.
33 #ifdef HAVE___ATTRIBUTE__
35 * We have __attribute__; we assume that means we have __attribute__((packed)).
36 * Declare packed structures containing a u_int16_t and a u_int32_t,
37 * cast the pointer to point to one of those, and fetch through it;
38 * the GCC manual doesn't appear to explicitly say that
39 * __attribute__((packed)) causes the compiler to generate unaligned-safe
40 * code, but it apppears to do so.
42 * We do this in case the compiler can generate, for this instruction set,
43 * better code to do an unaligned load and pass stuff to "ntohs()" or
44 * "ntohl()" than the code to fetch the bytes one at a time and
45 * assemble them. (That might not be the case on a little-endian platform,
46 * where "ntohs()" and "ntohl()" might not be done inline.)
48 typedef struct {
49 u_int16_t val;
50 } __attribute__((packed)) unaligned_u_int16_t;
52 typedef struct {
53 u_int32_t val;
54 } __attribute__((packed)) unaligned_u_int32_t;
56 #define EXTRACT_16BITS(p) \
57 ((u_int16_t)ntohs(((const unaligned_u_int16_t *)(p))->val))
58 #define EXTRACT_32BITS(p) \
59 ((u_int32_t)ntohl(((const unaligned_u_int32_t *)(p))->val))
60 #define EXTRACT_64BITS(p) \
61 ((u_int64_t)(((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 0)->val)) << 32 | \
62 ((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 1)->val)) << 0))
64 #else /* HAVE___ATTRIBUTE__ */
66 * We don't have __attribute__, so do unaligned loads of big-endian
67 * quantities the hard way - fetch the bytes one at a time and
68 * assemble them.
70 #define EXTRACT_16BITS(p) \
71 ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \
72 (u_int16_t)*((const u_int8_t *)(p) + 1)))
73 #define EXTRACT_32BITS(p) \
74 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \
75 (u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \
76 (u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \
77 (u_int32_t)*((const u_int8_t *)(p) + 3)))
78 #define EXTRACT_64BITS(p) \
79 ((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 0) << 56 | \
80 (u_int64_t)*((const u_int8_t *)(p) + 1) << 48 | \
81 (u_int64_t)*((const u_int8_t *)(p) + 2) << 40 | \
82 (u_int64_t)*((const u_int8_t *)(p) + 3) << 32 | \
83 (u_int64_t)*((const u_int8_t *)(p) + 4) << 24 | \
84 (u_int64_t)*((const u_int8_t *)(p) + 5) << 16 | \
85 (u_int64_t)*((const u_int8_t *)(p) + 6) << 8 | \
86 (u_int64_t)*((const u_int8_t *)(p) + 7)))
87 #endif /* HAVE___ATTRIBUTE__ */
88 #else /* LBL_ALIGN */
90 * The processor natively handles unaligned loads, so we can just
91 * cast the pointer and fetch through it.
93 #define EXTRACT_16BITS(p) \
94 ((u_int16_t)ntohs(*(const u_int16_t *)(p)))
95 #define EXTRACT_32BITS(p) \
96 ((u_int32_t)ntohl(*(const u_int32_t *)(p)))
97 #define EXTRACT_64BITS(p) \
98 ((u_int64_t)(((u_int64_t)ntohl(*((const u_int32_t *)(p) + 0))) << 32 | \
99 ((u_int64_t)ntohl(*((const u_int32_t *)(p) + 1))) << 0))
100 #endif /* LBL_ALIGN */
102 #define EXTRACT_24BITS(p) \
103 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \
104 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
105 (u_int32_t)*((const u_int8_t *)(p) + 2)))
108 * Macros to extract possibly-unaligned little-endian integral values.
109 * XXX - do loads on little-endian machines that support unaligned loads?
111 #define EXTRACT_LE_8BITS(p) (*(p))
112 #define EXTRACT_LE_16BITS(p) \
113 ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \
114 (u_int16_t)*((const u_int8_t *)(p) + 0)))
115 #define EXTRACT_LE_32BITS(p) \
116 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \
117 (u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \
118 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
119 (u_int32_t)*((const u_int8_t *)(p) + 0)))
120 #define EXTRACT_LE_64BITS(p) \
121 ((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 7) << 56 | \
122 (u_int64_t)*((const u_int8_t *)(p) + 6) << 48 | \
123 (u_int64_t)*((const u_int8_t *)(p) + 5) << 40 | \
124 (u_int64_t)*((const u_int8_t *)(p) + 4) << 32 | \
125 (u_int64_t)*((const u_int8_t *)(p) + 3) << 24 | \
126 (u_int64_t)*((const u_int8_t *)(p) + 2) << 16 | \
127 (u_int64_t)*((const u_int8_t *)(p) + 1) << 8 | \
128 (u_int64_t)*((const u_int8_t *)(p) + 0)))