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