/vendor/pcre/pcre_valid_utf8.c

http://github.com/feyeleanor/RubyGoLightly · C · 165 lines · 61 code · 27 blank · 77 comment · 39 complexity · 94fe6661c82f958f70c2b831c922b786 MD5 · raw file

  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-2008 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 "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. validate 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
  45. an 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.
  52. Arguments:
  53. string points to the string
  54. length length of string, or -1 if the string is zero-terminated
  55. Returns: < 0 if the string is a valid UTF-8 string
  56. >= 0 otherwise; the value is the offset of the bad byte
  57. */
  58. int
  59. _pcre_valid_utf8(const uschar *string, int length)
  60. {
  61. #ifdef SUPPORT_UTF8
  62. register const uschar *p;
  63. if (length < 0)
  64. {
  65. for (p = string; *p != 0; p++);
  66. length = p - string;
  67. }
  68. for (p = string; length-- > 0; p++)
  69. {
  70. register int ab;
  71. register int c = *p;
  72. if (c < 128) continue;
  73. if (c < 0xc0) return p - string;
  74. ab = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */
  75. if (length < ab || ab > 3) return p - string;
  76. length -= ab;
  77. /* Check top bits in the second byte */
  78. if ((*(++p) & 0xc0) != 0x80) return p - string;
  79. /* Check for overlong sequences for each different length, and for the
  80. excluded range 0xd000 to 0xdfff. */
  81. switch (ab)
  82. {
  83. /* Check for xx00 000x (overlong sequence) */
  84. case 1:
  85. if ((c & 0x3e) == 0) return p - string;
  86. continue; /* We know there aren't any more bytes to check */
  87. /* Check for 1110 0000, xx0x xxxx (overlong sequence) or
  88. 1110 1101, 1010 xxxx (0xd000 - 0xdfff) */
  89. case 2:
  90. if ((c == 0xe0 && (*p & 0x20) == 0) ||
  91. (c == 0xed && *p >= 0xa0))
  92. return p - string;
  93. break;
  94. /* Check for 1111 0000, xx00 xxxx (overlong sequence) or
  95. greater than 0x0010ffff (f4 8f bf bf) */
  96. case 3:
  97. if ((c == 0xf0 && (*p & 0x30) == 0) ||
  98. (c > 0xf4 ) ||
  99. (c == 0xf4 && *p > 0x8f))
  100. return p - string;
  101. break;
  102. #if 0
  103. /* These cases can no longer occur, as we restrict to a maximum of four
  104. bytes nowadays. Leave the code here in case we ever want to add an option
  105. for longer sequences. */
  106. /* Check for 1111 1000, xx00 0xxx */
  107. case 4:
  108. if (c == 0xf8 && (*p & 0x38) == 0) return p - string;
  109. break;
  110. /* Check for leading 0xfe or 0xff, and then for 1111 1100, xx00 00xx */
  111. case 5:
  112. if (c == 0xfe || c == 0xff ||
  113. (c == 0xfc && (*p & 0x3c) == 0)) return p - string;
  114. break;
  115. #endif
  116. }
  117. /* Check for valid bytes after the 2nd, if any; all must start 10 */
  118. while (--ab > 0)
  119. {
  120. if ((*(++p) & 0xc0) != 0x80) return p - string;
  121. }
  122. }
  123. #else
  124. (void)(string); /* Keep picky compilers happy */
  125. (void)(length);
  126. #endif
  127. return -1;
  128. }
  129. /* End of pcre_valid_utf8.c */