FBXTokenizer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**************************************************************************/
  2. /* FBXTokenizer.cpp */
  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-2019, 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 FBXTokenizer.cpp
  63. * @brief Implementation of the FBX broadphase lexer
  64. */
  65. // tab width for logging columns
  66. #define ASSIMP_FBX_TAB_WIDTH 4
  67. #include "FBXTokenizer.h"
  68. #include "core/print_string.h"
  69. namespace FBXDocParser {
  70. // ------------------------------------------------------------------------------------------------
  71. Token::Token(const char *p_sbegin, const char *p_send, TokenType p_type, unsigned int p_line, unsigned int p_column) :
  72. sbegin(p_sbegin),
  73. send(p_send),
  74. type(p_type),
  75. line(p_line),
  76. column(p_column) {
  77. #ifdef DEBUG_ENABLED
  78. contents = std::string(sbegin, static_cast<size_t>(send - sbegin));
  79. #endif
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. Token::~Token() {
  83. }
  84. namespace {
  85. // ------------------------------------------------------------------------------------------------
  86. void TokenizeError(const std::string &message, unsigned int line, unsigned int column) {
  87. print_error("[FBX-Tokenize]" + String(message.c_str()) + " " + itos(line) + ":" + itos(column));
  88. }
  89. // process a potential data token up to 'cur', adding it to 'output_tokens'.
  90. // ------------------------------------------------------------------------------------------------
  91. void ProcessDataToken(TokenList &output_tokens, const char *&start, const char *&end,
  92. unsigned int line,
  93. unsigned int column,
  94. TokenType type = TokenType_DATA,
  95. bool must_have_token = false) {
  96. if (start && end) {
  97. // sanity check:
  98. // tokens should have no whitespace outside quoted text and [start,end] should
  99. // properly delimit the valid range.
  100. bool in_double_quotes = false;
  101. for (const char *c = start; c != end + 1; ++c) {
  102. if (*c == '\"') {
  103. in_double_quotes = !in_double_quotes;
  104. }
  105. if (!in_double_quotes && IsSpaceOrNewLine(*c)) {
  106. TokenizeError("unexpected whitespace in token", line, column);
  107. }
  108. }
  109. if (in_double_quotes) {
  110. TokenizeError("non-terminated double quotes", line, column);
  111. }
  112. output_tokens.push_back(new_Token(start, end + 1, type, line, column));
  113. } else if (must_have_token) {
  114. TokenizeError("unexpected character, expected data token", line, column);
  115. }
  116. start = end = nullptr;
  117. }
  118. } // namespace
  119. // ------------------------------------------------------------------------------------------------
  120. void Tokenize(TokenList &output_tokens, const char *input, size_t length) {
  121. // line and column numbers numbers are one-based
  122. unsigned int line = 1;
  123. unsigned int column = 1;
  124. bool comment = false;
  125. bool in_double_quotes = false;
  126. bool pending_data_token = false;
  127. const char *token_begin = nullptr, *token_end = nullptr;
  128. // input (starting string), *cur the current string, column +=
  129. // modified to fix strlen() and stop buffer overflow
  130. for (size_t x = 0; x < length; x++) {
  131. const char c = input[x];
  132. const char *cur = &input[x];
  133. column += (c == '\t' ? ASSIMP_FBX_TAB_WIDTH : 1);
  134. if (IsLineEnd(c)) {
  135. comment = false;
  136. column = 0;
  137. ++line;
  138. }
  139. if (comment) {
  140. continue;
  141. }
  142. if (in_double_quotes) {
  143. if (c == '\"') {
  144. in_double_quotes = false;
  145. token_end = cur;
  146. ProcessDataToken(output_tokens, token_begin, token_end, line, column);
  147. pending_data_token = false;
  148. }
  149. continue;
  150. }
  151. switch (c) {
  152. case '\"':
  153. if (token_begin) {
  154. TokenizeError("unexpected double-quote", line, column);
  155. }
  156. token_begin = cur;
  157. in_double_quotes = true;
  158. continue;
  159. case ';':
  160. ProcessDataToken(output_tokens, token_begin, token_end, line, column);
  161. comment = true;
  162. continue;
  163. case '{':
  164. ProcessDataToken(output_tokens, token_begin, token_end, line, column);
  165. output_tokens.push_back(new_Token(cur, cur + 1, TokenType_OPEN_BRACKET, line, column));
  166. continue;
  167. case '}':
  168. ProcessDataToken(output_tokens, token_begin, token_end, line, column);
  169. output_tokens.push_back(new_Token(cur, cur + 1, TokenType_CLOSE_BRACKET, line, column));
  170. continue;
  171. case ',':
  172. if (pending_data_token) {
  173. ProcessDataToken(output_tokens, token_begin, token_end, line, column, TokenType_DATA, true);
  174. }
  175. output_tokens.push_back(new_Token(cur, cur + 1, TokenType_COMMA, line, column));
  176. continue;
  177. case ':':
  178. if (pending_data_token) {
  179. ProcessDataToken(output_tokens, token_begin, token_end, line, column, TokenType_KEY, true);
  180. } else {
  181. TokenizeError("unexpected colon", line, column);
  182. }
  183. continue;
  184. }
  185. if (IsSpaceOrNewLine(c)) {
  186. if (token_begin) {
  187. // peek ahead and check if the next token is a colon in which
  188. // case this counts as KEY token.
  189. TokenType type = TokenType_DATA;
  190. for (const char *peek = cur; *peek && IsSpaceOrNewLine(*peek); ++peek) {
  191. if (*peek == ':') {
  192. type = TokenType_KEY;
  193. cur = peek;
  194. break;
  195. }
  196. }
  197. ProcessDataToken(output_tokens, token_begin, token_end, line, column, type);
  198. }
  199. pending_data_token = false;
  200. } else {
  201. token_end = cur;
  202. if (!token_begin) {
  203. token_begin = cur;
  204. }
  205. pending_data_token = true;
  206. }
  207. }
  208. }
  209. } // namespace FBXDocParser