FBXUtil.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 FBXUtil.cpp
  34. * @brief Implementation of internal FBX utility functions
  35. */
  36. #include "FBXUtil.h"
  37. #include "FBXTokenizer.h"
  38. #include <assimp/TinyFormatter.h>
  39. #include <string>
  40. #include <cstring>
  41. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  42. namespace Assimp {
  43. namespace FBX {
  44. namespace Util {
  45. // ------------------------------------------------------------------------------------------------
  46. const char* TokenTypeString(TokenType t)
  47. {
  48. switch(t) {
  49. case TokenType_OPEN_BRACKET:
  50. return "TOK_OPEN_BRACKET";
  51. case TokenType_CLOSE_BRACKET:
  52. return "TOK_CLOSE_BRACKET";
  53. case TokenType_DATA:
  54. return "TOK_DATA";
  55. case TokenType_COMMA:
  56. return "TOK_COMMA";
  57. case TokenType_KEY:
  58. return "TOK_KEY";
  59. case TokenType_BINARY_DATA:
  60. return "TOK_BINARY_DATA";
  61. }
  62. ai_assert(false);
  63. return "";
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. std::string GetOffsetText(size_t offset)
  67. {
  68. return static_cast<std::string>( Formatter::format() << " (offset 0x" << std::hex << offset << ") " );
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. std::string GetLineAndColumnText(unsigned int line, unsigned int column)
  72. {
  73. return static_cast<std::string>( Formatter::format() << " (line " << line << " << col " << column << ") " );
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. std::string GetTokenText(const Token* tok)
  77. {
  78. if(tok->IsBinary()) {
  79. return static_cast<std::string>( Formatter::format() <<
  80. " (" << TokenTypeString(tok->Type()) <<
  81. ", offset 0x" << std::hex << tok->Offset() << ") " );
  82. }
  83. return static_cast<std::string>( Formatter::format() <<
  84. " (" << TokenTypeString(tok->Type()) <<
  85. ", line " << tok->Line() <<
  86. ", col " << tok->Column() << ") " );
  87. }
  88. // Generated by this formula: T["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
  89. static const uint8_t base64DecodeTable[128] = {
  90. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  91. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  92. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
  93. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255,
  94. 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  95. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
  96. 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  97. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255
  98. };
  99. uint8_t DecodeBase64(char ch)
  100. {
  101. const auto idx = static_cast<uint8_t>(ch);
  102. if (idx > 127)
  103. return 255;
  104. return base64DecodeTable[idx];
  105. }
  106. size_t ComputeDecodedSizeBase64(const char* in, size_t inLength)
  107. {
  108. if (inLength < 2)
  109. {
  110. return 0;
  111. }
  112. const size_t equals = size_t(in[inLength - 1] == '=') + size_t(in[inLength - 2] == '=');
  113. const size_t full_length = (inLength * 3) >> 2; // div by 4
  114. if (full_length < equals)
  115. {
  116. return 0;
  117. }
  118. return full_length - equals;
  119. }
  120. size_t DecodeBase64(const char* in, size_t inLength, uint8_t* out, size_t maxOutLength)
  121. {
  122. if (maxOutLength == 0 || inLength < 2) {
  123. return 0;
  124. }
  125. const size_t realLength = inLength - size_t(in[inLength - 1] == '=') - size_t(in[inLength - 2] == '=');
  126. size_t dst_offset = 0;
  127. int val = 0, valb = -8;
  128. for (size_t src_offset = 0; src_offset < realLength && dst_offset < maxOutLength; ++src_offset)
  129. {
  130. const uint8_t table_value = Util::DecodeBase64(in[src_offset]);
  131. if (table_value == 255)
  132. {
  133. return 0;
  134. }
  135. val = (val << 6) + table_value;
  136. valb += 6;
  137. if (valb >= 0)
  138. {
  139. out[dst_offset++] = static_cast<uint8_t>((val >> valb) & 0xFF);
  140. valb -= 8;
  141. val &= 0xFFF;
  142. }
  143. }
  144. return dst_offset;
  145. }
  146. static const char to_base64_string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  147. char EncodeBase64(char byte)
  148. {
  149. return to_base64_string[(size_t)byte];
  150. }
  151. /** Encodes a block of 4 bytes to base64 encoding
  152. *
  153. * @param bytes Bytes to encode.
  154. * @param out_string String to write encoded values to.
  155. * @param string_pos Position in out_string.*/
  156. void EncodeByteBlock(const char* bytes, std::string& out_string, size_t string_pos)
  157. {
  158. char b0 = (bytes[0] & 0xFC) >> 2;
  159. char b1 = (bytes[0] & 0x03) << 4 | ((bytes[1] & 0xF0) >> 4);
  160. char b2 = (bytes[1] & 0x0F) << 2 | ((bytes[2] & 0xC0) >> 6);
  161. char b3 = (bytes[2] & 0x3F);
  162. out_string[string_pos + 0] = EncodeBase64(b0);
  163. out_string[string_pos + 1] = EncodeBase64(b1);
  164. out_string[string_pos + 2] = EncodeBase64(b2);
  165. out_string[string_pos + 3] = EncodeBase64(b3);
  166. }
  167. std::string EncodeBase64(const char* data, size_t length)
  168. {
  169. // calculate extra bytes needed to get a multiple of 3
  170. size_t extraBytes = 3 - length % 3;
  171. // number of base64 bytes
  172. size_t encodedBytes = 4 * (length + extraBytes) / 3;
  173. std::string encoded_string(encodedBytes, '=');
  174. // read blocks of 3 bytes
  175. for (size_t ib3 = 0; ib3 < length / 3; ib3++)
  176. {
  177. const size_t iByte = ib3 * 3;
  178. const size_t iEncodedByte = ib3 * 4;
  179. const char* currData = &data[iByte];
  180. EncodeByteBlock(currData, encoded_string, iEncodedByte);
  181. }
  182. // if size of data is not a multiple of 3, also encode the final bytes (and add zeros where needed)
  183. if (extraBytes > 0)
  184. {
  185. char finalBytes[4] = { 0,0,0,0 };
  186. memcpy(&finalBytes[0], &data[length - length % 3], length % 3);
  187. const size_t iEncodedByte = encodedBytes - 4;
  188. EncodeByteBlock(&finalBytes[0], encoded_string, iEncodedByte);
  189. // add '=' at the end
  190. for (size_t i = 0; i < 4 * extraBytes / 3; i++)
  191. encoded_string[encodedBytes - i - 1] = '=';
  192. }
  193. return encoded_string;
  194. }
  195. } // !Util
  196. } // !FBX
  197. } // !Assimp
  198. #endif