author | Ryan C. Gordon <icculus@icculus.org> |
Fri, 12 Apr 2002 05:53:12 +0000 | |
changeset 194 | 09c353d30cd4 |
parent 177 | 9aaf01f4b8ec |
child 201 | 7ea2ae5d1a6c |
permissions | -rw-r--r-- |
177 | 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 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 |
#include <assert.h> |
|
14 |
#include "physfs.h" |
|
15 |
||
16 |
/* This byteorder stuff was lifted from SDL. http://www.libsdl.org/ */ |
|
17 |
#define PHYSFS_LIL_ENDIAN 1234 |
|
18 |
#define PHYSFS_BIG_ENDIAN 4321 |
|
19 |
||
20 |
#if defined(__i386__) || defined(__ia64__) || defined(WIN32) || \ |
|
21 |
(defined(__alpha__) || defined(__alpha)) || \ |
|
22 |
defined(__arm__) || \ |
|
23 |
(defined(__mips__) && defined(__MIPSEL__)) || \ |
|
24 |
defined(__SYMBIAN32__) || \ |
|
25 |
defined(__LITTLE_ENDIAN__) |
|
26 |
#define PHYSFS_BYTEORDER PHYSFS_LIL_ENDIAN |
|
27 |
#else |
|
28 |
#define PHYSFS_BYTEORDER PHYSFS_BIG_ENDIAN |
|
29 |
#endif |
|
30 |
||
31 |
||
32 |
/* The macros used to swap values */ |
|
33 |
/* Try to use superfast macros on systems that support them */ |
|
34 |
#ifdef linux |
|
35 |
#include <asm/byteorder.h> |
|
36 |
#ifdef __arch__swab16 |
|
37 |
#define PHYSFS_Swap16 __arch__swab16 |
|
38 |
#endif |
|
39 |
#ifdef __arch__swab32 |
|
40 |
#define PHYSFS_Swap32 __arch__swab32 |
|
41 |
#endif |
|
42 |
#endif /* linux */ |
|
194
09c353d30cd4
Patches for correctness and cleaner win32 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
177
diff
changeset
|
43 |
|
09c353d30cd4
Patches for correctness and cleaner win32 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
177
diff
changeset
|
44 |
#if (defined _MSC_VER) |
09c353d30cd4
Patches for correctness and cleaner win32 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
177
diff
changeset
|
45 |
#define inline __inline |
09c353d30cd4
Patches for correctness and cleaner win32 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
177
diff
changeset
|
46 |
#endif |
09c353d30cd4
Patches for correctness and cleaner win32 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
177
diff
changeset
|
47 |
|
177 | 48 |
#ifndef PHYSFS_Swap16 |
194
09c353d30cd4
Patches for correctness and cleaner win32 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
177
diff
changeset
|
49 |
static inline PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D) |
177 | 50 |
{ |
51 |
return((D<<8)|(D>>8)); |
|
52 |
} |
|
53 |
#endif |
|
54 |
#ifndef PHYSFS_Swap32 |
|
194
09c353d30cd4
Patches for correctness and cleaner win32 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
177
diff
changeset
|
55 |
static inline PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D) |
177 | 56 |
{ |
57 |
return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24)); |
|
58 |
} |
|
59 |
#endif |
|
60 |
#ifndef PHYSFS_NO_64BIT_SUPPORT |
|
61 |
#ifndef PHYSFS_Swap64 |
|
194
09c353d30cd4
Patches for correctness and cleaner win32 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
177
diff
changeset
|
62 |
static inline PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) { |
177 | 63 |
PHYSFS_uint32 hi, lo; |
64 |
||
65 |
/* Separate into high and low 32-bit values and swap them */ |
|
66 |
lo = (PHYSFS_uint32)(val&0xFFFFFFFF); |
|
67 |
val >>= 32; |
|
68 |
hi = (PHYSFS_uint32)(val&0xFFFFFFFF); |
|
69 |
val = PHYSFS_Swap32(lo); |
|
70 |
val <<= 32; |
|
71 |
val |= PHYSFS_Swap32(hi); |
|
72 |
return(val); |
|
73 |
} |
|
74 |
#endif |
|
75 |
#else |
|
76 |
#ifndef PHYSFS_Swap64 |
|
77 |
/* This is mainly to keep compilers from complaining in PHYSFS code. |
|
78 |
If there is no real 64-bit datatype, then compilers will complain about |
|
79 |
the fake 64-bit datatype that PHYSFS provides when it compiles user code. |
|
80 |
*/ |
|
81 |
#define PHYSFS_Swap64(X) (X) |
|
82 |
#endif |
|
83 |
#endif /* PHYSFS_NO_64BIT_SUPPORT */ |
|
84 |
||
85 |
||
86 |
/* Byteswap item from the specified endianness to the native endianness */ |
|
87 |
#if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN |
|
88 |
PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(x); } |
|
89 |
PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(x); } |
|
90 |
PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(x); } |
|
91 |
PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(x); } |
|
92 |
PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(x); } |
|
93 |
PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(x); } |
|
94 |
||
95 |
PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); } |
|
96 |
PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); } |
|
97 |
PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); } |
|
98 |
PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); } |
|
99 |
PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); } |
|
100 |
PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); } |
|
101 |
#else |
|
102 |
PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); } |
|
103 |
PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); } |
|
104 |
PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); } |
|
105 |
PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); } |
|
106 |
PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); } |
|
107 |
PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); } |
|
108 |
||
109 |
PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(x); } |
|
110 |
PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(x); } |
|
111 |
PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(x); } |
|
112 |
PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(x); } |
|
113 |
PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(x); } |
|
114 |
PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(x); } |
|
115 |
#endif |
|
116 |
||
117 |
/* end of physfs_byteorder.c ... */ |
|
118 |