utf8.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/infrastructure.html#decoded-as-utf-8,-with-error-handling
  32. // Adds a decoding error to the parser's error list, based on the current state
  33. // of the Utf8Iterator.
  34. static void add_error(Utf8Iterator* iter, GumboErrorType type) {
  35. GumboParser* parser = iter->_parser;
  36. GumboError* error = gumbo_add_error(parser);
  37. if (!error) {
  38. return;
  39. }
  40. error->type = type;
  41. error->position = iter->_pos;
  42. error->original_text = iter->_start;
  43. // At the point the error is recorded, the code point hasn't been computed
  44. // yet (and can't be, because it's invalid), so we need to build up the raw
  45. // hex value from the bytes under the cursor.
  46. uint64_t code_point = 0;
  47. for (int i = 0; i < iter->_width; ++i) {
  48. code_point = (code_point << 8) | (unsigned char) iter->_start[i];
  49. }
  50. error->v.codepoint = code_point;
  51. }
  52. // Reads the next UTF-8 character in the iter.
  53. // This assumes that iter->_start points to the beginning of the character.
  54. // When this method returns, iter->_width and iter->_current will be set
  55. // appropriately, as well as any error flags.
  56. static void read_char(Utf8Iterator* iter) {
  57. unsigned char c;
  58. unsigned char mask = '\0';
  59. int is_bad_char = false;
  60. c = (unsigned char) *iter->_start;
  61. if (c < 0x80) {
  62. // Valid one-byte sequence.
  63. iter->_width = 1;
  64. mask = 0xFF;
  65. } else if (c < 0xC0) {
  66. // Continuation character not following a multibyte sequence.
  67. // The HTML5 spec here says to consume the byte and output a replacement
  68. // character.
  69. iter->_width = 1;
  70. is_bad_char = true;
  71. } else if (c < 0xE0) {
  72. iter->_width = 2;
  73. mask = 0x1F; // 00011111 in binary.
  74. if (c < 0xC2) {
  75. // Overlong encoding; error according to UTF8/HTML5 spec.
  76. is_bad_char = true;
  77. }
  78. } else if (c < 0xF0) {
  79. iter->_width = 3;
  80. mask = 0xF; // 00001111 in binary.
  81. } else if (c < 0xF5) {
  82. iter->_width = 4;
  83. mask = 0x7; // 00000111 in binary.
  84. } else if (c < 0xF8) {
  85. // The following cases are all errors, but we need to handle them separately
  86. // so that we consume the proper number of bytes from the input stream
  87. // before replacing them with the replacement char. The HTML5 spec
  88. // specifies that we should consume the shorter of the length specified by
  89. // the first bit or the run leading up to the first non-continuation
  90. // character.
  91. iter->_width = 5;
  92. is_bad_char = true;
  93. } else if (c < 0xFC) {
  94. iter->_width = 6;
  95. is_bad_char = true;
  96. } else if (c < 0xFE) {
  97. iter->_width = 7;
  98. is_bad_char = true;
  99. } else {
  100. iter->_width = 1;
  101. is_bad_char = true;
  102. }
  103. // Check to make sure we have enough bytes left in the iter to read all that
  104. // we want. If not, we set the iter_truncated flag, mark this as a bad
  105. // character, and adjust the current width so that it consumes the rest of the
  106. // iter.
  107. uint64_t code_point = c & mask;
  108. if (iter->_start + iter->_width > iter->_end) {
  109. iter->_width = iter->_end - iter->_start;
  110. add_error(iter, GUMBO_ERR_UTF8_TRUNCATED);
  111. is_bad_char = true;
  112. }
  113. // Now we decode continuation bytes, shift them appropriately, and build up
  114. // the appropriate code point.
  115. assert(iter->_width < 8);
  116. for (int i = 1; i < iter->_width; ++i) {
  117. c = (unsigned char) iter->_start[i];
  118. if (c < 0x80 || c > 0xBF) {
  119. // Per HTML5 spec, we don't include the invalid continuation char in the
  120. // run that we consume here.
  121. iter->_width = i;
  122. is_bad_char = true;
  123. break;
  124. }
  125. code_point = (code_point << 6) | (c & ~0x80);
  126. }
  127. if (code_point > 0x10FFFF) is_bad_char = true;
  128. // If we had a decode error, set the current code point to the replacement
  129. // character and flip the flag indicating that a decode error occurred.
  130. // Ditto if we have a code point that is explicitly on the list of characters
  131. // prohibited by the HTML5 spec, such as control characters.
  132. if (is_bad_char || utf8_is_invalid_code_point(code_point)) {
  133. add_error(iter, GUMBO_ERR_UTF8_INVALID);
  134. code_point = kUtf8ReplacementChar;
  135. }
  136. // This is the special handling for carriage returns that is mandated by the
  137. // HTML5 spec. Since we're looking for particular 7-bit literal characters,
  138. // we operate in terms of chars and only need a check for iter overrun,
  139. // instead of having to read in a full next code point.
  140. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#preprocessing-the-input-stream
  141. if (code_point == '\r') {
  142. const char* next = iter->_start + iter->_width;
  143. if (next < iter->_end && *next == '\n') {
  144. // Advance the iter, as if the carriage return didn't exist.
  145. ++iter->_start;
  146. // Preserve the true offset, since other tools that look at it may be
  147. // unaware of HTML5's rules for converting \r into \n.
  148. ++iter->_pos.offset;
  149. }
  150. code_point = '\n';
  151. }
  152. // At this point, we know we have a valid character as the code point, so we
  153. // set it, and we're done.
  154. iter->_current = code_point;
  155. }
  156. static void update_position(Utf8Iterator* iter) {
  157. iter->_pos.offset += iter->_width;
  158. if (iter->_current == '\n') {
  159. ++iter->_pos.line;
  160. iter->_pos.column = 1;
  161. } else if(iter->_current == '\t') {
  162. int tab_stop = iter->_parser->_options->tab_stop;
  163. iter->_pos.column = ((iter->_pos.column / tab_stop) + 1) * tab_stop;
  164. } else {
  165. ++iter->_pos.column;
  166. }
  167. }
  168. // Returns true if this Unicode code point is in the list of characters
  169. // forbidden by the HTML5 spec, such as undefined control chars.
  170. bool utf8_is_invalid_code_point(int c) {
  171. return (c >= 0x1 && c <= 0x8) || c == 0xB || (c >= 0xE && c <= 0x1F) ||
  172. (c >= 0x7F && c <= 0x9F) || (c >= 0xFDD0 && c <= 0xFDEF) ||
  173. ((c & 0xFFFF) == 0xFFFE) || ((c & 0xFFFF) == 0xFFFF);
  174. }
  175. void utf8iterator_init(
  176. GumboParser* parser, const char* source, size_t source_length,
  177. Utf8Iterator* iter) {
  178. iter->_start = source;
  179. iter->_end = source + source_length;
  180. iter->_width = 0;
  181. iter->_pos.line = 1;
  182. iter->_pos.column = 1;
  183. iter->_pos.offset = 0;
  184. iter->_parser = parser;
  185. if (source_length) {
  186. read_char(iter);
  187. } else {
  188. iter->_current = -1;
  189. }
  190. }
  191. void utf8iterator_next(Utf8Iterator* iter) {
  192. iter->_start += iter->_width;
  193. // We update positions based on the *last* character read, so that the first
  194. // character following a newline is at column 1 in the next line.
  195. update_position(iter);
  196. if (iter->_start < iter->_end) {
  197. read_char(iter);
  198. } else { // EOF
  199. iter->_current = -1;
  200. }
  201. }
  202. int utf8iterator_current(const Utf8Iterator* iter) {
  203. return iter->_current;
  204. }
  205. void utf8iterator_get_position(
  206. const Utf8Iterator* iter, GumboSourcePosition* output) {
  207. *output = iter->_pos;
  208. }
  209. const char* utf8iterator_get_char_pointer(const Utf8Iterator* iter) {
  210. return iter->_start;
  211. }
  212. bool utf8iterator_maybe_consume_match(
  213. Utf8Iterator* iter, const char* prefix, size_t length,
  214. bool case_sensitive) {
  215. bool matched = (iter->_start + length <= iter->_end) && (case_sensitive ?
  216. !strncmp(iter->_start, prefix, length) :
  217. !strncasecmp(iter->_start, prefix, length));
  218. if (matched) {
  219. for (int i = 0; i < length; ++i) {
  220. utf8iterator_next(iter);
  221. }
  222. return true;
  223. } else {
  224. return false;
  225. }
  226. }
  227. void utf8iterator_mark(Utf8Iterator* iter) {
  228. iter->_mark = iter->_start;
  229. iter->_mark_pos = iter->_pos;
  230. }
  231. // Returns the current input stream position to the mark.
  232. void utf8iterator_reset(Utf8Iterator* iter) {
  233. iter->_start = iter->_mark;
  234. iter->_pos = iter->_mark_pos;
  235. read_char(iter);
  236. }
  237. // Sets the position and original text fields of an error to the value at the
  238. // mark.
  239. void utf8iterator_fill_error_at_mark(
  240. Utf8Iterator* iter, GumboError* error) {
  241. error->position = iter->_mark_pos;
  242. error->original_text = iter->_mark;
  243. }