1 #ifndef __INC_ENDIANFREE_H
2 #define __INC_ENDIANFREE_H
5 * Call endian free function when
6 * 1. Read/write packet content.
7 * 2. Before write integer to IO.
8 * 3. After read integer from IO.
10 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12 typedef enum{false = 0, true} bool;
16 #define __MACHINE_LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
17 #define __MACHINE_BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net, ppc */
19 #define BYTE_ORDER __MACHINE_LITTLE_ENDIAN
21 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
23 #define EF1Byte(_val) ((u8)(_val))
24 #define EF2Byte(_val) ((u16)(_val))
25 #define EF4Byte(_val) ((u32)(_val))
29 #define EF1Byte(_val) ((u8)(_val))
30 #define EF2Byte(_val) (((((u16)(_val))&0x00ff)<<8)|((((u16)(_val))&0xff00)>>8))
31 #define EF4Byte(_val) (((((u32)(_val))&0x000000ff)<<24)|\
32 ((((u32)(_val))&0x0000ff00)<<8)|\
33 ((((u32)(_val))&0x00ff0000)>>8)|\
34 ((((u32)(_val))&0xff000000)>>24))
37 // Read data from memory
38 #define ReadEF1Byte(_ptr) EF1Byte(*((u8 *)(_ptr)))
39 #define ReadEF2Byte(_ptr) EF2Byte(*((u16 *)(_ptr)))
40 #define ReadEF4Byte(_ptr) EF4Byte(*((u32 *)(_ptr)))
42 // Write data to memory
43 #define WriteEF1Byte(_ptr, _val) (*((u8 *)(_ptr)))=EF1Byte(_val)
44 #define WriteEF2Byte(_ptr, _val) (*((u16 *)(_ptr)))=EF2Byte(_val)
45 #define WriteEF4Byte(_ptr, _val) (*((u32 *)(_ptr)))=EF4Byte(_val)
46 // Convert Host system specific byte ording (litten or big endia) to Network byte ording (big endian).
47 // 2006.05.07, by rcnjko.
48 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
49 #define H2N1BYTE(_val) ((u8)(_val))
50 #define H2N2BYTE(_val) (((((u16)(_val))&0x00ff)<<8)|\
51 ((((u16)(_val))&0xff00)>>8))
52 #define H2N4BYTE(_val) (((((u32)(_val))&0x000000ff)<<24)|\
53 ((((u32)(_val))&0x0000ff00)<<8) |\
54 ((((u32)(_val))&0x00ff0000)>>8) |\
55 ((((u32)(_val))&0xff000000)>>24))
57 #define H2N1BYTE(_val) ((u8)(_val))
58 #define H2N2BYTE(_val) ((u16)(_val))
59 #define H2N4BYTE(_val) ((u32)(_val))
62 // Convert from Network byte ording (big endian) to Host system specific byte ording (litten or big endia).
63 // 2006.05.07, by rcnjko.
64 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
65 #define N2H1BYTE(_val) ((u8)(_val))
66 #define N2H2BYTE(_val) (((((u16)(_val))&0x00ff)<<8)|\
67 ((((u16)(_val))&0xff00)>>8))
68 #define N2H4BYTE(_val) (((((u32)(_val))&0x000000ff)<<24)|\
69 ((((u32)(_val))&0x0000ff00)<<8) |\
70 ((((u32)(_val))&0x00ff0000)>>8) |\
71 ((((u32)(_val))&0xff000000)>>24))
73 #define N2H1BYTE(_val) ((u8)(_val))
74 #define N2H2BYTE(_val) ((u16)(_val))
75 #define N2H4BYTE(_val) ((u32)(_val))
80 // BIT_LEN_MASK_32(0) => 0x00000000
81 // BIT_LEN_MASK_32(1) => 0x00000001
82 // BIT_LEN_MASK_32(2) => 0x00000003
83 // BIT_LEN_MASK_32(32) => 0xFFFFFFFF
85 #define BIT_LEN_MASK_32(__BitLen) (0xFFFFFFFF >> (32 - (__BitLen)))
88 // BIT_OFFSET_LEN_MASK_32(0, 2) => 0x00000003
89 // BIT_OFFSET_LEN_MASK_32(16, 2) => 0x00030000
91 #define BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) (BIT_LEN_MASK_32(__BitLen) << (__BitOffset))
95 // Return 4-byte value in host byte ordering from
96 // 4-byte pointer in litten-endian system.
98 #define LE_P4BYTE_TO_HOST_4BYTE(__pStart) (EF4Byte(*((u32 *)(__pStart))))
102 // Translate subfield (continuous bits in little-endian) of 4-byte value in litten byte to
103 // 4-byte value in host byte ordering.
105 #define LE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
107 ( LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset) ) \
109 BIT_LEN_MASK_32(__BitLen) \
114 // Mask subfield (continuous bits in little-endian) of 4-byte value in litten byte oredering
115 // and return the result in 4-byte value in host byte ordering.
117 #define LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
119 LE_P4BYTE_TO_HOST_4BYTE(__pStart) \
121 ( ~BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) ) \
126 // Set subfield of little-endian 4-byte value to specified value.
128 #define SET_BITS_TO_LE_4BYTE(__pStart, __BitOffset, __BitLen, __Value) \
129 *((u32 *)(__pStart)) = \
131 LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
133 ( (((u32)__Value) & BIT_LEN_MASK_32(__BitLen)) << (__BitOffset) ) \
137 #define BIT_LEN_MASK_16(__BitLen) \
138 (0xFFFF >> (16 - (__BitLen)))
140 #define BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) \
141 (BIT_LEN_MASK_16(__BitLen) << (__BitOffset))
143 #define LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
144 (EF2Byte(*((u16 *)(__pStart))))
146 #define LE_BITS_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
148 ( LE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset) ) \
150 BIT_LEN_MASK_16(__BitLen) \
153 #define LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
155 LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
157 ( ~BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) ) \
160 #define SET_BITS_TO_LE_2BYTE(__pStart, __BitOffset, __BitLen, __Value) \
161 *((u16 *)(__pStart)) = \
163 LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
165 ( (((u16)__Value) & BIT_LEN_MASK_16(__BitLen)) << (__BitOffset) ) \
168 #define BIT_LEN_MASK_8(__BitLen) \
169 (0xFF >> (8 - (__BitLen)))
171 #define BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) \
172 (BIT_LEN_MASK_8(__BitLen) << (__BitOffset))
174 #define LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
175 (EF1Byte(*((u8 *)(__pStart))))
177 #define LE_BITS_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
179 ( LE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset) ) \
181 BIT_LEN_MASK_8(__BitLen) \
184 #define LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
186 LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
188 ( ~BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) ) \
191 #define SET_BITS_TO_LE_1BYTE(__pStart, __BitOffset, __BitLen, __Value) \
192 *((u8 *)(__pStart)) = \
194 LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
196 ( (((u8)__Value) & BIT_LEN_MASK_8(__BitLen)) << (__BitOffset) ) \
199 #endif // #ifndef __INC_ENDIANFREE_H