FBXBinaryTokenizer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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 "Exceptional.h"
  44. #include "ByteSwapper.h"
  45. namespace Assimp {
  46. namespace FBX {
  47. enum Flag
  48. {
  49. e_unknown_0 = 1 << 0,
  50. e_unknown_1 = 1 << 1,
  51. e_unknown_2 = 1 << 2,
  52. e_unknown_3 = 1 << 3,
  53. e_unknown_4 = 1 << 4,
  54. e_unknown_5 = 1 << 5,
  55. e_unknown_6 = 1 << 6,
  56. e_unknown_7 = 1 << 7,
  57. e_unknown_8 = 1 << 8,
  58. e_unknown_9 = 1 << 9,
  59. e_unknown_10 = 1 << 10,
  60. e_unknown_11 = 1 << 11,
  61. e_unknown_12 = 1 << 12,
  62. e_unknown_13 = 1 << 13,
  63. e_unknown_14 = 1 << 14,
  64. e_unknown_15 = 1 << 15,
  65. e_unknown_16 = 1 << 16,
  66. e_unknown_17 = 1 << 17,
  67. e_unknown_18 = 1 << 18,
  68. e_unknown_19 = 1 << 19,
  69. e_unknown_20 = 1 << 20,
  70. e_unknown_21 = 1 << 21,
  71. e_unknown_22 = 1 << 22,
  72. e_unknown_23 = 1 << 23,
  73. e_flag_field_size_64_bit = 1 << 24, // Not sure what is
  74. e_unknown_25 = 1 << 25,
  75. e_unknown_26 = 1 << 26,
  76. e_unknown_27 = 1 << 27,
  77. e_unknown_28 = 1 << 28,
  78. e_unknown_29 = 1 << 29,
  79. e_unknown_30 = 1 << 30,
  80. e_unknown_31 = 1 << 31
  81. };
  82. bool check_flag(uint32_t flags, Flag to_check)
  83. {
  84. return (flags & to_check) != 0;
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. Token::Token(const char* sbegin, const char* send, TokenType type, unsigned int offset)
  88. :
  89. #ifdef DEBUG
  90. contents(sbegin, static_cast<size_t>(send-sbegin)),
  91. #endif
  92. sbegin(sbegin)
  93. , send(send)
  94. , type(type)
  95. , line(offset)
  96. , column(BINARY_MARKER)
  97. {
  98. ai_assert(sbegin);
  99. ai_assert(send);
  100. // binary tokens may have zero length because they are sometimes dummies
  101. // inserted by TokenizeBinary()
  102. ai_assert(send >= sbegin);
  103. }
  104. namespace {
  105. // ------------------------------------------------------------------------------------------------
  106. // signal tokenization error, this is always unrecoverable. Throws DeadlyImportError.
  107. AI_WONT_RETURN void TokenizeError(const std::string& message, unsigned int offset) AI_WONT_RETURN_SUFFIX;
  108. AI_WONT_RETURN void TokenizeError(const std::string& message, unsigned int offset)
  109. {
  110. throw DeadlyImportError(Util::AddOffset("FBX-Tokenize",message,offset));
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. uint32_t Offset(const char* begin, const char* cursor)
  114. {
  115. ai_assert(begin <= cursor);
  116. return static_cast<unsigned int>(cursor - begin);
  117. }
  118. // ------------------------------------------------------------------------------------------------
  119. void TokenizeError(const std::string& message, const char* begin, const char* cursor)
  120. {
  121. TokenizeError(message, Offset(begin, cursor));
  122. }
  123. // ------------------------------------------------------------------------------------------------
  124. uint32_t ReadWord(const char* input, const char*& cursor, const char* end)
  125. {
  126. const size_t k_to_read = sizeof( uint32_t );
  127. if(Offset(cursor, end) < k_to_read ) {
  128. TokenizeError("cannot ReadWord, out of bounds",input, cursor);
  129. }
  130. uint32_t word = *reinterpret_cast<const uint32_t*>(cursor);
  131. AI_SWAP4(word);
  132. cursor += k_to_read;
  133. return word;
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. uint64_t ReadDoubleWord(const char* input, const char*& cursor, const char* end)
  137. {
  138. const size_t k_to_read = sizeof(uint64_t);
  139. if(Offset(cursor, end) < k_to_read) {
  140. TokenizeError("cannot ReadDoubleWord, out of bounds",input, cursor);
  141. }
  142. uint64_t dword = *reinterpret_cast<const uint64_t*>(cursor);
  143. AI_SWAP8(dword);
  144. cursor += k_to_read;
  145. return dword;
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. uint8_t ReadByte(const char* input, const char*& cursor, const char* end)
  149. {
  150. if(Offset(cursor, end) < sizeof( uint8_t ) ) {
  151. TokenizeError("cannot ReadByte, out of bounds",input, cursor);
  152. }
  153. uint8_t word = *reinterpret_cast<const uint8_t*>(cursor);
  154. ++cursor;
  155. return word;
  156. }
  157. // ------------------------------------------------------------------------------------------------
  158. unsigned int ReadString(const char*& sbegin_out, const char*& send_out, const char* input, const char*& cursor, const char* end,
  159. bool long_length = false,
  160. bool allow_null = false)
  161. {
  162. const uint32_t len_len = long_length ? 4 : 1;
  163. if(Offset(cursor, end) < len_len) {
  164. TokenizeError("cannot ReadString, out of bounds reading length",input, cursor);
  165. }
  166. const uint32_t length = long_length ? ReadWord(input, cursor, end) : ReadByte(input, cursor, end);
  167. if (Offset(cursor, end) < length) {
  168. TokenizeError("cannot ReadString, length is out of bounds",input, cursor);
  169. }
  170. sbegin_out = cursor;
  171. cursor += length;
  172. send_out = cursor;
  173. if(!allow_null) {
  174. for (unsigned int i = 0; i < length; ++i) {
  175. if(sbegin_out[i] == '\0') {
  176. TokenizeError("failed ReadString, unexpected NUL character in string",input, cursor);
  177. }
  178. }
  179. }
  180. return length;
  181. }
  182. // ------------------------------------------------------------------------------------------------
  183. void ReadData(const char*& sbegin_out, const char*& send_out, const char* input, const char*& cursor, const char* end)
  184. {
  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. const uint32_t length = ReadWord(input, cursor, end);
  234. const uint32_t encoding = ReadWord(input, cursor, end);
  235. const uint32_t comp_len = ReadWord(input, cursor, end);
  236. // compute length based on type and check against the stored value
  237. if(encoding == 0) {
  238. uint32_t stride = 0;
  239. switch(type)
  240. {
  241. case 'f':
  242. case 'i':
  243. stride = 4;
  244. break;
  245. case 'd':
  246. case 'l':
  247. stride = 8;
  248. break;
  249. default:
  250. ai_assert(false);
  251. };
  252. ai_assert(stride > 0);
  253. if(length * stride != comp_len) {
  254. TokenizeError("cannot ReadData, calculated data stride differs from what the file claims",input, cursor);
  255. }
  256. }
  257. // zip/deflate algorithm (encoding==1)? take given length. anything else? die
  258. else if (encoding != 1) {
  259. TokenizeError("cannot ReadData, unknown encoding",input, cursor);
  260. }
  261. cursor += comp_len;
  262. break;
  263. }
  264. // string
  265. case 'S': {
  266. const char* sb, *se;
  267. // 0 characters can legally happen in such strings
  268. ReadString(sb, se, input, cursor, end, true, true);
  269. break;
  270. }
  271. default:
  272. TokenizeError("cannot ReadData, unexpected type code: " + std::string(&type, 1),input, cursor);
  273. }
  274. if(cursor > end) {
  275. TokenizeError("cannot ReadData, the remaining size is too small for the data type: " + std::string(&type, 1),input, cursor);
  276. }
  277. // the type code is contained in the returned range
  278. send_out = cursor;
  279. }
  280. // ------------------------------------------------------------------------------------------------
  281. bool ReadScope(TokenList& output_tokens, const char* input, const char*& cursor, const char* end, uint32_t const flags)
  282. {
  283. // the first word contains the offset at which this block ends
  284. const uint64_t end_offset = /*check_flag(flags, e_flag_field_size_64_bit) ? ReadDoubleWord(input, cursor, end) : */ReadWord(input, cursor, end);
  285. // we may get 0 if reading reached the end of the file -
  286. // fbx files have a mysterious extra footer which I don't know
  287. // how to extract any information from, but at least it always
  288. // starts with a 0.
  289. if(!end_offset) {
  290. return false;
  291. }
  292. if(end_offset > Offset(input, end)) {
  293. TokenizeError("block offset is out of range",input, cursor);
  294. }
  295. else if(end_offset < Offset(input, cursor)) {
  296. TokenizeError("block offset is negative out of range",input, cursor);
  297. }
  298. // the second data word contains the number of properties in the scope
  299. const uint64_t prop_count = /*check_flag(flags, e_flag_field_size_64_bit) ? ReadDoubleWord(input, cursor, end) : */ReadWord(input, cursor, end);
  300. // the third data word contains the length of the property list
  301. const uint64_t prop_length = /*check_flag(flags, e_flag_field_size_64_bit) ? ReadDoubleWord(input, cursor, end) :*/ ReadWord(input, cursor, end);
  302. // now comes the name of the scope/key
  303. const char* sbeg, *send;
  304. ReadString(sbeg, send, input, cursor, end);
  305. output_tokens.push_back(new_Token(sbeg, send, TokenType_KEY, Offset(input, cursor) ));
  306. // now come the individual properties
  307. const char* begin_cursor = cursor;
  308. for (unsigned int i = 0; i < prop_count; ++i) {
  309. ReadData(sbeg, send, input, cursor, begin_cursor + prop_length);
  310. output_tokens.push_back(new_Token(sbeg, send, TokenType_DATA, Offset(input, cursor) ));
  311. if(i != prop_count-1) {
  312. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_COMMA, Offset(input, cursor) ));
  313. }
  314. }
  315. if (Offset(begin_cursor, cursor) != prop_length) {
  316. TokenizeError("property length not reached, something is wrong",input, cursor);
  317. }
  318. // at the end of each nested block, there is a NUL record to indicate
  319. // that the sub-scope exists (i.e. to distinguish between P: and P : {})
  320. // this NUL record is 13 bytes long on 32 bit version and 25 bytes long on 64 bit.
  321. const size_t sentinel_block_length = /*check_flag(flags, e_flag_field_size_64_bit) ? (sizeof(uint64_t) * 3 + 1) : */(sizeof(uint32_t) * 3 + 1);
  322. if (Offset(input, cursor) < end_offset) {
  323. if (end_offset - Offset(input, cursor) < sentinel_block_length) {
  324. TokenizeError("insufficient padding bytes at block end",input, cursor);
  325. }
  326. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_OPEN_BRACKET, Offset(input, cursor) ));
  327. // XXX this is vulnerable to stack overflowing ..
  328. while(Offset(input, cursor) < end_offset - sentinel_block_length) {
  329. ReadScope(output_tokens, input, cursor, input + end_offset - sentinel_block_length, flags);
  330. }
  331. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_CLOSE_BRACKET, Offset(input, cursor) ));
  332. for (unsigned int i = 0; i < sentinel_block_length; ++i) {
  333. if(cursor[i] != '\0') {
  334. TokenizeError("failed to read nested block sentinel, expected all bytes to be 0",input, cursor);
  335. }
  336. }
  337. cursor += sentinel_block_length;
  338. }
  339. if (Offset(input, cursor) != end_offset) {
  340. TokenizeError("scope length not reached, something is wrong",input, cursor);
  341. }
  342. return true;
  343. }
  344. }
  345. // ------------------------------------------------------------------------------------------------
  346. void TokenizeBinary(TokenList& output_tokens, const char* input, unsigned int length)
  347. {
  348. ai_assert(input);
  349. if(length < 0x1b) {
  350. TokenizeError("file is too short",0);
  351. }
  352. if (strncmp(input,"Kaydara FBX Binary",18)) {
  353. TokenizeError("magic bytes not found",0);
  354. }
  355. //uint32_t offset = 0x15;
  356. const char* cursor = input + 0x15;
  357. const uint32_t flags = ReadWord(input, cursor, input + length);
  358. const uint8_t padding_0 = ReadByte(input, cursor, input + length); // unused
  359. const uint8_t padding_1 = ReadByte(input, cursor, input + length); // unused
  360. while (cursor < input + length)
  361. {
  362. if(!ReadScope(output_tokens, input, cursor, input + length, flags)) {
  363. break;
  364. }
  365. }
  366. }
  367. } // !FBX
  368. } // !Assimp
  369. #endif