physfs_byteorder.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 05 Apr 2002 18:10:02 +0000
changeset 177 9aaf01f4b8ec
child 194 09c353d30cd4
permissions -rw-r--r--
Initial add.
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 */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#ifndef PHYSFS_Swap16
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
static __inline__ PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
{
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
	return((D<<8)|(D>>8));
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
}
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
#ifndef PHYSFS_Swap32
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
static __inline__ PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
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
	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
}
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
#ifndef PHYSFS_NO_64BIT_SUPPORT
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
#ifndef PHYSFS_Swap64
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
static __inline__ PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
	PHYSFS_uint32 hi, lo;
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
	/* Separate into high and low 32-bit values and swap them */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
	lo = (PHYSFS_uint32)(val&0xFFFFFFFF);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
	val >>= 32;
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
	hi = (PHYSFS_uint32)(val&0xFFFFFFFF);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
	val = PHYSFS_Swap32(lo);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
	val <<= 32;
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
	val |= PHYSFS_Swap32(hi);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
	return(val);
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
}
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
#else
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
#ifndef PHYSFS_Swap64
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
/* This is mainly to keep compilers from complaining in PHYSFS code.
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
   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
    75
   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
    76
*/
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
#define PHYSFS_Swap64(X)	(X)
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
#endif /* PHYSFS_NO_64BIT_SUPPORT */
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
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
/* Byteswap item from the specified endianness to the native endianness */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
#if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
#else
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(x); }
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
#endif
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
/* end of physfs_byteorder.c ... */
9aaf01f4b8ec Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114