"Fossies" - the Fresh Open Source Software Archive 
Member "tcpflow-1.6.1/src/wifipcap/extract.h" (19 Feb 2021, 5653 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "extract.h" see the
Fossies "Dox" file reference documentation.
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: /home/cvs/wifitools/wifipcap/extract.h,v 1.1.1.1 2006/12/14 01:22:11 jpang Exp $ (LBL)
22 */
23
24 #ifndef UNI_EXTRACT_H
25 #define UNI_EXTRACT_H
26
27
28 /*
29 * Macros to extract possibly-unaligned big-endian integral values.
30 */
31 #ifdef LBL_ALIGN
32 /*
33 * The processor doesn't natively handle unaligned loads.
34 */
35 #ifdef HAVE___ATTRIBUTE__
36 /*
37 * We have __attribute__; we assume that means we have __attribute__((packed)).
38 * Declare packed structures containing a u_int16_t and a u_int32_t,
39 * cast the pointer to point to one of those, and fetch through it;
40 * the GCC manual doesn't appear to explicitly say that
41 * __attribute__((packed)) causes the compiler to generate unaligned-safe
42 * code, but it apppears to do so.
43 *
44 * We do this in case the compiler can generate, for this instruction set,
45 * better code to do an unaligned load and pass stuff to "ntohs()" or
46 * "ntohl()" than the code to fetch the bytes one at a time and
47 * assemble them. (That might not be the case on a little-endian platform,
48 * where "ntohs()" and "ntohl()" might not be done inline.)
49 */
50 typedef struct {
51 u_int16_t val;
52 } __attribute__((packed)) unaligned_u_int16_t;
53
54 typedef struct {
55 u_int32_t val;
56 } __attribute__((packed)) unaligned_u_int32_t;
57
58 #define EXTRACT_16BITS(p) \
59 ((u_int16_t)ntohs(((const unaligned_u_int16_t *)(p))->val))
60 #define EXTRACT_32BITS(p) \
61 ((u_int32_t)ntohl(((const unaligned_u_int32_t *)(p))->val))
62 #define EXTRACT_64BITS(p) \
63 ((u_int64_t)(((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 0)->val)) << 32 | \
64 ((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 1)->val)) << 0))
65
66 #else /* HAVE___ATTRIBUTE__ */
67 /*
68 * We don't have __attribute__, so do unaligned loads of big-endian
69 * quantities the hard way - fetch the bytes one at a time and
70 * assemble them.
71 */
72 #define EXTRACT_16BITS(p) \
73 ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \
74 (u_int16_t)*((const u_int8_t *)(p) + 1)))
75 #define EXTRACT_32BITS(p) \
76 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \
77 (u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \
78 (u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \
79 (u_int32_t)*((const u_int8_t *)(p) + 3)))
80 #define EXTRACT_64BITS(p) \
81 ((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 0) << 56 | \
82 (u_int64_t)*((const u_int8_t *)(p) + 1) << 48 | \
83 (u_int64_t)*((const u_int8_t *)(p) + 2) << 40 | \
84 (u_int64_t)*((const u_int8_t *)(p) + 3) << 32 | \
85 (u_int64_t)*((const u_int8_t *)(p) + 4) << 24 | \
86 (u_int64_t)*((const u_int8_t *)(p) + 5) << 16 | \
87 (u_int64_t)*((const u_int8_t *)(p) + 6) << 8 | \
88 (u_int64_t)*((const u_int8_t *)(p) + 7)))
89 #endif /* HAVE___ATTRIBUTE__ */
90 #else /* LBL_ALIGN */
91 /*
92 * The processor natively handles unaligned loads, so we can just
93 * cast the pointer and fetch through it.
94 */
95 #define EXTRACT_16BITS(p) \
96 ((u_int16_t)ntohs(*(const u_int16_t *)(p)))
97 #define EXTRACT_32BITS(p) \
98 ((u_int32_t)ntohl(*(const u_int32_t *)(p)))
99 #define EXTRACT_64BITS(p) \
100 ((u_int64_t)(((u_int64_t)ntohl(*((const u_int32_t *)(p) + 0))) << 32 | \
101 ((u_int64_t)ntohl(*((const u_int32_t *)(p) + 1))) << 0))
102 #endif /* LBL_ALIGN */
103
104 #define EXTRACT_24BITS(p) \
105 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \
106 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
107 (u_int32_t)*((const u_int8_t *)(p) + 2)))
108
109 /*
110 * Macros to extract possibly-unaligned little-endian integral values.
111 * XXX - do loads on little-endian machines that support unaligned loads?
112 */
113 #define EXTRACT_LE_8BITS(p) (*(p))
114 #define EXTRACT_LE_16BITS(p) \
115 ((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \
116 (u_int16_t)*((const u_int8_t *)(p) + 0)))
117 #define EXTRACT_LE_32BITS(p) \
118 ((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \
119 (u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \
120 (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
121 (u_int32_t)*((const u_int8_t *)(p) + 0)))
122 #define EXTRACT_LE_64BITS(p) \
123 ((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 7) << 56 | \
124 (u_int64_t)*((const u_int8_t *)(p) + 6) << 48 | \
125 (u_int64_t)*((const u_int8_t *)(p) + 5) << 40 | \
126 (u_int64_t)*((const u_int8_t *)(p) + 4) << 32 | \
127 (u_int64_t)*((const u_int8_t *)(p) + 3) << 24 | \
128 (u_int64_t)*((const u_int8_t *)(p) + 2) << 16 | \
129 (u_int64_t)*((const u_int8_t *)(p) + 1) << 8 | \
130 (u_int64_t)*((const u_int8_t *)(p) + 0)))
131 #endif