1 | #ifndef BSWAP_H
|
---|
2 | #define BSWAP_H
|
---|
3 |
|
---|
4 | #include "config-host.h"
|
---|
5 |
|
---|
6 | #include <inttypes.h>
|
---|
7 |
|
---|
8 | #ifdef HAVE_BYTESWAP_H
|
---|
9 | #include <byteswap.h>
|
---|
10 | #else
|
---|
11 |
|
---|
12 | #define bswap_16(x) __extension__ /* <- VBOX */ \
|
---|
13 | ({ \
|
---|
14 | uint16_t __x = (x); \
|
---|
15 | ((uint16_t)( \
|
---|
16 | (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
|
---|
17 | (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
|
---|
18 | })
|
---|
19 |
|
---|
20 | #define bswap_32(x) __extension__ /* <- VBOX */ \
|
---|
21 | ({ \
|
---|
22 | uint32_t __x = (x); \
|
---|
23 | ((uint32_t)( \
|
---|
24 | (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
|
---|
25 | (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
|
---|
26 | (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
|
---|
27 | (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
|
---|
28 | })
|
---|
29 |
|
---|
30 | #define bswap_64(x) __extension__ /* <- VBOX */ \
|
---|
31 | ({ \
|
---|
32 | uint64_t __x = (x); \
|
---|
33 | ((uint64_t)( \
|
---|
34 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
|
---|
35 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
|
---|
36 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
|
---|
37 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
|
---|
38 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
|
---|
39 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
|
---|
40 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
|
---|
41 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
|
---|
42 | })
|
---|
43 |
|
---|
44 | #endif /* !HAVE_BYTESWAP_H */
|
---|
45 |
|
---|
46 | #ifndef bswap16 /* BSD endian.h clash */
|
---|
47 | static inline uint16_t bswap16(uint16_t x)
|
---|
48 | {
|
---|
49 | return bswap_16(x);
|
---|
50 | }
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #ifndef bswap32 /* BSD endian.h clash */
|
---|
54 | static inline uint32_t bswap32(uint32_t x)
|
---|
55 | {
|
---|
56 | return bswap_32(x);
|
---|
57 | }
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | #ifndef bswap64 /* BSD endian.h clash. */
|
---|
61 | static inline uint64_t bswap64(uint64_t x)
|
---|
62 | {
|
---|
63 | return bswap_64(x);
|
---|
64 | }
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | static inline void bswap16s(uint16_t *s)
|
---|
68 | {
|
---|
69 | *s = bswap16(*s);
|
---|
70 | }
|
---|
71 |
|
---|
72 | static inline void bswap32s(uint32_t *s)
|
---|
73 | {
|
---|
74 | *s = bswap32(*s);
|
---|
75 | }
|
---|
76 |
|
---|
77 | static inline void bswap64s(uint64_t *s)
|
---|
78 | {
|
---|
79 | *s = bswap64(*s);
|
---|
80 | }
|
---|
81 |
|
---|
82 | #if defined(WORDS_BIGENDIAN)
|
---|
83 | #define be_bswap(v, size) (v)
|
---|
84 | #define le_bswap(v, size) bswap ## size(v)
|
---|
85 | #define be_bswaps(v, size)
|
---|
86 | #define le_bswaps(p, size) *p = bswap ## size(*p);
|
---|
87 | #else
|
---|
88 | #define le_bswap(v, size) (v)
|
---|
89 | #define be_bswap(v, size) bswap ## size(v)
|
---|
90 | #define le_bswaps(v, size)
|
---|
91 | #define be_bswaps(p, size) *p = bswap ## size(*p);
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | #define CPU_CONVERT(endian, size, type)\
|
---|
95 | static inline type endian ## size ## _to_cpu(type v)\
|
---|
96 | {\
|
---|
97 | return endian ## _bswap(v, size);\
|
---|
98 | }\
|
---|
99 | \
|
---|
100 | static inline type cpu_to_ ## endian ## size(type v)\
|
---|
101 | {\
|
---|
102 | return endian ## _bswap(v, size);\
|
---|
103 | }\
|
---|
104 | \
|
---|
105 | static inline void endian ## size ## _to_cpus(type *p)\
|
---|
106 | {\
|
---|
107 | endian ## _bswaps(p, size)\
|
---|
108 | }\
|
---|
109 | \
|
---|
110 | static inline void cpu_to_ ## endian ## size ## s(type *p)\
|
---|
111 | {\
|
---|
112 | endian ## _bswaps(p, size)\
|
---|
113 | }\
|
---|
114 | \
|
---|
115 | static inline type endian ## size ## _to_cpup(const type *p)\
|
---|
116 | {\
|
---|
117 | return endian ## size ## _to_cpu(*p);\
|
---|
118 | }\
|
---|
119 | \
|
---|
120 | static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
|
---|
121 | {\
|
---|
122 | *p = cpu_to_ ## endian ## size(v);\
|
---|
123 | }
|
---|
124 |
|
---|
125 | CPU_CONVERT(be, 16, uint16_t)
|
---|
126 | CPU_CONVERT(be, 32, uint32_t)
|
---|
127 | CPU_CONVERT(be, 64, uint64_t)
|
---|
128 |
|
---|
129 | CPU_CONVERT(le, 16, uint16_t)
|
---|
130 | CPU_CONVERT(le, 32, uint32_t)
|
---|
131 | CPU_CONVERT(le, 64, uint64_t)
|
---|
132 |
|
---|
133 | /* unaligned versions (optimized for frequent unaligned accesses)*/
|
---|
134 |
|
---|
135 | #if defined(__i386__) || defined(__powerpc__)
|
---|
136 |
|
---|
137 | #define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
|
---|
138 | #define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
|
---|
139 | #define le16_to_cpupu(p) le16_to_cpup(p)
|
---|
140 | #define le32_to_cpupu(p) le32_to_cpup(p)
|
---|
141 | #define be32_to_cpupu(p) be32_to_cpup(p)
|
---|
142 |
|
---|
143 | #define cpu_to_be16wu(p, v) cpu_to_be16w(p, v)
|
---|
144 | #define cpu_to_be32wu(p, v) cpu_to_be32w(p, v)
|
---|
145 |
|
---|
146 | #else
|
---|
147 |
|
---|
148 | static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
|
---|
149 | {
|
---|
150 | uint8_t *p1 = (uint8_t *)p;
|
---|
151 |
|
---|
152 | p1[0] = (uint8_t)v;
|
---|
153 | p1[1] = v >> 8;
|
---|
154 | }
|
---|
155 |
|
---|
156 | static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
|
---|
157 | {
|
---|
158 | uint8_t *p1 = (uint8_t *)p;
|
---|
159 |
|
---|
160 | p1[0] = (uint8_t)v;
|
---|
161 | p1[1] = v >> 8;
|
---|
162 | p1[2] = v >> 16;
|
---|
163 | p1[3] = v >> 24;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static inline uint16_t le16_to_cpupu(const uint16_t *p)
|
---|
167 | {
|
---|
168 | const uint8_t *p1 = (const uint8_t *)p;
|
---|
169 | return p1[0] | (p1[1] << 8);
|
---|
170 | }
|
---|
171 |
|
---|
172 | static inline uint32_t le32_to_cpupu(const uint32_t *p)
|
---|
173 | {
|
---|
174 | const uint8_t *p1 = (const uint8_t *)p;
|
---|
175 | return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
|
---|
176 | }
|
---|
177 |
|
---|
178 | static inline uint32_t be32_to_cpupu(const uint32_t *p)
|
---|
179 | {
|
---|
180 | const uint8_t *p1 = (const uint8_t *)p;
|
---|
181 | return p1[3] | (p1[2] << 8) | (p1[1] << 16) | (p1[0] << 24);
|
---|
182 | }
|
---|
183 |
|
---|
184 | static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
|
---|
185 | {
|
---|
186 | uint8_t *p1 = (uint8_t *)p;
|
---|
187 |
|
---|
188 | p1[0] = v >> 8;
|
---|
189 | p1[1] = (uint8_t)v;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
|
---|
193 | {
|
---|
194 | uint8_t *p1 = (uint8_t *)p;
|
---|
195 |
|
---|
196 | p1[0] = v >> 24;
|
---|
197 | p1[1] = v >> 16;
|
---|
198 | p1[2] = v >> 8;
|
---|
199 | p1[3] = (uint8_t)v;
|
---|
200 | }
|
---|
201 |
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | #ifdef WORDS_BIGENDIAN
|
---|
205 | #define cpu_to_32wu cpu_to_be32wu
|
---|
206 | #else
|
---|
207 | #define cpu_to_32wu cpu_to_le32wu
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | #undef le_bswap
|
---|
211 | #undef be_bswap
|
---|
212 | #undef le_bswaps
|
---|
213 | #undef be_bswaps
|
---|
214 |
|
---|
215 | #endif /* BSWAP_H */
|
---|