ByteSwapper.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2015, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. // Modified by Lasse Oorni for Urho3D
  34. /** @file Helper class tp perform various byte oder swappings
  35. (e.g. little to big endian) */
  36. #ifndef AI_BYTESWAPPER_H_INC
  37. #define AI_BYTESWAPPER_H_INC
  38. #include "../include/assimp/ai_assert.h"
  39. #include "../include/assimp/types.h"
  40. // Urho3D: VS2008 compatibility
  41. #if !defined(_MSC_VER) || (_MSC_VER >= 1600)
  42. #include <stdint.h>
  43. #else
  44. #include "../include/assimp/Compiler/pstdint.h"
  45. #endif
  46. #if _MSC_VER >= 1400
  47. #include <stdlib.h>
  48. #endif
  49. namespace Assimp {
  50. // --------------------------------------------------------------------------------------
  51. /** Defines some useful byte order swap routines.
  52. *
  53. * This is required to read big-endian model formats on little-endian machines,
  54. * and vice versa. Direct use of this class is DEPRECATED. Use #StreamReader instead. */
  55. // --------------------------------------------------------------------------------------
  56. class ByteSwap
  57. {
  58. ByteSwap() {}
  59. public:
  60. // ----------------------------------------------------------------------
  61. /** Swap two bytes of data
  62. * @param[inout] _szOut A void* to save the reintcasts for the caller. */
  63. static inline void Swap2(void* _szOut)
  64. {
  65. ai_assert(_szOut);
  66. #if _MSC_VER >= 1400
  67. uint16_t* const szOut = reinterpret_cast<uint16_t*>(_szOut);
  68. *szOut = _byteswap_ushort(*szOut);
  69. #else
  70. uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
  71. std::swap(szOut[0],szOut[1]);
  72. #endif
  73. }
  74. // ----------------------------------------------------------------------
  75. /** Swap four bytes of data
  76. * @param[inout] _szOut A void* to save the reintcasts for the caller. */
  77. static inline void Swap4(void* _szOut)
  78. {
  79. ai_assert(_szOut);
  80. #if _MSC_VER >= 1400
  81. uint32_t* const szOut = reinterpret_cast<uint32_t*>(_szOut);
  82. *szOut = _byteswap_ulong(*szOut);
  83. #else
  84. uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
  85. std::swap(szOut[0],szOut[3]);
  86. std::swap(szOut[1],szOut[2]);
  87. #endif
  88. }
  89. // ----------------------------------------------------------------------
  90. /** Swap eight bytes of data
  91. * @param[inout] _szOut A void* to save the reintcasts for the caller. */
  92. static inline void Swap8(void* _szOut)
  93. {
  94. ai_assert(_szOut);
  95. #if _MSC_VER >= 1400
  96. uint64_t* const szOut = reinterpret_cast<uint64_t*>(_szOut);
  97. *szOut = _byteswap_uint64(*szOut);
  98. #else
  99. uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
  100. std::swap(szOut[0],szOut[7]);
  101. std::swap(szOut[1],szOut[6]);
  102. std::swap(szOut[2],szOut[5]);
  103. std::swap(szOut[3],szOut[4]);
  104. #endif
  105. }
  106. // ----------------------------------------------------------------------
  107. /** ByteSwap a float. Not a joke.
  108. * @param[inout] fOut ehm. .. */
  109. static inline void Swap(float* fOut) {
  110. Swap4(fOut);
  111. }
  112. // ----------------------------------------------------------------------
  113. /** ByteSwap a double. Not a joke.
  114. * @param[inout] fOut ehm. .. */
  115. static inline void Swap(double* fOut) {
  116. Swap8(fOut);
  117. }
  118. // ----------------------------------------------------------------------
  119. /** ByteSwap an int16t. Not a joke.
  120. * @param[inout] fOut ehm. .. */
  121. static inline void Swap(int16_t* fOut) {
  122. Swap2(fOut);
  123. }
  124. static inline void Swap(uint16_t* fOut) {
  125. Swap2(fOut);
  126. }
  127. // ----------------------------------------------------------------------
  128. /** ByteSwap an int32t. Not a joke.
  129. * @param[inout] fOut ehm. .. */
  130. static inline void Swap(int32_t* fOut){
  131. Swap4(fOut);
  132. }
  133. static inline void Swap(uint32_t* fOut){
  134. Swap4(fOut);
  135. }
  136. // ----------------------------------------------------------------------
  137. /** ByteSwap an int64t. Not a joke.
  138. * @param[inout] fOut ehm. .. */
  139. static inline void Swap(int64_t* fOut) {
  140. Swap8(fOut);
  141. }
  142. static inline void Swap(uint64_t* fOut) {
  143. Swap8(fOut);
  144. }
  145. // ----------------------------------------------------------------------
  146. //! Templatized ByteSwap
  147. //! \returns param tOut as swapped
  148. template<typename Type>
  149. static inline Type Swapped(Type tOut)
  150. {
  151. return _swapper<Type,sizeof(Type)>()(tOut);
  152. }
  153. private:
  154. template <typename T, size_t size> struct _swapper;
  155. };
  156. template <typename T> struct ByteSwap::_swapper<T,2> {
  157. T operator() (T tOut) {
  158. Swap2(&tOut);
  159. return tOut;
  160. }
  161. };
  162. template <typename T> struct ByteSwap::_swapper<T,4> {
  163. T operator() (T tOut) {
  164. Swap4(&tOut);
  165. return tOut;
  166. }
  167. };
  168. template <typename T> struct ByteSwap::_swapper<T,8> {
  169. T operator() (T tOut) {
  170. Swap8(&tOut);
  171. return tOut;
  172. }
  173. };
  174. // --------------------------------------------------------------------------------------
  175. // ByteSwap macros for BigEndian/LittleEndian support
  176. // --------------------------------------------------------------------------------------
  177. #if (defined AI_BUILD_BIG_ENDIAN)
  178. # define AI_LE(t) (t)
  179. # define AI_BE(t) ByteSwap::Swapped(t)
  180. # define AI_LSWAP2(p)
  181. # define AI_LSWAP4(p)
  182. # define AI_LSWAP8(p)
  183. # define AI_LSWAP2P(p)
  184. # define AI_LSWAP4P(p)
  185. # define AI_LSWAP8P(p)
  186. # define LE_NCONST const
  187. # define AI_SWAP2(p) ByteSwap::Swap2(&(p))
  188. # define AI_SWAP4(p) ByteSwap::Swap4(&(p))
  189. # define AI_SWAP8(p) ByteSwap::Swap8(&(p))
  190. # define AI_SWAP2P(p) ByteSwap::Swap2((p))
  191. # define AI_SWAP4P(p) ByteSwap::Swap4((p))
  192. # define AI_SWAP8P(p) ByteSwap::Swap8((p))
  193. # define BE_NCONST
  194. #else
  195. # define AI_BE(t) (t)
  196. # define AI_LE(t) ByteSwap::Swapped(t)
  197. # define AI_SWAP2(p)
  198. # define AI_SWAP4(p)
  199. # define AI_SWAP8(p)
  200. # define AI_SWAP2P(p)
  201. # define AI_SWAP4P(p)
  202. # define AI_SWAP8P(p)
  203. # define BE_NCONST const
  204. # define AI_LSWAP2(p) ByteSwap::Swap2(&(p))
  205. # define AI_LSWAP4(p) ByteSwap::Swap4(&(p))
  206. # define AI_LSWAP8(p) ByteSwap::Swap8(&(p))
  207. # define AI_LSWAP2P(p) ByteSwap::Swap2((p))
  208. # define AI_LSWAP4P(p) ByteSwap::Swap4((p))
  209. # define AI_LSWAP8P(p) ByteSwap::Swap8((p))
  210. # define LE_NCONST
  211. #endif
  212. namespace Intern {
  213. // --------------------------------------------------------------------------------------------
  214. template <typename T, bool doit>
  215. struct ByteSwapper {
  216. void operator() (T* inout) {
  217. ByteSwap::Swap(inout);
  218. }
  219. };
  220. template <typename T>
  221. struct ByteSwapper<T,false> {
  222. void operator() (T*) {
  223. }
  224. };
  225. // --------------------------------------------------------------------------------------------
  226. template <bool SwapEndianess, typename T, bool RuntimeSwitch>
  227. struct Getter {
  228. void operator() (T* inout, bool le) {
  229. #ifdef AI_BUILD_BIG_ENDIAN
  230. le = le;
  231. #else
  232. le = !le;
  233. #endif
  234. if (le) {
  235. ByteSwapper<T,(sizeof(T)>1?true:false)> () (inout);
  236. }
  237. else ByteSwapper<T,false> () (inout);
  238. }
  239. };
  240. template <bool SwapEndianess, typename T>
  241. struct Getter<SwapEndianess,T,false> {
  242. void operator() (T* inout, bool /*le*/) {
  243. // static branch
  244. ByteSwapper<T,(SwapEndianess && sizeof(T)>1)> () (inout);
  245. }
  246. };
  247. } // end Intern
  248. } // end Assimp
  249. #endif //!! AI_BYTESWAPPER_H_INC