physfs_byteorder.c 9.7 KB

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