KVIrc  4.9.2
DeveloperAPIs
KviByteOrder.h
Go to the documentation of this file.
1 #ifndef _KVI_BYTEORDER_H_
2 #define _KVI_BYTEORDER_H_
3 
4 //=============================================================================
5 //
6 // File : KviByteOrder.h
7 // Creation date : Mon Dec 25 2006 19:56:16 CEST by Szymon Stefanek
8 // Based on file : kvi_bswap.h
9 // Creation date : Fri Mar 19 1999 03:15:21 CEST by Szymon Stefanek
10 //
11 // This file is part of the KVIrc IRC client distribution
12 // Copyright (C) 1999-2010 Szymon Stefanek (pragma at kvirc dot net)
13 //
14 // This program is FREE software. You can redistribute it and/or
15 // modify it under the terms of the GNU General Public License
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
18 //
19 // This program is distributed in the HOPE that it will be USEFUL,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 // See the GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program. If not, write to the Free Software Foundation,
26 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 //
28 //=============================================================================
29 
30 #include "kvi_settings.h"
31 #include "kvi_inttypes.h"
32 
33 namespace KviByteOrder
34 {
42  {
43  // abcdefgh to hgfedcba
44  return ((i << 56) | /* h to a */
45  ((i & 0xff00) << 40) | /* g to b */
46  ((i & 0xff0000) << 24) | /* f to c */
47  ((i & 0xff000000) << 8) | /* e to d */
48  ((i >> 8) & 0xff000000) | /* d to e */
49  ((i >> 24) & 0xff0000) | /* c to f */
50  ((i >> 40) & 0xff00) | /* b to g */
51  (i >> 56)); /* a to h */
52  }
53 
60  inline kvi_u32_t swap32(kvi_u32_t i)
61  {
62  // abcd to dcba
63  return ((i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24));
64  }
65 
72  inline kvi_u16_t swap16(kvi_u16_t i)
73  {
74  // ab to ba
75  return ((i << 8) | (i >> 8));
76  }
77 
78 //
79 // Byte Orders Reminder
80 // Number 0xaabbccdd
81 // Little Endian Stores 0xdd 0xcc 0xbb 0xaa
82 // Big Endian Stores 0xaa 0xbb 0xcc 0xdd
83 // Perverse Middle Endian 0xbb 0xaa 0xdd 0xcc or another braindamaged combination (unsupported)
84 // Network Byte Order is Big Endian
85 // Intel Stuff uses Little Endian
86 // PPC is Big Endian
87 // Universal binaries on MacOSX use both Big and Little Endian
88 //
89 
90 #ifdef BIG_ENDIAN_MACHINE_BYTE_ORDER
91 
92  inline kvi_u16_t localCpuToLittleEndian16(kvi_u16_t u)
93  {
94  return swap16(u);
95  }
96 
97  inline kvi_u32_t localCpuToLittleEndian32(kvi_u32_t u)
98  {
99  return swap32(u);
100  }
101 
103  {
104  return swap64(u);
105  }
106 
107  inline kvi_u16_t littleEndianToLocalCpu16(kvi_u16_t u)
108  {
109  return swap16(u);
110  }
111 
112  inline kvi_u32_t littleEndianToLocalCpu32(kvi_u32_t u)
113  {
114  return swap32(u);
115  }
116 
118  {
119  return swap64(u);
120  }
121 
122  inline kvi_u16_t localCpuToBigEndian16(kvi_u16_t u)
123  {
124  return u;
125  }
126 
127  inline kvi_u32_t localCpuToBigEndian32(kvi_u32_t u)
128  {
129  return u;
130  }
131 
133  {
134  return u;
135  }
136 
137  inline kvi_u16_t bigEndianToLocalCpu16(kvi_u16_t u)
138  {
139  return u;
140  }
141 
142  inline kvi_u32_t bigEndianToLocalCpu32(kvi_u32_t u)
143  {
144  return u;
145  }
146 
148  {
149  return u;
150  }
151 
152  inline kvi_u16_t localCpuToNetworkByteOrder16(kvi_u16_t u)
153  {
154  return u;
155  }
156 
157  inline kvi_u32_t localCpuToNetworkByteOrder32(kvi_u32_t u)
158  {
159  return u;
160  }
161 
163  {
164  return u;
165  }
166 
167  inline kvi_u16_t networkByteOrderToLocalCpu16(kvi_u16_t u)
168  {
169  return u;
170  }
171 
172  inline kvi_u32_t networkByteOrderToLocalCpu32(kvi_u32_t u)
173  {
174  return u;
175  }
176 
178  {
179  return u;
180  }
181 
182 #else
183 
184 // We ASSUME that the local cpu is little endian.. if it isn't.. well :)
185 #define LOCAL_CPU_LITTLE_ENDIAN 1
186 
187  inline kvi_u16_t localCpuToLittleEndian16(kvi_u16_t u)
188  {
189  return u;
190  }
191 
192  inline kvi_u32_t localCpuToLittleEndian32(kvi_u32_t u)
193  {
194  return u;
195  }
196 
198  {
199  return u;
200  }
201 
202  inline kvi_u16_t littleEndianToLocalCpu16(kvi_u16_t u)
203  {
204  return u;
205  }
206 
207  inline kvi_u32_t littleEndianToLocalCpu32(kvi_u32_t u)
208  {
209  return u;
210  }
211 
213  {
214  return u;
215  }
216 
217  inline kvi_u16_t localCpuToBigEndian16(kvi_u16_t u)
218  {
219  return swap16(u);
220  }
221 
222  inline kvi_u32_t localCpuToBigEndian32(kvi_u32_t u)
223  {
224  return swap32(u);
225  }
226 
228  {
229  return swap64(u);
230  }
231 
232  inline kvi_u16_t bigEndianToLocalCpu16(kvi_u16_t u)
233  {
234  return swap16(u);
235  }
236 
237  inline kvi_u32_t bigEndianToLocalCpu32(kvi_u32_t u)
238  {
239  return swap32(u);
240  }
241 
243  {
244  return swap64(u);
245  }
246 
247  inline kvi_u16_t localCpuToNetworkByteOrder16(kvi_u16_t u)
248  {
249  return swap16(u);
250  }
251 
252  inline kvi_u32_t localCpuToNetworkByteOrder32(kvi_u32_t u)
253  {
254  return swap32(u);
255  }
256 
258  {
259  return swap64(u);
260  }
261 
262  inline kvi_u16_t networkByteOrderToLocalCpu16(kvi_u16_t u)
263  {
264  return swap16(u);
265  }
266 
267  inline kvi_u32_t networkByteOrderToLocalCpu32(kvi_u32_t u)
268  {
269  return swap32(u);
270  }
271 
273  {
274  return swap64(u);
275  }
276 
277 #endif
278 
279 } // namespace KviByteOrder
280 
281 #endif // !_KVI_BYTEORDER_H_
kvi_u32_t localCpuToNetworkByteOrder32(kvi_u32_t u)
Definition: KviByteOrder.h:252
kvi_u32_t localCpuToLittleEndian32(kvi_u32_t u)
Definition: KviByteOrder.h:192
kvi_u16_t littleEndianToLocalCpu16(kvi_u16_t u)
Definition: KviByteOrder.h:202
kvi_u64_t littleEndianToLocalCpu64(kvi_u64_t u)
Definition: KviByteOrder.h:212
kvi_u16_t localCpuToLittleEndian16(kvi_u16_t u)
Definition: KviByteOrder.h:187
kvi_u32_t bigEndianToLocalCpu32(kvi_u32_t u)
Definition: KviByteOrder.h:237
kvi_u16_t bigEndianToLocalCpu16(kvi_u16_t u)
Definition: KviByteOrder.h:232
kvi_u64_t networkByteOrderToLocalCpu64(kvi_u64_t u)
Definition: KviByteOrder.h:272
#define i
Definition: detector.cpp:73
kvi_u64_t swap64(kvi_u64_t i)
Swaps the endianness of a kvi_u64_t.
Definition: KviByteOrder.h:41
kvi_u32_t swap32(kvi_u32_t i)
Swaps the endianness of a kvi_u32_t.
Definition: KviByteOrder.h:60
kvi_u16_t swap16(kvi_u16_t i)
Swaps the endianness of a kvi_u16_t.
Definition: KviByteOrder.h:72
unsigned long long int kvi_u64_t
Definition: kvi_inttypes.h:66
kvi_u32_t littleEndianToLocalCpu32(kvi_u32_t u)
Definition: KviByteOrder.h:207
kvi_u32_t localCpuToBigEndian32(kvi_u32_t u)
Definition: KviByteOrder.h:222
#define u
Definition: detector.cpp:85
kvi_u16_t networkByteOrderToLocalCpu16(kvi_u16_t u)
Definition: KviByteOrder.h:262
kvi_u16_t localCpuToBigEndian16(kvi_u16_t u)
Definition: KviByteOrder.h:217
kvi_u64_t bigEndianToLocalCpu64(kvi_u64_t u)
Definition: KviByteOrder.h:242
kvi_u64_t localCpuToNetworkByteOrder64(kvi_u64_t u)
Definition: KviByteOrder.h:257
kvi_u64_t localCpuToLittleEndian64(kvi_u64_t u)
Definition: KviByteOrder.h:197
This file contains compile time settings.
kvi_u16_t localCpuToNetworkByteOrder16(kvi_u16_t u)
Definition: KviByteOrder.h:247
kvi_u64_t localCpuToBigEndian64(kvi_u64_t u)
Definition: KviByteOrder.h:227
kvi_u32_t networkByteOrderToLocalCpu32(kvi_u32_t u)
Definition: KviByteOrder.h:267