FBXBinaryTokenizer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2024, 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 FBXBinaryTokenizer.cpp
  34. * @brief Implementation of a fake lexer for binary fbx files -
  35. * we emit tokens so the parser needs almost no special handling
  36. * for binary files.
  37. */
  38. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  39. #include "FBXTokenizer.h"
  40. #include "FBXUtil.h"
  41. #include <assimp/defs.h>
  42. #include <stdint.h>
  43. #include <cstdint>
  44. #include <assimp/Exceptional.h>
  45. #include <assimp/ByteSwapper.h>
  46. #include <assimp/DefaultLogger.hpp>
  47. #include <assimp/StringUtils.h>
  48. namespace Assimp {
  49. namespace FBX {
  50. //enum Flag
  51. //{
  52. // e_unknown_0 = 1 << 0,
  53. // e_unknown_1 = 1 << 1,
  54. // e_unknown_2 = 1 << 2,
  55. // e_unknown_3 = 1 << 3,
  56. // e_unknown_4 = 1 << 4,
  57. // e_unknown_5 = 1 << 5,
  58. // e_unknown_6 = 1 << 6,
  59. // e_unknown_7 = 1 << 7,
  60. // e_unknown_8 = 1 << 8,
  61. // e_unknown_9 = 1 << 9,
  62. // e_unknown_10 = 1 << 10,
  63. // e_unknown_11 = 1 << 11,
  64. // e_unknown_12 = 1 << 12,
  65. // e_unknown_13 = 1 << 13,
  66. // e_unknown_14 = 1 << 14,
  67. // e_unknown_15 = 1 << 15,
  68. // e_unknown_16 = 1 << 16,
  69. // e_unknown_17 = 1 << 17,
  70. // e_unknown_18 = 1 << 18,
  71. // e_unknown_19 = 1 << 19,
  72. // e_unknown_20 = 1 << 20,
  73. // e_unknown_21 = 1 << 21,
  74. // e_unknown_22 = 1 << 22,
  75. // e_unknown_23 = 1 << 23,
  76. // e_flag_field_size_64_bit = 1 << 24, // Not sure what is
  77. // e_unknown_25 = 1 << 25,
  78. // e_unknown_26 = 1 << 26,
  79. // e_unknown_27 = 1 << 27,
  80. // e_unknown_28 = 1 << 28,
  81. // e_unknown_29 = 1 << 29,
  82. // e_unknown_30 = 1 << 30,
  83. // e_unknown_31 = 1 << 31
  84. //};
  85. //
  86. //bool check_flag(uint32_t flags, Flag to_check)
  87. //{
  88. // return (flags & to_check) != 0;
  89. //}
  90. // ------------------------------------------------------------------------------------------------
  91. Token::Token(const char* sbegin, const char* send, TokenType type, size_t offset)
  92. :
  93. #ifdef DEBUG
  94. contents(sbegin, static_cast<size_t>(send-sbegin)),
  95. #endif
  96. sbegin(sbegin)
  97. , send(send)
  98. , type(type)
  99. , line(offset)
  100. , column(BINARY_MARKER)
  101. {
  102. ai_assert(sbegin);
  103. ai_assert(send);
  104. // binary tokens may have zero length because they are sometimes dummies
  105. // inserted by TokenizeBinary()
  106. ai_assert(send >= sbegin);
  107. }
  108. namespace {
  109. // ------------------------------------------------------------------------------------------------
  110. // signal tokenization error, this is always unrecoverable. Throws DeadlyImportError.
  111. AI_WONT_RETURN void TokenizeError(const std::string& message, size_t offset) AI_WONT_RETURN_SUFFIX;
  112. AI_WONT_RETURN void TokenizeError(const std::string& message, size_t offset)
  113. {
  114. throw DeadlyImportError("FBX-Tokenize", Util::GetOffsetText(offset), message);
  115. }
  116. // ------------------------------------------------------------------------------------------------
  117. size_t Offset(const char* begin, const char* cursor) {
  118. ai_assert(begin <= cursor);
  119. return cursor - begin;
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. AI_WONT_RETURN void TokenizeError(const std::string& message, const char* begin, const char* cursor) AI_WONT_RETURN_SUFFIX;
  123. void TokenizeError(const std::string& message, const char* begin, const char* cursor) {
  124. TokenizeError(message, Offset(begin, cursor));
  125. }
  126. // ------------------------------------------------------------------------------------------------
  127. uint32_t ReadWord(const char* input, const char*& cursor, const char* end) {
  128. const size_t k_to_read = sizeof( uint32_t );
  129. if(Offset(cursor, end) < k_to_read ) {
  130. TokenizeError("cannot ReadWord, out of bounds",input, cursor);
  131. }
  132. uint32_t word;
  133. ::memcpy(&word, cursor, 4);
  134. AI_SWAP4(word);
  135. cursor += k_to_read;
  136. return word;
  137. }
  138. // ------------------------------------------------------------------------------------------------
  139. uint64_t ReadDoubleWord(const char* input, const char*& cursor, const char* end) {
  140. const size_t k_to_read = sizeof(uint64_t);
  141. if(Offset(cursor, end) < k_to_read) {
  142. TokenizeError("cannot ReadDoubleWord, out of bounds",input, cursor);
  143. }
  144. uint64_t dword /*= *reinterpret_cast<const uint64_t*>(cursor)*/;
  145. ::memcpy( &dword, cursor, sizeof( uint64_t ) );
  146. AI_SWAP8(dword);
  147. cursor += k_to_read;
  148. return dword;
  149. }
  150. // ------------------------------------------------------------------------------------------------
  151. uint8_t ReadByte(const char* input, const char*& cursor, const char* end) {
  152. if(Offset(cursor, end) < sizeof( uint8_t ) ) {
  153. TokenizeError("cannot ReadByte, out of bounds",input, cursor);
  154. }
  155. uint8_t word;/* = *reinterpret_cast< const uint8_t* >( cursor )*/
  156. ::memcpy( &word, cursor, sizeof( uint8_t ) );
  157. ++cursor;
  158. return word;
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. unsigned int ReadString(const char*& sbegin_out, const char*& send_out, const char* input,
  162. const char*& cursor, const char* end, bool long_length = false, bool allow_null = false) {
  163. const uint32_t len_len = long_length ? 4 : 1;
  164. if(Offset(cursor, end) < len_len) {
  165. TokenizeError("cannot ReadString, out of bounds reading length",input, cursor);
  166. }
  167. const uint32_t length = long_length ? ReadWord(input, cursor, end) : ReadByte(input, cursor, end);
  168. if (Offset(cursor, end) < length) {
  169. TokenizeError("cannot ReadString, length is out of bounds",input, cursor);
  170. }
  171. sbegin_out = cursor;
  172. cursor += length;
  173. send_out = cursor;
  174. if(!allow_null) {
  175. for (unsigned int i = 0; i < length; ++i) {
  176. if(sbegin_out[i] == '\0') {
  177. TokenizeError("failed ReadString, unexpected NUL character in string",input, cursor);
  178. }
  179. }
  180. }
  181. return length;
  182. }
  183. // ------------------------------------------------------------------------------------------------
  184. void ReadData(const char*& sbegin_out, const char*& send_out, const char* input, const char*& cursor, const char* end) {
  185. if(Offset(cursor, end) < 1) {
  186. TokenizeError("cannot ReadData, out of bounds reading length",input, cursor);
  187. }
  188. const char type = *cursor;
  189. sbegin_out = cursor++;
  190. switch(type)
  191. {
  192. // 16 bit int
  193. case 'Y':
  194. cursor += 2;
  195. break;
  196. // 1 bit bool flag (yes/no)
  197. case 'C':
  198. cursor += 1;
  199. break;
  200. // 32 bit int
  201. case 'I':
  202. // <- fall through
  203. // float
  204. case 'F':
  205. cursor += 4;
  206. break;
  207. // double
  208. case 'D':
  209. cursor += 8;
  210. break;
  211. // 64 bit int
  212. case 'L':
  213. cursor += 8;
  214. break;
  215. // note: do not write cursor += ReadWord(...cursor) as this would be UB
  216. // raw binary data
  217. case 'R':
  218. {
  219. const uint32_t length = ReadWord(input, cursor, end);
  220. cursor += length;
  221. break;
  222. }
  223. case 'b':
  224. // TODO: what is the 'b' type code? Right now we just skip over it /
  225. // take the full range we could get
  226. cursor = end;
  227. break;
  228. // array of *
  229. case 'f':
  230. case 'd':
  231. case 'l':
  232. case 'i':
  233. case 'c': {
  234. const uint32_t length = ReadWord(input, cursor, end);
  235. const uint32_t encoding = ReadWord(input, cursor, end);
  236. const uint32_t comp_len = ReadWord(input, cursor, end);
  237. // compute length based on type and check against the stored value
  238. if(encoding == 0) {
  239. uint32_t stride = 0;
  240. switch(type)
  241. {
  242. case 'f':
  243. case 'i':
  244. stride = 4;
  245. break;
  246. case 'd':
  247. case 'l':
  248. stride = 8;
  249. break;
  250. case 'c':
  251. stride = 1;
  252. break;
  253. default:
  254. ai_assert(false);
  255. };
  256. ai_assert(stride > 0);
  257. if(length * stride != comp_len) {
  258. TokenizeError("cannot ReadData, calculated data stride differs from what the file claims",input, cursor);
  259. }
  260. }
  261. // zip/deflate algorithm (encoding==1)? take given length. anything else? die
  262. else if (encoding != 1) {
  263. TokenizeError("cannot ReadData, unknown encoding",input, cursor);
  264. }
  265. cursor += comp_len;
  266. break;
  267. }
  268. // string
  269. case 'S': {
  270. const char* sb, *se;
  271. // 0 characters can legally happen in such strings
  272. ReadString(sb, se, input, cursor, end, true, true);
  273. break;
  274. }
  275. default:
  276. TokenizeError("cannot ReadData, unexpected type code: " + std::string(&type, 1),input, cursor);
  277. }
  278. if(cursor > end) {
  279. TokenizeError("cannot ReadData, the remaining size is too small for the data type: " + std::string(&type, 1),input, cursor);
  280. }
  281. // the type code is contained in the returned range
  282. send_out = cursor;
  283. }
  284. // ------------------------------------------------------------------------------------------------
  285. bool ReadScope(TokenList &output_tokens, StackAllocator &token_allocator, const char *input, const char *&cursor, const char *end, bool const is64bits) {
  286. // the first word contains the offset at which this block ends
  287. const uint64_t end_offset = is64bits ? ReadDoubleWord(input, cursor, end) : ReadWord(input, cursor, end);
  288. // we may get 0 if reading reached the end of the file -
  289. // fbx files have a mysterious extra footer which I don't know
  290. // how to extract any information from, but at least it always
  291. // starts with a 0.
  292. if(!end_offset) {
  293. return false;
  294. }
  295. if(end_offset > Offset(input, end)) {
  296. TokenizeError("block offset is out of range",input, cursor);
  297. }
  298. else if(end_offset < Offset(input, cursor)) {
  299. TokenizeError("block offset is negative out of range",input, cursor);
  300. }
  301. // the second data word contains the number of properties in the scope
  302. const uint64_t prop_count = is64bits ? ReadDoubleWord(input, cursor, end) : ReadWord(input, cursor, end);
  303. // the third data word contains the length of the property list
  304. const uint64_t prop_length = is64bits ? ReadDoubleWord(input, cursor, end) : ReadWord(input, cursor, end);
  305. // now comes the name of the scope/key
  306. const char* sbeg, *send;
  307. ReadString(sbeg, send, input, cursor, end);
  308. output_tokens.push_back(new_Token(sbeg, send, TokenType_KEY, Offset(input, cursor) ));
  309. // now come the individual properties
  310. const char* begin_cursor = cursor;
  311. if ((begin_cursor + prop_length) > end) {
  312. TokenizeError("property length out of bounds reading length ", input, cursor);
  313. }
  314. for (unsigned int i = 0; i < prop_count; ++i) {
  315. ReadData(sbeg, send, input, cursor, begin_cursor + prop_length);
  316. output_tokens.push_back(new_Token(sbeg, send, TokenType_DATA, Offset(input, cursor) ));
  317. if(i != prop_count-1) {
  318. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_COMMA, Offset(input, cursor) ));
  319. }
  320. }
  321. if (Offset(begin_cursor, cursor) != prop_length) {
  322. TokenizeError("property length not reached, something is wrong",input, cursor);
  323. }
  324. // at the end of each nested block, there is a NUL record to indicate
  325. // that the sub-scope exists (i.e. to distinguish between P: and P : {})
  326. // this NUL record is 13 bytes long on 32 bit version and 25 bytes long on 64 bit.
  327. const size_t sentinel_block_length = is64bits ? (sizeof(uint64_t)* 3 + 1) : (sizeof(uint32_t)* 3 + 1);
  328. if (Offset(input, cursor) < end_offset) {
  329. if (end_offset - Offset(input, cursor) < sentinel_block_length) {
  330. TokenizeError("insufficient padding bytes at block end",input, cursor);
  331. }
  332. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_OPEN_BRACKET, Offset(input, cursor) ));
  333. // XXX this is vulnerable to stack overflowing ..
  334. while(Offset(input, cursor) < end_offset - sentinel_block_length) {
  335. ReadScope(output_tokens, token_allocator, input, cursor, input + end_offset - sentinel_block_length, is64bits);
  336. }
  337. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_CLOSE_BRACKET, Offset(input, cursor) ));
  338. for (unsigned int i = 0; i < sentinel_block_length; ++i) {
  339. if(cursor[i] != '\0') {
  340. TokenizeError("failed to read nested block sentinel, expected all bytes to be 0",input, cursor);
  341. }
  342. }
  343. cursor += sentinel_block_length;
  344. }
  345. if (Offset(input, cursor) != end_offset) {
  346. TokenizeError("scope length not reached, something is wrong",input, cursor);
  347. }
  348. return true;
  349. }
  350. } // anonymous namespace
  351. // ------------------------------------------------------------------------------------------------
  352. // TODO: Test FBX Binary files newer than the 7500 version to check if the 64 bits address behaviour is consistent
  353. void TokenizeBinary(TokenList &output_tokens, const char *input, size_t length, StackAllocator &token_allocator) {
  354. ai_assert(input);
  355. ASSIMP_LOG_DEBUG("Tokenizing binary FBX file");
  356. if(length < 0x1b) {
  357. TokenizeError("file is too short",0);
  358. }
  359. //uint32_t offset = 0x15;
  360. /* const char* cursor = input + 0x15;
  361. const uint32_t flags = ReadWord(input, cursor, input + length);
  362. const uint8_t padding_0 = ReadByte(input, cursor, input + length); // unused
  363. const uint8_t padding_1 = ReadByte(input, cursor, input + length); // unused*/
  364. if (strncmp(input,"Kaydara FBX Binary",18)) {
  365. TokenizeError("magic bytes not found",0);
  366. }
  367. const char* cursor = input + 18;
  368. /*Result ignored*/ ReadByte(input, cursor, input + length);
  369. /*Result ignored*/ ReadByte(input, cursor, input + length);
  370. /*Result ignored*/ ReadByte(input, cursor, input + length);
  371. /*Result ignored*/ ReadByte(input, cursor, input + length);
  372. /*Result ignored*/ ReadByte(input, cursor, input + length);
  373. const uint32_t version = ReadWord(input, cursor, input + length);
  374. ASSIMP_LOG_DEBUG("FBX version: ", version);
  375. const bool is64bits = version >= 7500;
  376. const char *end = input + length;
  377. try
  378. {
  379. while (cursor < end ) {
  380. if (!ReadScope(output_tokens, token_allocator, input, cursor, input + length, is64bits)) {
  381. break;
  382. }
  383. }
  384. }
  385. catch (const DeadlyImportError& e)
  386. {
  387. if (!is64bits && (length > std::numeric_limits<uint32_t>::max())) {
  388. throw DeadlyImportError("The FBX file is invalid. This may be because the content is too big for this older version (", ai_to_string(version), ") of the FBX format. (", e.what(), ")");
  389. }
  390. throw;
  391. }
  392. }
  393. } // !FBX
  394. } // !Assimp
  395. #endif