Skip to content

Latest commit

 

History

History
137 lines (121 loc) · 4.92 KB

physfs_byteorder.c

File metadata and controls

137 lines (121 loc) · 4.92 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
*
* This file written by Ryan C. Gordon.
*/
Jul 10, 2002
Jul 10, 2002
11
12
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Apr 5, 2002
Apr 5, 2002
13
14
#ifndef PHYSFS_Swap16
Aug 21, 2010
Aug 21, 2010
15
static inline PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
Apr 5, 2002
Apr 5, 2002
16
{
Jan 28, 2010
Jan 28, 2010
17
return ((D<<8)|(D>>8));
Apr 5, 2002
Apr 5, 2002
18
19
20
}
#endif
#ifndef PHYSFS_Swap32
Aug 21, 2010
Aug 21, 2010
21
static inline PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
Apr 5, 2002
Apr 5, 2002
22
{
Jan 28, 2010
Jan 28, 2010
23
return ((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
Apr 5, 2002
Apr 5, 2002
24
25
26
27
}
#endif
#ifndef PHYSFS_NO_64BIT_SUPPORT
#ifndef PHYSFS_Swap64
Aug 21, 2010
Aug 21, 2010
28
static inline PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
Jul 20, 2003
Jul 20, 2003
29
PHYSFS_uint32 hi, lo;
Apr 5, 2002
Apr 5, 2002
30
Jul 20, 2003
Jul 20, 2003
31
32
33
34
35
36
37
/* 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
38
return val;
Apr 5, 2002
Apr 5, 2002
39
40
41
42
43
44
45
46
}
#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
47
#define PHYSFS_Swap64(X) (X)
Apr 5, 2002
Apr 5, 2002
48
49
50
51
52
53
#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
54
55
56
57
58
59
60
61
62
63
64
65
66
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
67
#else
Jan 28, 2010
Jan 28, 2010
68
69
70
71
72
73
74
75
76
77
78
79
80
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
81
82
#endif
Aug 21, 2010
Aug 21, 2010
83
84
85
86
87
88
89
90
static inline int readAll(PHYSFS_File *file, void *val, const size_t len)
{
return (PHYSFS_readBytes(file, val, len) == len);
} /* readAll */
#define PHYSFS_BYTEORDER_READ(datatype, swaptype) \
int PHYSFS_read##swaptype(PHYSFS_File *file, PHYSFS_##datatype *val) { \
PHYSFS_##datatype in; \
Mar 20, 2012
Mar 20, 2012
91
92
BAIL_IF_MACRO(val == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0); \
BAIL_IF_MACRO(!readAll(file, &in, sizeof (in)), ERRPASS, 0); \
Aug 21, 2010
Aug 21, 2010
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
*val = PHYSFS_swap##swaptype(in); \
return 1; \
}
PHYSFS_BYTEORDER_READ(sint16, SLE16)
PHYSFS_BYTEORDER_READ(uint16, ULE16)
PHYSFS_BYTEORDER_READ(sint16, SBE16)
PHYSFS_BYTEORDER_READ(uint16, UBE16)
PHYSFS_BYTEORDER_READ(sint32, SLE32)
PHYSFS_BYTEORDER_READ(uint32, ULE32)
PHYSFS_BYTEORDER_READ(sint32, SBE32)
PHYSFS_BYTEORDER_READ(uint32, UBE32)
PHYSFS_BYTEORDER_READ(sint64, SLE64)
PHYSFS_BYTEORDER_READ(uint64, ULE64)
PHYSFS_BYTEORDER_READ(sint64, SBE64)
PHYSFS_BYTEORDER_READ(uint64, UBE64)
static inline int writeAll(PHYSFS_File *f, const void *val, const size_t len)
{
return (PHYSFS_writeBytes(f, val, len) == len);
} /* writeAll */
#define PHYSFS_BYTEORDER_WRITE(datatype, swaptype) \
int PHYSFS_write##swaptype(PHYSFS_File *file, PHYSFS_##datatype val) { \
const PHYSFS_##datatype out = PHYSFS_swap##swaptype(val); \
Mar 20, 2012
Mar 20, 2012
119
BAIL_IF_MACRO(!writeAll(file, &out, sizeof (out)), ERRPASS, 0); \
Aug 21, 2010
Aug 21, 2010
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
return 1; \
}
PHYSFS_BYTEORDER_WRITE(sint16, SLE16)
PHYSFS_BYTEORDER_WRITE(uint16, ULE16)
PHYSFS_BYTEORDER_WRITE(sint16, SBE16)
PHYSFS_BYTEORDER_WRITE(uint16, UBE16)
PHYSFS_BYTEORDER_WRITE(sint32, SLE32)
PHYSFS_BYTEORDER_WRITE(uint32, ULE32)
PHYSFS_BYTEORDER_WRITE(sint32, SBE32)
PHYSFS_BYTEORDER_WRITE(uint32, UBE32)
PHYSFS_BYTEORDER_WRITE(sint64, SLE64)
PHYSFS_BYTEORDER_WRITE(uint64, ULE64)
PHYSFS_BYTEORDER_WRITE(sint64, SBE64)
PHYSFS_BYTEORDER_WRITE(uint64, UBE64)
Jul 10, 2002
Jul 10, 2002
135
Apr 5, 2002
Apr 5, 2002
136
/* end of physfs_byteorder.c ... */