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