physfs_byteorder.c 3.9 KB

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