misc/libphysfs/physfs_byteorder.c
author koda
Fri, 22 Feb 2013 05:43:56 +0100
branchphysfslayer
changeset 8524 a65e9bcf0a03
parent 8522 misc/libphysfs/src/physfs_byteorder.c@1853628ae285
permissions -rw-r--r--
split extras and src
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/**
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * PhysicsFS; a portable, flexible file i/o abstraction.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * Documentation is in physfs.h. It's verbose, honest.  :)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
 *  This file written by Ryan C. Gordon.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
#ifndef PHYSFS_Swap16
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
static inline PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
    return ((D<<8)|(D>>8));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
#ifndef PHYSFS_Swap32
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
static inline PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
    return ((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
#ifndef PHYSFS_NO_64BIT_SUPPORT
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
#ifndef PHYSFS_Swap64
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
static inline PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
    PHYSFS_uint32 hi, lo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
    /* Separate into high and low 32-bit values and swap them */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
    lo = (PHYSFS_uint32)(val&0xFFFFFFFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
    val >>= 32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
    hi = (PHYSFS_uint32)(val&0xFFFFFFFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
    val = PHYSFS_Swap32(lo);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
    val <<= 32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
    val |= PHYSFS_Swap32(hi);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
    return val;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
#ifndef PHYSFS_Swap64
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
/* This is mainly to keep compilers from complaining in PHYSFS code.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
   If there is no real 64-bit datatype, then compilers will complain about
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
   the fake 64-bit datatype that PHYSFS provides when it compiles user code.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
*/
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
#define PHYSFS_Swap64(X)    (X)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
#endif /* PHYSFS_NO_64BIT_SUPPORT */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
/* Byteswap item from the specified endianness to the native endianness */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
#if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return PHYSFS_Swap16(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return PHYSFS_Swap16(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return PHYSFS_Swap32(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return PHYSFS_Swap32(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return PHYSFS_Swap64(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return PHYSFS_Swap64(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return PHYSFS_Swap16(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return PHYSFS_Swap16(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return PHYSFS_Swap32(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return PHYSFS_Swap32(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return PHYSFS_Swap64(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return PHYSFS_Swap64(x); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return x; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
static inline int readAll(PHYSFS_File *file, void *val, const size_t len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
    return (PHYSFS_readBytes(file, val, len) == len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
} /* readAll */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
#define PHYSFS_BYTEORDER_READ(datatype, swaptype) \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
    int PHYSFS_read##swaptype(PHYSFS_File *file, PHYSFS_##datatype *val) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
        PHYSFS_##datatype in; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
        BAIL_IF_MACRO(val == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
        BAIL_IF_MACRO(!readAll(file, &in, sizeof (in)), ERRPASS, 0); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
        *val = PHYSFS_swap##swaptype(in); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
        return 1; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
PHYSFS_BYTEORDER_READ(sint16, SLE16)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
PHYSFS_BYTEORDER_READ(uint16, ULE16)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
PHYSFS_BYTEORDER_READ(sint16, SBE16)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
PHYSFS_BYTEORDER_READ(uint16, UBE16)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
PHYSFS_BYTEORDER_READ(sint32, SLE32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
PHYSFS_BYTEORDER_READ(uint32, ULE32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
PHYSFS_BYTEORDER_READ(sint32, SBE32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
PHYSFS_BYTEORDER_READ(uint32, UBE32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
PHYSFS_BYTEORDER_READ(sint64, SLE64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
PHYSFS_BYTEORDER_READ(uint64, ULE64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
PHYSFS_BYTEORDER_READ(sint64, SBE64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
PHYSFS_BYTEORDER_READ(uint64, UBE64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
static inline int writeAll(PHYSFS_File *f, const void *val, const size_t len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
    return (PHYSFS_writeBytes(f, val, len) == len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
} /* writeAll */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
#define PHYSFS_BYTEORDER_WRITE(datatype, swaptype) \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   117
    int PHYSFS_write##swaptype(PHYSFS_File *file, PHYSFS_##datatype val) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118
        const PHYSFS_##datatype out = PHYSFS_swap##swaptype(val); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   119
        BAIL_IF_MACRO(!writeAll(file, &out, sizeof (out)), ERRPASS, 0); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
        return 1; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
PHYSFS_BYTEORDER_WRITE(sint16, SLE16)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
PHYSFS_BYTEORDER_WRITE(uint16, ULE16)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
PHYSFS_BYTEORDER_WRITE(sint16, SBE16)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
PHYSFS_BYTEORDER_WRITE(uint16, UBE16)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
PHYSFS_BYTEORDER_WRITE(sint32, SLE32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
PHYSFS_BYTEORDER_WRITE(uint32, ULE32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   129
PHYSFS_BYTEORDER_WRITE(sint32, SBE32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   130
PHYSFS_BYTEORDER_WRITE(uint32, UBE32)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   131
PHYSFS_BYTEORDER_WRITE(sint64, SLE64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
PHYSFS_BYTEORDER_WRITE(uint64, ULE64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
PHYSFS_BYTEORDER_WRITE(sint64, SBE64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
PHYSFS_BYTEORDER_WRITE(uint64, UBE64)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
/* end of physfs_byteorder.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137