HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / plugins / wimax / wimax_bits.h
blob6077411bc3851c83261c3865b362f2e60f30937a
1 /* wimax_bits.h
2 * WiMax MAC Management UL-MAP Message decoder
4 * Copyright (c) 2007 by Intel Corporation.
6 * Author: Mike Harvey <michael.harvey@intel.com>
8 * $Id$
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1999 Gerald Combs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #ifndef __wimax_bits_h__
30 #define __wimax_bits_h__
32 #include <wsutil/pint.h>
34 /********************************************************************
35 * Functions for working with nibbles and bits
38 /* SWAR functions */
39 #define _BITS(n,hi,lo) (((n)>>(lo))&((1<<(((hi)-(lo))+1))-1))
40 #define _ADD_SWAP(x,y) { (x) = (x) + (y); (y) = (x) - (y); (x) = (x) - (y); }
41 #define _XOR_SWAP(x,y) { (x) ^= (y); (y) ^= (x); (x) ^= (y); }
42 #define _SWAP(x,y) do { int t = (x); (x) = (y); (y) = t; } while(0)
45 /********************************************************************
46 * Functions for working with nibbles
50 #define NIBBLE_MASK 0x0F
51 #define BYTE_MASK 0xFF
53 /* extract the nibble at the given nibble address 'n' of buffer 'b' */
54 #define NIB_NIBBLE(n,b) \
55 (((n) & 1) \
56 ? (b)[(n)/2] & NIBBLE_MASK \
57 : ((b)[(n)/2] >> 4) & NIBBLE_MASK)
59 /* extract the byte at the given nibble address 'n' of buffer 'b' */
60 #define NIB_BYTE(n,b) \
61 (n) & 1 \
62 ? (pntohs( (b)+(n)/2 ) >> 4) & BYTE_MASK \
63 : (b)[(n)/2]
65 ? (pletohs((b)+(n)/2) >> 4) & BYTE_MASK \
68 /* extract 12 bits at the given nibble address */
69 #define NIB_BITS12(n,b) \
70 (NIB_NIBBLE(n,b+1) | (NIB_BYTE(n,b) << 4))
72 /* extract the word at the given nibble address 'n' of buffer 'b' */
73 #define NIB_WORD(n,b) \
74 (n) & 1 \
75 ? (gint)((pntohl(((b) + (n)/2)) >> 12) & 0x0000FFFF) \
76 : pntohs((b) + (n)/2)
78 : pletohs((b) + (n)/2)
79 ? (pletohl((b)+(n)/2) >> 12) & 0x0000FFFF \
82 /* extract the word at the given nibble address 'n' of buffer 'b' */
83 #define NIB_LONG(n,b) \
84 (n) & 1 \
85 ? (pntohl(((b) + (n)/2)) << 4) | (((b)[(n)/2 + 4] >> 4) & NIBBLE_MASK) \
86 : pntohl((b) + (n)/2)
88 ? (pletohl((b) + (n)/2) << 4) | (((b)[(n)/2 + 4] >> 4) & NIBBLE_MASK) \
89 : pletohl((b) + (n)/2)
92 /* Only currently used with nib == 1 or 2 */
93 #define NIB_NIBS(nib, buf, num) \
94 ((num) == 1 ? NIB_NIBBLE(nib,buf) : \
95 ((num) == 2 ? NIB_BYTE(nib,buf) : \
96 ((num) == 3 ? NIB_BITS12(nib,buf) : \
97 ((num) == 4 ? NIB_WORD(nib,buf) : \
98 0))))
101 /* to highlight nibfields correctly in wireshark
102 * AddItem(..., WSADDR(buf,bit), WSLEN(bit), ...) */
104 /* determine starting byte to highlight a series of nibbles */
105 #define NIB_ADDR(nib) ((nib)/2)
106 /* determine number of bytes to highlight a series of nibbles */
107 #define NIB_LEN(nib,len) ((1 + ((nib) &1) + (len))/2)
109 #define NIBHI(nib,len) NIB_ADDR(nib),NIB_LEN(nib,len)
111 /********************************************************************
112 * bitfield functions - for extracting bitfields from a buffer
114 * TODO: 64 bit functions use two 32-bit values;
115 * would be better to use 32+8 bits to avoid overrunning buffers
119 /* find the byte-address for the bitfield */
120 #define ADDR(bit) ((bit) / 8)
121 #define ADDR16(bit) ((bit) / 8)
122 #define ADDR32(bit) ((bit) / 8)
124 /* find the offset (from the MSB) to the start of the bitfield */
125 #define OFFSET(bit) ((bit) % 8)
126 #define OFFSET16(bit) ((bit) % 8)
127 #define OFFSET32(bit) ((bit) % 8)
129 /* find the number of bits to shift right (SHIFT64 is upper dword) */
130 #define SHIFT(bit,num) ( 8 - ((bit)%8) - (num))
131 #define SHIFT16(bit,num) (16 - ((bit)%8) - (num))
132 #define SHIFT32(bit,num) (32 - ((bit)%8) - (num))
133 #define SHIFT64a(bit,num) (num - (32 - OFFSET32(bit)))
134 #define SHIFT64b(bit,num) (32 - ((num) - (32 - OFFSET32(bit))))
136 /* create a mask to mask off the bitfield */
137 #define MASK8(num) (0xFF >> (8 - (num)))
138 #define MASK16(num) (0xFFFF >> (16 - (num)))
139 #define MASK32(num) (0xFFFFFFFF >> (32 - (num)))
140 #define MASK64a(bit) (MASK32(32 - OFFSET32(bit)))
141 #define MASK64b(bit,num) (MASK32(num - (32 - OFFSET32(bit))))
143 /* note that if you have a bitfield of length 2 or more, it may cross a
144 * byte boundary so you should use BIT_BITS16 */
146 /* extract a single bit
147 * bit ... bit address
148 * buf ... buffer
150 #define BIT_BIT(bit, buf) \
151 (( (buf)[ADDR(bit)] >> SHIFT(bit,1) ) & 0x1)
153 /* extract bitfield up to 9 bits
154 * bit ... bit address
155 * buf ... buffer
156 * num ... length of bitfield
158 #define BIT_BITS16(bit, buf, num) \
159 (( pntohs(buf+ADDR16(bit)) >> SHIFT16(bit,num) ) & MASK16(num))
161 /* extract bitfield up to 24 bits
162 * bit ... bit address
163 * buf ... buffer
164 * num ... length of bitfield
167 #define BIT_BITS32(bit, buf, num) \
168 ((pntohl(buf+ADDR32(bit)) >> SHIFT32(bit,num) ) & MASK32(num))
170 /* bitfield up to 32 bits */
171 #define BIT_BITS64a(bit, buf, num) \
172 ((pntohl(buf+ADDR32(bit)) & MASK64a(bit)) << SHIFT64a(bit,num))
174 #define BIT_BITS64b(bit, buf, num) \
175 ((pntohl(buf+ADDR32(bit)+4) >> SHIFT64b(bit,num) ) & MASK64b(bit,num))
177 #define BIT_BITS64(bit, buf, num) \
178 ( (OFFSET32(bit)+(num)) <= 32 \
179 ? BIT_BITS32(bit,buf,num) \
180 : BIT_BITS64a(bit,buf,num) \
181 | BIT_BITS64b(bit,buf,num) )
183 #define BIT_BITS(bit, buf, num) \
184 ((num) == 1 ? (gint)BIT_BIT(bit,buf) : \
185 ((num) <= 9 ? (gint)BIT_BITS16(bit,buf,num) : \
186 ((num) <= 24 ? (gint)BIT_BITS32(bit,buf,num) : \
187 ((num) <= 32 ? (gint)BIT_BITS64(bit,buf,num) : \
188 (gint)0 ))))
190 /* to highlight bitfields correctly in wireshark
191 * AddItem(..., WSADDR(buf,bit), WSLEN(bit), ...) */
193 /* determine starting byte to highlight a series of nibbles */
194 #define BIT_ADDR(bit) (ADDR(bit))
195 /* determine number of bytes to highlight */
196 #define BIT_LEN(bit,len) (1 + ((OFFSET(bit) + len - 1) / 8))
198 #define BITHI(bit,len) BIT_ADDR(bit),BIT_LEN(bit,len)
200 /* CONVENIENCE FUNCTIONS */
202 #define BIT_NIBBLE(bit,buf) BIT_BITS16(bit,buf,4)
203 #define BIT_BYTE(bit,buf) BIT_BITS16(bit,buf,8)
204 #define BIT_WORD(bit,buf) BIT_BITS32(bit,buf,16)
205 #define BIT_WORD24(bit,buf) BIT_BITS32(bit,buf,24)
206 #define BIT_LONG(bit,buf) BIT_BITS64(bit,buf,32)
208 /********************************************************************
209 * padding functions - return number of nibbles/bits needed to
210 * pad to a byte boundary */
212 #define BIT_PADDING(bit, bits) ((bit) % (bits)) ? ((bits) - ((bit) % (bits))) : 0
213 #define NIB_PADDING(nib) ((nib) & 0x1)
215 /********************************************************************
216 * conversion functions - between bytes, nibbles, and bits */
218 #define BYTE_TO_BIT(n) ((n) * 8)
219 #define BYTE_TO_NIB(n) ((n) * 2)
221 #define BIT_TO_BYTE(n) ((n) / 8)
222 #define BIT_TO_NIB(n) ((n) / 4)
224 #define NIB_TO_BYTE(n) ((n) / 2)
225 #define NIB_TO_BIT(n) ((n) * 4)
227 #endif