BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file byteorder.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Byte order conversion functions. |
||
32 | * |
||
33 | * hton* functions convert from host to big-endian (network) byte order. |
||
34 | * htol* functions convert from host to little-endian byte order. |
||
35 | * ntoh* functions convert from big-endian (network) to host byte order. |
||
36 | * ltoh* functions convert from little-endian to host byte order. |
||
37 | */ |
||
38 | |||
39 | #ifndef BADVPN_MISC_BYTEORDER_H |
||
40 | #define BADVPN_MISC_BYTEORDER_H |
||
41 | |||
42 | #if (defined(BADVPN_LITTLE_ENDIAN) + defined(BADVPN_BIG_ENDIAN)) != 1 |
||
43 | #error Unknown byte order or too many byte orders |
||
44 | #endif |
||
45 | |||
46 | #include <stdint.h> |
||
47 | |||
48 | static uint16_t badvpn_reverse16 (uint16_t x) |
||
49 | { |
||
50 | uint16_t y; |
||
51 | *((uint8_t *)&y+0) = *((uint8_t *)&x+1); |
||
52 | *((uint8_t *)&y+1) = *((uint8_t *)&x+0); |
||
53 | return y; |
||
54 | } |
||
55 | |||
56 | static uint32_t badvpn_reverse32 (uint32_t x) |
||
57 | { |
||
58 | uint32_t y; |
||
59 | *((uint8_t *)&y+0) = *((uint8_t *)&x+3); |
||
60 | *((uint8_t *)&y+1) = *((uint8_t *)&x+2); |
||
61 | *((uint8_t *)&y+2) = *((uint8_t *)&x+1); |
||
62 | *((uint8_t *)&y+3) = *((uint8_t *)&x+0); |
||
63 | return y; |
||
64 | } |
||
65 | |||
66 | static uint64_t badvpn_reverse64 (uint64_t x) |
||
67 | { |
||
68 | uint64_t y; |
||
69 | *((uint8_t *)&y+0) = *((uint8_t *)&x+7); |
||
70 | *((uint8_t *)&y+1) = *((uint8_t *)&x+6); |
||
71 | *((uint8_t *)&y+2) = *((uint8_t *)&x+5); |
||
72 | *((uint8_t *)&y+3) = *((uint8_t *)&x+4); |
||
73 | *((uint8_t *)&y+4) = *((uint8_t *)&x+3); |
||
74 | *((uint8_t *)&y+5) = *((uint8_t *)&x+2); |
||
75 | *((uint8_t *)&y+6) = *((uint8_t *)&x+1); |
||
76 | *((uint8_t *)&y+7) = *((uint8_t *)&x+0); |
||
77 | return y; |
||
78 | } |
||
79 | |||
80 | static uint8_t hton8 (uint8_t x) |
||
81 | { |
||
82 | return x; |
||
83 | } |
||
84 | |||
85 | static uint8_t htol8 (uint8_t x) |
||
86 | { |
||
87 | return x; |
||
88 | } |
||
89 | |||
90 | #if defined(BADVPN_LITTLE_ENDIAN) |
||
91 | |||
92 | static uint16_t hton16 (uint16_t x) |
||
93 | { |
||
94 | return badvpn_reverse16(x); |
||
95 | } |
||
96 | |||
97 | static uint32_t hton32 (uint32_t x) |
||
98 | { |
||
99 | return badvpn_reverse32(x); |
||
100 | } |
||
101 | |||
102 | static uint64_t hton64 (uint64_t x) |
||
103 | { |
||
104 | return badvpn_reverse64(x); |
||
105 | } |
||
106 | |||
107 | static uint16_t htol16 (uint16_t x) |
||
108 | { |
||
109 | return x; |
||
110 | } |
||
111 | |||
112 | static uint32_t htol32 (uint32_t x) |
||
113 | { |
||
114 | return x; |
||
115 | } |
||
116 | |||
117 | static uint64_t htol64 (uint64_t x) |
||
118 | { |
||
119 | return x; |
||
120 | } |
||
121 | |||
122 | #elif defined(BADVPN_BIG_ENDIAN) |
||
123 | |||
124 | static uint16_t hton16 (uint16_t x) |
||
125 | { |
||
126 | return x; |
||
127 | } |
||
128 | |||
129 | static uint32_t hton32 (uint32_t x) |
||
130 | { |
||
131 | return x; |
||
132 | } |
||
133 | |||
134 | static uint64_t hton64 (uint64_t x) |
||
135 | { |
||
136 | return x; |
||
137 | } |
||
138 | |||
139 | static uint16_t htol16 (uint16_t x) |
||
140 | { |
||
141 | return badvpn_reverse16(x); |
||
142 | } |
||
143 | |||
144 | static uint32_t htol32 (uint32_t x) |
||
145 | { |
||
146 | return badvpn_reverse32(x); |
||
147 | } |
||
148 | |||
149 | static uint64_t htol64 (uint64_t x) |
||
150 | { |
||
151 | return badvpn_reverse64(x); |
||
152 | } |
||
153 | |||
154 | #endif |
||
155 | |||
156 | static uint8_t ntoh8 (uint8_t x) |
||
157 | { |
||
158 | return hton8(x); |
||
159 | } |
||
160 | |||
161 | static uint16_t ntoh16 (uint16_t x) |
||
162 | { |
||
163 | return hton16(x); |
||
164 | } |
||
165 | |||
166 | static uint32_t ntoh32 (uint32_t x) |
||
167 | { |
||
168 | return hton32(x); |
||
169 | } |
||
170 | |||
171 | static uint64_t ntoh64 (uint64_t x) |
||
172 | { |
||
173 | return hton64(x); |
||
174 | } |
||
175 | |||
176 | static uint8_t ltoh8 (uint8_t x) |
||
177 | { |
||
178 | return htol8(x); |
||
179 | } |
||
180 | |||
181 | static uint16_t ltoh16 (uint16_t x) |
||
182 | { |
||
183 | return htol16(x); |
||
184 | } |
||
185 | |||
186 | static uint32_t ltoh32 (uint32_t x) |
||
187 | { |
||
188 | return htol32(x); |
||
189 | } |
||
190 | |||
191 | static uint64_t ltoh64 (uint64_t x) |
||
192 | { |
||
193 | return htol64(x); |
||
194 | } |
||
195 | |||
196 | #endif |