ByteSwapper.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. /** @file Helper class tp perform various byte order swappings
  34. (e.g. little to big endian) */
  35. #pragma once
  36. #ifndef AI_BYTESWAPPER_H_INC
  37. #define AI_BYTESWAPPER_H_INC
  38. #ifdef __GNUC__
  39. # pragma GCC system_header
  40. #endif
  41. #include <assimp/ai_assert.h>
  42. #include <assimp/types.h>
  43. #include <cstdint>
  44. #if _MSC_VER >= 1400
  45. #include <cstdlib>
  46. #endif
  47. namespace Assimp {
  48. // --------------------------------------------------------------------------------------
  49. /** Defines some useful byte order swap routines.
  50. *
  51. * This is required to read big-endian model formats on little-endian machines,
  52. * and vice versa. Direct use of this class is DEPRECATED. Use #StreamReader instead. */
  53. // --------------------------------------------------------------------------------------
  54. class ByteSwap {
  55. ByteSwap() AI_NO_EXCEPT = default;
  56. ~ByteSwap() = default;
  57. public:
  58. // ----------------------------------------------------------------------
  59. /** Swap two bytes of data
  60. * @param[inout] _szOut A void* to save the reintcasts for the caller. */
  61. static inline void Swap2(void* _szOut)
  62. {
  63. ai_assert(_szOut);
  64. #if _MSC_VER >= 1400
  65. uint16_t* const szOut = reinterpret_cast<uint16_t*>(_szOut);
  66. *szOut = _byteswap_ushort(*szOut);
  67. #else
  68. uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
  69. std::swap(szOut[0],szOut[1]);
  70. #endif
  71. }
  72. // ----------------------------------------------------------------------
  73. /** Swap four bytes of data
  74. * @param[inout] _szOut A void* to save the reintcasts for the caller. */
  75. static inline void Swap4(void* _szOut) {
  76. ai_assert(_szOut);
  77. #if _MSC_VER >= 1400
  78. uint32_t* const szOut = reinterpret_cast<uint32_t*>(_szOut);
  79. *szOut = _byteswap_ulong(*szOut);
  80. #else
  81. uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
  82. std::swap(szOut[0],szOut[3]);
  83. std::swap(szOut[1],szOut[2]);
  84. #endif
  85. }
  86. // ----------------------------------------------------------------------
  87. /** Swap eight bytes of data
  88. * @param[inout] _szOut A void* to save the reintcasts for the caller. */
  89. static inline void Swap8(void* _szOut)
  90. {
  91. ai_assert(_szOut);
  92. #if _MSC_VER >= 1400
  93. uint64_t* const szOut = reinterpret_cast<uint64_t*>(_szOut);
  94. *szOut = _byteswap_uint64(*szOut);
  95. #else
  96. uint8_t* const szOut = reinterpret_cast<uint8_t*>(_szOut);
  97. std::swap(szOut[0],szOut[7]);
  98. std::swap(szOut[1],szOut[6]);
  99. std::swap(szOut[2],szOut[5]);
  100. std::swap(szOut[3],szOut[4]);
  101. #endif
  102. }
  103. // ----------------------------------------------------------------------
  104. /** ByteSwap a float. Not a joke.
  105. * @param[inout] fOut ehm. .. */
  106. static inline void Swap(float* fOut) {
  107. Swap4(fOut);
  108. }
  109. // ----------------------------------------------------------------------
  110. /** ByteSwap a double. Not a joke.
  111. * @param[inout] fOut ehm. .. */
  112. static inline void Swap(double* fOut) {
  113. Swap8(fOut);
  114. }
  115. // ----------------------------------------------------------------------
  116. /** ByteSwap an int16t. Not a joke.
  117. * @param[inout] fOut ehm. .. */
  118. static inline void Swap(int16_t* fOut) {
  119. Swap2(fOut);
  120. }
  121. static inline void Swap(uint16_t* fOut) {
  122. Swap2(fOut);
  123. }
  124. // ----------------------------------------------------------------------
  125. /** ByteSwap an int32t. Not a joke.
  126. * @param[inout] fOut ehm. .. */
  127. static inline void Swap(int32_t* fOut){
  128. Swap4(fOut);
  129. }
  130. static inline void Swap(uint32_t* fOut){
  131. Swap4(fOut);
  132. }
  133. // ----------------------------------------------------------------------
  134. /** ByteSwap an int64t. Not a joke.
  135. * @param[inout] fOut ehm. .. */
  136. static inline void Swap(int64_t* fOut) {
  137. Swap8(fOut);
  138. }
  139. static inline void Swap(uint64_t* fOut) {
  140. Swap8(fOut);
  141. }
  142. // ----------------------------------------------------------------------
  143. //! Templatized ByteSwap
  144. //! \returns param tOut as swapped
  145. template<typename Type>
  146. static inline Type Swapped(Type tOut)
  147. {
  148. return _swapper<Type,sizeof(Type)>()(tOut);
  149. }
  150. private:
  151. template <typename T, size_t size> struct _swapper;
  152. };
  153. template <typename T> struct ByteSwap::_swapper<T,2> {
  154. T operator() (T tOut) {
  155. Swap2(&tOut);
  156. return tOut;
  157. }
  158. };
  159. template <typename T> struct ByteSwap::_swapper<T,4> {
  160. T operator() (T tOut) {
  161. Swap4(&tOut);
  162. return tOut;
  163. }
  164. };
  165. template <typename T> struct ByteSwap::_swapper<T,8> {
  166. T operator() (T tOut) {
  167. Swap8(&tOut);
  168. return tOut;
  169. }
  170. };
  171. // --------------------------------------------------------------------------------------
  172. // ByteSwap macros for BigEndian/LittleEndian support
  173. // --------------------------------------------------------------------------------------
  174. #if (defined AI_BUILD_BIG_ENDIAN)
  175. # define AI_LE(t) (t)
  176. # define AI_BE(t) Assimp::ByteSwap::Swapped(t)
  177. # define AI_LSWAP2(p)
  178. # define AI_LSWAP4(p)
  179. # define AI_LSWAP8(p)
  180. # define AI_LSWAP2P(p)
  181. # define AI_LSWAP4P(p)
  182. # define AI_LSWAP8P(p)
  183. # define LE_NCONST const
  184. # define AI_SWAP2(p) Assimp::ByteSwap::Swap2(&(p))
  185. # define AI_SWAP4(p) Assimp::ByteSwap::Swap4(&(p))
  186. # define AI_SWAP8(p) Assimp::ByteSwap::Swap8(&(p))
  187. # define AI_SWAP2P(p) Assimp::ByteSwap::Swap2((p))
  188. # define AI_SWAP4P(p) Assimp::ByteSwap::Swap4((p))
  189. # define AI_SWAP8P(p) Assimp::ByteSwap::Swap8((p))
  190. # define BE_NCONST
  191. #else
  192. # define AI_BE(t) (t)
  193. # define AI_LE(t) Assimp::ByteSwap::Swapped(t)
  194. # define AI_SWAP2(p)
  195. # define AI_SWAP4(p)
  196. # define AI_SWAP8(p)
  197. # define AI_SWAP2P(p)
  198. # define AI_SWAP4P(p)
  199. # define AI_SWAP8P(p)
  200. # define BE_NCONST const
  201. # define AI_LSWAP2(p) Assimp::ByteSwap::Swap2(&(p))
  202. # define AI_LSWAP4(p) Assimp::ByteSwap::Swap4(&(p))
  203. # define AI_LSWAP8(p) Assimp::ByteSwap::Swap8(&(p))
  204. # define AI_LSWAP2P(p) Assimp::ByteSwap::Swap2((p))
  205. # define AI_LSWAP4P(p) Assimp::ByteSwap::Swap4((p))
  206. # define AI_LSWAP8P(p) Assimp::ByteSwap::Swap8((p))
  207. # define LE_NCONST
  208. #endif
  209. namespace Intern {
  210. // --------------------------------------------------------------------------------------------
  211. template <typename T, bool doit>
  212. struct ByteSwapper {
  213. void operator() (T* inout) {
  214. ByteSwap::Swap(inout);
  215. }
  216. };
  217. template <typename T>
  218. struct ByteSwapper<T,false> {
  219. void operator() (T*) {
  220. }
  221. };
  222. // --------------------------------------------------------------------------------------------
  223. template <bool SwapEndianness, typename T, bool RuntimeSwitch>
  224. struct Getter {
  225. void operator() (T* inout, bool le) {
  226. #ifdef AI_BUILD_BIG_ENDIAN
  227. le = le;
  228. #else
  229. le = !le;
  230. #endif
  231. if (le) {
  232. ByteSwapper<T,(sizeof(T)>1?true:false)> () (inout);
  233. }
  234. else ByteSwapper<T,false> () (inout);
  235. }
  236. };
  237. template <bool SwapEndianness, typename T>
  238. struct Getter<SwapEndianness,T,false> {
  239. void operator() (T* inout, bool /*le*/) {
  240. // static branch
  241. ByteSwapper<T,(SwapEndianness && sizeof(T)>1)> () (inout);
  242. }
  243. };
  244. } // end Intern
  245. } // end Assimp
  246. #endif //!! AI_BYTESWAPPER_H_INC