Fix wimax UL-MAP ext IE decode error.
[obnox/wireshark/wip.git] / plugins / wimax / wimax_bits.h
1 /* wimax_bits.h
2  * WiMax MAC Management UL-MAP Message decoder
3  *
4  * Copyright (c) 2007 by Intel Corporation.
5  *
6  * Author: Mike Harvey <michael.harvey@intel.com>
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1999 Gerald Combs
13  *
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.
18  *
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.
23  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifndef __wimax_bits_h__
30 #define __wimax_bits_h__
31
32 /********************************************************************
33  * Functions for working with nibbles and bits
34  */
35
36 /* SWAR functions */
37 #define _BITS(n,hi,lo) (((n)>>(lo))&((1<<(((hi)-(lo))+1))-1))
38 #define _ADD_SWAP(x,y) { (x) = (x) + (y); (y) = (x) - (y); (x) = (x) - (y); }
39 #define _XOR_SWAP(x,y) { (x) ^= (y); (y) ^= (x); (x) ^= (y); }
40 #define _SWAP(x,y) do { int t = (x); (x) = (y); (y) = t; } while(0)
41
42
43 /********************************************************************
44  * Functions for working with nibbles
45  *
46  */
47
48 #define NIBBLE_MASK 0x0F
49 #define BYTE_MASK 0xFF
50
51 /* extract the nibble at the given nibble address 'n' of buffer 'b' */
52 #define NIB_NIBBLE(n,b) \
53     (((n) & 1) \
54     ?  (b)[(n)/2] & NIBBLE_MASK \
55     : ((b)[(n)/2] >> 4) & NIBBLE_MASK)
56
57 /* extract the byte at the given nibble address 'n' of buffer 'b' */
58 #define NIB_BYTE(n,b) \
59     (n) & 1 \
60     ? (pntohs( (b)+(n)/2 ) >> 4) & BYTE_MASK \
61     : (b)[(n)/2]
62     /*
63     ? (pletohs((b)+(n)/2) >> 4) & BYTE_MASK \
64     */
65
66 /* extract 12 bits at the given nibble address */
67 #define NIB_BITS12(n,b) \
68       (NIB_NIBBLE(n,b+1) | (NIB_BYTE(n,b) << 4))
69
70 /* extract the word at the given nibble address 'n' of buffer 'b' */
71 #define NIB_WORD(n,b) \
72     (n) & 1 \
73     ? (gint)((pntohl(((b) + (n)/2)) >> 12) & 0x0000FFFF) \
74     : pntohs((b) + (n)/2)
75     /*
76     : pletohs((b) + (n)/2)
77     ? (pletohl((b)+(n)/2) >> 12) & 0x0000FFFF \
78     */
79
80 /* extract the word at the given nibble address 'n' of buffer 'b' */
81 #define NIB_LONG(n,b) \
82     (n) & 1 \
83     ? (pntohl(((b) + (n)/2)) << 4) | (((b)[(n)/2 + 4] >> 4) & NIBBLE_MASK) \
84     : pntohl((b) + (n)/2)
85     /*
86     ? (pletohl((b) + (n)/2) << 4) | (((b)[(n)/2 + 4] >> 4) & NIBBLE_MASK) \
87     : pletohl((b) + (n)/2)
88     */
89
90 /* Only currently used with nib == 1 or 2 */
91 #define NIB_NIBS(nib, buf, num) \
92     ((num) == 1 ? NIB_NIBBLE(nib,buf) : \
93     ((num) == 2 ? NIB_BYTE(nib,buf) : \
94     ((num) == 3 ? NIB_BITS12(nib,buf) : \
95     ((num) == 4 ? NIB_WORD(nib,buf) : \
96     0))))
97
98
99 /* to highlight nibfields correctly in wireshark
100  * AddItem(..., WSADDR(buf,bit), WSLEN(bit), ...) */
101
102 /* determine starting byte to highlight a series of nibbles */
103 #define NIB_ADDR(nib) ((nib)/2)
104 /* determine number of bytes to highlight a series of nibbles */
105 #define NIB_LEN(nib,len)   ((1 +  ((nib)   &1) + (len))/2)
106
107 #define NIBHI(nib,len)  NIB_ADDR(nib),NIB_LEN(nib,len)
108
109 /********************************************************************
110  * bitfield functions - for extracting bitfields from a buffer
111  *
112  * TODO: 64 bit functions use two 32-bit values;
113  * would be better to use 32+8 bits to avoid overrunning buffers
114  *
115  */
116
117 /* find the byte-address for the bitfield */
118 #define ADDR(bit)   ((bit) / 8)
119 #define ADDR16(bit) ((bit) / 8)
120 #define ADDR32(bit) ((bit) / 8)
121
122 /* find the offset (from the MSB) to the start of the bitfield */
123 #define OFFSET(bit)    ((bit) % 8)
124 #define OFFSET16(bit)  ((bit) % 8)
125 #define OFFSET32(bit)  ((bit) % 8)
126
127 /* find the number of bits to shift right (SHIFT64 is upper dword) */
128 #define SHIFT(bit,num)    ( 8 - ((bit)%8) - (num))
129 #define SHIFT16(bit,num)  (16 - ((bit)%8) - (num))
130 #define SHIFT32(bit,num)  (32 - ((bit)%8) - (num))
131 #define SHIFT64a(bit,num) (num - (32 - OFFSET32(bit)))
132 #define SHIFT64b(bit,num) (32 - ((num) - (32 - OFFSET32(bit))))
133
134 /* create a mask to mask off the bitfield */
135 #define MASK8(num)       (0xFF >> (8 - (num)))
136 #define MASK16(num)      (0xFFFF >> (16 - (num)))
137 #define MASK32(num)      (0xFFFFFFFF >> (32 - (num)))
138 #define MASK64a(bit)     (MASK32(32 - OFFSET32(bit)))
139 #define MASK64b(bit,num) (MASK32(num - (32 - OFFSET32(bit))))
140
141 /* note that if you have a bitfield of length 2 or more, it may cross a
142  * byte boundary so you should use BIT_BITS16 */
143
144 /* extract a single bit
145  * bit ... bit address
146  * buf ... buffer
147  */
148 #define BIT_BIT(bit, buf) \
149     (( (buf)[ADDR(bit)] >> SHIFT(bit,1) ) & 0x1)
150
151 /* extract bitfield up to 9 bits
152  * bit ... bit address
153  * buf ... buffer
154  * num ... length of bitfield
155  */
156 #define BIT_BITS16(bit, buf, num) \
157     (( pntohs(buf+ADDR16(bit)) >> SHIFT16(bit,num) ) & MASK16(num))
158
159 /* extract bitfield up to 24 bits
160  * bit ... bit address
161  * buf ... buffer
162  * num ... length of bitfield
163  */
164
165 #define BIT_BITS32(bit, buf, num) \
166       ((pntohl(buf+ADDR32(bit)) >> SHIFT32(bit,num) ) & MASK32(num))
167
168 /* bitfield up to 32 bits */
169 #define BIT_BITS64a(bit, buf, num) \
170       ((pntohl(buf+ADDR32(bit)) & MASK64a(bit)) << SHIFT64a(bit,num))
171
172 #define BIT_BITS64b(bit, buf, num) \
173       ((pntohl(buf+ADDR32(bit)+4) >> SHIFT64b(bit,num) ) & MASK64b(bit,num))
174
175 #define BIT_BITS64(bit, buf, num) \
176       ( (OFFSET32(bit)+(num)) <= 32 \
177       ? BIT_BITS32(bit,buf,num) \
178       : BIT_BITS64a(bit,buf,num) \
179       | BIT_BITS64b(bit,buf,num) )
180
181 #define BIT_BITS(bit, buf, num) \
182     ((num) ==  1 ? (gint)BIT_BIT(bit,buf) : \
183     ((num) <=  9 ? (gint)BIT_BITS16(bit,buf,num) : \
184     ((num) <= 24 ? (gint)BIT_BITS32(bit,buf,num) : \
185     ((num) <= 32 ? (gint)BIT_BITS64(bit,buf,num) : \
186                    (gint)0 ))))
187
188 /* to highlight bitfields correctly in wireshark
189  * AddItem(..., WSADDR(buf,bit), WSLEN(bit), ...) */
190
191 /* determine starting byte to highlight a series of nibbles */
192 #define BIT_ADDR(bit) (ADDR(bit))
193 /* determine number of bytes to highlight */
194 #define BIT_LEN(bit,len) (1 + ((OFFSET(bit) + len - 1) / 8))
195
196 #define BITHI(bit,len)  BIT_ADDR(bit),BIT_LEN(bit,len)
197
198 /* CONVENIENCE FUNCTIONS */
199
200 #define BIT_NIBBLE(bit,buf) BIT_BITS16(bit,buf,4)
201 #define BIT_BYTE(bit,buf) BIT_BITS16(bit,buf,8)
202 #define BIT_WORD(bit,buf) BIT_BITS32(bit,buf,16)
203 #define BIT_WORD24(bit,buf) BIT_BITS32(bit,buf,24)
204 #define BIT_LONG(bit,buf) BIT_BITS64(bit,buf,32)
205
206 /********************************************************************
207  * padding functions - return number of nibbles/bits needed to
208  * pad to a byte boundary */
209
210 #define BIT_PADDING(bit, bits) ((bit) % (bits)) ? ((bits) - ((bit) % (bits))) : 0
211 #define NIB_PADDING(nib) ((nib) & 0x1)
212
213 /********************************************************************
214  * conversion functions - between bytes, nibbles, and bits */
215
216 #define BYTE_TO_BIT(n)  ((n) * 8)
217 #define BYTE_TO_NIB(n)  ((n) * 2)
218
219 #define BIT_TO_BYTE(n)  ((n) / 8)
220 #define BIT_TO_NIB(n)   ((n) / 4)
221
222 #define NIB_TO_BYTE(n)  ((n) / 2)
223 #define NIB_TO_BIT(n)   ((n) * 4)
224
225
226
227 #ifndef UNREFERENCED_PARAMETER
228 #define UNREFERENCED_PARAMETER(x) (x) = (x)
229 #endif
230
231 #endif
232