blob: bb57196449adc9aa3f99f4c8b4e27bb2dbc842f4 [file] [log] [blame]
Nick Kralevichf73ff172014-09-27 12:41:49 -07001/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
Janis Danisevskis112c9cc2016-03-31 13:35:25 +01009 Original API code Copyright (c) 1997-2012 University of Cambridge
Elliott Hughes4e19c8e2022-04-15 15:11:02 -070010 New API code Copyright (c) 2016-2022 University of Cambridge
Nick Kralevichf73ff172014-09-27 12:41:49 -070011
12-----------------------------------------------------------------------------
13Redistribution and use in source and binary forms, with or without
14modification, 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
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37POSSIBILITY OF SUCH DAMAGE.
38-----------------------------------------------------------------------------
39*/
40
Nick Kralevichf73ff172014-09-27 12:41:49 -070041/* This module contains an internal function that is used to match an extended
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010042class. It is used by pcre2_auto_possessify() and by both pcre2_match() and
43pcre2_def_match(). */
Nick Kralevichf73ff172014-09-27 12:41:49 -070044
45
46#ifdef HAVE_CONFIG_H
47#include "config.h"
48#endif
49
Nick Kralevichf73ff172014-09-27 12:41:49 -070050
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010051#include "pcre2_internal.h"
Nick Kralevichf73ff172014-09-27 12:41:49 -070052
53/*************************************************
54* Match character against an XCLASS *
55*************************************************/
56
57/* This function is called to match a character against an extended class that
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010058might contain codepoints above 255 and/or Unicode properties.
Nick Kralevichf73ff172014-09-27 12:41:49 -070059
60Arguments:
61 c the character
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010062 data points to the flag code unit of the XCLASS data
63 utf TRUE if in UTF mode
Nick Kralevichf73ff172014-09-27 12:41:49 -070064
65Returns: TRUE if character matches, else FALSE
66*/
67
68BOOL
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010069PRIV(xclass)(uint32_t c, PCRE2_SPTR data, BOOL utf)
Nick Kralevichf73ff172014-09-27 12:41:49 -070070{
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010071PCRE2_UCHAR t;
Nick Kralevichf73ff172014-09-27 12:41:49 -070072BOOL negated = (*data & XCL_NOT) != 0;
73
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010074#if PCRE2_CODE_UNIT_WIDTH == 8
Nick Kralevichf73ff172014-09-27 12:41:49 -070075/* In 8 bit mode, this must always be TRUE. Help the compiler to know that. */
76utf = TRUE;
77#endif
78
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010079/* Code points < 256 are matched against a bitmap, if one is present. If not,
80we still carry on, because there may be ranges that start below 256 in the
Nick Kralevichf73ff172014-09-27 12:41:49 -070081additional data. */
82
83if (c < 256)
84 {
85 if ((*data & XCL_HASPROP) == 0)
86 {
87 if ((*data & XCL_MAP) == 0) return negated;
Elliott Hughes0c26e192019-08-07 12:24:46 -070088 return (((uint8_t *)(data + 1))[c/8] & (1u << (c&7))) != 0;
Nick Kralevichf73ff172014-09-27 12:41:49 -070089 }
90 if ((*data & XCL_MAP) != 0 &&
Elliott Hughes0c26e192019-08-07 12:24:46 -070091 (((uint8_t *)(data + 1))[c/8] & (1u << (c&7))) != 0)
Nick Kralevichf73ff172014-09-27 12:41:49 -070092 return !negated; /* char found */
93 }
94
95/* First skip the bit map if present. Then match against the list of Unicode
96properties or large chars or ranges that end with a large char. We won't ever
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010097encounter XCL_PROP or XCL_NOTPROP when UTF support is not compiled. */
Nick Kralevichf73ff172014-09-27 12:41:49 -070098
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010099if ((*data++ & XCL_MAP) != 0) data += 32 / sizeof(PCRE2_UCHAR);
Nick Kralevichf73ff172014-09-27 12:41:49 -0700100
101while ((t = *data++) != XCL_END)
102 {
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100103 uint32_t x, y;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700104 if (t == XCL_SINGLE)
105 {
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100106#ifdef SUPPORT_UNICODE
Nick Kralevichf73ff172014-09-27 12:41:49 -0700107 if (utf)
108 {
109 GETCHARINC(x, data); /* macro generates multiple statements */
110 }
111 else
112#endif
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100113 x = *data++;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700114 if (c == x) return !negated;
115 }
116 else if (t == XCL_RANGE)
117 {
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100118#ifdef SUPPORT_UNICODE
Nick Kralevichf73ff172014-09-27 12:41:49 -0700119 if (utf)
120 {
121 GETCHARINC(x, data); /* macro generates multiple statements */
122 GETCHARINC(y, data); /* macro generates multiple statements */
123 }
124 else
125#endif
126 {
127 x = *data++;
128 y = *data++;
129 }
130 if (c >= x && c <= y) return !negated;
131 }
132
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100133#ifdef SUPPORT_UNICODE
Nick Kralevichf73ff172014-09-27 12:41:49 -0700134 else /* XCL_PROP & XCL_NOTPROP */
135 {
136 const ucd_record *prop = GET_UCD(c);
137 BOOL isprop = t == XCL_PROP;
Elliott Hughes4e19c8e2022-04-15 15:11:02 -0700138 BOOL ok;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700139
140 switch(*data)
141 {
142 case PT_ANY:
143 if (isprop) return !negated;
144 break;
145
146 case PT_LAMP:
147 if ((prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
148 prop->chartype == ucp_Lt) == isprop) return !negated;
149 break;
150
151 case PT_GC:
152 if ((data[1] == PRIV(ucp_gentype)[prop->chartype]) == isprop)
153 return !negated;
154 break;
155
156 case PT_PC:
157 if ((data[1] == prop->chartype) == isprop) return !negated;
158 break;
159
160 case PT_SC:
161 if ((data[1] == prop->script) == isprop) return !negated;
162 break;
163
Elliott Hughes4e19c8e2022-04-15 15:11:02 -0700164 case PT_SCX:
165 ok = (data[1] == prop->script ||
166 MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), data[1]) != 0);
167 if (ok == isprop) return !negated;
168 break;
169
Nick Kralevichf73ff172014-09-27 12:41:49 -0700170 case PT_ALNUM:
171 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
172 PRIV(ucp_gentype)[prop->chartype] == ucp_N) == isprop)
173 return !negated;
174 break;
175
176 /* Perl space used to exclude VT, but from Perl 5.18 it is included,
177 which means that Perl space and POSIX space are now identical. PCRE
178 was changed at release 8.34. */
179
180 case PT_SPACE: /* Perl space */
181 case PT_PXSPACE: /* POSIX space */
182 switch(c)
183 {
184 HSPACE_CASES:
185 VSPACE_CASES:
186 if (isprop) return !negated;
187 break;
188
189 default:
190 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == isprop)
191 return !negated;
192 break;
193 }
194 break;
195
196 case PT_WORD:
197 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
198 PRIV(ucp_gentype)[prop->chartype] == ucp_N || c == CHAR_UNDERSCORE)
199 == isprop)
200 return !negated;
201 break;
202
203 case PT_UCNC:
204 if (c < 0xa0)
205 {
206 if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
207 c == CHAR_GRAVE_ACCENT) == isprop)
208 return !negated;
209 }
210 else
211 {
212 if ((c < 0xd800 || c > 0xdfff) == isprop)
213 return !negated;
214 }
215 break;
216
Elliott Hughes4e19c8e2022-04-15 15:11:02 -0700217 case PT_BIDICL:
218 if ((UCD_BIDICLASS_PROP(prop) == data[1]) == isprop)
219 return !negated;
220 break;
221
222 case PT_BOOL:
223 ok = MAPBIT(PRIV(ucd_boolprop_sets) +
224 UCD_BPROPS_PROP(prop), data[1]) != 0;
225 if (ok == isprop) return !negated;
226 break;
227
Nick Kralevichf73ff172014-09-27 12:41:49 -0700228 /* The following three properties can occur only in an XCLASS, as there
229 is no \p or \P coding for them. */
230
231 /* Graphic character. Implement this as not Z (space or separator) and
232 not C (other), except for Cf (format) with a few exceptions. This seems
233 to be what Perl does. The exceptional characters are:
234
235 U+061C Arabic Letter Mark
236 U+180E Mongolian Vowel Separator
237 U+2066 - U+2069 Various "isolate"s
238 */
239
240 case PT_PXGRAPH:
241 if ((PRIV(ucp_gentype)[prop->chartype] != ucp_Z &&
242 (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
243 (prop->chartype == ucp_Cf &&
244 c != 0x061c && c != 0x180e && (c < 0x2066 || c > 0x2069))
245 )) == isprop)
246 return !negated;
247 break;
248
249 /* Printable character: same as graphic, with the addition of Zs, i.e.
250 not Zl and not Zp, and U+180E. */
251
252 case PT_PXPRINT:
253 if ((prop->chartype != ucp_Zl &&
254 prop->chartype != ucp_Zp &&
255 (PRIV(ucp_gentype)[prop->chartype] != ucp_C ||
256 (prop->chartype == ucp_Cf &&
257 c != 0x061c && (c < 0x2066 || c > 0x2069))
258 )) == isprop)
259 return !negated;
260 break;
261
262 /* Punctuation: all Unicode punctuation, plus ASCII characters that
263 Unicode treats as symbols rather than punctuation, for Perl
264 compatibility (these are $+<=>^`|~). */
265
266 case PT_PXPUNCT:
267 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_P ||
Elliott Hughes0ea98832015-12-04 23:18:20 -0800268 (c < 128 && PRIV(ucp_gentype)[prop->chartype] == ucp_S)) == isprop)
Nick Kralevichf73ff172014-09-27 12:41:49 -0700269 return !negated;
270 break;
271
272 /* This should never occur, but compilers may mutter if there is no
273 default. */
274
275 default:
276 return FALSE;
277 }
278
279 data += 2;
280 }
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100281#else
282 (void)utf; /* Avoid compiler warning */
283#endif /* SUPPORT_UNICODE */
Nick Kralevichf73ff172014-09-27 12:41:49 -0700284 }
285
286return negated; /* char did not match */
287}
288
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100289/* End of pcre2_xclass.c */