Skip to content

Latest commit

 

History

History
140 lines (123 loc) · 4.94 KB

physfs_byteorder.c

File metadata and controls

140 lines (123 loc) · 4.94 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
17
#ifndef PHYSFS_Swap16
Aug 21, 2010
Aug 21, 2010
18
static inline PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
Apr 5, 2002
Apr 5, 2002
19
{
Jan 28, 2010
Jan 28, 2010
20
return ((D<<8)|(D>>8));
Apr 5, 2002
Apr 5, 2002
21
22
23
}
#endif
#ifndef PHYSFS_Swap32
Aug 21, 2010
Aug 21, 2010
24
static inline PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
Apr 5, 2002
Apr 5, 2002
25
{
Jan 28, 2010
Jan 28, 2010
26
return ((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
Apr 5, 2002
Apr 5, 2002
27
28
29
30
}
#endif
#ifndef PHYSFS_NO_64BIT_SUPPORT
#ifndef PHYSFS_Swap64
Aug 21, 2010
Aug 21, 2010
31
static inline PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
Jul 20, 2003
Jul 20, 2003
32
PHYSFS_uint32 hi, lo;
Apr 5, 2002
Apr 5, 2002
33
Jul 20, 2003
Jul 20, 2003
34
35
36
37
38
39
40
/* 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
41
return val;
Apr 5, 2002
Apr 5, 2002
42
43
44
45
46
47
48
49
}
#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
50
#define PHYSFS_Swap64(X) (X)
Apr 5, 2002
Apr 5, 2002
51
52
53
54
55
56
#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
57
58
59
60
61
62
63
64
65
66
67
68
69
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
70
#else
Jan 28, 2010
Jan 28, 2010
71
72
73
74
75
76
77
78
79
80
81
82
83
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
84
85
#endif
Aug 21, 2010
Aug 21, 2010
86
87
88
89
90
91
92
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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; \
BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0); \
BAIL_IF_MACRO(!readAll(file, &in, sizeof (in)), NULL, 0); \
*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); \
BAIL_IF_MACRO(!writeAll(file, &out, sizeof (out)), NULL, 0); \
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
138
Apr 5, 2002
Apr 5, 2002
139
/* end of physfs_byteorder.c ... */