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.
11 #define __MACHINE_LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
12 #define __MACHINE_BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net, ppc */
14 #define BYTE_ORDER __MACHINE_LITTLE_ENDIAN
16 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
18 #define EF1Byte(_val) ((u8)(_val))
19 #define EF2Byte(_val) ((u16)(_val))
20 #define EF4Byte(_val) ((u32)(_val))
24 #define EF1Byte(_val) ((u8)(_val))
25 #define EF2Byte(_val) (((((u16)(_val))&0x00ff)<<8)|((((u16)(_val))&0xff00)>>8))
26 #define EF4Byte(_val) (((((u32)(_val))&0x000000ff)<<24)|\
27 ((((u32)(_val))&0x0000ff00)<<8)|\
28 ((((u32)(_val))&0x00ff0000)>>8)|\
29 ((((u32)(_val))&0xff000000)>>24))
32 // Read data from memory
33 #define ReadEF1Byte(_ptr) EF1Byte(*((u8 *)(_ptr)))
34 #define ReadEF2Byte(_ptr) EF2Byte(*((u16 *)(_ptr)))
35 #define ReadEF4Byte(_ptr) EF4Byte(*((u32 *)(_ptr)))
37 // Write data to memory
38 #define WriteEF1Byte(_ptr, _val) (*((u8 *)(_ptr)))=EF1Byte(_val)
39 #define WriteEF2Byte(_ptr, _val) (*((u16 *)(_ptr)))=EF2Byte(_val)
40 #define WriteEF4Byte(_ptr, _val) (*((u32 *)(_ptr)))=EF4Byte(_val)
41 // Convert Host system specific byte ording (litten or big endia) to Network byte ording (big endian).
42 // 2006.05.07, by rcnjko.
43 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
44 #define H2N1BYTE(_val) ((u8)(_val))
45 #define H2N2BYTE(_val) (((((u16)(_val))&0x00ff)<<8)|\
46 ((((u16)(_val))&0xff00)>>8))
47 #define H2N4BYTE(_val) (((((u32)(_val))&0x000000ff)<<24)|\
48 ((((u32)(_val))&0x0000ff00)<<8) |\
49 ((((u32)(_val))&0x00ff0000)>>8) |\
50 ((((u32)(_val))&0xff000000)>>24))
52 #define H2N1BYTE(_val) ((u8)(_val))
53 #define H2N2BYTE(_val) ((u16)(_val))
54 #define H2N4BYTE(_val) ((u32)(_val))
57 // Convert from Network byte ording (big endian) to Host system specific byte ording (litten or big endia).
58 // 2006.05.07, by rcnjko.
59 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
60 #define N2H1BYTE(_val) ((u8)(_val))
61 #define N2H2BYTE(_val) (((((u16)(_val))&0x00ff)<<8)|\
62 ((((u16)(_val))&0xff00)>>8))
63 #define N2H4BYTE(_val) (((((u32)(_val))&0x000000ff)<<24)|\
64 ((((u32)(_val))&0x0000ff00)<<8) |\
65 ((((u32)(_val))&0x00ff0000)>>8) |\
66 ((((u32)(_val))&0xff000000)>>24))
68 #define N2H1BYTE(_val) ((u8)(_val))
69 #define N2H2BYTE(_val) ((u16)(_val))
70 #define N2H4BYTE(_val) ((u32)(_val))
75 // BIT_LEN_MASK_32(0) => 0x00000000
76 // BIT_LEN_MASK_32(1) => 0x00000001
77 // BIT_LEN_MASK_32(2) => 0x00000003
78 // BIT_LEN_MASK_32(32) => 0xFFFFFFFF
80 #define BIT_LEN_MASK_32(__BitLen) (0xFFFFFFFF >> (32 - (__BitLen)))
83 // BIT_OFFSET_LEN_MASK_32(0, 2) => 0x00000003
84 // BIT_OFFSET_LEN_MASK_32(16, 2) => 0x00030000
86 #define BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) (BIT_LEN_MASK_32(__BitLen) << (__BitOffset))
90 // Return 4-byte value in host byte ordering from
91 // 4-byte pointer in litten-endian system.
93 #define LE_P4BYTE_TO_HOST_4BYTE(__pStart) (EF4Byte(*((u32 *)(__pStart))))
97 // Translate subfield (continuous bits in little-endian) of 4-byte value in litten byte to
98 // 4-byte value in host byte ordering.
100 #define LE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
102 ( LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset) ) \
104 BIT_LEN_MASK_32(__BitLen) \
109 // Mask subfield (continuous bits in little-endian) of 4-byte value in litten byte oredering
110 // and return the result in 4-byte value in host byte ordering.
112 #define LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
114 LE_P4BYTE_TO_HOST_4BYTE(__pStart) \
116 ( ~BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) ) \
121 // Set subfield of little-endian 4-byte value to specified value.
123 #define SET_BITS_TO_LE_4BYTE(__pStart, __BitOffset, __BitLen, __Value) \
124 *((u32 *)(__pStart)) = \
126 LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
128 ( (((u32)__Value) & BIT_LEN_MASK_32(__BitLen)) << (__BitOffset) ) \
132 #define BIT_LEN_MASK_16(__BitLen) \
133 (0xFFFF >> (16 - (__BitLen)))
135 #define BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) \
136 (BIT_LEN_MASK_16(__BitLen) << (__BitOffset))
138 #define LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
139 (EF2Byte(*((u16 *)(__pStart))))
141 #define LE_BITS_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
143 ( LE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset) ) \
145 BIT_LEN_MASK_16(__BitLen) \
148 #define LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
150 LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
152 ( ~BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) ) \
155 #define SET_BITS_TO_LE_2BYTE(__pStart, __BitOffset, __BitLen, __Value) \
156 *((u16 *)(__pStart)) = \
158 LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
160 ( (((u16)__Value) & BIT_LEN_MASK_16(__BitLen)) << (__BitOffset) ) \
163 #define BIT_LEN_MASK_8(__BitLen) \
164 (0xFF >> (8 - (__BitLen)))
166 #define BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) \
167 (BIT_LEN_MASK_8(__BitLen) << (__BitOffset))
169 #define LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
170 (EF1Byte(*((u8 *)(__pStart))))
172 #define LE_BITS_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
174 ( LE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset) ) \
176 BIT_LEN_MASK_8(__BitLen) \
179 #define LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
181 LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
183 ( ~BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) ) \
186 #define SET_BITS_TO_LE_1BYTE(__pStart, __BitOffset, __BitLen, __Value) \
187 *((u8 *)(__pStart)) = \
189 LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
191 ( (((u8)__Value) & BIT_LEN_MASK_8(__BitLen)) << (__BitOffset) ) \
194 #endif // #ifndef __INC_ENDIANFREE_H