pcre_valid_utf8.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2013 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains an internal function for validating UTF-8 character
  33. strings. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "pcre_config.h"
  36. #endif
  37. #include "pcre_internal.h"
  38. /*************************************************
  39. * Validate a UTF-8 string *
  40. *************************************************/
  41. /* This function is called (optionally) at the start of compile or match, to
  42. check that a supposed UTF-8 string is actually valid. The early check means
  43. that subsequent code can assume it is dealing with a valid string. The check
  44. can be turned off for maximum performance, but the consequences of supplying an
  45. invalid string are then undefined.
  46. Originally, this function checked according to RFC 2279, allowing for values in
  47. the range 0 to 0x7fffffff, up to 6 bytes long, but ensuring that they were in
  48. the canonical format. Once somebody had pointed out RFC 3629 to me (it
  49. obsoletes 2279), additional restrictions were applied. The values are now
  50. limited to be between 0 and 0x0010ffff, no more than 4 bytes long, and the
  51. subrange 0xd000 to 0xdfff is excluded. However, the format of 5-byte and 6-byte
  52. characters is still checked.
  53. From release 8.13 more information about the details of the error are passed
  54. back in the returned value:
  55. PCRE_UTF8_ERR0 No error
  56. PCRE_UTF8_ERR1 Missing 1 byte at the end of the string
  57. PCRE_UTF8_ERR2 Missing 2 bytes at the end of the string
  58. PCRE_UTF8_ERR3 Missing 3 bytes at the end of the string
  59. PCRE_UTF8_ERR4 Missing 4 bytes at the end of the string
  60. PCRE_UTF8_ERR5 Missing 5 bytes at the end of the string
  61. PCRE_UTF8_ERR6 2nd-byte's two top bits are not 0x80
  62. PCRE_UTF8_ERR7 3rd-byte's two top bits are not 0x80
  63. PCRE_UTF8_ERR8 4th-byte's two top bits are not 0x80
  64. PCRE_UTF8_ERR9 5th-byte's two top bits are not 0x80
  65. PCRE_UTF8_ERR10 6th-byte's two top bits are not 0x80
  66. PCRE_UTF8_ERR11 5-byte character is not permitted by RFC 3629
  67. PCRE_UTF8_ERR12 6-byte character is not permitted by RFC 3629
  68. PCRE_UTF8_ERR13 4-byte character with value > 0x10ffff is not permitted
  69. PCRE_UTF8_ERR14 3-byte character with value 0xd000-0xdfff is not permitted
  70. PCRE_UTF8_ERR15 Overlong 2-byte sequence
  71. PCRE_UTF8_ERR16 Overlong 3-byte sequence
  72. PCRE_UTF8_ERR17 Overlong 4-byte sequence
  73. PCRE_UTF8_ERR18 Overlong 5-byte sequence (won't ever occur)
  74. PCRE_UTF8_ERR19 Overlong 6-byte sequence (won't ever occur)
  75. PCRE_UTF8_ERR20 Isolated 0x80 byte (not within UTF-8 character)
  76. PCRE_UTF8_ERR21 Byte with the illegal value 0xfe or 0xff
  77. PCRE_UTF8_ERR22 Unused (was non-character)
  78. Arguments:
  79. string points to the string
  80. length length of string, or -1 if the string is zero-terminated
  81. errp pointer to an error position offset variable
  82. Returns: = 0 if the string is a valid UTF-8 string
  83. > 0 otherwise, setting the offset of the bad character
  84. */
  85. int
  86. PRIV(valid_utf)(PCRE_PUCHAR string, int length, int *erroroffset)
  87. {
  88. #ifdef SUPPORT_UTF
  89. register PCRE_PUCHAR p;
  90. if (length < 0)
  91. {
  92. for (p = string; *p != 0; p++);
  93. length = (int)(p - string);
  94. }
  95. for (p = string; length-- > 0; p++)
  96. {
  97. register pcre_uchar ab, c, d;
  98. c = *p;
  99. if (c < 128) continue; /* ASCII character */
  100. if (c < 0xc0) /* Isolated 10xx xxxx byte */
  101. {
  102. *erroroffset = (int)(p - string);
  103. return PCRE_UTF8_ERR20;
  104. }
  105. if (c >= 0xfe) /* Invalid 0xfe or 0xff bytes */
  106. {
  107. *erroroffset = (int)(p - string);
  108. return PCRE_UTF8_ERR21;
  109. }
  110. ab = PRIV(utf8_table4)[c & 0x3f]; /* Number of additional bytes */
  111. if (length < ab)
  112. {
  113. *erroroffset = (int)(p - string); /* Missing bytes */
  114. return ab - length; /* Codes ERR1 to ERR5 */
  115. }
  116. length -= ab; /* Length remaining */
  117. /* Check top bits in the second byte */
  118. if (((d = *(++p)) & 0xc0) != 0x80)
  119. {
  120. *erroroffset = (int)(p - string) - 1;
  121. return PCRE_UTF8_ERR6;
  122. }
  123. /* For each length, check that the remaining bytes start with the 0x80 bit
  124. set and not the 0x40 bit. Then check for an overlong sequence, and for the
  125. excluded range 0xd800 to 0xdfff. */
  126. switch (ab)
  127. {
  128. /* 2-byte character. No further bytes to check for 0x80. Check first byte
  129. for for xx00 000x (overlong sequence). */
  130. case 1: if ((c & 0x3e) == 0)
  131. {
  132. *erroroffset = (int)(p - string) - 1;
  133. return PCRE_UTF8_ERR15;
  134. }
  135. break;
  136. /* 3-byte character. Check third byte for 0x80. Then check first 2 bytes
  137. for 1110 0000, xx0x xxxx (overlong sequence) or
  138. 1110 1101, 1010 xxxx (0xd800 - 0xdfff) */
  139. case 2:
  140. if ((*(++p) & 0xc0) != 0x80) /* Third byte */
  141. {
  142. *erroroffset = (int)(p - string) - 2;
  143. return PCRE_UTF8_ERR7;
  144. }
  145. if (c == 0xe0 && (d & 0x20) == 0)
  146. {
  147. *erroroffset = (int)(p - string) - 2;
  148. return PCRE_UTF8_ERR16;
  149. }
  150. if (c == 0xed && d >= 0xa0)
  151. {
  152. *erroroffset = (int)(p - string) - 2;
  153. return PCRE_UTF8_ERR14;
  154. }
  155. break;
  156. /* 4-byte character. Check 3rd and 4th bytes for 0x80. Then check first 2
  157. bytes for for 1111 0000, xx00 xxxx (overlong sequence), then check for a
  158. character greater than 0x0010ffff (f4 8f bf bf) */
  159. case 3:
  160. if ((*(++p) & 0xc0) != 0x80) /* Third byte */
  161. {
  162. *erroroffset = (int)(p - string) - 2;
  163. return PCRE_UTF8_ERR7;
  164. }
  165. if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */
  166. {
  167. *erroroffset = (int)(p - string) - 3;
  168. return PCRE_UTF8_ERR8;
  169. }
  170. if (c == 0xf0 && (d & 0x30) == 0)
  171. {
  172. *erroroffset = (int)(p - string) - 3;
  173. return PCRE_UTF8_ERR17;
  174. }
  175. if (c > 0xf4 || (c == 0xf4 && d > 0x8f))
  176. {
  177. *erroroffset = (int)(p - string) - 3;
  178. return PCRE_UTF8_ERR13;
  179. }
  180. break;
  181. /* 5-byte and 6-byte characters are not allowed by RFC 3629, and will be
  182. rejected by the length test below. However, we do the appropriate tests
  183. here so that overlong sequences get diagnosed, and also in case there is
  184. ever an option for handling these larger code points. */
  185. /* 5-byte character. Check 3rd, 4th, and 5th bytes for 0x80. Then check for
  186. 1111 1000, xx00 0xxx */
  187. case 4:
  188. if ((*(++p) & 0xc0) != 0x80) /* Third byte */
  189. {
  190. *erroroffset = (int)(p - string) - 2;
  191. return PCRE_UTF8_ERR7;
  192. }
  193. if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */
  194. {
  195. *erroroffset = (int)(p - string) - 3;
  196. return PCRE_UTF8_ERR8;
  197. }
  198. if ((*(++p) & 0xc0) != 0x80) /* Fifth byte */
  199. {
  200. *erroroffset = (int)(p - string) - 4;
  201. return PCRE_UTF8_ERR9;
  202. }
  203. if (c == 0xf8 && (d & 0x38) == 0)
  204. {
  205. *erroroffset = (int)(p - string) - 4;
  206. return PCRE_UTF8_ERR18;
  207. }
  208. break;
  209. /* 6-byte character. Check 3rd-6th bytes for 0x80. Then check for
  210. 1111 1100, xx00 00xx. */
  211. case 5:
  212. if ((*(++p) & 0xc0) != 0x80) /* Third byte */
  213. {
  214. *erroroffset = (int)(p - string) - 2;
  215. return PCRE_UTF8_ERR7;
  216. }
  217. if ((*(++p) & 0xc0) != 0x80) /* Fourth byte */
  218. {
  219. *erroroffset = (int)(p - string) - 3;
  220. return PCRE_UTF8_ERR8;
  221. }
  222. if ((*(++p) & 0xc0) != 0x80) /* Fifth byte */
  223. {
  224. *erroroffset = (int)(p - string) - 4;
  225. return PCRE_UTF8_ERR9;
  226. }
  227. if ((*(++p) & 0xc0) != 0x80) /* Sixth byte */
  228. {
  229. *erroroffset = (int)(p - string) - 5;
  230. return PCRE_UTF8_ERR10;
  231. }
  232. if (c == 0xfc && (d & 0x3c) == 0)
  233. {
  234. *erroroffset = (int)(p - string) - 5;
  235. return PCRE_UTF8_ERR19;
  236. }
  237. break;
  238. }
  239. /* Character is valid under RFC 2279, but 4-byte and 5-byte characters are
  240. excluded by RFC 3629. The pointer p is currently at the last byte of the
  241. character. */
  242. if (ab > 3)
  243. {
  244. *erroroffset = (int)(p - string) - ab;
  245. return (ab == 4)? PCRE_UTF8_ERR11 : PCRE_UTF8_ERR12;
  246. }
  247. }
  248. #else /* Not SUPPORT_UTF */
  249. (void)(string); /* Keep picky compilers happy */
  250. (void)(length);
  251. (void)(erroroffset);
  252. #endif
  253. return PCRE_UTF8_ERR0; /* This indicates success */
  254. }
  255. /* End of pcre_valid_utf8.c */