pcre16_valid_utf16.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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-16 character
  33. strings. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "pcre_config.h"
  36. #endif
  37. /* Generate code with 16 bit character support. */
  38. #define COMPILE_PCRE16
  39. #include "pcre_internal.h"
  40. /*************************************************
  41. * Validate a UTF-16 string *
  42. *************************************************/
  43. /* This function is called (optionally) at the start of compile or match, to
  44. check that a supposed UTF-16 string is actually valid. The early check means
  45. that subsequent code can assume it is dealing with a valid string. The check
  46. can be turned off for maximum performance, but the consequences of supplying an
  47. invalid string are then undefined.
  48. From release 8.21 more information about the details of the error are passed
  49. back in the returned value:
  50. PCRE_UTF16_ERR0 No error
  51. PCRE_UTF16_ERR1 Missing low surrogate at the end of the string
  52. PCRE_UTF16_ERR2 Invalid low surrogate
  53. PCRE_UTF16_ERR3 Isolated low surrogate
  54. PCRE_UTF16_ERR4 Unused (was non-character)
  55. Arguments:
  56. string points to the string
  57. length length of string, or -1 if the string is zero-terminated
  58. errp pointer to an error position offset variable
  59. Returns: = 0 if the string is a valid UTF-16 string
  60. > 0 otherwise, setting the offset of the bad character
  61. */
  62. int
  63. PRIV(valid_utf)(PCRE_PUCHAR string, int length, int *erroroffset)
  64. {
  65. #ifdef SUPPORT_UTF
  66. register PCRE_PUCHAR p;
  67. register pcre_uint32 c;
  68. if (length < 0)
  69. {
  70. for (p = string; *p != 0; p++);
  71. length = p - string;
  72. }
  73. for (p = string; length-- > 0; p++)
  74. {
  75. c = *p;
  76. if ((c & 0xf800) != 0xd800)
  77. {
  78. /* Normal UTF-16 code point. Neither high nor low surrogate. */
  79. }
  80. else if ((c & 0x0400) == 0)
  81. {
  82. /* High surrogate. Must be a followed by a low surrogate. */
  83. if (length == 0)
  84. {
  85. *erroroffset = p - string;
  86. return PCRE_UTF16_ERR1;
  87. }
  88. p++;
  89. length--;
  90. if ((*p & 0xfc00) != 0xdc00)
  91. {
  92. *erroroffset = p - string;
  93. return PCRE_UTF16_ERR2;
  94. }
  95. }
  96. else
  97. {
  98. /* Isolated low surrogate. Always an error. */
  99. *erroroffset = p - string;
  100. return PCRE_UTF16_ERR3;
  101. }
  102. }
  103. #else /* SUPPORT_UTF */
  104. (void)(string); /* Keep picky compilers happy */
  105. (void)(length);
  106. (void)(erroroffset);
  107. #endif /* SUPPORT_UTF */
  108. return PCRE_UTF16_ERR0; /* This indicates success */
  109. }
  110. /* End of pcre16_valid_utf16.c */