physfs_byteorder.c 10 KB

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