physfs_byteorder.c 10 KB

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