physfs_byteorder.c 9.6 KB

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