physfs_byteorder.c 4.0 KB

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