/[pcre]/code/branches/pcre16/pcre_compile.c
ViewVC logotype

Contents of /code/branches/pcre16/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 785 - (show annotations)
Mon Dec 5 20:12:24 2011 UTC (7 years, 9 months ago) by zherczeg
File MIME type: text/plain
File size: 258985 byte(s)
Improving UTF-16 support by fixing a lot of issues.
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-2011 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 the external function pcre_compile(), along with
42 supporting internal functions that are not used by other modules. */
43
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #define NLBLOCK cd /* Block containing newline information */
50 #define PSSTART start_pattern /* Field containing processed string start */
51 #define PSEND end_pattern /* Field containing processed string end */
52
53 #include "pcre_internal.h"
54
55
56 /* When PCRE_DEBUG is defined, we need the pcre_printint() function, which is
57 also used by pcretest. PCRE_DEBUG is not defined when building a production
58 library. */
59
60 #ifdef PCRE_DEBUG
61 #include "pcre_printint.src"
62 #endif
63
64
65 /* Macro for setting individual bits in class bitmaps. */
66
67 #define SETBIT(a,b) a[b/8] |= (1 << (b%8))
68
69 /* Maximum length value to check against when making sure that the integer that
70 holds the compiled pattern length does not overflow. We make it a bit less than
71 INT_MAX to allow for adding in group terminating bytes, so that we don't have
72 to check them every time. */
73
74 #define OFLOW_MAX (INT_MAX - 20)
75
76
77 /*************************************************
78 * Code parameters and static tables *
79 *************************************************/
80
81 /* This value specifies the size of stack workspace that is used during the
82 first pre-compile phase that determines how much memory is required. The regex
83 is partly compiled into this space, but the compiled parts are discarded as
84 soon as they can be, so that hopefully there will never be an overrun. The code
85 does, however, check for an overrun. The largest amount I've seen used is 218,
86 so this number is very generous.
87
88 The same workspace is used during the second, actual compile phase for
89 remembering forward references to groups so that they can be filled in at the
90 end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
91 is 4 there is plenty of room. */
92
93 #define COMPILE_WORK_SIZE (4096)
94
95 /* The overrun tests check for a slightly smaller size so that they detect the
96 overrun before it actually does run off the end of the data block. */
97
98 #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)
99
100 /* Private flags added to firstchar and reqchar. */
101
102 #define REQ_CASELESS 0x10000000l /* Indicates caselessness */
103 #define REQ_VARY 0x20000000l /* Reqchar followed non-literal item */
104
105 /* Repeated character flags. */
106
107 #define UTF_LENGTH 0x10000000l /* The char contains its length. */
108
109 /* Table for handling escaped characters in the range '0'-'z'. Positive returns
110 are simple data values; negative values are for special things like \d and so
111 on. Zero means further processing is needed (for things like \x), or the escape
112 is invalid. */
113
114 #ifndef EBCDIC
115
116 /* This is the "normal" table for ASCII systems or for EBCDIC systems running
117 in UTF-8 mode. */
118
119 static const short int escapes[] = {
120 0, 0,
121 0, 0,
122 0, 0,
123 0, 0,
124 0, 0,
125 CHAR_COLON, CHAR_SEMICOLON,
126 CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN,
127 CHAR_GREATER_THAN_SIGN, CHAR_QUESTION_MARK,
128 CHAR_COMMERCIAL_AT, -ESC_A,
129 -ESC_B, -ESC_C,
130 -ESC_D, -ESC_E,
131 0, -ESC_G,
132 -ESC_H, 0,
133 0, -ESC_K,
134 0, 0,
135 -ESC_N, 0,
136 -ESC_P, -ESC_Q,
137 -ESC_R, -ESC_S,
138 0, 0,
139 -ESC_V, -ESC_W,
140 -ESC_X, 0,
141 -ESC_Z, CHAR_LEFT_SQUARE_BRACKET,
142 CHAR_BACKSLASH, CHAR_RIGHT_SQUARE_BRACKET,
143 CHAR_CIRCUMFLEX_ACCENT, CHAR_UNDERSCORE,
144 CHAR_GRAVE_ACCENT, 7,
145 -ESC_b, 0,
146 -ESC_d, ESC_e,
147 ESC_f, 0,
148 -ESC_h, 0,
149 0, -ESC_k,
150 0, 0,
151 ESC_n, 0,
152 -ESC_p, 0,
153 ESC_r, -ESC_s,
154 ESC_tee, 0,
155 -ESC_v, -ESC_w,
156 0, 0,
157 -ESC_z
158 };
159
160 #else
161
162 /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
163
164 static const short int escapes[] = {
165 /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
166 /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
167 /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
168 /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
169 /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
170 /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
171 /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
172 /* 80 */ 0, 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0,
173 /* 88 */-ESC_h, 0, 0, '{', 0, 0, 0, 0,
174 /* 90 */ 0, 0, -ESC_k, 'l', 0, ESC_n, 0, -ESC_p,
175 /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0,
176 /* A0 */ 0, '~', -ESC_s, ESC_tee, 0,-ESC_v, -ESC_w, 0,
177 /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0,
178 /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
179 /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
180 /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G,
181 /* C8 */-ESC_H, 0, 0, 0, 0, 0, 0, 0,
182 /* D0 */ '}', 0, -ESC_K, 0, 0,-ESC_N, 0, -ESC_P,
183 /* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0,
184 /* E0 */ '\\', 0, -ESC_S, 0, 0,-ESC_V, -ESC_W, -ESC_X,
185 /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0,
186 /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
187 /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
188 };
189 #endif
190
191
192 /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
193 searched linearly. Put all the names into a single string, in order to reduce
194 the number of relocations when a shared library is dynamically linked. The
195 string is built from string macros so that it works in UTF-8 mode on EBCDIC
196 platforms. */
197
198 typedef struct verbitem {
199 int len; /* Length of verb name */
200 int op; /* Op when no arg, or -1 if arg mandatory */
201 int op_arg; /* Op when arg present, or -1 if not allowed */
202 } verbitem;
203
204 static const char verbnames[] =
205 "\0" /* Empty name is a shorthand for MARK */
206 STRING_MARK0
207 STRING_ACCEPT0
208 STRING_COMMIT0
209 STRING_F0
210 STRING_FAIL0
211 STRING_PRUNE0
212 STRING_SKIP0
213 STRING_THEN;
214
215 static const verbitem verbs[] = {
216 { 0, -1, OP_MARK },
217 { 4, -1, OP_MARK },
218 { 6, OP_ACCEPT, -1 },
219 { 6, OP_COMMIT, -1 },
220 { 1, OP_FAIL, -1 },
221 { 4, OP_FAIL, -1 },
222 { 5, OP_PRUNE, OP_PRUNE_ARG },
223 { 4, OP_SKIP, OP_SKIP_ARG },
224 { 4, OP_THEN, OP_THEN_ARG }
225 };
226
227 static const int verbcount = sizeof(verbs)/sizeof(verbitem);
228
229
230 /* Tables of names of POSIX character classes and their lengths. The names are
231 now all in a single string, to reduce the number of relocations when a shared
232 library is dynamically loaded. The list of lengths is terminated by a zero
233 length entry. The first three must be alpha, lower, upper, as this is assumed
234 for handling case independence. */
235
236 static const char posix_names[] =
237 STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
238 STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
239 STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
240 STRING_word0 STRING_xdigit;
241
242 static const pcre_uint8 posix_name_lengths[] = {
243 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
244
245 /* Table of class bit maps for each POSIX class. Each class is formed from a
246 base map, with an optional addition or removal of another map. Then, for some
247 classes, there is some additional tweaking: for [:blank:] the vertical space
248 characters are removed, and for [:alpha:] and [:alnum:] the underscore
249 character is removed. The triples in the table consist of the base map offset,
250 second map offset or -1 if no second map, and a non-negative value for map
251 addition or a negative value for map subtraction (if there are two maps). The
252 absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
253 remove vertical space characters, 2 => remove underscore. */
254
255 static const int posix_class_maps[] = {
256 cbit_word, cbit_digit, -2, /* alpha */
257 cbit_lower, -1, 0, /* lower */
258 cbit_upper, -1, 0, /* upper */
259 cbit_word, -1, 2, /* alnum - word without underscore */
260 cbit_print, cbit_cntrl, 0, /* ascii */
261 cbit_space, -1, 1, /* blank - a GNU extension */
262 cbit_cntrl, -1, 0, /* cntrl */
263 cbit_digit, -1, 0, /* digit */
264 cbit_graph, -1, 0, /* graph */
265 cbit_print, -1, 0, /* print */
266 cbit_punct, -1, 0, /* punct */
267 cbit_space, -1, 0, /* space */
268 cbit_word, -1, 0, /* word - a Perl extension */
269 cbit_xdigit,-1, 0 /* xdigit */
270 };
271
272 /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
273 substitutes must be in the order of the names, defined above, and there are
274 both positive and negative cases. NULL means no substitute. */
275
276 #ifdef SUPPORT_UCP
277 static const pcre_uchar string_PNd[] = {
278 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
279 CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
280 static const pcre_uchar string_pNd[] = {
281 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
282 CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
283 static const pcre_uchar string_PXsp[] = {
284 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
285 CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
286 static const pcre_uchar string_pXsp[] = {
287 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
288 CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
289 static const pcre_uchar string_PXwd[] = {
290 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
291 CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
292 static const pcre_uchar string_pXwd[] = {
293 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
294 CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
295
296 static const pcre_uchar *substitutes[] = {
297 string_PNd, /* \D */
298 string_pNd, /* \d */
299 string_PXsp, /* \S */ /* NOTE: Xsp is Perl space */
300 string_pXsp, /* \s */
301 string_PXwd, /* \W */
302 string_pXwd /* \w */
303 };
304
305 static const pcre_uchar string_pL[] = {
306 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
307 CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
308 static const pcre_uchar string_pLl[] = {
309 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
310 CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
311 static const pcre_uchar string_pLu[] = {
312 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
313 CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
314 static const pcre_uchar string_pXan[] = {
315 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
316 CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
317 static const pcre_uchar string_h[] = {
318 CHAR_BACKSLASH, CHAR_h, '\0' };
319 static const pcre_uchar string_pXps[] = {
320 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
321 CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
322 static const pcre_uchar string_PL[] = {
323 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
324 CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
325 static const pcre_uchar string_PLl[] = {
326 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
327 CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
328 static const pcre_uchar string_PLu[] = {
329 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
330 CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
331 static const pcre_uchar string_PXan[] = {
332 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
333 CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
334 static const pcre_uchar string_H[] = {
335 CHAR_BACKSLASH, CHAR_H, '\0' };
336 static const pcre_uchar string_PXps[] = {
337 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
338 CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
339
340 static const pcre_uchar *posix_substitutes[] = {
341 string_pL, /* alpha */
342 string_pLl, /* lower */
343 string_pLu, /* upper */
344 string_pXan, /* alnum */
345 NULL, /* ascii */
346 string_h, /* blank */
347 NULL, /* cntrl */
348 string_pNd, /* digit */
349 NULL, /* graph */
350 NULL, /* print */
351 NULL, /* punct */
352 string_pXps, /* space */ /* NOTE: Xps is POSIX space */
353 string_pXwd, /* word */
354 NULL, /* xdigit */
355 /* Negated cases */
356 string_PL, /* ^alpha */
357 string_PLl, /* ^lower */
358 string_PLu, /* ^upper */
359 string_PXan, /* ^alnum */
360 NULL, /* ^ascii */
361 string_H, /* ^blank */
362 NULL, /* ^cntrl */
363 string_PNd, /* ^digit */
364 NULL, /* ^graph */
365 NULL, /* ^print */
366 NULL, /* ^punct */
367 string_PXps, /* ^space */ /* NOTE: Xps is POSIX space */
368 string_PXwd, /* ^word */
369 NULL /* ^xdigit */
370 };
371 #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
372 #endif
373
374 #define STRING(a) # a
375 #define XSTRING(s) STRING(s)
376
377 /* The texts of compile-time error messages. These are "char *" because they
378 are passed to the outside world. Do not ever re-use any error number, because
379 they are documented. Always add a new error instead. Messages marked DEAD below
380 are no longer used. This used to be a table of strings, but in order to reduce
381 the number of relocations needed when a shared library is loaded dynamically,
382 it is now one long string. We cannot use a table of offsets, because the
383 lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
384 simply count through to the one we want - this isn't a performance issue
385 because these strings are used only when there is a compilation error.
386
387 Each substring ends with \0 to insert a null character. This includes the final
388 substring, so that the whole string ends with \0\0, which can be detected when
389 counting through. */
390
391 static const char error_texts[] =
392 "no error\0"
393 "\\ at end of pattern\0"
394 "\\c at end of pattern\0"
395 "unrecognized character follows \\\0"
396 "numbers out of order in {} quantifier\0"
397 /* 5 */
398 "number too big in {} quantifier\0"
399 "missing terminating ] for character class\0"
400 "invalid escape sequence in character class\0"
401 "range out of order in character class\0"
402 "nothing to repeat\0"
403 /* 10 */
404 "operand of unlimited repeat could match the empty string\0" /** DEAD **/
405 "internal error: unexpected repeat\0"
406 "unrecognized character after (? or (?-\0"
407 "POSIX named classes are supported only within a class\0"
408 "missing )\0"
409 /* 15 */
410 "reference to non-existent subpattern\0"
411 "erroffset passed as NULL\0"
412 "unknown option bit(s) set\0"
413 "missing ) after comment\0"
414 "parentheses nested too deeply\0" /** DEAD **/
415 /* 20 */
416 "regular expression is too large\0"
417 "failed to get memory\0"
418 "unmatched parentheses\0"
419 "internal error: code overflow\0"
420 "unrecognized character after (?<\0"
421 /* 25 */
422 "lookbehind assertion is not fixed length\0"
423 "malformed number or name after (?(\0"
424 "conditional group contains more than two branches\0"
425 "assertion expected after (?(\0"
426 "(?R or (?[+-]digits must be followed by )\0"
427 /* 30 */
428 "unknown POSIX class name\0"
429 "POSIX collating elements are not supported\0"
430 "this version of PCRE is not compiled with PCRE_UTF8 support\0"
431 "spare error\0" /** DEAD **/
432 "character value in \\x{...} sequence is too large\0"
433 /* 35 */
434 "invalid condition (?(0)\0"
435 "\\C not allowed in lookbehind assertion\0"
436 "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
437 "number after (?C is > 255\0"
438 "closing ) for (?C expected\0"
439 /* 40 */
440 "recursive call could loop indefinitely\0"
441 "unrecognized character after (?P\0"
442 "syntax error in subpattern name (missing terminator)\0"
443 "two named subpatterns have the same name\0"
444 "invalid UTF-8 string\0"
445 /* 45 */
446 "support for \\P, \\p, and \\X has not been compiled\0"
447 "malformed \\P or \\p sequence\0"
448 "unknown property name after \\P or \\p\0"
449 "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
450 "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
451 /* 50 */
452 "repeated subpattern is too long\0" /** DEAD **/
453 "octal value is greater than \\377 (not in UTF-8 mode)\0"
454 "internal error: overran compiling workspace\0"
455 "internal error: previously-checked referenced subpattern not found\0"
456 "DEFINE group contains more than one branch\0"
457 /* 55 */
458 "repeating a DEFINE group is not allowed\0" /** DEAD **/
459 "inconsistent NEWLINE options\0"
460 "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
461 "a numbered reference must not be zero\0"
462 "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
463 /* 60 */
464 "(*VERB) not recognized\0"
465 "number is too big\0"
466 "subpattern name expected\0"
467 "digit expected after (?+\0"
468 "] is an invalid data character in JavaScript compatibility mode\0"
469 /* 65 */
470 "different names for subpatterns of the same number are not allowed\0"
471 "(*MARK) must have an argument\0"
472 "this version of PCRE is not compiled with PCRE_UCP support\0"
473 "\\c must be followed by an ASCII character\0"
474 "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
475 /* 70 */
476 "internal error: unknown opcode in find_fixedlength()\0"
477 "Not allowed UTF-8 / UTF-16 code point (>= 0xd800 && <= 0xdfff)\0"
478 ;
479
480 /* Table to identify digits and hex digits. This is used when compiling
481 patterns. Note that the tables in chartables are dependent on the locale, and
482 may mark arbitrary characters as digits - but the PCRE compiling code expects
483 to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
484 a private table here. It costs 256 bytes, but it is a lot faster than doing
485 character value tests (at least in some simple cases I timed), and in some
486 applications one wants PCRE to compile efficiently as well as match
487 efficiently.
488
489 For convenience, we use the same bit definitions as in chartables:
490
491 0x04 decimal digit
492 0x08 hexadecimal digit
493
494 Then we can use ctype_digit and ctype_xdigit in the code. */
495
496 /* Using a simple comparison for decimal numbers rather than a memory read
497 is much faster, and the resulting code is simpler (the compiler turns it
498 into a subtraction and unsigned comparison). */
499
500 #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
501
502 #ifndef EBCDIC
503
504 /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
505 UTF-8 mode. */
506
507 static const pcre_uint8 digitab[] =
508 {
509 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
510 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
511 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
512 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
513 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
514 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
515 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
516 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
517 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
518 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
519 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
520 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
521 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
522 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
523 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
524 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
525 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
526 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
527 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
528 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
529 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
530 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
531 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
532 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
533 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
534 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
535 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
536 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
537 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
538 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
539 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
540 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
541
542 #else
543
544 /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
545
546 static const pcre_uint8 digitab[] =
547 {
548 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
549 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
550 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
551 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
552 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
553 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
554 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
555 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
556 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
557 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
558 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
559 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
560 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
561 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
562 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
563 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
564 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
565 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
566 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
567 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
568 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
569 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
570 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
571 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
572 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
573 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
574 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
575 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
576 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
577 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
578 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
579 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
580
581 static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
582 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
583 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
584 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
585 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
586 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
587 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
588 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
589 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
590 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
591 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
592 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
593 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
594 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
595 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
596 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
597 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
598 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
599 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
600 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
601 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
602 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
603 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
604 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
605 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
606 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
607 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
608 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
609 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
610 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
611 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
612 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
613 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
614 #endif
615
616
617 /* Definition to allow mutual recursion */
618
619 static BOOL
620 compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
621 int *, int *, branch_chain *, compile_data *, int *);
622
623
624
625 /*************************************************
626 * Find an error text *
627 *************************************************/
628
629 /* The error texts are now all in one long string, to save on relocations. As
630 some of the text is of unknown length, we can't use a table of offsets.
631 Instead, just count through the strings. This is not a performance issue
632 because it happens only when there has been a compilation error.
633
634 Argument: the error number
635 Returns: pointer to the error string
636 */
637
638 static const char *
639 find_error_text(int n)
640 {
641 const char *s = error_texts;
642 for (; n > 0; n--)
643 {
644 while (*s++ != 0) {};
645 if (*s == 0) return "Error text not found (please report)";
646 }
647 return s;
648 }
649
650
651 /*************************************************
652 * Check for counted repeat *
653 *************************************************/
654
655 /* This function is called when a '{' is encountered in a place where it might
656 start a quantifier. It looks ahead to see if it really is a quantifier or not.
657 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
658 where the ddds are digits.
659
660 Arguments:
661 p pointer to the first char after '{'
662
663 Returns: TRUE or FALSE
664 */
665
666 static BOOL
667 is_counted_repeat(const pcre_uchar *p)
668 {
669 if (!IS_DIGIT(*p)) return FALSE;
670 p++;
671 while (IS_DIGIT(*p)) p++;
672 if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
673
674 if (*p++ != CHAR_COMMA) return FALSE;
675 if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
676
677 if (!IS_DIGIT(*p)) return FALSE;
678 p++;
679 while (IS_DIGIT(*p)) p++;
680
681 return (*p == CHAR_RIGHT_CURLY_BRACKET);
682 }
683
684
685
686 /*************************************************
687 * Handle escapes *
688 *************************************************/
689
690 /* This function is called when a \ has been encountered. It either returns a
691 positive value for a simple escape such as \n, or a negative value which
692 encodes one of the more complicated things such as \d. A backreference to group
693 n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When
694 UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,
695 ptr is pointing at the \. On exit, it is on the final character of the escape
696 sequence.
697
698 Arguments:
699 ptrptr points to the pattern position pointer
700 errorcodeptr points to the errorcode variable
701 bracount number of previous extracting brackets
702 options the options bits
703 isclass TRUE if inside a character class
704
705 Returns: zero or positive => a data character
706 negative => a special escape sequence
707 on error, errorcodeptr is set
708 */
709
710 static int
711 check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
712 int options, BOOL isclass)
713 {
714 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
715 BOOL utf = (options & PCRE_UTF8) != 0;
716 const pcre_uchar *ptr = *ptrptr + 1;
717 pcre_int32 c;
718 int i;
719
720 GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
721 ptr--; /* Set pointer back to the last byte */
722
723 /* If backslash is at the end of the pattern, it's an error. */
724
725 if (c == 0) *errorcodeptr = ERR1;
726
727 /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
728 in a table. A non-zero result is something that can be returned immediately.
729 Otherwise further processing may be required. */
730
731 #ifndef EBCDIC /* ASCII/UTF-8 coding */
732 /* Not alphanumeric */
733 else if (c < CHAR_0 || c > CHAR_z) {}
734 else if ((i = escapes[c - CHAR_0]) != 0) c = i;
735
736 #else /* EBCDIC coding */
737 /* Not alphanumeric */
738 else if (c < 'a' || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
739 else if ((i = escapes[c - 0x48]) != 0) c = i;
740 #endif
741
742 /* Escapes that need further processing, or are illegal. */
743
744 else
745 {
746 const pcre_uchar *oldptr;
747 BOOL braced, negated;
748
749 switch (c)
750 {
751 /* A number of Perl escapes are not handled by PCRE. We give an explicit
752 error. */
753
754 case CHAR_l:
755 case CHAR_L:
756 *errorcodeptr = ERR37;
757 break;
758
759 case CHAR_u:
760 if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
761 {
762 /* In JavaScript, \u must be followed by four hexadecimal numbers.
763 Otherwise it is a lowercase u letter. */
764 if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
765 && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
766 && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
767 && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
768 {
769 c = 0;
770 for (i = 0; i < 4; ++i)
771 {
772 register int cc = *(++ptr);
773 #ifndef EBCDIC /* ASCII/UTF-8 coding */
774 if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
775 c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
776 #else /* EBCDIC coding */
777 if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
778 c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
779 #endif
780 }
781 }
782 }
783 else
784 *errorcodeptr = ERR37;
785 break;
786
787 case CHAR_U:
788 /* In JavaScript, \U is an uppercase U letter. */
789 if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
790 break;
791
792 /* In a character class, \g is just a literal "g". Outside a character
793 class, \g must be followed by one of a number of specific things:
794
795 (1) A number, either plain or braced. If positive, it is an absolute
796 backreference. If negative, it is a relative backreference. This is a Perl
797 5.10 feature.
798
799 (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
800 is part of Perl's movement towards a unified syntax for back references. As
801 this is synonymous with \k{name}, we fudge it up by pretending it really
802 was \k.
803
804 (3) For Oniguruma compatibility we also support \g followed by a name or a
805 number either in angle brackets or in single quotes. However, these are
806 (possibly recursive) subroutine calls, _not_ backreferences. Just return
807 the -ESC_g code (cf \k). */
808
809 case CHAR_g:
810 if (isclass) break;
811 if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
812 {
813 c = -ESC_g;
814 break;
815 }
816
817 /* Handle the Perl-compatible cases */
818
819 if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
820 {
821 const pcre_uchar *p;
822 for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
823 if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
824 if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
825 {
826 c = -ESC_k;
827 break;
828 }
829 braced = TRUE;
830 ptr++;
831 }
832 else braced = FALSE;
833
834 if (ptr[1] == CHAR_MINUS)
835 {
836 negated = TRUE;
837 ptr++;
838 }
839 else negated = FALSE;
840
841 /* The integer range is limited by the machine's int representation. */
842 c = 0;
843 while (IS_DIGIT(ptr[1]))
844 {
845 if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
846 {
847 c = -1;
848 break;
849 }
850 c = c * 10 + *(++ptr) - CHAR_0;
851 }
852 if (((unsigned int)c) > INT_MAX) /* Integer overflow */
853 {
854 while (IS_DIGIT(ptr[1]))
855 ptr++;
856 *errorcodeptr = ERR61;
857 break;
858 }
859
860 if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
861 {
862 *errorcodeptr = ERR57;
863 break;
864 }
865
866 if (c == 0)
867 {
868 *errorcodeptr = ERR58;
869 break;
870 }
871
872 if (negated)
873 {
874 if (c > bracount)
875 {
876 *errorcodeptr = ERR15;
877 break;
878 }
879 c = bracount - (c - 1);
880 }
881
882 c = -(ESC_REF + c);
883 break;
884
885 /* The handling of escape sequences consisting of a string of digits
886 starting with one that is not zero is not straightforward. By experiment,
887 the way Perl works seems to be as follows:
888
889 Outside a character class, the digits are read as a decimal number. If the
890 number is less than 10, or if there are that many previous extracting
891 left brackets, then it is a back reference. Otherwise, up to three octal
892 digits are read to form an escaped byte. Thus \123 is likely to be octal
893 123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
894 value is greater than 377, the least significant 8 bits are taken. Inside a
895 character class, \ followed by a digit is always an octal number. */
896
897 case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
898 case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
899
900 if (!isclass)
901 {
902 oldptr = ptr;
903 /* The integer range is limited by the machine's int representation. */
904 c -= CHAR_0;
905 while (IS_DIGIT(ptr[1]))
906 {
907 if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
908 {
909 c = -1;
910 break;
911 }
912 c = c * 10 + *(++ptr) - CHAR_0;
913 }
914 if (((unsigned int)c) > INT_MAX) /* Integer overflow */
915 {
916 while (IS_DIGIT(ptr[1]))
917 ptr++;
918 *errorcodeptr = ERR61;
919 break;
920 }
921 if (c < 10 || c <= bracount)
922 {
923 c = -(ESC_REF + c);
924 break;
925 }
926 ptr = oldptr; /* Put the pointer back and fall through */
927 }
928
929 /* Handle an octal number following \. If the first digit is 8 or 9, Perl
930 generates a binary zero byte and treats the digit as a following literal.
931 Thus we have to pull back the pointer by one. */
932
933 if ((c = *ptr) >= CHAR_8)
934 {
935 ptr--;
936 c = 0;
937 break;
938 }
939
940 /* \0 always starts an octal number, but we may drop through to here with a
941 larger first octal digit. The original code used just to take the least
942 significant 8 bits of octal numbers (I think this is what early Perls used
943 to do). Nowadays we allow for larger numbers in UTF-8 mode, but no more
944 than 3 octal digits. */
945
946 case CHAR_0:
947 c -= CHAR_0;
948 while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
949 c = c * 8 + *(++ptr) - CHAR_0;
950 if (!utf && c > 0xff) *errorcodeptr = ERR51;
951 break;
952
953 /* \x is complicated. \x{ddd} is a character number which can be greater
954 than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.
955 If not, { is treated as a data character. */
956
957 case CHAR_x:
958 if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
959 {
960 /* In JavaScript, \x must be followed by two hexadecimal numbers.
961 Otherwise it is a lowercase x letter. */
962 if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
963 && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
964 {
965 c = 0;
966 for (i = 0; i < 2; ++i)
967 {
968 register int cc = *(++ptr);
969 #ifndef EBCDIC /* ASCII/UTF-8 coding */
970 if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
971 c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
972 #else /* EBCDIC coding */
973 if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
974 c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
975 #endif
976 }
977 }
978 break;
979 }
980
981 if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
982 {
983 const pcre_uchar *pt = ptr + 2;
984
985 c = 0;
986 while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
987 {
988 register int cc = *pt++;
989 if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
990
991 #ifndef EBCDIC /* ASCII/UTF-8 coding */
992 if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
993 c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
994 #else /* EBCDIC coding */
995 if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
996 c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
997 #endif
998
999 #ifdef COMPILE_PCRE8
1000 if (c > (utf ? 0x10ffff : 0xff)) { c = -1; break; }
1001 #else
1002 #ifdef COMPILE_PCRE16
1003 if (c > (utf ? 0x10ffff : 0xffff)) { c = -1; break; }
1004 #endif
1005 #endif
1006 }
1007
1008 if (c < 0)
1009 {
1010 while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1011 *errorcodeptr = ERR34;
1012 }
1013
1014 if (*pt == CHAR_RIGHT_CURLY_BRACKET)
1015 {
1016 if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR71;
1017 ptr = pt;
1018 break;
1019 }
1020
1021 /* If the sequence of hex digits does not end with '}', then we don't
1022 recognize this construct; fall through to the normal \x handling. */
1023 }
1024
1025 /* Read just a single-byte hex-defined char */
1026
1027 c = 0;
1028 while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1029 {
1030 int cc; /* Some compilers don't like */
1031 cc = *(++ptr); /* ++ in initializers */
1032 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1033 if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
1034 c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1035 #else /* EBCDIC coding */
1036 if (cc <= CHAR_z) cc += 64; /* Convert to upper case */
1037 c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1038 #endif
1039 }
1040 break;
1041
1042 /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
1043 An error is given if the byte following \c is not an ASCII character. This
1044 coding is ASCII-specific, but then the whole concept of \cx is
1045 ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1046
1047 case CHAR_c:
1048 c = *(++ptr);
1049 if (c == 0)
1050 {
1051 *errorcodeptr = ERR2;
1052 break;
1053 }
1054 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1055 if (c > 127) /* Excludes all non-ASCII in either mode */
1056 {
1057 *errorcodeptr = ERR68;
1058 break;
1059 }
1060 if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1061 c ^= 0x40;
1062 #else /* EBCDIC coding */
1063 if (c >= CHAR_a && c <= CHAR_z) c += 64;
1064 c ^= 0xC0;
1065 #endif
1066 break;
1067
1068 /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
1069 other alphanumeric following \ is an error if PCRE_EXTRA was set;
1070 otherwise, for Perl compatibility, it is a literal. This code looks a bit
1071 odd, but there used to be some cases other than the default, and there may
1072 be again in future, so I haven't "optimized" it. */
1073
1074 default:
1075 if ((options & PCRE_EXTRA) != 0) switch(c)
1076 {
1077 default:
1078 *errorcodeptr = ERR3;
1079 break;
1080 }
1081 break;
1082 }
1083 }
1084
1085 /* Perl supports \N{name} for character names, as well as plain \N for "not
1086 newline". PCRE does not support \N{name}. However, it does support
1087 quantification such as \N{2,3}. */
1088
1089 if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1090 !is_counted_repeat(ptr+2))
1091 *errorcodeptr = ERR37;
1092
1093 /* If PCRE_UCP is set, we change the values for \d etc. */
1094
1095 if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)
1096 c -= (ESC_DU - ESC_D);
1097
1098 /* Set the pointer to the final character before returning. */
1099
1100 *ptrptr = ptr;
1101 return c;
1102 }
1103
1104
1105
1106 #ifdef SUPPORT_UCP
1107 /*************************************************
1108 * Handle \P and \p *
1109 *************************************************/
1110
1111 /* This function is called after \P or \p has been encountered, provided that
1112 PCRE is compiled with support for Unicode properties. On entry, ptrptr is
1113 pointing at the P or p. On exit, it is pointing at the final character of the
1114 escape sequence.
1115
1116 Argument:
1117 ptrptr points to the pattern position pointer
1118 negptr points to a boolean that is set TRUE for negation else FALSE
1119 dptr points to an int that is set to the detailed property value
1120 errorcodeptr points to the error code variable
1121
1122 Returns: type value from ucp_type_table, or -1 for an invalid type
1123 */
1124
1125 static int
1126 get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1127 {
1128 int c, i, bot, top;
1129 const pcre_uchar *ptr = *ptrptr;
1130 pcre_uchar name[32];
1131
1132 c = *(++ptr);
1133 if (c == 0) goto ERROR_RETURN;
1134
1135 *negptr = FALSE;
1136
1137 /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
1138 negation. */
1139
1140 if (c == CHAR_LEFT_CURLY_BRACKET)
1141 {
1142 if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1143 {
1144 *negptr = TRUE;
1145 ptr++;
1146 }
1147 for (i = 0; i < (int)sizeof(name) - 1; i++)
1148 {
1149 c = *(++ptr);
1150 if (c == 0) goto ERROR_RETURN;
1151 if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1152 name[i] = c;
1153 }
1154 if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
1155 name[i] = 0;
1156 }
1157
1158 /* Otherwise there is just one following character */
1159
1160 else
1161 {
1162 name[0] = c;
1163 name[1] = 0;
1164 }
1165
1166 *ptrptr = ptr;
1167
1168 /* Search for a recognized property name using binary chop */
1169
1170 bot = 0;
1171 top = PRIV(utt_size);
1172
1173 while (bot < top)
1174 {
1175 i = (bot + top) >> 1;
1176 c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1177 if (c == 0)
1178 {
1179 *dptr = PRIV(utt)[i].value;
1180 return PRIV(utt)[i].type;
1181 }
1182 if (c > 0) bot = i + 1; else top = i;
1183 }
1184
1185 *errorcodeptr = ERR47;
1186 *ptrptr = ptr;
1187 return -1;
1188
1189 ERROR_RETURN:
1190 *errorcodeptr = ERR46;
1191 *ptrptr = ptr;
1192 return -1;
1193 }
1194 #endif
1195
1196
1197
1198
1199 /*************************************************
1200 * Read repeat counts *
1201 *************************************************/
1202
1203 /* Read an item of the form {n,m} and return the values. This is called only
1204 after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
1205 so the syntax is guaranteed to be correct, but we need to check the values.
1206
1207 Arguments:
1208 p pointer to first char after '{'
1209 minp pointer to int for min
1210 maxp pointer to int for max
1211 returned as -1 if no max
1212 errorcodeptr points to error code variable
1213
1214 Returns: pointer to '}' on success;
1215 current ptr on error, with errorcodeptr set non-zero
1216 */
1217
1218 static const pcre_uchar *
1219 read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr)
1220 {
1221 int min = 0;
1222 int max = -1;
1223
1224 /* Read the minimum value and do a paranoid check: a negative value indicates
1225 an integer overflow. */
1226
1227 while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;
1228 if (min < 0 || min > 65535)
1229 {
1230 *errorcodeptr = ERR5;
1231 return p;
1232 }
1233
1234 /* Read the maximum value if there is one, and again do a paranoid on its size.
1235 Also, max must not be less than min. */
1236
1237 if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
1238 {
1239 if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1240 {
1241 max = 0;
1242 while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;
1243 if (max < 0 || max > 65535)
1244 {
1245 *errorcodeptr = ERR5;
1246 return p;
1247 }
1248 if (max < min)
1249 {
1250 *errorcodeptr = ERR4;
1251 return p;
1252 }
1253 }
1254 }
1255
1256 /* Fill in the required variables, and pass back the pointer to the terminating
1257 '}'. */
1258
1259 *minp = min;
1260 *maxp = max;
1261 return p;
1262 }
1263
1264
1265
1266 /*************************************************
1267 * Subroutine for finding forward reference *
1268 *************************************************/
1269
1270 /* This recursive function is called only from find_parens() below. The
1271 top-level call starts at the beginning of the pattern. All other calls must
1272 start at a parenthesis. It scans along a pattern's text looking for capturing
1273 subpatterns, and counting them. If it finds a named pattern that matches the
1274 name it is given, it returns its number. Alternatively, if the name is NULL, it
1275 returns when it reaches a given numbered subpattern. Recursion is used to keep
1276 track of subpatterns that reset the capturing group numbers - the (?| feature.
1277
1278 This function was originally called only from the second pass, in which we know
1279 that if (?< or (?' or (?P< is encountered, the name will be correctly
1280 terminated because that is checked in the first pass. There is now one call to
1281 this function in the first pass, to check for a recursive back reference by
1282 name (so that we can make the whole group atomic). In this case, we need check
1283 only up to the current position in the pattern, and that is still OK because
1284 and previous occurrences will have been checked. To make this work, the test
1285 for "end of pattern" is a check against cd->end_pattern in the main loop,
1286 instead of looking for a binary zero. This means that the special first-pass
1287 call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1288 processing items within the loop are OK, because afterwards the main loop will
1289 terminate.)
1290
1291 Arguments:
1292 ptrptr address of the current character pointer (updated)
1293 cd compile background data
1294 name name to seek, or NULL if seeking a numbered subpattern
1295 lorn name length, or subpattern number if name is NULL
1296 xmode TRUE if we are in /x mode
1297 utf TRUE if we are in UTF-8 / UTF-16 mode
1298 count pointer to the current capturing subpattern number (updated)
1299
1300 Returns: the number of the named subpattern, or -1 if not found
1301 */
1302
1303 static int
1304 find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,
1305 BOOL xmode, BOOL utf, int *count)
1306 {
1307 pcre_uchar *ptr = *ptrptr;
1308 int start_count = *count;
1309 int hwm_count = start_count;
1310 BOOL dup_parens = FALSE;
1311
1312 /* If the first character is a parenthesis, check on the type of group we are
1313 dealing with. The very first call may not start with a parenthesis. */
1314
1315 if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1316 {
1317 /* Handle specials such as (*SKIP) or (*UTF8) etc. */
1318
1319 if (ptr[1] == CHAR_ASTERISK) ptr += 2;
1320
1321 /* Handle a normal, unnamed capturing parenthesis. */
1322
1323 else if (ptr[1] != CHAR_QUESTION_MARK)
1324 {
1325 *count += 1;
1326 if (name == NULL && *count == lorn) return *count;
1327 ptr++;
1328 }
1329
1330 /* All cases now have (? at the start. Remember when we are in a group
1331 where the parenthesis numbers are duplicated. */
1332
1333 else if (ptr[2] == CHAR_VERTICAL_LINE)
1334 {
1335 ptr += 3;
1336 dup_parens = TRUE;
1337 }
1338
1339 /* Handle comments; all characters are allowed until a ket is reached. */
1340
1341 else if (ptr[2] == CHAR_NUMBER_SIGN)
1342 {
1343 for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;
1344 goto FAIL_EXIT;
1345 }
1346
1347 /* Handle a condition. If it is an assertion, just carry on so that it
1348 is processed as normal. If not, skip to the closing parenthesis of the
1349 condition (there can't be any nested parens). */
1350
1351 else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
1352 {
1353 ptr += 2;
1354 if (ptr[1] != CHAR_QUESTION_MARK)
1355 {
1356 while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
1357 if (*ptr != 0) ptr++;
1358 }
1359 }
1360
1361 /* Start with (? but not a condition. */
1362
1363 else
1364 {
1365 ptr += 2;
1366 if (*ptr == CHAR_P) ptr++; /* Allow optional P */
1367
1368 /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */
1369
1370 if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&
1371 ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1372 {
1373 int term;
1374 const pcre_uchar *thisname;
1375 *count += 1;
1376 if (name == NULL && *count == lorn) return *count;
1377 term = *ptr++;
1378 if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;
1379 thisname = ptr;
1380 while (*ptr != term) ptr++;
1381 if (name != NULL && lorn == ptr - thisname &&
1382 STRNCMP_UC_UC(name, thisname, lorn) == 0)
1383 return *count;
1384 term++;
1385 }
1386 }
1387 }
1388
1389 /* Past any initial parenthesis handling, scan for parentheses or vertical
1390 bars. Stop if we get to cd->end_pattern. Note that this is important for the
1391 first-pass call when this value is temporarily adjusted to stop at the current
1392 position. So DO NOT change this to a test for binary zero. */
1393
1394 for (; ptr < cd->end_pattern; ptr++)
1395 {
1396 /* Skip over backslashed characters and also entire \Q...\E */
1397
1398 if (*ptr == CHAR_BACKSLASH)
1399 {
1400 if (*(++ptr) == 0) goto FAIL_EXIT;
1401 if (*ptr == CHAR_Q) for (;;)
1402 {
1403 while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
1404 if (*ptr == 0) goto FAIL_EXIT;
1405 if (*(++ptr) == CHAR_E) break;
1406 }
1407 continue;
1408 }
1409
1410 /* Skip over character classes; this logic must be similar to the way they
1411 are handled for real. If the first character is '^', skip it. Also, if the
1412 first few characters (either before or after ^) are \Q\E or \E we skip them
1413 too. This makes for compatibility with Perl. Note the use of STR macros to
1414 encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */
1415
1416 if (*ptr == CHAR_LEFT_SQUARE_BRACKET)
1417 {
1418 BOOL negate_class = FALSE;
1419 for (;;)
1420 {
1421 if (ptr[1] == CHAR_BACKSLASH)
1422 {
1423 if (ptr[2] == CHAR_E)
1424 ptr+= 2;
1425 else if (STRNCMP_UC_C8(ptr + 2,
1426 STR_Q STR_BACKSLASH STR_E, 3) == 0)
1427 ptr += 4;
1428 else
1429 break;
1430 }
1431 else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1432 {
1433 negate_class = TRUE;
1434 ptr++;
1435 }
1436 else break;
1437 }
1438
1439 /* If the next character is ']', it is a data character that must be
1440 skipped, except in JavaScript compatibility mode. */
1441
1442 if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&
1443 (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)
1444 ptr++;
1445
1446 while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)
1447 {
1448 if (*ptr == 0) return -1;
1449 if (*ptr == CHAR_BACKSLASH)
1450 {
1451 if (*(++ptr) == 0) goto FAIL_EXIT;
1452 if (*ptr == CHAR_Q) for (;;)
1453 {
1454 while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
1455 if (*ptr == 0) goto FAIL_EXIT;
1456 if (*(++ptr) == CHAR_E) break;
1457 }
1458 continue;
1459 }
1460 }
1461 continue;
1462 }
1463
1464 /* Skip comments in /x mode */
1465
1466 if (xmode && *ptr == CHAR_NUMBER_SIGN)
1467 {
1468 ptr++;
1469 while (*ptr != 0)
1470 {
1471 if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1472 ptr++;
1473 #ifdef SUPPORT_UTF
1474 if (utf) FORWARDCHAR(ptr);
1475 #endif
1476 }
1477 if (*ptr == 0) goto FAIL_EXIT;
1478 continue;
1479 }
1480
1481 /* Check for the special metacharacters */
1482
1483 if (*ptr == CHAR_LEFT_PARENTHESIS)
1484 {
1485 int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, count);
1486 if (rc > 0) return rc;
1487 if (*ptr == 0) goto FAIL_EXIT;
1488 }
1489
1490 else if (*ptr == CHAR_RIGHT_PARENTHESIS)
1491 {
1492 if (dup_parens && *count < hwm_count) *count = hwm_count;
1493 goto FAIL_EXIT;
1494 }
1495
1496 else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
1497 {
1498 if (*count > hwm_count) hwm_count = *count;
1499 *count = start_count;
1500 }
1501 }
1502
1503 FAIL_EXIT:
1504 *ptrptr = ptr;
1505 return -1;
1506 }
1507
1508
1509
1510
1511 /*************************************************
1512 * Find forward referenced subpattern *
1513 *************************************************/
1514
1515 /* This function scans along a pattern's text looking for capturing
1516 subpatterns, and counting them. If it finds a named pattern that matches the
1517 name it is given, it returns its number. Alternatively, if the name is NULL, it
1518 returns when it reaches a given numbered subpattern. This is used for forward
1519 references to subpatterns. We used to be able to start this scan from the
1520 current compiling point, using the current count value from cd->bracount, and
1521 do it all in a single loop, but the addition of the possibility of duplicate
1522 subpattern numbers means that we have to scan from the very start, in order to
1523 take account of such duplicates, and to use a recursive function to keep track
1524 of the different types of group.
1525
1526 Arguments:
1527 cd compile background data
1528 name name to seek, or NULL if seeking a numbered subpattern
1529 lorn name length, or subpattern number if name is NULL
1530 xmode TRUE if we are in /x mode
1531 utf TRUE if we are in UTF-8 / UTF-16 mode
1532
1533 Returns: the number of the found subpattern, or -1 if not found
1534 */
1535
1536 static int
1537 find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,
1538 BOOL utf)
1539 {
1540 pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1541 int count = 0;
1542 int rc;
1543
1544 /* If the pattern does not start with an opening parenthesis, the first call
1545 to find_parens_sub() will scan right to the end (if necessary). However, if it
1546 does start with a parenthesis, find_parens_sub() will return when it hits the
1547 matching closing parens. That is why we have to have a loop. */
1548
1549 for (;;)
1550 {
1551 rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, &count);
1552 if (rc > 0 || *ptr++ == 0) break;
1553 }
1554
1555 return rc;
1556 }
1557
1558
1559
1560
1561 /*************************************************
1562 * Find first significant op code *
1563 *************************************************/
1564
1565 /* This is called by several functions that scan a compiled expression looking
1566 for a fixed first character, or an anchoring op code etc. It skips over things
1567 that do not influence this. For some calls, it makes sense to skip negative
1568 forward and all backward assertions, and also the \b assertion; for others it
1569 does not.
1570
1571 Arguments:
1572 code pointer to the start of the group
1573 skipassert TRUE if certain assertions are to be skipped
1574
1575 Returns: pointer to the first significant opcode
1576 */
1577
1578 static const pcre_uchar*
1579 first_significant_code(const pcre_uchar *code, BOOL skipassert)
1580 {
1581 for (;;)
1582 {
1583 switch ((int)*code)
1584 {
1585 case OP_ASSERT_NOT:
1586 case OP_ASSERTBACK:
1587 case OP_ASSERTBACK_NOT:
1588 if (!skipassert) return code;
1589 do code += GET(code, 1); while (*code == OP_ALT);
1590 code += PRIV(OP_lengths)[*code];
1591 break;
1592
1593 case OP_WORD_BOUNDARY:
1594 case OP_NOT_WORD_BOUNDARY:
1595 if (!skipassert) return code;
1596 /* Fall through */
1597
1598 case OP_CALLOUT:
1599 case OP_CREF:
1600 case OP_NCREF:
1601 case OP_RREF:
1602 case OP_NRREF:
1603 case OP_DEF:
1604 code += PRIV(OP_lengths)[*code];
1605 break;
1606
1607 default:
1608 return code;
1609 }
1610 }
1611 /* Control never reaches here */
1612 }
1613
1614
1615
1616
1617 /*************************************************
1618 * Find the fixed length of a branch *
1619 *************************************************/
1620
1621 /* Scan a branch and compute the fixed length of subject that will match it,
1622 if the length is fixed. This is needed for dealing with backward assertions.
1623 In UTF8 mode, the result is in characters rather than bytes. The branch is
1624 temporarily terminated with OP_END when this function is called.
1625
1626 This function is called when a backward assertion is encountered, so that if it
1627 fails, the error message can point to the correct place in the pattern.
1628 However, we cannot do this when the assertion contains subroutine calls,
1629 because they can be forward references. We solve this by remembering this case
1630 and doing the check at the end; a flag specifies which mode we are running in.
1631
1632 Arguments:
1633 code points to the start of the pattern (the bracket)
1634 utf TRUE in UTF-8 / UTF-16 mode
1635 atend TRUE if called when the pattern is complete
1636 cd the "compile data" structure
1637
1638 Returns: the fixed length,
1639 or -1 if there is no fixed length,
1640 or -2 if \C was encountered (in UTF-8 mode only)
1641 or -3 if an OP_RECURSE item was encountered and atend is FALSE
1642 or -4 if an unknown opcode was encountered (internal error)
1643 */
1644
1645 static int
1646 find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1647 {
1648 int length = -1;
1649
1650 register int branchlength = 0;
1651 register pcre_uchar *cc = code + 1 + LINK_SIZE;
1652
1653 /* Scan along the opcodes for this branch. If we get to the end of the
1654 branch, check the length against that of the other branches. */
1655
1656 for (;;)
1657 {
1658 int d;
1659 pcre_uchar *ce, *cs;
1660 register int op = *cc;
1661 switch (op)
1662 {
1663 /* We only need to continue for OP_CBRA (normal capturing bracket) and
1664 OP_BRA (normal non-capturing bracket) because the other variants of these
1665 opcodes are all concerned with unlimited repeated groups, which of course
1666 are not of fixed length. */
1667
1668 case OP_CBRA:
1669 case OP_BRA:
1670 case OP_ONCE:
1671 case OP_ONCE_NC:
1672 case OP_COND:
1673 d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd);
1674 if (d < 0) return d;
1675 branchlength += d;
1676 do cc += GET(cc, 1); while (*cc == OP_ALT);
1677 cc += 1 + LINK_SIZE;
1678 break;
1679
1680 /* Reached end of a branch; if it's a ket it is the end of a nested call.
1681 If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1682 an ALT. If it is END it's the end of the outer call. All can be handled by
1683 the same code. Note that we must not include the OP_KETRxxx opcodes here,
1684 because they all imply an unlimited repeat. */
1685
1686 case OP_ALT:
1687 case OP_KET:
1688 case OP_END:
1689 case OP_ACCEPT:
1690 case OP_ASSERT_ACCEPT:
1691 if (length < 0) length = branchlength;
1692 else if (length != branchlength) return -1;
1693 if (*cc != OP_ALT) return length;
1694 cc += 1 + LINK_SIZE;
1695 branchlength = 0;
1696 break;
1697
1698 /* A true recursion implies not fixed length, but a subroutine call may
1699 be OK. If the subroutine is a forward reference, we can't deal with
1700 it until the end of the pattern, so return -3. */
1701
1702 case OP_RECURSE:
1703 if (!atend) return -3;
1704 cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1); /* Start subpattern */
1705 do ce += GET(ce, 1); while (*ce == OP_ALT); /* End subpattern */
1706 if (cc > cs && cc < ce) return -1; /* Recursion */
1707 d = find_fixedlength(cs + 2, utf, atend, cd);
1708 if (d < 0) return d;
1709 branchlength += d;
1710 cc += 1 + LINK_SIZE;
1711 break;
1712
1713 /* Skip over assertive subpatterns */
1714
1715 case OP_ASSERT:
1716 case OP_ASSERT_NOT:
1717 case OP_ASSERTBACK:
1718 case OP_ASSERTBACK_NOT:
1719 do cc += GET(cc, 1); while (*cc == OP_ALT);
1720 /* Fall through */
1721
1722 /* Skip over things that don't match chars */
1723
1724 case OP_MARK:
1725 case OP_PRUNE_ARG:
1726 case OP_SKIP_ARG:
1727 case OP_THEN_ARG:
1728 cc += cc[1] + PRIV(OP_lengths)[*cc];
1729 break;
1730
1731 case OP_CALLOUT:
1732 case OP_CIRC:
1733 case OP_CIRCM:
1734 case OP_CLOSE:
1735 case OP_COMMIT:
1736 case OP_CREF:
1737 case OP_DEF:
1738 case OP_DOLL:
1739 case OP_DOLLM:
1740 case OP_EOD:
1741 case OP_EODN:
1742 case OP_FAIL:
1743 case OP_NCREF:
1744 case OP_NRREF:
1745 case OP_NOT_WORD_BOUNDARY:
1746 case OP_PRUNE:
1747 case OP_REVERSE:
1748 case OP_RREF:
1749 case OP_SET_SOM:
1750 case OP_SKIP:
1751 case OP_SOD:
1752 case OP_SOM:
1753 case OP_THEN:
1754 case OP_WORD_BOUNDARY:
1755 cc += PRIV(OP_lengths)[*cc];
1756 break;
1757
1758 /* Handle literal characters */
1759
1760 case OP_CHAR:
1761 case OP_CHARI:
1762 case OP_NOT:
1763 case OP_NOTI:
1764 branchlength++;
1765 cc += 2;
1766 #ifdef SUPPORT_UTF
1767 if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1768 #endif
1769 break;
1770
1771 /* Handle exact repetitions. The count is already in characters, but we
1772 need to skip over a multibyte character in UTF8 mode. */
1773
1774 case OP_EXACT:
1775 case OP_EXACTI:
1776 case OP_NOTEXACT:
1777 case OP_NOTEXACTI:
1778 branchlength += GET2(cc,1);
1779 cc += 2 + IMM2_SIZE;
1780 #ifdef SUPPORT_UTF
1781 if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1782 #endif
1783 break;
1784
1785 case OP_TYPEEXACT:
1786 branchlength += GET2(cc,1);
1787 if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
1788 cc += 1 + IMM2_SIZE + 1;
1789 break;
1790
1791 /* Handle single-char matchers */
1792
1793 case OP_PROP:
1794 case OP_NOTPROP:
1795 cc += 2;
1796 /* Fall through */
1797
1798 case OP_HSPACE:
1799 case OP_VSPACE:
1800 case OP_NOT_HSPACE:
1801 case OP_NOT_VSPACE:
1802 case OP_NOT_DIGIT:
1803 case OP_DIGIT:
1804 case OP_NOT_WHITESPACE:
1805 case OP_WHITESPACE:
1806 case OP_NOT_WORDCHAR:
1807 case OP_WORDCHAR:
1808 case OP_ANY:
1809 case OP_ALLANY:
1810 branchlength++;
1811 cc++;
1812 break;
1813
1814 /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1815 otherwise \C is coded as OP_ALLANY. */
1816
1817 case OP_ANYBYTE:
1818 return -2;
1819
1820 /* Check a class for variable quantification */
1821
1822 #if defined SUPPORT_UTF || defined COMPILE_PCRE16
1823 case OP_XCLASS:
1824 cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];
1825 /* Fall through */
1826 #endif
1827
1828 case OP_CLASS:
1829 case OP_NCLASS:
1830 cc += PRIV(OP_lengths)[OP_CLASS];
1831
1832 switch (*cc)
1833 {
1834 case OP_CRPLUS:
1835 case OP_CRMINPLUS:
1836 case OP_CRSTAR:
1837 case OP_CRMINSTAR:
1838 case OP_CRQUERY:
1839 case OP_CRMINQUERY:
1840 return -1;
1841
1842 case OP_CRRANGE:
1843 case OP_CRMINRANGE:
1844 if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1845 branchlength += GET2(cc,1);
1846 cc += 1 + 2 * IMM2_SIZE;
1847 break;
1848
1849 default:
1850 branchlength++;
1851 }
1852 break;
1853
1854 /* Anything else is variable length */
1855
1856 case OP_ANYNL:
1857 case OP_BRAMINZERO:
1858 case OP_BRAPOS:
1859 case OP_BRAPOSZERO:
1860 case OP_BRAZERO:
1861 case OP_CBRAPOS:
1862 case OP_EXTUNI:
1863 case OP_KETRMAX:
1864 case OP_KETRMIN:
1865 case OP_KETRPOS:
1866 case OP_MINPLUS:
1867 case OP_MINPLUSI:
1868 case OP_MINQUERY:
1869 case OP_MINQUERYI:
1870 case OP_MINSTAR:
1871 case OP_MINSTARI:
1872 case OP_MINUPTO:
1873 case OP_MINUPTOI:
1874 case OP_NOTMINPLUS:
1875 case OP_NOTMINPLUSI:
1876 case OP_NOTMINQUERY:
1877 case OP_NOTMINQUERYI:
1878 case OP_NOTMINSTAR:
1879 case OP_NOTMINSTARI:
1880 case OP_NOTMINUPTO:
1881 case OP_NOTMINUPTOI:
1882 case OP_NOTPLUS:
1883 case OP_NOTPLUSI:
1884 case OP_NOTPOSPLUS:
1885 case OP_NOTPOSPLUSI:
1886 case OP_NOTPOSQUERY:
1887 case OP_NOTPOSQUERYI:
1888 case OP_NOTPOSSTAR:
1889 case OP_NOTPOSSTARI:
1890 case OP_NOTPOSUPTO:
1891 case OP_NOTPOSUPTOI:
1892 case OP_NOTQUERY:
1893 case OP_NOTQUERYI:
1894 case OP_NOTSTAR:
1895 case OP_NOTSTARI:
1896 case OP_NOTUPTO:
1897 case OP_NOTUPTOI:
1898 case OP_PLUS:
1899 case OP_PLUSI:
1900 case OP_POSPLUS:
1901 case OP_POSPLUSI:
1902 case OP_POSQUERY:
1903 case OP_POSQUERYI:
1904 case OP_POSSTAR:
1905 case OP_POSSTARI:
1906 case OP_POSUPTO:
1907 case OP_POSUPTOI:
1908 case OP_QUERY:
1909 case OP_QUERYI:
1910 case OP_REF:
1911 case OP_REFI:
1912 case OP_SBRA:
1913 case OP_SBRAPOS:
1914 case OP_SCBRA:
1915 case OP_SCBRAPOS:
1916 case OP_SCOND:
1917 case OP_SKIPZERO:
1918 case OP_STAR:
1919 case OP_STARI:
1920 case OP_TYPEMINPLUS:
1921 case OP_TYPEMINQUERY:
1922 case OP_TYPEMINSTAR:
1923 case OP_TYPEMINUPTO:
1924 case OP_TYPEPLUS:
1925 case OP_TYPEPOSPLUS:
1926 case OP_TYPEPOSQUERY:
1927 case OP_TYPEPOSSTAR:
1928 case OP_TYPEPOSUPTO:
1929 case OP_TYPEQUERY:
1930 case OP_TYPESTAR:
1931 case OP_TYPEUPTO:
1932 case OP_UPTO:
1933 case OP_UPTOI:
1934 return -1;
1935
1936 /* Catch unrecognized opcodes so that when new ones are added they
1937 are not forgotten, as has happened in the past. */
1938
1939 default:
1940 return -4;
1941 }
1942 }
1943 /* Control never gets here */
1944 }
1945
1946
1947
1948
1949 /*************************************************
1950 * Scan compiled regex for specific bracket *
1951 *************************************************/
1952
1953 /* This little function scans through a compiled pattern until it finds a
1954 capturing bracket with the given number, or, if the number is negative, an
1955 instance of OP_REVERSE for a lookbehind. The function is global in the C sense
1956 so that it can be called from pcre_study() when finding the minimum matching
1957 length.
1958
1959 Arguments:
1960 code points to start of expression
1961 utf TRUE in UTF-8 / UTF-16 mode
1962 number the required bracket number or negative to find a lookbehind
1963
1964 Returns: pointer to the opcode for the bracket, or NULL if not found
1965 */
1966
1967 const pcre_uchar *
1968 PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
1969 {
1970 for (;;)
1971 {
1972 register int c = *code;
1973
1974 if (c == OP_END) return NULL;
1975
1976 /* XCLASS is used for classes that cannot be represented just by a bit
1977 map. This includes negated single high-valued characters. The length in
1978 the table is zero; the actual length is stored in the compiled code. */
1979
1980 if (c == OP_XCLASS) code += GET(code, 1);
1981
1982 /* Handle recursion */
1983
1984 else if (c == OP_REVERSE)
1985 {
1986 if (number < 0) return (pcre_uchar *)code;
1987 code += PRIV(OP_lengths)[c];
1988 }
1989
1990 /* Handle capturing bracket */
1991
1992 else if (c == OP_CBRA || c == OP_SCBRA ||
1993 c == OP_CBRAPOS || c == OP_SCBRAPOS)
1994 {
1995 int n = GET2(code, 1+LINK_SIZE);
1996 if (n == number) return (pcre_uchar *)code;
1997 code += PRIV(OP_lengths)[c];
1998 }
1999
2000 /* Otherwise, we can get the item's length from the table, except that for
2001 repeated character types, we have to test for \p and \P, which have an extra
2002 two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2003 must add in its length. */
2004
2005 else
2006 {
2007 switch(c)
2008 {
2009 case OP_TYPESTAR:
2010 case OP_TYPEMINSTAR:
2011 case OP_TYPEPLUS:
2012 case OP_TYPEMINPLUS:
2013 case OP_TYPEQUERY:
2014 case OP_TYPEMINQUERY:
2015 case OP_TYPEPOSSTAR:
2016 case OP_TYPEPOSPLUS:
2017 case OP_TYPEPOSQUERY:
2018 if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2019 break;
2020
2021 case OP_TYPEUPTO:
2022 case OP_TYPEMINUPTO:
2023 case OP_TYPEEXACT:
2024 case OP_TYPEPOSUPTO:
2025 if (code[1 + IMM2_SIZE] == OP_PROP
2026 || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2027 break;
2028
2029 case OP_MARK:
2030 case OP_PRUNE_ARG:
2031 case OP_SKIP_ARG:
2032 code += code[1];
2033 break;
2034
2035 case OP_THEN_ARG:
2036 code += code[1];
2037 break;
2038 }
2039
2040 /* Add in the fixed length from the table */
2041
2042 code += PRIV(OP_lengths)[c];
2043
2044 /* In UTF-8 mode, opcodes that are followed by a character may be followed by
2045 a multi-byte character. The length in the table is a minimum, so we have to
2046 arrange to skip the extra bytes. */
2047
2048 #ifdef SUPPORT_UTF
2049 if (utf) switch(c)
2050 {
2051 case OP_CHAR:
2052 case OP_CHARI:
2053 case OP_EXACT:
2054 case OP_EXACTI:
2055 case OP_UPTO:
2056 case OP_UPTOI:
2057 case OP_MINUPTO:
2058 case OP_MINUPTOI:
2059 case OP_POSUPTO:
2060 case OP_POSUPTOI:
2061 case OP_STAR:
2062 case OP_STARI:
2063 case OP_MINSTAR:
2064 case OP_MINSTARI:
2065 case OP_POSSTAR:
2066 case OP_POSSTARI:
2067 case OP_PLUS:
2068 case OP_PLUSI:
2069 case OP_MINPLUS:
2070 case OP_MINPLUSI:
2071 case OP_POSPLUS:
2072 case OP_POSPLUSI:
2073 case OP_QUERY:
2074 case OP_QUERYI:
2075 case OP_MINQUERY:
2076 case OP_MINQUERYI:
2077 case OP_POSQUERY:
2078 case OP_POSQUERYI:
2079 if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2080 break;
2081 }
2082 #else
2083 (void)(utf); /* Keep compiler happy by referencing function argument */
2084 #endif
2085 }
2086 }
2087 }
2088
2089
2090
2091 /*************************************************
2092 * Scan compiled regex for recursion reference *
2093 *************************************************/
2094
2095 /* This little function scans through a compiled pattern until it finds an
2096 instance of OP_RECURSE.
2097
2098 Arguments:
2099 code points to start of expression
2100 utf TRUE in UTF-8 / UTF-16 mode
2101
2102 Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
2103 */
2104
2105 static const pcre_uchar *
2106 find_recurse(const pcre_uchar *code, BOOL utf)
2107 {
2108 for (;;)
2109 {
2110 register int c = *code;
2111 if (c == OP_END) return NULL;
2112 if (c == OP_RECURSE) return code;
2113
2114 /* XCLASS is used for classes that cannot be represented just by a bit
2115 map. This includes negated single high-valued characters. The length in
2116 the table is zero; the actual length is stored in the compiled code. */
2117
2118 if (c == OP_XCLASS) code += GET(code, 1);
2119
2120 /* Otherwise, we can get the item's length from the table, except that for
2121 repeated character types, we have to test for \p and \P, which have an extra
2122 two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2123 must add in its length. */
2124
2125 else
2126 {
2127 switch(c)
2128 {
2129 case OP_TYPESTAR:
2130 case OP_TYPEMINSTAR:
2131 case OP_TYPEPLUS:
2132 case OP_TYPEMINPLUS:
2133 case OP_TYPEQUERY:
2134 case OP_TYPEMINQUERY:
2135 case OP_TYPEPOSSTAR:
2136 case OP_TYPEPOSPLUS:
2137 case OP_TYPEPOSQUERY:
2138 if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2139 break;
2140
2141 case OP_TYPEPOSUPTO:
2142 case OP_TYPEUPTO:
2143 case OP_TYPEMINUPTO:
2144 case OP_TYPEEXACT:
2145 if (code[1 + IMM2_SIZE] == OP_PROP
2146 || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2147 break;
2148
2149 case OP_MARK:
2150 case OP_PRUNE_ARG:
2151 case OP_SKIP_ARG:
2152 code += code[1];
2153 break;
2154
2155 case OP_THEN_ARG:
2156 code += code[1];
2157 break;
2158 }
2159
2160 /* Add in the fixed length from the table */
2161
2162 code += PRIV(OP_lengths)[c];
2163
2164 /* In UTF-8 mode, opcodes that are followed by a character may be followed
2165 by a multi-byte character. The length in the table is a minimum, so we have
2166 to arrange to skip the extra bytes. */
2167
2168 #ifdef SUPPORT_UTF
2169 if (utf) switch(c)
2170 {
2171 case OP_CHAR:
2172 case OP_CHARI:
2173 case OP_EXACT:
2174 case OP_EXACTI:
2175 case OP_UPTO:
2176 case OP_UPTOI:
2177 case OP_MINUPTO:
2178 case OP_MINUPTOI:
2179 case OP_POSUPTO:
2180 case OP_POSUPTOI:
2181 case OP_STAR:
2182 case OP_STARI:
2183 case OP_MINSTAR:
2184 case OP_MINSTARI:
2185 case OP_POSSTAR:
2186 case OP_POSSTARI:
2187 case OP_PLUS:
2188 case OP_PLUSI:
2189 case OP_MINPLUS:
2190 case OP_MINPLUSI:
2191 case OP_POSPLUS:
2192 case OP_POSPLUSI:
2193 case OP_QUERY:
2194 case OP_QUERYI:
2195 case OP_MINQUERY:
2196 case OP_MINQUERYI:
2197 case OP_POSQUERY:
2198 case OP_POSQUERYI:
2199 if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2200 break;
2201 }
2202 #else
2203 (void)(utf); /* Keep compiler happy by referencing function argument */
2204 #endif
2205 }
2206 }
2207 }
2208
2209
2210
2211 /*************************************************
2212 * Scan compiled branch for non-emptiness *
2213 *************************************************/
2214
2215 /* This function scans through a branch of a compiled pattern to see whether it
2216 can match the empty string or not. It is called from could_be_empty()
2217 below and from compile_branch() when checking for an unlimited repeat of a
2218 group that can match nothing. Note that first_significant_code() skips over
2219 backward and negative forward assertions when its final argument is TRUE. If we
2220 hit an unclosed bracket, we return "empty" - this means we've struck an inner
2221 bracket whose current branch will already have been scanned.
2222
2223 Arguments:
2224 code points to start of search
2225 endcode points to where to stop
2226 utf TRUE if in UTF-8 / UTF-16 mode
2227 cd contains pointers to tables etc.
2228
2229 Returns: TRUE if what is matched could be empty
2230 */
2231
2232 static BOOL
2233 could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2234 BOOL utf, compile_data *cd)
2235 {
2236 register int c;
2237 for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2238 code < endcode;
2239 code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2240 {
2241 const pcre_uchar *ccode;
2242
2243 c = *code;
2244
2245 /* Skip over forward assertions; the other assertions are skipped by
2246 first_significant_code() with a TRUE final argument. */
2247
2248 if (c == OP_ASSERT)
2249 {
2250 do code += GET(code, 1); while (*code == OP_ALT);
2251 c = *code;
2252 continue;
2253 }
2254
2255 /* For a recursion/subroutine call, if its end has been reached, which
2256 implies a backward reference subroutine call, we can scan it. If it's a
2257 forward reference subroutine call, we can't. To detect forward reference
2258 we have to scan up the list that is kept in the workspace. This function is
2259 called only when doing the real compile, not during the pre-compile that
2260 measures the size of the compiled pattern. */
2261
2262 if (c == OP_RECURSE)
2263 {
2264 const pcre_uchar *scode;
2265 BOOL empty_branch;
2266
2267 /* Test for forward reference */
2268
2269 for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
2270 if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
2271
2272 /* Not a forward reference, test for completed backward reference */
2273
2274 empty_branch = FALSE;
2275 scode = cd->start_code + GET(code, 1);
2276 if (GET(scode, 1) == 0) return TRUE; /* Unclosed */
2277
2278 /* Completed backwards reference */
2279
2280 do
2281 {
2282 if (could_be_empty_branch(scode, endcode, utf, cd))
2283 {
2284 empty_branch = TRUE;
2285 break;
2286 }
2287 scode += GET(scode, 1);
2288 }
2289 while (*scode == OP_ALT);
2290
2291 if (!empty_branch) return FALSE; /* All branches are non-empty */
2292 continue;
2293 }
2294
2295 /* Groups with zero repeats can of course be empty; skip them. */
2296
2297 if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2298 c == OP_BRAPOSZERO)
2299 {
2300 code += PRIV(OP_lengths)[c];
2301 do code += GET(code, 1); while (*code == OP_ALT);
2302 c = *code;
2303 continue;
2304 }
2305
2306 /* A nested group that is already marked as "could be empty" can just be
2307 skipped. */
2308
2309 if (c == OP_SBRA || c == OP_SBRAPOS ||
2310 c == OP_SCBRA || c == OP_SCBRAPOS)
2311 {
2312 do code += GET(code, 1); while (*code == OP_ALT);
2313 c = *code;
2314 continue;
2315 }
2316
2317 /* For other groups, scan the branches. */
2318
2319 if (c == OP_BRA || c == OP_BRAPOS ||
2320 c == OP_CBRA || c == OP_CBRAPOS ||
2321 c == OP_ONCE || c == OP_ONCE_NC ||
2322 c == OP_COND)
2323 {
2324 BOOL empty_branch;
2325 if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */
2326
2327 /* If a conditional group has only one branch, there is a second, implied,
2328 empty branch, so just skip over the conditional, because it could be empty.
2329 Otherwise, scan the individual branches of the group. */
2330
2331 if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
2332 code += GET(code, 1);
2333 else
2334 {
2335 empty_branch = FALSE;
2336 do
2337 {
2338 if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))
2339 empty_branch = TRUE;
2340 code += GET(code, 1);
2341 }
2342 while (*code == OP_ALT);
2343 if (!empty_branch) return FALSE; /* All branches are non-empty */
2344 }
2345
2346 c = *code;
2347 continue;
2348 }
2349
2350 /* Handle the other opcodes */
2351
2352 switch (c)
2353 {
2354 /* Check for quantifiers after a class. XCLASS is used for classes that
2355 cannot be represented just by a bit map. This includes negated single
2356 high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2357 actual length is stored in the compiled code, so we must update "code"
2358 here. */
2359
2360 #ifdef SUPPORT_UTF8
2361 case OP_XCLASS:
2362 ccode = code += GET(code, 1);
2363 goto CHECK_CLASS_REPEAT;
2364 #endif
2365
2366 case OP_CLASS:
2367 case OP_NCLASS:
2368 ccode = code + PRIV(OP_lengths)[OP_CLASS];
2369
2370 #ifdef SUPPORT_UTF8
2371 CHECK_CLASS_REPEAT:
2372 #endif
2373
2374 switch (*ccode)
2375 {
2376 case OP_CRSTAR: /* These could be empty; continue */
2377 case OP_CRMINSTAR:
2378 case OP_CRQUERY:
2379 case OP_CRMINQUERY:
2380 break;
2381
2382 default: /* Non-repeat => class must match */
2383 case OP_CRPLUS: /* These repeats aren't empty */
2384 case OP_CRMINPLUS:
2385 return FALSE;
2386
2387 case OP_CRRANGE:
2388 case OP_CRMINRANGE:
2389 if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */
2390 break;
2391 }
2392 break;
2393
2394 /* Opcodes that must match a character */
2395
2396 case OP_PROP:
2397 case OP_NOTPROP:
2398 case OP_EXTUNI:
2399 case OP_NOT_DIGIT:
2400 case OP_DIGIT:
2401 case OP_NOT_WHITESPACE:
2402 case OP_WHITESPACE:
2403 case OP_NOT_WORDCHAR:
2404 case OP_WORDCHAR:
2405 case OP_ANY:
2406 case OP_ALLANY:
2407 case OP_ANYBYTE:
2408 case OP_CHAR:
2409 case OP_CHARI:
2410 case OP_NOT:
2411 case OP_NOTI:
2412 case OP_PLUS:
2413 case OP_MINPLUS:
2414 case OP_POSPLUS:
2415 case OP_EXACT:
2416 case OP_NOTPLUS:
2417 case OP_NOTMINPLUS:
2418 case OP_NOTPOSPLUS:
2419 case OP_NOTEXACT:
2420 case OP_TYPEPLUS:
2421 case OP_TYPEMINPLUS:
2422 case OP_TYPEPOSPLUS:
2423 case OP_TYPEEXACT:
2424 return FALSE;
2425
2426 /* These are going to continue, as they may be empty, but we have to
2427 fudge the length for the \p and \P cases. */
2428
2429 case OP_TYPESTAR:
2430 case OP_TYPEMINSTAR:
2431 case OP_TYPEPOSSTAR:
2432 case OP_TYPEQUERY:
2433 case OP_TYPEMINQUERY:
2434 case OP_TYPEPOSQUERY:
2435 if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2436 break;
2437
2438 /* Same for these */
2439
2440 case OP_TYPEUPTO:
2441 case OP_TYPEMINUPTO:
2442 case OP_TYPEPOSUPTO:
2443 if (code[1 + IMM2_SIZE] == OP_PROP
2444 || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2445 break;
2446
2447 /* End of branch */
2448
2449 case OP_KET:
2450 case OP_KETRMAX:
2451 case OP_KETRMIN:
2452 case OP_KETRPOS:
2453 case OP_ALT:
2454 return TRUE;
2455
2456 /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2457 MINUPTO, and POSUPTO may be followed by a multibyte character */
2458
2459 #ifdef SUPPORT_UTF
2460 case OP_STAR:
2461 case OP_STARI:
2462 case OP_MINSTAR:
2463 case OP_MINSTARI:
2464 case OP_POSSTAR:
2465 case OP_POSSTARI:
2466 case OP_QUERY:
2467 case OP_QUERYI:
2468 case OP_MINQUERY:
2469 case OP_MINQUERYI:
2470 case OP_POSQUERY:
2471 case OP_POSQUERYI:
2472 if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2473 break;
2474
2475 case OP_UPTO:
2476 case OP_UPTOI:
2477 case OP_MINUPTO:
2478 case OP_MINUPTOI:
2479 case OP_POSUPTO:
2480 case OP_POSUPTOI:
2481 if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2482 break;
2483 #endif
2484
2485 /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2486 string. */
2487
2488 case OP_MARK:
2489 case OP_PRUNE_ARG:
2490 case OP_SKIP_ARG:
2491 code += code[1];
2492 break;
2493
2494 case OP_THEN_ARG:
2495 code += code[1];
2496 break;
2497
2498 /* None of the remaining opcodes are required to match a character. */
2499
2500 default:
2501 break;
2502 }
2503 }
2504
2505 return TRUE;
2506 }
2507
2508
2509
2510 /*************************************************
2511 * Scan compiled regex for non-emptiness *
2512 *************************************************/
2513
2514 /* This function is called to check for left recursive calls. We want to check
2515 the current branch of the current pattern to see if it could match the empty
2516 string. If it could, we must look outwards for branches at other levels,
2517 stopping when we pass beyond the bracket which is the subject of the recursion.
2518 This function is called only during the real compile, not during the
2519 pre-compile.
2520
2521 Arguments:
2522 code points to start of the recursion
2523 endcode points to where to stop (current RECURSE item)
2524 bcptr points to the chain of current (unclosed) branch starts
2525 utf TRUE if in UTF-8 / UTF-16 mode
2526 cd pointers to tables etc
2527
2528 Returns: TRUE if what is matched could be empty
2529 */
2530
2531 static BOOL
2532 could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2533 branch_chain *bcptr, BOOL utf, compile_data *cd)
2534 {
2535 while (bcptr != NULL && bcptr->current_branch >= code)
2536 {
2537 if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))
2538 return FALSE;
2539 bcptr = bcptr->outer;
2540 }
2541 return TRUE;
2542 }
2543
2544
2545
2546 /*************************************************
2547 * Check for POSIX class syntax *
2548 *************************************************/
2549
2550 /* This function is called when the sequence "[:" or "[." or "[=" is
2551 encountered in a character class. It checks whether this is followed by a
2552 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
2553 reach an unescaped ']' without the special preceding character, return FALSE.
2554
2555 Originally, this function only recognized a sequence of letters between the
2556 terminators, but it seems that Perl recognizes any sequence of characters,
2557 though of course unknown POSIX names are subsequently rejected. Perl gives an
2558 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
2559 didn't consider this to be a POSIX class. Likewise for [:1234:].
2560
2561 The problem in trying to be exactly like Perl is in the handling of escapes. We
2562 have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
2563 class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
2564 below handles the special case of \], but does not try to do any other escape
2565 processing. This makes it different from Perl for cases such as [:l\ower:]
2566 where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
2567 "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
2568 I think.
2569
2570 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2571 It seems that the appearance of a nested POSIX class supersedes an apparent
2572 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2573 a digit.
2574
2575 In Perl, unescaped square brackets may also appear as part of class names. For
2576 example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2577 [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2578 seem right at all. PCRE does not allow closing square brackets in POSIX class
2579 names.
2580
2581 Arguments:
2582 ptr pointer to the initial [
2583 endptr where to return the end pointer
2584
2585 Returns: TRUE or FALSE
2586 */
2587
2588 static BOOL
2589 check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2590 {
2591 int terminator; /* Don't combine these lines; the Solaris cc */
2592 terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */
2593 for (++ptr; *ptr != 0; ptr++)
2594 {
2595 if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2596 ptr++;
2597 else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2598 else
2599 {
2600 if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2601 {
2602 *endptr = ptr;
2603 return TRUE;
2604 }
2605 if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2606 (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2607 ptr[1] == CHAR_EQUALS_SIGN) &&
2608 check_posix_syntax(ptr, endptr))
2609 return FALSE;
2610 }
2611 }
2612 return FALSE;
2613 }
2614
2615
2616
2617
2618 /*************************************************
2619 * Check POSIX class name *
2620 *************************************************/
2621
2622 /* This function is called to check the name given in a POSIX-style class entry
2623 such as [:alnum:].
2624
2625 Arguments:
2626 ptr points to the first letter
2627 len the length of the name
2628
2629 Returns: a value representing the name, or -1 if unknown
2630 */
2631
2632 static int
2633 check_posix_name(const pcre_uchar *ptr, int len)
2634 {
2635 const char *pn = posix_names;
2636 register int yield = 0;
2637 while (posix_name_lengths[yield] != 0)
2638 {
2639 if (len == posix_name_lengths[yield] &&
2640 STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;
2641 pn += posix_name_lengths[yield] + 1;
2642 yield++;
2643 }
2644 return -1;
2645 }
2646
2647
2648 /*************************************************
2649 * Adjust OP_RECURSE items in repeated group *
2650 *************************************************/
2651
2652 /* OP_RECURSE items contain an offset from the start of the regex to the group
2653 that is referenced. This means that groups can be replicated for fixed
2654 repetition simply by copying (because the recursion is allowed to refer to
2655 earlier groups that are outside the current group). However, when a group is
2656 optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
2657 inserted before it, after it has been compiled. This means that any OP_RECURSE
2658 items within it that refer to the group itself or any contained groups have to
2659 have their offsets adjusted. That one of the jobs of this function. Before it
2660 is called, the partially compiled regex must be temporarily terminated with
2661 OP_END.
2662
2663 This function has been extended with the possibility of forward references for
2664 recursions and subroutine calls. It must also check the list of such references
2665 for the group we are dealing with. If it finds that one of the recursions in
2666 the current group is on this list, it adjusts the offset in the list, not the
2667 value in the reference (which is a group number).
2668
2669 Arguments:
2670 group points to the start of the group
2671 adjust the amount by which the group is to be moved
2672 utf TRUE in UTF-8 / UTF-16 mode
2673 cd contains pointers to tables etc.
2674 save_hwm the hwm forward reference pointer at the start of the group
2675
2676 Returns: nothing
2677 */
2678
2679 static void
2680 adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
2681 pcre_uchar *save_hwm)
2682 {
2683 pcre_uchar *ptr = group;
2684
2685 while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
2686 {
2687 int offset;
2688 pcre_uchar *hc;
2689
2690 /* See if this recursion is on the forward reference list. If so, adjust the
2691 reference. */
2692
2693 for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
2694 {
2695 offset = GET(hc, 0);
2696 if (cd->start_code + offset == ptr + 1)
2697 {
2698 PUT(hc, 0, offset + adjust);
2699 break;
2700 }
2701 }
2702
2703 /* Otherwise, adjust the recursion offset if it's after the start of this
2704 group. */
2705
2706 if (hc >= cd->hwm)
2707 {
2708 offset = GET(ptr, 1);
2709 if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
2710 }
2711
2712 ptr += 1 + LINK_SIZE;
2713 }
2714 }
2715
2716
2717
2718 /*************************************************
2719 * Insert an automatic callout point *
2720 *************************************************/
2721
2722 /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
2723 callout points before each pattern item.
2724
2725 Arguments:
2726 code current code pointer
2727 ptr current pattern pointer
2728 cd pointers to tables etc
2729
2730 Returns: new code pointer
2731 */
2732
2733 static pcre_uchar *
2734 auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2735 {
2736 *code++ = OP_CALLOUT;
2737 *code++ = 255;
2738 PUT(code, 0, (int)(ptr - cd->start_pattern)); /* Pattern offset */
2739 PUT(code, LINK_SIZE, 0); /* Default length */
2740 return code + 2 * LINK_SIZE;
2741 }
2742
2743
2744
2745 /*************************************************
2746 * Complete a callout item *
2747 *************************************************/
2748
2749 /* A callout item contains the length of the next item in the pattern, which
2750 we can't fill in till after we have reached the relevant point. This is used
2751 for both automatic and manual callouts.
2752
2753 Arguments:
2754 previous_callout points to previous callout item
2755 ptr current pattern pointer
2756 cd pointers to tables etc
2757
2758 Returns: nothing
2759 */
2760
2761 static void
2762 complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2763 {
2764 int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2765 PUT(previous_callout, 2 + LINK_SIZE, length);
2766 }
2767
2768
2769
2770 #ifdef SUPPORT_UCP
2771 /*************************************************
2772 * Get othercase range *
2773 *************************************************/
2774
2775 /* This function is passed the start and end of a class range, in UTF-8 mode
2776 with UCP support. It searches up the characters, looking for internal ranges of
2777 characters in the "other" case. Each call returns the next one, updating the
2778 start address.
2779
2780 Arguments:
2781 cptr points to starting character value; updated
2782 d end value
2783 ocptr where to put start of othercase range
2784 odptr where to put end of othercase range
2785
2786 Yield: TRUE when range returned; FALSE when no more
2787 */
2788
2789 static BOOL
2790 get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,
2791 unsigned int *odptr)
2792 {
2793 unsigned int c, othercase, next;
2794
2795 for (c = *cptr; c <= d; c++)
2796 { if ((othercase = UCD_OTHERCASE(c)) != c) break; }
2797
2798 if (c > d) return FALSE;
2799
2800 *ocptr = othercase;
2801 next = othercase + 1;
2802
2803 for (++c; c <= d; c++)
2804 {
2805 if (UCD_OTHERCASE(c) != next) break;
2806 next++;
2807 }
2808
2809 *odptr = next - 1;
2810 *cptr = c;
2811
2812 return TRUE;
2813 }
2814
2815
2816
2817 /*************************************************
2818 * Check a character and a property *
2819 *************************************************/
2820
2821 /* This function is called by check_auto_possessive() when a property item
2822 is adjacent to a fixed character.
2823
2824 Arguments:
2825 c the character
2826 ptype the property type
2827 pdata the data for the type
2828 negated TRUE if it's a negated property (\P or \p{^)
2829
2830 Returns: TRUE if auto-possessifying is OK
2831 */
2832
2833 static BOOL
2834 check_char_prop(int c, int ptype, int pdata, BOOL negated)
2835 {
2836 const ucd_record *prop = GET_UCD(c);
2837 switch(ptype)
2838 {
2839 case PT_LAMP:
2840 return (prop->chartype == ucp_Lu ||
2841 prop->chartype == ucp_Ll ||
2842 prop->chartype == ucp_Lt) == negated;
2843
2844 case PT_GC:
2845 return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2846
2847 case PT_PC:
2848 return (pdata == prop->chartype) == negated;
2849
2850 case PT_SC:
2851 return (pdata == prop->script) == negated;
2852
2853 /* These are specials */
2854
2855 case PT_ALNUM:
2856 return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2857 PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2858
2859 case PT_SPACE: /* Perl space */
2860 return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2861 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2862 == negated;
2863
2864 case PT_PXSPACE: /* POSIX space */
2865 return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2866 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2867 c == CHAR_FF || c == CHAR_CR)
2868 == negated;
2869
2870 case PT_WORD:
2871 return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2872 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2873 c == CHAR_UNDERSCORE) == negated;
2874 }
2875 return FALSE;
2876 }
2877 #endif /* SUPPORT_UCP */
2878
2879
2880
2881 /*************************************************
2882 * Check if auto-possessifying is possible *
2883 *************************************************/
2884
2885 /* This function is called for unlimited repeats of certain items, to see
2886 whether the next thing could possibly match the repeated item. If not, it makes
2887 sense to automatically possessify the repeated item.
2888
2889 Arguments:
2890 previous pointer to the repeated opcode
2891 utf TRUE in UTF-8 / UTF-16 mode
2892 ptr next character in pattern
2893 options options bits
2894 cd contains pointers to tables etc.
2895
2896 Returns: TRUE if possessifying is wanted
2897 */
2898
2899 static BOOL
2900 check_auto_possessive(const pcre_uchar *previous, BOOL utf,
2901 const pcre_uchar *ptr, int options, compile_data *cd)
2902 {
2903 pcre_int32 c, next;
2904 int op_code = *previous++;
2905
2906 /* Skip whitespace and comments in extended mode */
2907
2908 if ((options & PCRE_EXTENDED) != 0)
2909 {
2910 for (;;)
2911 {
2912 while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2913 if (*ptr == CHAR_NUMBER_SIGN)
2914 {
2915 ptr++;
2916 while (*ptr != 0)
2917 {
2918 if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2919 ptr++;
2920 #ifdef SUPPORT_UTF
2921 if (utf) FORWARDCHAR(ptr);
2922 #endif
2923 }
2924 }
2925 else break;
2926 }
2927 }
2928
2929 /* If the next item is one that we can handle, get its value. A non-negative
2930 value is a character, a negative value is an escape value. */
2931
2932 if (*ptr == CHAR_BACKSLASH)
2933 {
2934 int temperrorcode = 0;
2935 next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);
2936 if (temperrorcode != 0) return FALSE;
2937 ptr++; /* Point after the escape sequence */
2938 }
2939 else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
2940 {
2941 #ifdef SUPPORT_UTF
2942 if (utf) { GETCHARINC(next, ptr); } else
2943 #endif
2944 next = *ptr++;
2945 }
2946 else return FALSE;
2947
2948 /* Skip whitespace and comments in extended mode */
2949
2950 if ((options & PCRE_EXTENDED) != 0)
2951 {
2952 for (;;)
2953 {
2954 while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2955 if (*ptr == CHAR_NUMBER_SIGN)
2956 {
2957 ptr++;
2958 while (*ptr != 0)
2959 {
2960 if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2961 ptr++;
2962 #ifdef SUPPORT_UTF
2963 if (utf) FORWARDCHAR(ptr);
2964 #endif
2965 }
2966 }
2967 else break;
2968 }
2969 }
2970
2971 /* If the next thing is itself optional, we have to give up. */
2972
2973 if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
2974 STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
2975 return FALSE;
2976
2977 /* Now compare the next item with the previous opcode. First, handle cases when
2978 the next item is a character. */
2979
2980 if (next >= 0) switch(op_code)
2981 {
2982 case OP_CHAR:
2983 #ifdef SUPPORT_UTF8
2984 GETCHARTEST(c, previous);
2985 #else
2986 c = *previous;
2987 #endif
2988 return c != next;
2989
2990 /* For CHARI (caseless character) we must check the other case. If we have
2991 Unicode property support, we can use it to test the other case of
2992 high-valued characters. */
2993
2994 case OP_CHARI:
2995 #ifdef SUPPORT_UTF8
2996 GETCHARTEST(c, previous);
2997 #else
2998 c = *previous;
2999 #endif
3000 if (c == next) return FALSE;
3001 #ifdef SUPPORT_UTF8
3002 if (utf)
3003 {
3004 unsigned int othercase;
3005 if (next < 128) othercase = cd->fcc[next]; else
3006 #ifdef SUPPORT_UCP
3007 othercase = UCD_OTHERCASE((unsigned int)next);
3008 #else
3009 othercase = NOTACHAR;
3010 #endif
3011 return (unsigned int)c != othercase;
3012 }
3013 else
3014 #endif /* SUPPORT_UTF8 */
3015 return (c != cd->fcc[next]); /* Non-UTF-8 mode */
3016
3017 /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These
3018 opcodes are not used for multi-byte characters, because they are coded using
3019 an XCLASS instead. */
3020
3021 case OP_NOT:
3022 return (c = *previous) == next;
3023
3024 case OP_NOTI:
3025 if ((c = *previous) == next) return TRUE;
3026 #ifdef SUPPORT_UTF8
3027 if (utf)
3028 {
3029 unsigned int othercase;
3030 if (next < 128) othercase = cd->fcc[next]; else
3031 #ifdef SUPPORT_UCP
3032 othercase = UCD_OTHERCASE(next);
3033 #else
3034 othercase = NOTACHAR;
3035 #endif
3036 return (unsigned int)c == othercase;
3037 }
3038 else
3039 #endif /* SUPPORT_UTF8 */
3040 return (c == cd->fcc[next]); /* Non-UTF-8 mode */
3041
3042 /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3043 When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3044
3045 case OP_DIGIT:
3046 return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;
3047
3048 case OP_NOT_DIGIT:
3049 return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;
3050
3051 case OP_WHITESPACE:
3052 return next > 127 || (cd->ctypes[next] & ctype_space) == 0;
3053
3054 case OP_NOT_WHITESPACE:
3055 return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;
3056
3057 case OP_WORDCHAR:
3058 return next > 127 || (cd->ctypes[next] & ctype_word) == 0;
3059
3060 case OP_NOT_WORDCHAR:
3061 return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;
3062
3063 case OP_HSPACE:
3064 case OP_NOT_HSPACE:
3065 switch(next)
3066 {
3067 case 0x09:
3068 case 0x20:
3069 case 0xa0:
3070 case 0x1680:
3071 case 0x180e:
3072 case 0x2000:
3073 case 0x2001:
3074 case 0x2002:
3075 case 0x2003:
3076 case 0x2004:
3077 case 0x2005:
3078 case 0x2006:
3079 case 0x2007:
3080 case 0x2008:
3081 case 0x2009:
3082 case 0x200A:
3083 case 0x202f:
3084 case 0x205f:
3085 case 0x3000:
3086 return op_code == OP_NOT_HSPACE;
3087 default:
3088 return op_code != OP_NOT_HSPACE;
3089 }
3090
3091 case OP_ANYNL:
3092 case OP_VSPACE:
3093 case OP_NOT_VSPACE:
3094 switch(next)
3095 {
3096 case 0x0a:
3097 case 0x0b:
3098 case 0x0c:
3099 case 0x0d:
3100 case 0x85:
3101 case 0x2028:
3102 case 0x2029:
3103 return op_code == OP_NOT_VSPACE;
3104 default:
3105 return op_code != OP_NOT_VSPACE;
3106 }
3107
3108 #ifdef SUPPORT_UCP
3109 case OP_PROP:
3110 return check_char_prop(next, previous[0], previous[1], FALSE);
3111
3112 case OP_NOTPROP:
3113 return check_char_prop(next, previous[0], previous[1], TRUE);
3114 #endif
3115
3116 default:
3117 return FALSE;
3118 }
3119
3120
3121 /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3122 is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3123 generated only when PCRE_UCP is *not* set, that is, when only ASCII
3124 characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are
3125 replaced by OP_PROP codes when PCRE_UCP is set. */
3126
3127 switch(op_code)
3128 {
3129 case OP_CHAR:
3130 case OP_CHARI:
3131 #ifdef SUPPORT_UTF8
3132 GETCHARTEST(c, previous);
3133 #else
3134 c = *previous;
3135 #endif
3136 switch(-next)
3137 {
3138 case ESC_d:
3139 return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;
3140
3141 case ESC_D:
3142 return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;
3143
3144 case ESC_s:
3145 return c > 127 || (cd->ctypes[c] & ctype_space) == 0;
3146
3147 case ESC_S:
3148 return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;
3149
3150 case ESC_w:
3151 return c > 127 || (cd->ctypes[c] & ctype_word) == 0;
3152
3153 case ESC_W:
3154 return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;
3155
3156 case ESC_h:
3157 case ESC_H:
3158 switch(c)
3159 {
3160 case 0x09:
3161 case 0x20:
3162 case 0xa0:
3163 case 0x1680:
3164 case 0x180e:
3165 case 0x2000:
3166 case 0x2001:
3167 case 0x2002:
3168 case 0x2003:
3169 case 0x2004:
3170 case 0x2005:
3171 case 0x2006:
3172 case 0x2007:
3173 case 0x2008:
3174 case 0x2009:
3175 case 0x200A:
3176 case 0x202f:
3177 case 0x205f:
3178 case 0x3000:
3179 return -next != ESC_h;
3180 default:
3181 return -next == ESC_h;
3182 }
3183
3184 case ESC_v:
3185 case ESC_V:
3186 switch(c)
3187 {
3188 case 0x0a:
3189 case 0x0b:
3190 case 0x0c:
3191 case 0x0d:
3192 case 0x85:
3193 case 0x2028:
3194 case 0x2029:
3195 return -next != ESC_v;
3196 default:
3197 return -next == ESC_v;
3198 }
3199
3200 /* When PCRE_UCP is set, these values get generated for \d etc. Find
3201 their substitutions and process them. The result will always be either
3202 -ESC_p or -ESC_P. Then fall through to process those values. */
3203
3204 #ifdef SUPPORT_UCP
3205 case ESC_du:
3206 case ESC_DU:
3207 case ESC_wu:
3208 case ESC_WU:
3209 case ESC_su:
3210 case ESC_SU:
3211 {
3212 int temperrorcode = 0;
3213 ptr = substitutes[-next - ESC_DU];
3214 next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);
3215 if (temperrorcode != 0) return FALSE;
3216 ptr++; /* For compatibility */
3217 }
3218 /* Fall through */
3219
3220 case ESC_p:
3221 case ESC_P:
3222 {
3223 int ptype, pdata, errorcodeptr;
3224 BOOL negated;
3225
3226 ptr--; /* Make ptr point at the p or P */
3227 ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);
3228 if (ptype < 0) return FALSE;
3229 ptr++; /* Point past the final curly ket */
3230
3231 /* If the property item is optional, we have to give up. (When generated
3232 from \d etc by PCRE_UCP, this test will have been applied much earlier,
3233 to the original \d etc. At this point, ptr will point to a zero byte. */
3234
3235 if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3236 STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3237 return FALSE;
3238
3239 /* Do the property check. */
3240
3241 return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);
3242 }
3243 #endif
3244
3245 default:
3246 return FALSE;
3247 }
3248
3249 /* In principle, support for Unicode properties should be integrated here as
3250 well. It means re-organizing the above code so as to get hold of the property
3251 values before switching on the op-code. However, I wonder how many patterns
3252 combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,
3253 these op-codes are never generated.) */
3254
3255 case OP_DIGIT:
3256 return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
3257 next == -ESC_h || next == -ESC_v || next == -ESC_R;
3258
3259 case OP_NOT_DIGIT:
3260 return next == -ESC_d;
3261
3262 case OP_WHITESPACE:
3263 return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;
3264
3265 case OP_NOT_WHITESPACE:
3266 return next == -ESC_s || next == -ESC_h || next == -ESC_v;
3267
3268 case OP_HSPACE:
3269 return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
3270 next == -ESC_w || next == -ESC_v || next == -ESC_R;
3271
3272 case OP_NOT_HSPACE:
3273 return next == -ESC_h;
3274
3275 /* Can't have \S in here because VT matches \S (Perl anomaly) */
3276 case OP_ANYNL:
3277 case OP_VSPACE:
3278 return next == -ESC_V || next == -ESC_d || next == -ESC_w;
3279
3280 case OP_NOT_VSPACE:
3281 return next == -ESC_v || next == -ESC_R;
3282
3283 case OP_WORDCHAR:
3284 return next == -ESC_W || next == -ESC_s || next == -ESC_h ||
3285 next == -ESC_v || next == -ESC_R;
3286
3287 case OP_NOT_WORDCHAR:
3288 return next == -ESC_w || next == -ESC_d;
3289
3290 default:
3291 return FALSE;
3292 }
3293
3294 /* Control does not reach here */
3295 }
3296
3297
3298
3299 /*************************************************
3300 * Compile one branch *
3301 *************************************************/
3302
3303 /* Scan the pattern, compiling it into the a vector. If the options are
3304 changed during the branch, the pointer is used to change the external options
3305 bits. This function is used during the pre-compile phase when we are trying
3306 to find out the amount of memory needed, as well as during the real compile
3307 phase. The value of lengthptr distinguishes the two phases.
3308
3309 Arguments:
3310 optionsptr pointer to the option bits
3311 codeptr points to the pointer to the current code point
3312 ptrptr points to the current pattern pointer
3313 errorcodeptr points to error code variable
3314 firstcharptr set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3315 reqcharptr set to the last literal character required, else < 0
3316 bcptr points to current branch chain
3317 cond_depth conditional nesting depth
3318 cd contains pointers to tables etc.
3319 lengthptr NULL during the real compile phase
3320 points to length accumulator during pre-compile phase
3321
3322 Returns: TRUE on success
3323 FALSE, with *errorcodeptr set non-zero on error
3324 */
3325
3326 static BOOL
3327 compile_branch(int *optionsptr, pcre_uchar **codeptr,
3328 const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,
3329 pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,
3330 compile_data *cd, int *lengthptr)
3331 {
3332 int repeat_type, op_type;
3333 int repeat_min = 0, repeat_max = 0; /* To please picky compilers */
3334 int bravalue = 0;
3335 int greedy_default, greedy_non_default;
3336 pcre_int32 firstchar, reqchar;
3337 pcre_int32 zeroreqchar, zerofirstchar;
3338 pcre_int32 req_caseopt, reqvary, tempreqvary;
3339 int options = *optionsptr; /* May change dynamically */
3340 int after_manual_callout = 0;
3341 int length_prevgroup = 0;
3342 register int c;
3343 register pcre_uchar *code = *codeptr;
3344 pcre_uchar *last_code = code;
3345 pcre_uchar *orig_code = code;
3346 pcre_uchar *tempcode;
3347 BOOL inescq = FALSE;
3348 BOOL groupsetfirstchar = FALSE;
3349 const pcre_uchar *ptr = *ptrptr;
3350 const pcre_uchar *tempptr;
3351 const pcre_uchar *nestptr = NULL;
3352 pcre_uchar *previous = NULL;
3353 pcre_uchar *previous_callout = NULL;
3354 pcre_uchar *save_hwm = NULL;
3355 pcre_uint8 classbits[32];
3356
3357 /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3358 must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3359 dynamically as we process the pattern. */
3360
3361 #ifdef SUPPORT_UTF8
3362 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
3363 BOOL utf = (options & PCRE_UTF8) != 0;
3364 pcre_uchar utf_chars[6];
3365 #else
3366 BOOL utf = FALSE;
3367 #endif
3368
3369 /* Helper variables for OP_XCLASS opcode (for characters > 255). */
3370
3371 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3372 BOOL xclass;
3373 pcre_uchar *class_uchardata;
3374 pcre_uchar *class_uchardata_base;
3375 #endif
3376
3377 #ifdef PCRE_DEBUG
3378 if (lengthptr != NULL) DPRINTF((">> start branch\n"));
3379 #endif
3380
3381 /* Set up the default and non-default settings for greediness */
3382
3383 greedy_default = ((options & PCRE_UNGREEDY) != 0);
3384 greedy_non_default = greedy_default ^ 1;
3385
3386 /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3387 matching encountered yet". It gets changed to REQ_NONE if we hit something that
3388 matches a non-fixed char first char; reqchar just remains unset if we never
3389 find one.
3390
3391 When we hit a repeat whose minimum is zero, we may have to adjust these values
3392 to take the zero repeat into account. This is implemented by setting them to
3393 zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3394 item types that can be repeated set these backoff variables appropriately. */
3395
3396 firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;
3397
3398 /* The variable req_caseopt contains either the REQ_CASELESS value
3399 or zero, according to the current setting of the caseless flag. The
3400 REQ_CASELESS leaves the lower 28 bit empty. It is added into the
3401 firstchar or reqchar variables to record the case status of the
3402 value. This is used only for ASCII characters. */
3403
3404 req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
3405
3406 /* Switch on next character until the end of the branch */
3407
3408 for (;; ptr++)
3409 {
3410 BOOL negate_class;
3411 BOOL should_flip_negation;
3412 BOOL possessive_quantifier;
3413 BOOL is_quantifier;
3414 BOOL is_recurse;
3415 BOOL reset_bracount;
3416 int class_charcount;
3417 int class_lastchar;
3418 int newoptions;
3419 int recno;
3420 int refsign;
3421 int skipbytes;
3422 int subreqchar;
3423 int subfirstchar;
3424 int terminator;
3425 int mclength;
3426 int tempbracount;
3427 pcre_uchar mcbuffer[8];
3428
3429 /* Get next character in the pattern */
3430
3431 c = *ptr;
3432
3433 /* If we are at the end of a nested substitution, revert to the outer level
3434 string. Nesting only happens one level deep. */
3435
3436 if (c == 0 && nestptr != NULL)
3437 {
3438 ptr = nestptr;
3439 nestptr = NULL;
3440 c = *ptr;
3441 }
3442
3443 /* If we are in the pre-compile phase, accumulate the length used for the
3444 previous cycle of this loop. */
3445
3446 if (lengthptr != NULL)
3447 {
3448 #ifdef PCRE_DEBUG
3449 if (code > cd->hwm) cd->hwm = code; /* High water info */
3450 #endif
3451 if (code > cd->start_workspace + WORK_SIZE_CHECK) /* Check for overrun */
3452 {
3453 *errorcodeptr = ERR52;
3454 goto FAILED;
3455 }
3456
3457 /* There is at least one situation where code goes backwards: this is the
3458 case of a zero quantifier after a class (e.g. [ab]{0}). At compile time,
3459 the class is simply eliminated. However, it is created first, so we have to
3460 allow memory for it. Therefore, don't ever reduce the length at this point.
3461 */
3462
3463 if (code < last_code) code = last_code;
3464
3465 /* Paranoid check for integer overflow */
3466
3467 if (OFLOW_MAX - *lengthptr < code - last_code)
3468 {
3469 *errorcodeptr = ERR20;
3470 goto FAILED;
3471 }
3472
3473 *lengthptr += (int)(code - last_code);
3474 DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
3475 (int)(code - last_code), c, c));
3476
3477 /* If "previous" is set and it is not at the start of the work space, move
3478 it back to there, in order to avoid filling up the work space. Otherwise,
3479 if "previous" is NULL, reset the current code pointer to the start. */
3480
3481 if (previous != NULL)
3482 {
3483 if (previous > orig_code)
3484 {
3485 memmove(orig_code, previous, IN_UCHARS(code - previous));
3486 code -= previous - orig_code;
3487 previous = orig_code;
3488 }
3489 }
3490 else code = orig_code;
3491
3492 /* Remember where this code item starts so we can pick up the length
3493 next time round. */
3494
3495 last_code = code;
3496 }
3497
3498 /* In the real compile phase, just check the workspace used by the forward
3499 reference list. */
3500
3501 else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK)
3502 {
3503 *errorcodeptr = ERR52;
3504 goto FAILED;
3505 }
3506
3507 /* If in \Q...\E, check for the end; if not, we have a literal */
3508
3509 if (inescq && c != 0)
3510 {
3511 if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
3512 {
3513 inescq = FALSE;
3514 ptr++;
3515 continue;
3516 }
3517 else
3518 {
3519 if (previous_callout != NULL)
3520 {
3521 if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
3522 complete_callout(previous_callout, ptr, cd);
3523 previous_callout = NULL;
3524 }
3525 if ((options & PCRE_AUTO_CALLOUT) != 0)
3526 {
3527 previous_callout = code;
3528 code = auto_callout(code, ptr, cd);
3529 }
3530 goto NORMAL_CHAR;
3531 }
3532 }
3533
3534 /* Fill in length of a previous callout, except when the next thing is
3535 a quantifier. */
3536
3537 is_quantifier =
3538 c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
3539 (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
3540
3541 if (!is_quantifier && previous_callout != NULL &&
3542 after_manual_callout-- <= 0)
3543 {
3544 if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
3545 complete_callout(previous_callout, ptr, cd);
3546 previous_callout = NULL;
3547 }
3548
3549 /* In extended mode, skip white space and comments. */
3550
3551 if ((options & PCRE_EXTENDED) != 0)
3552 {
3553 if ((cd->ctypes[c] & ctype_space) != 0) continue;
3554 if (c == CHAR_NUMBER_SIGN)
3555 {
3556 ptr++;
3557 while (*ptr != 0)
3558 {
3559 if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3560 ptr++;
3561 #ifdef SUPPORT_UTF
3562 if (utf) FORWARDCHAR(ptr);
3563 #endif
3564 }
3565 if (*ptr != 0) continue;
3566
3567 /* Else fall through to handle end of string */
3568 c = 0;
3569 }
3570 }
3571
3572 /* No auto callout for quantifiers. */
3573
3574 if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)
3575 {
3576 previous_callout = code;
3577 code = auto_callout(code, ptr, cd);
3578 }
3579
3580 switch(c)
3581 {
3582 /* ===================================================================*/
3583 case 0: /* The branch terminates at string end */
3584 case CHAR_VERTICAL_LINE: /* or | or ) */
3585 case CHAR_RIGHT_PARENTHESIS:
3586 *firstcharptr = firstchar;
3587 *reqcharptr = reqchar;
3588 *codeptr = code;
3589 *ptrptr = ptr;
3590 if (lengthptr != NULL)
3591 {
3592 if (OFLOW_MAX - *lengthptr < code - last_code)
3593 {
3594 *errorcodeptr = ERR20;
3595 goto FAILED;
3596 }
3597 *lengthptr += (int)(code - last_code); /* To include callout length */
3598 DPRINTF((">> end branch\n"));
3599 }
3600 return TRUE;
3601
3602
3603 /* ===================================================================*/
3604 /* Handle single-character metacharacters. In multiline mode, ^ disables
3605 the setting of any following char as a first character. */
3606
3607 case CHAR_CIRCUMFLEX_ACCENT:
3608 previous = NULL;
3609 if ((options & PCRE_MULTILINE) != 0)
3610 {
3611 if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3612 *code++ = OP_CIRCM;
3613 }
3614 else *code++ = OP_CIRC;
3615 break;
3616
3617 case CHAR_DOLLAR_SIGN:
3618 previous = NULL;
3619 *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3620 break;
3621
3622 /* There can never be a first char if '.' is first, whatever happens about
3623 repeats. The value of reqchar doesn't change either. */
3624
3625 case CHAR_DOT:
3626 if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3627 zerofirstchar = firstchar;
3628 zeroreqchar = reqchar;
3629 previous = code;
3630 *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3631 break;
3632
3633
3634 /* ===================================================================*/
3635 /* Character classes. If the included characters are all < 256, we build a
3636 32-byte bitmap of the permitted characters, except in the special case
3637 where there is only one such character. For negated classes, we build the
3638 map as usual, then invert it at the end. However, we use a different opcode
3639 so that data characters > 255 can be handled correctly.
3640
3641 If the class contains characters outside the 0-255 range, a different
3642 opcode is compiled. It may optionally have a bit map for characters < 256,
3643 but those above are are explicitly listed afterwards. A flag byte tells
3644 whether the bitmap is present, and whether this is a negated class or not.
3645
3646 In JavaScript compatibility mode, an isolated ']' causes an error. In
3647 default (Perl) mode, it is treated as a data character. */
3648
3649 case CHAR_RIGHT_SQUARE_BRACKET:
3650 if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
3651 {
3652 *errorcodeptr = ERR64;
3653 goto FAILED;
3654 }
3655 goto NORMAL_CHAR;
3656
3657 case CHAR_LEFT_SQUARE_BRACKET:
3658 previous = code;
3659
3660 /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
3661 they are encountered at the top level, so we'll do that too. */
3662
3663 if ((ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3664 ptr[1] == CHAR_EQUALS_SIGN) &&
3665 check_posix_syntax(ptr, &tempptr))
3666 {
3667 *errorcodeptr = (ptr[1] == CHAR_COLON)? ERR13 : ERR31;
3668 goto FAILED;
3669 }
3670
3671 /* If the first character is '^', set the negation flag and skip it. Also,
3672 if the first few characters (either before or after ^) are \Q\E or \E we
3673 skip them too. This makes for compatibility with Perl. */
3674
3675 negate_class = FALSE;
3676 for (;;)
3677 {
3678 c = *(++ptr);
3679 if (c == CHAR_BACKSLASH)
3680 {
3681 if (ptr[1] == CHAR_E)
3682 ptr++;
3683 else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
3684 ptr += 3;
3685 else
3686 break;
3687 }
3688 else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT)
3689 negate_class = TRUE;
3690 else break;
3691 }
3692
3693 /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
3694 an initial ']' is taken as a data character -- the code below handles
3695 that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
3696 [^] must match any character, so generate OP_ALLANY. */
3697
3698 if (c == CHAR_RIGHT_SQUARE_BRACKET &&
3699 (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
3700 {
3701 *code++ = negate_class? OP_ALLANY : OP_FAIL;
3702 if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3703 zerofirstchar = firstchar;
3704 break;
3705 }
3706
3707 /* If a class contains a negative special such as \S, we need to flip the
3708 negation flag at the end, so that support for characters > 255 works
3709 correctly (they are all included in the class). */
3710
3711 should_flip_negation = FALSE;
3712
3713 /* Keep a count of chars with values < 256 so that we can optimize the case
3714 of just a single character (as long as it's < 256). However, For higher
3715 valued UTF-8 characters, we don't yet do any optimization. */
3716
3717 class_charcount = 0;
3718 class_lastchar = -1;
3719
3720 /* Initialize the 32-char bit map to all zeros. We build the map in a
3721 temporary bit of memory, in case the class contains only 1 character (less
3722 than 256), because in that case the compiled code doesn't use the bit map.
3723 */
3724
3725 memset(classbits, 0, 32 * sizeof(pcre_uint8));
3726
3727 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3728 xclass = FALSE; /* No chars >= 256 */
3729 class_uchardata = code + LINK_SIZE + 2; /* For UTF-8 items */
3730 class_uchardata_base = class_uchardata; /* For resetting in pass 1 */
3731 #endif
3732
3733 /* Process characters until ] is reached. By writing this as a "do" it
3734 means that an initial ] is taken as a data character. At the start of the
3735 loop, c contains the first byte of the character. */
3736
3737 if (c != 0) do
3738 {
3739 const pcre_uchar *oldptr;
3740
3741 #ifdef SUPPORT_UTF8
3742 if (utf && c > 127)
3743 { /* Braces are required because the */
3744 GETCHARLEN(c, ptr, ptr); /* macro generates multiple statements */
3745 }
3746 #endif
3747
3748 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3749 /* In the pre-compile phase, accumulate the length of any extra
3750 data and reset the pointer. This is so that very large classes that
3751 contain a zillion > 255 characters no longer overwrite the work space
3752 (which is on the stack). */
3753
3754 if (lengthptr != NULL)
3755 {
3756 *lengthptr += class_uchardata - class_uchardata_base;
3757 class_uchardata = class_uchardata_base;
3758 }
3759 #endif
3760
3761 /* Inside \Q...\E everything is literal except \E */
3762
3763 if (inescq)
3764 {
3765 if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E) /* If we are at \E */
3766 {
3767 inescq = FALSE; /* Reset literal state */
3768 ptr++; /* Skip the 'E' */
3769 continue; /* Carry on with next */
3770 }
3771 goto CHECK_RANGE; /* Could be range if \E follows */
3772 }
3773
3774 /* Handle POSIX class names. Perl allows a negation extension of the
3775 form [:^name:]. A square bracket that doesn't match the syntax is
3776 treated as a literal. We also recognize the POSIX constructions
3777 [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
3778 5.6 and 5.8 do. */
3779
3780 if (c == CHAR_LEFT_SQUARE_BRACKET &&
3781 (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3782 ptr[1] == CHAR_EQUALS_SIGN) && check_posix_syntax(ptr, &tempptr))
3783 {
3784 BOOL local_negate = FALSE;
3785 int posix_class, taboffset, tabopt;
3786 register const pcre_uint8 *cbits = cd->cbits;
3787 pcre_uint8 pbits[32];
3788
3789 if (ptr[1] != CHAR_COLON)
3790 {
3791 *errorcodeptr = ERR31;
3792 goto FAILED;
3793 }
3794
3795 ptr += 2;
3796 if (*ptr == CHAR_CIRCUMFLEX_ACCENT)
3797 {
3798 local_negate = TRUE;
3799 should_flip_negation = TRUE; /* Note negative special */
3800 ptr++;
3801 }
3802
3803 posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
3804 if (posix_class < 0)
3805 {
3806 *errorcodeptr = ERR30;
3807 goto FAILED;
3808 }
3809
3810 /* If matching is caseless, upper and lower are converted to
3811 alpha. This relies on the fact that the class table starts with
3812 alpha, lower, upper as the first 3 entries. */
3813
3814 if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
3815 posix_class = 0;
3816
3817 /* When PCRE_UCP is set, some of the POSIX classes are converted to
3818 different escape sequences that use Unicode properties. */
3819
3820 #ifdef SUPPORT_UCP
3821 if ((options & PCRE_UCP) != 0)
3822 {
3823 int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
3824 if (posix_substitutes[pc] != NULL)
3825 {
3826 nestptr = tempptr + 1;
3827 ptr = posix_substitutes[pc] - 1;
3828 continue;
3829 }
3830 }
3831 #endif
3832 /* In the non-UCP case, we build the bit map for the POSIX class in a
3833 chunk of local store because we may be adding and subtracting from it,
3834 and we don't want to subtract bits that may be in the main map already.
3835 At the end we or the result into the bit map that is being built. */
3836
3837 posix_class *= 3;
3838
3839 /* Copy in the first table (always present) */
3840
3841 memcpy(pbits, cbits + posix_class_maps[posix_class],
3842 32 * sizeof(pcre_uint8));
3843
3844 /* If there is a second table, add or remove it as required. */
3845
3846 taboffset = posix_class_maps[posix_class + 1];
3847 tabopt = posix_class_maps[posix_class + 2];
3848
3849 if (taboffset >= 0)
3850 {
3851 if (tabopt >= 0)
3852 for (c = 0; c < 32; c++) pbits[c] |= cbits[c + taboffset];
3853 else
3854 for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
3855 }
3856
3857 /* Not see if we need to remove any special characters. An option
3858 value of 1 removes vertical space and 2 removes underscore. */
3859
3860 if (tabopt < 0) tabopt = -tabopt;
3861 if (tabopt == 1) pbits[1] &= ~0x3c;
3862 else if (tabopt == 2) pbits[11] &= 0x7f;
3863
3864 /* Add the POSIX table or its complement into the main table that is
3865 being built and we are done. */
3866
3867 if (local_negate)
3868 for (c = 0; c < 32; c++) classbits[c] |= ~pbits[c];
3869 else
3870 for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
3871
3872 ptr = tempptr + 1;
3873 class_charcount = 10; /* Set > 1; assumes more than 1 per class */
3874 continue; /* End of POSIX syntax handling */
3875 }
3876
3877 /* Backslash may introduce a single character, or it may introduce one
3878 of the specials, which just set a flag. The sequence \b is a special
3879 case. Inside a class (and only there) it is treated as backspace. We
3880 assume that other escapes have more than one character in them, so set
3881 class_charcount bigger than one. Unrecognized escapes fall through and
3882 are either treated as literal characters (by default), or are faulted if
3883 PCRE_EXTRA is set. */
3884
3885 if (c == CHAR_BACKSLASH)
3886 {
3887 c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3888 if (*errorcodeptr != 0) goto FAILED;
3889
3890 if (-c == ESC_b) c = CHAR_BS; /* \b is backspace in a class */
3891 else if (-c == ESC_Q) /* Handle start of quoted string */
3892 {
3893 if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
3894 {
3895 ptr += 2; /* avoid empty string */
3896 }
3897 else inescq = TRUE;
3898 continue;
3899 }
3900 else if (-c == ESC_E) continue; /* Ignore orphan \E */
3901
3902 if (c < 0)
3903 {
3904 register const pcre_uint8 *cbits = cd->cbits;
3905 class_charcount += 2; /* Greater than 1 is what matters */
3906
3907 switch (-c)
3908 {
3909 #ifdef SUPPORT_UCP
3910 case ESC_du: /* These are the values given for \d etc */
3911 case ESC_DU: /* when PCRE_UCP is set. We replace the */
3912 case ESC_wu: /* escape sequence with an appropriate \p */
3913 case ESC_WU: /* or \P to test Unicode properties instead */
3914 case ESC_su: /* of the default ASCII testing. */
3915 case ESC_SU:
3916 nestptr = ptr;
3917 ptr = substitutes[-c - ESC_DU] - 1; /* Just before substitute */
3918 class_charcount -= 2; /* Undo! */
3919 continue;
3920 #endif
3921 case ESC_d:
3922 for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
3923 continue;
3924
3925 case ESC_D:
3926 should_flip_negation = TRUE;
3927 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
3928 continue;
3929
3930 case ESC_w:
3931 for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word];
3932 continue;
3933
3934 case ESC_W:
3935 should_flip_negation = TRUE;
3936 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
3937 continue;
3938
3939 /* Perl 5.004 onwards omits VT from \s, but we must preserve it
3940 if it was previously set by something earlier in the character
3941 class. */
3942
3943 case ESC_s:
3944 classbits[0] |= cbits[cbit_space];
3945 classbits[1] |= cbits[cbit_space+1] & ~0x08;
3946 for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
3947 continue;
3948
3949 case ESC_S:
3950 should_flip_negation = TRUE;
3951 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
3952 classbits[1] |= 0x08; /* Perl 5.004 onwards omits VT from \s */
3953 continue;
3954
3955 case ESC_h:
3956 SETBIT(classbits, 0x09); /* VT */
3957 SETBIT(classbits, 0x20); /* SPACE */
3958 SETBIT(classbits, 0xa0); /* NSBP */
3959 #ifdef SUPPORT_UTF
3960 if (utf)
3961 {
3962 xclass = TRUE;
3963 *class_uchardata++ = XCL_SINGLE;
3964 class_uchardata += PRIV(ord2utf)(0x1680, class_uchardata);
3965 *class_uchardata++ = XCL_SINGLE;
3966 class_uchardata += PRIV(ord2utf)(0x180e, class_uchardata);
3967 *class_uchardata++ = XCL_RANGE;
3968 class_uchardata += PRIV(ord2utf)(0x2000, class_uchardata);
3969 class_uchardata += PRIV(ord2utf)(0x200A, class_uchardata);
3970 *class_uchardata++ = XCL_SINGLE;
3971 class_uchardata += PRIV(ord2utf)(0x202f, class_uchardata);
3972 *class_uchardata++ = XCL_SINGLE;
3973 class_uchardata += PRIV(ord2utf)(0x205f, class_uchardata);
3974 *class_uchardata++ = XCL_SINGLE;
3975 class_uchardata += PRIV(ord2utf)(0x3000, class_uchardata);
3976 }
3977 #endif
3978 continue;
3979
3980 case ESC_H:
3981 for (c = 0; c < 32; c++)
3982 {
3983 int x = 0xff;
3984 switch (c)
3985 {
3986 case 0x09/8: x ^= 1 << (0x09%8); break;
3987 case 0x20/8: x ^= 1 << (0x20%8); break;
3988 case 0xa0/8: x ^= 1 << (0xa0%8); break;
3989 default: break;
3990 }
3991 classbits[c] |= x;
3992 }
3993
3994 #ifdef SUPPORT_UTF
3995 if (utf)
3996 {
3997 xclass = TRUE;
3998 *class_uchardata++ = XCL_RANGE;
3999 class_uchardata += PRIV(ord2utf)(0x0100, class_uchardata);
4000 class_uchardata += PRIV(ord2utf)(0x167f, class_uchardata);
4001 *class_uchardata++ = XCL_RANGE;
4002 class_uchardata += PRIV(ord2utf)(0x1681, class_uchardata);
4003 class_uchardata += PRIV(ord2utf)(0x180d, class_uchardata);
4004 *class_uchardata++ = XCL_RANGE;
4005 class_uchardata += PRIV(ord2utf)(0x180f, class_uchardata);
4006 class_uchardata += PRIV(ord2utf)(0x1fff, class_uchardata);
4007 *class_uchardata++ = XCL_RANGE;
4008 class_uchardata += PRIV(ord2utf)(0x200B, class_uchardata);
4009 class_uchardata += PRIV(ord2utf)(0x202e, class_uchardata);
4010 *class_uchardata++ = XCL_RANGE;
4011 class_uchardata += PRIV(ord2utf)(0x2030, class_uchardata);
4012 class_uchardata += PRIV(ord2utf)(0x205e, class_uchardata);
4013 *class_uchardata++ = XCL_RANGE;
4014 class_uchardata += PRIV(ord2utf)(0x2060, class_uchardata);
4015 class_uchardata += PRIV(ord2utf)(0x2fff, class_uchardata);
4016 *class_uchardata++ = XCL_RANGE;
4017 class_uchardata += PRIV(ord2utf)(0x3001, class_uchardata);
4018 class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);
4019 }
4020 #endif
4021 continue;
4022
4023 case ESC_v:
4024 SETBIT(classbits, 0x0a); /* LF */
4025 SETBIT(classbits, 0x0b); /* VT */
4026 SETBIT(classbits, 0x0c); /* FF */
4027 SETBIT(classbits, 0x0d); /* CR */
4028 SETBIT(classbits, 0x85); /* NEL */
4029 #ifdef SUPPORT_UTF
4030 if (utf)
4031 {
4032 xclass = TRUE;
4033 *class_uchardata++ = XCL_RANGE;
4034 class_uchardata += PRIV(ord2utf)(0x2028, class_uchardata);
4035 class_uchardata += PRIV(ord2utf)(0x2029, class_uchardata);
4036 }
4037 #endif
4038 continue;
4039
4040 case ESC_V:
4041 for (c = 0; c < 32; c++)
4042 {
4043 int x = 0xff;
4044 switch (c)
4045 {
4046 case 0x0a/8: x ^= 1 << (0x0a%8);
4047 x ^= 1 << (0x0b%8);
4048 x ^= 1 << (0x0c%8);
4049 x ^= 1 << (0x0d%8);
4050 break;
4051 case 0x85/8: x ^= 1 << (0x85%8); break;
4052 default: break;
4053 }
4054 classbits[c] |= x;
4055 }
4056
4057 #ifdef SUPPORT_UTF
4058 if (utf)
4059 {
4060 xclass = TRUE;
4061 *class_uchardata++ = XCL_RANGE;
4062 class_uchardata += PRIV(ord2utf)(0x0100, class_uchardata);
4063 class_uchardata += PRIV(ord2utf)(0x2027, class_uchardata);
4064 *class_uchardata++ = XCL_RANGE;
4065 class_uchardata += PRIV(ord2utf)(0x2029, class_uchardata);
4066 class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);
4067 }
4068 #endif
4069 continue;
4070
4071 #ifdef SUPPORT_UCP
4072 case ESC_p:
4073 case ESC_P:
4074 {
4075 BOOL negated;
4076 int pdata;
4077 int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
4078 if (ptype < 0) goto FAILED;
4079 xclass = TRUE;
4080 *class_uchardata++ = ((-c == ESC_p) != negated)?
4081 XCL_PROP : XCL_NOTPROP;
4082 *class_uchardata++ = ptype;
4083 *class_uchardata++ = pdata;
4084 class_charcount -= 2; /* Not a < 256 character */
4085 continue;
4086 }
4087 #endif
4088 /* Unrecognized escapes are faulted if PCRE is running in its
4089 strict mode. By default, for compatibility with Perl, they are
4090 treated as literals. */
4091
4092 default:
4093 if ((options & PCRE_EXTRA) != 0)
4094 {
4095 *errorcodeptr = ERR7;
4096 goto FAILED;
4097 }
4098 class_charcount -= 2; /* Undo the default count from above */
4099 c = *ptr; /* Get the final character and fall through */
4100 break;
4101 }
4102 }
4103
4104 /* Fall through if we have a single character (c >= 0). This may be
4105 greater than 256 mode. */
4106
4107 } /* End of backslash handling */
4108
4109 /* A single character may be followed by '-' to form a range. However,
4110 Perl does not permit ']' to be the end of the range. A '-' character
4111 at the end is treated as a literal. Perl ignores orphaned \E sequences
4112 entirely. The code for handling \Q and \E is messy. */
4113
4114 CHECK_RANGE:
4115 while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4116 {
4117 inescq = FALSE;
4118 ptr += 2;
4119 }
4120
4121 oldptr = ptr;
4122
4123 /* Remember \r or \n */
4124
4125 if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4126
4127 /* Check for range */
4128
4129 if (!inescq && ptr[1] == CHAR_MINUS)
4130 {
4131 int d;
4132 ptr += 2;
4133 while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
4134
4135 /* If we hit \Q (not followed by \E) at this point, go into escaped
4136 mode. */
4137
4138 while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_Q)
4139 {
4140 ptr += 2;
4141 if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E)
4142 { ptr += 2; continue; }
4143 inescq = TRUE;
4144 break;
4145 }
4146
4147 if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4148 {
4149 ptr = oldptr;
4150 goto LONE_SINGLE_CHARACTER;
4151 }
4152
4153 #ifdef SUPPORT_UTF8
4154 if (utf)
4155 { /* Braces are required because the */
4156 GETCHARLEN(d, ptr, ptr); /* macro generates multiple statements */
4157 }
4158 else
4159 #endif
4160 d = *ptr; /* Not UTF-8 mode */
4161
4162 /* The second part of a range can be a single-character escape, but
4163 not any of the other escapes. Perl 5.6 treats a hyphen as a literal
4164 in such circumstances. */
4165
4166 if (!inescq && d == CHAR_BACKSLASH)
4167 {
4168 d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
4169 if (*errorcodeptr != 0) goto FAILED;
4170
4171 /* \b is backspace; any other special means the '-' was literal */
4172
4173 if (d < 0)
4174 {
4175 if (d == -ESC_b) d = CHAR_BS; else
4176 {
4177 ptr = oldptr;
4178 goto LONE_SINGLE_CHARACTER; /* A few lines below */
4179 }
4180 }
4181 }
4182
4183 /* Check that the two values are in the correct order. Optimize
4184 one-character ranges */
4185
4186 if (d < c)
4187 {
4188 *errorcodeptr = ERR8;
4189 goto FAILED;
4190 }
4191
4192 if (d == c) goto LONE_SINGLE_CHARACTER; /* A few lines below */
4193
4194 /* Remember \r or \n */
4195
4196 if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4197
4198 /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless
4199 matching, we have to use an XCLASS with extra data items. Caseless
4200 matching for characters > 127 is available only if UCP support is
4201 available. */
4202
4203 #ifdef SUPPORT_UTF
4204 if (utf && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))
4205 #endif
4206 #ifndef COMPILE_PCRE8
4207 if (d > 255)
4208 #endif
4209 #if defined SUPPORT_UTF || defined COMPILE_PCRE16
4210 {
4211 xclass = TRUE;
4212
4213 /* With UCP support, we can find the other case equivalents of
4214 the relevant characters. There may be several ranges. Optimize how
4215 they fit with the basic range. */
4216
4217 #ifdef SUPPORT_UCP
4218 if ((options & PCRE_CASELESS) != 0)
4219 {
4220 unsigned int occ, ocd;
4221 unsigned int cc = c;
4222 unsigned int origd = d;
4223 while (get_othercase_range(&cc, origd, &occ, &ocd))
4224 {
4225 if (occ >= (unsigned int)c &&
4226 ocd <= (unsigned int)d)
4227 continue; /* Skip embedded ranges */
4228
4229 if (occ < (unsigned int)c &&
4230 ocd >= (unsigned int)c - 1) /* Extend the basic range */
4231 { /* if there is overlap, */
4232 c = occ; /* noting that if occ < c */
4233 continue; /* we can't have ocd > d */
4234 } /* because a subrange is */
4235 if (ocd > (unsigned int)d &&
4236 occ <= (unsigned int)d + 1) /* always shorter than */
4237 { /* the basic range. */
4238 d = ocd;
4239 continue;
4240 }
4241
4242 if (occ == ocd)
4243 {
4244 *class_uchardata++ = XCL_SINGLE;
4245 }
4246 else
4247 {
4248 *class_uchardata++ = XCL_RANGE;
4249 class_uchardata += PRIV(ord2utf)(occ, class_uchardata);
4250 }
4251 class_uchardata += PRIV(ord2utf)(ocd, class_uchardata);
4252 }
4253 }
4254 #endif /* SUPPORT_UCP */
4255
4256 /* Now record the original range, possibly modified for UCP caseless
4257 overlapping ranges. */
4258
4259 *class_uchardata++ = XCL_RANGE;
4260 #ifdef SUPPORT_UTF
4261 class_uchardata += PRIV(ord2utf)(c, class_uchardata);
4262 class_uchardata += PRIV(ord2utf)(d, class_uchardata);
4263 #else
4264 *class_uchardata++ = c;
4265 *class_uchardata++ = d;
4266 #endif
4267
4268 /* With UCP support, we are done. Without UCP support, there is no
4269 caseless matching for UTF characters > 127; we can use the bit map
4270 for the smaller ones. As for 16 bit characters without UTF, we
4271 can still use */
4272
4273 #ifdef SUPPORT_UCP
4274 continue; /* With next character in the class */
4275 #else
4276 #ifdef SUPPORT_UTF
4277 if ((options & PCRE_CASELESS) == 0 || c > 127) continue;
4278 /* Adjust upper limit and fall through to set up the map */
4279 d = 127;
4280 #else
4281 if (c > 255) continue;
4282 /* Adjust upper limit and fall through to set up the map */
4283 d = 255;
4284 #endif /* SUPPORT_UTF */
4285 #endif /* SUPPORT_UCP */
4286 }
4287 #endif /* SUPPORT_UTF8 || COMPILE_PCRE16 */
4288
4289 /* We use the bit map for 8 bit mode, or when the characters fall
4290 partially or entirely to [0-255] ([0-127] for UCP) ranges. */
4291
4292 class_charcount += d - c + 1;
4293 class_lastchar = d;
4294
4295 /* We can save a bit of time by skipping this in the pre-compile. */
4296
4297 if (lengthptr == NULL) for (; c <= d; c++)
4298 {
4299 classbits[c/8] |= (1 << (c&7));
4300 if ((options & PCRE_CASELESS) != 0)
4301 {
4302 int uc = cd->fcc[c]; /* flip case */
4303 classbits[uc/8] |= (1 << (uc&7));
4304 }
4305 }
4306
4307 continue; /* Go get the next char in the class */
4308 }
4309
4310 /* Handle a lone single character - we can get here for a normal
4311 non-escape char, or after \ that introduces a single character or for an
4312 apparent range that isn't. */
4313
4314 LONE_SINGLE_CHARACTER:
4315
4316 /* Handle a character that cannot go in the bit map */
4317
4318 #ifdef SUPPORT_UTF
4319 if (utf && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))
4320 #endif
4321 #ifndef COMPILE_PCRE8
4322 if (c > 255)
4323 #endif
4324 #if defined SUPPORT_UTF || defined COMPILE_PCRE16
4325 {
4326 xclass = TRUE;
4327 *class_uchardata++ = XCL_SINGLE;
4328 #ifdef SUPPORT_UTF
4329 class_uchardata += PRIV(ord2utf)(c, class_uchardata);
4330 #else
4331 *class_uchardata++ = c;
4332 #endif
4333
4334 #ifdef SUPPORT_UCP
4335 if ((options & PCRE_CASELESS) != 0)
4336 {
4337 unsigned int othercase;
4338 if ((othercase = UCD_OTHERCASE(c)) != c)
4339 {
4340 *class_uchardata++ = XCL_SINGLE;
4341 class_uchardata += PRIV(ord2utf)(othercase, class_uchardata);
4342 }
4343 }
4344 #endif /* SUPPORT_UCP */
4345
4346 }
4347 else
4348 #endif /* SUPPORT_UTF8 */
4349
4350 /* Handle a single-byte character */
4351 {
4352 classbits[c/8] |= (1 << (c&7));
4353 if ((options & PCRE_CASELESS) != 0)
4354 {
4355 c = cd->fcc[c]; /* flip case */
4356 classbits[c/8] |= (1 << (c&7));
4357 }
4358 class_charcount++;
4359 class_lastchar = c;
4360 }
4361 }
4362
4363 /* Loop until ']' reached. This "while" is the end of the "do" far above.
4364 If we are at the end of an internal nested string, revert to the outer
4365 string. */
4366
4367 while (((c = *(++ptr)) != 0 ||
4368 (nestptr != NULL &&
4369 (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&
4370 (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4371
4372 /* Check for missing terminating ']' */
4373
4374 if (c == 0)
4375 {
4376 *errorcodeptr = ERR6;
4377 goto FAILED;
4378 }
4379
4380 /* If class_charcount is 1, we saw precisely one character whose value is
4381 less than 256. As long as there were no characters >= 128 and there was no
4382 use of \p or \P, in other words, no use of any XCLASS features, we can
4383 optimize.
4384
4385 In UTF-8 mode, we can optimize the negative case only if there were no
4386 characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR
4387 operate on single-bytes characters only. This is an historical hangover.
4388 Maybe one day we can tidy these opcodes to handle multi-byte characters.
4389
4390 The optimization throws away the bit map. We turn the item into a
4391 1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.
4392 Note that OP_NOT[I] does not support multibyte characters. In the positive
4393 case, it can cause firstchar to be set. Otherwise, there can be no first
4394 char if this item is first, whatever repeat count may follow. In the case
4395 of reqchar, save the previous value for reinstating. */
4396
4397 #ifdef SUPPORT_UTF
4398 if (class_charcount == 1 && !xclass &&
4399 (!utf || !negate_class || class_lastchar < 128))
4400 #else
4401 if (class_charcount == 1)
4402 #endif
4403 {
4404 zeroreqchar = reqchar;
4405
4406 /* The OP_NOT[I] opcodes work on one-byte characters only. */
4407
4408 if (negate_class)
4409 {
4410 if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4411 zerofirstchar = firstchar;
4412 *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4413 *code++ = class_lastchar;
4414 break;
4415 }
4416
4417 /* For a single, positive character, get the value into mcbuffer, and
4418 then we can handle this with the normal one-character code. */
4419
4420 #ifdef SUPPORT_UTF8
4421 if (utf && class_lastchar > 127)
4422 mclength = PRIV(ord2utf)(class_lastchar, mcbuffer);
4423 else
4424 #endif
4425 {
4426 mcbuffer[0] = class_lastchar;
4427 mclength = 1;
4428 }
4429 goto ONE_CHAR;
4430 } /* End of 1-char optimization */
4431
4432 /* The general case - not the one-char optimization. If this is the first
4433 thing in the branch, there can be no first char setting, whatever the
4434 repeat count. Any reqchar setting must remain unchanged after any kind of
4435 repeat. */
4436
4437 if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4438 zerofirstchar = firstchar;
4439 zeroreqchar = reqchar;
4440
4441 /* If there are characters with values > 255, we have to compile an
4442 extended class, with its own opcode, unless there was a negated special
4443 such as \S in the class, and PCRE_UCP is not set, because in that case all
4444 characters > 255 are in the class, so any that were explicitly given as
4445 well can be ignored. If (when there are explicit characters > 255 that must
4446 be listed) there are no characters < 256, we can omit the bitmap in the
4447 actual compiled code. */
4448
4449 #ifdef SUPPORT_UTF
4450 if (xclass && (!should_flip_negation || (options & PCRE_UCP) != 0))
4451 #endif
4452 #ifndef COMPILE_PCRE8
4453 if (xclass && !should_flip_negation)
4454 #endif
4455 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4456 {
4457 *class_uchardata++ = XCL_END; /* Marks the end of extra data */
4458 *code++ = OP_XCLASS;
4459 code += LINK_SIZE;
4460 *code = negate_class? XCL_NOT:0;
4461
4462 /* If the map is required, move up the extra data to make room for it;
4463 otherwise just move the code pointer to the end of the extra data. */
4464
4465 if (class_charcount > 0)
4466 {
4467 *code++ |= XCL_MAP;
4468 memmove(code + (32 / sizeof(pcre_uchar)), code,
4469 IN_UCHARS(class_uchardata - code));
4470 memcpy(code, classbits, 32);
4471 code = class_uchardata + (32 / sizeof(pcre_uchar));
4472 }
4473 else code = class_uchardata;
4474
4475 /* Now fill in the complete length of the item */
4476
4477 PUT(previous, 1, code - previous);
4478 break; /* End of class handling */
4479 }
4480 #endif
4481
4482 /* If there are no characters > 255, or they are all to be included or
4483 excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
4484 whole class was negated and whether there were negative specials such as \S
4485 (non-UCP) in the class. Then copy the 32-byte map into the code vector,
4486 negating it if necessary. */
4487
4488 *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
4489 if (lengthptr == NULL) /* Save time in the pre-compile phase */
4490 {
4491 if (negate_class)
4492 for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
4493 memcpy(code, classbits, 32);
4494 }
4495 code += 32 / sizeof(pcre_uchar);
4496 break;
4497
4498
4499 /* ===================================================================*/
4500 /* Various kinds of repeat; '{' is not necessarily a quantifier, but this
4501 has been tested above. */
4502
4503 case CHAR_LEFT_CURLY_BRACKET:
4504 if (!is_quantifier) goto NORMAL_CHAR;
4505 ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorcodeptr);
4506 if (*errorcodeptr != 0) goto FAILED;
4507 goto REPEAT;
4508
4509 case CHAR_ASTERISK:
4510 repeat_min = 0;
4511 repeat_max = -1;
4512 goto REPEAT;
4513
4514 case CHAR_PLUS:
4515 repeat_min = 1;
4516 repeat_max = -1;
4517 goto REPEAT;
4518
4519 case CHAR_QUESTION_MARK:
4520 repeat_min = 0;
4521 repeat_max = 1;
4522
4523 REPEAT:
4524 if (previous == NULL)
4525 {
4526 *errorcodeptr = ERR9;
4527 goto FAILED;
4528 }
4529
4530 if (repeat_min == 0)
4531 {
4532 firstchar = zerofirstchar; /* Adjust for zero repeat */
4533 reqchar = zeroreqchar; /* Ditto */
4534 }
4535
4536 /* Remember whether this is a variable length repeat */
4537
4538 reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY;
4539
4540 op_type = 0; /* Default single-char op codes */
4541 possessive_quantifier = FALSE; /* Default not possessive quantifier */
4542
4543 /* Save start of previous item, in case we have to move it up in order to
4544 insert something before it. */
4545
4546 tempcode = previous;
4547
4548 /* If the next character is '+', we have a possessive quantifier. This
4549 implies greediness, whatever the setting of the PCRE_UNGREEDY option.
4550 If the next character is '?' this is a minimizing repeat, by default,
4551 but if PCRE_UNGREEDY is set, it works the other way round. We change the
4552 repeat type to the non-default. */
4553
4554 if (ptr[1] == CHAR_PLUS)
4555 {
4556 repeat_type = 0; /* Force greedy */
4557 possessive_quantifier = TRUE;
4558 ptr++;
4559 }
4560 else if (ptr[1] == CHAR_QUESTION_MARK)
4561 {
4562 repeat_type = greedy_non_default;
4563 ptr++;
4564 }
4565 else repeat_type = greedy_default;
4566
4567 /* If previous was a recursion call, wrap it in atomic brackets so that
4568 previous becomes the atomic group. All recursions were so wrapped in the
4569 past, but it no longer happens for non-repeated recursions. In fact, the
4570 repeated ones could be re-implemented independently so as not to need this,
4571 but for the moment we rely on the code for repeating groups. */
4572
4573 if (*previous == OP_RECURSE)
4574 {
4575 memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
4576 *previous = OP_ONCE;
4577 PUT(previous, 1, 2 + 2*LINK_SIZE);
4578 previous[2 + 2*LINK_SIZE] = OP_KET;
4579 PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4580 code += 2 + 2 * LINK_SIZE;
4581 length_prevgroup = 3 + 3*LINK_SIZE;
4582
4583 /* When actually compiling, we need to check whether this was a forward
4584 reference, and if so, adjust the offset. */
4585
4586 if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4587 {
4588 int offset = GET(cd->hwm, -LINK_SIZE);
4589 if (offset == previous + 1 - cd->start_code)
4590 PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4591 }
4592 }
4593
4594 /* Now handle repetition for the different types of item. */
4595
4596 /* If previous was a character match, abolish the item and generate a
4597 repeat item instead. If a char item has a minumum of more than one, ensure
4598 that it is set in reqchar - it might not be if a sequence such as x{3} is
4599 the first thing in a branch because the x will have gone into firstchar
4600 instead. */
4601
4602 if (*previous == OP_CHAR || *previous == OP_CHARI)
4603 {
4604 op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;
4605
4606 /* Deal with UTF characters that take up more than one character. It's
4607 easier to write this out separately than try to macrify it. Use c to
4608 hold the length of the character in bytes, plus UTF_LENGTH to flag that
4609 it's a length rather than a small character. */
4610
4611 #ifdef SUPPORT_UTF
4612 #ifdef COMPILE_PCRE8
4613 if (utf && (code[-1] & 0x80) != 0)
4614 #endif /* COMPILE_PCRE8 */
4615 #ifdef COMPILE_PCRE16
4616 if (utf && (code[-1] & 0xfc00) == 0xdc00)
4617 #endif /* COMPILE_PCRE8 */
4618 {
4619 pcre_uchar *lastchar = code - 1;
4620 BACKCHAR(lastchar);
4621 c = code - lastchar; /* Length of UTF-8 character */
4622 memcpy(utf_chars, lastchar, IN_UCHARS(c)); /* Save the char */
4623 c |= UTF_LENGTH; /* Flag c as a length */
4624 }
4625 else
4626 #endif /* SUPPORT_UTF */
4627
4628 /* Handle the case of a single charater - either with no UTF support, or
4629 with UTF disabled, or for a single character UTF character. */
4630
4631 {
4632 c = code[-1];
4633 if (repeat_min > 1) reqchar = c | req_caseopt | cd->req_varyopt;
4634 }
4635
4636 /* If the repetition is unlimited, it pays to see if the next thing on
4637 the line is something that cannot possibly match this character. If so,
4638 automatically possessifying this item gains some performance in the case
4639 where the match fails. */
4640
4641 if (!possessive_quantifier &&
4642 repeat_max < 0 &&
4643 check_auto_possessive(previous, utf, ptr + 1, options, cd))
4644 {
4645 repeat_type = 0; /* Force greedy */
4646 possessive_quantifier = TRUE;
4647 }
4648
4649 goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */
4650 }
4651
4652 /* If previous was a single negated character ([^a] or similar), we use
4653 one of the special opcodes, replacing it. The code is shared with single-
4654 character repeats by setting opt_type to add a suitable offset into
4655 repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI
4656 are currently used only for single-byte chars. */
4657
4658 else if (*previous == OP_NOT || *previous == OP_NOTI)
4659 {
4660 op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;
4661 c = previous[1];
4662 if (!possessive_quantifier &&
4663 repeat_max < 0 &&
4664 check_auto_possessive(previous, utf, ptr + 1, options, cd))
4665 {
4666 repeat_type = 0; /* Force greedy */
4667 possessive_quantifier = TRUE;
4668 }
4669 goto OUTPUT_SINGLE_REPEAT;
4670 }
4671
4672 /* If previous was a character type match (\d or similar), abolish it and
4673 create a suitable repeat item. The code is shared with single-character
4674 repeats by setting op_type to add a suitable offset into repeat_type. Note
4675 the the Unicode property types will be present only when SUPPORT_UCP is
4676 defined, but we don't wrap the little bits of code here because it just
4677 makes it horribly messy. */
4678
4679 else if (*previous < OP_EODN)
4680 {
4681 pcre_uchar *oldcode;
4682 int prop_type, prop_value;
4683 op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */
4684 c = *previous;
4685
4686 if (!possessive_quantifier &&
4687 repeat_max < 0 &&
4688 check_auto_possessive(previous, utf, ptr + 1, options, cd))
4689 {
4690 repeat_type = 0; /* Force greedy */
4691 possessive_quantifier = TRUE;
4692 }
4693
4694 OUTPUT_SINGLE_REPEAT:
4695 if (*previous == OP_PROP || *previous == OP_NOTPROP)
4696 {
4697 prop_type = previous[1];
4698 prop_value = previous[2];
4699 }
4700 else prop_type = prop_value = -1;
4701
4702 oldcode = code;
4703 code = previous; /* Usually overwrite previous item */
4704
4705 /* If the maximum is zero then the minimum must also be zero; Perl allows
4706 this case, so we do too - by simply omitting the item altogether. */
4707
4708 if (repeat_max == 0) goto END_REPEAT;
4709
4710 /*--------------------------------------------------------------------*/
4711 /* This code is obsolete from release 8.00; the restriction was finally
4712 removed: */
4713
4714 /* All real repeats make it impossible to handle partial matching (maybe
4715 one day we will be able to remove this restriction). */
4716
4717 /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
4718 /*--------------------------------------------------------------------*/
4719
4720 /* Combine the op_type with the repeat_type */
4721
4722 repeat_type += op_type;
4723
4724 /* A minimum of zero is handled either as the special case * or ?, or as
4725 an UPTO, with the maximum given. */
4726
4727 if (repeat_min == 0)
4728 {
4729 if (repeat_max == -1) *code++ = OP_STAR + repeat_type;
4730 else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type;
4731 else
4732 {
4733 *code++ = OP_UPTO + repeat_type;
4734 PUT2INC(code, 0, repeat_max);
4735 }
4736 }
4737
4738 /* A repeat minimum of 1 is optimized into some special cases. If the
4739 maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
4740 left in place and, if the maximum is greater than 1, we use OP_UPTO with
4741 one less than the maximum. */
4742
4743 else if (repeat_min == 1)
4744 {
4745 if (repeat_max == -1)
4746 *code++ = OP_PLUS + repeat_type;
4747 else
4748 {
4749 code = oldcode; /* leave previous item in place */
4750 if (repeat_max == 1) goto END_REPEAT;
4751 *code++ = OP_UPTO + repeat_type;
4752 PUT2INC(code, 0, repeat_max - 1);
4753 }
4754 }
4755
4756 /* The case {n,n} is just an EXACT, while the general case {n,m} is
4757 handled as an EXACT followed by an UPTO. */
4758
4759 else
4760 {
4761 *code++ = OP_EXACT + op_type; /* NB EXACT doesn't have repeat_type */
4762 PUT2INC(code, 0, repeat_min);
4763
4764 /* If the maximum is unlimited, insert an OP_STAR. Before doing so,
4765 we have to insert the character for the previous code. For a repeated
4766 Unicode property match, there are two extra bytes that define the
4767 required property. In UTF-8 mode, long characters have their length in
4768 c, with the UTF_LENGTH bit as a flag. */
4769
4770 if (repeat_max < 0)
4771 {
4772 #ifdef SUPPORT_UTF
4773 if (utf && (c & UTF_LENGTH) != 0)
4774 {
4775 memcpy(code, utf_chars, IN_UCHARS(c & 7));
4776 code += c & 7;
4777 }
4778 else
4779 #endif
4780 {
4781 *code++ = c;
4782 if (prop_type >= 0)
4783 {
4784 *code++ = prop_type;
4785 *code++ = prop_value;
4786 }
4787 }
4788 *code++ = OP_STAR + repeat_type;
4789 }
4790
4791 /* Else insert an UPTO if the max is greater than the min, again
4792 preceded by the character, for the previously inserted code. If the
4793 UPTO is just for 1 instance, we can use QUERY instead. */
4794
4795 else if (repeat_max != repeat_min)
4796 {
4797 #ifdef SUPPORT_UTF
4798 if (utf && (c & UTF_LENGTH) != 0)
4799 {
4800 memcpy(code, utf_chars, IN_UCHARS(c & 7));
4801 code += c & 7;
4802 }
4803 else
4804 #endif
4805 *code++ = c;
4806 if (prop_type >= 0)
4807 {
4808 *code++ = prop_type;
4809 *code++ = prop_value;
4810 }
4811 repeat_max -= repeat_min;
4812
4813 if (repeat_max == 1)
4814 {
4815 *code++ = OP_QUERY + repeat_type;
4816 }
4817 else
4818 {
4819 *code++ = OP_UPTO + repeat_type;
4820 PUT2INC(code, 0, repeat_max);
4821 }
4822 }
4823 }
4824
4825 /* The character or character type itself comes last in all cases. */
4826
4827 #ifdef SUPPORT_UTF
4828 if (utf && (c & UTF_LENGTH) != 0)
4829 {
4830 memcpy(code, utf_chars, IN_UCHARS(c & 7));
4831 code += c & 7;
4832 }
4833 else
4834 #endif
4835 *code++ = c;
4836
4837 /* For a repeated Unicode property match, there are two extra bytes that
4838 define the required property. */
4839
4840 #ifdef SUPPORT_UCP
4841 if (prop_type >= 0)
4842 {
4843 *code++ = prop_type;
4844 *code++ = prop_value;
4845 }
4846 #endif
4847 }
4848
4849 /* If previous was a character class or a back reference, we put the repeat
4850 stuff after it, but just skip the item if the repeat was {0,0}. */
4851
4852 else if (*previous == OP_CLASS ||
4853 *previous == OP_NCLASS ||
4854 #if defined SUPPORT_UTF8 || !defined COMPILE_PCRE8
4855 *previous == OP_XCLASS ||
4856 #endif
4857 *previous == OP_REF ||
4858 *previous == OP_REFI)
4859 {
4860 if (repeat_max == 0)
4861 {
4862 code = previous;
4863 goto END_REPEAT;
4864 }
4865
4866 /*--------------------------------------------------------------------*/
4867 /* This code is obsolete from release 8.00; the restriction was finally
4868 removed: */
4869
4870 /* All real repeats make it impossible to handle partial matching (maybe
4871 one day we will be able to remove this restriction). */
4872
4873 /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
4874 /*--------------------------------------------------------------------*/
4875
4876 if (repeat_min == 0 && repeat_max == -1)
4877 *code++ = OP_CRSTAR + repeat_type;
4878 else if (repeat_min == 1 && repeat_max == -1)
4879 *code++ = OP_CRPLUS + repeat_type;
4880 else if (repeat_min == 0 && repeat_max == 1)
4881 *code++ = OP_CRQUERY + repeat_type;
4882 else
4883 {
4884 *code++ = OP_CRRANGE + repeat_type;
4885 PUT2INC(code, 0, repeat_min);
4886 if (repeat_max == -1) repeat_max = 0; /* 2-byte encoding for max */
4887 PUT2INC(code, 0, repeat_max);
4888 }
4889 }
4890
4891 /* If previous was a bracket group, we may have to replicate it in certain
4892 cases. Note that at this point we can encounter only the "basic" bracket
4893 opcodes such as BRA and CBRA, as this is the place where they get converted
4894 into the more special varieties such as BRAPOS and SBRA. A test for >=
4895 OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
4896 ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow
4897 repetition of assertions, but now it does, for Perl compatibility. */
4898
4899 else if (*previous >= OP_ASSERT && *previous <= OP_COND)
4900 {
4901 register int i;
4902 int len = (int)(code - previous);
4903 pcre_uchar *bralink = NULL;
4904 pcre_uchar *brazeroptr = NULL;
4905
4906 /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
4907 we just ignore the repeat. */
4908
4909 if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
4910 goto END_REPEAT;
4911
4912 /* There is no sense in actually repeating assertions. The only potential
4913 use of repetition is in cases when the assertion is optional. Therefore,
4914 if the minimum is greater than zero, just ignore the repeat. If the
4915 maximum is not not zero or one, set it to 1. */
4916
4917 if (*previous < OP_ONCE) /* Assertion */
4918 {
4919 if (repeat_min > 0) goto END_REPEAT;
4920 if (repeat_max < 0 || repeat_max > 1) repeat_max = 1;
4921 }
4922
4923 /* The case of a zero minimum is special because of the need to stick
4924 OP_BRAZERO in front of it, and because the group appears once in the
4925 data, whereas in other cases it appears the minimum number of times. For
4926 this reason, it is simplest to treat this case separately, as otherwise
4927 the code gets far too messy. There are several special subcases when the
4928 minimum is zero. */
4929
4930 if (repeat_min == 0)
4931 {
4932 /* If the maximum is also zero, we used to just omit the group from the
4933 output altogether, like this:
4934
4935 ** if (repeat_max == 0)
4936 ** {
4937 ** code = previous;
4938 ** goto END_REPEAT;
4939 ** }
4940
4941 However, that fails when a group or a subgroup within it is referenced
4942 as a subroutine from elsewhere in the pattern, so now we stick in
4943 OP_SKIPZERO in front of it so that it is skipped on execution. As we
4944 don't have a list of which groups are referenced, we cannot do this
4945 selectively.
4946
4947 If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
4948 and do no more at this point. However, we do need to adjust any
4949 OP_RECURSE calls inside the group that refer to the group itself or any
4950 internal or forward referenced group, because the offset is from the
4951 start of the whole regex. Temporarily terminate the pattern while doing
4952 this. */
4953
4954 if (repeat_max <= 1) /* Covers 0, 1, and unlimited */
4955 {
4956 *code = OP_END;
4957 adjust_recurse(previous, 1, utf, cd, save_hwm);
4958 memmove(previous + 1, previous, IN_UCHARS(len));
4959 code++;
4960 if (repeat_max == 0)
4961 {
4962 *previous++ = OP_SKIPZERO;
4963 goto END_REPEAT;
4964 }
4965 brazeroptr = previous; /* Save for possessive optimizing */
4966 *previous++ = OP_BRAZERO + repeat_type;
4967 }
4968
4969 /* If the maximum is greater than 1 and limited, we have to replicate
4970 in a nested fashion, sticking OP_BRAZERO before each set of brackets.
4971 The first one has to be handled carefully because it's the original
4972 copy, which has to be moved up. The remainder can be handled by code
4973 that is common with the non-zero minimum case below. We have to
4974 adjust the value or repeat_max, since one less copy is required. Once
4975 again, we may have to adjust any OP_RECURSE calls inside the group. */
4976
4977 else
4978 {
4979 int offset;
4980 *code = OP_END;
4981 adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm);
4982 memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
4983 code += 2 + LINK_SIZE;
4984 *previous++ = OP_BRAZERO + repeat_type;
4985 *previous++ = OP_BRA;
4986
4987 /* We chain together the bracket offset fields that have to be
4988 filled in later when the ends of the brackets are reached. */
4989
4990 offset = (bralink == NULL)? 0 : (int)(previous - bralink);
4991 bralink = previous;
4992 PUTINC(previous, 0, offset);
4993 }
4994
4995 repeat_max--;
4996 }
4997
4998 /* If the minimum is greater than zero, replicate the group as many
4999 times as necessary, and adjust the maximum to the number of subsequent
5000 copies that we need. If we set a first char from the group, and didn't
5001 set a required char, copy the latter from the former. If there are any
5002 forward reference subroutine calls in the group, there will be entries on
5003 the workspace list; replicate these with an appropriate increment. */
5004
5005 else
5006 {
5007 if (repeat_min > 1)
5008 {
5009 /* In the pre-compile phase, we don't actually do the replication. We
5010 just adjust the length as if we had. Do some paranoid checks for
5011 potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
5012 integer type when available, otherwise double. */
5013
5014 if (lengthptr != NULL)
5015 {
5016 int delta = (repeat_min - 1)*length_prevgroup;
5017 if ((INT64_OR_DOUBLE)(repeat_min - 1)*
5018 (INT64_OR_DOUBLE)length_prevgroup >
5019 (INT64_OR_DOUBLE)INT_MAX ||
5020 OFLOW_MAX - *lengthptr < delta)
5021 {
5022 *errorcodeptr = ERR20;
5023 goto FAILED;
5024 }
5025 *lengthptr += delta;
5026 }
5027
5028 /* This is compiling for real */
5029
5030 else
5031 {
5032 if (groupsetfirstchar && reqchar < 0) reqchar = firstchar;
5033 for (i = 1; i < repeat_min; i++)
5034 {
5035 pcre_uchar *hc;
5036 pcre_uchar *this_hwm = cd->hwm;
5037 memcpy(code, previous, IN_UCHARS(len));
5038 for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5039 {
5040 PUT(cd->hwm, 0, GET(hc, 0) + len);
5041 cd->hwm += LINK_SIZE;
5042 }
5043 save_hwm = this_hwm;
5044 code += len;
5045 }
5046 }
5047 }
5048
5049 if (repeat_max > 0) repeat_max -= repeat_min;
5050 }
5051
5052 /* This code is common to both the zero and non-zero minimum cases. If
5053 the maximum is limited, it replicates the group in a nested fashion,
5054 remembering the bracket starts on a stack. In the case of a zero minimum,
5055 the first one was set up above. In all cases the repeat_max now specifies
5056 the number of additional copies needed. Again, we must remember to
5057 replicate entries on the forward reference list. */
5058
5059 if (repeat_max >= 0)
5060 {
5061 /* In the pre-compile phase, we don't actually do the replication. We
5062 just adjust the length as if we had. For each repetition we must add 1
5063 to the length for BRAZERO and for all but the last repetition we must
5064 add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
5065 paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
5066 a 64-bit integer type when available, otherwise double. */
5067
5068 if (lengthptr != NULL && repeat_max > 0)
5069 {
5070 int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
5071 2 - 2*LINK_SIZE; /* Last one doesn't nest */
5072 if ((INT64_OR_DOUBLE)repeat_max *
5073 (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
5074 > (INT64_OR_DOUBLE)INT_MAX ||
5075 OFLOW_MAX - *lengthptr < delta)
5076 {
5077 *errorcodeptr = ERR20;
5078 goto FAILED;
5079 }
5080 *lengthptr += delta;
5081 }
5082
5083 /* This is compiling for real */
5084
5085 else for (i = repeat_max - 1; i >= 0; i--)
5086 {
5087 pcre_uchar *hc;
5088 pcre_uchar *this_hwm = cd->hwm;
5089
5090 *code++ = OP_BRAZERO + repeat_type;
5091
5092 /* All but the final copy start a new nesting, maintaining the
5093 chain of brackets outstanding. */
5094
5095 if (i != 0)
5096 {
5097 int offset;
5098 *code++ = OP_BRA;
5099 offset = (bralink == NULL)? 0 : (int)(code - bralink);
5100 bralink = code;
5101 PUTINC(code, 0, offset);
5102 }
5103
5104 memcpy(code, previous, IN_UCHARS(len));
5105 for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5106 {
5107 PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
5108 cd->hwm += LINK_SIZE;
5109 }
5110 save_hwm = this_hwm;
5111 code += len;
5112 }
5113
5114 /* Now chain through the pending brackets, and fill in their length
5115 fields (which are holding the chain links pro tem). */
5116
5117 while (bralink != NULL)
5118 {
5119 int oldlinkoffset;
5120 int offset = (int)(code - bralink + 1);
5121 pcre_uchar *bra = code - offset;
5122 oldlinkoffset = GET(bra, 1);
5123 bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
5124 *code++ = OP_KET;
5125 PUTINC(code, 0, offset);
5126 PUT(bra, 1, offset);
5127 }
5128 }
5129
5130 /* If the maximum is unlimited, set a repeater in the final copy. For
5131 ONCE brackets, that's all we need to do. However, possessively repeated
5132 ONCE brackets can be converted into non-capturing brackets, as the
5133 behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
5134 deal with possessive ONCEs specially.
5135
5136 Otherwise, when we are doing the actual compile phase, check to see
5137 whether this group is one that could match an empty string. If so,
5138 convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
5139 that runtime checking can be done. [This check is also applied to ONCE
5140 groups at runtime, but in a different way.]
5141
5142 Then, if the quantifier was possessive and the bracket is not a
5143 conditional, we convert the BRA code to the POS form, and the KET code to
5144 KETRPOS. (It turns out to be convenient at runtime to detect this kind of
5145 subpattern at both the start and at the end.) The use of special opcodes
5146 makes it possible to reduce greatly the stack usage in pcre_exec(). If
5147 the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
5148
5149 Then, if the minimum number of matches is 1 or 0, cancel the possessive
5150 flag so that the default action below, of wrapping everything inside
5151 atomic brackets, does not happen. When the minimum is greater than 1,
5152 there will be earlier copies of the group, and so we still have to wrap
5153 the whole thing. */
5154
5155 else
5156 {
5157 pcre_uchar *ketcode = code - 1 - LINK_SIZE;
5158 pcre_uchar *bracode = ketcode - GET(ketcode, 1);
5159
5160 /* Convert possessive ONCE brackets to non-capturing */
5161
5162 if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
5163 possessive_quantifier) *bracode = OP_BRA;
5164
5165 /* For non-possessive ONCE brackets, all we need to do is to
5166 set the KET. */
5167
5168 if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
5169 *ketcode = OP_KETRMAX + repeat_type;
5170
5171 /* Handle non-ONCE brackets and possessive ONCEs (which have been
5172 converted to non-capturing above). */
5173
5174 else
5175 {
5176 /* In the compile phase, check for empty string matching. */
5177
5178 if (lengthptr == NULL)
5179 {
5180 pcre_uchar *scode = bracode;
5181 do
5182 {
5183 if (could_be_empty_branch(scode, ketcode, utf, cd))
5184 {
5185 *bracode += OP_SBRA - OP_BRA;
5186 break;
5187 }
5188 scode += GET(scode, 1);
5189 }
5190 while (*scode == OP_ALT);
5191 }
5192
5193 /* Handle possessive quantifiers. */
5194
5195 if (possessive_quantifier)
5196 {
5197 /* For COND brackets, we wrap the whole thing in a possessively
5198 repeated non-capturing bracket, because we have not invented POS
5199 versions of the COND opcodes. Because we are moving code along, we
5200 must ensure that any pending recursive references are updated. */
5201
5202 if (*bracode == OP_COND || *bracode == OP_SCOND)
5203 {
5204 int nlen = (int)(code - bracode);
5205 *code = OP_END;
5206 adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm);
5207 memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
5208 code += 1 + LINK_SIZE;
5209 nlen += 1 + LINK_SIZE;
5210 *bracode = OP_BRAPOS;
5211 *code++ = OP_KETRPOS;
5212 PUTINC(code, 0, nlen);
5213 PUT(bracode, 1, nlen);
5214 }
5215
5216 /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
5217
5218 else
5219 {
5220 *bracode += 1; /* Switch to xxxPOS opcodes */
5221 *ketcode = OP_KETRPOS;
5222 }
5223
5224 /* If the minimum is zero, mark it as possessive, then unset the
5225 possessive flag when the minimum is 0 or 1. */
5226
5227 if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
5228 if (repeat_min < 2) possessive_quantifier = FALSE;
5229 }
5230
5231 /* Non-possessive quantifier */
5232
5233 else *ketcode = OP_KETRMAX + repeat_type;
5234 }
5235 }
5236 }
5237
5238 /* If previous is OP_FAIL, it was generated by an empty class [] in
5239 JavaScript mode. The other ways in which OP_FAIL can be generated, that is
5240 by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat"
5241 error above. We can just ignore the repeat in JS case. */
5242
5243 else if (*previous == OP_FAIL) goto END_REPEAT;
5244
5245 /* Else there's some kind of shambles */
5246
5247 else
5248 {
5249 *errorcodeptr = ERR11;
5250 goto FAILED;
5251 }
5252
5253 /* If the character following a repeat is '+', or if certain optimization
5254 tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
5255 there are special alternative opcodes for this case. For anything else, we
5256 wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
5257 notation is just syntactic sugar, taken from Sun's Java package, but the
5258 special opcodes can optimize it.
5259
5260 Some (but not all) possessively repeated subpatterns have already been
5261 completely handled in the code just above. For them, possessive_quantifier
5262 is always FALSE at this stage.
5263
5264 Note that the repeated item starts at tempcode, not at previous, which
5265 might be the first part of a string whose (former) last char we repeated.
5266
5267 Possessifying an 'exact' quantifier has no effect, so we can ignore it. But
5268 an 'upto' may follow. We skip over an 'exact' item, and then test the
5269 length of what remains before proceeding. */
5270
5271 if (possessive_quantifier)
5272 {
5273 int len;
5274
5275 if (*tempcode == OP_TYPEEXACT)
5276 tempcode += PRIV(OP_lengths)[*tempcode] +
5277 ((tempcode[1 + IMM2_SIZE] == OP_PROP
5278 || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
5279
5280 else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5281 {
5282 tempcode += PRIV(OP_lengths)[*tempcode];
5283 #ifdef SUPPORT_UTF
5284 if (utf && HAS_EXTRALEN(tempcode[-1]))
5285 tempcode += GET_EXTRALEN(tempcode[-1]);
5286 #endif
5287 }
5288
5289 len = (int)(code - tempcode);
5290 if (len > 0) switch (*tempcode)
5291 {
5292 case OP_STAR: *tempcode = OP_POSSTAR; break;
5293 case OP_PLUS: *tempcode = OP_POSPLUS; break;
5294 case OP_QUERY: *tempcode = OP_POSQUERY; break;
5295 case OP_UPTO: *tempcode = OP_POSUPTO; break;
5296
5297 case OP_STARI: *tempcode = OP_POSSTARI; break;
5298 case OP_PLUSI: *tempcode = OP_POSPLUSI; break;
5299 case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
5300 case OP_UPTOI: *tempcode = OP_POSUPTOI; break;
5301
5302 case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break;
5303 case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break;
5304 case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
5305 case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break;
5306
5307 case OP_NOTSTARI: *tempcode = OP_NOTPOSSTARI; break;
5308 case OP_NOTPLUSI: *tempcode = OP_NOTPOSPLUSI; break;
5309 case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
5310 case OP_NOTUPTOI: *tempcode = OP_NOTPOSUPTOI; break;
5311
5312 case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break;
5313 case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break;
5314 case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
5315 case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break;
5316
5317 /* Because we are moving code along, we must ensure that any
5318 pending recursive references are updated. */
5319
5320 default:
5321 *code = OP_END;
5322 adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm);
5323 memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
5324 code += 1 + LINK_SIZE;
5325 len += 1 + LINK_SIZE;
5326 tempcode[0] = OP_ONCE;
5327 *code++ = OP_KET;
5328 PUTINC(code, 0, len);
5329 PUT(tempcode, 1, len);
5330 break;
5331 }
5332 }
5333
5334 /* In all case we no longer have a previous item. We also set the
5335 "follows varying string" flag for subsequently encountered reqchars if
5336 it isn't already set and we have just passed a varying length item. */
5337
5338 END_REPEAT:
5339 previous = NULL;
5340 cd->req_varyopt |= reqvary;
5341 break;
5342
5343
5344 /* ===================================================================*/
5345 /* Start of nested parenthesized sub-expression, or comment or lookahead or
5346 lookbehind or option setting or condition or all the other extended
5347 parenthesis forms. */
5348
5349 case CHAR_LEFT_PARENTHESIS:
5350 newoptions = options;
5351 skipbytes = 0;
5352 bravalue = OP_CBRA;
5353 save_hwm = cd->hwm;
5354 reset_bracount = FALSE;
5355
5356 /* First deal with various "verbs" that can be introduced by '*'. */
5357
5358 if (*(++ptr) == CHAR_ASTERISK &&
5359 ((cd->ctypes[ptr[1]] & ctype_letter) != 0 || ptr[1] == ':'))
5360 {
5361 int i, namelen;
5362 int arglen = 0;
5363 const char *vn = verbnames;
5364 const pcre_uchar *name = ptr + 1;
5365 const pcre_uchar *arg = NULL;
5366 previous = NULL;
5367 while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};
5368 namelen = (int)(ptr - name);
5369
5370 /* It appears that Perl allows any characters whatsoever, other than
5371 a closing parenthesis, to appear in arguments, so we no longer insist on
5372 letters, digits, and underscores. */
5373
5374 if (*ptr == CHAR_COLON)
5375 {
5376 arg = ++ptr;
5377 while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5378 arglen = (int)(ptr - arg);
5379 }
5380
5381 if (*ptr != CHAR_RIGHT_PARENTHESIS)
5382 {
5383 *errorcodeptr = ERR60;
5384 goto FAILED;
5385 }
5386
5387 /* Scan the table of verb names */
5388
5389 for (i = 0; i < verbcount; i++)
5390 {
5391 if (namelen == verbs[i].len &&
5392 STRNCMP_UC_C8(name, vn, namelen) == 0)
5393 {
5394 /* Check for open captures before ACCEPT and convert it to
5395 ASSERT_ACCEPT if in an assertion. */
5396
5397 if (verbs[i].op == OP_ACCEPT)
5398 {
5399 open_capitem *oc;
5400 if (arglen != 0)
5401 {
5402 *errorcodeptr = ERR59;
5403 goto FAILED;
5404 }
5405 cd->had_accept = TRUE;
5406 for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5407 {
5408 *code++ = OP_CLOSE;
5409 PUT2INC(code, 0, oc->number);
5410 }
5411 *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5412
5413 /* Do not set firstchar after *ACCEPT */
5414 if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
5415 }
5416
5417 /* Handle other cases with/without an argument */
5418
5419 else if (arglen == 0)
5420 {
5421 if (verbs[i].op < 0) /* Argument is mandatory */
5422 {
5423 *errorcodeptr = ERR66;
5424 goto FAILED;
5425 }
5426 *code = verbs[i].op;
5427 if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;
5428 }
5429
5430 else
5431 {
5432 if (verbs[i].op_arg < 0) /* Argument is forbidden */
5433 {
5434 *errorcodeptr = ERR59;
5435 goto FAILED;
5436 }
5437 *code = verbs[i].op_arg;
5438 if (*code++ == OP_THEN_ARG) cd->external_flags |= PCRE_HASTHEN;
5439 *code++ = arglen;
5440 memcpy(code, arg, IN_UCHARS(arglen));
5441 code += arglen;
5442 *code++ = 0;
5443 }
5444
5445 break; /* Found verb, exit loop */
5446 }
5447
5448 vn += verbs[i].len + 1;
5449 }
5450
5451 if (i < verbcount) continue; /* Successfully handled a verb */
5452 *errorcodeptr = ERR60; /* Verb not recognized */
5453 goto FAILED;
5454 }
5455
5456 /* Deal with the extended parentheses; all are introduced by '?', and the
5457 appearance of any of them means that this is not a capturing group. */
5458
5459 else if (*ptr == CHAR_QUESTION_MARK)
5460 {
5461 int i, set, unset, namelen;
5462 int *optset;
5463 const pcre_uchar *name;
5464 pcre_uchar *slot;
5465
5466 switch (*(++ptr))
5467 {
5468 case CHAR_NUMBER_SIGN: /* Comment; skip to ket */
5469 ptr++;
5470 while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5471 if (*ptr == 0)
5472 {
5473 *errorcodeptr = ERR18;
5474 goto FAILED;
5475 }
5476 continue;
5477
5478
5479 /* ------------------------------------------------------------ */
5480 case CHAR_VERTICAL_LINE: /* Reset capture count for each branch */
5481 reset_bracount = TRUE;
5482 /* Fall through */
5483
5484 /* ------------------------------------------------------------ */
5485 case CHAR_COLON: /* Non-capturing bracket */
5486 bravalue = OP_BRA;
5487 ptr++;
5488 break;
5489
5490
5491 /* ------------------------------------------------------------ */
5492 case CHAR_LEFT_PARENTHESIS:
5493 bravalue = OP_COND; /* Conditional group */
5494
5495 /* A condition can be an assertion, a number (referring to a numbered
5496 group), a name (referring to a named group), or 'R', referring to
5497 recursion. R<digits> and R&name are also permitted for recursion tests.
5498
5499 There are several syntaxes for testing a named group: (?(name)) is used
5500 by Python; Perl 5.10 onwards uses (?(<name>) or (?('name')).
5501
5502 There are two unfortunate ambiguities, caused by history. (a) 'R' can
5503 be the recursive thing or the name 'R' (and similarly for 'R' followed
5504 by digits), and (b) a number could be a name that consists of digits.
5505 In both cases, we look for a name first; if not found, we try the other
5506 cases. */
5507
5508 /* For conditions that are assertions, check the syntax, and then exit
5509 the switch. This will take control down to where bracketed groups,
5510 including assertions, are processed. */
5511
5512 if (ptr[1] == CHAR_QUESTION_MARK && (ptr[2] == CHAR_EQUALS_SIGN ||
5513 ptr[2] == CHAR_EXCLAMATION_MARK || ptr[2] == CHAR_LESS_THAN_SIGN))
5514 break;
5515
5516 /* Most other conditions use OP_CREF (a couple change to OP_RREF
5517 below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */
5518
5519 code[1+LINK_SIZE] = OP_CREF;
5520 skipbytes = 1+IMM2_SIZE;
5521 refsign = -1;
5522
5523 /* Check for a test for recursion in a named group. */
5524
5525 if (ptr[1] == CHAR_R && ptr[2] == CHAR_AMPERSAND)
5526 {
5527 terminator = -1;
5528 ptr += 2;
5529 code[1+LINK_SIZE] = OP_RREF; /* Change the type of test */
5530 }
5531
5532 /* Check for a test for a named group's having been set, using the Perl
5533 syntax (?(<name>) or (?('name') */
5534
5535 else if (ptr[1] == CHAR_LESS_THAN_SIGN)
5536 {
5537 terminator = CHAR_GREATER_THAN_SIGN;
5538 ptr++;
5539 }
5540 else if (ptr[1] == CHAR_APOSTROPHE)
5541 {
5542 terminator = CHAR_APOSTROPHE;
5543 ptr++;
5544 }
5545 else
5546 {
5547 terminator = 0;
5548 if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
5549 }
5550
5551 /* We now expect to read a name; any thing else is an error */
5552
5553 if ((cd->ctypes[ptr[1]] & ctype_word) == 0)
5554 {
5555 ptr += 1; /* To get the right offset */
5556 *errorcodeptr = ERR28;
5557 goto FAILED;
5558 }
5559
5560 /* Read the name, but also get it as a number if it's all digits */
5561
5562 recno = 0;
5563 name = ++ptr;
5564 while ((cd->ctypes[*ptr] & ctype_word) != 0)
5565 {
5566 if (recno >= 0)
5567 recno = (IS_DIGIT(*ptr))? recno * 10 + *ptr - CHAR_0 : -1;
5568 ptr++;
5569 }
5570 namelen = (int)(ptr - name);
5571
5572 if ((terminator > 0 && *ptr++ != terminator) ||
5573 *ptr++ != CHAR_RIGHT_PARENTHESIS)
5574 {
5575 ptr--; /* Error offset */
5576 *errorcodeptr = ERR26;
5577 goto FAILED;
5578 }
5579
5580 /* Do no further checking in the pre-compile phase. */
5581
5582 if (lengthptr != NULL) break;
5583
5584 /* In the real compile we do the work of looking for the actual
5585 reference. If the string started with "+" or "-" we require the rest to
5586 be digits, in which case recno will be set. */
5587
5588 if (refsign > 0)
5589 {
5590 if (recno <= 0)
5591 {
5592 *errorcodeptr = ERR58;
5593 goto FAILED;
5594 }
5595 recno = (refsign == CHAR_MINUS)?
5596 cd->bracount - recno + 1 : recno +cd->bracount;
5597 if (recno <= 0 || recno > cd->final_bracount)
5598 {
5599 *errorcodeptr = ERR15;
5600 goto FAILED;
5601 }
5602 PUT2(code, 2+LINK_SIZE, recno);
5603 break;
5604 }
5605
5606 /* Otherwise (did not start with "+" or "-"), start by looking for the
5607 name. If we find a name, add one to the opcode to change OP_CREF or
5608 OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,
5609 except they record that the reference was originally to a name. The
5610 information is used to check duplicate names. */
5611
5612 slot = cd->name_table;
5613 for (i = 0; i < cd->names_found; i++)
5614 {
5615 if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0) break;
5616 slot += cd->name_entry_size;
5617 }
5618
5619 /* Found a previous named subpattern */
5620
5621 if (i < cd->names_found)
5622 {
5623 recno = GET2(slot, 0);
5624 PUT2(code, 2+LINK_SIZE, recno);
5625 code[1+LINK_SIZE]++;
5626 }
5627
5628 /* Search the pattern for a forward reference */
5629
5630 else if ((i = find_parens(cd, name, namelen,
5631 (options & PCRE_EXTENDED) != 0, utf)) > 0)
5632 {
5633 PUT2(code, 2+LINK_SIZE, i);
5634 code[1+LINK_SIZE]++;
5635 }
5636
5637 /* If terminator == 0 it means that the name followed directly after
5638 the opening parenthesis [e.g. (?(abc)...] and in this case there are
5639 some further alternatives to try. For the cases where terminator != 0
5640 [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have
5641 now checked all the possibilities, so give an error. */
5642
5643 else if (terminator != 0)
5644 {
5645 *errorcodeptr = ERR15;
5646 goto FAILED;
5647 }
5648
5649 /* Check for (?(R) for recursion. Allow digits after R to specify a
5650 specific group number. */
5651
5652 else if (*name == CHAR_R)
5653 {
5654 recno = 0;
5655 for (i = 1; i < namelen; i++)
5656 {
5657 if (!IS_DIGIT(name[i]))
5658 {
5659 *errorcodeptr = ERR15;
5660 goto FAILED;
5661 }
5662 recno = recno * 10 + name[i] - CHAR_0;
5663 }
5664 if (recno == 0) recno = RREF_ANY;
5665 code[1+LINK_SIZE] = OP_RREF; /* Change test type */
5666 PUT2(code, 2+LINK_SIZE, recno);
5667 }
5668
5669 /* Similarly, check for the (?(DEFINE) "condition", which is always
5670 false. */
5671
5672 else if (namelen == 6 && STRNCMP_UC_C8(name, STRING_DEFINE, 6) == 0)
5673 {
5674 code[1+LINK_SIZE] = OP_DEF;
5675 skipbytes = 1;
5676 }
5677
5678 /* Check for the "name" actually being a subpattern number. We are
5679 in the second pass here, so final_bracount is set. */
5680
5681 else if (recno > 0 && recno <= cd->final_bracount)
5682 {
5683 PUT2(code, 2+LINK_SIZE, recno);
5684 }
5685
5686 /* Either an unidentified subpattern, or a reference to (?(0) */
5687
5688 else
5689 {
5690 *errorcodeptr = (recno == 0)? ERR35: ERR15;
5691 goto FAILED;
5692 }
5693 break;
5694
5695
5696 /* ------------------------------------------------------------ */
5697 case CHAR_EQUALS_SIGN: /* Positive lookahead */
5698 bravalue = OP_ASSERT;
5699 cd->assert_depth += 1;
5700 ptr++;
5701 break;
5702
5703
5704 /* ------------------------------------------------------------ */
5705 case CHAR_EXCLAMATION_MARK: /* Negative lookahead */
5706 ptr++;
5707 if (*ptr == CHAR_RIGHT_PARENTHESIS) /* Optimize (?!) */
5708 {
5709 *code++ = OP_FAIL;
5710 previous = NULL;
5711 continue;
5712 }
5713 bravalue = OP_ASSERT_NOT;
5714 cd->assert_depth += 1;
5715 break;
5716
5717
5718 /* ------------------------------------------------------------ */
5719 case CHAR_LESS_THAN_SIGN: /* Lookbehind or named define */
5720 switch (ptr[1])
5721 {
5722 case CHAR_EQUALS_SIGN: /* Positive lookbehind */
5723 bravalue = OP_ASSERTBACK;
5724 cd->assert_depth += 1;
5725 ptr += 2;
5726 break;
5727
5728 case CHAR_EXCLAMATION_MARK: /* Negative lookbehind */
5729 bravalue = OP_ASSERTBACK_NOT;
5730 cd->assert_depth += 1;
5731 ptr += 2;
5732 break;
5733
5734 default: /* Could be name define, else bad */
5735 if ((cd->ctypes[ptr[1]] & ctype_word) != 0) goto DEFINE_NAME;
5736 ptr++; /* Correct offset for error */
5737 *errorcodeptr = ERR24;
5738 goto FAILED;
5739 }
5740 break;
5741
5742
5743 /* ------------------------------------------------------------ */
5744 case CHAR_GREATER_THAN_SIGN: /* One-time brackets */
5745 bravalue = OP_ONCE;
5746 ptr++;
5747 break;
5748
5749
5750 /* ------------------------------------------------------------ */
5751 case CHAR_C: /* Callout - may be followed by digits; */
5752 previous_callout = code; /* Save for later completion */
5753 after_manual_callout = 1; /* Skip one item before completing */
5754 *code++ = OP_CALLOUT;
5755 {
5756 int n = 0;
5757 ptr++;
5758 while(IS_DIGIT(*ptr))
5759 n = n * 10 + *ptr++ - CHAR_0;
5760 if (*ptr != CHAR_RIGHT_PARENTHESIS)
5761 {
5762 *errorcodeptr = ERR39;
5763 goto FAILED;
5764 }
5765 if (n > 255)
5766 {
5767 *errorcodeptr = ERR38;
5768 goto FAILED;
5769 }
5770 *code++ = n;
5771 PUT(code, 0, (int)(ptr - cd->start_pattern + 1)); /* Pattern offset */
5772 PUT(code, LINK_SIZE, 0); /* Default length */
5773 code += 2 * LINK_SIZE;
5774 }
5775 previous = NULL;
5776 continue;
5777
5778
5779 /* ------------------------------------------------------------ */
5780 case CHAR_P: /* Python-style named subpattern handling */
5781 if (*(++ptr) == CHAR_EQUALS_SIGN ||
5782 *ptr == CHAR_GREATER_THAN_SIGN) /* Reference or recursion */
5783 {
5784 is_recurse = *ptr == CHAR_GREATER_THAN_SIGN;
5785 terminator = CHAR_RIGHT_PARENTHESIS;
5786 goto NAMED_REF_OR_RECURSE;
5787 }
5788 else if (*ptr != CHAR_LESS_THAN_SIGN) /* Test for Python-style defn */
5789 {
5790 *errorcodeptr = ERR41;
5791 goto FAILED;
5792 }
5793 /* Fall through to handle (?P< as (?< is handled */
5794
5795
5796 /* ------------------------------------------------------------ */
5797 DEFINE_NAME: /* Come here from (?< handling */
5798 case CHAR_APOSTROPHE:
5799 {
5800 terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
5801 CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
5802 name = ++ptr;
5803
5804 while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5805 namelen = (int)(ptr - name);
5806
5807 /* In the pre-compile phase, just do a syntax check. */
5808
5809 if (lengthptr != NULL)
5810 {
5811 if (*ptr != terminator)
5812 {
5813 *errorcodeptr = ERR42;
5814 goto FAILED;
5815 }
5816 if (cd->names_found >= MAX_NAME_COUNT)
5817 {
5818 *errorcodeptr = ERR49;
5819 goto FAILED;
5820 }
5821 if (namelen + 3 > cd->name_entry_size)
5822 {
5823 cd->name_entry_size = namelen + 3;
5824 if (namelen > MAX_NAME_SIZE)
5825 {
5826 *errorcodeptr = ERR48;
5827 goto FAILED;
5828 }
5829 }
5830 }
5831
5832 /* In the real compile, create the entry in the table, maintaining
5833 alphabetical order. Duplicate names for different numbers are
5834 permitted only if PCRE_DUPNAMES is set. Duplicate names for the same
5835 number are always OK. (An existing number can be re-used if (?|
5836 appears in the pattern.) In either event, a duplicate name results in
5837 a duplicate entry in the table, even if the number is the same. This
5838 is because the number of names, and hence the table size, is computed
5839 in the pre-compile, and it affects various numbers and pointers which
5840 would all have to be modified, and the compiled code moved down, if
5841 duplicates with the same number were omitted from the table. This
5842 doesn't seem worth the hassle. However, *different* names for the
5843 same number are not permitted. */
5844
5845 else
5846 {
5847 BOOL dupname = FALSE;
5848 slot = cd->name_table;
5849
5850 for (i = 0; i < cd->names_found; i++)
5851 {
5852 int crc = memcmp(name, slot+2, namelen);
5853 if (crc == 0)
5854 {
5855 if (slot[2+namelen] == 0)
5856 {
5857 if (GET2(slot, 0) != cd->bracount + 1 &&
5858 (options & PCRE_DUPNAMES) == 0)
5859 {
5860 *errorcodeptr = ERR43;
5861 goto FAILED;
5862 }
5863 else dupname = TRUE;
5864 }
5865 else crc = -1; /* Current name is a substring */
5866 }
5867
5868 /* Make space in the table and break the loop for an earlier
5869 name. For a duplicate or later name, carry on. We do this for
5870 duplicates so that in the simple case (when ?(| is not used) they
5871 are in order of their numbers. */
5872
5873 if (crc < 0)
5874 {
5875 memmove(slot + cd->name_entry_size, slot,
5876 IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
5877 break;
5878 }
5879
5880 /* Continue the loop for a later or duplicate name */
5881
5882 slot += cd->name_entry_size;
5883 }
5884
5885 /* For non-duplicate names, check for a duplicate number before
5886 adding the new name. */
5887
5888 if (!dupname)
5889 {
5890 pcre_uchar *cslot = cd->name_table;
5891 for (i = 0; i < cd->names_found; i++)
5892 {
5893 if (cslot != slot)
5894 {
5895 if (GET2(cslot, 0) == cd->bracount + 1)
5896 {
5897 *errorcodeptr = ERR65;
5898 goto FAILED;
5899 }
5900 }
5901 else i--;
5902 cslot += cd->name_entry_size;
5903 }
5904 }
5905
5906 PUT2(slot, 0, cd->bracount + 1);
5907 memcpy(slot + 2, name, IN_UCHARS(namelen));
5908 slot[2 + namelen] = 0;
5909 }
5910 }
5911
5912 /* In both pre-compile and compile, count the number of names we've
5913 encountered. */
5914
5915 cd->names_found++;
5916 ptr++; /* Move past > or ' */
5917 goto NUMBERED_GROUP;
5918
5919
5920 /* ------------------------------------------------------------ */
5921 case CHAR_AMPERSAND: /* Perl recursion/subroutine syntax */
5922 terminator = CHAR_RIGHT_PARENTHESIS;
5923 is_recurse = TRUE;
5924 /* Fall through */
5925
5926 /* We come here from the Python syntax above that handles both
5927 references (?P=name) and recursion (?P>name), as well as falling
5928 through from the Perl recursion syntax (?&name). We also come here from
5929 the Perl \k<name> or \k'name' back reference syntax and the \k{name}
5930 .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
5931
5932 NAMED_REF_OR_RECURSE:
5933 name = ++ptr;
5934 while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5935 namelen = (int)(ptr - name);
5936
5937 /* In the pre-compile phase, do a syntax check. We used to just set
5938 a dummy reference number, because it was not used in the first pass.
5939 However, with the change of recursive back references to be atomic,
5940 we have to look for the number so that this state can be identified, as
5941 otherwise the incorrect length is computed. If it's not a backwards
5942 reference, the dummy number will do. */
5943
5944 if (lengthptr != NULL)
5945 {
5946 const pcre_uchar *temp;
5947
5948 if (namelen == 0)
5949 {
5950 *errorcodeptr = ERR62;
5951 goto FAILED;
5952 }
5953 if (*ptr != terminator)
5954 {
5955 *errorcodeptr = ERR42;
5956 goto FAILED;
5957 }
5958 if (namelen > MAX_NAME_SIZE)
5959 {
5960 *errorcodeptr = ERR48;
5961 goto FAILED;
5962 }
5963
5964 /* The name table does not exist in the first pass, so we cannot
5965 do a simple search as in the code below. Instead, we have to scan the
5966 pattern to find the number. It is important that we scan it only as
5967 far as we have got because the syntax of named subpatterns has not
5968 been checked for the rest of the pattern, and find_parens() assumes
5969 correct syntax. In any case, it's a waste of resources to scan
5970 further. We stop the scan at the current point by temporarily
5971 adjusting the value of cd->endpattern. */
5972
5973 temp = cd->end_pattern;
5974 cd->end_pattern = ptr;
5975 recno = find_parens(cd, name, namelen,
5976 (options & PCRE_EXTENDED) != 0, utf);
5977 cd->end_pattern = temp;
5978 if (recno < 0) recno = 0; /* Forward ref; set dummy number */
5979 }
5980
5981 /* In the real compile, seek the name in the table. We check the name
5982 first, and then check that we have reached the end of the name in the
5983 table. That way, if the name that is longer than any in the table,
5984 the comparison will fail without reading beyond the table entry. */
5985
5986 else
5987 {
5988 slot = cd->name_table;
5989 for (i = 0; i < cd->names_found; i++)
5990 {
5991 if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
5992 slot[2+namelen] == 0)
5993 break;
5994 slot += cd->name_entry_size;
5995 }
5996
5997 if (i < cd->names_found) /* Back reference */
5998 {
5999 recno = GET2(slot, 0);
6000 }
6001 else if ((recno = /* Forward back reference */
6002 find_parens(cd, name, namelen,
6003 (options & PCRE_EXTENDED) != 0, utf)) <= 0)
6004 {
6005 *errorcodeptr = ERR15;
6006 goto FAILED;
6007 }
6008 }
6009
6010 /* In both phases, we can now go to the code than handles numerical
6011 recursion or backreferences. */
6012
6013 if (is_recurse) goto HANDLE_RECURSION;
6014 else goto HANDLE_REFERENCE;
6015
6016
6017 /* ------------------------------------------------------------ */
6018 case CHAR_R: /* Recursion */
6019 ptr++; /* Same as (?0) */
6020 /* Fall through */
6021
6022
6023 /* ------------------------------------------------------------ */
6024 case CHAR_MINUS: case CHAR_PLUS: /* Recursion or subroutine */
6025 case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:
6026 case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
6027 {
6028 const pcre_uchar *called;
6029 terminator = CHAR_RIGHT_PARENTHESIS;
6030
6031 /* Come here from the \g<...> and \g'...' code (Oniguruma
6032 compatibility). However, the syntax has been checked to ensure that
6033 the ... are a (signed) number, so that neither ERR63 nor ERR29 will
6034 be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
6035 ever be taken. */
6036
6037 HANDLE_NUMERICAL_RECURSION:
6038
6039 if ((refsign = *ptr) == CHAR_PLUS)
6040 {
6041 ptr++;
6042 if (!IS_DIGIT(*ptr))
6043 {
6044 *errorcodeptr = ERR63;
6045 goto FAILED;
6046 }
6047 }
6048 else if (refsign == CHAR_MINUS)
6049 {
6050 if (!IS_DIGIT(ptr[1]))
6051 goto OTHER_CHAR_AFTER_QUERY;
6052 ptr++;
6053 }
6054
6055 recno = 0;
6056 while(IS_DIGIT(*ptr))
6057 recno = recno * 10 + *ptr++ - CHAR_0;
6058
6059 if (*ptr != terminator)
6060 {
6061 *errorcodeptr = ERR29;
6062 goto FAILED;
6063 }
6064
6065 if (refsign == CHAR_MINUS)
6066 {
6067 if (recno == 0)
6068 {
6069 *errorcodeptr = ERR58;
6070 goto FAILED;
6071 }
6072 recno = cd->bracount - recno + 1;
6073 if (recno <= 0)
6074 {
6075 *errorcodeptr = ERR15;
6076 goto FAILED;
6077 }
6078 }
6079 else if (refsign == CHAR_PLUS)
6080 {
6081 if (recno == 0)
6082 {
6083 *errorcodeptr = ERR58;
6084 goto FAILED;
6085 }
6086 recno += cd->bracount;
6087 }
6088
6089 /* Come here from code above that handles a named recursion */
6090
6091 HANDLE_RECURSION:
6092
6093 previous = code;
6094 called = cd->start_code;
6095
6096 /* When we are actually compiling, find the bracket that is being
6097 referenced. Temporarily end the regex in case it doesn't exist before
6098 this point. If we end up with a forward reference, first check that
6099 the bracket does occur later so we can give the error (and position)
6100 now. Then remember this forward reference in the workspace so it can
6101 be filled in at the end. */
6102
6103 if (lengthptr == NULL)
6104 {
6105 *code = OP_END;
6106 if (recno != 0)
6107 called = PRIV(find_bracket)(cd->start_code, utf, recno);
6108
6109 /* Forward reference */
6110
6111 if (called == NULL)
6112 {
6113 if (find_parens(cd, NULL, recno,
6114 (options & PCRE_EXTENDED) != 0, utf) < 0)
6115 {
6116 *errorcodeptr = ERR15;
6117 goto FAILED;
6118 }
6119
6120 /* Fudge the value of "called" so that when it is inserted as an
6121 offset below, what it actually inserted is the reference number
6122 of the group. Then remember the forward reference. */
6123
6124 called = cd->start_code + recno;
6125 PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
6126 }
6127
6128 /* If not a forward reference, and the subpattern is still open,
6129 this is a recursive call. We check to see if this is a left
6130 recursion that could loop for ever, and diagnose that case. We
6131 must not, however, do this check if we are in a conditional
6132 subpattern because the condition might be testing for recursion in
6133 a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
6134 Forever loops are also detected at runtime, so those that occur in
6135 conditional subpatterns will be picked up then. */
6136
6137 else if (GET(called, 1) == 0 && cond_depth <= 0 &&
6138 could_be_empty(called, code, bcptr, utf, cd))
6139 {
6140 *errorcodeptr = ERR40;
6141 goto FAILED;
6142 }
6143 }
6144
6145 /* Insert the recursion/subroutine item. */
6146
6147 *code = OP_RECURSE;
6148 PUT(code, 1, (int)(called - cd->start_code));
6149 code += 1 + LINK_SIZE;
6150 }
6151
6152 /* Can't determine a first byte now */
6153
6154 if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
6155 continue;
6156
6157
6158 /* ------------------------------------------------------------ */
6159 default: /* Other characters: check option setting */
6160 OTHER_CHAR_AFTER_QUERY:
6161 set = unset = 0;
6162 optset = &set;
6163
6164 while (*ptr != CHAR_RIGHT_PARENTHESIS && *ptr != CHAR_COLON)
6165 {
6166 switch (*ptr++)
6167 {
6168 case CHAR_MINUS: optset = &unset; break;
6169
6170 case CHAR_J: /* Record that it changed in the external options */
6171 *optset |= PCRE_DUPNAMES;
6172 cd->external_flags |= PCRE_JCHANGED;
6173 break;
6174
6175 case CHAR_i: *optset |= PCRE_CASELESS; break;
6176 case CHAR_m: *optset |= PCRE_MULTILINE; break;
6177 case CHAR_s: *optset |= PCRE_DOTALL; break;
6178 case CHAR_x: *optset |= PCRE_EXTENDED; break;
6179 case CHAR_U: *optset |= PCRE_UNGREEDY; break;
6180 case CHAR_X: *optset |= PCRE_EXTRA; break;
6181
6182 default: *errorcodeptr = ERR12;
6183 ptr--; /* Correct the offset */
6184 goto FAILED;
6185 }
6186 }
6187
6188 /* Set up the changed option bits, but don't change anything yet. */
6189
6190 newoptions = (options | set) & (~unset);
6191
6192 /* If the options ended with ')' this is not the start of a nested
6193 group with option changes, so the options change at this level. If this
6194 item is right at the start of the pattern, the options can be
6195 abstracted and made external in the pre-compile phase, and ignored in
6196 the compile phase. This can be helpful when matching -- for instance in
6197 caseless checking of required bytes.
6198
6199 If the code pointer is not (cd->start_code + 1 + LINK_SIZE), we are
6200 definitely *not* at the start of the pattern because something has been
6201 compiled. In the pre-compile phase, however, the code pointer can have
6202 that value after the start, because it gets reset as code is discarded
6203 during the pre-compile. However, this can happen only at top level - if
6204 we are within parentheses, the starting BRA will still be present. At
6205 any parenthesis level, the length value can be used to test if anything
6206 has been compiled at that level. Thus, a test for both these conditions
6207 is necessary to ensure we correctly detect the start of the pattern in
6208 both phases.
6209
6210 If we are not at the pattern start, reset the greedy defaults and the
6211 case value for firstchar and reqchar. */
6212
6213 if (*ptr == CHAR_RIGHT_PARENTHESIS)
6214 {
6215 if (code == cd->start_code + 1 + LINK_SIZE &&
6216 (lengthptr == NULL || *lengthptr == 2 + 2*LINK_SIZE))
6217 {
6218 cd->external_options = newoptions;
6219 }
6220 else
6221 {
6222 greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
6223 greedy_non_default = greedy_default ^ 1;
6224 req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
6225 }
6226
6227 /* Change options at this level, and pass them back for use
6228 in subsequent branches. */
6229
6230 *optionsptr = options = newoptions;
6231 previous = NULL; /* This item can't be repeated */
6232 continue; /* It is complete */
6233 }
6234
6235 /* If the options ended with ':' we are heading into a nested group
6236 with possible change of options. Such groups are non-capturing and are
6237 not assertions of any kind. All we need to do is skip over the ':';
6238 the newoptions value is handled below. */
6239
6240 bravalue = OP_BRA;
6241 ptr++;
6242 } /* End of switch for character following (? */
6243 } /* End of (? handling */
6244
6245 /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
6246 is set, all unadorned brackets become non-capturing and behave like (?:...)
6247 brackets. */
6248
6249 else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)
6250 {
6251 bravalue = OP_BRA;
6252 }
6253
6254 /* Else we have a capturing group. */
6255
6256 else
6257 {
6258 NUMBERED_GROUP:
6259 cd->bracount += 1;
6260 PUT2(code, 1+LINK_SIZE, cd->bracount);
6261 skipbytes = IMM2_SIZE;
6262 }
6263
6264 /* Process nested bracketed regex. Assertions used not to be repeatable,
6265 but this was changed for Perl compatibility, so all kinds can now be
6266 repeated. We copy code into a non-register variable (tempcode) in order to
6267 be able to pass its address because some compilers complain otherwise. */
6268
6269 previous = code; /* For handling repetition */
6270 *code = bravalue;
6271 tempcode = code;
6272 tempreqvary = cd->req_varyopt; /* Save value before bracket */
6273 tempbracount = cd->bracount; /* Save value before bracket */
6274 length_prevgroup = 0; /* Initialize for pre-compile phase */
6275
6276 if (!compile_regex(
6277 newoptions, /* The complete new option state */
6278 &tempcode, /* Where to put code (updated) */
6279 &ptr, /* Input pointer (updated) */
6280 errorcodeptr, /* Where to put an error message */
6281 (bravalue == OP_ASSERTBACK ||
6282 bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
6283 reset_bracount, /* True if (?| group */
6284 skipbytes, /* Skip over bracket number */
6285 cond_depth +
6286 ((bravalue == OP_COND)?1:0), /* Depth of condition subpatterns */
6287 &subfirstchar, /* For possible first char */
6288 &subreqchar, /* For possible last char */
6289 bcptr, /* Current branch chain */
6290 cd, /* Tables block */
6291 (lengthptr == NULL)? NULL : /* Actual compile phase */
6292 &length_prevgroup /* Pre-compile phase */
6293 ))
6294 goto FAILED;
6295
6296 /* If this was an atomic group and there are no capturing groups within it,
6297 generate OP_ONCE_NC instead of OP_ONCE. */
6298
6299 if (bravalue == OP_ONCE && cd->bracount <= tempbracount)
6300 *code = OP_ONCE_NC;
6301
6302 if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
6303 cd->assert_depth -= 1;
6304
6305 /* At the end of compiling, code is still pointing to the start of the
6306 group, while tempcode has been updated to point past the end of the group.
6307 The pattern pointer (ptr) is on the bracket.
6308
6309 If this is a conditional bracket, check that there are no more than
6310 two branches in the group, or just one if it's a DEFINE group. We do this
6311 in the real compile phase, not in the pre-pass, where the whole group may
6312 not be available. */
6313
6314 if (bravalue == OP_COND && lengthptr == NULL)
6315 {
6316 pcre_uchar *tc = code;
6317 int condcount = 0;
6318
6319 do {
6320 condcount++;
6321 tc += GET(tc,1);
6322 }
6323 while (*tc != OP_KET);
6324
6325 /* A DEFINE group is never obeyed inline (the "condition" is always
6326 false). It must have only one branch. */
6327
6328 if (code[LINK_SIZE+1] == OP_DEF)
6329 {
6330 if (condcount > 1)
6331 {
6332 *errorcodeptr = ERR54;
6333 goto FAILED;
6334 }
6335 bravalue = OP_DEF; /* Just a flag to suppress char handling below */
6336 }
6337
6338 /* A "normal" conditional group. If there is just one branch, we must not
6339 make use of its firstchar or reqchar, because this is equivalent to an
6340 empty second branch. */
6341
6342 else
6343 {
6344 if (condcount > 2)
6345 {
6346 *errorcodeptr = ERR27;
6347 goto FAILED;
6348 }
6349 if (condcount == 1) subfirstchar = subreqchar = REQ_NONE;
6350 }
6351 }
6352
6353 /* Error if hit end of pattern */
6354
6355 if (*ptr != CHAR_RIGHT_PARENTHESIS)
6356 {
6357 *errorcodeptr = ERR14;
6358 goto FAILED;
6359 }
6360
6361 /* In the pre-compile phase, update the length by the length of the group,
6362 less the brackets at either end. Then reduce the compiled code to just a
6363 set of non-capturing brackets so that it doesn't use much memory if it is
6364 duplicated by a quantifier.*/
6365
6366 if (lengthptr != NULL)
6367 {
6368 if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
6369 {
6370 *errorcodeptr = ERR20;
6371 goto FAILED;
6372 }
6373 *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
6374 code++; /* This already contains bravalue */
6375 PUTINC(code, 0, 1 + LINK_SIZE);
6376 *code++ = OP_KET;
6377 PUTINC(code, 0, 1 + LINK_SIZE);
6378 break; /* No need to waste time with special character handling */
6379 }
6380
6381 /* Otherwise update the main code pointer to the end of the group. */
6382
6383 code = tempcode;
6384
6385 /* For a DEFINE group, required and first character settings are not
6386 relevant. */
6387
6388 if (bravalue == OP_DEF) break;
6389
6390 /* Handle updating of the required and first characters for other types of
6391 group. Update for normal brackets of all kinds, and conditions with two
6392 branches (see code above). If the bracket is followed by a quantifier with
6393 zero repeat, we have to back off. Hence the definition of zeroreqchar and
6394 zerofirstchar outside the main loop so that they can be accessed for the
6395 back off. */
6396
6397 zeroreqchar = reqchar;
6398 zerofirstchar = firstchar;
6399 groupsetfirstchar = FALSE;
6400
6401 if (bravalue >= OP_ONCE)
6402 {
6403 /* If we have not yet set a firstchar in this branch, take it from the
6404 subpattern, remembering that it was set here so that a repeat of more
6405 than one can replicate it as reqchar if necessary. If the subpattern has
6406 no firstchar, set "none" for the whole branch. In both cases, a zero
6407 repeat forces firstchar to "none". */
6408
6409 if (firstchar == REQ_UNSET)
6410 {
6411 if (subfirstchar >= 0)
6412 {
6413 firstchar = subfirstchar;
6414 groupsetfirstchar = TRUE;
6415 }
6416 else firstchar = REQ_NONE;
6417 zerofirstchar = REQ_NONE;
6418 }
6419
6420 /* If firstchar was previously set, convert the subpattern's firstchar
6421 into reqchar if there wasn't one, using the vary flag that was in
6422 existence beforehand. */
6423
6424 else if (subfirstchar >= 0 && subreqchar < 0)
6425 subreqchar = subfirstchar | tempreqvary;
6426
6427 /* If the subpattern set a required byte (or set a first byte that isn't
6428 really the first byte - see above), set it. */
6429
6430 if (subreqchar >= 0) reqchar = subreqchar;
6431 }
6432
6433 /* For a forward assertion, we take the reqchar, if set. This can be
6434 helpful if the pattern that follows the assertion doesn't set a different
6435 char. For example, it's useful for /(?=abcde).+/. We can't set firstchar
6436 for an assertion, however because it leads to incorrect effect for patterns
6437 such as /(?=a)a.+/ when the "real" "a" would then become a reqchar instead
6438 of a firstchar. This is overcome by a scan at the end if there's no
6439 firstchar, looking for an asserted first char. */
6440
6441 else if (bravalue == OP_ASSERT && subreqchar >= 0) reqchar = subreqchar;
6442 break; /* End of processing '(' */
6443
6444
6445 /* ===================================================================*/
6446 /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
6447 are arranged to be the negation of the corresponding OP_values in the
6448 default case when PCRE_UCP is not set. For the back references, the values
6449 are ESC_REF plus the reference number. Only back references and those types
6450 that consume a character may be repeated. We can test for values between
6451 ESC_b and ESC_Z for the latter; this may have to change if any new ones are
6452 ever created. */
6453
6454 case CHAR_BACKSLASH:
6455 tempptr = ptr;
6456 c = check_escape(&ptr, errorcodeptr, cd->bracount, options, FALSE);
6457 if (*errorcodeptr != 0) goto FAILED;
6458
6459 if (c < 0)
6460 {
6461 if (-c == ESC_Q) /* Handle start of quoted string */
6462 {
6463 if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
6464 ptr += 2; /* avoid empty string */
6465 else inescq = TRUE;
6466 continue;
6467 }
6468
6469 if (-c == ESC_E) continue; /* Perl ignores an orphan \E */
6470
6471 /* For metasequences that actually match a character, we disable the
6472 setting of a first character if it hasn't already been set. */
6473
6474 if (firstchar == REQ_UNSET && -c > ESC_b && -c < ESC_Z)
6475 firstchar = REQ_NONE;
6476
6477 /* Set values to reset to if this is followed by a zero repeat. */
6478
6479 zerofirstchar = firstchar;
6480 zeroreqchar = reqchar;
6481
6482 /* \g<name> or \g'name' is a subroutine call by name and \g<n> or \g'n'
6483 is a subroutine call by number (Oniguruma syntax). In fact, the value
6484 -ESC_g is returned only for these cases. So we don't need to check for <
6485 or ' if the value is -ESC_g. For the Perl syntax \g{n} the value is
6486 -ESC_REF+n, and for the Perl syntax \g{name} the result is -ESC_k (as
6487 that is a synonym for a named back reference). */
6488
6489 if (-c == ESC_g)
6490 {
6491 const pcre_uchar *p;
6492 save_hwm = cd->hwm; /* Normally this is set when '(' is read */
6493 terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
6494 CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
6495
6496 /* These two statements stop the compiler for warning about possibly
6497 unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
6498 fact, because we actually check for a number below, the paths that
6499 would actually be in error are never taken. */
6500
6501 skipbytes = 0;
6502 reset_bracount = FALSE;
6503
6504 /* Test for a name */
6505
6506 if (ptr[1] != CHAR_PLUS && ptr[1] != CHAR_MINUS)
6507 {
6508 BOOL isnumber = TRUE;
6509 for (p = ptr + 1; *p != 0 && *p != terminator; p++)
6510 {
6511 if ((cd->ctypes[*p] & ctype_digit) == 0) isnumber = FALSE;
6512 if ((cd->ctypes[*p] & ctype_word) == 0) break;
6513 }
6514 if (*p != terminator)
6515 {
6516 *errorcodeptr = ERR57;
6517 break;
6518 }
6519 if (isnumber)
6520 {
6521 ptr++;
6522 goto HANDLE_NUMERICAL_RECURSION;
6523 }
6524 is_recurse = TRUE;
6525 goto NAMED_REF_OR_RECURSE;
6526 }
6527
6528 /* Test a signed number in angle brackets or quotes. */
6529
6530 p = ptr + 2;
6531 while (IS_DIGIT(*p)) p++;
6532 if (*p != terminator)
6533 {
6534 *errorcodeptr = ERR57;
6535 break;
6536 }
6537 ptr++;
6538 goto HANDLE_NUMERICAL_RECURSION;
6539 }
6540
6541 /* \k<name> or \k'name' is a back reference by name (Perl syntax).
6542 We also support \k{name} (.NET syntax). */
6543
6544 if (-c == ESC_k)
6545 {
6546 if ((ptr[1] != CHAR_LESS_THAN_SIGN &&
6547 ptr[1] != CHAR_APOSTROPHE && ptr[1] != CHAR_LEFT_CURLY_BRACKET))
6548 {
6549 *errorcodeptr = ERR69;
6550 break;
6551 }
6552 is_recurse = FALSE;
6553 terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
6554 CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?
6555 CHAR_APOSTROPHE : CHAR_RIGHT_CURLY_BRACKET;
6556 goto NAMED_REF_OR_RECURSE;
6557 }
6558
6559 /* Back references are handled specially; must disable firstchar if
6560 not set to cope with cases like (?=(\w+))\1: which would otherwise set
6561 ':' later. */
6562
6563 if (-c >= ESC_REF)
6564 {
6565 open_capitem *oc;
6566 recno = -c - ESC_REF;
6567
6568 HANDLE_REFERENCE: /* Come here from named backref handling */
6569 if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
6570 previous = code;
6571 *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
6572 PUT2INC(code, 0, recno);
6573 cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6574 if (recno > cd->top_backref) cd->top_backref = recno;
6575
6576 /* Check to see if this back reference is recursive, that it, it
6577 is inside the group that it references. A flag is set so that the
6578 group can be made atomic. */
6579
6580 for (oc = cd->open_caps; oc != NULL; oc = oc->next)
6581 {
6582 if (oc->number == recno)
6583 {
6584 oc->flag = TRUE;
6585 break;
6586 }
6587 }
6588 }
6589
6590 /* So are Unicode property matches, if supported. */
6591
6592 #ifdef SUPPORT_UCP
6593 else if (-c == ESC_P || -c == ESC_p)
6594 {
6595 BOOL negated;
6596 int pdata;
6597 int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
6598 if (ptype < 0) goto FAILED;
6599 previous = code;
6600 *code++ = ((-c == ESC_p) != negated)? OP_PROP : OP_NOTPROP;
6601 *code++ = ptype;
6602 *code++ = pdata;
6603 }
6604 #else
6605
6606 /* If Unicode properties are not supported, \X, \P, and \p are not
6607 allowed. */
6608
6609 else if (-c == ESC_X || -c == ESC_P || -c == ESC_p)
6610 {
6611 *errorcodeptr = ERR45;
6612 goto FAILED;
6613 }
6614 #endif
6615
6616 /* For the rest (including \X when Unicode properties are supported), we
6617 can obtain the OP value by negating the escape value in the default
6618 situation when PCRE_UCP is not set. When it *is* set, we substitute
6619 Unicode property tests. */
6620
6621 else
6622 {
6623 #ifdef SUPPORT_UCP
6624 if (-c >= ESC_DU && -c <= ESC_wu)
6625 {
6626 nestptr = ptr + 1; /* Where to resume */
6627 ptr = substitutes[-c - ESC_DU] - 1; /* Just before substitute */
6628 }
6629 else
6630 #endif
6631 /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE
6632 so that it works in DFA mode and in lookbehinds. */
6633
6634 {
6635 previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;
6636 *code++ = (!utf && c == -ESC_C)? OP_ALLANY : -c;
6637 }
6638 }
6639 continue;
6640 }
6641
6642 /* We have a data character whose value is in c. In UTF-8 mode it may have
6643 a value > 127. We set its repres