🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.9 KiB

  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. return (PHYSFS_readBytes(file, val, len) == len);
  78. } /* readAll */
  79. #define PHYSFS_BYTEORDER_READ(datatype, swaptype) \
  80. int PHYSFS_read##swaptype(PHYSFS_File *file, PHYSFS_##datatype *val) { \
  81. PHYSFS_##datatype in; \
  82. BAIL_IF(val == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0); \
  83. BAIL_IF_ERRPASS(!readAll(file, &in, sizeof (in)), 0); \
  84. *val = PHYSFS_swap##swaptype(in); \
  85. return 1; \
  86. }
  87. PHYSFS_BYTEORDER_READ(sint16, SLE16)
  88. PHYSFS_BYTEORDER_READ(uint16, ULE16)
  89. PHYSFS_BYTEORDER_READ(sint16, SBE16)
  90. PHYSFS_BYTEORDER_READ(uint16, UBE16)
  91. PHYSFS_BYTEORDER_READ(sint32, SLE32)
  92. PHYSFS_BYTEORDER_READ(uint32, ULE32)
  93. PHYSFS_BYTEORDER_READ(sint32, SBE32)
  94. PHYSFS_BYTEORDER_READ(uint32, UBE32)
  95. PHYSFS_BYTEORDER_READ(sint64, SLE64)
  96. PHYSFS_BYTEORDER_READ(uint64, ULE64)
  97. PHYSFS_BYTEORDER_READ(sint64, SBE64)
  98. PHYSFS_BYTEORDER_READ(uint64, UBE64)
  99. static inline int writeAll(PHYSFS_File *f, const void *val, const size_t len)
  100. {
  101. return (PHYSFS_writeBytes(f, val, len) == len);
  102. } /* writeAll */
  103. #define PHYSFS_BYTEORDER_WRITE(datatype, swaptype) \
  104. int PHYSFS_write##swaptype(PHYSFS_File *file, PHYSFS_##datatype val) { \
  105. const PHYSFS_##datatype out = PHYSFS_swap##swaptype(val); \
  106. BAIL_IF_ERRPASS(!writeAll(file, &out, sizeof (out)), 0); \
  107. return 1; \
  108. }
  109. PHYSFS_BYTEORDER_WRITE(sint16, SLE16)
  110. PHYSFS_BYTEORDER_WRITE(uint16, ULE16)
  111. PHYSFS_BYTEORDER_WRITE(sint16, SBE16)
  112. PHYSFS_BYTEORDER_WRITE(uint16, UBE16)
  113. PHYSFS_BYTEORDER_WRITE(sint32, SLE32)
  114. PHYSFS_BYTEORDER_WRITE(uint32, ULE32)
  115. PHYSFS_BYTEORDER_WRITE(sint32, SBE32)
  116. PHYSFS_BYTEORDER_WRITE(uint32, UBE32)
  117. PHYSFS_BYTEORDER_WRITE(sint64, SLE64)
  118. PHYSFS_BYTEORDER_WRITE(uint64, ULE64)
  119. PHYSFS_BYTEORDER_WRITE(sint64, SBE64)
  120. PHYSFS_BYTEORDER_WRITE(uint64, UBE64)
  121. /* end of physfs_byteorder.c ... */