pcre_xclass.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 that is used to match an extended
  33. class. It is used by both pcre_exec() and pcre_def_exec(). */
  34. #include "pcre_config.h"
  35. #include "pcre_internal.h"
  36. /*************************************************
  37. * Match character against an XCLASS *
  38. *************************************************/
  39. /* This function is called to match a character against an extended class that
  40. might contain values > 255 and/or Unicode properties.
  41. Arguments:
  42. c the character
  43. data points to the flag byte of the XCLASS data
  44. Returns: TRUE if character matches, else FALSE
  45. */
  46. BOOL
  47. PRIV(xclass)(pcre_uint32 c, const pcre_uchar *data, BOOL utf)
  48. {
  49. pcre_uchar t;
  50. BOOL negated = (*data & XCL_NOT) != 0;
  51. (void)utf;
  52. #ifdef COMPILE_PCRE8
  53. /* In 8 bit mode, this must always be TRUE. Help the compiler to know that. */
  54. utf = TRUE;
  55. #endif
  56. /* Character values < 256 are matched against a bitmap, if one is present. If
  57. not, we still carry on, because there may be ranges that start below 256 in the
  58. additional data. */
  59. if (c < 256)
  60. {
  61. if ((*data & XCL_HASPROP) == 0)
  62. {
  63. if ((*data & XCL_MAP) == 0) return negated;
  64. return (((pcre_uint8 *)(data + 1))[c/8] & (1 << (c&7))) != 0;
  65. }
  66. if ((*data & XCL_MAP) != 0 &&
  67. (((pcre_uint8 *)(data + 1))[c/8] & (1 << (c&7))) != 0)
  68. return !negated; /* char found */
  69. }
  70. /* First skip the bit map if present. Then match against the list of Unicode
  71. properties or large chars or ranges that end with a large char. We won't ever
  72. encounter XCL_PROP or XCL_NOTPROP when UCP support is not compiled. */
  73. if ((*data++ & XCL_MAP) != 0) data += 32 / sizeof(pcre_uchar);
  74. while ((t = *data++) != XCL_END)
  75. {
  76. pcre_uint32 x, y;
  77. if (t == XCL_SINGLE)
  78. {
  79. #ifdef SUPPORT_UTF
  80. if (utf)
  81. {
  82. GETCHARINC(x, data); /* macro generates multiple statements */
  83. }
  84. else
  85. #endif
  86. x = *data++;
  87. if (c == x) return !negated;
  88. }
  89. else if (t == XCL_RANGE)
  90. {
  91. #ifdef SUPPORT_UTF
  92. if (utf)
  93. {
  94. GETCHARINC(x, data); /* macro generates multiple statements */
  95. GETCHARINC(y, data); /* macro generates multiple statements */
  96. }
  97. else
  98. #endif
  99. {
  100. x = *data++;
  101. y = *data++;
  102. }
  103. if (c >= x && c <= y) return !negated;
  104. }
  105. #ifdef SUPPORT_UCP
  106. else /* XCL_PROP & XCL_NOTPROP */
  107. {
  108. const ucd_record *prop = GET_UCD(c);
  109. BOOL isprop = t == XCL_PROP;
  110. switch(*data)
  111. {
  112. case PT_ANY:
  113. if (isprop) return !negated;
  114. break;
  115. case PT_LAMP:
  116. if ((prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
  117. prop->chartype == ucp_Lt) == isprop) return !negated;
  118. break;
  119. case PT_GC:
  120. if ((data[1] == PRIV(ucp_gentype)[prop->chartype]) == isprop)
  121. return !negated;
  122. break;
  123. case PT_PC:
  124. if ((data[1] == prop->chartype) == isprop) return !negated;
  125. break;
  126. case PT_SC:
  127. if ((data[1] == prop->script) == isprop) return !negated;
  128. break;
  129. case PT_ALNUM:
  130. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  131. PRIV(ucp_gentype)[prop->chartype] == ucp_N) == isprop)
  132. return !negated;
  133. break;
  134. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  135. which means that Perl space and POSIX space are now identical. PCRE
  136. was changed at release 8.34. */
  137. case PT_SPACE: /* Perl space */
  138. case PT_PXSPACE: /* POSIX space */
  139. switch(c)
  140. {
  141. HSPACE_CASES:
  142. VSPACE_CASES:
  143. if (isprop) return !negated;
  144. break;
  145. default:
  146. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == isprop)
  147. return !negated;
  148. break;
  149. }
  150. break;
  151. case PT_WORD:
  152. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  153. PRIV(ucp_gentype)[prop->chartype] == ucp_N || c == CHAR_UNDERSCORE)
  154. == isprop)
  155. return !negated;
  156. break;
  157. case PT_UCNC:
  158. if (c < 0xa0)
  159. {
  160. if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  161. c == CHAR_GRAVE_ACCENT) == isprop)
  162. return !negated;
  163. }
  164. else
  165. {
  166. if ((c < 0xd800 || c > 0xdfff) == isprop)
  167. return !negated;
  168. }
  169. break;
  170. /* The following three properties can occur only in an XCLASS, as there
  171. is no \p or \P coding for them. */
  172. /* Graphic character. Implement this as not Z (space or separator) and
  173. not C (other), except for Cf (format) with a few exceptions. This seems
  174. to be what Perl does. The exceptional characters are:
  175. U+061C Arabic Letter Mark
  176. U+180E Mongolian Vowel Separator
  177. U+2066 - U+2069 Various "isolate"s
  178. */
  179. case PT_PXGRAPH:
  180. if ((PRIV(ucp_gentype)[prop->chartype] != ucp_Z &&
  181. (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
  182. (prop->chartype == ucp_Cf &&
  183. c != 0x061c && c != 0x180e && (c < 0x2066 || c > 0x2069))
  184. )) == isprop)
  185. return !negated;
  186. break;
  187. /* Printable character: same as graphic, with the addition of Zs, i.e.
  188. not Zl and not Zp, and U+180E. */
  189. case PT_PXPRINT:
  190. if ((prop->chartype != ucp_Zl &&
  191. prop->chartype != ucp_Zp &&
  192. (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
  193. (prop->chartype == ucp_Cf &&
  194. c != 0x061c && (c < 0x2066 || c > 0x2069))
  195. )) == isprop)
  196. return !negated;
  197. break;
  198. /* Punctuation: all Unicode punctuation, plus ASCII characters that
  199. Unicode treats as symbols rather than punctuation, for Perl
  200. compatibility (these are $+<=>^`|~). */
  201. case PT_PXPUNCT:
  202. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_P ||
  203. (c < 256 && PRIV(ucp_gentype)[prop->chartype] == ucp_S)) == isprop)
  204. return !negated;
  205. break;
  206. /* This should never occur, but compilers may mutter if there is no
  207. default. */
  208. default:
  209. return FALSE;
  210. }
  211. data += 2;
  212. }
  213. #endif /* SUPPORT_UCP */
  214. }
  215. return negated; /* char did not match */
  216. }
  217. /* End of pcre_xclass.c */