You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

398 regels
11 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. #include "AssimpPCH.h"
  39. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  40. #include "FBXTokenizer.h"
  41. #include "FBXUtil.h"
  42. namespace Assimp {
  43. namespace FBX {
  44. // ------------------------------------------------------------------------------------------------
  45. Token::Token(const char* sbegin, const char* send, TokenType type, unsigned int offset)
  46. : sbegin(sbegin)
  47. , send(send)
  48. , type(type)
  49. , line(offset)
  50. , column(BINARY_MARKER)
  51. #ifdef DEBUG
  52. , contents(sbegin, static_cast<size_t>(send-sbegin))
  53. #endif
  54. {
  55. ai_assert(sbegin);
  56. ai_assert(send);
  57. // binary tokens may have zero length because they are sometimes dummies
  58. // inserted by TokenizeBinary()
  59. ai_assert(send >= sbegin);
  60. }
  61. namespace {
  62. // ------------------------------------------------------------------------------------------------
  63. // signal tokenization error, this is always unrecoverable. Throws DeadlyImportError.
  64. void TokenizeError(const std::string& message, unsigned int offset)
  65. {
  66. throw DeadlyImportError(Util::AddOffset("FBX-Tokenize",message,offset));
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. uint32_t Offset(const char* begin, const char* cursor)
  70. {
  71. ai_assert(begin <= cursor);
  72. return static_cast<unsigned int>(cursor - begin);
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. void TokenizeError(const std::string& message, const char* begin, const char* cursor)
  76. {
  77. TokenizeError(message, Offset(begin, cursor));
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. uint32_t ReadWord(const char* input, const char*& cursor, const char* end)
  81. {
  82. if(Offset(cursor, end) < 4) {
  83. TokenizeError("cannot ReadWord, out of bounds",input, cursor);
  84. }
  85. uint32_t word = *reinterpret_cast<const uint32_t*>(cursor);
  86. AI_SWAP4(word);
  87. cursor += 4;
  88. return word;
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. uint8_t ReadByte(const char* input, const char*& cursor, const char* end)
  92. {
  93. if(Offset(cursor, end) < 1) {
  94. TokenizeError("cannot ReadByte, out of bounds",input, cursor);
  95. }
  96. uint8_t word = *reinterpret_cast<const uint8_t*>(cursor);
  97. ++cursor;
  98. return word;
  99. }
  100. // ------------------------------------------------------------------------------------------------
  101. unsigned int ReadString(const char*& sbegin_out, const char*& send_out, const char* input, const char*& cursor, const char* end,
  102. bool long_length = false,
  103. bool allow_null = false)
  104. {
  105. const uint32_t len_len = long_length ? 4 : 1;
  106. if(Offset(cursor, end) < len_len) {
  107. TokenizeError("cannot ReadString, out of bounds reading length",input, cursor);
  108. }
  109. const uint32_t length = long_length ? ReadWord(input, cursor, end) : ReadByte(input, cursor, end);
  110. if (Offset(cursor, end) < length) {
  111. TokenizeError("cannot ReadString, length is out of bounds",input, cursor);
  112. }
  113. sbegin_out = cursor;
  114. cursor += length;
  115. send_out = cursor;
  116. if(!allow_null) {
  117. for (unsigned int i = 0; i < length; ++i) {
  118. if(sbegin_out[i] == '\0') {
  119. TokenizeError("failed ReadString, unexpected NUL character in string",input, cursor);
  120. }
  121. }
  122. }
  123. return length;
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. void ReadData(const char*& sbegin_out, const char*& send_out, const char* input, const char*& cursor, const char* end)
  127. {
  128. if(Offset(cursor, end) < 1) {
  129. TokenizeError("cannot ReadData, out of bounds reading length",input, cursor);
  130. }
  131. const char type = *cursor;
  132. sbegin_out = cursor++;
  133. switch(type)
  134. {
  135. // 16 bit int
  136. case 'Y':
  137. cursor += 2;
  138. break;
  139. // 1 bit bool flag (yes/no)
  140. case 'C':
  141. cursor += 1;
  142. break;
  143. // 32 bit int
  144. case 'I':
  145. // <- fall thru
  146. // float
  147. case 'F':
  148. cursor += 4;
  149. break;
  150. // double
  151. case 'D':
  152. cursor += 8;
  153. break;
  154. // 64 bit int
  155. case 'L':
  156. cursor += 8;
  157. break;
  158. // note: do not write cursor += ReadWord(...cursor) as this would be UB
  159. // raw binary data
  160. case 'R':
  161. {
  162. const uint32_t length = ReadWord(input, cursor, end);
  163. cursor += length;
  164. break;
  165. }
  166. case 'b':
  167. // TODO: what is the 'b' type code? Right now we just skip over it /
  168. // take the full range we could get
  169. cursor = end;
  170. break;
  171. // array of *
  172. case 'f':
  173. case 'd':
  174. case 'l':
  175. case 'i': {
  176. const uint32_t length = ReadWord(input, cursor, end);
  177. const uint32_t encoding = ReadWord(input, cursor, end);
  178. const uint32_t comp_len = ReadWord(input, cursor, end);
  179. // compute length based on type and check against the stored value
  180. if(encoding == 0) {
  181. uint32_t stride = 0;
  182. switch(type)
  183. {
  184. case 'f':
  185. case 'i':
  186. stride = 4;
  187. break;
  188. case 'd':
  189. case 'l':
  190. stride = 8;
  191. break;
  192. default:
  193. ai_assert(false);
  194. };
  195. ai_assert(stride > 0);
  196. if(length * stride != comp_len) {
  197. TokenizeError("cannot ReadData, calculated data stride differs from what the file claims",input, cursor);
  198. }
  199. }
  200. // zip/deflate algorithm (encoding==1)? take given length. anything else? die
  201. else if (encoding != 1) {
  202. TokenizeError("cannot ReadData, unknown encoding",input, cursor);
  203. }
  204. cursor += comp_len;
  205. break;
  206. }
  207. // string
  208. case 'S': {
  209. const char* sb, *se;
  210. // 0 characters can legally happen in such strings
  211. ReadString(sb, se, input, cursor, end, true, true);
  212. break;
  213. }
  214. default:
  215. TokenizeError("cannot ReadData, unexpected type code: " + std::string(&type, 1),input, cursor);
  216. }
  217. if(cursor > end) {
  218. TokenizeError("cannot ReadData, the remaining size is too small for the data type: " + std::string(&type, 1),input, cursor);
  219. }
  220. // the type code is contained in the returned range
  221. send_out = cursor;
  222. }
  223. // ------------------------------------------------------------------------------------------------
  224. bool ReadScope(TokenList& output_tokens, const char* input, const char*& cursor, const char* end)
  225. {
  226. // the first word contains the offset at which this block ends
  227. const uint32_t end_offset = ReadWord(input, cursor, end);
  228. // we may get 0 if reading reached the end of the file -
  229. // fbx files have a mysterious extra footer which I don't know
  230. // how to extract any information from, but at least it always
  231. // starts with a 0.
  232. if(!end_offset) {
  233. return false;
  234. }
  235. if(end_offset > Offset(input, end)) {
  236. TokenizeError("block offset is out of range",input, cursor);
  237. }
  238. else if(end_offset < Offset(input, cursor)) {
  239. TokenizeError("block offset is negative out of range",input, cursor);
  240. }
  241. // the second data word contains the number of properties in the scope
  242. const uint32_t prop_count = ReadWord(input, cursor, end);
  243. // the third data word contains the length of the property list
  244. const uint32_t prop_length = ReadWord(input, cursor, end);
  245. // now comes the name of the scope/key
  246. const char* sbeg, *send;
  247. ReadString(sbeg, send, input, cursor, end);
  248. output_tokens.push_back(new_Token(sbeg, send, TokenType_KEY, Offset(input, cursor) ));
  249. // now come the individual properties
  250. const char* begin_cursor = cursor;
  251. for (unsigned int i = 0; i < prop_count; ++i) {
  252. ReadData(sbeg, send, input, cursor, begin_cursor + prop_length);
  253. output_tokens.push_back(new_Token(sbeg, send, TokenType_DATA, Offset(input, cursor) ));
  254. if(i != prop_count-1) {
  255. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_COMMA, Offset(input, cursor) ));
  256. }
  257. }
  258. if (Offset(begin_cursor, cursor) != prop_length) {
  259. TokenizeError("property length not reached, something is wrong",input, cursor);
  260. }
  261. // at the end of each nested block, there is a NUL record to indicate
  262. // that the sub-scope exists (i.e. to distinguish between P: and P : {})
  263. // this NUL record is 13 bytes long.
  264. #define BLOCK_SENTINEL_LENGTH 13
  265. if (Offset(input, cursor) < end_offset) {
  266. if (end_offset - Offset(input, cursor) < BLOCK_SENTINEL_LENGTH) {
  267. TokenizeError("insufficient padding bytes at block end",input, cursor);
  268. }
  269. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_OPEN_BRACKET, Offset(input, cursor) ));
  270. // XXX this is vulnerable to stack overflowing ..
  271. while(Offset(input, cursor) < end_offset - BLOCK_SENTINEL_LENGTH) {
  272. ReadScope(output_tokens, input, cursor, input + end_offset - BLOCK_SENTINEL_LENGTH);
  273. }
  274. output_tokens.push_back(new_Token(cursor, cursor + 1, TokenType_CLOSE_BRACKET, Offset(input, cursor) ));
  275. for (unsigned int i = 0; i < BLOCK_SENTINEL_LENGTH; ++i) {
  276. if(cursor[i] != '\0') {
  277. TokenizeError("failed to read nested block sentinel, expected all bytes to be 0",input, cursor);
  278. }
  279. }
  280. cursor += BLOCK_SENTINEL_LENGTH;
  281. }
  282. if (Offset(input, cursor) != end_offset) {
  283. TokenizeError("scope length not reached, something is wrong",input, cursor);
  284. }
  285. return true;
  286. }
  287. }
  288. // ------------------------------------------------------------------------------------------------
  289. void TokenizeBinary(TokenList& output_tokens, const char* input, unsigned int length)
  290. {
  291. ai_assert(input);
  292. if(length < 0x1b) {
  293. TokenizeError("file is too short",0);
  294. }
  295. if (strncmp(input,"Kaydara FBX Binary",18)) {
  296. TokenizeError("magic bytes not found",0);
  297. }
  298. //uint32_t offset = 0x1b;
  299. const char* cursor = input + 0x1b;
  300. while (cursor < input + length) {
  301. if(!ReadScope(output_tokens, input, cursor, input + length)) {
  302. break;
  303. }
  304. }
  305. }
  306. } // !FBX
  307. } // !Assimp
  308. #endif