physfs_byteorder.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include <stdio.h>
  11. #include <stdlib.h>
  12. #define __PHYSICSFS_INTERNAL__
  13. #include "physfs_internal.h"
  14. #if (defined macintosh) && !(defined __MWERKS__)
  15. #define __inline__
  16. #endif
  17. #if (defined _MSC_VER)
  18. #define __inline__ __inline
  19. #endif
  20. #ifndef PHYSFS_Swap16
  21. static __inline__ PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
  22. {
  23. return((D<<8)|(D>>8));
  24. }
  25. #endif
  26. #ifndef PHYSFS_Swap32
  27. static __inline__ PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
  28. {
  29. return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
  30. }
  31. #endif
  32. #ifndef PHYSFS_NO_64BIT_SUPPORT
  33. #ifndef PHYSFS_Swap64
  34. static __inline__ PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
  35. PHYSFS_uint32 hi, lo;
  36. /* Separate into high and low 32-bit values and swap them */
  37. lo = (PHYSFS_uint32)(val&0xFFFFFFFF);
  38. val >>= 32;
  39. hi = (PHYSFS_uint32)(val&0xFFFFFFFF);
  40. val = PHYSFS_Swap32(lo);
  41. val <<= 32;
  42. val |= PHYSFS_Swap32(hi);
  43. return(val);
  44. }
  45. #endif
  46. #else
  47. #ifndef PHYSFS_Swap64
  48. /* This is mainly to keep compilers from complaining in PHYSFS code.
  49. If there is no real 64-bit datatype, then compilers will complain about
  50. the fake 64-bit datatype that PHYSFS provides when it compiles user code.
  51. */
  52. #define PHYSFS_Swap64(X) (X)
  53. #endif
  54. #endif /* PHYSFS_NO_64BIT_SUPPORT */
  55. /* Byteswap item from the specified endianness to the native endianness */
  56. #if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
  57. PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(x); }
  58. PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(x); }
  59. PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(x); }
  60. PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(x); }
  61. PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(x); }
  62. PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(x); }
  63. PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
  64. PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
  65. PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
  66. PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
  67. PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
  68. PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
  69. #else
  70. PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
  71. PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
  72. PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
  73. PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
  74. PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
  75. PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
  76. PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(x); }
  77. PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(x); }
  78. PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(x); }
  79. PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(x); }
  80. PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(x); }
  81. PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(x); }
  82. #endif
  83. int PHYSFS_readSLE16(PHYSFS_File *file, PHYSFS_sint16 *val)
  84. {
  85. PHYSFS_sint16 in;
  86. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  87. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  88. *val = PHYSFS_swapSLE16(in);
  89. return(1);
  90. } /* PHYSFS_readSLE16 */
  91. int PHYSFS_readULE16(PHYSFS_File *file, PHYSFS_uint16 *val)
  92. {
  93. PHYSFS_uint16 in;
  94. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  95. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  96. *val = PHYSFS_swapULE16(in);
  97. return(1);
  98. } /* PHYSFS_readULE16 */
  99. int PHYSFS_readSBE16(PHYSFS_File *file, PHYSFS_sint16 *val)
  100. {
  101. PHYSFS_sint16 in;
  102. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  103. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  104. *val = PHYSFS_swapSBE16(in);
  105. return(1);
  106. } /* PHYSFS_readSBE16 */
  107. int PHYSFS_readUBE16(PHYSFS_File *file, PHYSFS_uint16 *val)
  108. {
  109. PHYSFS_uint16 in;
  110. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  111. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  112. *val = PHYSFS_swapUBE16(in);
  113. return(1);
  114. } /* PHYSFS_readUBE16 */
  115. int PHYSFS_readSLE32(PHYSFS_File *file, PHYSFS_sint32 *val)
  116. {
  117. PHYSFS_sint32 in;
  118. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  119. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  120. *val = PHYSFS_swapSLE32(in);
  121. return(1);
  122. } /* PHYSFS_readSLE32 */
  123. int PHYSFS_readULE32(PHYSFS_File *file, PHYSFS_uint32 *val)
  124. {
  125. PHYSFS_uint32 in;
  126. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  127. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  128. *val = PHYSFS_swapULE32(in);
  129. return(1);
  130. } /* PHYSFS_readULE32 */
  131. int PHYSFS_readSBE32(PHYSFS_File *file, PHYSFS_sint32 *val)
  132. {
  133. PHYSFS_sint32 in;
  134. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  135. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  136. *val = PHYSFS_swapSBE32(in);
  137. return(1);
  138. } /* PHYSFS_readSBE32 */
  139. int PHYSFS_readUBE32(PHYSFS_File *file, PHYSFS_uint32 *val)
  140. {
  141. PHYSFS_uint32 in;
  142. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  143. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  144. *val = PHYSFS_swapUBE32(in);
  145. return(1);
  146. } /* PHYSFS_readUBE32 */
  147. int PHYSFS_readSLE64(PHYSFS_File *file, PHYSFS_sint64 *val)
  148. {
  149. PHYSFS_sint64 in;
  150. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  151. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  152. *val = PHYSFS_swapSLE64(in);
  153. return(1);
  154. } /* PHYSFS_readSLE64 */
  155. int PHYSFS_readULE64(PHYSFS_File *file, PHYSFS_uint64 *val)
  156. {
  157. PHYSFS_uint64 in;
  158. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  159. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  160. *val = PHYSFS_swapULE64(in);
  161. return(1);
  162. } /* PHYSFS_readULE64 */
  163. int PHYSFS_readSBE64(PHYSFS_File *file, PHYSFS_sint64 *val)
  164. {
  165. PHYSFS_sint64 in;
  166. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  167. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  168. *val = PHYSFS_swapSBE64(in);
  169. return(1);
  170. } /* PHYSFS_readSBE64 */
  171. int PHYSFS_readUBE64(PHYSFS_File *file, PHYSFS_uint64 *val)
  172. {
  173. PHYSFS_uint64 in;
  174. BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
  175. BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
  176. *val = PHYSFS_swapUBE64(in);
  177. return(1);
  178. } /* PHYSFS_readUBE64 */
  179. int PHYSFS_writeSLE16(PHYSFS_File *file, PHYSFS_sint16 val)
  180. {
  181. PHYSFS_sint16 out = PHYSFS_swapSLE16(val);
  182. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  183. return(1);
  184. } /* PHYSFS_writeSLE16 */
  185. int PHYSFS_writeULE16(PHYSFS_File *file, PHYSFS_uint16 val)
  186. {
  187. PHYSFS_uint16 out = PHYSFS_swapULE16(val);
  188. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  189. return(1);
  190. } /* PHYSFS_writeULE16 */
  191. int PHYSFS_writeSBE16(PHYSFS_File *file, PHYSFS_sint16 val)
  192. {
  193. PHYSFS_sint16 out = PHYSFS_swapSBE16(val);
  194. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  195. return(1);
  196. } /* PHYSFS_writeSBE16 */
  197. int PHYSFS_writeUBE16(PHYSFS_File *file, PHYSFS_uint16 val)
  198. {
  199. PHYSFS_uint16 out = PHYSFS_swapUBE16(val);
  200. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  201. return(1);
  202. } /* PHYSFS_writeUBE16 */
  203. int PHYSFS_writeSLE32(PHYSFS_File *file, PHYSFS_sint32 val)
  204. {
  205. PHYSFS_sint32 out = PHYSFS_swapSLE32(val);
  206. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  207. return(1);
  208. } /* PHYSFS_writeSLE32 */
  209. int PHYSFS_writeULE32(PHYSFS_File *file, PHYSFS_uint32 val)
  210. {
  211. PHYSFS_uint32 out = PHYSFS_swapULE32(val);
  212. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  213. return(1);
  214. } /* PHYSFS_writeULE32 */
  215. int PHYSFS_writeSBE32(PHYSFS_File *file, PHYSFS_sint32 val)
  216. {
  217. PHYSFS_sint32 out = PHYSFS_swapSBE32(val);
  218. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  219. return(1);
  220. } /* PHYSFS_writeSBE32 */
  221. int PHYSFS_writeUBE32(PHYSFS_File *file, PHYSFS_uint32 val)
  222. {
  223. PHYSFS_uint32 out = PHYSFS_swapUBE32(val);
  224. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  225. return(1);
  226. } /* PHYSFS_writeUBE32 */
  227. int PHYSFS_writeSLE64(PHYSFS_File *file, PHYSFS_sint64 val)
  228. {
  229. PHYSFS_sint64 out = PHYSFS_swapSLE64(val);
  230. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  231. return(1);
  232. } /* PHYSFS_writeSLE64 */
  233. int PHYSFS_writeULE64(PHYSFS_File *file, PHYSFS_uint64 val)
  234. {
  235. PHYSFS_uint64 out = PHYSFS_swapULE64(val);
  236. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  237. return(1);
  238. } /* PHYSFS_writeULE64 */
  239. int PHYSFS_writeSBE64(PHYSFS_File *file, PHYSFS_sint64 val)
  240. {
  241. PHYSFS_sint64 out = PHYSFS_swapSBE64(val);
  242. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  243. return(1);
  244. } /* PHYSFS_writeSBE64 */
  245. int PHYSFS_writeUBE64(PHYSFS_File *file, PHYSFS_uint64 val)
  246. {
  247. PHYSFS_uint64 out = PHYSFS_swapUBE64(val);
  248. BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
  249. return(1);
  250. } /* PHYSFS_writeUBE64 */
  251. /* end of physfs_byteorder.c ... */