physfs_byteorder.c
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--
Patches for correctness and cleaner win32 support.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
177
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * PhysicsFS; a portable, flexible file i/o abstraction.
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * Documentation is in physfs.h. It's verbose, honest.  :)
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 * Please see the file LICENSE in the source's root directory.
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *  This file written by Ryan C. Gordon.
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <stdio.h>
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include <stdlib.h>
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include <assert.h>
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#include "physfs.h"
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
/* This byteorder stuff was lifted from SDL. http://www.libsdl.org/ */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#define PHYSFS_LIL_ENDIAN  1234
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#define PHYSFS_BIG_ENDIAN  4321
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#if  defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    (defined(__alpha__) || defined(__alpha)) || \
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
     defined(__arm__) || \
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    (defined(__mips__) && defined(__MIPSEL__)) || \
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
     defined(__SYMBIAN32__) || \
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
     defined(__LITTLE_ENDIAN__)
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
#define PHYSFS_BYTEORDER    PHYSFS_LIL_ENDIAN
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#else
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#define PHYSFS_BYTEORDER    PHYSFS_BIG_ENDIAN
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
/* The macros used to swap values */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
/* Try to use superfast macros on systems that support them */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#ifdef linux
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include <asm/byteorder.h>
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#ifdef __arch__swab16
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#define PHYSFS_Swap16  __arch__swab16
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
#ifdef __arch__swab32
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
#define PHYSFS_Swap32  __arch__swab32
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    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
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    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
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
{
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
	return((D<<8)|(D>>8));
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
}
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    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
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
{
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
}
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
#ifndef PHYSFS_NO_64BIT_SUPPORT
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    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
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
	PHYSFS_uint32 hi, lo;
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
	/* Separate into high and low 32-bit values and swap them */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
	lo = (PHYSFS_uint32)(val&0xFFFFFFFF);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
	val >>= 32;
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
	hi = (PHYSFS_uint32)(val&0xFFFFFFFF);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
	val = PHYSFS_Swap32(lo);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
	val <<= 32;
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
	val |= PHYSFS_Swap32(hi);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
	return(val);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
}
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
#else
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
#ifndef PHYSFS_Swap64
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
/* This is mainly to keep compilers from complaining in PHYSFS code.
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
   If there is no real 64-bit datatype, then compilers will complain about
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
   the fake 64-bit datatype that PHYSFS provides when it compiles user code.
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
*/
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
#define PHYSFS_Swap64(X)	(X)
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
#endif /* PHYSFS_NO_64BIT_SUPPORT */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
/* Byteswap item from the specified endianness to the native endianness */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
#if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
#else
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
/* end of physfs_byteorder.c ... */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118