2 * PhysicsFS; a portable, flexible file i/o abstraction.
4 * Documentation is in physfs.h. It's verbose, honest. :)
6 * Please see the file LICENSE.txt in the source's root directory.
8 * This file written by Ryan C. Gordon.
14 #define __PHYSICSFS_INTERNAL__
15 #include "physfs_internal.h"
17 #if (defined macintosh) && !(defined __MWERKS__)
21 #if (defined _MSC_VER)
22 #define __inline__ __inline
26 static __inline__ PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
28 return((D<<8)|(D>>8));
32 static __inline__ PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
34 return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
37 #ifndef PHYSFS_NO_64BIT_SUPPORT
39 static __inline__ PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
42 /* Separate into high and low 32-bit values and swap them */
43 lo = (PHYSFS_uint32)(val&0xFFFFFFFF);
45 hi = (PHYSFS_uint32)(val&0xFFFFFFFF);
46 val = PHYSFS_Swap32(lo);
48 val |= PHYSFS_Swap32(hi);
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.
58 #define PHYSFS_Swap64(X) (X)
60 #endif /* PHYSFS_NO_64BIT_SUPPORT */
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); }
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)); }
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)); }
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); }
95 int PHYSFS_readSLE16(PHYSFS_File *file, PHYSFS_sint16 *val)
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);
102 } /* PHYSFS_readSLE16 */
105 int PHYSFS_readULE16(PHYSFS_File *file, PHYSFS_uint16 *val)
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);
112 } /* PHYSFS_readULE16 */
115 int PHYSFS_readSBE16(PHYSFS_File *file, PHYSFS_sint16 *val)
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);
122 } /* PHYSFS_readSBE16 */
125 int PHYSFS_readUBE16(PHYSFS_File *file, PHYSFS_uint16 *val)
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);
132 } /* PHYSFS_readUBE16 */
135 int PHYSFS_readSLE32(PHYSFS_File *file, PHYSFS_sint32 *val)
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);
142 } /* PHYSFS_readSLE32 */
145 int PHYSFS_readULE32(PHYSFS_File *file, PHYSFS_uint32 *val)
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);
152 } /* PHYSFS_readULE32 */
155 int PHYSFS_readSBE32(PHYSFS_File *file, PHYSFS_sint32 *val)
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);
162 } /* PHYSFS_readSBE32 */
165 int PHYSFS_readUBE32(PHYSFS_File *file, PHYSFS_uint32 *val)
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);
172 } /* PHYSFS_readUBE32 */
175 int PHYSFS_readSLE64(PHYSFS_File *file, PHYSFS_sint64 *val)
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);
182 } /* PHYSFS_readSLE64 */
185 int PHYSFS_readULE64(PHYSFS_File *file, PHYSFS_uint64 *val)
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);
192 } /* PHYSFS_readULE64 */
195 int PHYSFS_readSBE64(PHYSFS_File *file, PHYSFS_sint64 *val)
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);
202 } /* PHYSFS_readSBE64 */
205 int PHYSFS_readUBE64(PHYSFS_File *file, PHYSFS_uint64 *val)
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);
212 } /* PHYSFS_readUBE64 */
216 int PHYSFS_writeSLE16(PHYSFS_File *file, PHYSFS_sint16 val)
218 PHYSFS_sint16 out = PHYSFS_swapSLE16(val);
219 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
221 } /* PHYSFS_writeSLE16 */
224 int PHYSFS_writeULE16(PHYSFS_File *file, PHYSFS_uint16 val)
226 PHYSFS_uint16 out = PHYSFS_swapULE16(val);
227 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
229 } /* PHYSFS_writeULE16 */
232 int PHYSFS_writeSBE16(PHYSFS_File *file, PHYSFS_sint16 val)
234 PHYSFS_sint16 out = PHYSFS_swapSBE16(val);
235 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
237 } /* PHYSFS_writeSBE16 */
240 int PHYSFS_writeUBE16(PHYSFS_File *file, PHYSFS_uint16 val)
242 PHYSFS_uint16 out = PHYSFS_swapUBE16(val);
243 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
245 } /* PHYSFS_writeUBE16 */
248 int PHYSFS_writeSLE32(PHYSFS_File *file, PHYSFS_sint32 val)
250 PHYSFS_sint32 out = PHYSFS_swapSLE32(val);
251 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
253 } /* PHYSFS_writeSLE32 */
256 int PHYSFS_writeULE32(PHYSFS_File *file, PHYSFS_uint32 val)
258 PHYSFS_uint32 out = PHYSFS_swapULE32(val);
259 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
261 } /* PHYSFS_writeULE32 */
264 int PHYSFS_writeSBE32(PHYSFS_File *file, PHYSFS_sint32 val)
266 PHYSFS_sint32 out = PHYSFS_swapSBE32(val);
267 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
269 } /* PHYSFS_writeSBE32 */
272 int PHYSFS_writeUBE32(PHYSFS_File *file, PHYSFS_uint32 val)
274 PHYSFS_uint32 out = PHYSFS_swapUBE32(val);
275 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
277 } /* PHYSFS_writeUBE32 */
280 int PHYSFS_writeSLE64(PHYSFS_File *file, PHYSFS_sint64 val)
282 PHYSFS_sint64 out = PHYSFS_swapSLE64(val);
283 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
285 } /* PHYSFS_writeSLE64 */
288 int PHYSFS_writeULE64(PHYSFS_File *file, PHYSFS_uint64 val)
290 PHYSFS_uint64 out = PHYSFS_swapULE64(val);
291 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
293 } /* PHYSFS_writeULE64 */
296 int PHYSFS_writeSBE64(PHYSFS_File *file, PHYSFS_sint64 val)
298 PHYSFS_sint64 out = PHYSFS_swapSBE64(val);
299 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
301 } /* PHYSFS_writeSBE64 */
304 int PHYSFS_writeUBE64(PHYSFS_File *file, PHYSFS_uint64 val)
306 PHYSFS_uint64 out = PHYSFS_swapUBE64(val);
307 BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
309 } /* PHYSFS_writeUBE64 */
311 /* end of physfs_byteorder.c ... */