physfs_byteorder.c 10 KB

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