Skip to content

Latest commit

 

History

History
312 lines (251 loc) · 9.31 KB

physfs_byteorder.c

File metadata and controls

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