pcre2_xclass.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2016-2019 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. /* This module contains an internal function that is used to match an extended
  34. class. It is used by pcre2_auto_possessify() and by both pcre2_match() and
  35. pcre2_def_match(). */
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39. #include "pcre2_internal.h"
  40. /*************************************************
  41. * Match character against an XCLASS *
  42. *************************************************/
  43. /* This function is called to match a character against an extended class that
  44. might contain codepoints above 255 and/or Unicode properties.
  45. Arguments:
  46. c the character
  47. data points to the flag code unit of the XCLASS data
  48. utf TRUE if in UTF mode
  49. Returns: TRUE if character matches, else FALSE
  50. */
  51. BOOL
  52. PRIV(xclass)(uint32_t c, PCRE2_SPTR data, BOOL utf)
  53. {
  54. PCRE2_UCHAR t;
  55. BOOL negated = (*data & XCL_NOT) != 0;
  56. #if PCRE2_CODE_UNIT_WIDTH == 8
  57. /* In 8 bit mode, this must always be TRUE. Help the compiler to know that. */
  58. utf = TRUE;
  59. #endif
  60. /* Code points < 256 are matched against a bitmap, if one is present. If not,
  61. we still carry on, because there may be ranges that start below 256 in the
  62. additional data. */
  63. if (c < 256)
  64. {
  65. if ((*data & XCL_HASPROP) == 0)
  66. {
  67. if ((*data & XCL_MAP) == 0) return negated;
  68. return (((uint8_t *)(data + 1))[c/8] & (1u << (c&7))) != 0;
  69. }
  70. if ((*data & XCL_MAP) != 0 &&
  71. (((uint8_t *)(data + 1))[c/8] & (1u << (c&7))) != 0)
  72. return !negated; /* char found */
  73. }
  74. /* First skip the bit map if present. Then match against the list of Unicode
  75. properties or large chars or ranges that end with a large char. We won't ever
  76. encounter XCL_PROP or XCL_NOTPROP when UTF support is not compiled. */
  77. if ((*data++ & XCL_MAP) != 0) data += 32 / sizeof(PCRE2_UCHAR);
  78. while ((t = *data++) != XCL_END)
  79. {
  80. uint32_t x, y;
  81. if (t == XCL_SINGLE)
  82. {
  83. #ifdef SUPPORT_UNICODE
  84. if (utf)
  85. {
  86. GETCHARINC(x, data); /* macro generates multiple statements */
  87. }
  88. else
  89. #endif
  90. x = *data++;
  91. if (c == x) return !negated;
  92. }
  93. else if (t == XCL_RANGE)
  94. {
  95. #ifdef SUPPORT_UNICODE
  96. if (utf)
  97. {
  98. GETCHARINC(x, data); /* macro generates multiple statements */
  99. GETCHARINC(y, data); /* macro generates multiple statements */
  100. }
  101. else
  102. #endif
  103. {
  104. x = *data++;
  105. y = *data++;
  106. }
  107. if (c >= x && c <= y) return !negated;
  108. }
  109. #ifdef SUPPORT_UNICODE
  110. else /* XCL_PROP & XCL_NOTPROP */
  111. {
  112. const ucd_record *prop = GET_UCD(c);
  113. BOOL isprop = t == XCL_PROP;
  114. switch(*data)
  115. {
  116. case PT_ANY:
  117. if (isprop) return !negated;
  118. break;
  119. case PT_LAMP:
  120. if ((prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
  121. prop->chartype == ucp_Lt) == isprop) return !negated;
  122. break;
  123. case PT_GC:
  124. if ((data[1] == PRIV(ucp_gentype)[prop->chartype]) == isprop)
  125. return !negated;
  126. break;
  127. case PT_PC:
  128. if ((data[1] == prop->chartype) == isprop) return !negated;
  129. break;
  130. case PT_SC:
  131. if ((data[1] == prop->script) == isprop) return !negated;
  132. break;
  133. case PT_ALNUM:
  134. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  135. PRIV(ucp_gentype)[prop->chartype] == ucp_N) == isprop)
  136. return !negated;
  137. break;
  138. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  139. which means that Perl space and POSIX space are now identical. PCRE
  140. was changed at release 8.34. */
  141. case PT_SPACE: /* Perl space */
  142. case PT_PXSPACE: /* POSIX space */
  143. switch(c)
  144. {
  145. HSPACE_CASES:
  146. VSPACE_CASES:
  147. if (isprop) return !negated;
  148. break;
  149. default:
  150. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == isprop)
  151. return !negated;
  152. break;
  153. }
  154. break;
  155. case PT_WORD:
  156. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  157. PRIV(ucp_gentype)[prop->chartype] == ucp_N || c == CHAR_UNDERSCORE)
  158. == isprop)
  159. return !negated;
  160. break;
  161. case PT_UCNC:
  162. if (c < 0xa0)
  163. {
  164. if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  165. c == CHAR_GRAVE_ACCENT) == isprop)
  166. return !negated;
  167. }
  168. else
  169. {
  170. if ((c < 0xd800 || c > 0xdfff) == isprop)
  171. return !negated;
  172. }
  173. break;
  174. /* The following three properties can occur only in an XCLASS, as there
  175. is no \p or \P coding for them. */
  176. /* Graphic character. Implement this as not Z (space or separator) and
  177. not C (other), except for Cf (format) with a few exceptions. This seems
  178. to be what Perl does. The exceptional characters are:
  179. U+061C Arabic Letter Mark
  180. U+180E Mongolian Vowel Separator
  181. U+2066 - U+2069 Various "isolate"s
  182. */
  183. case PT_PXGRAPH:
  184. if ((PRIV(ucp_gentype)[prop->chartype] != ucp_Z &&
  185. (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
  186. (prop->chartype == ucp_Cf &&
  187. c != 0x061c && c != 0x180e && (c < 0x2066 || c > 0x2069))
  188. )) == isprop)
  189. return !negated;
  190. break;
  191. /* Printable character: same as graphic, with the addition of Zs, i.e.
  192. not Zl and not Zp, and U+180E. */
  193. case PT_PXPRINT:
  194. if ((prop->chartype != ucp_Zl &&
  195. prop->chartype != ucp_Zp &&
  196. (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
  197. (prop->chartype == ucp_Cf &&
  198. c != 0x061c && (c < 0x2066 || c > 0x2069))
  199. )) == isprop)
  200. return !negated;
  201. break;
  202. /* Punctuation: all Unicode punctuation, plus ASCII characters that
  203. Unicode treats as symbols rather than punctuation, for Perl
  204. compatibility (these are $+<=>^`|~). */
  205. case PT_PXPUNCT:
  206. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_P ||
  207. (c < 128 && PRIV(ucp_gentype)[prop->chartype] == ucp_S)) == isprop)
  208. return !negated;
  209. break;
  210. /* This should never occur, but compilers may mutter if there is no
  211. default. */
  212. default:
  213. return FALSE;
  214. }
  215. data += 2;
  216. }
  217. #else
  218. (void)utf; /* Avoid compiler warning */
  219. #endif /* SUPPORT_UNICODE */
  220. }
  221. return negated; /* char did not match */
  222. }
  223. /* End of pcre2_xclass.c */