Wireshark  4.3.0
The Wireshark network protocol analyzer
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  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1999 Gerald Combs
11  *
12  * SPDX-License-Identifier: GPL-2.0-or-later
13  */
14 
15 #ifndef __wimax_bits_h__
16 #define __wimax_bits_h__
17 
18 #include <wsutil/pint.h>
19 
20 /********************************************************************
21  * Functions for working with nibbles and bits
22  */
23 
24 /* SWAR functions */
25 #define _BITS(n,hi,lo) (((n)>>(lo))&((1<<(((hi)-(lo))+1))-1))
26 #define _ADD_SWAP(x,y) { (x) = (x) + (y); (y) = (x) - (y); (x) = (x) - (y); }
27 #define _XOR_SWAP(x,y) { (x) ^= (y); (y) ^= (x); (x) ^= (y); }
28 #define _SWAP(x,y) do { int t = (x); (x) = (y); (y) = t; } while(0)
29 
30 
31 /********************************************************************
32  * Functions for working with nibbles
33  *
34  */
35 
36 #define NIBBLE_MASK 0x0F
37 #define BYTE_MASK 0xFF
38 
39 /* extract the nibble at the given nibble address 'n' of tvbuff_t 't' */
40 #define TVB_NIB_NIBBLE(n,t) \
41  (((n) & 1) \
42  ? tvb_get_guint8((t), (n)/2) & NIBBLE_MASK \
43  : (tvb_get_guint8((t), (n)/2) >> 4) & NIBBLE_MASK)
44 
45 /* extract the byte at the given nibble address 'n' of tvbuff_t 't' */
46 #define TVB_NIB_BYTE(n,t) \
47  (n) & 1 \
48  ? (tvb_get_ntohs((t), (n)/2) >> 4) & BYTE_MASK \
49  : tvb_get_guint8((t), (n)/2)
50 
51 /* extract 12 bits at the given nibble address */
52 #define TVB_NIB_BITS12(n,t) \
53  (TVB_NIB_NIBBLE(n+2,t) | (TVB_NIB_BYTE(n,t) << 4))
54 
55 /* extract the word at the given nibble address 'n' of tvbuff_t 't' */
56 #define TVB_NIB_WORD(n,t) \
57  (n) & 1 \
58  ? (int)((tvb_get_ntohl((t), (n)/2) >> 12) & 0x0000FFFF) \
59  : tvb_get_ntohs((t), (n)/2)
60 
61 /* extract the word at the given nibble address 'n' of tvbuff_t 't' */
62 #define TVB_NIB_LONG(n,t) \
63  (n) & 1 \
64  ? (tvb_get_ntohl((t), (n)/2) << 4) | ((tvb_get_guint8((t), (n)/2 + 4) >> 4) & NIBBLE_MASK) \
65  : tvb_get_ntohl((t), (n)/2)
66 
67 /* Only currently used with nib == 1 or 2 */
68 #define TVB_NIB_NIBS(nib, t, num) \
69  ((num) == 1 ? TVB_NIB_NIBBLE(nib,t) : \
70  ((num) == 2 ? TVB_NIB_BYTE(nib,t) : \
71  ((num) == 3 ? TVB_NIB_BITS12(nib,t) : \
72  ((num) == 4 ? TVB_NIB_WORD(nib,t) : \
73  0))))
74 
75 /* to highlight nibfields correctly in wireshark
76  * AddItem(..., WSADDR(buf,bit), WSLEN(bit), ...) */
77 
78 /* determine starting byte to highlight a series of nibbles */
79 #define NIB_ADDR(nib) ((nib)/2)
80 /* determine number of bytes to highlight a series of nibbles */
81 #define NIB_LEN(nib,len) ((1 + ((nib) &1) + (len))/2)
82 
83 #define NIBHI(nib,len) NIB_ADDR(nib),NIB_LEN(nib,len)
84 
85 /********************************************************************
86  * bitfield functions - for extracting bitfields from a buffer
87  *
88  * TODO: 64 bit functions use two 32-bit values;
89  * would be better to use 32+8 bits to avoid overrunning buffers
90  *
91  */
92 
93 /* find the byte-address for the bitfield */
94 #define ADDR(bit) ((bit) / 8)
95 #define ADDR16(bit) ((bit) / 8)
96 #define ADDR32(bit) ((bit) / 8)
97 
98 /* find the offset (from the MSB) to the start of the bitfield */
99 #define OFFSET(bit) ((bit) % 8)
100 #define OFFSET16(bit) ((bit) % 8)
101 #define OFFSET32(bit) ((bit) % 8)
102 
103 /* find the number of bits to shift right (SHIFT64 is upper dword) */
104 #define SHIFT(bit,num) ( 8 - ((bit)%8) - (num))
105 #define SHIFT16(bit,num) (16 - ((bit)%8) - (num))
106 #define SHIFT32(bit,num) (32 - ((bit)%8) - (num))
107 #define SHIFT64a(bit,num) (num - (32 - OFFSET32(bit)))
108 #define SHIFT64b(bit,num) (32 - ((num) - (32 - OFFSET32(bit))))
109 
110 /* create a mask to mask off the bitfield */
111 #define MASK8(num) (0xFF >> (8 - (num)))
112 #define MASK16(num) (0xFFFF >> (16 - (num)))
113 #define MASK32(num) (0xFFFFFFFF >> (32 - (num)))
114 #define MASK64a(bit) (MASK32(32 - OFFSET32(bit)))
115 #define MASK64b(bit,num) (MASK32(num - (32 - OFFSET32(bit))))
116 
117 /* note that if you have a bitfield of length 2 or more, it may cross a
118  * byte boundary so you should use TVB_BIT_BITS16 */
119 
120 /* extract a single bit
121  * bit ... bit address
122  * tvb ... tvbuff_t
123  */
124 #define TVB_BIT_BIT(bit, tvb) \
125  (( tvb_get_guint8(tvb, ADDR(bit)) >> SHIFT(bit,1) ) & 0x1)
126 
127 /* extract bitfield up to 9 bits
128  * bit ... bit address
129  * tvb ... tvbuff_t
130  * num ... length of bitfield
131  */
132 #define TVB_BIT_BITS16(bit, tvb, num) \
133  (( tvb_get_ntohs(tvb, ADDR16(bit)) >> SHIFT16(bit,num) ) & MASK16(num))
134 
135 /* extract bitfield up to 24 bits
136  * bit ... bit address
137  * tvb ... tvbuff_t
138  * num ... length of bitfield
139  */
140 
141 #define TVB_BIT_BITS32(bit, tvb, num) \
142  ((tvb_get_ntohl(tvb, ADDR32(bit)) >> SHIFT32(bit,num) ) & MASK32(num))
143 
144 /* bitfield up to 32 bits */
145 #define TVB_BIT_BITS64a(bit, tvb, num) \
146  ((tvb_get_ntohl(tvb, ADDR32(bit)) & MASK64a(bit)) << SHIFT64a(bit,num))
147 
148 #define TVB_BIT_BITS64b(bit, tvb, num) \
149  ((tvb_get_ntohl(tvb, ADDR32(bit)+4) >> SHIFT64b(bit,num) ) & MASK64b(bit,num))
150 
151 #define TVB_BIT_BITS64(bit, tvb, num) \
152  ( (OFFSET32(bit)+(num)) <= 32 \
153  ? TVB_BIT_BITS32(bit,tvb,num) \
154  : TVB_BIT_BITS64a(bit,tvb,num) \
155  | TVB_BIT_BITS64b(bit,tvb,num) )
156 
157 #define TVB_BIT_BITS(bit, tvb, num) \
158  ((num) == 1 ? (int)TVB_BIT_BIT(bit,tvb) : \
159  ((num) <= 9 ? (int)TVB_BIT_BITS16(bit,tvb,num) : \
160  ((num) <= 24 ? (int)TVB_BIT_BITS32(bit,tvb,num) : \
161  ((num) <= 32 ? (int)TVB_BIT_BITS64(bit,tvb,num) : \
162  (int)0 ))))
163 
164 /* to highlight bitfields correctly in wireshark
165  * AddItem(..., WSADDR(buf,bit), WSLEN(bit), ...) */
166 
167 /* determine starting byte to highlight a series of nibbles */
168 #define BIT_ADDR(bit) (ADDR(bit))
169 /* determine number of bytes to highlight */
170 #define BIT_LEN(bit,len) (1 + ((OFFSET(bit) + len - 1) / 8))
171 
172 #define BITHI(bit,len) BIT_ADDR(bit),BIT_LEN(bit,len)
173 
174 /********************************************************************
175  * padding functions - return number of nibbles/bits needed to
176  * pad to a byte boundary */
177 
178 #define BIT_PADDING(bit, bits) ((bit) % (bits)) ? ((bits) - ((bit) % (bits))) : 0
179 #define NIB_PADDING(nib) ((nib) & 0x1)
180 
181 /********************************************************************
182  * conversion functions - between bytes, nibbles, and bits */
183 
184 #define BYTE_TO_BIT(n) ((n) * 8)
185 #define BYTE_TO_NIB(n) ((n) * 2)
186 
187 #define BIT_TO_BYTE(n) ((n) / 8)
188 #define BIT_TO_NIB(n) ((n) / 4)
189 
190 #define NIB_TO_BYTE(n) ((n) / 2)
191 #define NIB_TO_BIT(n) ((n) * 4)
192 
193 #endif
194