Initial integration, lots of broken stuff
[supertux.git] / src / unison / physfs-1.1.1 / physfs_byteorder.c
1 /**
2  * PhysicsFS; a portable, flexible file i/o abstraction.
3  *
4  * Documentation is in physfs.h. It's verbose, honest.  :)
5  *
6  * Please see the file LICENSE.txt in the source's root directory.
7  *
8  *  This file written by Ryan C. Gordon.
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #define __PHYSICSFS_INTERNAL__
15 #include "physfs_internal.h"
16
17 /* The macros used to swap values */
18 /* Try to use superfast macros on systems that support them */
19 #ifdef linux
20 #include <asm/byteorder.h>
21 #ifdef __arch__swab16
22 #define PHYSFS_Swap16  __arch__swab16
23 #endif
24 #ifdef __arch__swab32
25 #define PHYSFS_Swap32  __arch__swab32
26 #endif
27 #endif /* linux */
28
29 #if (defined macintosh) && !(defined __MWERKS__)
30 #define __inline__
31 #endif
32
33 #if (defined _MSC_VER)
34 #define __inline__ __inline
35 #endif
36
37 #ifndef PHYSFS_Swap16
38 static __inline__ PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
39 {
40     return((D<<8)|(D>>8));
41 }
42 #endif
43 #ifndef PHYSFS_Swap32
44 static __inline__ PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
45 {
46     return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
47 }
48 #endif
49 #ifndef PHYSFS_NO_64BIT_SUPPORT
50 #ifndef PHYSFS_Swap64
51 static __inline__ PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
52     PHYSFS_uint32 hi, lo;
53
54     /* Separate into high and low 32-bit values and swap them */
55     lo = (PHYSFS_uint32)(val&0xFFFFFFFF);
56     val >>= 32;
57     hi = (PHYSFS_uint32)(val&0xFFFFFFFF);
58     val = PHYSFS_Swap32(lo);
59     val <<= 32;
60     val |= PHYSFS_Swap32(hi);
61     return(val);
62 }
63 #endif
64 #else
65 #ifndef PHYSFS_Swap64
66 /* This is mainly to keep compilers from complaining in PHYSFS code.
67    If there is no real 64-bit datatype, then compilers will complain about
68    the fake 64-bit datatype that PHYSFS provides when it compiles user code.
69 */
70 #define PHYSFS_Swap64(X)    (X)
71 #endif
72 #endif /* PHYSFS_NO_64BIT_SUPPORT */
73
74
75 /* Byteswap item from the specified endianness to the native endianness */
76 #if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
77 PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(x); }
78 PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(x); }
79 PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(x); }
80 PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(x); }
81 PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(x); }
82 PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(x); }
83
84 PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
85 PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
86 PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
87 PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
88 PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
89 PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
90 #else
91 PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
92 PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
93 PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
94 PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
95 PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
96 PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
97
98 PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(x); }
99 PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(x); }
100 PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(x); }
101 PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(x); }
102 PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(x); }
103 PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(x); }
104 #endif
105
106
107 int PHYSFS_readSLE16(PHYSFS_File *file, PHYSFS_sint16 *val)
108 {
109     PHYSFS_sint16 in;
110     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
111     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
112     *val = PHYSFS_swapSLE16(in);
113     return(1);
114 } /* PHYSFS_readSLE16 */
115
116
117 int PHYSFS_readULE16(PHYSFS_File *file, PHYSFS_uint16 *val)
118 {
119     PHYSFS_uint16 in;
120     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
121     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
122     *val = PHYSFS_swapULE16(in);
123     return(1);
124 } /* PHYSFS_readULE16 */
125
126
127 int PHYSFS_readSBE16(PHYSFS_File *file, PHYSFS_sint16 *val)
128 {
129     PHYSFS_sint16 in;
130     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
131     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
132     *val = PHYSFS_swapSBE16(in);
133     return(1);
134 } /* PHYSFS_readSBE16 */
135
136
137 int PHYSFS_readUBE16(PHYSFS_File *file, PHYSFS_uint16 *val)
138 {
139     PHYSFS_uint16 in;
140     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
141     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
142     *val = PHYSFS_swapUBE16(in);
143     return(1);
144 } /* PHYSFS_readUBE16 */
145
146
147 int PHYSFS_readSLE32(PHYSFS_File *file, PHYSFS_sint32 *val)
148 {
149     PHYSFS_sint32 in;
150     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
151     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
152     *val = PHYSFS_swapSLE32(in);
153     return(1);
154 } /* PHYSFS_readSLE32 */
155
156
157 int PHYSFS_readULE32(PHYSFS_File *file, PHYSFS_uint32 *val)
158 {
159     PHYSFS_uint32 in;
160     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
161     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
162     *val = PHYSFS_swapULE32(in);
163     return(1);
164 } /* PHYSFS_readULE32 */
165
166
167 int PHYSFS_readSBE32(PHYSFS_File *file, PHYSFS_sint32 *val)
168 {
169     PHYSFS_sint32 in;
170     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
171     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
172     *val = PHYSFS_swapSBE32(in);
173     return(1);
174 } /* PHYSFS_readSBE32 */
175
176
177 int PHYSFS_readUBE32(PHYSFS_File *file, PHYSFS_uint32 *val)
178 {
179     PHYSFS_uint32 in;
180     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
181     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
182     *val = PHYSFS_swapUBE32(in);
183     return(1);
184 } /* PHYSFS_readUBE32 */
185
186
187 int PHYSFS_readSLE64(PHYSFS_File *file, PHYSFS_sint64 *val)
188 {
189     PHYSFS_sint64 in;
190     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
191     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
192     *val = PHYSFS_swapSLE64(in);
193     return(1);
194 } /* PHYSFS_readSLE64 */
195
196
197 int PHYSFS_readULE64(PHYSFS_File *file, PHYSFS_uint64 *val)
198 {
199     PHYSFS_uint64 in;
200     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
201     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
202     *val = PHYSFS_swapULE64(in);
203     return(1);
204 } /* PHYSFS_readULE64 */
205
206
207 int PHYSFS_readSBE64(PHYSFS_File *file, PHYSFS_sint64 *val)
208 {
209     PHYSFS_sint64 in;
210     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
211     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
212     *val = PHYSFS_swapSBE64(in);
213     return(1);
214 } /* PHYSFS_readSBE64 */
215
216
217 int PHYSFS_readUBE64(PHYSFS_File *file, PHYSFS_uint64 *val)
218 {
219     PHYSFS_uint64 in;
220     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
221     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
222     *val = PHYSFS_swapUBE64(in);
223     return(1);
224 } /* PHYSFS_readUBE64 */
225
226
227
228 int PHYSFS_writeSLE16(PHYSFS_File *file, PHYSFS_sint16 val)
229 {
230     PHYSFS_sint16 out = PHYSFS_swapSLE16(val);
231     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
232     return(1);
233 } /* PHYSFS_writeSLE16 */
234
235
236 int PHYSFS_writeULE16(PHYSFS_File *file, PHYSFS_uint16 val)
237 {
238     PHYSFS_uint16 out = PHYSFS_swapULE16(val);
239     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
240     return(1);
241 } /* PHYSFS_writeULE16 */
242
243
244 int PHYSFS_writeSBE16(PHYSFS_File *file, PHYSFS_sint16 val)
245 {
246     PHYSFS_sint16 out = PHYSFS_swapSBE16(val);
247     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
248     return(1);
249 } /* PHYSFS_writeSBE16 */
250
251
252 int PHYSFS_writeUBE16(PHYSFS_File *file, PHYSFS_uint16 val)
253 {
254     PHYSFS_uint16 out = PHYSFS_swapUBE16(val);
255     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
256     return(1);
257 } /* PHYSFS_writeUBE16 */
258
259
260 int PHYSFS_writeSLE32(PHYSFS_File *file, PHYSFS_sint32 val)
261 {
262     PHYSFS_sint32 out = PHYSFS_swapSLE32(val);
263     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
264     return(1);
265 } /* PHYSFS_writeSLE32 */
266
267
268 int PHYSFS_writeULE32(PHYSFS_File *file, PHYSFS_uint32 val)
269 {
270     PHYSFS_uint32 out = PHYSFS_swapULE32(val);
271     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
272     return(1);
273 } /* PHYSFS_writeULE32 */
274
275
276 int PHYSFS_writeSBE32(PHYSFS_File *file, PHYSFS_sint32 val)
277 {
278     PHYSFS_sint32 out = PHYSFS_swapSBE32(val);
279     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
280     return(1);
281 } /* PHYSFS_writeSBE32 */
282
283
284 int PHYSFS_writeUBE32(PHYSFS_File *file, PHYSFS_uint32 val)
285 {
286     PHYSFS_uint32 out = PHYSFS_swapUBE32(val);
287     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
288     return(1);
289 } /* PHYSFS_writeUBE32 */
290
291
292 int PHYSFS_writeSLE64(PHYSFS_File *file, PHYSFS_sint64 val)
293 {
294     PHYSFS_sint64 out = PHYSFS_swapSLE64(val);
295     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
296     return(1);
297 } /* PHYSFS_writeSLE64 */
298
299
300 int PHYSFS_writeULE64(PHYSFS_File *file, PHYSFS_uint64 val)
301 {
302     PHYSFS_uint64 out = PHYSFS_swapULE64(val);
303     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
304     return(1);
305 } /* PHYSFS_writeULE64 */
306
307
308 int PHYSFS_writeSBE64(PHYSFS_File *file, PHYSFS_sint64 val)
309 {
310     PHYSFS_sint64 out = PHYSFS_swapSBE64(val);
311     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
312     return(1);
313 } /* PHYSFS_writeSBE64 */
314
315
316 int PHYSFS_writeUBE64(PHYSFS_File *file, PHYSFS_uint64 val)
317 {
318     PHYSFS_uint64 out = PHYSFS_swapUBE64(val);
319     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
320     return(1);
321 } /* PHYSFS_writeUBE64 */
322
323 /* end of physfs_byteorder.c ... */
324