ByteSwapper.h 9.6 KB

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