physfs_byteorder.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.txt in the source's root directory.
  7. *
  8. * This file written by Ryan C. Gordon.
  9. */
  10. #define __PHYSICSFS_INTERNAL__
  11. #include "physfs_internal.h"
  12. #ifndef PHYSFS_Swap16
  13. static inline PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
  14. {
  15. return ((D<<8)|(D>>8));
  16. }
  17. #endif
  18. #ifndef PHYSFS_Swap32
  19. static inline PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
  20. {
  21. return ((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
  22. }
  23. #endif
  24. #ifndef PHYSFS_NO_64BIT_SUPPORT
  25. #ifndef PHYSFS_Swap64
  26. static inline PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
  27. PHYSFS_uint32 hi, lo;
  28. /* Separate into high and low 32-bit values and swap them */
  29. lo = (PHYSFS_uint32)(val&0xFFFFFFFF);
  30. val >>= 32;
  31. hi = (PHYSFS_uint32)(val&0xFFFFFFFF);
  32. val = PHYSFS_Swap32(lo);
  33. val <<= 32;
  34. val |= PHYSFS_Swap32(hi);
  35. return val;
  36. }
  37. #endif
  38. #else
  39. #ifndef PHYSFS_Swap64
  40. /* This is mainly to keep compilers from complaining in PHYSFS code.
  41. If there is no real 64-bit datatype, then compilers will complain about
  42. the fake 64-bit datatype that PHYSFS provides when it compiles user code.
  43. */
  44. #define PHYSFS_Swap64(X) (X)
  45. #endif
  46. #endif /* PHYSFS_NO_64BIT_SUPPORT */
  47. /* Byteswap item from the specified endianness to the native endianness */
  48. #if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
  49. PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return x; }
  50. PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return x; }
  51. PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return x; }
  52. PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return x; }
  53. PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return x; }
  54. PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return x; }
  55. PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return PHYSFS_Swap16(x); }
  56. PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return PHYSFS_Swap16(x); }
  57. PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return PHYSFS_Swap32(x); }
  58. PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return PHYSFS_Swap32(x); }
  59. PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return PHYSFS_Swap64(x); }
  60. PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return PHYSFS_Swap64(x); }
  61. #else
  62. PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return PHYSFS_Swap16(x); }
  63. PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return PHYSFS_Swap16(x); }
  64. PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return PHYSFS_Swap32(x); }
  65. PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return PHYSFS_Swap32(x); }
  66. PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return PHYSFS_Swap64(x); }
  67. PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return PHYSFS_Swap64(x); }
  68. PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return x; }
  69. PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return x; }
  70. PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return x; }
  71. PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return x; }
  72. PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return x; }
  73. PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return x; }
  74. #endif
  75. static inline int readAll(PHYSFS_File *file, void *val, const size_t len)
  76. {
  77. PHYSFS_sint64 amount_read = PHYSFS_readBytes(file, val, len);
  78. if (amount_read < 0)
  79. {
  80. return 0;
  81. }
  82. return ((PHYSFS_uint64)amount_read == len);
  83. } /* readAll */
  84. #define PHYSFS_BYTEORDER_READ(datatype, swaptype) \
  85. int PHYSFS_read##swaptype(PHYSFS_File *file, PHYSFS_##datatype *val) { \
  86. PHYSFS_##datatype in; \
  87. BAIL_IF(val == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0); \
  88. BAIL_IF_ERRPASS(!readAll(file, &in, sizeof (in)), 0); \
  89. *val = PHYSFS_swap##swaptype(in); \
  90. return 1; \
  91. }
  92. PHYSFS_BYTEORDER_READ(sint16, SLE16)
  93. PHYSFS_BYTEORDER_READ(uint16, ULE16)
  94. PHYSFS_BYTEORDER_READ(sint16, SBE16)
  95. PHYSFS_BYTEORDER_READ(uint16, UBE16)
  96. PHYSFS_BYTEORDER_READ(sint32, SLE32)
  97. PHYSFS_BYTEORDER_READ(uint32, ULE32)
  98. PHYSFS_BYTEORDER_READ(sint32, SBE32)
  99. PHYSFS_BYTEORDER_READ(uint32, UBE32)
  100. PHYSFS_BYTEORDER_READ(sint64, SLE64)
  101. PHYSFS_BYTEORDER_READ(uint64, ULE64)
  102. PHYSFS_BYTEORDER_READ(sint64, SBE64)
  103. PHYSFS_BYTEORDER_READ(uint64, UBE64)
  104. static inline int writeAll(PHYSFS_File *file, const void *val, const size_t len)
  105. {
  106. PHYSFS_sint64 amount_written = PHYSFS_writeBytes(file, val, len);
  107. if (amount_written < 0)
  108. {
  109. return 0;
  110. }
  111. return ((PHYSFS_uint64)amount_written == len);
  112. } /* writeAll */
  113. #define PHYSFS_BYTEORDER_WRITE(datatype, swaptype) \
  114. int PHYSFS_write##swaptype(PHYSFS_File *file, PHYSFS_##datatype val) { \
  115. const PHYSFS_##datatype out = PHYSFS_swap##swaptype(val); \
  116. BAIL_IF_ERRPASS(!writeAll(file, &out, sizeof (out)), 0); \
  117. return 1; \
  118. }
  119. PHYSFS_BYTEORDER_WRITE(sint16, SLE16)
  120. PHYSFS_BYTEORDER_WRITE(uint16, ULE16)
  121. PHYSFS_BYTEORDER_WRITE(sint16, SBE16)
  122. PHYSFS_BYTEORDER_WRITE(uint16, UBE16)
  123. PHYSFS_BYTEORDER_WRITE(sint32, SLE32)
  124. PHYSFS_BYTEORDER_WRITE(uint32, ULE32)
  125. PHYSFS_BYTEORDER_WRITE(sint32, SBE32)
  126. PHYSFS_BYTEORDER_WRITE(uint32, UBE32)
  127. PHYSFS_BYTEORDER_WRITE(sint64, SLE64)
  128. PHYSFS_BYTEORDER_WRITE(uint64, ULE64)
  129. PHYSFS_BYTEORDER_WRITE(sint64, SBE64)
  130. PHYSFS_BYTEORDER_WRITE(uint64, UBE64)
  131. /* end of physfs_byteorder.c ... */