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