pcre_xclass.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 that is used to match an extended
  33. class (one that contains characters whose values are > 255). It is used by both
  34. pcre_exec() and pcre_def_exec(). */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #include "pcre_internal.h"
  39. /*************************************************
  40. * Match character against an XCLASS *
  41. *************************************************/
  42. /* This function is called to match a character against an extended class that
  43. might contain values > 255.
  44. Arguments:
  45. c the character
  46. data points to the flag byte of the XCLASS data
  47. Returns: TRUE if character matches, else FALSE
  48. */
  49. BOOL
  50. _pcre_xclass(int c, const uschar *data)
  51. {
  52. int t;
  53. BOOL negated = (*data & XCL_NOT) != 0;
  54. /* Character values < 256 are matched against a bitmap, if one is present. If
  55. not, we still carry on, because there may be ranges that start below 256 in the
  56. additional data. */
  57. if (c < 256)
  58. {
  59. if ((*data & XCL_MAP) != 0 && (data[1 + c/8] & (1 << (c&7))) != 0)
  60. return !negated; /* char found */
  61. }
  62. /* First skip the bit map if present. Then match against the list of Unicode
  63. properties or large chars or ranges that end with a large char. We won't ever
  64. encounter XCL_PROP or XCL_NOTPROP when UCP support is not compiled. */
  65. if ((*data++ & XCL_MAP) != 0) data += 32;
  66. while ((t = *data++) != XCL_END)
  67. {
  68. int x, y;
  69. if (t == XCL_SINGLE)
  70. {
  71. GETCHARINC(x, data);
  72. if (c == x) return !negated;
  73. }
  74. else if (t == XCL_RANGE)
  75. {
  76. GETCHARINC(x, data);
  77. GETCHARINC(y, data);
  78. if (c >= x && c <= y) return !negated;
  79. }
  80. #ifdef SUPPORT_UCP
  81. else /* XCL_PROP & XCL_NOTPROP */
  82. {
  83. int chartype, script;
  84. int category = _pcre_ucp_findprop(c, &chartype, &script);
  85. switch(*data)
  86. {
  87. case PT_ANY:
  88. if (t == XCL_PROP) return !negated;
  89. break;
  90. case PT_LAMP:
  91. if ((chartype == ucp_Lu || chartype == ucp_Ll || chartype == ucp_Lt) ==
  92. (t == XCL_PROP)) return !negated;
  93. break;
  94. case PT_GC:
  95. if ((data[1] == category) == (t == XCL_PROP)) return !negated;
  96. break;
  97. case PT_PC:
  98. if ((data[1] == chartype) == (t == XCL_PROP)) return !negated;
  99. break;
  100. case PT_SC:
  101. if ((data[1] == script) == (t == XCL_PROP)) return !negated;
  102. break;
  103. /* This should never occur, but compilers may mutter if there is no
  104. default. */
  105. default:
  106. return FALSE;
  107. }
  108. data += 2;
  109. }
  110. #endif /* SUPPORT_UCP */
  111. }
  112. return negated; /* char did not match */
  113. }
  114. /* End of pcre_xclass.c */