utf8.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: [email protected] (Jonathan Tang)
  16. #include "utf8.h"
  17. #include <assert.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <strings.h> // For strncasecmp.
  21. #include "error.h"
  22. #include "gumbo.h"
  23. #include "parser.h"
  24. #include "util.h"
  25. #include "vector.h"
  26. const int kUtf8ReplacementChar = 0xFFFD;
  27. // Reference material:
  28. // Wikipedia: http://en.wikipedia.org/wiki/UTF-8#Description
  29. // RFC 3629: http://tools.ietf.org/html/rfc3629
  30. // HTML5 Unicode handling:
  31. // http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#preprocessing-the-input-stream
  32. //
  33. // This implementation is based on a DFA-based decoder by Bjoern Hoehrmann
  34. // <[email protected]>. We wrap the inner table-based decoder routine in our
  35. // own handling for newlines, tabs, invalid continuation bytes, and other
  36. // conditions that the HTML5 spec fully specifies but normal UTF8 decoders do
  37. // not handle.
  38. // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. Full text of
  39. // the license agreement and code follows.
  40. // Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
  41. // Permission is hereby granted, free of charge, to any person obtaining a copy
  42. // of this software and associated documentation files (the "Software"), to deal
  43. // in the Software without restriction, including without limitation the rights
  44. // to
  45. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  46. // of the Software, and to permit persons to whom the Software is furnished to
  47. // do
  48. // so, subject to the following conditions:
  49. // The above copyright notice and this permission notice shall be included in
  50. // all copies or substantial portions of the Software.
  51. #define UTF8_ACCEPT 0
  52. #define UTF8_REJECT 12
  53. static const uint8_t utf8d[] = {
  54. // The first part of the table maps bytes to character classes that
  55. // to reduce the size of the transition table and create bitmasks.
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9,
  62. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  63. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 2, 2, 2, 2, 2, 2,
  64. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 10,
  65. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 11, 6, 6, 6, 5, 8, 8, 8, 8, 8,
  66. 8, 8, 8, 8, 8, 8,
  67. // The second part is a transition table that maps a combination
  68. // of a state of the automaton and a character class to a state.
  69. 0, 12, 24, 36, 60, 96, 84, 12, 12, 12, 48, 72, 12, 12, 12, 12, 12, 12, 12,
  70. 12, 12, 12, 12, 12, 12, 0, 12, 12, 12, 12, 12, 0, 12, 0, 12, 12, 12, 24, 12,
  71. 12, 12, 12, 12, 24, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12,
  72. 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12,
  73. 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12,
  74. 12, 36, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  75. };
  76. uint32_t static inline decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
  77. uint32_t type = utf8d[byte];
  78. *codep = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*codep << 6)
  79. : (0xff >> type) & (byte);
  80. *state = utf8d[256 + *state + type];
  81. return *state;
  82. }
  83. // END COPIED CODE.
  84. // Adds a decoding error to the parser's error list, based on the current state
  85. // of the Utf8Iterator.
  86. static void add_error(Utf8Iterator* iter, GumboErrorType type) {
  87. GumboParser* parser = iter->_parser;
  88. GumboError* error = gumbo_add_error(parser);
  89. if (!error) {
  90. return;
  91. }
  92. error->type = type;
  93. error->position = iter->_pos;
  94. error->original_text = iter->_start;
  95. // At the point the error is recorded, the code point hasn't been computed
  96. // yet (and can't be, because it's invalid), so we need to build up the raw
  97. // hex value from the bytes under the cursor.
  98. uint64_t code_point = 0;
  99. for (int i = 0; i < iter->_width; ++i) {
  100. code_point = (code_point << 8) | (unsigned char) iter->_start[i];
  101. }
  102. error->v.codepoint = code_point;
  103. }
  104. // Reads the next UTF-8 character in the iter.
  105. // This assumes that iter->_start points to the beginning of the character.
  106. // When this method returns, iter->_width and iter->_current will be set
  107. // appropriately, as well as any error flags.
  108. static void read_char(Utf8Iterator* iter) {
  109. if (iter->_start >= iter->_end) {
  110. // No input left to consume; emit an EOF and set width = 0.
  111. iter->_current = -1;
  112. iter->_width = 0;
  113. return;
  114. }
  115. uint32_t code_point = 0;
  116. uint32_t state = UTF8_ACCEPT;
  117. for (const char* c = iter->_start; c < iter->_end; ++c) {
  118. decode(&state, &code_point, (uint32_t)(unsigned char) (*c));
  119. if (state == UTF8_ACCEPT) {
  120. iter->_width = c - iter->_start + 1;
  121. // This is the special handling for carriage returns that is mandated by
  122. // the HTML5 spec. Since we're looking for particular 7-bit literal
  123. // characters, we operate in terms of chars and only need a check for iter
  124. // overrun, instead of having to read in a full next code point.
  125. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#preprocessing-the-input-stream
  126. if (code_point == '\r') {
  127. assert(iter->_width == 1);
  128. const char* next = c + 1;
  129. if (next < iter->_end && *next == '\n') {
  130. // Advance the iter, as if the carriage return didn't exist.
  131. ++iter->_start;
  132. // Preserve the true offset, since other tools that look at it may be
  133. // unaware of HTML5's rules for converting \r into \n.
  134. ++iter->_pos.offset;
  135. }
  136. code_point = '\n';
  137. }
  138. if (utf8_is_invalid_code_point(code_point)) {
  139. add_error(iter, GUMBO_ERR_UTF8_INVALID);
  140. code_point = kUtf8ReplacementChar;
  141. }
  142. iter->_current = code_point;
  143. return;
  144. } else if (state == UTF8_REJECT) {
  145. // We don't want to consume the invalid continuation byte of a multi-byte
  146. // run, but we do want to skip past an invalid first byte.
  147. iter->_width = c - iter->_start + (c == iter->_start);
  148. iter->_current = kUtf8ReplacementChar;
  149. add_error(iter, GUMBO_ERR_UTF8_INVALID);
  150. return;
  151. }
  152. }
  153. // If we got here without exiting early, then we've reached the end of the
  154. // iterator. Add an error for truncated input, set the width to consume the
  155. // rest of the iterator, and emit a replacement character. The next time we
  156. // enter this method, it will detect that there's no input to consume and
  157. // output an EOF.
  158. iter->_current = kUtf8ReplacementChar;
  159. iter->_width = iter->_end - iter->_start;
  160. add_error(iter, GUMBO_ERR_UTF8_TRUNCATED);
  161. }
  162. static void update_position(Utf8Iterator* iter) {
  163. iter->_pos.offset += iter->_width;
  164. if (iter->_current == '\n') {
  165. ++iter->_pos.line;
  166. iter->_pos.column = 1;
  167. } else if (iter->_current == '\t') {
  168. int tab_stop = iter->_parser->_options->tab_stop;
  169. iter->_pos.column = ((iter->_pos.column / tab_stop) + 1) * tab_stop;
  170. } else if (iter->_current != -1) {
  171. ++iter->_pos.column;
  172. }
  173. }
  174. // Returns true if this Unicode code point is in the list of characters
  175. // forbidden by the HTML5 spec, such as undefined control chars.
  176. bool utf8_is_invalid_code_point(int c) {
  177. return (c >= 0x1 && c <= 0x8) || c == 0xB || (c >= 0xE && c <= 0x1F) ||
  178. (c >= 0x7F && c <= 0x9F) || (c >= 0xFDD0 && c <= 0xFDEF) ||
  179. ((c & 0xFFFF) == 0xFFFE) || ((c & 0xFFFF) == 0xFFFF);
  180. }
  181. void utf8iterator_init(GumboParser* parser, const char* source,
  182. size_t source_length, Utf8Iterator* iter) {
  183. iter->_start = source;
  184. iter->_end = source + source_length;
  185. iter->_pos.line = 1;
  186. iter->_pos.column = 1;
  187. iter->_pos.offset = 0;
  188. iter->_parser = parser;
  189. read_char(iter);
  190. }
  191. void utf8iterator_next(Utf8Iterator* iter) {
  192. // We update positions based on the *last* character read, so that the first
  193. // character following a newline is at column 1 in the next line.
  194. update_position(iter);
  195. iter->_start += iter->_width;
  196. read_char(iter);
  197. }
  198. int utf8iterator_current(const Utf8Iterator* iter) { return iter->_current; }
  199. void utf8iterator_get_position(
  200. const Utf8Iterator* iter, GumboSourcePosition* output) {
  201. *output = iter->_pos;
  202. }
  203. const char* utf8iterator_get_char_pointer(const Utf8Iterator* iter) {
  204. return iter->_start;
  205. }
  206. const char* utf8iterator_get_end_pointer(const Utf8Iterator* iter) {
  207. return iter->_end;
  208. }
  209. bool utf8iterator_maybe_consume_match(Utf8Iterator* iter, const char* prefix,
  210. size_t length, bool case_sensitive) {
  211. bool matched = (iter->_start + length <= iter->_end) &&
  212. (case_sensitive ? !strncmp(iter->_start, prefix, length)
  213. : !strncasecmp(iter->_start, prefix, length));
  214. if (matched) {
  215. for (unsigned int i = 0; i < length; ++i) {
  216. utf8iterator_next(iter);
  217. }
  218. return true;
  219. } else {
  220. return false;
  221. }
  222. }
  223. void utf8iterator_mark(Utf8Iterator* iter) {
  224. iter->_mark = iter->_start;
  225. iter->_mark_pos = iter->_pos;
  226. }
  227. // Returns the current input stream position to the mark.
  228. void utf8iterator_reset(Utf8Iterator* iter) {
  229. iter->_start = iter->_mark;
  230. iter->_pos = iter->_mark_pos;
  231. read_char(iter);
  232. }
  233. // Sets the position and original text fields of an error to the value at the
  234. // mark.
  235. void utf8iterator_fill_error_at_mark(Utf8Iterator* iter, GumboError* error) {
  236. error->position = iter->_mark_pos;
  237. error->original_text = iter->_mark;
  238. }