pcre_xclass.c 8.0 KB

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